// Filename: valid.js
// Project : Javascript Library
//
// Description:
// Validation routines for form processing.
//
// Revision History:
// 12FEB2001 - MJT - Initial development.
// 29JUL2001 - MJT - Added the email address validation function

// VAL_Required
// Verifies that the required fields has been filled out.
// strForm: name of the form to test
// strField: name of the form field to test
// returns: true if the required value is filled out, false if it is empty
function VAL_Required( strForm, strField )
{
	var f = document.forms[ strForm ];
	return ( f[ strField ].value.length > 0 );
}

// VAL_RadioRequired
// Verifies that one of a set of radio buttons is checked
// strForm: name of the for to test
// strField: checkbox to test
// returns: true if checkbox is selected
function VAL_RadioRequired( strForm, strField )
{
	var f = document.forms[ strForm ];
	var fld;
	var i;
	
	if ( fld = f[ strField ] )
	{
		if ( fld.length == undefined || fld.length < 1 )
		{
			if ( fld.checked )
			{
				return true;
			}
		}
		else
		{
			for ( i=0; i < fld.length; i++ )
			{
				if ( fld[i].checked )
				{
					return true;
				}
			}
		}
	}
	return false;
}

// VAL_RequiredList
// Accepts a list of fields to be tested with the list as a CSV
// strForm: name of the for to test
// strFieldList: comma delimited list of fields
// blnNotify: true causes error alert box to be raised.
// returns: true if all elements are ok
function VAL_RequiredList( strForm, strFieldList, blnNotify )
{
	var arrFields = strFieldList.split( ',' );
	var i;
	for ( i=0; i < arrFields.length; i++ )
	{
		if (!VAL_Required( strForm, arrFields[i] ) )
		{
			if (blnNotify)
			{
				alert( 'Please complete the required fields before submitting the form.' );
			}
			return false;
		}
	}
	return true;
}

// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    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++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

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 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function VAL_Date( dtStr, blnNotify)
{
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		if ( blnNotify ) { alert("The date format should be : mm/dd/yyyy"); }
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		if ( blnNotify ) { alert("Please enter a valid month"); }
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		if (blnNotify) { alert("Please enter a valid day"); }
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		if (blnNotify) { alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear) }
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		if (blnNotify) { alert("Please enter a valid date"); }
		return false
	}
return true
}

// VAL_DateOrEmpty
// Validate the date if anything is supplied, but allow for blank entries
function VAL_DateOrEmpty( strValue, blnNotify )
{
	if ( strValue != '' )
	{
		return VAL_Date( strValue, blnNotify )
	}
	return true;
}

// VAL_EMailAddress
// Verifies that the supplied string appears to be a valid e-mail address with an
// optional message box if it doesn't
// An e-mail address needs to look roughly like:  name@domain.ext
// where name is not empty, @ is found and to the right of the @ is a domain and at least one
// extension that is not empty
// strValue: string to test
// blnNotify: if true show message box if fails
// returns: true if the string looks like a valid e-mail address
function VAL_EMailAddress( strValue, blnNotify )
{
	var err = 0;  		// figure out what went wrong
	var intAtPos;		// string position of the at sign
	var intDotPos;		// string position of the domain separator
	var numAtSigns = 0;		// the number of '@' characters in the string
	
	if ( strValue.length == 0 )
	{
		err = 1; // empty string
	}
	else
	{
		// at sign must not be the first character
		intAtPos = strValue.indexOf( '@', 1 );
		if ( intAtPos == -1 )
		{
			err = 2;
		}
		else
		{
			// dot must come after the @ sign
			intDotPos = strValue.indexOf( '.', intAtPos );
			if ( intDotPos == -1 )
			{
				err = 3;
			}
		}
	}
	
	// Verify that the user didn't enter TWO e-mail addresses into our single field
	for ( var i = 0; i < strValue.length; i++ )
	{
		if ( strValue.charAt( i ) == "@" )
		{
			if ( ++numAtSigns > 1 )
			{
				err = 4;
				break;
			}
		}
	}
	
	// Handle errors
	if ( err != 0 )
	{
		if ( blnNotify )
		{
			alert( 'Please enter a single valid e-mail address in \'name@domain.ext\' format.' );
		}
		return false;
	}
	else
	{
		return true;
	}
}