/********************************************************************************************/

/* FieldValidation.js                                                                       */

/*                                                                                          */

/* These routines are standard javascript functions that perform validations on CGI form    */

/* fields.  All have been tested on browser versions 4 & 5 for IE and Netscape 4.           */

/********************************************************************************************/





/* Check if required text field is blank. If yes, return error message.

 * Accepts list of one to many form elements as parameters.

 */



	function TrapBlankText()

	{

		for (i = 0; i < arguments.length; i++)

		{

			if ((arguments[i].type == "text") | (arguments[i].type == "textarea"))

			{

				if ((arguments[i].value.length == 0) || (arguments[i].value <= '    '))

				{

					arguments[i].focus();

					alert("You have left blank a field that is required.  Please check your input "

							+ "when you are returned to the field. ");

					arguments[i].focus();

					return false;

				}

			}

		}

		return true;

	}



	

	

/* Check if numerical field is populated with text. If yes, replace with 0.

 * Accepts a list of one to many form elements as parameters. 

 */

	function ReplaceNanTextWithZero()

	{

		for (i = 0; i < arguments.length; i++)

		{

			if (arguments[i].type == "text")

			{

				if ( isNaN(Number(arguments[i].value)) || (arguments[i].value <= "") )

				{

					arguments[i].value = 0;

				}

			}

		}

		return true;

	}

	

	

	

/* Check if required numerical field is populated with text. If yes, return error message.

 * Accepts a list of one to many form elements as parameters.

 */	

	function TrapNanText()

	{

		for (i = 0; i < arguments.length; i++)

		{

			if (arguments[i].type == "text")

			{

				if ( isNaN(Number(arguments[i].value)) )

				{

					alert("This field must be a number.  The update canceled.");

					arguments[i].focus();

					return false;

				}

			}

		}

		return true;

	}

	

	

	

/* Check if required option selection has been made. If not, return error message.

 * Accepts a list of one to many form elements as parameters.

 */

	function TrapNoneSelected()

	{

		for (i = 0; i < arguments.length; i++)

		{

			if ((arguments[i].type == "select-one") | (arguments[i].type == "select-multiple"))

			{

				

				if (arguments[i].selectedIndex == 0)

				{

					alert("A selection in this field must be made. The submit has been canceled.");

					arguments[i].focus();

					return false;				

				}

			}

		}

		return true;

	}

	

	

	

/* Check if required radio selection has been made. If not, return error message.

 * Accepts a list of one to many form elements as parameters.

 */	

	function TrapNoRadioSelected()

	{

		for (i = 0; i < arguments.length; i++)

		{

			if (arguments[i][0].type == "radio")

			{

				tempRad = arguments[i];

								

				for (j = 0; j < tempRad.length; j++)

				{

					if (tempRad[j].checked) break;

				}

				if ((j == tempRad.length) || (!tempRad[j].checked))

				{

					alert("A selection in this field must be made. The submit has been canceled.");

					tempRad[0].focus();

					return false;				

				}

			}

		}

		return true;

	}

	

	

	

/* Check if index of required option selection is 0. This is the index number of the first option

 * in the list which is usually "None Selected" or similar. If yes, return error message.

 * Accepts a list of one to many form elements as parameters.

 */

	function TrapZeroSelectValue()

	{

		for (i = 0; i < arguments.length; i++)

		{

			if ((arguments[i].type == "select-one") | (arguments[i].type == "select-multiple"))

			{

				if (arguments[i].options[arguments[i].selectedIndex].value == 0)

				{

					alert("A selection must be make in this field to continue.  "

							+ "Please select another option from the list");

					arguments[i].focus();

					return false;				

				}

			}

		}

		return true;

	}



	

	

	

	

	

	

/*	Check if the EMail address has a valid syntax.  NOTE: this does not go out and

 *  very that the given Email server and address actually exist, it only checks

 *  for proper formatting.  Accepts a list of one to many text elements as parameters. 

 */

	function TrapInvalidEmail() 

	{



/*  First form the Regular Expression object that describes a proper Email address */



		var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";

		var regex = new RegExp(emailReg);

		

/*  Then loop through the argument list (they should all be text input elements) and 

 *  check each one against the regular expression.

 */

		for (i = 0; i < arguments.length; i++)

		{

			if (arguments[i].type == "text")

			{

				if (! regex.test(arguments[i].value))

				{

					alert("The Email address entered does not appear to be in a valid format. "

							+ "Please check and reenter. ");

							arguments[i].focus();

							return false;

				}

			}

		}

		

/*  If we get through and everything passes, return true  */

		return true;

	}

	

/*	The next two functions are to findout if a date is before or after the other.

	It accepts only two delimiters, '/' and '-' 

*/

	function isbefore(date1,date2) {

		if (date1.indexOf("/") != -1) {

			var d1_array = date1.split("/");

			var d2_array = date2.split("/");

		}

		else {

			var d1_array = date1.split("-");

			var d2_array = date2.split("-");

		}

		var dt1 = new Date(d1_array[2],d1_array[0]-1,d1_array[1]);

		var dt2 = new Date(d2_array[2],d2_array[0]-1,d2_array[1]);

		

		if (dt1 < dt2) {

			return true;

		}

		else {

			return false;

		}

	}

	

	function isafter(date1,date2) {

		

		return isbefore(date2,date1);

		

	}

		