/** Simple Ajax
 *  Based on SAL by Nigel Liefrink
 */

function ajax_getXmlHttp() {
	var xmlhttp = false;
	if (window.XMLHttpRequest)
	{
		xmlhttp = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		try
		{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try
			{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (E) {
				xmlhttp = false;
			}
		}
	}
	return xmlhttp;
}

function ajax_setupXmlHttp(url, callbackFunction, type, params)
{
	var xmlhttp = new ajax_getXmlHttp();
	if (xmlhttp)
	{
		xmlhttp.onreadystatechange = 
			function ()
			{
				if (xmlhttp && xmlhttp.readyState==4)
				{
					if (xmlhttp.status==200)
					{
						var response = xmlhttp.responseText;
						var functionToCall = null;
						if (params != null)
						{
							functionToCall = callbackFunction + '(response,'+params+')';
						} else {
							functionToCall = callbackFunction + '(response)';
						}
						eval(functionToCall);
					}
				}
			}
		xmlhttp.open(type,url,true);
		/*
		var errormess = "Your security settings are too high, in that they are currently restricting this page from requesting data.  Please enable 'Run ActiveX controls and plug-ins' and 'Script ActiveX controls marked safe for scripting' in order to view this page.";
		try
		{
			if (!xmlhttp.open)
			{
				alert(errormess);
				return(null);
			} else {
				xmlhttp.open(type,url,true);
			}
		} catch (E) {
			alert(errormess);
			return(null);
		}
		*/
	}
	return(xmlhttp);
}

function ajax_sendGetParams(url, callbackFunction, params)
{
	xmlhttp = ajax_setupXmlHttp(url, callbackFunction, "GET", params);
	if (xmlhttp != null)
		xmlhttp.send(null);
}

function ajax_sendGet(url, callbackFunction)
{
	var sentID = null;
	// append unique id to end of url to avoid page caching
	sentID = wito_getRandSeq();
	url += "&retcode=" + sentID;
	// setup sender
	xmlhttp = ajax_setupXmlHttp(url, callbackFunction, "GET", null);
	// send
	if (xmlhttp != null)
		xmlhttp.send(null);
	return(sentID);
}

// additional code from WhatIthinkof.com, used under license from Lightenna.com
function wito_getRandSeq()
{
	// translate float [0-1] to safe char seq [0-9][a-z][A-Z][-][_]
	var num = Math.random() * 1e9;
	return(wito_convertToCharSeq(num));
}

function wito_convertToCharSeq(num)
{
	var clipped;
	var output = "";
	for (var i=0 ; i<24 ; i+=6)
	{
		clipped = (num >> i) & 0x3F; // 0-63
		output += String.fromCharCode(wito_getRandLookup(clipped));
	}
	return(output);
}

function wito_getRandLookup(i)
{
	// char values [45][48-57][65-90][95][97-122] = 64 values
	// intervals [01][10][26][26]
	// interval upper boundaries [01][11][37][38][64]
	// corresponding offsets   = [45][47][54][58][59]
	if (i<1) return(i+45);
	if (i<11) return(i+47);
	if (i<37) return(i+54);
	if (i<38) return(i+58);
	if (i<64) return(i+59);
	// backup failure case (i>=64)
	return(45);	
}

function wito_arrayRemove( elem, arr)
{
	var index = -1;
	for (var i=0 ; i<arr.length ; ++i)
	{
		if (arr[i] == elem)
		{
			index = i;
			break;
		}
	}
	if (index != -1)
	{
		arr.splice(index, 1);
	}
}

