﻿//************************//
//Author 		: ldc
//Date			: 2008-04-25
//LastModify	: 2008-04-25
//E-Mail		: ldc003@163.com
function $(o){
	return document.getElementById(o);
}
function getA(o,name){
	return o.getAttribute(name);
}
String.prototype.inc = function(s){
	if(this.indexOf(s)!=-1)
		return true;
	return false;
}

function isTaiwanIdCard(idCard){
	if (null==idCard||""==idCard) return false;
    if (!(/^[a-zA-Z]{1}[\d]{9}$/).test(idCard)) return false;

	var x1 = 0;
    var x2 = parseInt(idCard.substr(1, 1));
    var x3 = parseInt(idCard.substr(2, 1));
    var x4 = parseInt(idCard.substr(3, 1));
    var x5 = parseInt(idCard.substr(4, 1));
    var x6 = parseInt(idCard.substr(5, 1));
    var x7 = parseInt(idCard.substr(6, 1));
    var x8 = parseInt(idCard.substr(7, 1));
    var x9 = parseInt(idCard.substr(8, 1));
    var x10 = parseInt(idCard.substr(9, 1));
    //第一位字母数字映射表
	var obj = {
		'A':10,
		"B":11,
		'C':12,
		'D':13,
		'E':14,
		'F':15,
		'G':16,
		'H':17,
		'I':34,
		'J':18,
		'K':19,
		'L':20,
		'M':21,
		'N':22,
		'O':35,
		'P':23,
		'Q':24,
		'R':25,
		'S':26,
		'T':27,
		'U':28,
		'V':29,
		'W':32,
		'X':30,
		'Y':31,
		'Z':33
	};
	var xx1 = idCard.substr(0, 1);
	var kv = null;
	for (var a in obj) {
		if (a.toLowerCase() == xx1.toLowerCase()) {
			x1 = obj[a];
            break;
        }
	}
	if (x1 == 0) return false;
    var s1 = parseInt(x1 / 10);
    var s2 = x1 % 10;
    var ck = s1 + 9 * s2 + 8 * x2 + 7 * x3 + 6 * x4 + 5 * x5 + 4 * x6 + 3 * x7 + 2 * x8 + x9;
    var k = 10 - ck % 10;
    if (k == 10) k = 0;
	if (k == x10) return true;
    return false;
}
function Validator(){
	me = this,
	this.errMsg = null,
	this.form = null,
	this.setCheck = function(form){
		me.form = document.forms[form];
		var os = me.form.elements;
		var o = null;
		for(var i=0;i<os.length;i++){
			o = os[i];
			if(getA(o,"chkRule")){
				o.onblur = me.setblur;
				o.onfocus = me.setfocus;
			}
		}
	},
	this.setblur = function(){
		if(me.check(this,false)){
			this.className = "";
			if($(this.id+"_span")){
				$(this.id+"_span").className = "chkok";
				$(this.id+"_span").innerHTML = "OK！";
			}
		}
	},
	this.setfocus = function(){
		this.className = "focus";
		if($(this.id+"_span")){
			$(this.id+"_span").className = "chknormal";
			$(this.id+"_span").innerHTML = getA(this,"baktext");
		}
	},
	this.check = function(o,chkall){
		var rule = getA(o,"chkRule");
		var result = /\/([0-9]{1,8})-([0-9]{1,8})\//.exec(rule);
		if(rule.inc("/null/")){
			if(o.value==""){
				o.className = "waring";
				if($(o.id+"_span")){
					$(o.id+"_span").className = "chkwaring";
					$(o.id+"_span").innerHTML = getA(o,"tname")+"不能為空！";
				}
				if(chkall==true){
					this.errMsg.push(getA(o,"tname")+"不能為空！");
				}
				return false;
			}
		}
		if(rule.inc("/^a/")){
			if(!(/^[a-zA-Z]{1}$/.test(o.value.substring(0,1)))){
				o.className = "waring";
				if($(o.id+"_span")){
					$(o.id+"_span").className = "chkwaring";
					$(o.id+"_span").innerHTML = getA(o,"tname")+"第一個字元必須是字母！";
				}
				if(chkall==true){
					this.errMsg.push(getA(o,"tname")+"第一個字元必須是字母！");
				}
				return false;
			}
		} 
		if(result){
			var nmin = result[1];
			var nmax = result[2];
			if(o.value.length<nmin||o.value.length>nmax){
				o.className = "waring";
				if($(o.id+"_span")){
					$(o.id+"_span").className = "chkwaring";
					$(o.id+"_span").innerHTML = getA(o,"tname")+"長度必須是"+nmin+"-"+nmax+"個字元！";
				}
				if(chkall==true){
					this.errMsg.push(getA(o,"tname")+"長度必須是"+nmin+"-"+nmax+"個字元！");
				}
				return false;
			}
		}
		if(rule.inc("/compare/")){
			var cop = $(getA(o,"compareto"));
			if(o.value!=cop.value){
				o.className = "waring";
				if($(o.id+"_span")){
					$(o.id+"_span").className = "chkwaring";
					$(o.id+"_span").innerHTML = getA(o,"tname")+"與"+getA(cop,"tname")+"輸入的不一致！";
				}
				if(chkall==true){
					this.errMsg.push(getA(o,"tname")+"與"+getA(cop,"tname")+"輸入的不一致！");
				}
				return false;
			}
		} 
		if(rule.inc("/mail/")){
			if(!(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(o.value))){
				o.className = "waring";
				if($(o.id+"_span")){
					$(o.id+"_span").className = "chkwaring";
					$(o.id+"_span").innerHTML = getA(o,"tname")+"格式有誤！";
				}
				if(chkall==true){
					this.errMsg.push(getA(o,"tname")+"格式有誤！");
				}
				return false;
			}
		} 
		if(rule.inc("/date/")){
				
			if(!(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/.test(o.value))&&!(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/.test(o.value))){
				o.className = "waring";
				if($(o.id+"_span")){
					$(o.id+"_span").className = "chkwaring";
					$(o.id+"_span").innerHTML = getA(o,"tname")+"格式有誤！";
				}
				if(chkall==true){
					this.errMsg.push(getA(o,"tname")+"格式有誤！");
				}
				return false;
			}
		} 
		if(rule.inc("/num/")){
			if(!(/^[0-9]{1,8}$/.test(o.value))){
				o.className = "waring";
				if($(o.id+"_span")){
					$(o.id+"_span").className = "chkwaring";
					$(o.id+"_span").innerHTML = getA(o,"tname")+"只能為數字！";
				}
				if(chkall==true){
					this.errMsg.push(getA(o,"tname")+"只能為數字！");
				}
				return false;
			}
		} 
		if(rule.inc("/abc/")){
			if(!(/^[a-zA-Z]+$/.test(o.value))){
				o.className = "waring";
				if($(o.id+"_span")){
					$(o.id+"_span").className = "chkwaring";
					$(o.id+"_span").innerHTML = getA(o,"tname")+"只能為英文字母！";
				}
				if(chkall==true){
					this.errMsg.push(getA(o,"tname")+"只能為英文字母！");
				}
				return false;
			}
		} 
		if(rule.inc("/abc1/")){
			if(!(/^[a-zA-Z0-9]+$/.test(o.value))){
				o.className = "waring";
				if($(o.id+"_span")){
					$(o.id+"_span").className = "chkwaring";
					$(o.id+"_span").innerHTML = getA(o,"tname")+"只能為英文或者數位！";
				}
				if(chkall==true){
					this.errMsg.push(getA(o,"tname")+"只能為英文或者數位！");
				}
				return false;
			}
		}
		if(rule.inc("/custom/")){
		    var r = new RegExp(getA(o,"exp"));
			if(!r.test(o.value)){
				o.className = "waring";
				if($(o.id+"_span")){
					$(o.id+"_span").className = "chkwaring";
					$(o.id+"_span").innerHTML = getA(o,"tips");
				}
				if(chkall==true){
					this.errMsg.push(getA(o,"tips"));
				}
				return false;
			}
		}
		if(rule.inc("/idcard/")){
			if(!isTaiwanIdCard(o.value)){
				o.className = "waring";
				if($(o.id+"_span")){
					$(o.id+"_span").className = "chkwaring";
					$(o.id+"_span").innerHTML = getA(o,"tips");
				}
				if(chkall==true){
					this.errMsg.push(getA(o,"tname")+"格式有誤！");
				}
				return false;
			}
		}
		if(rule.inc("/exist/")){
			if(chkall==false){
				$(o.id+"_span").className = "chknormal";
				$(o.id+"_span").innerHTML = "正在驗證是否可用..";
				var callback = getA(o,"fn");
				try{
					eval("var fn = "+callback);
					fn(o.value,function(res){
						if(res.value==1){
							o.className = "";
							$(o.id+"_span").className = "chkok";
							$(o.id+"_span").innerHTML = "該"+getA(o,"tname")+"可以註冊！";
						}else{
							o.className = "waring";
							$(o.id+"_span").className = "chkwaring";
							$(o.id+"_span").innerHTML = getA(o,"tname")+" "+o.value+" 已經被註冊！";
						}
					});return;
				}catch(e){alert(e.message)};
			}
		}
		return true;	
	},
	this.checkall = function(){
		me.errMsg = new Array();
		var os = me.form.elements;
		var o = null;
		for(var i=0;i<os.length;i++){
			o = os[i];
			if(getA(o,"chkRule")){
				me.check(o,true);
			}
		}
		var msg = "";
		if(me.errMsg.length>0){
			for(var i=0;i<me.errMsg.length;i++){
				if(i>0){
					msg+= "\n";
				}
				msg+= "- "+ me.errMsg[i];
			}
			alert(msg);
			return false;
		}
		return true;
	}
}
