//ajax
var xmlhttp = false;
try 
{
	xmlhttp = new XMLHttpRequest();
}
catch (ms)
{
	try 
	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(oldms)
	{
		try
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(fail)
		{
			xmlhttp = false;
		}
	}
}

function getRequest()
{
	var stories = trim(document.getElementById("Stories").value);
	var sqftValue = trim(document.getElementById("Sqft").value);
	var state = document.getElementById("State").value;
	var url = "/calculated2.asp?Stories="+stories+"&Sqft="+sqftValue+"&State="+state+"&"+buildQuery('Pests')+"&"+buildQuery('Locations');
	xmlhttp.open("GET", url, true);
	xmlhttp.onreadystatechange = calculateResult;
	xmlhttp.send(null);
}

function calculateResult()
{
	if (xmlhttp.readyState == 4)
	{
		if (xmlhttp.status == 200)
		{
			document.getElementById("calculatorContent").innerHTML = xmlhttp.responseText;
		}
	}
}

function checkFields() {
	var sqft = document.getElementById("Sqft");
	var sqftValue = trim(sqft.value);
	var sqftError = document.getElementById("sqftError");

	if (sqftValue == "" || !isNumeric(sqftValue)) {
		sqftError.innerHTML = (sqftValue == "") ? "&lt; Required" : "&lt; Not a number";
		sqft.focus();
		return false;
	} else {
		sqftError.innerHTML = "";
	}

	return true;
}
	
function trim(text) {
	return text.replace(/^\s+|\s+$/g,"");
}

function isNumeric(text) {
	var validChars = "0123456789.,";
	for (i = 0; i < text.length; i++) {
		if (validChars.indexOf(text.charAt(i)) == -1) {
			return false;
		}
	}
	return true;
}

function PrintChecks(title, name, checks, nColumns)
{
	document.write("<table class='calculatorStep'>");
	document.write("<tr><th colspan='100%'>"+title+"</th></tr>");
	for (var i = 0; i <= checks.length -1; i++)
	{
		if (i % nColumns == 0)
			document.write("<tr>");
		document.write("<td><input type='checkbox' name='"+name+"' value='"+checks[i]+"'/>"+checks[i]+"</td>");
		
		if (i % nColumns == (nColumns - 1))
			document.write("</tr>");
	}
	if (i % nColumns > 0)
		document.write("</tr>");
	document.write("<tr><td colspan='100%'><input type='checkbox'/> Other <input type='text' name='"+name+"_other' size='20'/></td></tr></table>");
}

function buildQuery(name) //this adds the pests and locations array to the url.
{
	var query = "";
	var el = document.getElementsByName(name);
	var other = document.getElementsByName(name+"_other")
	for (var i = 0; i<= el.length -1; i++)
	{
		if (el[i].checked == true)
			query += name+"="+el[i].value+"&";
	}
	query = query.slice(0,-1);
	//handle "other" types.
	if (other[0].value != "")
	{
		if (name == "Pests")
			name = "Types_other";
		else
			name = "Location_other";
		if (query != "") query += "&";
		query += name+"="+other[0].value;
	}
	return query;
}

var calculator = false;
var wait = 0;
//jquery to toggle the calculator.
$(document).ready(function()
{
  //$(".calculatorBody").hide();
  $(".calculatorContent").hide();
  $(".togclick").click(function()
  {
	$(".calculatorContent").slideToggle(600);
	wait = new Date();
	if (calculator == false)
	{
		calculator = true;
		document.getElementById("operator").innerHTML ='-';
	}
	else
	{
		calculator = false;
		document.getElementById("operator").innerHTML ='+';
	}		
  });
});

document.onclick = hideCalculator
function hideCalculator(e) //This function is used to hide the calculator if they click outside the box.
{
	if(calculator != true || (new Date() - wait) <= 600)
		return;
	el = document.getElementById("calculatorContent");
	var x1 = el.offsetLeft;
	var x2 = el.offsetLeft + el.offsetWidth;
	var y1 = el.offsetTop;
	var y2 = el.offsetTop + el.offsetHeight;
	if(e)
	{
		var mouseX = e.pageX;
		var mouseY = e.pageY;
	}
	else if(window.event)
	{
		var mouseX = window.event.clientX+document.documentElement.scrollLeft;
		var mouseY = window.event.clientY+document.documentElement.scrollTop;
	}
	if((mouseX < x1 || mouseX > x2) || (mouseY < y1 || mouseY > y2))
	{
		calculator = false;
		wait = 0;
		$(".calculatorContent").slideToggle(600);
		document.getElementById("operator").innerHTML ='+';
	}
}
