
// FormCheck.js
//
// VARIABLE DECLARATIONS

var whitespace = " \t\n\r";
var defaultEmptyOK = false

// Check whether string s is empty.

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}



// Returns true if string s is empty or 
// whitespace characters only.

function isWhitespace (s)

{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

function validEmail (objName)
{ 
	if ((objName.value!=null) & (objName.value!=""))
	{ 
	 if (!isEmail(objName.value))
	   {
	 	alert(objName.title + ' has an invalid format');
		objName.focus();
		return false;
		}
	 }	
}
// returns true if the string only contains characters A-Z, a-z
function isAlpha(str){
  var re = /[^a-zA-Z]/g
  if (re.test(str)) return false;
  return true;
}

// returns true if the string is a valid email
function isEmail2(str){
  if(isEmpty(str)) return false;
  var re = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i
  return re.test(str);
}
// stronger email validation
function isEmail (s)
{   if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
   
    // is s whitespace?
    if (isWhitespace(s)) return false;
    
    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { 
	 i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;


    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least two characters after the .
    if ((i >= sLength - 2) || (s.charAt(i) != ".")) return false;
    //else if ((sLength - i - 1) > 3) return false;
	
	// Now check to see if the suffix is anything but A-Z or a-z
	//i++;
    //while ((i < sLength) && (isAlpha(s.charAt(i))))
    //{ 
	//i++
    // }

	//if (!isAlpha(s.charAt(i))) return false;
	if (!isEmail2(s)) return false;
	else return true;
}

/**
 * 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 = "()x- ";
// 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 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 validPhone (objName)
{ 
	if ((objName.value!=null) & (objName.value!=""))
	{ 
	 if (!isValidPhone(objName.value))
	   {
	 	alert(objName.title + ' has an invalid format');
		objName.focus();
		return false;
		}
		
     var theCount = 0;
     var theString = objName.value;
     var newString = "";
     var myString = theString;
     var theLen = myString.length;
     for ( var i = 0 ; i < theLen ; i++ )
     {
     // Character codes for ints 1 - 9 are 48 - 57
          if ( (myString.charCodeAt(i) >= 48 ) && (myString.charCodeAt(i) <= 57) )
          newString = newString + myString.charAt(i);   
     }
	// Now the validation to determine that the remaining string is 9 characters.
     if (newString.length == 10 )
     {
	// Now the string has been stripped of other chars it can be reformatted to (999) 999-9999
          var newLen = newString.length;
          var newPhone = "";
          for ( var i = 0 ; i < newLen ; i++ )
          {
               if ( i == 0 )
                    newPhone = newPhone + "(" + newString.charAt(i);
               else if ( i == 2 ) 
                    newPhone = newPhone + newString.charAt(i) + ") ";
               else if ( i == 5 )
                    newPhone = newPhone + newString.charAt(i) + "-";
               else
                 newPhone = newPhone + newString.charAt(i);
          }
          objName.value = newPhone;
          return true;
	   }	  
	 }	
}

function checkInternationalPhone(strPhone){
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function isValidPhone(strPhone){
	
	if (checkInternationalPhone(strPhone)==false){
		return false
	}
	return true
 }


<!-- script start

	// validates the Integer in the field of the form 
	function CheckInteger(CF_Form)
	{
		//Returns false if both text fields are empty

		if (CF_Form.empcode.value.length == 0)
		{
			alert("Please enter a valid Email Address");
			return false;
		}

		if (!isEmail(CF_Form.empcode.value) & CF_Form.empcode.value != '')
		{
			alert("Email Address has an invalid format");
			return false;
		}


		//Returns false if pin has less than 4 characters
		if (CF_Form.pin.value.length != 4)
		{
			alert("Please enter 4 digits as your password");
			return false;
		}
		//Returns true if value is a number containing only the characters 0-9.
		
		var number_format = "0123456789";
		var check_char;
		var digits = true;
		
		//Remaining characters can be only a digit.
		for (var i = 0; i <=3; i++)
		{
			check_char = number_format.indexOf(CF_Form.pin.value.charAt(i))
	
			if (check_char < 0)
			{
				digits = false;
				break;
			}
		}
	
		if (!digits)
			{
			alert("Please enter 4 digits as your password");
			return false;
			}
		//All tests passed, so...
		return true
	}
	function HelpWindow()
	{
		window.open("HelpFrame.cfm?empcode=000000&page=Start1.cfm","newWindow","width=550,height=500,scrollbars=1,menubar=0,location=0,toolbar=0,status=1,resizable=1");
	}
// script end -->
