// Communication class is for simple ajax call functionality, needs to be out here because of IE
var xmlhttp = null;
var callback = null;

//create the object
function Communication(url) {
	this.url = url;
	this.initialize();
}

//Quick function to carry out posting
Communication.post = function(url, theData, aCallBack){
	var comm = new Communication(url);
	var finalData = "";
	if(theData instanceof Object){
		for (i in theData){
			if(finalData.length > 0){
				finalData = finalData + "&";
			}
			finalData = finalData + i + "=" + theData[i];
		}
	} else {
		finalData = theData; 
	}
	comm.sendByPost(finalData, aCallBack);
};

//Quick function to carry out get
Communication.get = function(url, theData, aCallBack){
	var comm = new Communication(url);
	var finalData = "";
	if(theData instanceof Object){
		for (i in theData){
			if(finalData.length > 0){
				finalData = finalData + "&";
			}
			finalData = finalData + i + "=" + theData[i];
		}
	} else {
		finalData = theData; 
	}
	comm.send(finalData, aCallBack);
};

Communication.callBack = null;

Communication.prototype.initialize=function(){
	try
    {
        xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
        try
        {
        	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(oc)
        {
        	xmlhttp=null;
        }
    }

    if(!xmlhttp&&typeof XMLHttpRequest!="undefined")
    {
    	xmlhttp = new XMLHttpRequest();
	}
};

/**
 * Send content under GET
 * @param query
 * @param aCallback
 * @return
 */
Communication.prototype.send=function(query, aCallback){
    if(xmlhttp!=null)
    {
    	Communication.callback = aCallback;
    	xmlhttp.open("GET", this.url, true);
    	xmlhttp.onreadystatechange=this.process;
    	xmlhttp.send(query);
    }
};
/**
 * Send content under POST
 * @param query
 * @param aCallback
 * @return
 */
Communication.prototype.sendByPost=function(query, aCallback){
    if(xmlhttp!=null)
    {
    	Communication.callback = aCallback;
    	xmlhttp.open("POST", this.url, true);
    	xmlhttp.onreadystatechange=this.process;
    	try
    	{
	    	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	    	xmlhttp.setRequestHeader("Content-length", query.length);
	    	xmlhttp.setRequestHeader("Connection", "close");
    	} catch (e) {
    		alert("Problem occured, unable to make the request");
    	}
    	
    	xmlhttp.send(query);
    }
};

Communication.prototype.process=function() {
	try
	{
		if(Communication.callback && xmlhttp.readyState == 4)
		{
			Communication.callback(xmlhttp.responseText);
		}
	}
	catch(e)
	{
		alert(e.message);
	}
};

