/**
 * @author michael
 */

var _AJAXBUSY_ = false;	
var ajaxObjArray = new Array();

function ajaxCallRetry() {
	if(_AJAXBUSY_) setTimeout("ajaxCallRetry()",100);
	else {
		var e = ajaxObjArray.pop();
		e.ajaxCall();
	}
}

function ajaxObj(file, type, func, params) {		
		this.xmlhttp = false; // ajax cache
		this.file = file;
		this.type = type;
		this.func = func;
		this.params = params;
		this.returnContent = null;
		ajaxObjArray.push(this);

		this.ajaxCall = function(){			
			if(_AJAXBUSY_) { setTimeout("ajaxCallRetry()",100); return 0; }
			_AJAXBUSY_ = true;
			if(ajaxObjArray.length>0)
				ajaxObjArray.pop();
				
			//Clear our fetching variable
			
			xmlhttp = false;
			//Try to create active x object
			try {
				xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
			} 
			catch (e) {
				try {
					xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
				} 
				catch (E) {
					xmlhttp = false;
				}
			}
			if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
				xmlhttp = new XMLHttpRequest();
			}
			
			
			
			//Nullify the XMLHttpRequest
			if (type == 'GET') {
				var url = file+'?'+params;
				xmlhttp.open(type, url, true);
				xmlhttp.onreadystatechange = function(){
					if (xmlhttp.readyState == 1) {
					
					//Check if it is ready to recieve data 
					}
					else 
						if (xmlhttp.readyState == 4) {
							//Make sure there is something in the content variable
							returnContent = xmlhttp.responseText;
							_AJAXBUSY_ = false;
							func(returnContent);
						}
				}
				xmlhttp.send(null);
			}
			else {
				if (type == 'POST') {
					xmlhttp.open(type, file, true);
					xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
					xmlhttp.setRequestHeader("Content-length", params.length);
					xmlhttp.setRequestHeader("Connection", "close");
					xmlhttp.send(params);
					
					xmlhttp.onreadystatechange = function(){
						if (xmlhttp.readyState == 1) {
						
						//Check if it is ready to recieve data 
						}
						else 
							if (xmlhttp.readyState == 4) {
								//Make sure there is something in the content variable
								
								returnContent = xmlhttp.responseText;
								_AJAXBUSY_ = false;
								func(returnContent);
								//The content data which has been retrieved
								
							}
						
					}
				}
			}
			return;
		}
	}
