var G_apilite = new Array();
var G_httpOK = 4;
var G_apistack = 0;
function getHTTPObject() {
	var C=null;
	try {C=new ActiveXObject("Msxml2.XMLHTTP")}
	catch(e){
		try{C=new ActiveXObject("Microsoft.XMLHTTP")}
		catch(sc){C=null}
	}
	if(!C&&typeof XMLHttpRequest!="undefined"){C=new XMLHttpRequest()}
	return C
}

function apilite_handleresponse() {
	//Retrieve response string from HTTP call.
	if (G_apilite['httpobj'].readyState == G_httpOK) {
		//Split the first line off the string, and look for 1 or 0 to determine if successful or not.
		var tmp = G_apilite['httpobj'].responseText.split("\n");
		var l1 = tmp.shift();
		G_apistack--;
		if (l1 == '1') {
			//If successful, then call success function, with remaining return string re-joined.
			ShowDebug("API OK: "+G_apilite['okfunc']+" / "+G_apilite['okargs']+" [Stack: "+G_apistack+"]<br />Response: "+tmp.join("<br />")+"<br /><br />");
			window[G_apilite['okfunc']](tmp.join("\n"),G_apilite['okargs']);
		} else {
			//If failure, then call fail function.
			ShowDebug("API FAIL: "+G_apilite['failfunc']+" / "+G_apilite['failargs']+" [Stack: "+G_apistack+"]<br />Response: "+tmp.join("<br />")+"<br /><br />");
			window[G_apilite['failfunc']](tmp.join("\n"),G_apilite['failargs']);
		}
	}
}

function apilite_call(url,okfunc,okargs,failfunc,failargs,fulldir) {
	//force all calls to go via the new post code, reformatting args string into the object required.
	var trmd=url.split(/(\?|&)/);
	url=trmd.shift();
	var narg=new Array();
	while(trmd.length>0) {
		var tmp=trmd.shift().split(/=/);
		if(tmp[1]) {narg[tmp[0]]=tmp[1];}
	}
	apilite_post(url,narg,okfunc,okargs,failfunc,failargs,fulldir);
	return;
}
// function apilite_call(url,okfunc,okargs,failfunc,failargs,fulldir) {
//	var url='lookup.php?picked_id='+id;	//Specify the URL to be called.
// 	ShowDebug("API call: "+url+" : "+okfunc+" / "+okargs+" : "+failfunc+" / "+failargs+" [Stack: "+G_apistack+"]<br /><br />");
// 
// 	//Keep track of the success and failure funcs and args (these are in globals so that handleresponse has access to them).
// 	G_apistack++;
// 	G_apilite['okfunc']=okfunc;
// 	G_apilite['okargs']=okargs;
// 	G_apilite['failfunc']=failfunc;
// 	G_apilite['failargs']=failargs;
// 
// 	//Set up and call the HTTP 
// 	G_apilite['httpobj'] = getHTTPObject();
// 	G_apilite['httpobj'].open('GET', (fulldir?'':'callout/')+url, true);
// 	G_apilite['httpobj'].onreadystatechange = apilite_handleresponse;
// 	G_apilite['httpobj'].send(null);
// }

function apilite_post(url,postobject,okfunc,okargs,failfunc,failargs,fulldir) {
	//PostObject should be formatted {name:value,name:value,...}
//	var url='lookup.php?picked_id='+id;	//Specify the URL to be called.
	var sendstring="";
	var count=0;
	postobject["extrarandombit"]=Math.round(Math.random()*99999999);

	//postobject.extrarandombit=Math.round(Math.random()*99999999);
	for(var prop in postobject){ 
		if(sendstring) {sendstring+="&"};
		if(postobject[prop]) {sendstring+=prop+"="+urlEncode(postobject[prop]);}
	}
	ShowDebug("API post: "+url+" [ "+sendstring+" ]: "+okfunc+" / "+okargs+" : "+failfunc+" / "+failargs+" [Stack: "+G_apistack+"]<br /><br />");

	//Keep track of the success and failure funcs and args (these are in globals so that handleresponse has access to them).
	G_apistack++;
	G_apilite['okfunc']=okfunc;
	G_apilite['okargs']=okargs;
	G_apilite['failfunc']=failfunc;
	G_apilite['failargs']=failargs;

	//Set up and call the HTTP 
	G_apilite['httpobj'] = getHTTPObject();
// 	G_apilite['httpobj'].open('POST', (fulldir?'':'callout/')+url+"?"+randombit, true);
	G_apilite['httpobj'].open('POST', (fulldir?'':'callout/')+url, true);
	G_apilite['httpobj'].setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
	G_apilite['httpobj'].onreadystatechange = apilite_handleresponse;
	G_apilite['httpobj'].send(sendstring);
}

//-----------------------------------------------------------------------------------------------
//converts URL reserved characters (eg &%?, etc) to URL entities (eg %20)
function urlEncode(val) {
	var len = val.length;
	var i = 0;
	var newStr = "";

	if(typeof(val)=='number') { return(val.toString(10)); }	//don't bother encoding if it's a numeric; just return it as a string.

	for (i=0;i<len;i++) {
		if (val.substring(i,i+1).charCodeAt(0) <= 255) {
			newStr += Convert(val.substring(i,i+1));
		}
	}
	return newStr;
}

function Convert(character) {
	var val=character.charCodeAt(0);

	if(isSafe(character)) {
		return character;
	} else {
		var str=val.toString(16);
		if(str.length==1) {str='0'+str;}
		return "%"+str;
	}
}

function isSafe(compareChar) {
	var unsafeString = "\"<>%\\^[]`\+\$\,;/?:@=&#";
	return (unsafeString.indexOf(compareChar) == -1 && compareChar.charCodeAt(0) > 32 && compareChar.charCodeAt(0) < 123);
}

function ShowDebug(debugstring) {
}
