// JavaScript Document
// some code modified from Validation script by Henrik Petersen / NetKontoret
// www.echoecho.com/jsforms.htm
function emailvalidation(entered, alertbox)
{
with (entered)
{
apos=value.indexOf("@");
dotpos=value.lastIndexOf(".");
lastpos=value.length-1;
if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2)
{if (alertbox) {alert(alertbox);} return false;}
else {return true;}
}
}

function valuevalidation(entered, min, max, alertbox, datatype)
{
with (entered)
{
checkvalue=parseFloat(value);
if (datatype)
{smalldatatype=datatype.toLowerCase();
if (smalldatatype.charAt(0)=="i") {checkvalue=parseInt(value)};
}
if ((parseFloat(min)==min && checkvalue<min) || (parseFloat(max)==max && checkvalue>max) || value!=checkvalue)
{if (alertbox!="") {alert(alertbox);} return false;}
else {return true;}
}
}


function digitvalidation(entered, min, max, alertbox, datatype)
{
with (entered)
{
checkvalue=parseFloat(value);
if (datatype)
{smalldatatype=datatype.toLowerCase();
if (smalldatatype.charAt(0)=="i")
{checkvalue=parseInt(value); if (value.indexOf(".")!=-1) {checkvalue=checkvalue+1}};
}
if ((parseFloat(min)==min && value.length<min) || (parseFloat(max)==max && value.length>max) || value!=checkvalue)
{if (alertbox!="") {alert(alertbox);} return false;}
else {return true;}
}
}


function emptyvalidation(entered, alertbox)
{
with (entered)
{
if (value==null || value=="")
{if (alertbox!="") {alert(alertbox);} return false;}
else {return true;}
}
}

function CheckPhoneNumber(PhoneNo,TagStr)

{
	// get rid of non-numerics
	var checkOK = new String("0123456789");
	var strNo = new String(PhoneNo.value);
	PhoneNo.value="";
	for ( i = 0; i < strNo.length; i++ )
	{
		ch = strNo.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
				break;
		if (j != checkOK.length)
		{
			PhoneNo.value=String(PhoneNo.value)+String(ch);
		}
	}


	// get rid of leading 1
	if ( ( PhoneNo.value.length == 11 ) && ( PhoneNo.value.charAt(0) == 1 ) )
	{
		PhoneNo.value = PhoneNo.value.substr(1,10);
	}

	if ( (PhoneNo.value.length != 0) && (PhoneNo.value.length < 10) )
	{
		alert("Please enter exactly 10 digits in \"" + TagStr + "\".");
		PhoneNo.focus();
		return (false);
	}

	if (PhoneNo.value.length > 10)
	{
		alert("Please enter exactly 10 digits in \"" + TagStr + "\".");
		PhoneNo.focus();
		return (false);
	}

	var checkStr = PhoneNo.value;
	var allValid = true;
	var decPoints = 0;
	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
		if (ch == checkOK.charAt(j))
		break;
		if (j == checkOK.length)
		{
			allValid = false;
			break;
		}
	}
	if (!allValid)
	{
		alert("Please enter only digits in \"" + TagStr + "\".");
		PhoneNo.focus();
		return (false);
	}

	if ( checkStr.length == 10 )
	{
		if ( checkStr.charAt(0) < '2' )
		{
			alert("The first digit of the area code in \"" + TagStr + "\" cannot be a '1' or '0'");
			PhoneNo.focus();
			return (false);
		}

		if ( checkStr.charAt(3) < '2' )
		{
			alert("The first digit of the prefix in \"" + TagStr + "\" cannot be a '1' or '0'");
			PhoneNo.focus();
			return (false);
		}

		if ( checkStr.substring(0,3) == "900" )
		{
			alert("The area code in \"" + TagStr + "\" cannot be '900'");
			PhoneNo.focus();
			return (false);
		}
	}

	else
		return (true);
}




function contactvalidation(thisform)
{
// This function checks the entire form before it is submitted
with (thisform)
{
if (emptyvalidation(name,"Please enter your full name.")==false) {name.focus(); return false;};
if (emailvalidation(email,"Please enter a valid email address.")==false) {email.focus(); return false;};
//if (CheckPhoneNumber(phone,"your phone number")==false) {alert("Please enter a valid 10 digit phone number.") phone.focus(); return(false);};
if (emptyvalidation(phone,"Please enter your 10 digit phone number.")==false) {phone.focus(); return false;};
if (digitvalidation(zip,5,5,"Please enter your 5 digit zip code","I")==false) {zip.focus(); return false;};
}
}