// ------------------------ BEGIN PHONE NUMBER CHECK -------------------------
//PhoneVerify.js

// isUSPhoneNumber returns true if string s is a valid U.S. Phone
// Number.  Must be 10 digits.
//
// NOTE: Strip out any delimiters (spaces, hyphens, parentheses, etc.)
// from string s before calling this function.

/* 
This file is used to verify US Phone Numbers.  All variables begin with PH to differentiate
from other variables that may be present in other functions.

Place a call to the function onChange="return checkUSPhone(this, true);" in appropriate input box

use true if the input box can be left empty, use false if it is required.
*/

// ---------------------- VARIABLE DECLARATIONS -------------------------------

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

//************************* PHstripCharsInBag Function *******************************
//called from checkUSPhone function
// Removes all characters which appear in string bag (PHdelimiters) from string s.
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))
}


// END PHONE NUMBER CHECK 