//This constructor must be used when we send data to server throw XMLRequest
// sName - variable name
// sValue - value of the variable
function DataToServer(sName, sValue)
{		
	this.name = sName;
	this.value = sValue;
}

//The objects of this class is used to XMLRequests
// sMethod = GET/POST - method of sending data
// sURL - URL
// executableFunction - function that executes when request is done
// aDataToServer - data which we sending to server
function BxRequest(sMethod, sURL, executableFunction, aDataToServer)
{
	this.sMethod = sMethod;
	this.sURL = sURL;
	//this.executableFunction = function executableFunction;
	this.sDataToServer = aDataToServer? this.convertADatatoSData(aDataToServer) : null;
	this.requestObject = this.createXMLHttpRequestObject(executableFunction) ;
}

//Converts aData array to string by format: name1=value1, name2=value2, name3=value3,...
BxRequest.prototype.convertADatatoSData = function(aData)
{
	try
	{
		var sResult = aData[0].name + '=' + aData[0].value.replace(' ','+');
		for(var i=1; i < aData.length; i++)	sResult += '&' + aData[i].name + '=' + aData[i].value.replace(' ','+');
	}
	catch (except1)
	{
		var sResult = aData.name + '=' + aData.value.replace(' ','+');
	}
	return sResult;
}

//Executes XMLHttpRequest
// executableFunction - function that executes when request is done
BxRequest.prototype.createXMLHttpRequestObject = function(executableFunction)
{
	var XMLHttpRequestObject = false;
	try
	{
		XMLHttpRequestObject = new ActiveXObject('MSXML2.XMLHTTP');
	}
	catch (except1)
	{
		try
		{
			XMLHttpRequestObject = new ActiveXObject('Microsoft.XMLHTTP');
		}
		catch (except2)
		{
			XMLHttpRequestObject = false;
		}
	}		
	
	if (!XMLHttpRequestObject && window.XMLHttpRequest)
	{
		XMLHttpRequestObject = new XMLHttpRequest();
	}
	
	if(!XMLHttpRequestObject) new BxError("HttpXml object creation failed", "please upgrade your browser");
	
	if(XMLHttpRequestObject)
	{
		var sURLGen = (this.sMethod == 'GET')? (this.sURL + '?' + this.sDataToServer) : this.sURL;
		XMLHttpRequestObject.open(this.sMethod, sURLGen, true);
		XMLHttpRequestObject.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		
		XMLHttpRequestObject.onreadystatechange = function()
		{			
			if (XMLHttpRequestObject.readyState == 4)
	    	{
			    if (XMLHttpRequestObject.status == 200 || XMLHttpRequestObject.status == 304)
				{
	            	executableFunction(XMLHttpRequestObject);
		    	}
				else new BxError("XML read failed:" + XMLHttpRequestObject.status, "There was a problem retrieving the XML data:\n" + this.sURL);
	    	}
		}
		XMLHttpRequestObject.send((this.sMethod=='POST')? this.sDataToServer : null);
	}
}
