// 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; 

/***************************************************
 * Function 	checkForEmpties
 * Purpose	This routine will check to see if the indicated fields
 *		have a value in them.  It returns true if all indicated
 *		fields have some sort of value in them, false otherwise.
 * Parms	obForm - The Form we are looking at
 *		aFldList - A list of fields to look at
 ***************************************************/
 function checkForEmpties(obForm, aFldList)
 {
 	sError = "Cannot Be Blank!";
 	sIfExp = "bIfValue = sVal =='';" ;
 	
 	return commonCheckRoutine(obForm, aFldList, sIfExp, sError, null)
 }

/*****************************************************************
*
*
*****************************************************************/
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);
}

/*****************************************************************
*
*
*****************************************************************/
function checkForPhoneNumbers()
{
	var Phone=document.MyForm.phone
	
	if ((Phone.value==null)||(Phone.value==""))
	{
		alert("Please Enter your Phone Number")
		Phone.focus()
		return false
	}
	if (checkInternationalPhone(Phone.value)==false){
		alert("Please Enter a Valid Phone Number in the Format XXX-XXX-XXXX or (XXX) XXX-XXXX.")
		Phone.value=""
		Phone.focus()
		return false
	}
	return true
 }
 
 /*******************************************************
 * Function Check For Valid Mail
 * Purpose	This routine will ensure that the given fields
 *				have a "valid" phone number contained within them.
 * Parms		obForm	- The Current form we are working with
 *				aFldList - The Set of fields that are to match.
 ********************************************************/
 function checkForValidMail(obForm, aFldList)
 {
		sMailPat = /^\w[\w\.-]*@[\w-]|\.([\w-]+\.)*\w$/;
		
		sError = "Format is Incorrect!";
		sIfExp = "bIfValue = !myRegExp.test(sVal);";
		
		return commonCheckRoutine(obForm, aFldList, sIfExp, sError, sMailPat);	
 }
 
 /*************************************************************
 * Function: capitlaizeMe
 **************************************************************/
function capitalizeMe(obj) 
{
        val = obj;
        newVal = '';
        val = val.split(' ');
        for(var c=0; c < val.length; c++) 
		{
           newVal += val[c].substring(0,1).toUpperCase() +
val[c].substring(1,val[c].length) + ' ';
        }
        obj.value = newVal;
		return newVal;
}
 
/******************************************************
  * Function 	commonCheckRoutine
  * Purpose		We note that a lot of the checkroutines have 
  *				commanility in their approach to solving the 
  *				validation problem.  This routine will allow us
  *				to centralize a lot of the checking algorithm in
  *				on function so as to cut down on potential errors.
  * Parms		obForm  - The Form we are checking.
  *				aFldList - The List of fields we wish to check.
  *				sIfExp - The expression to evaluate for our If Statement
  *				sError - The error statement to be printed with the alert

  *				myRegExpr - An optional parameter that includes a 
  *								regular expression to evaluate.
  *								Note the sIfExpr must set a variable 
  *								bIfValue to work with with this function
  *								It Expression needs to starts as bIfValue =			
 	*********************************************************/
 function commonCheckRoutine(obForm, aFldList, sIfExpr, sError, myRegExp)
 {						
	for(i=0; i < aFldList.length; i++)
  	{
  		exp = "sVal = obForm." + aFldList[i] + ".value  ;";
  		eval(exp);
  		eval(sIfExpr);
  				
  		if (bIfValue)
  		{
			formVal = capitalizeMe(aFldList[i]);
  			alert(formVal + sError);
			//alert(aFldList[i] + sError);
  			exp = "obForm." + aFldList[i] + ".focus();";
  			eval(exp);
  			return false;
  		}				
  	}
  	return true;
 }