/**
------------------------validate.js------------------------
*/
function $(id){return typeof(id) === 'object' ? id:document.getElementById(id);}
function $C(tag){return typeof(tag) === 'object' ? tag:document.createElement(tag);}
function Trim(str){return str.replace(/(^\s*)|(\s*$)/g,"");}
var chk = {
	reg:{
		name:[/^[a-zA-Z]{1}[a-zA-Z0-9_\-]+$/, "只能由字母、数字以及(-_)组成，必须以字母开头."],
		str:[/^[0-9a-zA-Z_\u0391-\uFFE5]+$/, "只能由中文、字母、数字以及下划线组成."],
		msg:[/(^\s*)|(\s*$)/g, "不能为空"],
		mail:[/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/, "格式不正确."],		
		url:[/^(((ht|f)tp(s?))\:\/\/)[a-zA-Z0-9]+\.[a-zA-Z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/, "不是有效的URL地址."]
	},
	config:[], //chk.config = [{id:"obj.id", title:"tipTitle", arr:[chk.reg.item, minsize, maxsize, isnull, comregeID]}]
	form:function(fm){
		if (this.config && $(fm)){
			$(fm).onsubmit = function(){
				var msg = "", n = 1, str, tip, rst, arr ,reg;
				for (i in chk.config){
					str = Trim($(chk.config[i].id).value);
					if (str=="Name" || str=="Company" || str=="Company / Product Website" || str=="Your E-mail" || str=="TEL / Mobile" || str=="请您留言..."){
						str = "";
						}
					tip = chk.config[i].tip;
					arr = chk.config[i].arr;
					if (tip){
						if (!str){
							if (arr[3] === true) msg += "[" + (n++) + "]." + tip + "不能为空.\n";
						}else{
							reg = chk.reg[arr[0]];
							if (arr[1] || arr[2]){
								rst = chk.limit(str, arr[1], arr[2]);
								if (rst !== "yes") msg += "[" + (n++) + "]." + tip + rst + "\n";
							}
							if (arr[4] && str !== $(arr[4]).value) {msg += "[" + (n++) + "]." + tip + "与上次输入内容不同." + "\n"}
							if (reg && !reg[0].test(str)){msg += "[" + (n++) + "]." + tip + reg[1] + "\n";}
						}
					}
					
				}
				if (msg){msg = "以下原因导致提交失败：\n" + msg;}
				return chk.showErr("", msg);
			}
		}
	},
	input:function(){
		var msg, obj, arr, reg, k;
		for (i in chk.config){
			$(chk.config[i].id).setAttribute("key", i);
			$(chk.config[i].id).onblur = function(){
				k = chk.config[this.getAttribute("key")];
				if (k.tip){
					msg = "";
					obj = $(k.id);
					arr = k.arr;
					if (!obj.value){
						msg = (arr[3] === true) ? "不能为空!":"";
					}else{
						msg = "yes";
						reg = chk.reg[arr[0]];
						if (arr[1] || arr[2]) msg = chk.limit(obj.value, arr[1], arr[2]);
						if (msg === "yes" && arr[4] && obj.value !== $(arr[4]).value) msg = "与上次输入内容不同.";
						if (msg === "yes" && reg && !reg[0].test(obj.value)) msg = reg[1];
					}
					if (msg && msg !== "yes"){msg = k.tip + msg;}
					chk.showErr(obj, msg);
				}
				obj.style.backgroundColor = "";
			}
			$(chk.config[i].id).onfocus = function(){$(chk.config[this.getAttribute("key")].id).style.backgroundColor = "#EAFFCA";}
		}
	},
	limit:function(str, n, m){
		if (str){
			var len = str.length;
			return (n > 0 && len < n && "不能少于" + n + "个字符.") || (m > 0 && len > m && "不能超过" + m + "个字符.") || "yes";
		}
	},
	showErr:function(obj, errs){
		if (!obj){
			if (errs){
				alert(errs);
				return false;
			}
		}else{
			var tips = $("ck_" + obj.id);
			if(!tips){
				tips = $C("span");
				tips.setAttribute('id',"ck_" + obj.id);
				tips.style.color="#F00";
				tips.style.fontSize="12px";
				obj.parentNode.appendChild(tips);
			}
			switch (errs) {
			}
		}
	}
}

