﻿// 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 (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;
}



// 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)
{
	var filter  = /^(([\w_\&\'\-\+]+)\.([\w_\&\'\-\+]*))*([\w_\&\'\-\+]+)\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,})$/;

	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));
}


// 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 "";
}

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;
	}
}
