﻿/*
====================================================================================
Ticket #9978 Ray 4/17/2008
	- A general validate form metohd.
====================================================================================
Notes:
	- Properties
		- IsReqired, Boolean 0 or 1. check it is required or not.
		- IsContact, Boolean 0 or 1. 1 = Yes, 0 = No. Contact 
		- DataLength, integer . 0 or '' or null => No limit.
		- DisplayName, String. Display name for user.
		- FieldName, String. For txtKeys.
		- ValidateType, String,if null then don't validate it . Depeond on `is_string_validate`
	- Return value
		- Success, return true
		- Fail, return false ( get message from objForm.arrMSG)
	- Others Action
		-	Set txtKeys and txtValues properties into current form.
====================================================================================
Sample
	JS:
		function fnc_Sample_validate(objForm){
			var booReturn = get_general_form_validate(objForm);
			var arrMSG = new Array();
			var txtKeys = objForm.txtKeys;
			var txtValues = objForm.Values;

			if(booReturn == false){
				arrMSG = objForm.arrMSG;
				alert(arrMSG);
			}
			
			
			return false;
		}
	HTML:

		<Form action="" method="" onSubmit="return fnc_Sample_validate(this)">
			<input type="input" IsRequired="1" DisplayName="input1" ValidateType="int" IsContact=1 FieldName="input1" DataLength="5" name="input1" id="input1" value=""/><br/>
			<input type="checkbox" IsRequired="1" DisplayName="Checkbox1"  IsContact=1 FieldName="checkbox1" id="checkbox1" name="checkbox1" value="12345" ><br/>

			<input type="radio" IsRequired="1" DisplayName="Radio1" IsContact=1 FieldName="Radio1" name="Radio1" value="1" checked>
			<input type="radio" IsRequired="1" DisplayName="Radio1" IsContact=1 FieldName="Radio1" name="Radio1" value="2" ><br/>

			<select name="select1" id="select1" IsRequired="1" DisplayName="Select1" IsContact=1 FieldName="Select1"><br/>
				<option value="">--try--</option>
				<option value="111">111</option>
				<option value="222">222</option>
				<option value="333">333</option>
			</select><br/>
			<textarea isRequired="0" DisplayName="Textarea1" IsContact=0 FieldName="textarea1" name="textarea1" id="textarea1" value=""></textarea>
			
			<input type="submit"/><br/>
		</Form>
====================================================================================
*/

function get_general_form_validate(objForm){

	var arrTags = new Array('input','textarea','select');
	var arrMSG = new Array;
	var arrKeys = new Array;
	var arrValues = new Array;
	var arrElements = '';
	var strKeyDelim = '\r\n';
	//Properties
	var IsRequired ='';
	var IsAllowEmpty = '';
	var IsContact ='';

	var FieldName = ''
	var DisplayName = '';
	var DatatLength = '';
	var ValidateType = '';
	
	var objThis = '';
	// ------------------------------------------------------------------
	// Not includeing 
	//		- Radio
	// Including
	//		- input (text), checkbox, select , textarea
	for(key in arrTags){
		var arrElements = objForm.getElementsByTagName(arrTags[key]);
		
		for(var i=0;i<arrElements.length;i++){
			
			objThis = arrElements[i]
			
			if(objThis){
				
				IsRequired = objThis.getAttribute('IsRequired');

				IsContact = objThis.getAttribute('IsContact');

				FieldName = objThis.getAttribute('FieldName');
				DisplayName = objThis.getAttribute('DisplayName');
				DataLength = objThis.getAttribute('DataLength');
				ValidateType = objThis.getAttribute('ValidateType');
				
				// --------------------------------------------
				
				if(arrTags[key]=="input" && (objThis.type=="checkbox" || objThis.type=="radio")){
					
					
					if(IsContact ==1 ){
						
						//Checkbox
						if(objThis.type =="checkbox"){
							arrKeys.push(FieldName);
							if(objThis.checked){
								arrValues.push(objThis.value);
							}else{
								arrValues.push("");
							}
						//Checkbox
						}else if(objThis.type =="radio"){ 
							
							if(objThis.checked == true){
								arrKeys.push(FieldName);
								arrValues.push(objThis.value);
							}
						}
					}
				}else{
					//Default
					//	- for input(text),
					//	- textarea
					//	- select
					if(IsRequired ==1 ){
						if(!objThis.value){
							arrMSG.push(DisplayName+' is required.');
						}
					}
					// ---------------------------------------------
					if(DataLength != null && DataLength !=0){
						if(objThis.value.length >DataLength){
							arrMSG.push(DisplayName+' is over size.');
						}
					}
					// ---------------------------------------------
					if(ValidateType != null){
						if(!is_string_validate(objThis.value,ValidateType)){
							arrMSG.push(DisplayName+' is invalid.');
						}
					}
					// ---------------------------------------------
					if(IsContact == 1){
						arrKeys.push(FieldName);
						arrValues.push(objThis.value);
					}
				}
			}
		}
	}
	// ----------------------------------
	objForm.txtKeys = arrKeys.join(strKeyDelim)
	objForm.txtValues = arrValues.join(strKeyDelim)
	// ----------------------------------
	if(arrMSG.length == 0){
		//Success
		return true;
	}else{
		//Fail , return false
		//	 - append message text in objForm.arrMSG
		objForm.arrMSG = arrMSG;
		return false;
	}
}
