

function plPath()
{
	var scripts = document.getElementsByTagName("script");
	for (var i=0; i<scripts.length; ++i)
	{
		var src = scripts[i].src;
		if (-1!=src.indexOf('pl_test/'))
			return 'acgpl_test';
	}
	return (-1!=location.pathname.indexOf('pl_test/')?'acgpl_test':'acgpl');
}

function request(sourceUri, service, request, handler)
{
	var http = false;
	if(navigator.appName == "Microsoft Internet Explorer")
		http = new ActiveXObject("Microsoft.XMLHTTP");
	else
		http = new XMLHttpRequest();

	var timeoutFunc = function ()
	{
		if (typeof log != 'undefined' && typeof log == 'function')
			log("request timeout");
			
		http.abort();
		var respObj = new Object;
		respObj.error = "Communications timeout. Please try again.";
		handler(respObj);
	}
	var timer = setTimeout(timeoutFunc, 40000);
	
	http.onreadystatechange=function()
	{
		if(4 == http.readyState)
		{
			clearTimeout(timer);
			
			if (200 == http.status)
			{
				//if (typeof log != 'undefined' && typeof log == 'function')
				//	log("resp:"+http.responseText);
				var respObj = JSON.parse(http.responseText);
				handler(respObj);
			}
			else
			{
				var respObj = new Object;
				respObj.error = "Communications error (" + http.status + "). Please try again.";
				handler(respObj);
			}
		}
	}
	
	// construct and open the service
	var uri;
	if (sourceUri.length == 0) {
		uri = location.protocol + '//' + location.hostname + '/';
		uri += plPath();
		uri += '/services/' + service + '.php';
	}
	else {
		// depricated method (first argument of function is not necessary)
		var slashPos = sourceUri.indexOf('acgpl/');
		uri = sourceUri.substring(0, slashPos) + "acgpl/services/" + service + ".php";
	}
	http.open("POST", uri, true);
	//log(uri);

	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

	// encode object as post parameters
	var parms = '';
	if (request)
	{
		for (var k in request) {
			if (parms.length > 0) 
				parms += '&';
			parms += '' + k + '=' + request[k];
		}
	}
	//log(parms);
	http.send(parms);
}
