﻿//讀取 提供的規則驗證物件陣列進行驗證 並 表單( Form Object) 中的資料
function validateInputForm(objForm, objPolicyArray) {
	var elementsIndex;
	var objValidate = null;
	var InValidate_Flag = false;
	//讀取 驗證物件陣列資料並依序比較驗證資料格式
	for (elementsIndex=0 ; elementsIndex < objPolicyArray.length  ; elementsIndex++){
			//一般驗證物件取得需驗證的表單物件 並利用區域變數來進行操作增加效率
		objValidate = objForm.elements[objPolicyArray[elementsIndex].name];
		if (objValidate != null)
		{//呼叫驗證物件提供的驗證函式
			//Button, Checkbox, FileUpload, Hidden, Password, Radio, Reset, Select, Submit, Text, or Textarea 
			switch (objValidate.type) {
			    case "hidden":
				case "button":
				case "text":
					if ( objPolicyArray[elementsIndex].policy.checkPolicy(objValidate.value) == false)
					{
						InValidate_Flag = true;
						//objValidate.focus();
						//return false;
					}
					break;
				default:				
					if ( objPolicyArray[elementsIndex].policy.checkPolicy(objValidate) == false)
						InValidate_Flag = true;
					break;
					
			}
			objValidate = null;
		}	
	}
	if ( InValidate_Flag == false) 
		return true;
	else
		return false;	
}

//表單資料值規則驗證物件
function PolicyChecker(name, objcaption ,policy)
{
	this.name = name;
	this.objcaption = objcaption;
	this.policy = policy;
	this.getMessage = getPolicyCheckerMessage;
}
function getPolicyCheckerMessage()
{
	if (this.policy == null)
		return "需初始化Policy物件!\n"
	if (this.policy.message != "")
		return this.objcaption + this.policy.message;
	else
		return "";	
}
//驗證單一表單資料
//需要一個 常數驗證規則陣列 PolicyChecker_Array 變數
function validateSingleForm(objForm , Checker_Array) {	
	var ErrorMessage = "";
	var i = 0;	
	//alert("breakpoint 1, Form name=" + objForm.name);
	//alert("breakpoint 2, PolicyChecker_Array Length=" + Checker_Array.length);
	if ( validateInputForm(objForm, Checker_Array) == false )
	{
		for (i= 0; i <  Checker_Array.length ; i++) {
			ErrorMessage = ErrorMessage + Checker_Array[i].getMessage();
		}
		alert(ErrorMessage);
		return false;
	}
	return true;
}