//Added by Les: checkPhone2 - checks 7 digit phone

// FormChek.js
//
// 18 Feb 97 created Eric Krock
//
// (c) 1997 Netscape Communications Corporation


var digits = "0123456789";

var lowercaseLetters = "abcdefghijklmnopqrstuvwxyz"

var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

var whitespace = " \t\n\r";


var decimalPointDelimiter = "."


var phoneNumberDelimiters = "()- ";


var validUSPhoneChars = digits + phoneNumberDelimiters;


var validWorldPhoneChars = digits + phoneNumberDelimiters + "+";


var SSNDelimiters = "- ";

var validSSNChars = digits + SSNDelimiters;

var digitsInSocialSecurityNumber = 9;

var digitsInUSPhoneNumber = 10;

var mPrefix = "You did not enter a value into the "
var mSuffix = " field. This is a required field. Please enter it now."

var s = ""
var sPhone = "Phone Number"
var sEmail = "Email"

var iUSPhone = "This field must be a 10 digit U.S. phone number (like 415 555 1212). Please reenter it now."
var iUSPhone2 = "This field must be a 7 digit U.S. phone number (like 5551212). Please reenter it now."
var iInteger = "This field must be a valid integer. Please reenter it now."
var iIntegerLength = "The Area code field must contain a 3 digit integer. Please reenter now."
var iEmail = "This field must be a valid email address (like foo@bar.com). Please reenter it now."

var pEntryPrompt = "Please enter a "
var pUSPhone = "10 digit U.S. phone number (like 415 555 1212)."
var pEmail = "valid email address (like foo@bar.com)."

var defaultEmptyOK = false


function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}


function isWhitespace (s)

{   var i;

    if (isEmpty(s)) 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;
    }

    return true;
}



function stripCharsInBag (s, bag)

{   var i;
    var returnString = "";


    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }

    return returnString;
}



function stripCharsNotInBag (s, bag)

{   var i;
    var 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 stripWhitespace (s)

{   return stripCharsInBag (s, whitespace)
}





function charInString (c, s)
{   for (i = 0; i < s.length; i++)
    {   if (s.charAt(i) == c) return true;
    }
    return false
}



function stripInitialWhitespace (s)

{   var i = 0;

    while ((i < s.length) && charInString (s.charAt(i), whitespace))
       i++;
    
    return s.substring (i, s.length);
}


function isLetter (c)
{   return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

function isLetterOrDigit (c)
{   return (isLetter(c) || isDigit(c))
}


function isInteger (s)

{   var i;

    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) return false;

    }

    // All characters are numbers.
    return 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))
    }
}



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





function isNonnegativeInteger (s)
{   var secondArg = defaultEmptyOK;

    if (isNonnegativeInteger.arguments.length > 1)
        secondArg = isNonnegativeInteger.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 number >= 0

    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) >= 0) ) );
}


// isNegativeInteger (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 isNegativeInteger (s)
{   var secondArg = defaultEmptyOK;

    if (isNegativeInteger.arguments.length > 1)
        secondArg = isNegativeInteger.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 negative, not positive, number

    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) < 0) ) );
}



function isNonpositiveInteger (s)
{   var secondArg = defaultEmptyOK;

    if (isNonpositiveInteger.arguments.length > 1)
        secondArg = isNonpositiveInteger.arguments[1];

    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) <= 0) ) );
}


function isIntegerInRange (s, a, b)
{   if (isEmpty(s)) 
       if (isIntegerInRange.arguments.length == 1) return defaultEmptyOK;
       else return (isIntegerInRange.arguments[1] == true);

    if (!isInteger(s, false)) return false;
    var num = parseInt (s);
    return ((num >= a) && (num <= b));
}


function prompt (s)
{   window.status = s
}




function promptEntry (s)
{   window.status = pEntryPrompt + s
}


function warnEmpty (theField, s)
{   theField.focus()
    alert(mPrefix + s + mSuffix)
    return false
}


function warnInvalid (theField, s)
{   theField.focus()
    theField.select()
    alert(s)
    return false
}


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 warnEmpty (theField, s);
    else return true;
}

//created by Les White 10/16/03 to check isInteger plus length = len
function checkIntegerLength (theField, len, emptyOK)
{   if (checkIntegerLength.arguments.length == 2) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (isWhitespace(theField.value)) 
       return warnEmpty (theField, theField.name);
	   else
    {  var justInteger = stripCharsNotInBag(theField.value, "1234567890")
       if (!isInteger(justInteger, false)) 
          return warnInvalid (theField, iInteger);
       else if (justInteger.length != len)
	   return warnInvalid (theField, iIntegerLength);
	   else
       {  theField.value = justInteger
          return true;
       }
    }
}

//modified to strip out non-numeric characters and return that stripped value to field
function checkInteger (theField, emptyOK)
{   if (checkInteger.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (isWhitespace(theField.value)) 
       return warnEmpty (theField, theField.name);
	   else
    {  var justInteger = stripCharsNotInBag(theField.value, "1234567890")
       if (!isInteger(justInteger, false)) 
          return warnInvalid (theField, iInteger);
	   else
       {  theField.value = justInteger
          return true;
       }
    }
}

//This function added by Les White from JavaScript Source

function isValidDate(theField, dateStr, emptyOK) 
// Checks for the following valid date formats:
// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
// Also separates date into month, day, and year variables
{
    if (isValidDate.arguments.length == 2) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(dateStr))) return true;
    if (isWhitespace(dateStr)) 
       return warnEmpty (theField, theField.name);
    else 

{
var datePat = /^(\d{1,2}|\d{4})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;

// To require a 4 digit year entry, use this line instead:
// var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;

var matchArray = dateStr.match(datePat); // is the format ok?
if (matchArray == null) {
alert("Date is not in a valid format.")
return false;
}
var firstNum = matchArray[1];
if (firstNum > 100) {
year = matchArray[1]; // parse date into variables
month = matchArray[3];
day = matchArray[4];
}
else {
month = matchArray[1]; // parse date into variables
day = matchArray[3];
year = matchArray[4];
} //if (matchArray[1].
if (month < 1 || month > 12) { // check month range
alert("Month must be between 1 and 12.");
return false;
}
if (day < 1 || day > 31) {
alert("Day must be between 1 and 31.");
return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Month "+month+" doesn't have 31 days!")
return false
}
if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day>29 || (day==29 && !isleap)) {
alert("February " + year + " doesn't have " + day + " days!");
return false;
   }
}
return true;  // date is valid
}
}


// Get checked value from radio button.

function getRadioButtonValue (radio)
{   var found = 0
	for (var i = 0; i < radio.length; i++)
    {   if (radio[i].checked) 
		{   found = 1
			break }
    }
    if (found == 1)
	{ return radio[i].value; }
	else
	{ return false; }
}

function checkRadio(theField, theMessage)
{	var bdvalue= false
	bdvalue= eval(getRadioButtonValue(theField))
   if (bdvalue != false)
     { return true; }
	else
{ alert ("You have not selected " + theMessage + ". This is a required field. Please click OK and correct.");
return false;}
}

function isEmail (s)
{   if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
   
    if (isWhitespace(s)) return false;
    
    var i = 1;
    var sLength = s.length;

    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}

function checkEmail (theField, emptyOK)
{   if (checkEmail.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else if (!isEmail(theField.value, false)) 
       return warnInvalid (theField, iEmail);
    else return true;
}


//  End -->
