
// VARIABLE DECLARATIONS

var digits = "0123456789";
var whitespace = " \t\n\r";	// whitespace characters
var defaultEmptyOK = false;
// 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 isEmpty(s)		// Check whether string s is empty.
{   return ((s == null) || (s.length == 0)) }

function isWhitespace (s)	// Returns true if string s is empty or whitespace characters only.
{   var i;
    if (isEmpty(s)) return true;	// Is s empty?
    // 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++)
    {   
        var c = s.charAt(i);	// Check that current character isn't whitespace.
        if (whitespace.indexOf(c) == -1) return false;
    }
    return true;		// All characters are whitespace.
}

//----------------------------------------------------------------------------------------------------
// Removes initial (leading) whitespace characters from s.
// Global variable whitespace (see above)
// defines which characters are considered whitespace.

function stripInitialWhitespace (s)		// Removes initial (leading) whitespace characters from s.
{   var i = 0;
    while ((i < s.length) && charInString (s.charAt(i), whitespace))
       i++;
    return s.substring (i, s.length);
}

//----------------------------------------------------------------------------------------------------
// Returns true if character c is a digit 
// (0 .. 9).

function isDigit (c)
{   return ((c >= "0") && (c <= "9")) }

function isLetterOrDigit (c)	// Returns true if character c is a letter or digit.
{   return (isLetter(c) || isDigit(c)) }


//----------------------------------------------------------------------------------------------------
// isInteger (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if all characters in string s are numbers.
// Accepts non-signed integers only. Does not accept floating point, exponential notation, etc.
// We don't use parseInt because that would accept a string with trailing non-numeric characters.
// By default, returns defaultEmptyOK if s is empty.  There is an optional second argument called emptyOK.
// emptyOK is used to override for a single function call the default behavior which is specified globally by
// defaultEmptyOK.  If emptyOK is false (or any value other than true), the function will return false if s is empty.
// If emptyOK is true, the function will return true if s is empty.
//
// EXAMPLE FUNCTION CALL:     	RESULT:
// isInteger ("5")			true 
// isInteger ("")			      defaultEmptyOK
// isInteger ("-5")			false
// isInteger ("", true)			true
// isInteger ("", false)		false
// isInteger ("5", false)		true

function isInteger (s)
{   var i;
    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.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++)
    {   
        var c = s.charAt(i);	// Check that current character is number.
        if (!isDigit(c)) return false;
    }
    return true;		// All characters are numbers.
}

//----------------------------------------------------------------------------------------------------
// isSignedInteger (STRING s [, BOOLEAN emptyOK])
// Returns true if all characters are numbers; 
// first character is allowed to be + or - as well.
// Does not accept floating point, exponential notation, etc.
// We don't use parseInt because that would accept a string
// with trailing non-numeric characters.

// EXAMPLE FUNCTION CALL:		RESULT:
// isSignedInteger ("5")		true 
// isSignedInteger ("")			defaultEmptyOK
// isSignedInteger ("-5")		true
// isSignedInteger ("+5")		true
// isSignedInteger ("", false)		false
// isSignedInteger ("", true)		true

function isSignedInteger (s)

{   if (isEmpty(s)) 
       if (isSignedInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isSignedInteger.arguments[1] == true);

    else {
        var startPos = 0;
        var secondArg = defaultEmptyOK;

        if (isSignedInteger.arguments.length > 1)
            secondArg = isSignedInteger.arguments[1];

        // skip leading + or -
        if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
           startPos = 1;    
        return (isInteger(s.substring(startPos, s.length), secondArg))
    }
}

//----------------------------------------------------------------------------------------------------
// isPositiveInteger (STRING s [, BOOLEAN emptyOK])
// Returns true if string s is an integer > 0.
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isPositiveInteger (s)
{   var secondArg = defaultEmptyOK;
    if (isPositiveInteger.arguments.length > 1) secondArg = isPositiveInteger.arguments[1];

    // The next line is a bit byzantine.  What it means is:
    // a) s must be a signed integer, AND
    // b) one of the following must be true:
    //    i)  s is empty and we are supposed to return true for
    //        empty strings
    //    ii) this is a positive, not negative, number

    return (isSignedInteger(s, secondArg) && ( (isEmpty(s) && secondArg)  || (parseInt (s) > 0) ) );
}

//----------------------------------------------------------------------------------------------------
// isFloat (STRING s [, BOOLEAN emptyOK])
// 
// True if string s is an unsigned floating point (real) number. 
// Also returns true for unsigned integers. If you wish
// to distinguish between integers and floating point numbers,
// first call isInteger, like If (!isInteger (s, true)) && isFloat (s, true))
// Does not accept exponential notation.

function isFloat (s)
{   var i;
    var seenDecimalPoint = false;
    var decimalPointDelimiter = ".";
    if (isEmpty(s)) 
       if (isFloat.arguments.length == 1) return defaultEmptyOK;
       else return (isFloat.arguments[1] == true);
    if (s == decimalPointDelimiter) return false;

    // 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++)
    {   
        var c = s.charAt(i);	// Check that current character is number.
        if ((c == decimalPointDelimiter) && !seenDecimalPoint) seenDecimalPoint = true;
        else if (!isDigit(c)) return false;
    }
    return true;		// All characters are numbers.
}

//----------------------------------------------------------------------------------------------------
// isSignedFloat (STRING s [, BOOLEAN emptyOK])
// 
// True if string s is a signed or unsigned floating point 
// (real) number. First character is allowed to be + or -.
//
// Also returns true for unsigned integers. If you wish
// to distinguish between integers and floating point numbers,
// first call isSignedInteger, then call isSignedFloat.
//
// Does not accept exponential notation.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isSignedFloat (s)

{   if (isEmpty(s))
       if (isSignedFloat.arguments.length == 1) return defaultEmptyOK;
       else return (isSignedFloat.arguments[1] == true);

    else {
        var startPos = 0;
        var secondArg = defaultEmptyOK;

        if (isSignedFloat.arguments.length > 1)
            secondArg = isSignedFloat.arguments[1];

        // skip leading + or -
        if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
           startPos = 1;    
        return (isFloat(s.substring(startPos, s.length), secondArg))
    }
}

//----------------------------------------------------------------------------------------------------
// isIntegerInRange (STRING s, INTEGER a, INTEGER b [, BOOLEAN emptyOK])
// isIntegerInRange returns true if string s is an integer within the range of integer arguments a and b, inclusive.

function isIntegerInRange (s, a, b)
{   if (isEmpty(s))
       if (isIntegerInRange.arguments.length == 1) return defaultEmptyOK;
       else return (isIntegerInRange.arguments[1] == true);

    // Catch non-integer strings to avoid creating a NaN below,
    // which isn't available on JavaScript 1.0 for Windows.
    if (!isInteger(s, false)) return false;

    // Now, explicitly change the type to integer via parseInt
    // so that the comparison code below will work both on 
    // JavaScript 1.2 (which typechecks in equality comparisons)
    // and JavaScript 1.1 and before (which doesn't).
    var num = parseInt (s);
    return ((num >= a) && (num <= b));
}

//----------------------------------------------------------------------------------------------------
// checkString (TEXTFIELD theField, STRING s, [, BOOLEAN emptyOK==false])
// Check that string theField.value is not all whitespace.

function checkString (theField, s, emptyOK)
{   // Next line is needed on NN3 to avoid "undefined is not a number" error in equality comparison below.
    if (checkString.arguments.length == 2) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (isWhitespace(theField.value)) return false;
    else return true;
}

//----------------------------------------------------------------------------------------------------
// isYear (STRING s [, BOOLEAN emptyOK])
// isYear returns true if string s is a valid Year number.  Must be 2 or 4 digits only.
// For Year 2000 compliance, you are advised to use 4-digit year numbers everywhere.

function isYear (s)
{   if (isEmpty(s)) 
       if (isYear.arguments.length == 1) return defaultEmptyOK;
       else return (isYear.arguments[1] == true);
    if (!isNonnegativeInteger(s)) return false;
    return ((s.length == 2) || (s.length == 4));
}

//----------------------------------------------------------------------------------------------------
// isMonth (STRING s [, BOOLEAN emptyOK])
// isMonth returns true if string s is a valid month number between 1 and 12.
function isMonth (s)
{   if (isEmpty(s)) 
       if (isMonth.arguments.length == 1) return defaultEmptyOK;
       else return (isMonth.arguments[1] == true);
    return isIntegerInRange (s, 1, 12);
}

//----------------------------------------------------------------------------------------------------
// isDay (STRING s [, BOOLEAN emptyOK])
// isDay returns true if string s is a valid day number between 1 and 31.
function isDay (s)
{   if (isEmpty(s)) 
       if (isDay.arguments.length == 1) return defaultEmptyOK;
       else return (isDay.arguments[1] == true);   
    return isIntegerInRange (s, 1, 31);
}

//----------------------------------------------------------------------------------------------------
// daysInFebruary (INTEGER year)
// Given integer argument year, returns number of days in February of that year.
function daysInFebruary (year)
{   // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (  ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );
}

//----------------------------------------------------------------------------------------------------
// isDate (STRING year, STRING month, STRING day)
// isDate returns true if string arguments year, month, and day form a valid date.

function isDate (year, month, day)
{   // catch invalid years (not 2- or 4-digit) and invalid months and days.
    if (! (isYear(year, false) && isMonth(month, false) && isDay(day, false))) return false;

    // Explicitly change type to integer to make code work in both JavaScript 1.1 and JavaScript 1.2.
    var intYear = parseInt(year);
    var intMonth = parseInt(month);
    var intDay = parseInt(day);   

    if (intDay > daysInMonth[intMonth]) return false;	// catch invalid days, except for February
    if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;
    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 checkPhone(Phone){
	
	if ((Phone.value==null)||(Phone.value=="")){
		alert("Please Enter Phone Number")
		return false
	}
	if (checkInternationalPhone(Phone.value)==false){
		alert("Please Enter a Valid Phone Number")
		return false
	}
	return true
 }