var emailregex = /^[\w\_\-\.]+@[\w\_\-\.]+\.\w+$/;

//define available digits
var PHdigits = "0123456789";

// non-digit characters which are allowed in phone numbers
var PHdelimiters = "()- .\/";

// characters which are allowed in US phone numbers
var PHvalidchar = PHdigits + PHdelimiters;

// U.S. phone numbers have 10 digits, without delimiters.
// They are formatted as 123 456 7890 or (123) 456-7890.
var PHnumofdigits = 10;

var PHalert = "Please enter a 10 digit U.S. phone number.";

var PHdefaultEmptyOK = false;

var ZIPdigits = "0123456789";

//a zip contains 5 digits
//this script does not accomodate for specific zip codes like 57252-3101
var ZIPnumofdigits = 5;

var ZIPalert = "Please enter a 5 digit U.S. zip code.";

var ZIPdefaultEmptyOK = true;


function checkEmail(theField, emptyOK)
{
	if(theField.value.length == 0 && (emptyOK))
	{
		return true;
	}
	else
	{
		if(!theField.value.match(emailregex))
			{
			alert("The email address  " + theField.value + "  is not in a correct format.\n Please try again");
			theField.focus();
			theField.select();
			return false;
		}
	}

}

function PHstripCharsInBag (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;
}

//************************* isUSPhoneNumber Function *******************************
//called from checkUSPhone function

function isUSPhoneNumber (s)
{   
    return (PHisInteger(s) && s.length == PHnumofdigits)
}

//************************* PHisInteger Function *******************************
//called from isUSPhoneNumber function

function PHisInteger (s)
{   var i;

    if (PHisEmpty(s)) 
       if (PHisInteger.arguments.length == 1) return PHdefaultEmptyOK;
       else return (PHisInteger.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if (!PHisDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}

//************************* isEmpty Function *******************************
//called from PHisInteger function

// Check whether string s is empty.
function PHisEmpty(s)
{   return ((s == null) || (s.length == 0))
}

//************************* PHisDigit Function *******************************
//called from PHisInteger function

// Returns true if character c is a digit 
// (0 .. 9).
function PHisDigit (c)
{   return ((c >= "0") && (c <= "9"))
}


//************************* PHwarnInvalid Function ****************************
//called from checkUSPhone function

// Notify user that contents of field theField are invalid.
// String s describes expected contents of theField.value.
// Put select theField, put focus in it, and return false.
function PHwarnInvalid (theField, s)
{   alert(s)
    theField.focus()
    theField.select()
    return false;
}

//************************* checkUSPhone Function *******************************
//called from form 

// Check that string theField.value is a valid US Phone.

function checkUSPhone (theField, emptyOK)
{ 

if(theField.value.length == 0 && (emptyOK))
	{
		return true;
	}
else
{  
 var PHnormalized = PHstripCharsInBag(theField.value, PHdelimiters)
 if (!isUSPhoneNumber(PHnormalized, false)) 
    return PHwarnInvalid(theField, PHalert);
 else 
 {  // if you don't want to reformat as (123) 456-789, comment next line out
    theField.value = reformatUSPhone(PHnormalized)
    return true;
 }
}

}


//************************* PHreformat Function *******************************
//called from reformatUSPhone function

//           "(", 3, ") ", 3, "-", 4)

function PHreformat (s)
{   var arg;
    var stringPos = 0;
    var resultString = "";
    for (var i = 1; i < PHreformat.arguments.length; i++) {
       arg = PHreformat.arguments[i];
       if (i % 2 == 1) resultString += arg;
       else {
           resultString += s.substring(stringPos, stringPos + arg);
           stringPos += arg;
       }
    }
    return resultString;
}

//************************* reformatUSPhone Function *******************************
//called from checkUSPhone function

// takes USPhone, a string of 10 digits
// and reformats as (123) 456-789
function reformatUSPhone (USPhone)
{   return (PHreformat (USPhone, "(", 3, ") ", 3, "-", 4))
}


//************************* checkUSZip Function *******************************
//called from form 

// Check that string theField.value is a valid US Zip Code.

function checkUSZip(theField, emptyOK)
{  

if(theField.value.length == 0 && (emptyOK))
	{
		return true;
	}
else
{
 
 var normalized = theField.value
 if (!isUSZip(normalized, false)) 
    return ZIPwarnInvalid(theField, ZIPalert);
 else 
 { 
    return true;
 }

} 

}

function submitForm(theForm) {
	// need to verify that the required fields have values.
	// firstName, lastName, phone, email address
	if ( theForm.first_Name.value == "") {
		alert ("You must enter a first name.");
		return false;
	}
	if ( theForm.last_Name.value == "") {
		alert ("You must enter a last name.");
		return false;
	}


for (var i=0; i < theForm.contact_method.length; i++)
   {
   if (theForm.contact_method[i].checked)
      {
      var rad_val = theForm.contact_method[i].value;
      }
   }

	if ( rad_val=="email" && theForm.email.value == "") {
		alert ("You must enter an email address.");
		return false;
	}

	if ( rad_val=="phone" && theForm.phone.value == "") {
		alert ("You must enter a phone number.");
		return false;
	}


}

