var vResponse = null;

function AJAXRequest(pURL, pMethod, pParam, pCallback)
{
	this.vURL = pURL;
	this.vParam = '';
	this.vMethod = pMethod;
	this.vCallback = pCallback;
	this.objHTTPRequest = this.GetHTTPRequest();
	
	for(var i=0; i<pParam.length; i++) this.vParam += (pParam[i][0] + '=' + encodeURIComponent(pParam[i][1]) + (i+1==pParam.length ? '' : '&'));

	if(this.objHTTPRequest)
	{
		var instanceOfThis = this;
		this.objHTTPRequest.onreadystatechange = function() { instanceOfThis.stateChanged(); };
		
		if(this.vMethod==1)
		{
			this.objHTTPRequest.open('GET', this.vURL + '?' + this.vParam, true);
			this.objHTTPRequest.send(null);
		}
		else if(this.vMethod==2)
		{
			this.objHTTPRequest.open('POST', this.vURL, true);
			this.objHTTPRequest.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
			this.objHTTPRequest.setRequestHeader('content-length', this.vParam.length);
			this.objHTTPRequest.setRequestHeader('connection', 'close');
			this.objHTTPRequest.send(this.vParam);
		}
	}
	else
	{
		eval(this.vCallback + '(true, "Failed to submit... Contact system administrator...")');
			return null;
	}
}

AJAXRequest.prototype.GetHTTPRequest = function ()
{
	var objHTTPRequest = null;
    
	if (window.XMLHttpRequest)
	{
		objHTTPRequest = new XMLHttpRequest();
        if(objHTTPRequest.overrideMimeType) { objHTTPRequest.overrideMimeType('text/xml'); }
	}
	else if (window.ActiveXObject)
	{
    	try { objHTTPRequest = new ActiveXObject("Msxml2.XMLHTTP"); }
        catch(e) { try { objHTTPRequest = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} }
	}
	
	return objHTTPRequest;
}

AJAXRequest.prototype.stateChanged = function ()
{
	if (this.objHTTPRequest.readyState==4)
	{
		if (this.objHTTPRequest.status==200)
		{
			result = this.objHTTPRequest.responseXML;
//			alert(this.objHTTPRequest.responseText);

			if(result.getElementsByTagName("ERROR").length > 0) eval(this.vCallback + '(true, "' + result.getElementsByTagName("ERROR")[0].firstChild.nodeValue + '")');
			else { vResponse = result.getElementsByTagName("SUCCESS")[0]; eval(this.vCallback + '(false, vResponse)'); }
        }
        else
        {
			eval(this.vCallback + '(true, "Failed to submit... Please retry...")');
        }
	}
}
