<!--
// Form Checker.  Can check text boxes, select boxes, text areas, and radio buttons.
// Programmer:  Shawn Tomlinson
// Last UpDate:  6/5/2002
// 
// Directions for use:
//   1.  Make sure your form's name is 'MGEN_info'.  This set of functions assumes that.
//   2.  In order to make a field required, input a hidden field after the field you want required.
//   3.  Set the name of the hidden field according to the chart below.
//   4.  Set the hidden field's value to whatever you want it to display when the javascipt window pops up.
//	Ex.  If you have the 'First Name' field required, set the value of the hidden field to 'First Name'.
//		On popup, the window will display 'The following fields are required: First Name' 
//   5.  On your form element, add an onSubmit
//		onSubmit="return checkFields(Type, Replace Link, Check for username/password length)
//			Type (1 for saving, 2 for deleting)
//			Replace Link (optional, if you use Type 2 which is confirm delete, it will replace the page after confirmation with this page)
//			Check for username/password ( optional, 1 for yes)
//				If yes, you must have a field named 'username' and 'password' and 'retypepassword' for this to work,
//				It will check each field to make sure it is at least 4 characters long and it will make sure 
//				the 'password' field and 'retypepassword' field are the same.
//
//   6. Example hidden field -><input type=hidden name="requiredFieldEmailAddress" value="Email Address">
//	Example form element -><form name="MGEN_info" action="whatever.asp" method=post onSubmit="return checkFields(1,'',0)">
//	Example form element -><form name="MGEN_info" action="whatever.asp" method=post onSubmit="return checkFields(1)">
//	
//				
//	NAME				WHAT IT DOES
//--------------------------------------------------------------------------------------------------------------------
//	requiredField			Checks to see if there is something entered/select in the specified form item.
//	requiredFieldNumeric		Field not empty, plus the value must be a number. (Not usefull on select boxes)
//	requiredFieldPhoneNumber	Field not empty, plus the value is limited to these characters -> 123456789-()
//	requiredFieldZipCode		Field not empty, plus the value is limited to these characters -> 123456789-				
//	requiredFieldEmailAddress	Field not empty, plus the value must have an @ symbol and a .
//	requiredFieldMoneyValue		Field not empty, plus the value is limited to these characters -> 123456789.
//	requiredFieldisDate			Field not empty, plus the value is required to be in date format (mm/dd/yyyy)
//
//	optionalFieldNumeric		If field is not empty, the value must be a number. (Not usefull on select boxes)
//	optionalFieldPhoneNumber	If field is not empty, the value is limited to these characters -> 123456789-()
//	optionalFieldZipCode		If field is not empty, the value is limited to these characters -> 123456789-				
//	optionalFieldEmailAddress	If field is not empty, the value must have an @ symbol and a .
//	optionalFieldMoneyValue		If field is not empty, the value is limited to these characters -> 123456789.
// optionalFieldisDate			If field is not empty, the value is required to bein date format (mm/dd/yyyy)
//
// 7.  This validation script now supports on the fly validation, just add the following onblur event to your element field
//    <input type=text name=mytext onBlur="validateField(this)">

//''''''''''''''''''''''''''''''''''''''''
//The isValid function checks to see if a certain string is in the allowed set.

function isValid(string,allowed) {
	for (var i=0; i< string.length; i++) {
	       if (allowed.indexOf(string.charAt(i)) == -1)
	          return false;
	    }
	    return true;
	}
//''''''''''''''''''''''''''''''''''''''''
//The markErrorField function sets the background color of a control to a specified color if it is invalid.

function markErrorField(fld, error)
	{
	/*
	Change the field's background color to pink if there is an error.
	*/

	if (error)
		fld.style.background = "#FFFF66";
	else
		fld.style.background = "#FFFFFF";
	}

//''''''''''''''''''''''''''''''''''''''''
//The isEmpty function will check a string to see if it is empty.

function isEmpty(str)
	{
	/*
	Check whether string is empty.
	*/

	for (var iCnt = 0; iCnt < str.length; iCnt++)
		if (str.charAt(iCnt) != " ") return false;
	return true;
	}
//'''''''''''''''''''''''''''''''''''''''
//The isaNumber function checks to see if a string is a number.

function isaNumber(string) {
    
    if (string.length == 0)
        return false;
    for (var i=0;i < string.length;i++)
        if ((string.substring(i,i+1) < '0') || (string.substring(i,i+1) > '9'))
            return false;
    
    return true;
}

//'''''''''''''''''''''''''''''''''''''''
//The validatePhone function checks to see if a string is a valide phone number.

function validatePhone(string) 
		{
		if (!string) return false;
		var Chars = "0123456789-()";
		for (var i = 0; i < string.length; i++) {
			if (Chars.indexOf(string.charAt(i)) == -1)
				return false;
			}
				return true;
		}
		
//'''''''''''''''''''''''''''''''''''''''
//The validateZip function checks to see if a string is a valide zip code.

function validateZip(string) 
		{
		if (!string) return false;
		var Chars = "0123456789-";
		for (var i = 0; i < string.length; i++) {
			if (Chars.indexOf(string.charAt(i)) == -1)
				return false;
			}
				return true;
		}


//'''''''''''''''''''''''''''''''''''''''
//The validateEmail function checks to see if a string seems to be a valid email address.

function validateEmail(string)
		{
		if (!string) return false;
			if(string.indexOf('@', 0) == -1 || string.indexOf('.', 0) == -1){
				return false;
			}else{
				return true;
			}
		}
		

//'''''''''''''''''''''''''''''''''''''''
//The validate money function checks to see if a string is in money format (without commas).

function validateMoney(string) 
		{
		if (!string) return false;
		var Chars = "0123456789.";
		for (var i = 0; i < string.length; i++) {
			if (Chars.indexOf(string.charAt(i)) == -1)
				return false;
			}
				return true;
		}

//'''''''''''''''''''''''''''''''''''''''
//The isDate function checks to see if the entry is in valid date format.

function isDate(dateStr) {

    var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
    var matchArray = dateStr.match(datePat); // is the format ok?

    if (matchArray == null) {
        //alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");
        return false;
    }

    month = matchArray[1]; // parse date into variables
    day = matchArray[3];
    year = matchArray[5];

    if (month < 1 || month > 12) { // check month range
        //alert("Month must be between 1 and 12.");
        return false;
    }

    if (day < 1 || day > 31) {
        //alert("Day must be between 1 and 31.");
        return false;
    }

    if ((month==4 || month==6 || month==9 || month==11) && day==31) {
        //alert("Month "+month+" doesn't have 31 days!")
        return false;
    }

    if (month == 2) { // check for february 29th
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day==29 && !isleap)) {
            //alert("February " + year + " doesn't have " + day + " days!");
            return false;
        }
    }
    return true; // date is valid
}

//''''''''''''''''''''''''''''''''''''''''
//The checkFields function

function checkFields(sType, nLink, iCheck) {
	var hereCheck = 0;
	if (iCheck==1){
		if (checkLength() == false) {
			hereCheck = 1;
		}else{
			hereCheck = 0;
		}
	}
	if (hereCheck==0){
		if (sType==1){
			
			if (checkRequired() == false) {
				check = 1;
			}else{
				check = 0;
			}
			
		}else{
			check = 1
			if (confirm("Are you sure you want to delete this record?")){
				/* Confirm delete. */
				window.location.replace(nLink);
			}else{
				var eFunct = 1;
			}
		}
		if (check == 0 && eFunct != 1){
			return true;
			var checknLink = nLink + "link";
			if (isValid(".",checknLink) == true){
				window.location.replace(nLink);
			}
		}else if (eFunct != 1){
		
			return false;
		
		}
	}else{		
		return false;
	}	
	
}

//''''''''''''''''''''''''''''''''''''''''''
// Check to see if the current field is valid 

function validateField(objElement)
{
	var iCheckType;
	var iElementNumber;
	var iFieldType = 0;
			
	for (var iElementCnt = 0; iElementCnt < document.MGEN_info.elements.length; iElementCnt++)
	{
		if (document.MGEN_info.elements[iElementCnt] == objElement)
		{
			iElementNumber = iElementCnt + 1;
		}
	}
	
	if (isValid('requiredField',document.MGEN_info.elements[iElementNumber].name) == true){
		iFieldType = 1;
	}else if(isValid('optionalField',document.MGEN_info.elements[iElementNumber].name) == true){
		iFieldType = 2;
	}
							
	if (isValid('Numeric',document.MGEN_info.elements[iElementNumber].name) == true){
		iCheckType = 1;
	}else if (isValid('PhoneNumber',document.MGEN_info.elements[iElementNumber].name) == true){
		iCheckType = 2;
	}else if (isValid('ZipCode',document.MGEN_info.elements[iElementNumber].name) == true){
		iCheckType = 3;
	}else if (isValid('MoneyValue',document.MGEN_info.elements[iElementNumber].name) == true){
		iCheckType = 4;
	}else if (isValid('EmailAddress',document.MGEN_info.elements[iElementNumber].name) == true){
		iCheckType = 5;
	}else if (isValid('isDate',document.MGEN_info.elements[iElementNumber].name) == true){
		iCheckType = 6;
	}
	
	if (document.MGEN_info.elements[iElementNumber-1].type == "text")
	{
		if (isEmpty(document.MGEN_info.elements[iElementNumber-1].value))
		{
			if (iFieldType == 1)
			{
				if (navigator.appName == "Microsoft Internet Explorer")
				{
					markErrorField(document.MGEN_info.elements[iElementNumber-1],true);
				}
			}
			else
			{
				markErrorField(document.MGEN_info.elements[iElementNumber-1],false);
			}
		}
		else if (isaNumber(document.MGEN_info.elements[iElementNumber-1].value) == false && iCheckType == 1 && iFieldType != 0) 
		{
			if (navigator.appName == "Microsoft Internet Explorer")
			{
				markErrorField(document.MGEN_info.elements[iElementNumber-1],true);
			}						
		}
		else if (validateMoney(document.MGEN_info.elements[iElementNumber-1].value) == false && iCheckType == 4 && iFieldType != 0) 
		{
			if (navigator.appName == "Microsoft Internet Explorer")
			{
				markErrorField(document.MGEN_info.elements[iElementNumber-1],true);
			}
		}
		else if (validatePhone(document.MGEN_info.elements[iElementNumber-1].value) == false && iCheckType == 2 && iFieldType != 0) 
		{
			if (navigator.appName == "Microsoft Internet Explorer")
			{
				markErrorField(document.MGEN_info.elements[iElementNumber-1],true);
			}
		}
		else if (validateZip(document.MGEN_info.elements[iElementNumber-1].value) == false && iCheckType == 3 && iFieldType != 0) 
		{
			if (navigator.appName == "Microsoft Internet Explorer")
			{
				markErrorField(document.MGEN_info.elements[iElementNumber-1],true);
			}
		}
		else if (validateEmail(document.MGEN_info.elements[iElementNumber-1].value) == false && iCheckType == 5 && iFieldType != 0) 
		{
			if (navigator.appName == "Microsoft Internet Explorer")
			{
				markErrorField(document.MGEN_info.elements[iElementNumber-1],true);
			}
		}
		else if (isDate(document.MGEN_info.elements[iElementNumber-1].value) == false && iCheckType == 6 && iFieldType != 0) 
		{
			if (navigator.appName == "Microsoft Internet Explorer")
			{
				markErrorField(document.MGEN_info.elements[iElementNumber-1],true);
			}
		}
		else
		{
			if (navigator.appName == "Microsoft Internet Explorer")
			{
				markErrorField(document.MGEN_info.elements[iElementNumber-1],false);
			}
		}
	}
	else if(document.MGEN_info.elements[iElementNumber-1].type == "select-one")
	{
		if (document.MGEN_info.elements[iElementNumber-1].options[0].selected == true)
		{
			if (navigator.appName == "Microsoft Internet Explorer")
			{
				markErrorField(document.MGEN_info.elements[iElementNumber-1],true);
			}
		}
		else	
		{
			if (navigator.appName == "Microsoft Internet Explorer")
			{
				markErrorField(document.MGEN_info.elements[iElementNumber-1],false);
			}
		}
	}
}

//''''''''''''''''''''''''''''''''''''''''''
//The checkRequired function

function checkRequired()
	{
	/*
	Check required attribute for all fields on the form.
	If the field is required and is empty, mark it as an error,
	otherwise clear any existing errors for that field
	(ie. change its background color).
	*/
	var sError = "";
	var nError = "";
	var iFieldType = 0;
	var iNumeric = 0;
	var iMoney = 0;
	var iEmail = 0;
	var iPhone = 0;
	var iZip = 0;
	var iFirstElement = 999;

	for (var iCnt = 0; iCnt < document.MGEN_info.elements.length; iCnt++)
		{
		iNumeric = 0;
		iZip = 0;
		iPhone = 0;
		iMoney = 0;
		iEmail = 0;
		iDate = 0;

		if (document.MGEN_info.elements[iCnt].type == "hidden" && isValid('requiredField',document.MGEN_info.elements[iCnt].name) == true || document.MGEN_info.elements[iCnt].type == "hidden" && isValid('optionalField',document.MGEN_info.elements[iCnt].name) == true)
			{
				if (isValid('requiredField',document.MGEN_info.elements[iCnt].name) == true){
					iFieldType = 1;
				}else if(isValid('optionalField',document.MGEN_info.elements[iCnt].name) == true){
					iFieldType = 2;
				}
				
								
				if (isValid('Numeric',document.MGEN_info.elements[iCnt].name) == true){
					iNumeric = 1;
				}else if (isValid('PhoneNumber',document.MGEN_info.elements[iCnt].name) == true){
					iPhone = 1;
				}else if (isValid('ZipCode',document.MGEN_info.elements[iCnt].name) == true){
					iZip = 1;
				}else if (isValid('MoneyValue',document.MGEN_info.elements[iCnt].name) == true){
					iMoney = 1;
				}else if (isValid('EmailAddress',document.MGEN_info.elements[iCnt].name) == true){
					iEmail = 1;
				}else if (isValid('isDate',document.MGEN_info.elements[iCnt].name) == true){
					iDate = 1;
				}
			iCnt = iCnt - 1;
			
				if (iFieldType == 2 && isEmpty(document.MGEN_info.elements[iCnt].value)){
					iFieldType = 0;
				}
			
			if (document.MGEN_info.elements[iCnt].type == "text" || document.MGEN_info.elements[iCnt].type == "password")
				{
				if (isEmpty(document.MGEN_info.elements[iCnt].value) && iFieldType == 1)
					{
					if (navigator.appName == "Microsoft Internet Explorer"){
							markErrorField(document.MGEN_info.elements[iCnt],true);
						}
						if (iFirstElement == 999){
							iFirstElement = iCnt;
						}
					iCnt = iCnt + 1;
					sError += "  " + document.MGEN_info.elements[iCnt].value + "\n";
					}
				else if (isaNumber(document.MGEN_info.elements[iCnt].value) == false && iNumeric == 1 && iFieldType != 0) 
					{
					if (navigator.appName == "Microsoft Internet Explorer"){
							markErrorField(document.MGEN_info.elements[iCnt],true);
						}
						if (iFirstElement == 999){
							iFirstElement = iCnt;
						}
					iCnt = iCnt + 1;
					nError += "  " + document.MGEN_info.elements[iCnt].value + "\n";
						
					}
				else if (validateMoney(document.MGEN_info.elements[iCnt].value) == false && iMoney == 1 && iFieldType != 0) 
					{
					if (navigator.appName == "Microsoft Internet Explorer"){
							markErrorField(document.MGEN_info.elements[iCnt],true);
						}
						if (iFirstElement == 999){
							iFirstElement = iCnt;
						}
					iCnt = iCnt + 1;
					nError += "  " + document.MGEN_info.elements[iCnt].value + "\n";
						
					}
				else if (validatePhone(document.MGEN_info.elements[iCnt].value) == false && iPhone == 1 && iFieldType != 0) 
					{
					if (navigator.appName == "Microsoft Internet Explorer"){
							markErrorField(document.MGEN_info.elements[iCnt],true);
						}
						if (iFirstElement == 999){
							iFirstElement = iCnt;
						}
					iCnt = iCnt + 1;
					nError += "  " + document.MGEN_info.elements[iCnt].value + "\n";
						
					}
				else if (validateZip(document.MGEN_info.elements[iCnt].value) == false && iZip == 1 && iFieldType != 0) 
					{
					if (navigator.appName == "Microsoft Internet Explorer"){
							markErrorField(document.MGEN_info.elements[iCnt],true);
						}
						if (iFirstElement == 999){
							iFirstElement = iCnt;
						}
					iCnt = iCnt + 1;
					nError += "  " + document.MGEN_info.elements[iCnt].value + "\n";
						
					}
				else if (validateEmail(document.MGEN_info.elements[iCnt].value) == false && iEmail == 1 && iFieldType != 0) 
					{
					if (navigator.appName == "Microsoft Internet Explorer"){
							markErrorField(document.MGEN_info.elements[iCnt],true);
						}
						if (iFirstElement == 999){
							iFirstElement = iCnt;
						}
					iCnt = iCnt + 1;
					nError += "  " + document.MGEN_info.elements[iCnt].value + "\n";
						
					}
				else if (isDate(document.MGEN_info.elements[iCnt].value) == false && iDate == 1 && iFieldType != 0) 
					{
					if (navigator.appName == "Microsoft Internet Explorer"){
							markErrorField(document.MGEN_info.elements[iCnt],true);
						}
						if (iFirstElement == 999){
							iFirstElement = iCnt;
						}
					iCnt = iCnt + 1;
					nError += "  " + document.MGEN_info.elements[iCnt].value + "\n";
						
					}
				else
					{
						if (navigator.appName == "Microsoft Internet Explorer"){
							markErrorField(document.MGEN_info.elements[iCnt],false);
						}
					iCnt = iCnt + 1;
					}
				}
		
			else if(document.MGEN_info.elements[iCnt].type == "select-one")
				{
				if (document.MGEN_info.elements[iCnt].options[0].selected == true)
					{
						if (navigator.appName == "Microsoft Internet Explorer"){
							markErrorField(document.MGEN_info.elements[iCnt],true);
						}
						if (iFirstElement == 999){
							iFirstElement = iCnt;
						}
					iCnt = iCnt + 1;
					sError += "  " + document.MGEN_info.elements[iCnt].value + "\n";
					}
				else	{
						if (navigator.appName == "Microsoft Internet Explorer"){
							markErrorField(document.MGEN_info.elements[iCnt],false);
						}
					iCnt = iCnt + 1;
					}
				}
			else if(document.MGEN_info.elements[iCnt].type == "radio")
				{
				if (document.MGEN_info.elements[iCnt].checked == false)
					{
						var strElementName = document.MGEN_info.elements[iCnt].name;
						var iOldCnt = iCnt;
						var iRadioChecked = 0;
						
						while (document.MGEN_info.elements[iOldCnt].name == strElementName && iOldCnt != 0)
							{
							
							if (document.MGEN_info.elements[iOldCnt].checked == true)
								{
								iRadioChecked = 1;
								}
							iOldCnt = iOldCnt - 1;
							}
						
						if (iRadioChecked == 0){
							if (navigator.appName == "Microsoft Internet Explorer"){
								while (iOldCnt <= iCnt)
									{
									markErrorField(document.MGEN_info.elements[iOldCnt],true);
									iOldCnt = iOldCnt + 1;
									}
							}
							if (iFirstElement == 999){
								iFirstElement = iCnt;
							}
							iCnt = iCnt + 1;
							sError += "  " + document.MGEN_info.elements[iCnt].value + "\n";
						}else{
							if (navigator.appName == "Microsoft Internet Explorer"){
								while (iOldCnt <= iCnt)
									{
									markErrorField(document.MGEN_info.elements[iOldCnt],false);
									iOldCnt = iOldCnt + 1;
									}
							}
						}
						iCnt = iCnt + 1;
					}
				else	{
						var strElementName = document.MGEN_info.elements[iCnt].name;
						var iOldCnt = iCnt;
						
						if (navigator.appName == "Microsoft Internet Explorer"){
								while (document.MGEN_info.elements[iOldCnt].name == strElementName && iOldCnt != 0)
									{
									iOldCnt = iOldCnt - 1;
									}
								while (iOldCnt <= iCnt)
									{
									markErrorField(document.MGEN_info.elements[iOldCnt],false);
									iOldCnt = iOldCnt + 1;
									}
							}
					iCnt = iCnt + 1;
					}
				}
			else if(document.MGEN_info.elements[iCnt].type == "textarea")
				{
				if (isEmpty(document.MGEN_info.elements[iCnt].value))
					{
					if (navigator.appName == "Microsoft Internet Explorer"){
							markErrorField(document.MGEN_info.elements[iCnt],true);
						}
						if (iFirstElement == 999){
							iFirstElement = iCnt;
						}
					iCnt = iCnt + 1;
					sError += "  " + document.MGEN_info.elements[iCnt].value + "\n";
					}
				else
					{
						if (navigator.appName == "Microsoft Internet Explorer"){
							markErrorField(document.MGEN_info.elements[iCnt],false);
						}
					iCnt = iCnt + 1;
					}
				}
		
			else if(document.MGEN_info.elements[iCnt].type == "file")
				{				
				if (isEmpty(document.MGEN_info.elements[iCnt].value))
					{
						if (navigator.appName == "Microsoft Internet Explorer"){
							markErrorField(document.MGEN_info.elements[iCnt],true);
						}
						if (iFirstElement == 999){
							iFirstElement = iCnt;
						}
					iCnt = iCnt + 1;
					sError += "  " + document.MGEN_info.elements[iCnt].value + "\n";
					}
				else if (isValid('.bmp',document.MGEN_info.elements[iCnt].value.toLowerCase()) == false && isValid('.jpg',document.MGEN_info.elements[iCnt].value.toLowerCase()) == false && isValid('.tif',document.MGEN_info.elements[iCnt].value.toLowerCase()) == false)
					{
						if (navigator.appName == "Microsoft Internet Explorer"){
							markErrorField(document.MGEN_info.elements[iCnt],true);
						}
						if (iFirstElement == 999){
							iFirstElement = iCnt;
						}
					iCnt = iCnt + 1;
					sError += "  " + document.MGEN_info.elements[iCnt].value + "\n";
					}
				else	{
						if (navigator.appName == "Microsoft Internet Explorer"){
							markErrorField(document.MGEN_info.elements[iCnt],false);
						}
					iCnt = iCnt + 1;
					}
				}
			
			}
			
		}

		// Display a dialog box if there are errors.
		if (sError != "" || nError != "")
			{
				if (nError != "" && sError != ""){
					alert("The following fields are required:\n" + sError + "\nThe following fields are not in valid format:\n" + nError);
					document.MGEN_info.elements[iFirstElement].focus();
				}else if (nError != "" && sError == ""){
					alert("The following fields are not in valid format:\n" + nError);
					document.MGEN_info.elements[iFirstElement].focus();
				}else{
					alert("The following fields are required:\n" + sError);
					document.MGEN_info.elements[iFirstElement].focus();
				}
			return false;
			}
		else
			return true;
	}
	
//''''''''''''''''''''''''''''''''''''''''''
//The checkLength function	

function checkLength() {
    var nCheck = "";
    var sCheck = "";
    var iFirstElement = "";
    var iTaken = "";
    
    if (document.MGEN_info.username.value.length < 4){
    	nCheck += "  " + "username is not 4 characters" + "\n";
    	sCheck = 1;
    	iFirstElement = 0;
    	iTaken = 1;
    }
    if (document.MGEN_info.password.value.length < 4){
    	nCheck += "  " + "password is not 4 characters" + "\n";
    	sCheck = 1;
    	if (iTaken==""){
    		iFirstElement = 2;
    		iTaken = 1;
    	}
    	
    }
    if (document.MGEN_info.retypepassword.value.length < 4){
    	nCheck += "  " + "Re-Type Password is not 4 characters" + "\n";
    	sCheck = 1;
    	if (iTaken==""){
    		iFirstElement = 4;
    		iTaken = 1;
    	}
    }
    if (document.MGEN_info.retypepassword.value != document.MGEN_info.password.value){
    	nCheck += "  " + "Password and Re-Type Password are not the same" + "\n";
    	sCheck = 1;
    	if (iTaken==""){
    		iFirstElement = 2;
    		iTaken = 1;
    	}
    }
   
   if (sCheck != ""){
	alert("Please correct the following:\n" + nCheck);
	document.MGEN_info.elements[iFirstElement].focus();
	return false;
   }else{
   	return true;
   }
}
//-->
