


//****************************************************************** 
//Author: Kevin VanderWoude
//Last Updated: 7/18/06
//Generic Javascript Form Validation Functions 
//****************************************************************** 

/**
 * DHTML phone number validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

// 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;

//Support function for Phone Validation Script
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;
}

//Support function for Phone Validation Script
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;
}

//Support function for Phone Validation Script
function checkInternationalPhone(strPhone){
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}



//Use to validate phone and fax fields (for required fields only)
function validate_phonefax(field,alerttxt)
{
		with (field)
		{
			if ((value==null)||(value==""))
				{alert(alerttxt);return false}
			//else {return true}
			{
				if (checkInternationalPhone(value)==false)
					{alert(alerttxt);return false}
				else 
					{ return true }
			}
		}
}
 
	
//Use to validate email fields
function validate_email(field,alerttxt)
{
		with (field)
		{
				apos=value.indexOf("@")
				dotpos=value.lastIndexOf(".")
				if (apos<1||dotpos-apos<2) 
			 	 {alert(alerttxt);return false}
				else {return true}
		}
}
	

//Use to validate text fields
function validate_required(field,alerttxt)
{
		with (field)
		{
		if (value==null||value=="")
  		{alert(alerttxt);return false}
		//else {alert("GO GO! " + alerttxt);return true}
		else {return true}
		}
} 


//Use to validate select drop-down box
function validate_dropdown(field,alerttxt) 
{
	
    	with (field)
		{
    	if (selectedIndex == 0) 
  		{alert(alerttxt);return false}
		else {return true}
		}
}     


//Use this function to validate radio buttons
function validate_radio(field,alerttxt)
{
		myOption = -1;
		for (i=field.length-1; i > -1; i--) 
			{ 
			if (field[i].checked) 
				{
				myOption = i;
				}
			}
			
		if (myOption == -1)
		{alert(alerttxt);return false}
		else {return true}
		
} 
	
function validate_numeric(field,alerttxt)
{
	var strValidChars = "0123456789.-";
   	var strChar;
	var blnResult = true;
	
	with (field)
	{
		if (value.length == 0) return false;
		//  test strString consists of valid characters listed above
			
		for (i = 0; i < value.length && blnResult == true; i++)
      	{
      		strChar = value.charAt(i);
      		if (strValidChars.indexOf(strChar) == -1)
         	{
         	blnResult = false;

         	}
      	}
		
		if (blnResult==false)
		{alert(alerttxt);return false}
		
   		//return blnResult;
		
	}

}