// On page load sets resource numbers to 0 or location to 1
function zeroNum() {
	document.f.numac.value = 0;
	document.f.numstaff.value = 0;
	document.f.numother.value = 0;
	document.f.numtank.value = 0;
	document.f.numloc.value = 1;
}
// On blur of use entry of additional locations this calculates the total
// Adds the 1 orginial set on page load plus the number of additional entered by user
function calcLoc() {
	totalloc = Number(document.f.numloc.value) + Number(document.f.locnum.value);
	document.f.numloc.value = totalloc
}
// Hide or Display and Element
function setDisplay(objectID,state) {
	var object = document.getElementById(objectID);
	object.style.display = state;
}
// Checks on certain menus if a checkbox is checked or not
function checkCheck(evt,whatCheck,whatPop,whatMenu,whatEdition) {	
	var evt = (evt) ? evt : ((window.event) ? event : null);
	//var isCheck = document.getElementById(whatCheck).checked;
	var isCheck = whatCheck.checked;
	var whatName = whatCheck.name;
	if (isCheck==false) {	//If something is being unchecked it closes clamshell and 0'x values	
		toggleClamShellMenu(whatMenu);
		getPrice(); //If something is being unchecked it call getPrice function
		if (whatName == 'schedule') {
			if (document.f.dispatch.checked || document.f.charter.checked) {
				return;	
			}
			else {
				document.f.numac.value = 0;
				document.f.numstaff.value = 0;
				document.f.numother.value = 0;
			}
			setDisplay(whatMenu,'none');
		}
		if (whatName == 'dispatch') {
			if (document.f.schedule.checked || document.f.charter.checked) {	
				return;	
			}
			else {
				document.f.numac.value = 0;
				document.f.numstaff.value = 0;
				document.f.numother.value = 0;
			}
			setDisplay(whatMenu,'none');
		}
		if (whatName == 'charter') {
			if ((document.f.schedule.checked == true) || (document.f.dispatch.checked == true)) {
				return;
			}
			else {
				document.f.numac.value = 0;
				document.f.numstaff.value = 0;
				document.f.numother.value = 0;
			}
			setDisplay(whatMenu,'none');
		}
		if (whatName == 'fueling') {
			document.f.numtank.value = 0;
			setDisplay(whatMenu,'none');
		}
		if (whatName == 'location') {
			document.f.numloc.value = 1;
			setDisplay(whatMenu,'none');
		}
	}
	else {
		if (whatName == 'schedule') { //If dispatch is being checked it checks to see if resource is greater than 0. 
			if ((document.f.numac.value + document.f.numstaff.value + document.f.numother.value) > 0) {
				toggleClamShellMenu(whatMenu); //If resource is greater than 0 it opens clamshell
				//This prevents dispatch selection without any resources
				getPrice();
			}
			else {
				document.f.schedule.disabled = true; //Disables check box while resource entry div is open
				popUp(evt,whatPop); //If resource is NOT greater than 0 then it pops asking for resources. 
			}
		}
		if (whatName == 'dispatch') { //If dispatch is being checked it checks to see if resource is greater than 0. 
			if ((document.f.numac.value + document.f.numstaff.value + document.f.numother.value) > 0) {
				toggleClamShellMenu(whatMenu); //If resource is greater than 0 it opens clamshell
				//This prevents dispatch selection without any resources
				getPrice();
			}
			else {
				document.f.dispatch.disabled = true; //Disables check box while resource entry div is open
				popUp(evt,whatPop); //If resource is NOT greater than 0 then it pops asking for resources. 
			}
		}
		if (whatName == 'charter') { //If charter is being checked it checks to see if resource is greater than 0. 
			if ((document.f.numac.value + document.f.numstaff.value + document.f.numother.value) > 0) {
				toggleClamShellMenu(whatMenu); //If resource is greater than 0 it opens clamshell
				//This prevents dispatch selection without any resources
				getPrice();
			}
			else {
				document.f.charter.disabled = true; //Disables check box while resource entry div is open
				popUp(evt,whatPop); //If resource is NOT greater than 0 then it pops asking for resources. 
			}
		}
		if (whatName == 'fueling' || whatName == 'location') {
			popUp(evt,whatPop);
		}
	}
}
// Checks another checkbox if duplicate checkbox is checked
function crossCheck(evt,whatCheck) {	
	var evt = (evt) ? evt : ((window.event) ? event : null);
	//var isCheck = document.getElementById(whatCheck).checked;
	var isCheck = whatCheck.checked;
	var whatName = whatCheck.name;
	if (whatName == 'shopinven') {
		if (isCheck== true) {
			document.f.inventory.checked = true;
		}
		else {
			document.f.inventory.checked = false;
		}
		toggleClamShellMenu('menuinventory');
	}
	if (whatName == 'inventory') {
		if (isCheck== true) {
			document.f.shopinven.checked = true;
		}
		else {
			document.f.shopinven.checked = false;
		}
	}
	if (whatName == 'duty') {
		if (isCheck == true) {
			var dcheck = document.f.dispatch.checked;
			if (dcheck == false) {
				document.f.dispatch.checked = true;
				toggleClamShellMenu('menudispatch');
			}
		}
	}
	if (whatName == 'dispatch') {
		if (isCheck == true) {
			document.f.duty.checked = true;
		}
		else {
			document.f.duty.checked = false;
		}
	}
	getPrice();
}
//Function to recalculate and populate tank number fields
function resTankNum() {
	if (document.f.tanknum.value != '') {
		document.f.numtank.value = document.f.tanknum.value
		getPrice();
	}
}
//Function to recalculate and populate resource number fields on schedule select
function resNum() {
	if (document.f.acnum.value != '') {
		document.f.numac.value = document.f.acnum.value
	}
	if (document.f.staffnum.value != '') {
		document.f.numstaff.value = document.f.staffnum.value
	}
	if (document.f.othernum.value != '') {
		document.f.numother.value = document.f.othernum.value
	}
	getPrice();
}
//Function to recalculate and populate resource number fields on dispatch select
function resNumD() {
	if (document.f.acnumd.value != '') {
		document.f.numac.value = document.f.acnumd.value
	}
	if (document.f.staffnumd.value != '') {
		document.f.numstaff.value = document.f.staffnumd.value
	}
	if (document.f.othernumd.value != '') {
		document.f.numother.value = document.f.othernumd.value
	}
	getPrice();
}
function resNumCH() {
	if (document.f.acnumch.value != '') {
		document.f.numac.value = document.f.acnumch.value
	}
	if (document.f.staffnumch.value != '') {
		document.f.numstaff.value = document.f.staffnumch.value
	}
	if (document.f.othernumch.value != '') {
		document.f.numother.value = document.f.othernumch.value
	}
	getPrice();
}
// Home page clam shell menu
function toggleClamShellMenu(objectID) {
	var object = document.getElementById(objectID);
	if (object.style.display =='block') {
		object.style.display='none';
	}
	else {
		object.style.display='block';
	}
	return;
}
// On mouse out of description replaces desc div content with loading message
function clearHer() {
  document.getElementById("desc").innerHTML="Loading...";
 }
 // AJAX call to display bullet description content in floaing div
var xmlHttp;

function showHint(str) {
	if (str.length==0) { 
		document.getElementById("desc").innerHTML="";
		return;
	}
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) {
		alert ("Your browser does not support AJAX!");
		return;
	} 
	var url="ajax.asp";
	url=url+"?q="+str;
	//url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
} 

function stateChanged() {
	if (xmlHttp.readyState==4) { 
		document.getElementById("desc").innerHTML = xmlHttp.responseText;
		adjustDesc();//Calls function that checks if div will run off page and is so adjusts
	}
}
function GetXmlHttpObject() {
	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;
}

//Function that checks if div will run off page and is so adjusts
function adjustDesc() {
	var objectID = document.getElementById("desc");
	var ScrollTop = findScrollTop();
	var pageHeight = findOffSet();
	var DivHeight = objectID.offsetHeight;
	var DivTop = objectID.offsetTop - ScrollTop;
	DivTotal = DivHeight + DivTop; 
	if (DivTotal > pageHeight) {
		howmuchover = DivTotal - pageHeight;
		howmuchmove = objectID.offsetTop - howmuchover - 6;
		objectID.style.top = howmuchmove + 'px';
	}
}

// Pops up floating bullet description div onmouseover
var position = 0;
var objPopUp = null;
var popMenuOn = 0;

function popUp(evt,objectID) {
	if (popMenuOn == 1) popHide();
	//document.onclick = popHide;
	var evt = (evt) ? evt : ((window.event) ? event : null);
	objPopUp = document.getElementById(objectID);
	scrdif = findScrollTop();
	xPos = evt.clientX;
	yPos = evt.clientY + scrdif;
	if (xPos + objPopUp.offsetWidth >  document.body.clientWidth) xPos = xPos - objPopUp.offsetWidth;
	if (yPos + objPopUp.offsetHeight >  document.body.clientHeight) yPos = yPos - objPopUp.offsetHeight;
	objPopUp.style.left = (xPos + 20) + 'px';
	objPopUp.style.top = (yPos - 15) + 'px';
	objPopUp.style.display = 'block';
}
function popHide() {
	if (popMenuOn == 0) {
		popMenuOn = 1;
		return
	}
	else {
		objPopUp.style.display = 'none';
		objPopUp = null;
		popMenuOn = 0;
		document.onclick = null;
	}
}
//Reads form fields and builds AJAX getPrice POST String
function buildPOST(theFormName) {
	theForm = document.forms[theFormName]; 
	var qs = '';
	for (e=0; e < theForm.elements.length; e++) { 
		if (theForm.elements[e].name != '') {
        	if (theForm.elements[e].type == "checkbox") {
				if (theForm.elements[e].checked==true) {
					var name = theForm.elements[e].name; 
					qs+=(qs=='')?'':'&' 
					qs+= name+'='+escape(theForm.elements[e].value); 
				}
				else {
					var name = theForm.elements[e].name;
					qs+=(qs=='')?'':'&' 
					qs+= name+'='+''; 
				}
			}
			else {
					var name = theForm.elements[e].name;
					qs+=(qs=='')?'':'&' 
					qs+= name+'='+escape(theForm.elements[e].value); 
			}
		}
	}
	qs+="\n"; 
	return qs
}
//Sends AJAX form string and returns price
var XMLHttpRequestObject = false; 

if (window.XMLHttpRequest) {
	XMLHttpRequestObject = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
	XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
}

function getPrice() {
	if(XMLHttpRequestObject) {
		var data = buildPOST('f');
		var obj = document.getElementById("totalprice"); 
		XMLHttpRequestObject.open("POST", "getquote.asp"); 
		XMLHttpRequestObject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
		XMLHttpRequestObject.onreadystatechange = function() { 
			if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) { 
				obj.innerHTML = XMLHttpRequestObject.responseText;
			}
		} 
		XMLHttpRequestObject.send(data); 
	}
}
//Finds scroll position of page	
function findScrollTop() {
	if (document.body.scrollTop != null) return document.body.scrollTop;
	if (window.pageYOffset != null) return window.pageYOffset;
	return (null);
}
// Floating Scrolling Price Bar
window.onscroll = moveMenuBar;
document.onscroll = moveMenuBar;

function moveMenuBar() {
	var object=document.getElementById('pricebar');
	var scrolly = findScrollTop();
	var whatH =  findOffSet();
	var y = whatH + scrolly - 229;
	object.style.display = 'none';
	object.style.top = y + 'px';
	object.style.display = 'block';
}
//Sets position of price box div to bottom on page load
function setPos() {
	var object = document.getElementById('pricebar');
	var whatH = findOffSet();
	var isScroll = findScrollTop();
	if (isScroll > 0) {
		y = whatH + isScroll - 229;
	}
	else {
		y = whatH - 229;
	}
	object.style.top = y + 'px';
}
function setPosMail() {
	var object = document.getElementById('get_contact');
	var whatH = findOffSet();
	var isScroll = findScrollTop();
	if (isScroll > 0) {
		y = whatH + isScroll - 237;
	}
	else {
		y = whatH - 237;
	}
	object.style.top = y + 'px';
}
//Finds page height
function findOffSet() {
	if (document.all) {
		return document.body.offsetHeight;
	}
	else {
		return window.innerHeight;
	}
	return (null);
}
//Home page email quote validation
function validateForm() {
	if (document.f.first_name.value == "") {
		alert("You must enter a first name")
		document.f.first_name.focus()
		return false
	}
	if (document.f.last_name.value == "") {
		alert("You must enter a last name")
		document.f.last_name.focus()
		return false
	}
	if (document.f.apt.value == "") {
		alert("You must enter an ID")
		document.f.apt.focus()
		return false
	}
	if (document.f.apt.value.length < 3) {
		alert("Invalid ID")
		document.f.apt.focus()
		return false
	}
	var emailFilter = /^.+@.+\..{2,4}$/;
	if (!(emailFilter.test(document.f.email_address.value))) {
		alert("Please enter a valid e-mail address (e.g. user@domain.com).");
		document.f.email_address.focus();
		return false;
	}
	else {
		var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
		if (document.f.email_address.value.match(illegalChars)) {
			alert("The e-mail address contains illegal characters. Please enter a valid e-mail address (e.g. user@domain.com).");
			document.f.email_address.focus();
			return false;
		}
	}
	document.f.submit();
}
function validateRes(numName) {
	var isitNum = numName.value;
	var isitNumName = numName.name;
	if (isNaN(isitNum)) {
		alert("Value must be numeric")
		numName.value = '';
		numName.focus()
		return false
	}
	if (isitNum < 0) {
		alert("Value must be a positive number")
		numName.value = '';
		numName.focus()
		return false
	}
	if (isitNumName == "locnum") {
		calcLoc();
	}
	return true;
}