// JavaScript Document
var digits = "0123456789";
var phoneNumberDelimiters = "()- ";
var validWorldPhoneChars = phoneNumberDelimiters + "+";
var minDigitsInIPhoneNumber = 10;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function trim(s)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
var bracket=3
strPhone=trim(strPhone)
if(strPhone.indexOf("+")>1) return false
if(strPhone.indexOf("-")!=-1)bracket=bracket+1
if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket)return false
var brchr=strPhone.indexOf("(")
if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")")return false
if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function check_req(field)
{
	with (field)
	{
		if (value==null||value=="")
			{style.backgroundColor="#880000";style.color="#EEEEEE";focus();return false;}
		else
			{style.backgroundColor="#dadad0";style.color="#111111";return true;}
	}
}
function check_email(field)
{
	with (field)
	{
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		if (apos<1||dotpos-apos<2) 
			{style.backgroundColor="#880000";style.color="#EEEEEE";focus();return false;}
		else
			{style.backgroundColor="#dadad0";style.color="#111111";return true;}
	}
}
function check_phone(field)
{
	if(check_req(field))
	{
		with(field)
		{
			if(!checkInternationalPhone(value))
				{style.backgroundColor="#880000";style.color="#EEEEEE";focus();return false;}
			else
				{style.backgroundColor="#dadad0";style.color="#111111";return true;}
		}
	}
}
function check_zip(field)
{
	with (field)
	{
		len=value.length;
		dotpos=value.indexOf("-");
		if (len!=5||dotpos-apos<2) 
			{style.backgroundColor="#880000";style.color="#EEEEEE";return false;}
		else
			{style.backgroundColor="#dadad0";style.color="#111111";return true;}
	}
}

function checkform_01(thisform)
{
	pass=true;
	with(thisform)
	{
		if(!check_req(first_name)) pass=false;
		if(!check_req(last_name)) pass=false;
		if(!check_req(address1)) pass=false;
		if(!check_req(city)) pass=false;
		if(!check_req(state)) pass=false;
		if(!check_req(zip)) pass=false;
		if(!check_email(email)) pass=false;
		if(!check_phone(dayphone)) pass=false;
		if(!check_req(reason)) pass=false;
	}
	return pass;
}
function clearform_01(thisform)
{
	with(thisform)
	{
		first_name.style.backgroundColor="#dadad0";firstname.style.color="#111111";
		last_name.style.backgroundColor="#dadad0";lastname.style.color="#111111";
		address1.style.backgroundColor="#dadad0";address1.style.color="#111111";
		city.style.backgroundColor="#dadad0";city.style.color="#111111";
		state.style.backgroundColor="#dadad0";state.style.color="#111111";
		zip.style.backgroundColor="#dadad0";zip.style.color="#111111";
		email.style.backgroundColor="#dadad0";email.style.color="#111111";
		dayphone.style.backgroundColor="#dadad0";dayphone.style.color="#111111";
		reason.style.backgroundColor="#dadad0";reason.style.color="#111111";
	}
}



