
<!-- ============== Form Validation ====================== -->
//This is used to validate the forms w/ business logic
var allowTags = false;

var labelBlankItem = '';
var labelField = '';
var labelCheckbox = '';
var labelEmail = '';
var labelNumberItem = '';
var lableMatching = '';

//- This function gives the field focus
	function ffocus(field, myForm) { myForm.elements[field].focus() };

//- This function checks a value to ensure it is not an HTLM tag
	function isTag (t)
	{	return ((t != ">") && (t != "<"))   }
 
 	function fchecktag(val)
	{
		if (allowTags)
		{return true;}

		var j
	    for (j = 0; j < val.length; j++)
	   {   
	       var t = val.charAt(j);
	       if (!isTag(t)) return false;
	   }
	   // All characters are valid.
	   return true;
	} 


//- This function checks for teh double quote mark in a string
	function isQuote (t)
	{	return (t != "\"")   }

		function fCheckQuote(val)
	{
		var j
	    for (j = 0; j < val.length; j++)
	   {   
	       var t = val.charAt(j);
	       if (!isQuote(t)) return false;
	   }
	   // All characters are valid.
	   return true;
	}


//- This function makes sure that all characters ina string are numeric
	function isDigit (c)
	{   return ((c >= "0") && (c <= "9")) }
			
	function fchecknum(val)
	 {
		var i
	     for (i = 0; i < val.length; i++)
	    {   
	        // Check that current character is number.
	        var c = val.charAt(i);
	        if (!isDigit(c)) return false;
	    }
	    // All characters are numbers.
	    return true;
	 }			



//- This function checks to make sure string is not made up entirely of spaces
	function fcheckspaces(strValue)
	{
		var cntChar
	    for (cntChar = 0; cntChar < strValue.length; cntChar++)
	   {   
	       // Check to see if current character is a space
	       var vChar = strValue.charAt(cntChar);
	       if (vChar != ' ') return true;
	   }
	   return false;
	}

//- This function checks to make sure that there was data entered into a field
	function fcheckBlank(myForm, field) {
		var val = myForm.elements[field].value;
			if (!fchecktag(val))
		{ return false }
			
		if (val.length == 0)
		{ return false }
					
		if (!fcheckspaces(val))
		{ return false }
				
		else
		{ return true }
	}
			
	function fcheckBlankItem(myForm, field) {
		var indexvalue = myForm.elements[field].selectedIndex

		if (myForm.elements[field].size == 0 || myForm.elements[field][indexvalue].value==''
			|| myForm.elements[field][indexvalue].value == 0) {
			//false
//			return (myForm.elements[field].selectedIndex > 0)
			return (false)
		}
		else {
			//true
			return (true)
//			return (myForm.elements[field].selectedIndex > -1)
		}
	}			

	// checks to make sure that a checkbox in a list is selected
	function fcheckCheckbox(myForm, field) {
		var cntCheck
		var fieldLength = myForm.elements[field].length
		
		if (!fieldLength) {fieldLength = 1}
				
		for(cntCheck = 0; cntCheck < fieldLength; cntCheck++) {
			if (fieldLength == 1 )	{
				if (myForm.elements[field].checked) {return true}
			}
			else {
				if (myForm.elements[field][cntCheck].checked) {return true};
			}
		}
		
		return (false);
	}			
			


//- This function accepts a string variable and verifies if it is a
	// proper date or not. It validates format matching either
	// mm-dd-yyyy or mm/dd/yyyy. Then it checks to make sure the month
	// has the proper number of days, based on which month it is.

    // The function returns true if a valid date, false if not.
	// ******************************************************************

	function isDate(dateStr) {
		
	    var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	    var matchArray = dateStr.match(datePat); // is the format ok?

		if (dateStr == null || dateStr == '') 
			{ return 0 } // do not check if blank
		 				
		if (matchArray == null) 
			{ return 1 }
				
	    month = matchArray[1]; // parse date into variables
	    day = matchArray[3];
	    year = matchArray[5];

	    if (month < 1 || month > 12) 
			{ return 2 }

	    if (day < 1 || day > 31) 
			{ return 3 }

	    if ((month==4 || month==6 || month==9 || month==11) && day==31) 
			{ return 4 }

	    if (month == 2) { // check for february 29th
	        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
	        if (day > 29 || (day==29 && !isleap)) 
	        { return 5 }
	    }
			    
			    
	    return 0; // date is valid
	}
	
	function isDate_ddmmyyyy(dateStr) {
		
	    var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	    var matchArray = dateStr.match(datePat); // is the format ok?

		if (dateStr == null || dateStr == '') 
			{ return 0 } // do not check if blank
		 				
		if (matchArray == null) 
			{ return 1 }
				
	    day = matchArray[1]; // parse date into variables
	    month = matchArray[3];
	    year = matchArray[5];

	    if (month < 1 || month > 12) 
			{ return 2 }

	    if (day < 1 || day > 31) 
			{ return 3 }

	    if ((month==4 || month==6 || month==9 || month==11) && day==31) 
			{ return 4 }

	    if (month == 2) { // check for february 29th
	        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
	        if (day > 29 || (day==29 && !isleap)) 
	        { return 5 }
	    }
			    
			    
	    return 0; // date is valid
	}
			

	function vfyDate(myForm, field, title) {
	
		var dateStr = myForm.elements[field].value
		
		var dateType = isDate(dateStr)

		if (dateType == 0)
			{ return true } //date is valid
		else if (dateType == 1) { 
			alert(title + " is not a valid date.")
			myForm.elements[field].focus()
			return false
		}
		else if (dateType == 2) { 
			alert(title + ": Month must be between 1 and 12.")
			myForm.elements[field].focus()
			return false
		}
		else if (dateType == 3) { 
			alert(title + ': Day must be between 1 and 31.')
			myForm.elements[field].focus()
			return false
		}
		else if (dateType == 4) { 
			alert(title + ": Month "+month+" doesn't have 31 days.")
			myForm.elements[field].focus()
			return false
		}
		else if (dateType == 5) { 
			alert(title + ": February " + year + " doesn't have " + day + " days.")
			myForm.elements[field].focus()
			return false
		}																
	}
	
	function vfyDate_ddmmyyyy(myForm, field, title) {
	
		var dateStr = myForm.elements[field].value
		
		var dateType = isDate_ddmmyyyy(dateStr)

		if (dateType == 0)
			{ return true } //date is valid
		else if (dateType == 1) { 
			alert(title + " is not a valid date.")
			myForm.elements[field].focus()
			return false
		}
		else if (dateType == 2) { 
			alert(title + ": Month must be between 1 and 12.")
			myForm.elements[field].focus()
			return false
		}
		else if (dateType == 3) { 
			alert(title + ': Day must be between 1 and 31.')
			myForm.elements[field].focus()
			return false
		}
		else if (dateType == 4) { 
			alert(title + ": Month "+month+" doesn't have 31 days.")
			myForm.elements[field].focus()
			return false
		}
		else if (dateType == 5) { 
			alert(title + ": February " + year + " doesn't have " + day + " days.")
			myForm.elements[field].focus()
			return false
		}																
	}



//- This function checks to make sure that there was data entered into a field
	function vfyBlank(myForm, field, title)
	{
		var val = myForm.elements[field].value;

		if (!fcheckBlank(myForm, field))
		{
			alert(labelBlankItem + title + labelField);
			ffocus(field, myForm);
			return false;
		}
		else
		{ return true }
	}
	
//-This function makes sure the credit card is not expired
	function vfyExpired(myForm, field, field2 ) 
	{
        	var now = new Date();							
        	var newMonth = (myForm.elements[field].value - 1);
			var newYear = myForm.elements[field2].value;
			var expiresIn = new Date(newYear,newMonth,0,0,0);		// create an expired on date object with valid thru expiration date
        	expiresIn.setMonth(expiresIn.getMonth() + 1);		// adjust the month, to first day, hour, minute & second of expired month
        	if( now.getTime() >= expiresIn.getTime() ){ 
				alert("Credit Card is Expired");
				ffocus(month,myForm);
				return false;
			}
        	return true;									// then we get the miliseconds, and do a long integer comparison
    }
	
	
//- This function checks that a date is in the future;currently takes 4 digit years only
	function vfyFutureDate(myForm, day, month, year, title)
	{
			var now = new Date();
			var newMonth = (myForm.elements[month].value - 1);
			var newYear = (myForm.elements[year].value);
			var newDay = myForm.elements[day].value;
			var userDate = new Date(newYear,newMonth,newDay);
			if( now.getTime() <= userDate.getTime()){
				return true;
			}
			alert("The "+title+" Entered has Passed");
			ffocus(day,myForm);
			return false;
	}
	
//- This function takes in two dates and compares them;currently takes 4 digit years only
	function fCompareDates(myForm, day1,month1,year1, day2, month2, year2,title,title2)
	{
			var inMonth = (myForm.elements[month1].value - 1);
			var inYear = (myForm.elements[year1].value);
			var inDay = myForm.elements[day1].value;
			var inDate = new Date(inYear,inMonth,inDay);
			var outMonth = (myForm.elements[month2].value - 1);
			var outYear = (myForm.elements[year2].value);
			var outDay = myForm.elements[day2].value;
			var outDate = new Date(outYear,outMonth,outDay);
			if(outDate.getTime() <= inDate.getTime()){
				alert("The "+ title2+" is not after the "+title);
				return false;
			}
			return true;
	}
			
//- This function makes sure that an item was selected
	function vfyBlankItem(myForm, field, title)
	{
		if (!fcheckBlankItem(myForm, field)) {
			alert(labelBlankItem + title + labelField);
			return false
		}
		else
		{ return true }; 
	}

//- This function makes sure that an checkbox in a list was selected
	function vfyBlankCheckbox(myForm, field, title)
	{
		if (!fcheckCheckbox(myForm, field)) {
			alert(labelCheckbox + title + labelField);
			return false
		}
		else
		{ return true }; 
	}

//- This function makes sure that the value is a number
	function vfyNumberItem(myForm, field, title)
	{
		var val = myForm.elements[field].value;

		if (!fchecknum(val)) {
			alert(labelNumberItem + title + labelField);
			return false
		}
		else
		{ return true }; 
	}


//- This function checks to make sure that two fields match
	function vfyMatch(myForm, field1, field2, title1, title2)
	{
		var val1 = myForm.elements[field1].value;
		var val2 = myForm.elements[field2].value;

		if (val1 != val2)
		{
			alert(labelMatching + ' ' + title1 + ' - ' + title2);
			ffocus(field1, myForm);
			return false;
		}
		else
		{ return true }
	}


//- This function prompts the user before a delete is made
	function fDelete(id)	{
		if (confirm('These records will be deleted. Continue?')) {
			return (true)
			
			//var Location = document.location.href
			//var Del = "&Delete=True"
			//var SecondaryId = "&SecondaryID=" + id;
			
			// RegExp to get rid of parameters already in location, 
			// otherwise they are added twice
			//DeleteExp = /&Delete=True/gi;
			//IdExp = /&SecondaryID=[0-9a-z]*/gi;
			//Location = Location.replace(DeleteExp, "")
			//Location = Location.replace(IdExp, "")
			
			// redirect location
			//document.location.href = Location + Del + SecondaryId;
		}
		return (false)
	}


//- This fucntion is used to submit a form
	function fSubmitForm(formname)	{
		document.forms[formname].submit()
	}
	
	
	
//- This function checks to make sure that there was data entered is a valid Email address
//- added by vivian
function vfyEmail (myForm, field, title)
{   
		var val = myForm.elements[field].value;
		
		if (!fcheckBlank(myForm, field))
		{
			alert(labelEmail + title + labelField);
			ffocus(field, myForm);
			return false;
		}
   
    // is s whitespace?
    if (!fcheckspaces(val))
    {
			alert(labelEmail + title + labelField);
			ffocus(field, myForm);
			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 = val.length;

    // look for @
    while ((i < sLength) && (val.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (val.charAt(i) != "@")) 
    {
				alert(labelEmail + title + labelField);
				return false;
		}
    else i += 2;

    // look for .
    while ((i < sLength) && (val.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (val.charAt(i) != ".")) 
    {
				alert(labelEmail + title + labelField);
				return false;
		}
    else return true;
    
    
}


