// JAVASCRIPT FORM VALIDATION SUBROUTINES

// VALIDATE EMAIL ADDRESS
function valEmail(pFld)
{
	// If it is blank that is ok, just checking if it is a valid email address.
	if (pFld.value == "") {	return true; }
	
	with (pFld)
	{
		atPos  = value.indexOf("@");
		dotPos = value.lastIndexOf(".");		
		if (atPos < 1 || dotPos-atPos < 2)
		{
			pFld.focus();
			pFld.select();
			window.alert('Not a valid Email Address.');
			return false;
		}
	}
}

// VALIDATE FIELD NOT EMPTY
function valFldNotEmpty(pFld, pAlertFld)
{
	if (pFld.value === "")
	{
		pFld.focus();
		pFld.select();
		window.alert(pAlertFld+" Required.");
		return false;
	}	
}

// VALIDATE IF 2 FIELDS MATCH
function val2FldsMatch(pFld1, pFld2, pAlertFld)
{		
	if (pFld1.value != pFld2.value)
	{
		pFld1.focus();
		pFld1.select();
		window.alert("Your "+pAlertFld+" did not match.");
		pFld2.value = "";
		return false;
	}	
}

// VALIDATE DROPDOWN FIELD (one must be selected)
function valDropdown(pFld, pAlertFld)
{
	var vFldValue = pFld.selectedIndex;
	
	if (vFldValue == 0)
	{
		window.alert("Please select one from the "+pAlertFld+" Drop-Down List.");
		pFld.focus();
		return false;
	}
}

// VALIDATE CURRENCY FIELD
function valDollar(pFld, pAlertFld)
{	
	var i;
	var vFldValue = pFld.value;
	var vChars = "0123456789.,$";
	
	if (vFldValue == "")
	{
		pFld.value = "$0.00";
		pFld.focus();
		pFld.select();
		return false;
	}	
	
	for (i = 0; i < vFldValue.length; i++)
	{
		if (vChars.indexOf(vFldValue.charAt(i)) == -1)
		{
			window.alert("Invalid "+pAlertFld+".\n\nOnly numbers (0-9), a dollar sign, a comma, and a period are allowed in this field.");
			pFld.focus();
			pFld.select();
			return false;
		}
	}
}

// VALIDATE PHONE NUMBER
function valPhone(pFld)
{
	// Declaring required variables
	var digits = "0123456789";
	// non-digit characters which are allowed in phone numbers
	var phoneNumberDelimiters = "()-. ";
	// characters which are allowed in international phone numbers
	// (a leading + is OK)
	var validWorldPhoneChars = phoneNumberDelimiters + "+";
	// Minimum no of digits in an international phone no.
	var minDigitsInIPhoneNumber = 10;
	
	// If it is blank that is ok, just checking if it is a valid phone number.
	if (pFld.value == "") {	return true; }
	
	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 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)
	{
		s = stripCharsInBag(strPhone,validWorldPhoneChars);
		return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
	}
		
	if (checkInternationalPhone(pFld.value)==false){
		pFld.focus();
		pFld.select();
		window.alert('Not a valid Phone Number. e.g., (555) 555-5555');
		return false;
	}
}

// VALIDATE ZIP CODE
function valZip(pFld)
{
	var i;
	var temp;
	var valid = "0123456789-";
	var hyphencount = 0;
	var vFldValue = pFld.value;
	
	// If it is blank that is ok, just checking if it is a valid zip code.
	if (pFld.value == "") {	return true; }
	
	if (vFldValue.length != 5 && vFldValue.length != 10)
	{
		pFld.focus();
		pFld.select();
		window.alert("Not a valid Zip Code. 5 digit or 5 digit+4 Zip Code. e.g., 12345 or 12345-6789");
		return false;
	}
	
	for (i=0; i < vFldValue.length; i++)
	{
		temp = "" + vFldValue.substring(i, i+1);
		
		if (temp == "-")
		{
			hyphencount++;
		}
		
		if (valid.indexOf(temp) == "-1")
		{
			pFld.focus();
			pFld.select();
			window.alert('Invalid characters in your Zip Code.  Please try again.');
			return false;
		}
		
		if ((hyphencount > 1) || ((vFldValue.length==10) && "" + vFldValue.charAt(5) != "-"))
		{
			pFld.focus();
			pFld.select();
			alert("The hyphen character should be used with a properly formatted 5 digit+4 Zip Code, like '12345-6789'. Please try again.");
			return false;
		}
	}	
}








