AJAX=function(strLoading){
    this.Init(strLoading);
}

AJAX.prototype.request="";
AJAX.prototype.strLoading;
AJAX.prototype.Init=_Init;
AJAX.prototype.loading=false;
AJAX.prototype.GetElement=_GetElement;
AJAX.prototype.GetRequest=_GetRequest;
AJAX.prototype.SendAndLoad=_SendAndLoad;
AJAX.prototype.Loading=function(){};
AJAX.prototype.Loaded=function(){};
AJAX.prototype.Interactive=function(){};
AJAX.prototype.SendAndLoadText=_LoadText;
AJAX.prototype.SendAndLoadScript=_LoadScript;

AJAX.prototype.Upload=_Upload;

function _Init(strLoading){
    this.strLoading=strLoading;
    this.request=this.GetRequest();
}

function _GetElement(ele){
    if (ele && typeof ele == "string") return document.getElementById(ele);
	return ele;
}

function _GetRequest(){
	if(window.XMLHttpRequest){
		this.request=new XMLHttpRequest();
		return new XMLHttpRequest();
	}else if(window.ActiveXObject){
		var msxmls = new Array('Msxml2.XMLHTTP.5.0','Msxml2.XMLHTTP.4.0','Msxml2.XMLHTTP.3.0','Msxml2.XMLHTTP','Microsoft.XMLHTTP');
		for (var i = 0; i < msxmls.length; i++){
			try{
				this.request=new ActiveXObject(msxmls[i]);
				return new ActiveXObject(msxmls[i]);
			}catch (e){
				alert(e);	
			}
		}
	}
	throw new Error("Could not instantiate XMLHttpRequest");
}

function _SendAndLoad(url){
	var method=(arguments.length>1)?"POST":"GET";
	var instance=this;
	if(instance.loading){
		var strConfirm="Data is now being proccessed. \nDo you want to cancel the process?";
		var choice=confirm(strConfirm);
		if(!choice) return;
	}
	instance.loading=true;
	
	var now=new Date();
	if(url.split("?").length>1)url+="&ext="+now.getSeconds()+Math.random()*300;
	else url+="?ext="+now.getSeconds()+Math.random()*300;	
	var data=(arguments.length>1)?arguments[1]:null;	

	var strData="tmp=1";
    for(var i in data){
        strData+="&"+i+"="+escape(encodeURI(data[i]));
    }    

	instance.request.open(method.toUpperCase(),url,true);
	instance.request.onreadystatechange=function(){
		switch(instance.request.readyState){
			case 1:
				instance.Loading();
				break;
			case 2:
				instance.Loaded();
				break;
			case 3:
				instance.Interactive();
				break;
			case 4:
				instance.Complete(instance.request.status,instance.request.statusText,instance.request.responseText,instance.request.responseXML);
				instance.loading=false;
				//document.getElementById(responseLabel).innerHTML=instance.request.responseText;
				break;
		}
	}
	
	if(method.toLowerCase()=="post"){
		instance.request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		instance.request.setRequestHeader("Content-length",data.length);
		instance.request.setRequestHeader("Connection","close");
	}
	instance.request.send(strData);
}

function _LoadText(url, responseLabel){
	var instance=this;
	instance.GetElement(responseLabel).innerHTML=instance.strLoading;
	instance.Complete=function(status, statusText, responseText, responseXML){
		this.GetElement(responseLabel).innerHTML=responseText;
	}
	if(arguments.length==2) instance.SendAndLoad(url);
	else instance.SendAndLoad(url, arguments[2]);
}

function _LoadScript(url){
	var instance=this;
	instance.Complete=function(status, statusText, responseText, responseXML){
		eval(responseText);
	}
	if(arguments.length==1) instance.SendAndLoad(url);
	else instance.SendAndLoad(url, arguments[1]);
}


function _Upload(url,form, ctr){	
	var instance=this;
	if(instance.loading){
		alert("Data is now being sent, please wait");
		return;
	}
	instance.loading=true;
	var i=0;
	var sep=(url.split("?").length>1)?"&":"?";
	var urlSend=url+sep+ctr+"="+instance.GetElement(ctr).value;
	var objForm = instance.GetElement(form);
	objForm.action=url;
	objForm.submit();
	timer=window.setInterval(function(){
		instance.request.onreadystatechange=function(){
			if(instance.request.readyState==4){
				instance.loading=false;
				var responded=instance.request.responseText;
				if(responded.indexOf("completed") !=-1) {
					clearInterval(timer);
					instance.loading=false;
				}
			}
		}
		instance.request.open("GET",urlSend);
		instance.request.send(null);
	},1000);
}