//----------------------------------------
// NAME OF SCRIPT:  FORM VALIDATOR v1.2   |
// AUTHOR        :  Carolyn Brown Ray     |
// CREATED       :  2003                  |
// LAST MODIFIED :  2004                  |
// FILENAME      :  validateFields.js     |
//--------------------------------------------------------------------
// DESCRIPTION: Validate form fields before submit

var invalidChar="\\~`!@$%^*+=\";\/:?><";
var invalidEmailChar="\\,~`!$%^&*()+=\";:\/?><";
var validEmailChar=".@";

/*
  -------------------------------------------------------------
    Function:       textLength
    Purpose:        object used to set input length standards
    Parameters:     none
    Return Value:   none

  -------------------------------------------------------------
*/

function textLength () {

	this.minInput=1;
	this.stdInput=30;
	this.extInput=40;
	this.maxInput=200;
	this.superInput=500;
	this.zipInput=5;
	this.phoneInput=12;
	this.ssnInput=11;
	this.ssnInputNoDash=9;
	this.yearInput=4;

}

/*
  -------------------------------------------------------------
    Function:       textSizeError
    Purpose:        text input error message
    Parameters:     [form value], [ field length ]
    Return Value:   none
  -------------------------------------------------------------
*/

function textSizeError(cName, fieldLength) {

	alert("\"" + cName + "\" exceeds " + fieldLength + "-character limit");

}

/*
  -------------------------------------------------------------
    Function:       inputRequiredError
    Purpose:        display input required error message
    Parameters:     [Common Name ]
    Return Value:   none
  -------------------------------------------------------------
*/

function inputRequiredError(cName) {

	alert("\"" + cName + "\" is required to complete this form");

}

/*
  -------------------------------------------------------------
    Function:       isRequired
    Purpose:        tests whether or not field is required
    Parameters:     [fieldValue], [common name] [Boolean required]
    Return Value:   boolean
  -------------------------------------------------------------
*/

function isRequired(fieldValue, cName, required) {

		
		if (required != false && required != true) {
				return "advance";
	        }



								
		var fieldLength=parseInt(fieldValue.length);	
		
		if	(fieldLength > 0) {
			return "advance";
		}
				
		if (required) {
	   		inputRequiredError(cName);
		   return false;
		}
		if (! required) {
		 return true;
		}
}

/*
  -------------------------------------------------------------
    Function:       validText
    Purpose:        validate input text, input textArea
    Parameters:     [form name], [ field name ] , [common name], 
					[ max size ], [required]
    Return Value:   boolean
  -------------------------------------------------------------
*/

function validText(formName, fieldName, commonName, size, required) {


	var fullFieldName;

	var cmd = "fullFieldName=window.document." + formName + "." + fieldName
	eval(cmd)


	var condition=isRequired(fullFieldName.value, commonName, required);
	if (condition != "advance")
		return condition;

	if (parseInt(fullFieldName.value.length) > size) {
		textSizeError(commonName, size);
		return false;
	}

	if (! validCharacters(fullFieldName.value, commonName) )
		return false;
	return true;

}
function optionChosen(formName, fieldName, commonName) {

	var fullFieldName;
	var isChecked;
	var cmd = "fullFieldName=window.document." + formName + "." + fieldName
	eval(cmd)
//if (document.all) {
//alert("all");
//cmd="isChecked=document.all[" + formName + "]." + fieldName + ".checked";
//alert(cmd);
//eval(cmd);
//alert("all:  " + isChecked);
//return false;
//}else
if (document.layers) {
alert("layers");
cmd="isChecked=document.layers['" + formName + "'].document." + formName + "." + fieldName + ".checked";
eval(cmd);
alert("layers:  " + isChecked);
}else 

if (document.getElementById ) {
alert("getElement");
           cmd="isChecked=document." + formName + "." + fieldName + ".checked";
alert(cmd);
           eval(cmd);
alert("getElement:  " + isChecked);
alert("Value:  " + document.email.Gender.options[0]);
}
return isChecked;

	return true;

}

/*
  -------------------------------------------------------------
    Function:       validCharacters
    Purpose:        validate Characters
    Parameters:     [ field value ] , [common name]
    Return Value:   boolean
  -------------------------------------------------------------
*/

function validCharacters (fieldValue, cName) {
		
	var limit=fieldValue.length;
	
	for (j=0; j<limit; j++) {

	    for (k=0; k < invalidChar.length; k++) {
		
		if ( fieldValue.charAt(j) == invalidChar.charAt(k) ) {
		   alert(cName + " must not contain character:  \'" + invalidChar.charAt(k) + "\'");
	 	   return false;
		}
			

	    }

	}

	return true;

}

/*
  -------------------------------------------------------------
    Function:       validNumber
    Purpose:        validate input number
    Parameters:     [form name], [ field name ] , [common name], 
					[ max size ], [required]
    Return Value:   boolean
  -------------------------------------------------------------
*/

function validNumber(formName, fieldName, commonName, size, required) {


	var fullFieldName, j;

	var cmd = "fullFieldName=window.document." + formName + "." + fieldName
	eval(cmd)


	var condition=isRequired(fullFieldName.value, commonName, required);
	if (condition != "advance")
		return condition;

	if (parseInt(fullFieldName.value.length) > size) {
		textSizeError(commonName, size);
		return false;
	}

	var varNumber = fullFieldName.value;

	for (j=0; j < size; j++) {
		if ( isNaN( varNumber.charAt(j)) ) {
		alert(commonName + " must contain numeric characters only");
		return false;

		}
	}

	return true;

}

/*
  -------------------------------------------------------------
    Function:       validPhone
    Purpose:        validate telephone input text
    Parameters:     [form name], [ field name ], [common name],
	                [boolean required]
    Return Value:   boolean
  -------------------------------------------------------------
*/

function validPhone (formName, fieldName, commonName, size, required) {


	//format:  000-000-0000

	var fullFieldName;

	var cmd = "fullFieldName=window.document." + formName + "." + fieldName
	eval(cmd)

	var condition=isRequired(fullFieldName.value, commonName, required);
	if (condition != "advance")
		return condition;

	var fieldLength=parseInt(fullFieldName.value.length);

	var j;
	
	if (fieldLength == size) {

		var phoneNumber = fullFieldName.value;


		for (j=0; j < size; j++ ) {

		  if (j != 3 && j != 7 ){



			if ( isNaN( phoneNumber.charAt(j)) ) {
			  alert(commonName + " must contain numeric characters only");
			  return false;
		    }
				
		  }
		   else {

			 if (phoneNumber.charAt(j) == '-') {
				continue;
			 }
			 else {
			   alert(commonName + " format required:  000-000-0000");
			   return false;
			 }
		  }
		
		}
		
	}
	else {

		alert(commonName + " format required:  000-000-0000");
		return false;

	}
	return true;

}//validPhone

/*
  -------------------------------------------------------------
    Function:       validDate
    Purpose:        validate date
    Parameters:     [form name], [ year ], [month], [day], 
					[common name], [required]
    Return Value:   boolean
  -------------------------------------------------------------
*/

	function validDate(formName, y, m, d, commonName, required) {
	
		var dForm="window.document." + formName;

		var cmd="year=" + dForm + "." + y + ".options[" + dForm + "." + y + ".selectedIndex].value"
		eval(cmd)

		var cmd="month=" + dForm + "." + m + ".options[" + dForm + "." + m + 		".selectedIndex].value"
		eval(cmd)

		var cmd="day=" + dForm + "." + d + ".options[" + dForm + "." + d + ".selectedIndex].value"
		eval(cmd)


		var condition=isRequired(year, commonName, required);
		if (condition != "advance") 
		  return condition;

		condition=isRequired(month, commonName, required)
		if (condition != "advance")
		  return condition;

		condition=isRequired(day, commonName, required)
		if (condition != "advance")
		  return condition;

			switch (month) {

				case "09":
				case "04": 
                                case "06": 
                                case "11": {
				  if (parseInt(day) > 30) {
					alert ("Invalid date:  " + year + "-" + month + "-" + day);
					return false;
					}
				  else
					return true;
				}

				case "02": {
			   if (parseInt(day) > 29 ) {
				alert ("Invalid date:  " + year + "-" + month + "-" + day);
					 return false;
				}
			        if (parseInt(day) == 29) {
					if (leapYear(year))
						return true;
					else {
						alert ("Invalid date:  " + year + "-" + month + "-" + day);
						return false;
					}
				  }
				}

				
			}//switch

			return true;
	}

/*
  -------------------------------------------------------------

    Function:       leapYear
    Purpose:        tests whether or not a year is a leap year
    Parameters:     [year]
    Return Value:   boolean
  -------------------------------------------------------------
*/

	function leapYear(year) {


		
			if ( (parseInt(year)%400 == 0)  ||
				parseInt(year)% 4 == 0 )
				return true;
			if ( (parseInt(year)%100) == 0)
				return false;


	}//leapYear		

/*
  -------------------------------------------------------------
    Function:       validSSN
    Purpose:        validate social security input, format 000000000
    Parameters:     [form name], [ field name ], [common name],
	                [boolean required]
    Return Value:   boolean
  -------------------------------------------------------------
*/

function validSSN (formName, fieldName, commonName, size, required) {


	//format:  000-00-0000

	var fullFieldName;

	var cmd = "fullFieldName=window.document." + formName + "." + fieldName
	eval(cmd)

	var condition=isRequired(fullFieldName.value, commonName, required);
	if (condition != "advance")
		return condition;

	var fieldLength=parseInt(fullFieldName.value.length);

	var j;
	
	if (fieldLength == size) {

		var ssn = fullFieldName.value;


		for (j=0; j < size; j++ ) {

		  if (j != 3 && j != 6 ){



			if ( isNaN( ssn.charAt(j)) ) {
			  alert(commonName + " must contain numeric characters only");
			  return false;
		    }
				
		  }
		   else {

			 if (ssn.charAt(j) == '-') {
				continue;
			 }
			 else {
			   alert(commonName + " format required:  000-00-0000");
			   return false;
			 }
		  }
		
		}
		
	}
	else {

		alert(commonName + " format required:  000-00-0000");
		return false;

	}
	return true;

}//validSSN


/*
  -------------------------------------------------------------
    Function:       validYear
    Purpose:        validate input year
    Parameters:     [form name], [ field name ] , [common name], 
					[ max size ], [required]
    Return Value:   boolean
  -------------------------------------------------------------
*/

function validYear(formName, fieldName, commonName, size, required) {


	var fullFieldName, j;

	var cmd = "fullFieldName=window.document." + formName + "." + fieldName
	eval(cmd)


	var condition=isRequired(fullFieldName.value, commonName, required);
	if (condition != "advance")
		return condition;

	if (parseInt(fullFieldName.value.length) > size) {
		textSizeError(commonName, size);
		return false;
	}

	var varNumber = fullFieldName.value;
	if (  
		(varNumber.charAt(0)  == 2 && varNumber.charAt(1) != 0)  ||
		(varNumber.charAt(0)  == 1 && varNumber.charAt(1) != 9)  
	) {

		alert("Invalid " + commonName + " entered");
		return false;
	}
		


	for (j=0; j < size; j++) {
		if ( isNaN( varNumber.charAt(j)) ) {
		alert(commonName + " must contain numeric characters only");
		return false;

		}
	}

	return true;

}


/*
  -------------------------------------------------------------
    Function:       validSSNnoDash
    Purpose:        validate social security input, format: 000000000
    Parameters:     [form name], [ field name ], [common name],
	                [boolean required]
    Return Value:   boolean
  -------------------------------------------------------------
*/

function validSSNnoDash (formName, fieldName, commonName, size, required) {


	//format:  000000000

	var fullFieldName;

	var cmd = "fullFieldName=window.document." + formName + "." + fieldName
	eval(cmd)

	var condition=isRequired(fullFieldName.value, commonName, required);
	if (condition != "advance")
		return condition;

	var fieldLength=parseInt(fullFieldName.value.length);

	var j;
	
	if (fieldLength == size) {

		var ssn = fullFieldName.value;


		for (j=0; j < size; j++ ) {


			if ( isNaN( ssn.charAt(j)) ) {
			  alert(commonName + " must contain numeric characters only");
			  return false;
		    }
				
		}
		
	}
	else {

		alert(commonName + " format required:  000000000");
		return false;

	}
	return true;

}//validSSNnoDash

function validEmailCharacters (fieldValue, cName) {
		
	var limit=fieldValue.length;
	var ampersand=0;
	var period=0;
	
	for (j=0; j<limit; j++) {

	    for (k=0; k < invalidEmailChar.length; k++) {
		
		if ( fieldValue.charAt(j) == invalidEmailChar.charAt(k) ) {
		   alert(cName + " must not contain character:  \'" + invalidEmailChar.charAt(k) + "\'");
	 	   return false;
		}

	    }
	    if ( fieldValue.charAt(j) == "@" ) {
		ampersand= ampersand + 1;
	    }
	    if ( fieldValue.charAt(j) == "." ) {
		period= period + 1;
	    }

	}
	if (ampersand > 1 || period < 1 || ampersand == 0  ) {
          alert(cName + " is invalid");
	  return false;

	}

	return true;

}

function validEmail(formName, fieldName, commonName, size, required) {


	var fullFieldName;

	var cmd = "fullFieldName=window.document." + formName + "." + fieldName
	eval(cmd)


	var condition=isRequired(fullFieldName.value, commonName, required);
	if (condition != "advance")
		return condition;

	if (parseInt(fullFieldName.value.length) > size) {
		textSizeError(commonName, size);
		return false;
	}

	if (! validEmailCharacters(fullFieldName.value, commonName) )
		return false;
	return true;

}
function validCheckbox(formName, fieldName, required, errorMsg) {


     var fullFieldName;
  
     var cmd = "fullFieldName=window.document." + formName + "." + fieldName
     eval(cmd)

       if  ( (fullFieldName.checked != required ) && ( errorMsg != undefined )  && ( required == true) ) {
	     
	      alert(errorMsg); 
	}
        if  (fullFieldName.checked != required  && required == true) {
	     return false;
	}

       return true;

}

