var xhrObj=false;
function showWait()
{
	if(divid)getEl(divid).innerHTML  = "<img src='images/status.gif'>"
	//status="waiting...";
}

/* XHR Processing functions */
var iRemoteProcedure=0;
var tRemoteProcedures={};

function RemoteProcedure(sUrl,fnOnLoad,iMethod,params,fnOnPatience)
{
	this.req=null;
	this.fnOnLoad=null;
	
	this.id=++iRemoteProcedure;
	tRemoteProcedures[this.id]=this;
	this.timer=null;
	this.call(sUrl,fnOnLoad,iMethod,params,fnOnPatience);
}

RemoteProcedure.prototype.call=function(url,fnOnLoad,iMethod,params,fnOnPatience)
{
	if(xhrObj && xhrObj.readyState!=0)
	{
		xhrObj.abort();
	}
	this.fnOnLoad=fnOnLoad;
	this.fnOnPatience=fnOnPatience;
	
	this.timer=window.setTimeout('tRemoteProcedures['+this.id+'].showPatience()',500);
	//var sRand=(url.indexOf('?')==-1?'?':'&')+'Rand='+Math.random();
	var sRand=(url.indexOf('?')==-1?'?':'&')+'Rand=7873611043781034';

	if (window.XMLHttpRequest)
	{
		this.req=new XMLHttpRequest();
		this.req.onreadystatechange=new Function('tRemoteProcedures['+this.id+'].processReqChange()');
		//this.req.onreadystatechange=new Function('tRemoteProcedures['+this.id+'].processReqChange()');
		
		//if iMethod ==1 then post else 0 - get
		if (iMethod==0)
		{
			//get method
			this.req.open("GET",url+sRand,true);
			this.req.send(null);
		}
		else 
		{
			//post method
			this.req.open("POST", url+sRand, true);	
			this.req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			this.req.setRequestHeader("Content-length", params.length);
			this.req.setRequestHeader("Connection", "close");
			this.req.send(params);	   
		}
	}
	else if (window.ActiveXObject)
	{
		this.req=new ActiveXObject("Microsoft.XMLHTTP");
		if (this.req)
		{
			this.req.onreadystatechange=new Function('tRemoteProcedures['+this.id+'].processReqChange()');
			//if iMethod ==1 then post else 0 - get
			if (iMethod==0)
			{
				//get  method
				this.req.open("GET",url+sRand,true);
				this.req.send();
			}
			else 
			{
				//post method
				this.req.open("POST", url+sRand, true);	
				this.req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				this.req.setRequestHeader("Content-length", params.length);
				this.req.setRequestHeader("Connection", "close");
				this.req.send(params);	   
			}
		}
	}
}

RemoteProcedure.prototype.showPatience=function()
{
	this.clearTimeout();
	if(this.fnOnPatience)
		this.fnOnPatience();
}

RemoteProcedure.prototype.clearTimeout=function()
{
	if (this.timer!=null){
		window.clearTimeout(this.timer);
		this.timer=null;
	}
}

RemoteProcedure.prototype.showPatience=function()
{
	this.clearTimeout();
	if(this.fnOnPatience)
		this.fnOnPatience();
}
	
RemoteProcedure.prototype.clearTimeout=function()
{
	if (this.timer!=null)
	{
		window.clearTimeout(this.timer);
		this.timer=null;
	}
}



RemoteProcedure.prototype.processReqChange=function()
{
	if (this.req.readyState==4)
	{ 	
		// 4 means "loaded"
		this.clearTimeout();
		if (this.req.status==200 || this.req.status==0 ){ // 200 means "OK"
			if(this.fnOnLoad)
				this.fnOnLoad(this.req);//this.fnOnLoad(this.req.responseXML);
		}
		else
			alert("Error " + this.req.status + "\n" + this.req.statusText + "\n\n" + this.req.responseText.replace(/\r\n/g,''));
	}
}

/* DOM Processing functions */
function getEl(id){return document.getElementById(id)}

function chk_auth(url) 
{  
	//var usr =document.form1.username.value;
	//var pass =document.form1.password.value;
	var usr=getEl("username").value;
	var pass =getEl("password").value; 
	var params= "username="+usr +"&password="+pass;
	if(req)
	{
		var sRand=url+'?Rand='+Math.random();
		req.open("POST", sRand, true);	
		req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		req.setRequestHeader("Content-length", params.length);
		req.setRequestHeader("Connection", "close");
		req.onreadystatechange = processReqChange;
		req.send(params);	   
	}
}
