/**
 *  Author: Robin Pan (htmlor [at] gmail.com)
 *
 *
 *  表单验证类
 *
 *  
 *
/*----------------------------------------------------------------*/


function Validator(eForm){
	var $form = eForm;
	var $names = [],
	$rules = {},
	$identical;
	var $errors = [];
	
	var ME = this;
	
	this.requireNames = function(aList){
		$names = aList;
	};
	this.setRules = function(oRules){
		$rules = oRules;
	};
	this.addRule = function(sName, vFormat, sMessage){
		
		$rules[sName] = {
			format: vFormat,
			message: sMessage
		};
		
		for(var i=0;i<$names.length;i++)
		{
			if($names[i] == sName)return true;
		}
		$names[$names.length] = sName;
		//alert($names);
	};
	this.delRule = function(sName){
		if($rules[sName])$rules[sName] = null ;
		for(var i=0;i<$names.length;i++)
		{
			if($names[i] == sName)$names[i] = null;
		}
		//alert($names);
	};
	
	this.keepIdentical = function(sName1, sName2){
		if($identical == null){
			$identical = {};
		}
		$identical.name1 = sName1;
		$identical.name2 = sName2;
	};
	
	this.init = function(){
		if($form == null){
			return;
		}
		$form.onsubmit = function(){
			return ME.validate();
		};
	};
	
	this.validate = function(){
		// 每次验证前先清空错误
		$errors = [];
		
		var i, ele, name, val, rule;
		for(i=0; name=$names[i]; i++){
			ele = $form.elements[name];
			
			highlightElement(ele, 0);
			
			val = getElementValue(ele);
			//window.alert(name + ": " + val);
			// 如有格式限制，就不检查是否为空了
			if(keyExists(name, $rules)){
				rule = $rules[name];
				if(!isMatched(val, rule.format)){
					$errors.push({
						ele: ele,
						message: rule.message
					});
				}
			}
			// 没有格式限制，才检查是否为空
			else{
				if(isEmpty(val)){
					$errors.push({
						ele: ele,
						message: getElementTitle(ele) + "不能为空"
					});
				}
			}
		}
		if($identical != null){
			var ele1 = $form.elements[$identical.name1],
			ele2 = $form.elements[$identical.name2];
			var val1 = getElementValue(ele1),
			val2 = getElementValue(ele2);
			
			if(val2 != val1){
				$errors.push({
					ele: ele2,
					message: "请确认两次输入的" + ele1.title + "一致！"
				});
			}
		}
		if($errors.length == 0){
			return true;
		}
		
		this.render();
		return false;
	};
	this.render = function(){
		var i, err;
		var messages = "请检查以下输入项：";
		for(i=0; err=$errors[i]; i++){
			highlightElement(err.ele);
			messages += "\n" + (i + 1) + "). " + err.message;
		}
		window.alert(messages);
	};
	
	function highlightElement(eTarget, iStatus){
		if(iStatus == null){
			iStatus = 1;
		}
		
		if(eTarget.nodeName){
			eTarget.style.background = (iStatus == 1 ? "#fc0" : "");
		}
		else{
			highlightElement(eTarget[0], iStatus);
		}
	}
	function isMatched(sValue, vFormat){
		if(typeof(sValue) != "string"){
			return true;
		}
		if(typeof(vFormat) == "string"){
			return isValidFormat(sValue, vFormat);
		}
		return vFormat.test(sValue);
		//var matches = sValue.match();
	}
}


