var loading_exchange = document.getElementById("loadDiv_exchange");

// holds an instance of XMLHttpRequest
var xmlHttp_exchange = createXmlHttpRequestObject_exchange();
// creates an XMLHttpRequest instance
function createXmlHttpRequestObject_exchange()
{
	// will store the reference to the XMLHttpRequest object
	var xmlHttp;
	// this should work for all browsers except IE6 and older
	try
	{
		// try to create XMLHttpRequest object
		xmlHttp = new XMLHttpRequest();
	}
	catch(e)
	{
		// assume IE6 or older
		var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
		"MSXML2.XMLHTTP.5.0",
		"MSXML2.XMLHTTP.4.0",
		"MSXML2.XMLHTTP.3.0",
		"MSXML2.XMLHTTP",
		"Microsoft.XMLHTTP");
		// try every prog id until one works
		for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++)
		{
			try
			{
				// try to create XMLHttpRequest object
				xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
			}
			catch (e) {}
		}
	}
	// return the created object or display an error message
	if (!xmlHttp)
		alert("Error creating the XMLHttpRequest object.");
	else
		return xmlHttp;
}
function tryExchangeRequest()
{
	text_value = document.getElementById('exchange_value').value;
	text_from = document.getElementById('exchange_from').options[document.getElementById('exchange_from').selectedIndex].value;
	text_to = document.getElementById('exchange_to').options[document.getElementById('exchange_to').selectedIndex].value;

	if (text_value > 0 && text_from >= 0 && text_to >= 0)
		sendExchangeRequest();

}
// called to read a file from the server
function sendExchangeRequest()
{
		// only continue if xmlHttp isn't void
		if (xmlHttp_exchange)
		{
			// try to connect to the server
			try
			{
				text_value = document.getElementById('exchange_value').value;
				text_from = document.getElementById('exchange_from').options[document.getElementById('exchange_from').selectedIndex].value;
				text_to = document.getElementById('exchange_to').options[document.getElementById('exchange_to').selectedIndex].value;
				
				var params = "value=" + escape(text_value) + "&from=" + escape(text_from) + "&to=" + escape(text_to);
				call = "http://" + window.location.hostname + "/include/processExchangeRequest.php";

				xmlHttp_exchange.open("POST", call, true);
				
				//Send the proper header information along with the request
				xmlHttp_exchange.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				xmlHttp_exchange.setRequestHeader("Content-length", params.length);
				xmlHttp_exchange.setRequestHeader("Connection", "close");			
				
				xmlHttp_exchange.onreadystatechange = statechange_sendExchangeRequest;
				
				xmlHttp_exchange.send(params);
			
			}
			// display the error in case of failure
			catch (e)
			{
				alert("Can't connect to server:\n" + e.toString());
			}
		}
}	
// function that handles the HTTP response
function statechange_sendExchangeRequest()
{
	
	// display the status of the request
	if (xmlHttp_exchange.readyState == 1)
	{
		loading_exchange.style.visibility = "visible";		
	}
	
	// when readyState is 4, we also read the server response
	else if (xmlHttp_exchange.readyState == 4)
	{
		// continue only if HTTP status is "OK"
		if (xmlHttp_exchange.status == 200)
		{
			try
			{
				loading_exchange.style.visibility = 'hidden';
				
				// read the message from the server
				response = xmlHttp_exchange.responseXML;
				
				
				if (response.childNodes[0].childNodes[0].childNodes[0].nodeValue != 'OK')
				{
					//error	
				}
				else
				{
					document.getElementById('result_exchange').innerHTML = response.childNodes[0].childNodes[1].childNodes[0].nodeValue;	
				}
				
			}
			catch(e)
			{
				// display error message
				alert("Error reading the response: " + e.toString());
			}
		}
		else
		{
			// display status message
			alert("There was a problem retrieving the data:\n" +
			xmlHttp_exchange.statusText);
		}
	}
}
