
//This is used to validate the forms w/ business logic
var allowTags = false;

//- 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 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
		}																
	}



//- 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('Please complete the ' + title + ' field.');
			ffocus(field, myForm);
			return false;
		}
		else
		{ return true }
	}
			
//- This function makes sure that an item was selected
	function vfyBlankItem(myForm, field, title)
	{
		if (!fcheckBlankItem(myForm, field)) {
			alert('Please complete the ' + title + ' field.')
			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('Please select an option in the ' + title + ' field.')
			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('Please enter a vaild Email address in the ' + title + ' field.');
			ffocus(field, myForm);
			return false;
		}
   
    // is s whitespace?
    if (!fcheckspaces(val))
    {
			alert('Please enter a vaild Email address in the ' + title + ' field.');
			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('Please enter a vaild Email address in the ' + title + ' field.');
				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('Please enter a vaild Email address in the ' + title + ' field.');
				return false;
		}
    else return true;
    
    
}



