
var httpObject = null;

function doCalc(requesttype){

	if (requesttype == 'ratechange') {

		var loanamount = document.getElementById("loanamount").value;
		if (!verifyDigits(loanamount)) return false;

		var prevrate = document.getElementById("prevrate").value;
		if (!verifyDigits(prevrate)) return false;
	
		var currrate = document.getElementById("currrate").value;
		if (!verifyDigits(currrate)) return false;
	
		httpObject = getHttpObject();
	
		if (httpObject != null) {
			httpObject.onreadystatechange = stateChanged;
			httpObject.open("GET", "calcs.php?requesttype=ratechange"
							+"&loanamount="+loanamount
							+"&prevrate="+prevrate
							+"&currrate="+currrate, true);
			httpObject.send(null);
		}
	}
	
	return false;
}


function stateChanged(){
	if(httpObject.readyState == 4 || httpObject.readyState=="complete"){
		xmlDoc=httpObject.responseXML;
		
		
		if(document.all){
			// IE
			var type = xmlDoc.getElementsByTagName("requesttype")[0].childNodes[0].nodeValue;

			if (type == "ratechange") {			
				document.getElementById("currentinterestyear").innerText = "$"+xmlDoc.getElementsByTagName("currentinterestyear")[0].childNodes[0].nodeValue;
				document.getElementById("currentinterestmonth").innerText = "$"+xmlDoc.getElementsByTagName("currentinterestmonth")[0].childNodes[0].nodeValue;
				document.getElementById("interestchangeyear").innerText = "$"+xmlDoc.getElementsByTagName("interestchangeyear")[0].childNodes[0].nodeValue;
				document.getElementById("interestchangemonth").innerText = "$"+xmlDoc.getElementsByTagName("interestchangemonth")[0].childNodes[0].nodeValue;
			}
		} else{
			// firefox
			document.getElementById("currentinterestyear").textContent = "$"+xmlDoc.getElementsByTagName("currentinterestyear")[0].childNodes[0].nodeValue;
			document.getElementById("currentinterestmonth").textContent = "$"+xmlDoc.getElementsByTagName("currentinterestmonth")[0].childNodes[0].nodeValue;
			document.getElementById("interestchangeyear").textContent = "$"+xmlDoc.getElementsByTagName("interestchangeyear")[0].childNodes[0].nodeValue;
			document.getElementById("interestchangemonth").textContent = "$"+xmlDoc.getElementsByTagName("interestchangemonth")[0].childNodes[0].nodeValue;
		}
		
		window.location.hash="results";
	}

}


function verifyDigits(text)
{
	if (!text.match(/^[0-9.]+$/)) {
		alert("Please enter decimal numbers only");
		return false;
	}
	
	return true;
}



function getHttpObject()
{
	var xmlHttp=null;

	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e){
		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e){
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}