﻿// FormChek.js
// 18 Feb 97 created Eric Krock
// (c) 1997 Netscape Communications Corporation

var whitespace = "\r\n 	";

var mPrefix = "You did not enter a value into the "
var mSuffix = " field. This is a required field. Please enter it now."

// s is an abbreviation for "string"

var sEmail = "Email"

// i is an abbreviation for "invalid"

var iEmail = "This field must be a valid email address (like me@here.com). Please re-enter it now."


// p is an abbreviation for "prompt"

var pEmail = "valid email address (like foo@bar.com)."

var defaultEmptyOK = false

// 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 checkZero(theField, s, emptyOK)
{
    if (checkZero.arguments.length == 2) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (theField.value == 0)) return true;
    if (theField.value == 0)
       return warnEmpty (theField, s);
    else return true;
}

// 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
}

// isEmail (STRING s [, BOOLEAN emptyOK])
//
// Email address must be of form a@b.c -- in other words:
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isEmail (emailStr)
{
    if (isEmpty(emailStr))
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);

    emailStr = emailStr.replace(/^ +/,"");	//trim spaces from beginning
    emailStr = emailStr.replace(/ +$/,"");	//trim spaces from end
    
/* 
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- V1.1.3: Sandeep V. Tamhankar (stamhankar@hotmail.com) -->
<!-- Original:  Sandeep V. Tamhankar (stamhankar@hotmail.com) -->
<!-- Changes:
1.1.4: Fixed a bug where upper ASCII characters (i.e. accented letters
international characters) were allowed.

1.1.3: Added the restriction to only accept addresses ending in two
letters (interpreted to be a country code) or one of the known
TLDs (com, net, org, edu, int, mil, gov, arpa), including the
new ones (biz, aero, name, coop, info, pro, museum).  One can
easily update the list (if ICANN adds even more TLDs in the
future) by updating the knownDomsPat variable near the
top of the function.  Also, I added a variable at the top
of the function that determines whether or not TLDs should be
checked at all.  This is good if you are using this function
internally (i.e. intranet site) where hostnames don't have to 
conform to W3C standards and thus internal organization e-mail
addresses don't have to either.
Changed some of the logic so that the function will work properly
with Netscape 6.

1.1.2: Fixed a bug where trailing . in e-mail address was passing
(the bug is actually in the weak regexp engine of the browser; I
simplified the regexps to make it work).

1.1.1: Removed restriction that countries must be preceded by a domain,
so abc@host.uk is now legal.  However, there's still the 
restriction that an address must end in a two or three letter
word.

1.1: Rewrote most of the function to conform more closely to RFC 822.

1.0: Original  */
// -->

/* The following variable tells the rest of the function whether or not
to verify that the address ends in a two-letter country or well-known
TLD.  1 means check it, 0 means don't. */

	var checkTLD=0;

/* The following is the list of known TLDs that an e-mail address must end with. */

	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

/* The following pattern is used to check if the entered e-mail address
fits the user@domain format.  It also is used to separate the username
from the domain. */

	var emailPat=/^(.+)@(.+)$/;

/* The following string represents the pattern for matching all special
characters.  We don't want to allow special characters in the address. 
These characters include ( ) < > @ , ; : \ " . [ ] */

	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

/* The following string represents the range of characters allowed in a 
username or domainname.  It really states which chars aren't allowed.*/

	var validChars="\[^\\s" + specialChars + "\]";

/* The following pattern applies if the "user" is a quoted string (in
which case, there are no rules about which characters are allowed
and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
is a legal e-mail address. */

	var quotedUser="(\"[^\"]*\")";

/* The following pattern applies for domains that are IP addresses,
rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
e-mail address. NOTE: The square brackets are required. */

	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

/* The following string represents an atom (basically a series of non-special characters.) */

	var atom=validChars + '+';

/* The following string represents one word in the typical username.
For example, in john.doe@somewhere.com, john and doe are words.
Basically, a word is either an atom or quoted string. */

	var word="(" + atom + "|" + quotedUser + ")";

// The following pattern describes the structure of the user

	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

/* The following pattern describes the structure of a normal symbolic
domain, as opposed to ipDomainPat, shown above. */

	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

/* Finally, let's start trying to figure out if the supplied address is valid. */

/* Begin with the coarse pattern to simply break up user@domain into
different pieces that are easy to analyze. */

	var matchArray=emailStr.match(emailPat);

	if (matchArray==null) {

/* Too many/few @'s or something; basically, this address doesn't
even fit the general mould of a valid e-mail address. */

//alert("Email address seems incorrect (check @ and .'s)");
			return false;
	}
	
	var user=matchArray[1];
	var domain=matchArray[2];

// Start by checking that only basic ASCII characters are in the strings (0-127).

	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
//alert("Ths username contains invalid characters.");
			return false;
		}
	}
	
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
//alert("Ths domain name contains invalid characters.");
			return false;
		}
	}

	
	// See if "user" is valid 

	if (user.match(userPat)==null) {

			// user is not valid

			//alert("The username doesn't seem to be valid.");
		return false;
	}

/* if the e-mail address is at an IP address (as opposed to a symbolic
host name) make sure the IP address is valid. */

	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
		// this is an IP address
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
			//alert("Destination IP address is invalid!");
				return false;
			}
		}
		return true;
	}

	// Domain is symbolic name.  Check if it's valid.
 
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	
	for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat)==-1) {
			//alert("The domain name does not seem to be valid.");
			return false;
		}
	}

	/* domain name seems valid, but now make sure that it ends in a
	known top-level domain (like com, edu, gov) or a two-letter word,
	representing country (uk, nl), and that there's a hostname preceding 
	the domain or country. */

	if (checkTLD && domArr[domArr.length-1].length!=2 && 
			domArr[domArr.length-1].search(knownDomsPat)==-1) {
			//alert("The address must end in a well-known domain or two letter " + "country.");
			return false;
	}

	// Make sure there's a host name preceding the domain.

	if (len<2) {
	//alert("This address is missing a hostname!");
		return false;
	}

	// If we've gotten this far, everything's valid!
	return true;
	//  End -->
}




// 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;
}

// 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 (!isValidEmail(theField.value))
       return warnInvalid (theField, iEmail);
    else return true;
}

// 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
}


// isValidEmail (STRING s [, BOOLEAN emptyOK])
//
// Evaluates an email address according to the regular
// expression below. Generally email addresses must be in
// the form name@domain.com.  This differs from the isEmail()
// function in that it allows for a more general format in the
// name and domain fields as well as checking for illegal chars
// like spaces and commas.
//
// Note: The regular expression used here should be compatible
//       with the expression used in the IsValidEmail() function
//		 in the MNSSecurity DLL as well as the ValidEmailAddress()
//		 function in the functions folder.
//
// Modified
// 11/07/02 TJackson  Added the "_" char explicitly because IE5.0
// does not include it in the \w list.
//
// 04/25/03 TJackson Added the "+" char as legal.
//
function isValidEmail (s)
{
	return isEmail(s);
}


// isValidSSN (STRING s)
//
//
function isValidSSN (s)
{
	var filter  = /^\d{3}[ -]{0,1}\d{2}[ -]{0,1}\d{4}$/;

	s = s.replace(/^ +/,"");	//trim spaces from beginning
	s = s.replace(/ +$/,"");	//trim spaces from end
	
	return (filter.test(s));
}


// isValidSSN (STRING s)
//
//
function isValidSSN (s)
{
	var filter  = /^\d{3}[ -]{0,1}\d{2}[ -]{0,1}\d{4}$/;

	s = s.replace(/^ +/,"");	//trim spaces from beginning
	s = s.replace(/ +$/,"");	//trim spaces from end
	
	return (filter.test(s));
}


// ***********************************************************************
// Date functions
//
// ***********************************************************************
var daysInMonth = Array();
daysInMonth[1] = 31;
daysInMonth[2] = 29;   // must programmatically check this
daysInMonth[3] = 31;
daysInMonth[4] = 30;
daysInMonth[5] = 31;
daysInMonth[6] = 30;
daysInMonth[7] = 31;
daysInMonth[8] = 31;
daysInMonth[9] = 30;
daysInMonth[10] = 31;
daysInMonth[11] = 30;
daysInMonth[12] = 31;


// 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);

	var filter  = /^\d+$/;

	return (filter.test(s));
}


// 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];

	if (isEmpty(s)) return (secondArg);
	
	var filter  = /^[\+]{0,1}\d+$/;

	return (filter.test(s));
}




// 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 (!isPositiveInteger(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 = parseInt (s);
    return ((num >= a) && (num <= b));
}



// 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 = parseInt(year);
    var intMonth = parseInt(month);
    var intDay = parseInt(day);

    // catch invalid days, except for February
    if (intDay > daysInMonth[intMonth]) return false;

    if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;

    return true;
}

// isUSDate (STRING date)
//
// isUSDate returns true if string in format mm/dd/yyyy
// or mm-dd-yyyy.
//
function isUSDate(strdate ) {
	var dateFormat = /^\s*(\d{1,2})[\/-](\d{1,2})[\/-](\d{4})/;
	var result;
	var month;
	var day;
	var year;

	result = strdate.match( dateFormat );

	if ( result == null ) {
		return false;
	}
	else {
		month = parseInt(result[1],10) + "";
		day = parseInt(result[2],10) + "";
		year = result[3];

		return isDate(year, month, day);
	}
}


// isMonthYear (STRING date)
//
// isMonthYear returns true if string in format mm/yyyy
// or mm-yyyy.
//
function isMonthYear(strdate ) {
	var dateFormat = /^\s*(\d{1,2})[\/-](\d{4})/;
	var result;
	var month;
	var day;
	var year;

	result = strdate.match( dateFormat );

	if ( result == null ) {
		return false;
	}
	else {
		month = parseInt(result[1],10) + "";
		day = "1";    //assume it is the 1st day of the month
		year = result[2];

		return isDate(year, month, day);
	}
}


// Get checked value from radio button.
function getRadioButtonValue (radio)
{   for (var i = 0; i < radio.length; i++)
    {   
    	if (radio[i].checked) { 
    		return radio[i].value; 
    	}
    }

    return "";
}

// isSelected (TEXTFIELD theField, STRING s, [, BOOLEAN emptyOK==false])
function isSelected (theField, s, emptyOK)
{   if (theField.selectedIndex != 0)
	{
	    return true;
	}
	else 
	{
	   alert(mPrefix + s + mSuffix);
	   return false;
	}
}


function selectDate( ID )
{
	var setDate = document.getElementById( ID );
	var days = document.getElementById( "Days_"+ID );
	var months = document.getElementById( "Months_"+ID );
	var years = document.getElementById( "Years_"+ID );

	var needRefill = false;
	if( setDate.value.toLowerCase() == "today" )
	{
		var currentDate = new Date();
		setDate.value = currentDate.getFullYear() + "-" + ( currentDate.getMonth() + 1 ) + "-" + currentDate.getDate();
	}
	else
	{
		needRefill = true;
	}

	if (setDate.value != "")
	{
		var re = /-/g;
		var newDate = new Date( setDate.value.replace( re, "/" ) );
		years.selectedIndex = newDate.getFullYear() - years[ 1 ].value + 1;
		months.selectedIndex = newDate.getMonth() + 1;
		days.selectedIndex = newDate.getDate();
		if( needRefill )
		{
			dayFiller( ID );
		}
	}
}

function yearFiller( startingYear,ID )
{
	var setDate = document.getElementById( ID );
	var days = document.getElementById( "Days_"+ID );
	var months = document.getElementById( "Months_"+ID );
	var years = document.getElementById( "Years_"+ID );

	var date = new Date();
	endYear = date.getFullYear() + 10;
	
	years.length = 0;
		
	var option = document.createElement("OPTION");
	years.add( option );
	option.text = "Select";
	option.value = "";	
	
	for( i = startingYear; i <= endYear; i++ )
	{
		var year = document.createElement("OPTION");
		years.add( year );
		year.text = i;
		year.value = i;
	}
}

function monthFiller( ID )
{	
	var setDate = document.getElementById( ID );
	var days = document.getElementById( "Days_"+ID );
	var months = document.getElementById( "Months_"+ID );
	var years = document.getElementById( "Years_"+ID );


	months.length = 0;
	
	var month = document.createElement("OPTION");
	month.text = "Select";
	month.value = "";
	months.add( month );
	
	month = document.createElement("OPTION");
	month.text = "Jan   ";
	month.value = "01";
	months.add( month );
	
	month = document.createElement("OPTION");
	month.text = "Feb   ";
	month.value = "02";
	months.add( month );
	
	month = document.createElement("OPTION");
	month.text = "Mar   ";
	month.value = "03";
	months.add( month );
	
	month = document.createElement("OPTION");
	month.text = "Apr   ";
	month.value = "04";
	months.add( month );
	
	month = document.createElement("OPTION");
	month.text = "May   ";
	month.value = "05";
	months.add( month );
	
	month = document.createElement("OPTION");
	month.text = "Jun   ";
	month.value = "06";
	months.add( month );
	
	month = document.createElement("OPTION");
	month.text = "Jul   ";
	month.value = "07";
	months.add( month );
	
	month = document.createElement("OPTION");
	month.text = "Aug   ";
	month.value = "08";
	months.add( month );
	
	month = document.createElement("OPTION");
	month.text = "Sep   ";
	month.value = "09";
	months.add( month );
	
	month = document.createElement("OPTION");
	month.text = "Oct   ";
	month.value = "10";
	months.add( month );
	
	month = document.createElement("OPTION");
	month.text = "Nov";
	month.value = "11";
	months.add( month );
	
	month = document.createElement("OPTION");
	month.text = "Dec";
	month.value = "12";
	months.add( month );
}

	function dayFiller( ID )
{
	var setDate = document.getElementById( ID );
	var days = document.getElementById( "Days_"+ID );
	var months = document.getElementById( "Months_"+ID );
	var years = document.getElementById( "Years_"+ID );

	selectedYear = years[ years.selectedIndex ];
	selectedMonth = months[ months.selectedIndex ];
	
	selectedDayValue = 0;
	if( days.selectedIndex != -1 )
	{
		selectedDayValue = days[ days.selectedIndex ].value;
	}
	
	days.length = 0;
	
	var option = document.createElement("OPTION");
	option.text="Select";
	option.value="";
	days.add(option);
	
	var maxDays = getMaxDays( selectedYear.value, selectedMonth.value );
	
	for( i=1; i <=maxDays; i++ )
	{
		option = document.createElement("OPTION")
		option.value = i;
		option.text = Math.floor( ( i / 10 ) ).toString();
		option.text = option.text + ( i % 10 ).toString();

		days.add(option);
	}
	
	if( selectedDayValue != "" )
	{
		if( selectedDayValue > maxDays )
		{
			selectedDayValue = maxDays;
		}
		days.selectedIndex = selectedDayValue;
	}
}

function getMaxDays( year, month )
{
	if( month == "" ) return 31
	if( month == "02" )
	{
		if( ( year % 4 ) == 0 )
		{
			return 29;
		}
		else
		{
			return 28;
		}
	}
	else
	{
		if( ( month == "01" ) || ( month == "03" ) 
		|| ( month == "05" ) || ( month == "07" ) 
		|| ( month == "08" ) || ( month == "10" )
		|| ( month == "12" ) )
		{
			return 31;
		}
		else
		{
			return 30;
		}
	}
}

function dateChanged( ID, DropDown )
{
	var setDate = document.getElementById( ID );
	var days = document.getElementById( "Days_"+ID );
	var months = document.getElementById( "Months_"+ID );
	var years = document.getElementById( "Years_"+ID );

	selectedYear = years[ years.selectedIndex ];
	selectedMonth = months[ months.selectedIndex ];
	selectedDay = days[ days.selectedIndex ];

	if( selectedYear.value != "" && selectedMonth.value != "" && selectedDay.value != "" )
	{
		setDate.value = selectedYear.value + "-" + selectedMonth.value + "-" + selectedDay.value;
	}
	
	if( (selectedYear.value == "" && DropDown == "Year") ||
		(selectedDay.value == "" && DropDown == "Day") ||
		(selectedMonth.value == "" && DropDown == "Month") )
	{
		setDate.value = "";
		days.selectedIndex = 0;
		months.selectedIndex = 0;
		years.selectedIndex = 0;
	}
}


