<!--
function ajax(){
	this.request_url = '';
	this.target_id = '';
	this.result = '';
	this.mode = 'async';
	this.is_async_mode = 1;
	this.request_object = null;
	this.request_mode = 'GET';
	this.cookie = '';
	this.referer = '';
	
	this.set_header_cookie = function(cookie){
		this.cookie = cookie;
	}
	
	this.set_header_referer = function(referer){
		this.referer = referer;
	}
	// Get request url
	this.get_url = function(){
		var my_url = this.request_url;
		// Prevent AJAX URL caching
		my_url += '&_no_cache=' + new Date().getTime();
		return my_url;
	}
	// Get requested data
	this.get_result = function(){
		if (this.is_async_mode){
			return "this function is not supported in asynchronous mode";
		}
		return this.result;		
	}
	// Set request url
	this.set_url = function(url){
		this.request_url = url;
	}
	// Set a div to post responseText
	this.set_target = function(targetid){
		this.target_id = targetid;		
	}
	// Set request mode
	this.set_request_mode = function(method){
		var re = /post/i;
		if (re.test(method)){
			this.request_mode = 'POST';
		}else{
			this.request_mode = 'GET';
		}
	}
	// Set request mode to synchronous
	this.sync_mode = function(){
		this.is_async_mode = 0;
		this.mode = 'sync';
	}
	// Set request mode to asynchronous
	this.async_mode = function(){
		this.is_async_mode = 1;
		this.mode = 'async';
	}
	// Create a httprequest object
	this.create_request_object = function (){
		var myObj = null;
		try{
			myObj = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e1) {
			try{
				myObj = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e2){			
				myObj = null;
			}
		}	
		if(!myObj && typeof XMLHttpRequest != "undefined"){
			myObj = new XMLHttpRequest();
		}
		this.request_object = myObj;
	}
	// Process asynchronous get requests
	this.async_http_request_get = function(){
		var mode = this.is_async_mode ? true : false;
		var req = this.request_object;
		var targetid = this.target_id;
		var request_mode = this.request_mode;		
		this.request_object.onreadystatechange = function() {				
			// only if req shows "loaded"
			if (req.readyState == 4) {
				// request is complete
				var result;			
				if (req.status == 200) {
	    				// sub data back into the document
	    				result = req.responseText;
				} else {
	    				result = "<p class=error>AJAX error: " + req.status + ' ' + req.statusText + "</p>";
				}
				// clear the request object so that a new request can be launched
				this.request_object = null;
				this.result = result;
				if (targetid && document.getElementById(targetid)){
					document.getElementById(targetid).innerHTML = result;
				}				
    			}
		}
		this.request_object.open(request_mode,this.get_url(),mode);
    		this.request_object.send(null);
	}
	
	// WORK IN PROGRESS
	// Process asynchronous post requests
	this.async_http_request_post = function(){
	}	
	this.async_http_request = function (){		
		var request_mode = this.request_mode;
		if (request_mode == 'POST'){
			this.async_http_request_post();
		}else{
			this.async_http_request_get();
		}	
	}
	// Process synchronous get requests
	this.sync_http_request_get = function(){
		var request_mode = this.request_mode;
		var mode = this.is_async_mode ? true : false;		
		this.request_object.open(request_mode,this.get_url(),mode)
		this.request_object.send(null);
		var targetid = this.target_id;
		var result;
		if (this.request_object.status == 200){
			result = this.request_object.responseText;
		}else{
			result = "<p class=error>Problem: " + url + " - " + myObj.statusText + "</p>\n";
		}
		this.result = result;
		if (targetid){
			document.getElementById(targetid).innerHTML = result;
		}
	}
	
	// WORK IN PROGRESS
	// Process synchronous post requests
	this.sync_http_request_post = function(){
	}	
	this.sync_http_request = function(){
		var request_mode = this.request_mode;		
		if (request_mode == 'POST'){
			this.sync_http_request_post();
		}else{
			this.sync_http_request_get();
		}
	}	
	this.send_http_request = function(){
		var mode = this.is_async_mode ? true : false;		
		if (mode == true){
			this.async_http_request();
		}else{
			this.sync_http_request();
		}		
	}
	// Fetch http request data
	this.request_data = function(){	
		if (this.request_object){
			alert("Please wait for the current request to finish loading.");
		}else{		
			this.create_request_object();
			this.send_http_request();
		}
	}	
	this.get_data = function(){
		this.set_request_mode("GET");
		this.request_data();
	}
	this.post_data = function(){
		this.set_request_mode("POST");
		this.request_data();
	}
	
	// WORK IN PROGRESS
	this.sync_post_data = function(url){
		this.sync_mode();
		this.set_url(url);
		this.get_data();
		return this.get_result();
	}
	// WORK IN PROGRESS
	this.sync_post_data_target = function(url,target){
		this.sync_mode();
		this.set_url(url);
		this.set_target(target);
		this.get_data();
	}
	
	this.sync_get_data = function(url){
		this.sync_mode();
		this.set_url(url);
		this.get_data();
		return this.get_result();
	}
	this.sync_get_data_target = function(url,target){
		this.sync_mode();
		this.set_url(url);
		this.set_target(target);
		this.get_data();		
	}	
	this.async_get_data = function(url,target){
		this.async_mode();
		this.set_url(url);
		this.set_target(target);
		this.get_data();
	}
	// WORK IN PROGRESS
	this.async_post_data = function(url,target){
	}	
	
	// Need to add code to set cookie and referer
}

///////////////////////////////////////////////////////////////////////////////////////////////
// AJAX/HttpRequest Methods for simple GETs
/*
function sendHttpRequest_old(url){
	var xmlObj = createRequestObj();
	xmlObj.open("GET",url,false);
	xmlObj.send(null);
	return xmlObj;
}
function getRequestData_old(url){	
	var result, myObj;
	myObj = sendHttpRequest(url);
	if (myObj.status == 200) {
	   result = myObj.responseText;
	}
	else {
	   result = "<p class=error>Problem: " + url + " - " + myObj.statusText + "</p>\n";
	}
	return result;
}
*/

function getRequestData(url){	
	var ajx = new ajax();
	return ajx.sync_get_data(url);		
}
function subRequestData(id,url){
	var ajx = new ajax();
	ajx.sync_get_data_target(url,id);	
}

function getRequestDataAsync(url,id){
	var ajx = new ajax();
	ajx.async_get_data(url,id);
}

// Basic AJAX/HttpRequest object

function createRequestObj(){
	var myObj = null;
	try{
		myObj = new ActiveXObject("Msxml2.XMLHTTP");
	} catch(e1) {
		try{
			myObj = new ActiveXObject("Microsoft.XMLHTTP");
		} catch(e2){			
			myObj = null;
		}
	}	
	if(!myObj && typeof XMLHttpRequest != "undefined"){
		myObj = new XMLHttpRequest();
	}
	return myObj;
}


// AJAX/HttpRequest Methods for POSTS

// FIXME: it would be better to use asynchronous requests, but that 
// requires saving state in some globals, which could get very messy with
// multiple AJAX elements in a single page.  

// AJAX forms must use this for their submit button:
// <input type="button" value="Submit" onclick="javascript:subRequestData_Post(this.parentNode,destination_div_id,post_url);">

function sendHttpRequest_Post(url,postdata){
	var xmlObj = createRequestObj();
	xmlObj.open("POST",url,false);
	xmlObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlObj.setRequestHeader("Content-length", postdata.length);
	xmlObj.setRequestHeader("Connection", "close");
      	xmlObj.send(postdata);
	return xmlObj;
}
function getRequestData_Post(obj,url) {
	var result, myObj;
	var postdata = "";
	var f = obj;
	//var f = document.myform;
	for (var i=0; i < f.elements.length; i++) {
            var formel = f.elements[i];
	    if (i > 0) { postdata = postdata + "&"; }
            postdata = postdata + formel.name + "=" + encodeURI( formel.value );
	}
	myObj = sendHttpRequest_Post(url,postdata);
	if (myObj.status == 200) {
	   result = myObj.responseText;
	}
	else {
	   result = "<p class=error>Problem: " + url + " - " + myObj.statusText + "</p>\n";
	}	
	return result;
}
function subRequestData_Post(form,id,url){
	var data = getRequestData_Post(form,url);
	var loc = document.getElementById(id);
        loc.innerHTML = data;
}
//-->

