//////////////////////////////////////////////////////////////////////////////
// zajlik.hu (c) 1999-2007 – ajax class
//////////////////////////////////////////////////////////////////////////////
// class: zajAjax
// written by: hontalan /aron budinszky - hontalan@gmail.com/
// version: 0.9
// known issues: some problems with IE 6 and earlier - it cannot call itself, 
// and processing of POST requests is quite slow (see below)...
//////////////////////////////////////////////////////////////////////////////
// copyright notice: use of this class is permitted, but requires a notice to
// be sent to webmester@zajlik.hu. (in other words, i just want to know if you
// found it and want to use it...thanks :))
//////////////////////////////////////////////////////////////////////////////
// IE5/6 bug: the processing time is really slow, and IE cannot send a POST
// request to the same file that calls it. So example.php cannot call example.php
// with a POST request!!!
//////////////////////////////////////////////////////////////////////////////
/* usage:


*/
//////////////////////////////////////////////////////////////////////////////
// zajAjax class
var zajobj = new zajAjax();

function zajAjax(){
	// define variables
	var isProcessing = false;
	var myUrl = '';
	var myDiv = '';
	var myForm = '';
	var myUrlQueue = new Array();
	var myDivQueue = new Array();
	var myFormQueue = new Array();
	var myFunctionQueue = new Array();
	var zajhttp = zajCreateRequestObject();	
	// define member functions
	this.addGetRequest = zajAjaxAddGetRequest;
	this.addPostRequest = zajAjaxAddPostRequest;

	/////////////////////////////////////////
	// Add a GET request to the queue
	function zajAjaxAddGetRequest(url, divid, processfunction){		
		// Add to queue
		myUrlQueue.push(url);
		myDivQueue.push(divid);
		myFormQueue.push('');
		myFunctionQueue.push(processfunction);

		// If processing, then kick / otherwise start
		if(isProcessing == true) return false;
		else zajAjaxStartNextRequest();
	}
	/////////////////////////////////////////
	// Start a GET request
	function zajAjaxStartGetRequest(){
		// Start processing
		isProcessing = true;
		zajhttp.onreadystatechange = zajAjaxProcessRequest;
		zajhttp.open('get', myUrl);
		zajhttp.send(null);
	}

	/////////////////////////////////////////
	// Add a POST request to the queue
	function zajAjaxAddPostRequest(url,formid,divid,processfunction){	
		// Add to queue
		myUrlQueue.push(url);
		myDivQueue.push(divid);
		myFormQueue.push(formid);
		myFunctionQueue.push(processfunction);
		
		// If processing, then kick / otherwise start
		if(isProcessing == true) return false;
		else zajAjaxStartNextRequest();
	}
	/////////////////////////////////////////
	// Start a POST request
	function zajAjaxStartPostRequest(){
		// Generate POST query
		parameters = zajGeneratePostQuery(myForm);
		// Start processing
		isProcessing = true;
		zajhttp.onreadystatechange = zajAjaxProcessRequest;
		zajhttp.open('POST', myUrl, true);
		zajhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		zajhttp.setRequestHeader("Content-length", parameters.length);
		zajhttp.setRequestHeader("Connection", "close");
		zajhttp.send(parameters);
	}
	
	/////////////////////////////////////////
	// Process the result of a query
	function zajAjaxProcessRequest(){
    	if(zajhttp.readyState == 4){
			if(myDiv != '') document.getElementById(myDiv).innerHTML = zajhttp.responseText;
			isProcessing = false;
			rtext = zajhttp.responseText;
			// Recreate object for stupid IE
			if(navigator.appName == "Microsoft Internet Explorer") zajhttp = zajCreateRequestObject();
			
			// Now send to a processing function
			if(myFunction != null){
				myFunction(rtext);
			}
			// Now start other requests if there are any...
			zajAjaxStartNextRequest();
        }
	}

	/////////////////////////////////////////
	// Get next item in queue
	function zajAjaxStartNextRequest(){
		// if no items left, then stop
		if(myUrlQueue.length <= 0) return false;
		// Get next item in array
		myUrl = myUrlQueue.shift();
		myDiv = myDivQueue.shift();
		myForm = myFormQueue.shift();
		myFunction = myFunctionQueue.shift();
		// now start processing
		if(myForm == "") zajAjaxStartGetRequest();
		else zajAjaxStartPostRequest();
	}
}

// end of ajax class
////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////
// other ajax functions

function zajCreateRequestObject(){
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer") ro = new ActiveXObject("Microsoft.XMLHTTP");
    else ro = new XMLHttpRequest();
    return ro;
}
function zajGeneratePostQuery(formid){
	var querystring = "absolutelynothing=asdf";
	for(i = 0; i < document.getElementById(formid).length; i++){
		// if the element is a checkbox
		if(document.getElementById(formid).elements[i].type=="checkbox" || document.getElementById(formid).elements[i].type=="radio"){
			// only add if checked
			if(document.getElementById(formid).elements[i].checked){
				querystring = querystring+"&"+document.getElementById(formid).elements[i].name+"="+document.getElementById(formid).elements[i].value;
			}
		}
		else{
			// replace problematic characters
			var qs = document.getElementById(formid).elements[i].value;
			qs = zajMakePostCompatible(qs);
			querystring = querystring+"&"+document.getElementById(formid).elements[i].name+"="+qs;
		}
	}
	return querystring;
}
function zajMakePostCompatible(str){
	// exchange all chars to acceptable ones...
	str = str.replace(/&/g, "%26");
	str = str.replace(/\+/g, "%2b");
	return str;
}
