// JavaScript Document
//Form Check Functions in JavaScript
//The following functions are used to validate:
//1. Email Address
//2. Characters or String
//3. Numeric and Digit

// toInteger(s)  					   Converts the string to a valid Integer Number
// isEmpty (s)                    	   Check whether string s is empty
// isWhitespace (s)                    Check whether string s is empty or whitespace.
// isLetter (c)                        Check whether character c is an English letter 
// isDigit (c)                         Check whether character c is a digit 
// isLetterOrDigit (c)                 Check whether character c is a letter or digit.
// isInteger (s [,eok])                True if all characters in string s are numbers.
// isAlphabetic (s [,eok])             True if string s is English letters 
// isAlphanumeric (s [,eok])           True if string s is English letters and numbers only.
// isFloat (s [,eok])                  True if string s is an unsigned floating point (real) number. (Integers also OK.)
// isEmail (s [,eok])                  True if string s is a valid email address.
// isYear (s [,eok])                   True if string s is a valid Year number.
// isIntegerInRange (s, a, b [,eok])   True if string s is an integer between a and b, inclusive.
// isDate (year, month, day)           True if string arguments form a valid date.
// checkString (theField, s [,eok])    Check that theField.value is not empty or all whitespace.

// VARIABLE DECLARATIONS
var daysInMonth = Array(0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

var digits = "0123456789";

var lowercaseLetters = "abcdefghijklmnopqrstuvwxyz"

var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

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

// decimal point character differs by language and culture
var decimalPointDelimiter = "."

// Global variable defaultEmptyOK defines default return value 
// for many functions when they are passed the empty string. 
// By default, they will return defaultEmptyOK.
//
// defaultEmptyOK is false, which means that by default, 
// these functions will do "strict" validation.  Function
// isInteger, for example, will only return true if it is
// passed a string containing an integer; if it is passed
// the empty string, it will return false.
//
// You can change this default behavior globally (for all 
// functions which use defaultEmptyOK) by changing the value
// of defaultEmptyOK.
//
// Most of these functions have an optional argument emptyOK
// which allows you to override the default behavior for 
// the duration of a function call.
//
// This functionality is useful because it is possible to
// say "if the user puts anything in this field, it must
// be an integer (or a phone number, or a string, etc.), 
// but it's OK to leave the field empty too."
// This is the case for fields which are optional but which
// must have a certain kind of content if filled in.
var defaultEmptyOK = false

// CONSTANT STRING DECLARATIONS
// (grouped for ease of translation and localization)

// m is an abbreviation for "missing"

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

var iPassword = "The Password and the Confirm Password field do not match. Please reenter it now."
var iEmail = "This field must be a valid email address (like me@home.com). Please reenter it now."
var iDay = "This field must be a day number between 1 and 31.  Please reenter it now."
var iMonth = "This field must be a month number between 1 and 12.  Please reenter it now."
var iYear = "This field must be a 2 or 4 digit year number.  Please reenter it now."
var iDatePrefix = "The Day, Month, and Year for "
var iDateSuffix = " do not form a valid date.  Please reenter them now."
var iHour = "This field must be a hour number between 0 and 23.  Please reenter it now."
var iMinute = "This field must be a minute number between 0 and 59.  Please reenter it now."
var iSecond = "This field must be a second number between 0 and 59.  Please reenter it now."

// Check whether string s is empty.

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

function toInteger(str) {
	i=0; 
	if(!str.length) 
		return 0;
	while(str.charAt(i) == "0")
		i++;
	str = str.substr(i, str.length);
	return str.length == 0 ? 0: parseInt(str);
}
function toFloat(str) {
	i=0; 
	while(str.charAt(i) == "0")
		i++;
	str = str.substr(i, str.length);
	return str.length == 0 ? 0: parseFloat(str);
}

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


// Returns true if character c is an English letter 
// (A .. Z, a..z).
//
// NOTE: Need i18n version to support European characters.
// This could be tricky due to different character
// sets and orderings for various languages and platforms.

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



// Returns true if character c is a digit 
// (0 .. 9).

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



// Returns true if character c is a letter or digit.

function isLetterOrDigit (c)
{   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++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}

// isAlphabetic (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if string s is English letters 
// (A .. Z, a..z) only.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
//
// NOTE: Need i18n version to support European characters.
// This could be tricky due to different character
// sets and orderings for various languages and platforms.

function isAlphabetic (s)

{   var i;

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

    // Search through string's characters one by one
    // until we find a non-alphabetic character.
    // When we do, return false; if we don't, return true.

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

        if (!isLetter(c))
        return false;
    }

    // All characters are letters.
    return true;
}




// isAlphanumeric (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if string s is English letters 
// (A .. Z, a..z) and numbers only.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
//
// NOTE: Need i18n version to support European characters.
// This could be tricky due to different character
// sets and orderings for various languages and platforms.

function isAlphanumeric (s)

{   var i;

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

    // Search through string's characters one by one
    // until we find a non-alphanumeric 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 or letter.
        var c = s.charAt(i);

        if (! (isLetter(c) || isDigit(c) ) )
        return false;
    }

    // All characters are numbers or letters.
    return true;
}

// 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.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
//
// 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)  || (toInteger (s) > 0) ) );
}






// isNonnegativeInteger (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 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)  || (toInteger (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)  || (toInteger (s) < 0) ) );
}






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

    if (isNonpositiveInteger.arguments.length > 1)
        secondArg = isNonpositiveInteger.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)  || (toInteger (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, then call isFloat.
//
// Does not accept exponential notation.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isFloat (s, sign)

{   var i;
    var seenDecimalPoint = false;

    if (isEmpty(s)) 
       if (isFloat.arguments.length == 2) return defaultEmptyOK;
       else return (isFloat.arguments[2] == 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++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if ((c == decimalPointDelimiter) && !seenDecimalPoint) seenDecimalPoint = true;
        else if (!isDigit(c)) return false;
    }
	if(isNaN(parseFloat(s))) return false;
	if(parseFloat(s)>=0 && sign == 1) return true;
	if(parseFloat(s)<0 && sign == -1) return true;
	if(!sign) return true;
    // All characters are numbers.
}

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 one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) 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.
//
// And yes, this function is not Year 10000 compliant, but 
// because I am giving you 8003 years of advance notice,
// I don't feel very guilty about this ...
//
// For B.C. compliance, write your own function. ;->
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

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



// 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.
// 
// For explanation of optional argument emptyOK,
// see comments of function isInteger.


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 = toInteger(s);
    return ((num >= a) && (num <= b));
}

function isHour (s)
{   if (isEmpty(s)) 
       if (isHour.arguments.length == 1) return defaultEmptyOK;
       else return (isHour.arguments[1] == true);
    return isIntegerInRange (s, 0, 23);
}
function isMinute (s)
{   if (isEmpty(s)) 
       if (isMinute.arguments.length == 1) return defaultEmptyOK;
       else return (isMinute.arguments[1] == true);
    return isIntegerInRange (s, 0, 59);
}
function isSecond (s)
{   if (isEmpty(s)) 
       if (isSecond.arguments.length == 1) return defaultEmptyOK;
       else return (isSecond.arguments[1] == true);
    return isIntegerInRange (s, 0, 59);
}


// isMonth (STRING s [, BOOLEAN emptyOK])
// 
// isMonth returns true if string s is a valid 
// month number between 1 and 12.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

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.
// 
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

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 = toInteger(year);
    var intMonth = toInteger(month);
    var intDay = toInteger(day);

    // catch invalid days, except for February
    if (intDay > daysInMonth[intMonth]) return false; 

    if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;

    return true;
}

/* FUNCTIONS TO NOTIFY USER OF INPUT REQUIREMENTS OR MISTAKES. */


// Display prompt string s in status bar.

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



// Display data entry prompt string s in status bar.

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




// Notify user that required field theField is empty.
// String s describes expected contents of theField.value.
// Put focus in theField and return false.

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



// Notify user that contents of field theField are invalid.
// String s describes expected contents of theField.value.
// Put select theField, pu focus in it, and return false.

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


function warnInvalidSelect (theField, s)
{   alert(s)
    return false
}


/* FUNCTIONS TO INTERACTIVELY CHECK VARIOUS FIELDS. */

// checkString (TEXTFIELD theField, STRING s, [, BOOLEAN emptyOK==false])
//
// Check that string theField.value is not all whitespace.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

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

function checkInteger (theField, s, emptyOK)
{   // Next line is needed on NN3 to avoid "undefined is not a number" error
    // in equality comparison below.
    if (checkInteger.arguments.length == 2) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (!isInteger(theField.value)) 
       return warnInvalid (theField, s);
    else return true;
}

function checkIntegerRange (theField, startVal, endVal, s, emptyOK)
{   // Next line is needed on NN3 to avoid "undefined is not a number" error
    // in equality comparison below.
    if (checkIntegerRange.arguments.length == 2) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (!isInteger(theField.value)) 
       return warnInvalid (theField, s);
    else if(!isIntegerInRange(theField.value, startVal, endVal))
		return warnInvalid(theField, s);
	return true;
}

function checkFloat (theField, s, sign, emptyOK)
{   // Next line is needed on NN3 to avoid "undefined is not a number" error
    // in equality comparison below.
    if (checkFloat.arguments.length == 3) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (!isFloat(theField.value, sign)) 
       return warnInvalid (theField, s);
    else return true;
}

// checkEmail (TEXTFIELD theField [, BOOLEAN emptyOK==false])
//
// Check that string theField.value is a valid Email.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

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




// checkDate (yearField, monthField, dayField, STRING labelString [, OKtoOmitDay==false])
//
// Check that yearField.value, monthField.value, and dayField.value 
// form a valid date.
//
// If they don't, labelString (the name of the date, like "Birth Date")
// is displayed to tell the user which date field is invalid.
//
// If it is OK for the day field to be empty, set optional argument
// OKtoOmitDay to true.  It defaults to false.

function checkDate (yearField, monthField, dayField, labelString, OKtoOmitDay)
{   // Next line is needed on NN3 to avoid "undefined is not a number" error
    // in equality comparison below.
    if (checkDate.arguments.length == 4) OKtoOmitDay = false;
    if (!isYear(yearField.value)) return warnInvalid (yearField, iYear);
    if (!isMonth(monthField.value)) return warnInvalid (monthField, iMonth);
    if ( (OKtoOmitDay == true) && isEmpty(dayField.value) ) return true;
    else if (!isDay(dayField.value)) 
       return warnInvalid (dayField, iDay);
    if (isDate (yearField.value, monthField.value, dayField.value))
       return true;
    alert (iDatePrefix + labelString + iDateSuffix)
    return false
}

function checkTime (hourField, minuteField, secondField, labelString)
{   // Next line is needed on NN3 to avoid "undefined is not a number" error
    // in equality comparison below.
    if (!isHour(hourField.value)) return warnInvalid (hourField, iHour);
    if (!isMinute(minuteField.value)) return warnInvalid (minuteField, iMinute);
    if (!isSecond(secondField.value)) return warnInvalid (secondField, iSecond);
	return true;
}


// password - between 6-8 chars, uppercase, lowercase, and numeral

function checkPassword (theField, s, emptyOK) {
	
    var illegalChars = /[\W_]/; // allow only letters and numbers
    if (checkPassword.arguments.length == 2) emptyOK = defaultEmptyOK;
	if(checkString (theField, s, emptyOK)) {
		if ((theField.value.length < 6)) {
		   s = "The password should be more than 6 characters long.\n";
		  return warnInvalid (theField, s);
		}
		else if (illegalChars.test(theField.value)) {
		  s = "The password contains illegal characters.\n";
		  return warnInvalid (theField, s);
		} 
	}
	else 
		return false;
	return true;    
}    

function validatePassword(theField_p, theField_c, s, emptyOK) {
	
	if (validatePassword.arguments.length == 3) emptyOK = defaultEmptyOK;
	if(checkPassword (theField_p, s, emptyOK) && checkPassword (theField_c, s, emptyOK)) {
		if(theField_p.value !=theField_c.value) {
			theField_p.value ="";
			theField_c.value ="";
			return warnInvalid (theField_p, iPassword);
		}
	}
	else 
		return false;
	return true;    
}    
// Get checked value from radio button.

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

function checkDateVal (year, month, day, theField, labelString, OKtoOmitDay, emptyOK)
{   // Next line is needed on NN3 to avoid "undefined is not a number" error
    // in equality comparison below.
    if (checkDateVal.arguments.length == 4) { OKtoOmitDay = false; emptyOK = defaultEmptyOK; }
	if (checkDateVal.arguments.length == 5) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (!isYear(year)) return warnInvalid (theField, iYear);
    if (!isMonth(month)) return warnInvalid (theField, iMonth);
    if ( (OKtoOmitDay == true) && isEmpty(day) ) return true;
    else if (!isDay(day)) 
       return warnInvalid (theField, iDay);
    if (isDate (year, month, day))
       return true;
    alert (iDatePrefix + labelString + iDateSuffix);
    return false
}

function checkTimeVal (hour, minute, second, theField, labelString, emptyOK)
{   // Next line is needed on NN3 to avoid "undefined is not a number" error
    // in equality comparison below.
	if (checkTimeVal.arguments.length == 5) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (!isHour(hour)) return warnInvalid (theField, iHour);
    if (!isMinute(minute)) return warnInvalid (theField, iMinute);
    if (!isSecond(second)) return warnInvalid (theField, iSecond);
	return true;
}

//check the range of date
function checkDateRange (yearFromField, monthFromField, dayFromField, yearToField, monthToField, dayToField, labelString, OKtoOmitDay, emptyOK)
{   // Next line is needed on NN3 to avoid "undefined is not a number" error
    // in equality comparison below.
    if (checkDateRange.arguments.length == 8) { OKtoOmitDay = false; emptyOK = defaultEmptyOK;}
    if (checkDateRange.arguments.length == 9) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(dayFromField.value))) return true;
	if(!checkDate (yearFromField, monthFromField, dayFromField, labelString, OKtoOmitDay) || 
		!checkDate(yearToField, monthToField, dayToField, labelString, OKtoOmitDay))
		return false;
	fromDt = new Date(yearFromField.value, monthFromField.value, dayFromField.value);
	toDt = new Date(yearToField.value, monthToField.value, dayToField.value);
	if(fromDt.getTime() > toDt.getTime())
		 return warnInvalid (dayFromField, labelString);
    return true;
}

function checkDateTimeVal (fromField, yearFrom, monthFrom, dayFrom, timeFrom, yearTo, monthTo, dayTo, timeTo, labelString, OKtoOmitDay, emptyOK)
{   // Next line is needed on NN3 to avoid "undefined is not a number" error
    // in equality comparison below.
    if (checkDateTimeVal.arguments.length == 10) { OKtoOmitDay = false; emptyOK = defaultEmptyOK;}
    if (checkDateTimeVal.arguments.length == 9) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(fromField.value))) return true;
	if(!checkDateVal (yearFrom, monthFrom, dayFrom, labelString, OKtoOmitDay) || 
		!checkDateVal(yearTo, monthTo, dayTo, labelString, OKtoOmitDay))
		return false;
	var timeFromArray = timeFrom.split(":");
	var timeToArray = timeTo.split(":");
	if(!checkTimeVal (timeFromArray[0], timeFromArray[1], timeFromArray[2], labelString) || 
		!checkTimeVal(timeToArray[0], timeToArray[1], timeToArray[2], labelString))
		return false;
	fromDt = new Date(yearFrom, monthFrom, dayFrom, timeFromArray[0], timeFromArray[1], timeFromArray[2]);
	toDt = new Date(yearTo, monthTo, dayTo, timeToArray[0], timeToArray[1], timeToArray[2]);
	if(fromDt.getTime() > toDt.getTime()) {
	    alert ("The From Date cannot be greater than the End Date. Please Enter them now.");
		fromField.focus();
		fromField.select();
		return false;
	}
    return true;
}
function checkFloatRange(theField, length, msg, sign, emptyOK, lengthDec) {
    if (checkFloatRange.arguments.length == 4) {emptyOK = defaultEmptyOK; lengthDec = 2; }
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (checkFloatRange.arguments.length == 5) lengthDec = 2;
	s="";
	for(i=0; i<length; i++) {
		s = s +"9";
	}
	s = s+".";
	for(i=0; i<lengthDec; i++) {
		s = s +"9";
	}
	numS = parseFloat(s);
	if(checkFloat (theField, msg, sign, emptyOK)) {
		pos = theField.value.indexOf(".");
		if(pos == -1)
			pos=theField.value.length;
		if(parseFloat(theField.value) > numS)
			return warnInvalid (theField, msg);
		if(pos > length )
			return warnInvalid (theField, msg);
		return true;
	}
	return false;
}
function checkIPAddress(thisField, msg, emptyOK) {
	if (checkIPAddress.arguments.length == 2) emptyOK = defaultEmptyOK;
	ip_address_array = thisField.value.split(".");
	if (ip_address_array.length > 4)
		return warnInvalid (thisField, msg);
	for(i=0; i< ip_address_array.length; i++) {
		str = ip_address_array[i];
		if(str.length > 3)
			return warnInvalid (thisField, msg);
		if(toInteger(ip_address_array[i])< 0 || toInteger(ip_address_array[i])>255)
			return warnInvalid (thisField, msg);
	}
	return true;
}
function checkSelect (theField, s, emptyOK)
{   // Next line is needed on NN3 to avoid "undefined is not a number" error
    // in equality comparison below.
    if (checkSelect.arguments.length == 2) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.options[theField.selectedIndex].value))) return true;
    if (isWhitespace(theField.options[theField.selectedIndex].value)) 
       return warnEmpty (theField, s);
    else return true;
}
