/*Header Begin

$Author: Hem.balla $

$Workfile: FieldValidation.js $

$Modtime: 8/10/05 2:55p $

$Revision: 2 $

$NoKeywords: $

Change History: Put the issue ID and your description of the changes here

Header Ends*/

/********************************************************************************************/
/* 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].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 required text field is blank. If yes, return false.
 * Accepts list of one to many form elements as parameters.
 */
	function SilentTrapBlankText()
	{
		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 <= '    '))
				{
					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 option selection has been made. If not, return false.
 * Accepts a list of one to many form elements as parameters.
 */
	function SilentTrapNoneSelected()
	{
		for (i = 0; i < arguments.length; i++)
		{
			if ((arguments[i].type == "select-one") | (arguments[i].type == "select-multiple"))
			{
				
				if (arguments[i].selectedIndex == 0)
				{
					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 TrapNoneSelectedDesc()
	{
		for (i = 0; i < arguments.length; i=i+2)
		{
			if ((arguments[i].type == "select-one") | (arguments[i].type == "select-multiple"))
			{
				
				if (arguments[i].selectedIndex == 0)
				{
					alert("A selection must be made for \"" 
							+ arguments[i+1] + "\". 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 required radio selection has been made. If not, return error message.
 * Accepts a list of one to many form elements as parameters.
 */	
	function SilentTrapNoRadioSelected()
	{
		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))
				{
					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 TrapNoRadioSelectedDesc()
	{
		for (i = 0; i < arguments.length; i=i+2)
		{
			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 must be made for \"" + arguments[i+1]
							+ "\".  Please correct and resubmit.");
					tempRad[0].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 TrapNoCheckboxSelectedDesc()
	{
		for (i = 0; i < arguments.length; i=i+2)
		{
			if (arguments[i][0].type == "checkbox")
			{
				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 must be made for \"" + arguments[i+1]
							+ "\".  Please correct and resubmit.");
					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);
		var regex = /^\w+([\.-\\'\-]*\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;

/*  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;
	}

/*	Check if the dollar amount is passed in the form "XXXXX.XX". */
	function TrapInvalidDollar() 
	{

/*  First form the Regular Expression object that describes a proper Email address */

		var regex = /^\d*.\d{2}/;

/*  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 dollar amount entered does not appear to be in a valid format. "
							+ "'xxxxx.xx' " 
							+ "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);
		
	} 
	
	function LTrim(str) {
		var new_str="";
		start_pos = 0;
		new_len = str.length;
		if (str.length != 0) {
			for (i=0;i<str.length;i++) {
				if (str.substring(i,i+1) != ' ') {
					start_pos = i;
					i = str.length;
				}
				else {
					new_len--;
				}
			}
			new_str = str.substr(start_pos,new_len);
		}
		
		return new_str;
	}
	
	function RTrim(str) {
		var new_str="";
		var end_pos = str.length;
		if (str.length != 0) {
			for (i=str.length-1;i>=0;i--) {
				if (str.substring(i,i+1) != ' ') {
					end_pos = i;
					i = -1;
				}
			}
			new_str = str.substring(0,end_pos+1);
		}
		return new_str;
	}
	
	
/* Check if required text field is blank. If yes, return error message.
 * This is a variation on "TrapBlankText" that accepts a list alternating text form
 * elements and strings that describe the preceding form field.  These strings show
 * up in the error message.
 */
	function TrapBlankTextDesc()
	{
		for (i = 0; i < arguments.length; i=i+2)
		{
			if ((arguments[i].type == "text") | (arguments[i].type == "textarea"))
			{
				if ((arguments[i].value.length == 0) || (arguments[i].value <= '    '))
				{
					arguments[i].focus();
					alert("The required field \""+ arguments[i+1] + "\" has been left blank.  "
							+ "Please check your input "
							+ "when you are returned to the field. ");
					arguments[i].focus();
					return false;
				}
			}
		}
		return true;
	}

/**
 * Converts field values to title case (Which Looks Like This).
 */
	function ToTitleCase() {
		for (i = 0; i < arguments.length; i++)
		{
			var s = arguments[i].value;
			var newString = '';
			var capNext = true;
			for(var j=0; j<s.length; j++) {
				if(capNext) {
					newString += s.charAt(j).toUpperCase();
				} else {
					newString += s.charAt(j).toLowerCase();
				}
				capNext = false;
				if((s.charAt(j) < 'a' || s.charAt(j) > 'z') && (s.charAt(j) < 'A' || s.charAt(j) > 'Z'))
				{
					capNext = true;
				}
			}
			arguments[i].value = newString;
		}
	}	

/**
 **  This strips out commas, dollar signs and blanks.  A good cleanup for money input fields.
**/	
	function CleanMoneyInput()
	{
		for (i = 0; i < arguments.length; i++)
		{
			var s = arguments[i].value;
			var newString = '';
			for (var j=0; j < s.length; j++)
			{
				if ((s.charAt(j) == ',') | (s.charAt(j) == '$') | (s.charAt(j) == ' '))
				{
					// do nothing
				}
				else
				{
					newString += s.charAt(j);
				}
			}
			arguments[i].value = newString;
		}
	}
	
	