var rpc_js_loaded = true;

function asyncrpcrequest(xmlHttp, url, handler, attrArray)
{
	rpcrequest(xmlHttp, url, handler, attrArray, true)
}

function syncrpcrequest(xmlHttp, url, handler, attrArray)
{
	rpcrequest(xmlHttp, url, handler, attrArray, false)
}

function rpcrequest(xmlHttp, url, handler, attrArray, async)
{
	var data = "sid=" + Math.random();
	var x;
	
	//rpcFormat down there is very necessary, because any string data with spaces or newlines or similar will corrupt the data string
	for(x in attrArray)
	{
		// this != undefined bit is here because some javascript code likes to add members to the prototype of the array or object class. This code will iterate through any of those so they must be ignored.
		if(attrArray[x][0] != undefined)
		{
			data += "&"+(rpcFormat(attrArray[x][0]))+"="+(rpcFormat(attrArray[x][1]));
		}
	}
	
	rpcDoRequest(xmlHttp, url, handler, data, async);
}

function rpcError(xmlHttp)
{
	if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		if(xmlHttp.status != 200)
		{
			return true;
		}
		
		if(xmlHttp.responseText.indexOf("Microsoft VBScript runtime") != -1)
		{
			return true;
		}	
	}
	
	return false;
}

function rpcFormat(v)
{
	//return encodeURI(new String(v).replace(/&/,"%26"));
	return escape(v);
}

function rpcDoRequest(xmlHttp, url, handler, data, async)
{
    if(async == undefined) async = true;
    xmlHttp.open("POST", url, async);
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlHttp.onreadystatechange = handler;
    // siteTimeout returns immediately, so its not a good idea for synchronous requests
//    if(async == false) setTimeout(function() { alert("sending synchronous xmlhttp request to " + url + " with data " + data); xmlHttp.send(data); }, Math.floor(Math.random()*50));
//    else setTimeout(function() { xmlHttp.send(data); }, Math.floor(Math.random()*50));

    if(async == false)
    {
        xmlHttp.send(data);
        handler(); // onreadystatechange isn't available when doing synchronous requests
    }
    else
    {
        xmlHttp.send(data);
    }
//    xmlHttp.send(data);
}

function rpcGetXmlHttpObject()
{ 
    var objXmlHttp=null;

    if (navigator.userAgent.indexOf("MSIE")>=0)
    {
        var strName="Msxml2.XMLHTTP";
        if (navigator.appVersion.indexOf("MSIE 5.5")>=0)
        {
            strName="Microsoft.XMLHTTP";
        } 
        try
        { 
            objXmlHttp=new ActiveXObject(strName);
            return objXmlHttp;
        } 
        catch(e)
        { 
            alert("Error. Scripting for ActiveX might be disabled");
            return;
        } 
    }
    else
    //if (navigator.userAgent.indexOf("Mozilla")>=0)
    {
        objXmlHttp=new XMLHttpRequest();
        return objXmlHttp;
    }
} 

// deprecated but kept around for compatibility

function doRequest(xmlHttp, url, handler, data, async)
{ 
	return rpcDoRequest(xmlHttp, url, handler, data, async);
}

function GetXmlHttpObject()
{ 
    return rpcGetXmlHttpObject();
}