//<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.												*
*										Revision  14
*						Author: Tharin Sittitummacharee
*						  Supervisor : Mark Able					*
*							Rev. Date : Oct 1 , 2001							*
*										Revision  15
*								version 1.5
*						Author: Tharin Sittitummacharee
*						  Supervisor : Mark Able					*
*							Rev. Date : Nov 5 , 2001							*
********************************************************************************/

// Global variable definitions

var fieldErrMsg = '';
var fieldErrMsgCount = 1;
var vwidth;
var vheight; 
var vsbar; // jil
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(){
	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){
	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){
	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,f7){
	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
	// jil
	if (f7 == 0)
		vsbar = 0;
	else
		vsbar = 1;	
	// jil
	// 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){
	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(){
	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);
			//alert(frms[formPos].fields[i].name);
		}
		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
		vwidth = 310;
		
		if(!isValid){
			// generates the error messages
			
			fieldErrMsg += fieldErrMsgCount++ + ") " + frms[formPos].fields[i].msg + "<p>";
			//alert(fieldErrMsg);
			vheight += 50;
			// focusing on the first error field
			if(!toFocus){
				var obj = eval("document." + frms[formPos].name + "." + frms[formPos].fields[i].name);
				//alert(obj.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 != ''){
	var msg = fieldErrMsg;
	var frmName1 = frms[formPos].name;
	
	var frmtemp = eval('document.' + frms[formPos].name + '.' + 'hidMsg');
//	document.theForm.hidMsg.value = msg;
	frmtemp.value = msg;
	vheight = vheight + 140;
	if (vsbar == 0){
//	    window.open( "../_ScriptLibrary/AlertTh.asp?frmName="+frmName1, '_blank',  "height="+vheight.toString()+",width="+vwidth.toString()+",status=no,toolbar=no,menubar=no,location=no,scrollbars=yes");  }
	    window.open( "../_ScriptLibrary/AlertTh.asp?frmName="+frmName1, '_blank',  "height="+vheight.toString()+",width=350"+",status=no,toolbar=no,menubar=no,location=no,scrollbars=yes");  }
	else{
	    window.open( "../_ScriptLibrary/AlertTh.asp?frmName="+frmName1, '_blank',  "height=400" +",width="+vwidth.toString()+",status=no,toolbar=no,menubar=no,location=no,scrollbars=yes");  }
	return false;
	}
	else
		return true;
}


function fieldValidation(index){
	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 = 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 = textChk(index, obj);
		case "file"					:	isValid = textChk(index, obj);
	}
	return isValid;
}
function textChk(frmsIndex, obj){
	var isValid = false;
	var lenval;
	if ((frms[formPos].fields[frmsIndex].opts ==2)) 
	{
		if(obj.value.length != 0)
		{
			//lenval=frms[formPos].fields[frmsIndex].dataType;
			//							isValid=lengthChk(obj.value,lenval);
			
			switch(frms[formPos].fields[frmsIndex].dataType.substring(0,6).toLowerCase()){
				//'length'			:	Accept	->	Character + Numeric or Character only and length <= lenval
				//					:	Reject	->	Numeric only or length > lenval
				case 'length'		:	lenval=frms[formPos].fields[frmsIndex].dataType;
										isValid=lengthChk(obj.value,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.value,lenval);
			//							break;
				//'lengthID'		:	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.value,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.value,lenval);
			//							break;
			//	case 'compare'		:	lenval=frms[formPos].fields[frmsIndex].opts;
			//							isValid=CompareChk(obj,lenval);
			//							break;
				default				:	alert("Internal Error Message: \nThere is no datatype: '" + frms[formPos].fields[frmsIndex].dataType + "'.");
			}
		}
		else
		{
			isValid = true;
		}
	} 
	else 
	{
		if(frms[formPos].fields[frmsIndex].opts == 1)
		{
			if(obj.value.length != 0)
			{
				switch(frms[formPos].fields[frmsIndex].dataType.toLowerCase())
				{	
					case 'alphabetth'		:	isValid=isAlphabeticTh(obj.value);
													break;
					//'alphabet'		:	Accept	->	Character + Numeric or Character only
					//					:	Reject	->	Numeric only
					case 'alphanumericth'		:	
											isValid=isAlphaNumericTh(obj.value);
													break;
					case 'alphanumericthq'		:	isValid=isAlphaNumericThQ(obj.value);
													break;
					case 'alphanumericthorblank'		:	isValid=isAlphaNumericThOrBlank(obj.value);
												break;
					case 'alphabet'		:	isValid=isAlphabetic(obj.value);
													break;
				
					//'alphabet'		:	Accept	->	Character + Numeric or Character only
					//					:	Reject	->	Numeric only
					case 'alphanumeric'		:	isValid=isAlphaNumeric(obj.value);
													break;
					//'numeric'			:	Accept	->	Numeric only (can be float and negative number)
					//						Reject	->	Character
					case 'number'		:	isValid=isNumber(obj.value);
													break;
					//'date:d-m-y'		:	Accept	->	Date 
					case 'date:d-m-y'	:
					case 'date:m-d-y'	:	isValid=isDate2(frms[formPos].fields[frmsIndex].dataType, obj.value);
													break;
					case 'date:y-m-d'	:	isValid=isDate2(frms[formPos].fields[frmsIndex].dataType, obj.value);
													break;
					//'email'			:	Accept	->	Email format
					case 'email'		:	isValid=isEmail(obj.value);
												break;						
					case 'emailorblank'		:	isValid=isEmailOrBlank(obj.value);
												break;					
					//'phone'			:	Accept	->	Character + Numeric or Number only
					//						Reject	->	Character only
					case 'phone'		:	isValid=isPhone(obj.value);
												break;						
					case 'phoneorblank'		:	isValid=isPhoneOrBlank(obj.value);
												break;						
						
					//'positiveint'		:	Accept	->	Numeric only (must be positive integer)
					//					:	Reject	->	Negative number and decimal
					case 'posinteger'	:	isValid=isPosInteger(obj.value);
												break;		
					case 'posintegerorblank'	:	isValid=isPosIntegerBlank(obj.value);
												break;
					//'positivenum'		:	Accept	->	Numeric only and '.' (must be positive), can be float or integer
					//					:	Reject	->	Negative number
					case 'posnumber'	:	isValid=isPosNumber(obj.value);					
													break;				
					//'timer'			:	Accept	->	Numeric and ':', '.' (must be positive), can be float or integer
					//					:	Reject	->	Negative number
					case 'time'		:	isValid=isTime(obj.value);					
												break;
					//'textonly'		:	Accept	->	Character only
					//					:	Reject	->	Number
					case 'textonly'		:	isValid=TextOnlyChk(obj.value);
													break;
					//'mixed'			:	Accept	->	Character only, Number only, Character+Number
					case 'mixed'		:	isValid=MixedChk(obj);
												break;
					case 'jpgfile'		:	isValid=JpgFileChk(obj.value);
												break;
					default				:	alert("Internal Error Message: \nThere is no datatype: '" + frms[formPos].fields[frmsIndex].dataType + "'.");
				}
			}
			else
			{
			switch(frms[formPos].fields[frmsIndex].dataType.toLowerCase())
				{	
					case 'alphabetth'		:	isValid=false;
													break;
					//'alphabet'		:	Accept	->	Character + Numeric or Character only
					//					:	Reject	->	Numeric only
					case 'alphanumericth'		:	isValid=false;
													break;
					case 'alphanumericthq'		:	isValid=false;
													break;
					case 'alphanumericthorblank'	:	isValid=isAlphaNumericThOrBlank(obj.value);
												break;
					case 'alphabet'		:	isValid=false;
													break;
				
					//'alphabet'		:	Accept	->	Character + Numeric or Character only
					//					:	Reject	->	Numeric only
					case 'alphanumeric'		:	isValid=false;
													break;
					//'numeric'			:	Accept	->	Numeric only (can be float and negative number)
					//						Reject	->	Character
					case 'number'		:	isValid=false;
													break;
					//'date:d-m-y'		:	Accept	->	Date 
					case 'date:d-m-y'	:
					case 'date:m-d-y'	:	
					case 'date:y-m-d'	:	isValid=false;
													break;
					//'email'			:	Accept	->	Email format
					case 'email'		:	isValid=false;
												break;			
					case 'emailorblank'		:	isValid=isEmailOrBlank(obj.value);
												break;
					case 'phone'		:	isValid=false;
												break;						
					case 'phoneorblank'		:	isValid=isPhoneOrBlank(obj.value);
												break;						
					//'positiveint'		:	Accept	->	Numeric only (must be positive integer)
					//					:	Reject	->	Negative number and decimal
					case 'posinteger'	:	isValid=false;
												break;		
					case 'posintegerorblank'	:	isValid=isPosIntegerBlank(obj.value);
												break;		

					//'positivenum'		:	Accept	->	Numeric only and '.' (must be positive), can be float or integer
					//					:	Reject	->	Negative number
					case 'posnumber'	:	isValid=false;					
													break;				
					//'timer'			:	Accept	->	Numeric and ':', '.' (must be positive), can be float or integer
					//					:	Reject	->	Negative number
					case 'time'		:	isValid=false;					
													break;			
					//'textonly'		:	Accept	->	Character only
					//					:	Reject	->	Number
					case 'textonly'		:	isValid=false;					
													break;			
					//'mixed'			:	Accept	->	Character only, Number only, Character+Number
					case 'mixed'		:	isValid=false;					
													break;			
					case 'jpgfile'		:	isValid=false;					
													break;			
					default				:	alert("Internal Error Message: \nThere is no datatype: '" + frms[formPos].fields[frmsIndex].dataType + "'.");
												break;			
				}
			}
		}
		else
		{
			if((frms[formPos].fields[frmsIndex].opts ==3)) 
			{
				lenval=frms[formPos].fields[frmsIndex].dataType;
				isValid = passwordChk(obj, lenval);
			}
				
			else 
			{
				if(frms[formPos].fields[frmsIndex].opts == 0)
				{
					if(obj.value.length != 0)
						isValid = true;
					else // jil add
						isValid = false;	// jil add
				}
				else
					alert("Internal Error Message: \nThe option of the field: '" + frms[formPos].fields[frmsIndex].name + "' should be only 1 or 0.");
			}
		}
	}
	return isValid;
}
function CompareChk(obj,val){
	var ObjVal = obj.value;
	var Objlen = eval(val);
	
	result = true;
	if (ObjVal!=Objlen.value)
		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 textareaChk(obj){

	if(obj.value == '')
		return false;
	else
		return true;
}
function selectChk(frmsIndex, obj){
	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){

	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){
	if(obj.checked)
		return true;
	else
		return false;
}
function passwordChk(obj, lenval){
	//alert( "document."+lenval+".value== '"+obj.value+"'");
	//alert(eval("document."+lenval+".value== '"+obj.value+"'"));
	if(eval("document."+lenval+".value== '"+obj.value+"'"))
		return true;
	else
		return false;
}
function validate(obj){

	var isItemExists = recordVerification(obj.name);
	fieldErrMsg = '';
	fieldErrMsgCount = 1;
	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>