var xmlHttp = createXMLHttpRequestObject(); 

function createXMLHttpRequestObject()
{
	var xmlHttp;
	try 
	{
		xmlHttp = new XMLHttpRequest();
	}
	catch(e)
	{
		var XMLHttpVersions = new Array ("MSXML2.XMLHTTP.6.0", "MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0",
								         "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP");
		for (var i=0; i<XMLHttpVersions.length && !xmlHttp; i++)
		{
			try 
			{
				xmlHttp = new ActiveXObject(XMLHttpVersions[i]);
			}
			catch(e) {} 
		}
	}
	if (!xmlHttp) 
		displayError("Error creating the XMLHttpRequest object.");
	else
		return xmlHttp;
}

function process()
{
	if (!xmlHttp) return;
	if ( !(xmlHttp.readyState == 0 || xmlHttp.readyState == 4) )
		alert("Can't connect to server, please try again later.");
	else
	{
		try 
		{
			var pret = document.getElementById("pret").value;
			xmlHttp.open("GET", "comision.php?pret="+pret, true);
			xmlHttp.onreadystatechange = handleRequestStateChange;
			xmlHttp.send(null);	
		}
		catch (e)
		{
			alert("Can't connect to the server :\n"+e.toString());
		}
	}
}

function handleRequestStateChange()
{
	if (xmlHttp.readyState == 4)
	{
		if (xmlHttp.status == 200)
		{
			try
			{
				handleServerResponse();
			}
			catch(e)
			{
				alert("Error reading the response: "+e.toString());
			}
		}
		else
		{
			alert("There was a problem retrieving the data:\n"+xmlHttp.statusText);
		}
	}
}

function handleServerResponse()
{
	var xmlResponse = xmlHttp.responseXML;
	if (!xmlResponse || !xmlResponse.documentElement)
		throw("Invalid XML structure : \n"+xmlHttp.responseText);
	var rootNodeName = xmlResponse.documentElement.nodeName;
	if (rootNodeName == "parsererror")
		throw("Invalid XML structure : \n"+xmlHttp.responseText);
	xmlRoot = xmlResponse.documentElement;
	if (rootNodeName != "response" || !xmlRoot.firstChild)
		throw("Invalid XML structure : \n"+xmlHttp.responseText);
	firstvalue = xmlRoot.getElementsByTagName("commission1").item(0).firstChild.data;
	document.getElementById("c3").value = firstvalue;
	if ( firstvalue != '0' )
	{
		secondvalue = xmlRoot.getElementsByTagName("commission2").item(0).firstChild.data;
		document.getElementById("c6").value = secondvalue;
	}
	else
		document.getElementById("c6").value = firstvalue;
}
