//<Script Language=JavaScript>

/********************************************************************************
*						JavaScript : FormValidation.js							*
*					Author : Korakot Tiannguen, R&D Department					*
*						  Supervisor : Thalwin Hulsebos							*
*								Version : Rev.13								*
*							Created : July 14, 2000								*
*							Rev. Date : Aug 18, 2000							*
********************************************************************************/
/********************************************************************************
*									Description									*
*	This is a JavaScript library that can validate spcified fields. It allows	*
* programmers to specify the need of field checking at the begining of the HTML	*
* page. This library takes only parameters that are necessary for the checking	*
* action. The properties of this library are : field type, field name, field	*
* need(1 or 0), field datatype(only for "text", field default value, field		*
* options and field error messages the programmers want to say to the users. If	*
* the requirement is satisfied, the form is submitted but if it is not, error	*
* message will be displayed and the required field is focused automatically.	*
* The script can also capable of checking muliple-form and produces error		*
* messages in one alert statement.												*
*																				*
*	In conjuction to the multi-form validation, the script can now be able to	*
* specify conditional statement that the will be checked if the statement is	*
* true. With the lastest improvement, the checkBox validation has been added in	*
* the case of comfirmation needed in a Web page before submitting the form.		*
********************************************************************************/
/********************************************************************************
*									Revision 1									*
*																				*
*	- Major changing by not trying to perform auto-replacement on those error	*
*		fields																	*
*																				*
*									Revision 2									*
*																				*
*	- Adding flexibility format to the 'addValidation()' to take parameters		*
*		dynamically according to the filed types								*
*																				*
*									Revision 3									*
*																				*
*	- Taking Checkbox validation out of the validating function					*
*																				*
*									Revision 4									*
*																				*
*	- Adding DATE validation to the data-type allow DATE format to be reconized	*
*	- Allow the programmers to specifiy their own error messages				*
*	- Adding focus capability onto those error fields							*
*																				*
*									Revision 5									*
*																				*
*	- Solve the DATE checking problem that has been tested by QA				*
*																				*
*									Revision 6									*
*																				*
*	- Improving DATE checking by adding flexibility to dating format			*
*	- Improving numeric checking formation that can take comma separator		*
*	- Adding the recommended validating function of email address				*
*	- More complete program comments											*
*																				*
*									Revision 7									*
*								date: Aug 3,2000								*
*																				*
*	- Fixing bugs found by QA in DATE year checking and date digits problem		*
*		(01 and 1)																*
*																				*
*									Revision 8									*
*								date: Aug 28, 2000								*
*																				*
*	- Fixing the multi-form incompatibility of the script						*
*	- An extra field, form name, needs to be referred to in the second position	*
*		of the addValidation() function.										*
*	- Two global variables are intended for multi-form compatibility.			*
*	- Error messages are now been integrated into one alert message				*
*																				*
*									Revision 9									*
*								date: Sep 4, 2000								*
*																				*
*	- Re-engineering the formValidation to be an object-oriented approach of	*
*		the form.																*
*	- Added the conditional ability to paticular fields that will be validated	*
*		according to other fields.												*
*	- Improving processing time by get rid of looping throught the form array	*
*																				*
*									Revision 10									*
*								date: Oct 5, 2000								*
*																				*
*	- Adding the checkBox functionality to satisfy the confirmation validation	*
*		of a Web page															*
*																				*
*									Revision 11									*
*								date: Oct 18, 2000								*
*																				*
*	- Adding the password field checking. However the check would be made just	*
*		to check for an unempty password field.									*
*																				*
*									Revision 12									*
*								date: Nov 9, 2000								*
*																				*
*	- Fixing bug, the form is now able to submit if there is no addValidation	*
*		existing in that form (no form to be validated).						*
*																				*
*									Revision 13									*
*								date: August 18, 2001							*
*																				*
*	- Revision for supporting the InetMessage alert test (If want this option 	*
*		please insert the InetMessage.js into your code.).						*
*		Modified on function validateConditionFields().							*
*********************************************************************************/
// Global variable definitions

var fieldErrMsg = '';
var fieldErrMsgCount = 1;

var frms = new Array();			// An array that keeps the registered addValidation elements
var formPos = -1;				// The form object position of the registered addValidation elements

function frmObj(){
//-----------------------------------------------------------------------------
// Function Name:	frmObj()
//
// Functionality:	Stores the registering form name and the field object by using
//					its addItem method of this class.
//
// Input		:	Takes the a serie of field objects and stores them in the
//					array together with the form name.
//
// Output		:	A collection of fields objects and the form name.
//-----------------------------------------------------------------------------
	this.fields = new Array();		// an array of field objects
	this.name = '';					// the form name

	this.addItem = addingItem;		// a method to add new items to the registering form
}

function addingItem(item, frmName){
//-----------------------------------------------------------------------------
// Function Name:	addingItem()
//
// Functionality:	This is the method of the frmObj class that stores an array
//					of the field objects array with the form name.
//
// Input		:	A field object and the name of the form of that field.
//
// Output		:	A form and field structure of the registering field in a form.
//-----------------------------------------------------------------------------
	if(!this.created){
		this.fields[this.fields.length] = item;		// stores the field objects in this array
		this.name = frmName;						// stores the form name
	}
}

function fieldObj(name, opts, dataType, cond, msg){
//-----------------------------------------------------------------------------
// Function Name:	fieldObj()
//
// Functionality:	This is the field object class that stores the fields' information.
//
// Input		:	The field name, option, data-type, condition, and an error message.
//
// Output		:	The field object that can be stored in the frmObj class.
//-----------------------------------------------------------------------------
	this.cond = cond;							// condition of the validating field
	this.name = name;							// the field name
	this.opts = opts;							// the option
	this.dataType = dataType;					// the data type
	this.msg = msg;								// the error message
	this.type = '';								// the field type

	this.setType = setFieldType;				// the method to set the field type
	this.getType = getFieldType;				// the method to get the field type
}

function setFieldType(type){
	if(!this.created)
		this.type = type;						// stores the field type
}

function getFieldType(){
	return this.type;							// retrieves the field type
}

function addValidation(f1, f2, f3, f4, f5, f6){
//-----------------------------------------------------------------------------
// Function Name:	addValidation()
//
// Functionality:	This function registers incomming field into the field object
//					and stores them into an array of an object by using a class
//					'fieldObj'. This is the reference registration routine. Then
//					store it into the form object in the form array.
//
// Input		:	6 input elements needed to be inserted into the 'fieldObj'
//					in the order as refer to in the 'fieldObj' function. The need
//					of the parameters in any field is determined here. 'Null' values
//					are used for the unnessary properties of the field.
//
// Output		:	An array of registered fields in an object with its properties.
//
// Example		:	addValidation("form1", "radio1", "Null", "Null", "Null", "Error!! Please check one of the choice!!");
//-----------------------------------------------------------------------------
	var theField;									// the field object's object
	var form;										// the form object's object
	var existPos = -1;								// the form submitted position in the frms array
	
	
	
	// creates the field object
	theField = new fieldObj(f2, f3, f4, f5, f6);
	
	// initialises the form object
	form = new frmObj();
	if(frms.length == 0){
		frms[frms.length] = form;
		frms[frms.length-1].addItem(theField, f1);
	}
	else{
		// checks if the form name is exists
		for(var i=0; i<frms.length; i++){
			if(frms[i].name == f1){
				existPos = i;
				break;
			}
			else
				existPos = -1;
		}
		if(existPos != -1){
			// stores the field object in the existing form name
			frms[existPos].addItem(theField, f1);
		}
		else{
			// stores the field object in the new form name
			frms[frms.length] = form;
			frms[frms.length-1].addItem(theField, f1);
		}
	}
	
}

function recordVerification(name){
//-----------------------------------------------------------------------------
// Function Name:	recordVerification()
//
// Functionality:	Verifies that the registered forms and fields are exists in
//					the submitting form object. It also stores the submmitting
//					form object position to the field object as one of its property.
//
// Input		:	The registered form and the summitting form object elements.
//
// Output		:	The result will be true if the registered fields and forms are
//					exists in the submitting form object and vice versa. The error
//					messages will be generated if the item is not matched the validating
//					criteria.
//-----------------------------------------------------------------------------
	var internalErrMsg = '';									// Internal error message string
	var internalErrMsgCount = 1;								// Internal error message count
	
	// searchs for the form in the frms array against the submitted form
	for(var i=0; i<frms.length; i++){
		if(frms[i].name == name)
			// stores the position of the form as a global variable
			formPos = i;
	}
	// if the form is found in the submitted form object
	if(formPos != -1){
		// looping for every elements in the frms array for the field object
		for(var i=0; i<frms[formPos].fields.length; i++){
			// if the submitted form object match the registered object in the frms array
			if (typeof(eval('document.' + frms[formPos].name + '.' + frms[formPos].fields[i].name)) == "undefined"){
				internalErrMsg+= internalErrMsgCount++ + ") " + frms[formPos].fields[i].name + "\n";
			}
			else{
				// if found, stores the information of the submitted form in the field object in the frms array
				var temp = eval('document.' + frms[formPos].name + '.' + frms[formPos].fields[i].name);
				// special type of the radio array
				if(typeof(temp.type) == "undefined"){
					frms[formPos].fields[i].setType("radio");
				}
				else
					frms[formPos].fields[i].setType(temp.type);
			}
		}
		
		// checks for the error message and alerts it
		if(internalErrMsg != ''){
			alert("Validation Module: The Following item(s) not exists in the form '" + frms[formPos].name + "' \n\n" + internalErrMsg);
			return "noField";
		}
		else
			return true;
	}
	return "noForm";
}

function validateConditionFields(){
//-----------------------------------------------------------------------------
// Function Name:	validateConditionFields()
//
// Functionality:	This function checks for the condition specified with the
//					registered field and act accordingly upon it.
//
// Input		:	The formPos that keeps the position of the operating form
//
// Output		:	The true is return if the field is correctly validated and false
//					if it is not. The error message will be generated if the field
//					is not satisfy the validating criteria.
//-----------------------------------------------------------------------------
	
	
	//var fieldErrMsg = '';						// field error message string
	//var fieldErrMsgCount = 1;					// field error message counter
	
	var isValid = false;						// stores the validation result
	var toFocus = false;						// stores the focusing result

	// searching through the field objects in the frms array of the condition
	for(var i=0; i<frms[formPos].fields.length; i++){
		// if no condtion
		if(frms[formPos].fields[i].cond.toLowerCase() == "null"){
			isValid = fieldValidation(i);
		}
		else{
			// if the condtion presents
			if(eval("document." + frms[formPos].fields[i].cond)){
				isValid = fieldValidation(i);
			}
			else
				isValid = true;
		}
		// if the error is found from the validation
		if(!isValid){
			// generates the error messages
			// Add enter at the last of messages if that message wrote in fomate have not ENTER
			if (frms[formPos].fields[i].msg.lastIndexOf('\n') == -1) 
				fieldErrMsg += fieldErrMsgCount++ + ") " + frms[formPos].fields[i].msg + "<br>";
				//fieldErrMsg += fieldErrMsgCount++ + ") " + frms[formPos].fields[i].msg + "\n";
			else
				fieldErrMsg += fieldErrMsgCount++ + ") " + frms[formPos].fields[i].msg + "<br>";
			
			// focusing on the first error field
			if(!toFocus){
				var obj = eval("document." + frms[formPos].name + "." + frms[formPos].fields[i].name);
				if(typeof(obj.type) == "undefined") {
					obj[0].focus();
					toFocus = true;
				}
				else {
					if(obj.type != "hidden") {
						obj.focus();
						toFocus = true;
					}	
				}
			}
		}
	}
	
	// alerts the error message	
	if(fieldErrMsg != ''){
		//alert("Validation Warning, please check the following list:\n\n"+ fieldErrMsg);
		
		var msg = "Please check the following list:<br><br>"+ fieldErrMsg;
		var frmName1 = frms[formPos].name;
		var frmtemp = eval('document.' + frms[formPos].name + '.' + 'hidMsg');

		frmtemp.value = msg;
		//window.open( "windowmsg.asp?frmName="+frmName1, '_blank',  "height=400,width=350,top=200,left=320,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes"); 	
		
		
		return false;
	}
	else
		return true;
	
}

function fieldValidation(index){
//-----------------------------------------------------------------------------
// Function Name:	fieldValidation()
//
// Functionality:	Classifying the type of the field and calls function  according
//					to the correct type.
//
// Input		:	The index of the field object in the frms array.
//
// Output		:	The result of the field validation wil be sent back to the
//					validateConditionFields().
//-----------------------------------------------------------------------------
	var isValid = false;
	var obj = eval("document." + frms[formPos].name + "." + frms[formPos].fields[index].name);
	
	switch(frms[formPos].fields[index].type){
		case "text"				:	isValid = textChk(index, obj);
									break;
		//case "textarea"			:	isValid = textareaChk(obj);
		case "textarea"			:	isValid = textChk(index, obj);
									break;
		case "radio"			:	isValid = radioChk(index, obj);
									break;
		case "select-one"		:
		case "select-multiple"	:	isValid = selectChk(index, obj);
									break;
		case "hidden"			:	isValid = textChk(index, obj);
									break;
		case "checkbox"			:	isValid = checkBoxChk(obj);
									break;
		//case "password"			:	isValid = passwordChk(obj);
		case "password"			:	isValid = textChk(index, obj);
	}
	return isValid;
}

function textChk(frmsIndex, obj){
//-----------------------------------------------------------------------------
// Function Name:	textChk()
//
// Functionality:	Classifying the data type of the specify text field and then
//					calling the appropriate function according to the following
//					data type; 'alphabet', 'numeric' and 'date'.
//
// Input		:	The frms array's index and the formObj's element
//					index are needed in order to get the correct pair of elements.
//
// Output		:	If the returned value from the function called is true, the
//					it will be returned back to the fieldChecker(), otherwise
//					false will be returned.
//
// Example		:	addValidation('form1', 'text1', 1, 'alphabet', "Null", 'Error!! this field is needed');
//					- The first item is the form name.
//					- The second item is the field name.
//					- The third item is the indicated if the field is needed to be
//						evaluated its value(1) or just check if the field is not empty only(0).
//					- The fouth item is the type of the input.
//					- The fifth item is the condition of the field.
//					- The sixth item is the programmer's error message.
//-----------------------------------------------------------------------------
	var isValid = false;
	
	if ((frms[formPos].fields[frmsIndex].opts != 1)&&(frms[formPos].fields[frmsIndex].opts != 0)) {
		if(obj.value.length != 0){
			switch(frms[formPos].fields[frmsIndex].dataType){
				//'length'			:	Accept	->	Character + Numeric or Character only and length <= lenval
				//					:	Reject	->	Numeric only or length > lenval
				case 'length'		:	lenval=frms[formPos].fields[frmsIndex].opts;
										isValid=lengthChk(obj,lenval);
										break;
				//'lengthID'		:	Accept	->	Numeric or - and length <= lenval
				//					:	Reject	->	Character or length > lenval
				case 'lengthID'		:	lenval=frms[formPos].fields[frmsIndex].opts;
										isValid=lengthIDChk(obj,lenval);
										break;
				//'lengthMixed'		:	Accept	->	Character + Numeric or Character only or Numeric only and length <= lenval
				//					:	Reject	->	length > lenval
				case 'lengthMixed'	:	lenval=frms[formPos].fields[frmsIndex].opts;
										isValid=lengthMixedChk(obj,lenval);
										break;
				//'lengthID'		:	Accept	->	Character + Numeric or Numeric only and length <= lenval
				//					:	Reject	->	Character only or length > lenval
				case 'lengthNAT'	:	lenval=frms[formPos].fields[frmsIndex].opts;
										isValid=lengthNATChk(obj,lenval);
										break;
				case 'compare'		:	lenval=frms[formPos].fields[frmsIndex].opts;
										isValid=CompareChk(obj,lenval);
										break;
				case 'lengthNoSpecial'	:lenval=frms[formPos].fields[frmsIndex].opts;
										isValid=lengthNOSPChk(obj,lenval);
										break;
				case 'lengthNoNum'		:lenval=frms[formPos].fields[frmsIndex].opts;
										isValid=lengthNONumChk(obj,lenval);
										break;
				default				:	alert("Internal Error Message: \nThere is no datatype: '" + frms[formPos].fields[frmsIndex].dataType + "'.");
			}
		}
	} 
	else {
		if(frms[formPos].fields[frmsIndex].opts == 1){
			if(obj.value.length != 0){
				switch(frms[formPos].fields[frmsIndex].dataType){
					//'alphabet'		:	Accept	->	Character + Numeric or Character only
					//					:	Reject	->	Numeric only
					case 'alphabet'		:	isValid=alphabetChk(obj);
											break;
											
					//'numeric'			:	Accept	->	Numeric only (can be float and negative number)
					//						Reject	->	Character
					case 'numeric'		:	isValid=numericChk(obj);
											break;
					
					//'date:d-m-y'		:	Accept	->	Date 
					case 'date:d-m-y'	:
					case 'date:m-d-y'	:	isValid=dateChk(frmsIndex, obj);
											break;
					case 'date:y-m-d'	:	isValid=dateChk(frmsIndex, obj);
											break;
					
					//'email'			:	Accept	->	Email format
					case 'email'		:	isValid=emailChk(obj);
											break;
											
					//'phone'			:	Accept	->	Character + Numeric or Number only
					//						Reject	->	Character only
					case 'phone'		:	isValid=PhoneChk(obj);
											break;
											
					//'positiveint'		:	Accept	->	Numeric only (must be positive integer)
					//					:	Reject	->	Negative number and decimal
					case 'positiveint'	:	isValid=PosIntChk(obj);
											break;
											
					//'positivenum'		:	Accept	->	Numeric only and '.' (must be positive), can be float or integer
					//					:	Reject	->	Negative number
					case 'positivenum'	:	isValid=PosChk(obj);					
											break;
											
					//'timer'			:	Accept	->	Numeric and ':', '.' (must be positive), can be float or integer
					//					:	Reject	->	Negative number
					case 'timer'		:	isValid=TimeChk(obj);					
											break;
											
					//'textonly'		:	Accept	->	Character only
					//					:	Reject	->	Number
					case 'textonly'		:	isValid=TextOnlyChk(obj);
											break;
					
					//'mixed'			:	Accept	->	Character only, Number only, Character+Number
					case 'mixed'		:	isValid=MixedChk(obj);
											break;
					
					//'spaceExp			:   Exception space and ""
					case 'spaceExp'		:	isValid=SpaceExpChk(obj);
											break;
											
					case 'notnull'		:	isValid=NotNullChk(obj);
											break;
					
					default				:	alert("Internal Error Message: \nThere is no datatype: '" + frms[formPos].fields[frmsIndex].dataType + "'.");
				}
			}
		}
		else{
			if(frms[formPos].fields[frmsIndex].opts == 0){
				if(obj.value.length != 0)
					isValid = true;
			}
			else
				alert("Internal Error Message: \nThe option of the field: '" + frms[formPos].fields[frmsIndex].name + "' should be only 1 or 0.");
		}
	}
	return isValid;
	
}

function NotNullChk(obj) {
	objVal = obj.value;
	if (objVal.length==0)
		return false;
	else
		return true;
}

function alphabetChk(obj){
//-----------------------------------------------------------------------------
// Function Name:	alphabetChk()
//
// Functionality:	Validating the correct format of alphanumeric values from
//					the input text. By using isNaN() function that will return
//					true if the string input is not a number and vise versa.
//
// Input		:	The formObj's element index in order to get the field's value
//
// Output		:	False will be returned to the textChk() if a numeric value
//					found or else true will be returned.
//-----------------------------------------------------------------------------
	if(isNaN(obj.value))
		return true;
	else
		return false;
}

function numericChk(obj){
//-----------------------------------------------------------------------------
// Function Name:	numericChk()
//
// Functionality:	Validating the correct format of numeric values from
//					the input text. By using isNaN() function that will return
//					false if the sting input is a number and vise versa.
//
// Input		:	The formObj's element index in order to get the field's value.
//
// Output		:	True will be returned to the textChk() if a numeric value
//					found or else false will be returned.
//-----------------------------------------------------------------------------
	var numStr = "";					// initializes numeric string
	
	// if the period is presented
	if(obj.value.indexOf(".") != -1){
		// finds if there is more than one period
		var periodArr = obj.value.split(".");
		if((periodArr.length > 2) || (periodArr[1].length == 0))
			return false;
		else{ // period is 1
			// if there are commas behides the period
			if(periodArr[1].indexOf(",") != -1)
				return false;
			// if there are commas present before the period
			if(periodArr[0].indexOf(",") != -1){
				// finds the commas' dimention against the numeric values
				var commaArr = periodArr[0].split(",");
				if((commaArr[0].length > 3) || (commaArr[0].length == 0))
					return false;
				if(commaArr.length > 1){
					for(var i=1; i<commaArr.length; i++){
						if(commaArr[i].length != 3)
							return false;
					}
				}
				for(var i=0; i<commaArr.length; i++)
					numStr+=commaArr[i];
				numStr+="." + periodArr[1];
			}
			else{
				if(periodArr[1].indexOf(",") != -1) // if comma comes after the period
					return false;
				numStr+= periodArr[0] + "." + periodArr[1];
			}
		}
	}
	else
		numStr+=obj.value;
		
	// evaluates the newly constructed numeric string
	if(!isNaN(numStr))
		return true;
	else
		return false;
}

function dateChk(frmsIndex, obj){
//-----------------------------------------------------------------------------
// Function Name:	dateChk()
//
// Functionality:	Handles the string date format by using two more functions
//					to help validating the date; dateBreaker() and formatting().
//
// Input		:	The frms's index and the formObj's element index are
//					required in order to match the checking pair of date.
//
// Output		:	If the date format is correct, true will be returned back to
//					the textChk() and vice versa
//-----------------------------------------------------------------------------
	var theDate = obj.value;
	var notAllowStr = ' /-+';
	var dateArr;
	var formation;
	var toDate;
	var dateStr;
	var dateIdent;
	var str;

	// finding formation that programmer defined
	var tmp = frms[formPos].fields[frmsIndex].dataType;
	var tmpArr = tmp.split(":");
	formation = tmpArr[1];
	
	// calling the dateBreaker() function to break down user's input into array
	dateArr = dateBreaker(theDate);

	// checking to find the illegal date format from the returned date array
	if((dateArr == -1) || (dateArr.length != 3) || (dateArr[0]=='') || (dateArr[dateArr.length-1]==''))
		return false;
	
	// calling the formatting() function by the formation specified, a string of
	// date is send back
	switch(formation){
		case 'd-m-y' :	dateStr = formatting(dateArr[1], dateArr[1], dateArr[0], dateArr[2]);
						dateIdent = dateArr[0];
						break;
		case 'm-d-y' :	dateStr = formatting(dateArr[0], dateArr[0], dateArr[1], dateArr[2]);
						dateIdent = dateArr[1];
						break;
		case 'y-m-d' :	dateStr = formatting(dateArr[1], dateArr[1], dateArr[2], dateArr[0]);
						dateIdent = dateArr[2];
						break;
		default		 :	alert('Please check if the defined date format allowed.');
						return false;
	}
	
	// creating the date object by passing the dateStr into it
	var dateObj = Date.parse(dateStr);
	
	// checking if the date object is successfully created, if not, return false
	if(dateObj.toString() == "NaN"){
		return false;
	}
	else {
		toDate = new Date(dateStr);
	}
		
	// scaning through the elements in the array for not allowed strings
	for(j=0; j<dateArr.length; j++){
		for(k=0; k<dateArr[j].length; k++){
			subArray = dateArr[j].substring(k, k+1);
			if(notAllowStr.indexOf(subArray) != -1)
				return false;
		}
	}
	
	// getting a serie of date string from the successfully setup date object
	str = toDate.toString();
	
	// spliting the str into array using a blank space as a separator
	dateGen = str.split(' ');
	
	// comparing generated date to the one by the user to find the correct day
	// per month
	if(eval(dateGen[2]) != eval(dateIdent))
		return false;
	
	// simplifying the month name input by the user by setting it to be in 
	// lower case
	obj.value = obj.value.toLowerCase();
	
	// if everything is corrected, return true to the textChk()
	return true;
}
function formatting(month, elem1, elem2, elem3){
//-----------------------------------------------------------------------------
// Function Name:	formatting()
//
// Functionality:	Reforming the date elements for the acceptable JavaScript's
//					date object. The convention are blank spaces for month by
//					name and '/' for month by digit.
//
// Input		:	4 parameters are needed; month for month by name or by digit
//					checking and elem1-elem3 are the order of suitable date
//					format, which will be composed to a single string.
//
// Output		:	A complete string of the suitable date or an empty string
//					if the month is fail to be determined its value.
//-----------------------------------------------------------------------------
	var dateStr = ' ';
	
	if(elem3.length!=4)
		return dateStr;
	
	// checking for the month by name
	if(isNaN(month)){
		
		// if the month by name has less than 3 characters
		if(month.length < 3)
			return dateStr;
			
		// insert blank spaces as the date separators to the dateStr
		dateStr = elem1+' '+elem2+' '+elem3;
	}
	else{
	
		// if the month by digit has a proper format
		if((month>12) || (month<1) || (month.length > 2) || (elem3.length != 4))
			return dateStr;
		
		// insert '/' as the date separators to the dateStr
		dateStr = elem1+'/'+elem2+'/'+elem3;
	}
	
	// return the dateStr back to the dateChk()
	return dateStr;
}

function dateBreaker(dateIn){
//-----------------------------------------------------------------------------
// Function Name:	dateBreaker()
//
// Functionality:	Splitting the incomming date string into date elements by
//					examining the separator as '/', '-' and a blank space.
//
// Input		:	A sting of input date is passed from the dateChk().
//
// Output		:	An array of string contains the date elements or -1 value
//					if this function fail to break down the date string.
//-----------------------------------------------------------------------------
	var dateArr;
	var separator = ' /-';
	var subSep;
	var isSeparator = false;

	// Searching for the standard separators
	for(j=0; j<separator.length; j++){
		subSep = separator.substring(j, j+1);
		if(dateIn.indexOf(subSep) != -1){
			dateArr = dateIn.split(subSep);
			isSeparator = true;
			break;
		}
		else
			isSeparator = false;
	}
	
	// If the standard separators are not found
	if(!isSeparator)
		return -1;
		
	// The standard separators are found and the date string is returned
	return dateArr;
}

function emailChk(obj){
//-----------------------------------------------------------------------------
// Function Name:	emailChk()
//
// Functionality:	Validating email address format by allowing some of separators
//					to be in the string if input.
//
// Input		:	Form elements' index is needed to find the correct field
//					that contains email address.
//
// output		:	This function returns FALSE value if illegal expression is
//					detected in the email. TRUE value will be returned if there
//					is no error presented.
//-----------------------------------------------------------------------------
	var emailAdd = obj.value;
	var emailExp = /[^\w\.@\_-]/;		// Regular expression of legal email format
	var frontAtom = /^[\._\-]/;	// RE, finds '.', '-' and '_' at the begining of string 
	var rareAtom = /[\._\-]$/;		// RE, finds '.', '-' and '_' at the ending of string
	var emailAd = /@/;				// RE, finds '@' in the string
	var underStream = /_{2,}/;		// RE, limits '_' to appear only once on it own
	var hyphenStream = /\-{2,}/;	// RE, limits '-' to appear only once on it own
	var dotStream = /\.{2,}/;		// RE, limits '.' to appear only once on it own
	var hyphenSpec = /\-/;			// RE, finds '-' in the string
	var dotSpec = /\./;			// RE, finds '.' in the string
	var addArr;						// An array keeps email address that split by '@'
	var dotArr;						// An array keeps email address that split by '.'
	var hyphenArr;					// An array keeps email address that split by '-'

	// checking for alllowed expression of email address
	if(emailExp.test(emailAdd))
		return false;

	// checking that the email address has only one '@'
	if(!emailAd.test(emailAdd))
		return false;
	if((underStream.test(emailAdd)) || (hyphenStream.test(emailAdd)) || (dotStream.test(emailAdd)))
		return false;
	
	// checking that the split '@' array is not empty
	addArr = emailAdd.split('@');
	if((addArr.length>2) || (addArr.length<1) || (addArr[0] == '') || (addArr[1] == ''))
		return false; 
	
	// looking for extra special separators in the sender part
	if((frontAtom.test(addArr[0])) || (rareAtom.test(addArr[0])))
		return false;
	
	// looking for extra special separators in the address part
	if((frontAtom.test(addArr[1])) || (rareAtom.test(addArr[1])))
		return false;
	
	// looking for at least a dot separator in the address part
	if(!dotSpec.test(addArr[1]))
		return false;
	
	// finding extra special separator by '.'
	dotArr = emailAdd.split('.');
	for(j=0; j<dotArr.length; j++){
		if((frontAtom.test(dotArr[j])) || (rareAtom.test(dotArr[j])))
			return false;
	}
	
	// finding extra special separator by '-'
	if(hyphenSpec.test(emailAdd)){
		hyphenArr = emailAdd.split('-');
		for(j=0; j<hyphenArr.length; j++){
			if((frontAtom.test(hyphenArr[j])) || (rareAtom.test(hyphenArr[j])))
				return false;
		}
	}
	
	// if there is nothing wrong, return true
	return true;
}

function CompareChk(obj,val){
	var ObjVal = obj.value;
	var Objlen = eval(val);
	
	result = true;
	if (ObjVal!=Objlen.value)
		result = false;
	return result;
}

function lengthNONumChk(obj,val){
	var ObjVal = obj.value;
	var Objlen = ObjVal.length;
	var len = val;
	
	result = true;
	if (Objlen>len) 
		result = false;
	if(!isNaN(obj.value))
		result = false;
	for(var i=0; i<Objlen; i++){
		j = i+1;
		Chr = ObjVal.substring(i,j)
		//if ((Chr=="%")||(Chr=='"')) {
		if ((Chr=='"')||(Chr=='0')||(Chr=='1')||(Chr=='2')||(Chr=='3')||(Chr=='4')||(Chr=='5')||(Chr=='6')||(Chr=='7')||(Chr=='8')||(Chr=='9')) {
			result = false;
			break;
		}
	}
	return result;
}

function lengthNOSPChk(obj,val){
	var ObjVal = obj.value;
	var Objlen = ObjVal.length;
	var len = val;
	
	result = true;
	if (Objlen>len) 
		result = false;
	//if(!isNaN(obj.value))
	//	result = false;
	for(var i=0; i<Objlen; i++){
		j = i+1;
		Chr = ObjVal.substring(i,j)
		//if ((Chr=="%")||(Chr=='"')) {
		if (Chr=='"') {
			result = false;
			break;
		}
	}
	return result;
}


/*
function lengthNATChk(obj,val){
	var ObjVal = obj.value;
	var Objlen = ObjVal.length;
	var len = val;
	var Exp = /\w/;
	var Exp2 = /[()0-9]/;
	
	result = true;
	flag = 0;
	if (Objlen>len) 
		result = false;
	
	for(var i=0; i<ObjVal.length; i++){
		j = i+1;
		Chr = ObjVal.substring(i,j)
		if (((Exp.test(Chr)==false)||(Chr=='"'))&&(Chr!='.')&&(Chr!='-')) {
			result = false;
			break;
		}
		if (Exp2.test(Chr)==true) {
			flag = 1;
		}
	}
	
	if (flag==0) result = false;
	return result;
}
*/


function lengthNATChk(obj,val){
	var ObjVal = obj.value;
	var Objlen = ObjVal.length;
	var len = val;
	var Exp = /[0-9]/;
	
	flagNum = 0
	
	result = true;
	if (Objlen>len) 
		result = false;
	//if(!isNaN(obj.value))
	//	result = false;
	for(var i=0; i<Objlen; i++){
		j = i+1;
		Chr = ObjVal.substring(i,j)
		//if ((Chr=="%")||(Chr=='"')) {
		if (Chr=='"') {
			result = false;
			break;
		}
		if (Exp.test(Chr)==true) {
			flagNum = 1;
		}
	}
	
	if (flagNum==0) result = false;
	return result;
}


function lengthMixedChk(obj,val){
	var ObjVal = obj.value;
	var Objlen = ObjVal.length;
	var len = val;
	var Exp = /[,\w]/;
	
	result = true;
	if (Objlen>len) 
		result = false;
	
	for(var i=0; i<ObjVal.length; i++){
		j = i+1;
		Chr = ObjVal.substring(i,j)
		if ((Exp.test(Chr)==false)||(Chr=='"')) {
			result = false;
			break;
		}
	}
	return result;
}

function lengthIDChk(obj,val){
	var ObjVal = obj.value;
	var Objlen = ObjVal.length;
	var len = val;
	var Exp = /[-\w]/;
	var Exp2 = /[0-9]/;
	
	flag = 0;
	result = true;
	if (Objlen>len) 
		result = false;
	
	for(var i=0; i<ObjVal.length; i++){
		j = i+1;
		Chr = ObjVal.substring(i,j)
		if ((Exp.test(Chr)==false)||(Chr=='"')) {
			result = false;
			break;
		}
		if (Exp2.test(Chr)==true) {
			flag = 1;
		}
	}
	if (flag==0) result = false;
	return result;
}

function lengthChk(obj,val){
	var ObjVal = obj.value;
	var Objlen = ObjVal.length;
	var len = val;
	
	result = true;
	if (Objlen>len) 
		result = false;
	//if(!isNaN(obj.value))
	//	result = false;
	
	for(var i=0; i<Objlen; i++){
		j = i+1;
		Chr = ObjVal.substring(i,j)
		if (Chr=='"') {
			result = false;
			break;
		}
	}
	return result;
}
function PhoneChk(obj){
	var PhoneAdd = obj.value;
	var FirstChr = PhoneAdd.substring(0,1);
	var PhoneExp = /[0-9]/;		
	
	if(!PhoneExp.test(FirstChr))
		return false;
	
	// if there is nothing wrong, return true
	return true;
}
function PosChk(obj){
	var PosIntAdd = obj.value;
	var Exp = /[.0-9]/;
	var result;
	var Chr;
	var j;
	var ZeroVal;
	
	ZeroVal = true;
	result = true;
	for(var i=0; i<PosIntAdd.length; i++){
		j = i+1;
		Chr = PosIntAdd.substring(i,j)
		//if ((Chr!="0")&&(Chr!=".")) 
		if (Chr!=".")
			ZeroVal = false;
		if (Exp.test(Chr)==false) result = false;
	}
	FloatVal = parseFloat(PosIntAdd);
	if (ZeroVal==true) result = false;
	//if (FloatVal<0.005) result = false;
	if (FloatVal<0) result = false;
	return result;
}
function TimeChk(obj){
	var TimeAdd = obj.value;
	var Exp = /[:0-9]/;
	var result;
	var Chr;
	var j;
	var ZeroVal;
	
	ZeroVal = true;
	result = true;
	ChkCol = 0;
	for(var i=0; i<TimeAdd.length; i++){
		j = i+1;
		Chr = TimeAdd.substring(i,j)
		if ((Chr!="0")&&(Chr!=".")) 
			ZeroVal = false;
		if (Chr==":")
			ChkCol = ChkCol+1;	
		if (Exp.test(Chr)==false) result = false;
	}
	if (ZeroVal==true) result = false;
	if (ChkCol==0) {
		HourSpend = parseInt(TimeAdd);
		if ((HourSpend<0)||(HourSpend>23)) result = false;
	}
	if (ChkCol==1) {
		PosC = TimeAdd.indexOf(':')
		StrHour = TimeAdd.substring(0,PosC);
		PosC2 = PosC+1;
		StrMN = TimeAdd.substring(PosC2,TimeAdd.length);
		HourSpend = parseInt(StrHour);
		MNSpend = parseInt(StrMN);
		if ((HourSpend<0)||(HourSpend>23)) result = false;
		if ((MNSpend<0)||(MNSpend>59)) result = false;
	}
	if (ChkCol>1)  result = false;
	if (TimeAdd.indexOf(':')==-1)result = false;
	
	return result;
}

function PosIntChk(obj) {
	var PosIntAdd = obj.value;
	var Exp = /[0-9]/;
	var result;
	var Chr;
	var j;
	
	result = true;
	for(var i=0; i<PosIntAdd.length; i++){
		j = i+1;
		Chr = PosIntAdd.substring(i,j)
		if (Exp.test(Chr)==false) result = false;
	}
	IntVal = parseInt(PosIntAdd);
	if (IntVal<0) result = false;
	//if (IntVal<1) result = false;
	return result;
}
function TextOnlyChk(obj) {
	var TextAdd = obj.value;
	var Exp = /[A-Za-z]/;
	var result;
	var j;
	var Chr;
	
	result = true;
	for(var i=0; i<TextAdd.length; i++){
		j = i+1;
		Chr = TextAdd.substring(i,j)
		if (Exp.test(Chr)==false) result = false;
	}
	return result;
}
function MixedChk(obj) {
	var MixedAdd = obj.value;
	var Exp = /\w/;
	var result;
	var j;
	var Chr;
	
	result = true;
	for(var i=0; i<MixedAdd.length; i++){
		j = i+1;
		Chr = MixedAdd.substring(i,j)
		if (Exp.test(Chr)==false) result = false;
	}
	return result;
}

function SpaceExpChk(obj) {
	var strSpaceExp = /^\s+$/;
	var result;
		
	result = true;
	if ((obj.value == "" )|| (strSpaceExp.test(obj.value))) {
		result = false;
	}
	return result;
}



function textareaChk(obj){
//-----------------------------------------------------------------------------
// Function Name:	textareaChk()
//
// Functionality:	Validating the textarea field that it does not have a blank
//					space.
//
// Input		:	The fieldHolder's index and the formObj element's index are
//					needed to match the validating pair.
//
// Output		:	The true value will be returned if the field has an
//					alphanumeric value otherwise false will be returned.
//
// Example		:	addValidation("form1", "text1", 1, "Null", "Null", "Error!! this field is needed");
//					- The first item is the form name.
//					- The second item is the field name.
//					- The third item is the indicated if the field is needed to be
//						evaluated its value(1) or just check if the field is not empty only(0).
//					- The fourth field is "Null" value.
//					- The fifth item is the conditional of the field.
//					- The sixth item is the programmer's error message.
//-----------------------------------------------------------------------------
	if(obj.value == '')
		return false;
	else
		return true;
}

function selectChk(frmsIndex, obj){
//-----------------------------------------------------------------------------
// Function Name:	selectChk()
//
// Functionality:	Classifying the select characteristics; 'select-one' or
//					'select-multiple' and searching for the selected options.
//					An option that is not allowed to be selected will be
//					checked here.
//
// Input		:	The fieldHolder's index and the formObj element's index are
//					required.
//
// Output		:	True value will be returned if one of the select's option
//					is selected and the non-permit option is not selected
//					otherwise false will be returned.
//
// Example		:	addValidation('form1', 'select1', 0, "Null", "Null", 'Error!! this field is needed');
//					- The first item is the form name.
//					- The second item is the field name.
//					- The third item is the indicated the option that can not be selected.
//						the first option(0) cannot be selected or "Null" represents any options.
//					- The fourth field is "Null" value.
//					- The fifth field is the field's condition.
//					- The sixth item is the programmer's error message.
//-----------------------------------------------------------------------------
	var isValid = false;
	
	if(frms[formPos].fields[frmsIndex].opts == "Null"){
		for(var i=0; i<obj.options.length; i++){
			if(obj.options[i].selected){
				isValid = true;
				break;
			}
		}
	}
	else{
		for(var i=0; i<obj.options.length; i++){
			if(obj.options[i].selected){
				if(frms[formPos].fields[frmsIndex].opts == i){
					isValid = false;
					break;
				}
				else
					isValid = true;
			}
		}
	}
	return isValid;
}

function radioChk(frmsIndex, obj){
//-----------------------------------------------------------------------------
// Function Name:	radioChk()
//
// Functionality:	Searching a selected radio button in its own group.
//
// Input		:	The frms array index and the formObj's element index
//					are needed.
//
// Output		:	True value will be returned if one of the radio button is
//					checked and false will be returned if non of the radio group
//					members is checked.
//
// Example		:	addValidation('form1', 'radio1', "Null", "Null", "Null", 'Error!! this field is needed');
//					- The first item is the form name.
//					- The second item is the field name.
//					- The third item is the "Null".
//					- The fourth field is "Null" value.
//					- The fifth field is "Null" value.
//					- The sixth item is the programmer's error message.
//-----------------------------------------------------------------------------
	var radioName = frms[formPos].fields[frmsIndex].name;
	var isValid = false;
	var singleRadio = "";
	
	singleRadio += obj.length;
	if(singleRadio.toLowerCase() == "undefined"){
		if(obj.checked)
			isValid = true;
	}
	else{
		for(var i=0; i<obj.length; i++)
			if((obj[i].name == radioName) && (obj[i].checked)){
				isValid = true;
		}
	}
	return isValid;
}

function checkBoxChk(obj){
//-----------------------------------------------------------------------------
// Function Name:	checkBoxChk()
//
// Functionality:	Searching a checked checkbox.
//
// Input		:	The formObj's element is needed.
//
// Output		:	True value will be returned if the checkBox is checked and
//					false will be returned if the checkBox is not checked.
//
// Example		:	addValidation('form1', 'checkBox', "Null", "Null", "Null", 'Error!! this field is needed');
//					- The first item is the form name.
//					- The second item is the field name.
//					- The third item is the "Null".
//					- The fourth field is "Null" value.
//					- The fifth field is "Null" value.
//					- The sixth item is the programmer's error message.
//-----------------------------------------------------------------------------
	if(obj.checked)
		return true;
	else
		return false;
}

function passwordChk(obj){
//-----------------------------------------------------------------------------
// Function Name:	passwordChk()
//
// Functionality:	Verifying the password field for any value.
//
// Input		:	The formObj's element is needed.
//
// Output		:	True value will be returned if the password field is dectected any value
//					and will return false if the field is empty.
//
// Example		:	addValidation('form1', 'password', "Null", "Null", "Null", 'Error!! this field is needed');
//					- The first item is the form name.
//					- The second item is the field name.
//					- The third item is the "Null".
//					- The fourth field is "Null" value.
//					- The fifth field is "Null" value.
//					- The sixth item is the programmer's error message.
//-----------------------------------------------------------------------------
	if(obj.value.length != 0)
		return true;
	else
		return false;
}

function validate(obj){
//-----------------------------------------------------------------------------
// Function Name:	validate()
//
// Functionality:	Starting the field validating process by calling the
//					recordVerification() to start validating the registered field against
//					the submitted form object. It also calls the validateConditionFields()
//					to starts validation process of the registered fields.
//
// Input		:	The form object from the HTML page.
//
// Output		:	The true value will be sent back to the HTML page to
//					continue processing the ACTION action in the FORM tag. If
//					false is presented, it will be sent back to the HTML page
//					and will cause no actions on the AcTION in the FORM tag.
//
// Example		:	return validation(this)
//					- The return indicates that there is a value to be returned by
//						this validate() function, in this case, true or false.
//					- The this represents the form object, in the onSubmit event.
//-----------------------------------------------------------------------------
	var isItemExists = recordVerification(obj.name);
	vwidth = 0;
	vheight = 15; 
	
	// there is no such a field in the form
	if(isItemExists == "noField")
		return false;
	else{
		// there is no addValidation element of the form
		if(isItemExists == "noForm")
			return true;
		else{
			// the forma and field are found
			var isValid = validateConditionFields();
			return isValid;
		}			
	}
}


//</Script>