//Declare global variables: rV = Value Returned By AJAX; xmlHttp = AJAX Object; stopArrows = Used to stop scrolling with arrow keys

var rV;
var xmlHttp;
var stopArrows=0;

//For drop down boxes

function showOptions(selct, n)
{
document.getElementById("optionsDiv" + n).style.display="inline";
document.getElementById(selct).focus();
}

//Also for drop down boxes

function setVal(selct, inpt, n)
{
document.getElementById(inpt).value=document.getElementById(selct).value;
document.getElementById("optionsDiv" + n).style.display="none";
}

//Formats phone number

function addDash()
{
phone=document.getElementById("phone").value;
len=phone.length;
firstChar=phone.slice(0,1);
if (len==3 && !(firstChar=="(")){document.getElementById("phone").value="(" + phone + ") ";}
if (len==5){document.getElementById("phone").value=phone.slice(0,len-1);}
if (len==9){document.getElementById("phone").value=phone + "-";}
if (len==10){document.getElementById("phone").value=phone.slice(0,len-1);}
}

//Only allow numbers to be entered in fields that call this function

function numbersOnly(evt)
{
try
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;

return true;
}
catch (e)
{
}
}

//AJAX to get city and state based on zipcode

function getCityState()
	{

	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  } 
	var url="zipcode.php?func=getinfo&divider=d&zip=" + document.getElementById("zip").value;
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	}

function stateChanged() 
{ 
if (xmlHttp.readyState==4)
{ 
returnedValue = xmlHttp.responseText;
cityEnd=returnedValue.indexOf("#");
city=returnedValue.slice(0,cityEnd);
stateEnd=returnedValue.lastIndexOf("#");
state=returnedValue.slice(cityEnd + 1,stateEnd);
document.getElementById("cityLabel").style.display="inline";
document.getElementById("cityInput").style.display="inline";
document.getElementById("stateLabel").style.display="inline";
document.getElementById("stateInput").style.display="inline";
document.getElementById("city").value=city;
document.getElementById("state").value=state;
}
}

//Checks to see if the CAPTCHA code is correct

function checkCode()
	{
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  } 
	var url="contactfiles/check.php?code=" + document.getElementById("code").value;
	xmlHttp.onreadystatechange=checkCodeChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	}

function checkCodeChanged() 
{
if (xmlHttp.readyState==4)
{
rV = xmlHttp.responseText;
validate();
}
}

//Creates AJAX object

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;
}

//Validates form

function validate()
{

document.getElementById("captchaProb").style.display="none";
document.getElementById("emailProb").style.display="none";
document.getElementById("firstNameProb").style.display="none";
document.getElementById("lastNameProb").style.display="none";
document.getElementById("phoneProb").style.display="none";

showAlert = 0;
if (!(rV==1))
{
showAlert = 1;
document.getElementById("codeCaptcha").style.display="inline";
document.getElementById("captchaProb").style.display="inline";
document.getElementById('captcha').src='contactfiles/securimage_show.php?' + Math.random();
document.getElementById('code').value="";
}
if (document.getElementById("firstName").value=="")
{
showAlert = 1;
document.getElementById("codeName").style.display="inline";
document.getElementById("firstNameProb").style.display="inline";
}
if (document.getElementById("lastName").value=="")
{
showAlert = 1;
document.getElementById("codeName").style.display="inline";
document.getElementById("lastNameProb").style.display="inline";
}
if (document.getElementById("phone").value=="")
{
showAlert = 1;
document.getElementById("codePhone").style.display="inline";
document.getElementById("phoneProb").style.display="inline";
}
emailAddress=document.getElementById("email").value;
atSymbol=emailAddress.indexOf("@");
if (document.getElementById("email").value=="" || atSymbol==(-1))
{
showAlert = 1;
document.getElementById("codeEmail").style.display="inline";
document.getElementById("emailProb").style.display="inline";
}
if (showAlert==1){coverAll();}
if (!(showAlert==1)) {document.getElementById("cform").submit();}
}

//If form is invalid covers screen with shadow and shows error box

function coverAll()
{
scrolledX = 0; scrolledY = 0;
if( typeof( window.pageYOffset ) == 'number' ){scrolledY = window.pageYOffset; scrolledX = window.pageXOffset;} 
else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {scrolledY = document.body.scrollTop; scrolledX = document.body.scrollLeft;} 
else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {scrolledY = document.documentElement.scrollTop; scrolledX = document.documentElement.scrollLeft;}
document.getElementById("coverAllShadow").style.display="inline";
var w = window.screen.width + "px";
var h = window.screen.height + "px";
document.getElementById("coverAllShadow").style.width=w;
document.getElementById("coverAllShadow").style.height=h;
document.getElementById("coverAllShadow").style.top=scrolledY + "px";
document.getElementById("infoContent").style.display="inline";
var newH = 0;
if( typeof( window.innerWidth ) == 'number' ) {newH = window.innerHeight; newW = window.innerWidth;} 
else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {newH = document.documentElement.clientHeight; newW = document.documentElement.clientWidth;} 
else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {newH = document.body.clientHeight; newW = document.body.clientWidth;}
var boxW = newW/2 - 150 + scrolledX;
var boxH = newH/2 - 110 + scrolledY;
document.getElementById("infoContent").style.top=boxH + "px";
document.getElementById("infoContent").style.left=boxW + "px";
stopArrows=1;
document.getElementById("bdy").style.overflow="hidden";
document.getElementById("htl").style.overflow="hidden";
}

//Hides error box and shadow

function uncoverAll()
{
document.getElementById("coverAllShadow").style.display="none";
document.getElementById("bdy").style.overflow="auto";
document.getElementById("htl").style.overflow="auto";
document.getElementById("infoContent").style.display="none";
document.getElementById("codeCaptcha").style.display="none";
document.getElementById("codeName").style.display="none";
document.getElementById("codeName").style.display="none";
document.getElementById("codePhone").style.display="none";
document.getElementById("codeEmail").style.display="none";
stopArrows=0;
}

//Reloads CAPTCHA

function reloadCaptcha()
{
document.getElementById('captcha').src='contactfiles/securimage_show.php?' + Math.random();
return false
}

//If everything is covered by shadow and error message is showing this does not allow arrow keys

keyCatcher=function(e)
{
if (stopArrows==1){
        if(e)
        {
                if (e.stopPropagation) e.stopPropagation();
                e.cancelBubble = true;
                e.returnValue = false;
        }
        /* You need to return false here for Firefox! */
        return false;
}
}
 
//Catches all key strokes and passes them to the keyCatcher function 

document.onkeypress=keyCatcher;
