/* communication.js
 * By Joshua Wilson
 * (C) 2009 Quick Cable Corporation
 * 8/20/09
 *
 * All ajax's function are in here to communicate
 */
function createREQ()
{
	try
	{
		req = new XMLHttpRequest(); /*Firefox*/
	} catch (err1) {
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP"); /* Early ID */
		} catch (err2) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP"); /* Other ID */
			} catch (err3) {
				req = false;
                throw new Exception();
			}
		}
	}
	return req;
}
function requestGET(url, query, req)
{
	var myRand = parseInt(Math.random()*9999999);
	req.open('GET', url+'?'+query+'&rand='+myRand,true);
	req.send(null);
}
function requestPOST(url, query, req)
{
	req.open('POST', url, true);
	req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	req.send(query);
}
function doAjax(url, query, callback, reqtype, getxml)
{
	var myreq = createREQ();
	myreq.onreadystatechange = function() {

		if  (myreq.readyState == 4)
		{
			if (myreq.status == 200)
			{
				//hideWorkingPane();
				var item = myreq.responseText;
				if (getxml == 1)
				{
					item = myreq.responseXML;
				}
				/* Checks to see what the call back is. Then sends
				 * information to the callback function to display
				 * the correct information in a div
				 */
				if ( callback != null && item != null )
				{
					doCallBack(callback, item);
				}
			}
		}
	}
	if (reqtype == 'post')
		requestPOST(url, query, myreq);
	else
		requestGET(url, query, myreq);
}
function doCallBack(callback, item)
{
	eval(callback + '(item)');
}

