/***************************************************************
 * js基础框架代码库v0.1
 * author:Alexander.Lee
 * date:  2010-09-07
 **************************************************************/
var v_num=/^[0-9]*$/;              //验证全包含数字
var v_numchar=/^[0-9a-zA-Z]+$/;   //包含数字，字母
var v_int=/^([0-9]{1}|[1-9]{1}[0-9]*)$/;     //验证整数
var v_login=/^[a-zA-Z0-9\-_@\.]+$/;
var v_email=/^[\w\.-]+(\+[\w-]*)?@([\w-]+\.)+[\w-]+$/;
var v_chinese=/^[\u4e00-\u9fa5]+$/;
var v_date=/^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$/;
var v_postcode=/^[1-9]\d{5}(?!\d)$/;

//用户表单对象
function UserForm(items){
    this.f_item=items;
    this.last_error="";
    this.lock=false;
    for(var idx=0;idx<this.f_item.length;idx++){
        var fc=this.get_check(this.f_item[idx]);
        var of=this.get_focus(this.f_item[idx]);
        $("#"+this.f_item[idx].field_id).focus(of);
        $("#"+this.f_item[idx].field_id).blur(fc);
    }
}
//绑定验证事件的处理器
UserForm.prototype.get_check=function(v){
    return function(){
        v.validate();
    }
}

UserForm.prototype.get_focus=function(v){
    return function(){
        v.checked=false;
    }
}

//验证所有的字段
UserForm.prototype.validate=function(){
    for(var idx=0;idx<this.f_item.length;idx++){
        this.f_item[idx].validate();
        if(!this.f_item[idx].checked){
            this.last_error=this.f_item[idx].last_error;
            return false;
        }
    }
    return true;
}
//被绑定的提交事件包装器
UserForm.prototype.on_submit=function(bind,err){
    if(this.validate()){
        if(!this.lock){
            bind();
        }else{
            alert("locked");
        }
    }else{
        if(err){
            err(this.last_error);
        }
    }
}
//绑定提交事件到元件
UserForm.prototype.set_submit=function(bid,bind,err){
    var self=this;
    $("#"+bid).unbind();
    $("#"+bid).click(
        function(){
            self.on_submit(bind,err);
        }
    );
    $(document).keydown(
        function(e){
            if(e.keyCode==13){
                self.on_submit(bind,err);
            }
        }
    )
}

//被验证字段类
function Field(params){
    this.field_id=params.fid;
    this.tip_id=params.tid;
    this.validators=params.val;
    this.on_suc=params.suc;
    this.on_error=params.err;
    this.checked=false;
    this.last_error="";
    for(var idx=0;idx<this.validators.length;idx++){
        this.set_callback(this.validators[idx]);
        this.validators[idx].init(this.data());
    }
}
//获取字段值的方法
Field.prototype.data=function(){
        return document.getElementById(this.field_id).value;
    }
//设定验证回调事件
Field.prototype.set_callback=function(val){
        var self=this;
        val.on_suc=function(){
            self.checked=true;
            self.on_suc($("#"+self.tip_id),val.tips);
        }
        val.on_error=function(){
            self.checked=false;
            self.on_error($("#"+self.tip_id),val.tips);
        }
    }
//执行验证逻辑
Field.prototype.validate=function(){
        for(var idx=0;idx<this.validators.length;idx++){
            this.set_callback(this.validators[idx]);
            if(!this.validators[idx].validate(this.data())){
                this.last_error=this.validators[idx].tips;
                this.checked=false;
                return;
            }
        }
    }
//长度验证的验证器类
function Len_val(min_l,max_l,tip){
    this.min_v=min_l;
    this.max_v=max_l;
    this.tips=tip;
    this.on_suc=null;
    this.on_error=null;
}
Len_val.prototype.init=function(fd){}
Len_val.prototype.validate=function(fd){
    if(fd.length<this.min_v||fd.length>this.max_v){
        this.on_error();
        return false;
    }
    this.on_suc();
    return true;
}
//正则表达式验证器
function Exp_val(expresion,tip){
    this.exps=expresion;
    this.tips=tip;
    this.on_suc=null;
    this.on_error=null;
}
Exp_val.prototype.init=function(fd){}
Exp_val.prototype.validate=function(fd){
    if(!fd){
        this.on_suc();
        return true;
    }
    if(this.exps.test(fd)){
        this.on_suc();
        return true;
    }else{
        this.on_error();
        return false;
    }
}
//密码类验证器
function Pass_val(tid,css,tip){
    this.tip_id=tid;
    this.css=css;
    this.tips=tip;
    this.on_suc=null;
    this.on_error=null;
}
Pass_val.prototype.init=function(fd){}
Pass_val.prototype.validate=function(fd){
    if(!fd){
        $("#"+this.tip_id).attr("src",this.css.weak);
        this.on_error();
        return false;
    }
    if(fd.length<6){
        $("#"+this.tip_id).attr("src",this.css.weak);
        this.on_error();
        return false;
    }
    if(v_num.test(fd)){
        $("#"+this.tip_id).attr("src",this.css.weak);
        this.on_error();
        return false;
    }
    if(v_numchar.test(fd)){
        $("#"+this.tip_id).attr("src",this.css.mid);
        this.on_suc();
        return true;
    }
    $("#"+this.tip_id).attr("src",this.css.strong);
    this.on_suc();
    return true;
}
//远程验证器
function Remote_val(url,tip){
    this.p_url=url;
    this.tips=tip;
    this.on_suc=null;
    this.on_error=null;
    this.checked=false;
}
Remote_val.prototype.init=function(fd){
    this.validate(fd);
}
Remote_val.prototype.validate=function(fd){
    var self=this;
    $.post(this.p_url,{p:fd},
        function(data){
            if(data.rs){
                self.checked=true;
                self.on_suc();
                return;
            }else{
                self.on_error();
            }
        },"json"
    );
    return this.checked;
}
//自定义函数验证器
function Man_val(tip,func){
    this.tips=tip;
    this.val_func=func;
    this.on_suc=null;
    this.on_error=null;
}
Man_val.prototype.init=function(fd){}
Man_val.prototype.validate=function(fd){
    if(this.val_func(fd)){
        this.on_suc();
        return true;
    }else{
        this.on_error();
        return false;
    }
}

function showError(id,text){
    $("#"+id).html("<img border=\"0\" src=\"/static/image/Cry.gif\"/>&nbsp;&nbsp;"+text);
    $("#"+id).dialog();
    $("#"+id).dialog("option","resizable",false);
    $("#"+id).dialog("option","modal",true);
}

function showWarning(id,text){
    $("#"+id).html("<img border=\"0\" src=\"/static/image/warning.gif\"/>&nbsp;&nbsp;"+text);
    $("#"+id).dialog();
    $("#"+id).dialog("option","resizable",false);
    $("#"+id).dialog("option","modal",true);
}

function showError2(id,text){
    $("#"+id).html("&nbsp;&nbsp;"+text);
    $("#"+id).show();
    $("#"+id).click(
        function(){
            $("#"+id).hide();
        }
    );
}

function showSuc1(id,text){
    $("#"+id).html("&nbsp;&nbsp;"+text);
    $("#"+id).show();
    $("#"+id).click(
        function(){
            $("#"+id).hide();
        }
    );
}


function serverlist(id){
    $("#"+id).html('<iframe frameborder="0" width="656" height="384" src="/game/servers" scrolling="no"></iframe>');
    $("#"+id).dialog({width:700});
    $("#"+id).dialog("option","resizable",false);
}

function randurl(){
    return "/randcode?v="+Math.round(Math.random()*10000);
}

function setCookie(name,value)//两个参数，一个是cookie的名字，一个是值
{
    var Days = 30; //此 cookie 将被保存 30 天
    var exp  = new Date();    //new Date("December 31, 9998");
    exp.setTime(exp.getTime() + Days*24*60*60*1000);
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
function getCookie(name)//取cookies函数
{
    var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
     if(arr != null) return unescape(arr[2]); return null;
}
function delCookie(name)//删除cookie
{
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval=getCookie(name);
    if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}

function checkIdcard(idcard){
	var Errors=new Array(
	"",
	"身份证号码位数不对",
	"身份证号码出生日期超出范围或含有非法字符",
	"身份证号码校验错误",
	"身份证地区错误"
	);
	var area={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外"}
	var idcard,Y,JYM;
	var S,M;
	var idcard_array = new Array();
	idcard_array = idcard.split("");
	//地区检验
	if(area[parseInt(idcard.substr(0,2))]==null) return Errors[4];
	//身份号码位数及格式检验
	switch(idcard.length){
		case 15:
			if ( (parseInt(idcard.substr(6,2))+1900) % 4 == 0 || ((parseInt(idcard.substr(6,2))+1900) % 100 == 0 && (parseInt(idcard.substr(6,2))+1900) % 4 == 0 )){
				ereg=/^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$/;//测试出生日期的合法性
			} else {
				ereg=/^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$/;//测试出生日期的合法性
			}
			if(ereg.test(idcard)) return Errors[0];
			else return Errors[2];
			break;
		case 18:
			//18位身份号码检测
			//出生日期的合法性检查
			//闰年月日:((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))
			//平年月日:((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))
			if ( parseInt(idcard.substr(6,4)) % 4 == 0 || (parseInt(idcard.substr(6,4)) % 100 == 0 && parseInt(idcard.substr(6,4))%4 == 0 )){
				ereg=/^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$/;//闰年出生日期的合法性正则表达式
			} else {
				ereg=/^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$/;//平年出生日期的合法性正则表达式
			}
			if(ereg.test(idcard)){//测试出生日期的合法性
				//计算校验位
				S = (parseInt(idcard_array[0]) + parseInt(idcard_array[10])) * 7
				+ (parseInt(idcard_array[1]) + parseInt(idcard_array[11])) * 9
				+ (parseInt(idcard_array[2]) + parseInt(idcard_array[12])) * 10
				+ (parseInt(idcard_array[3]) + parseInt(idcard_array[13])) * 5
				+ (parseInt(idcard_array[4]) + parseInt(idcard_array[14])) * 8
				+ (parseInt(idcard_array[5]) + parseInt(idcard_array[15])) * 4
				+ (parseInt(idcard_array[6]) + parseInt(idcard_array[16])) * 2
				+ parseInt(idcard_array[7]) * 1
				+ parseInt(idcard_array[8]) * 6
				+ parseInt(idcard_array[9]) * 3 ;
				Y = S % 11;
				M = "F";
				JYM = "10X98765432";
				M = JYM.substr(Y,1);//判断校验位
				if(M == idcard_array[17]) return Errors[0]; //检测ID的校验位
				else return Errors[3];
			}
			else return Errors[2];
			break;
		default:
			return Errors[1];
		break;
	}
}

function validate(id,range,pattern,cannull){
    patterns={
        'login':/^[a-zA-Z0-9\.\-@_]+$/,
        'charnumber':/^[a-zA-Z0-9]+$/,
        'email':/^[\w\.-]+(\+[\w-]*)?@([\w-]+\.)+[\w-]+$/,
        'nohtme':/^[^<|^>]+$/,
        'idcard':/^(\d{17}[a-zA-Z0-9]{1})|\d{15}$/,
        'chinese':/^[\u4e00-\u9fa5]+$/,
        'number':/^[0-9]+$/,
        'mobile':/^0{0,1}(13[0-9]|15[0-9]|18[0-9])[0-9]{8}$/,
        'date':/^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$/,
        'postcode':/^[1-9]\d{5}(?!\d)$/
    };
    v=$("#"+id).val();
    if(v)
    {
        if(range[0]<v.length&&v.length<range[1]){
            if(pattern){
	            if(patterns[pattern]){
	            	if(patterns[pattern].exec(v)){
	            		return true;
	            	}else{
	            		return false;
	            	}
	            }else{
	            	return false;
	            }
            }else{
            	return true;
            }
        }else{
        	return false;
        }
    }else{
    	if(cannull){
    		return true;
    	}else{
    		return false;
    	}
    }
}

function count_coin(fee,coin,rate){
    $(fee).change(
        function(){
            $(coin).val(Number($(fee).val())*Number(rate));
        }
    );
    $(fee).keyup(
        function(){
            $(coin).val(Number($(fee).val())*Number(rate));
        }
    );
}

function must_number(item){
    $(item).css('ime-mode','disabled');
    $(item).keypress(function(event) {
	        if (!$.browser.mozilla) {
	            if (event.keyCode && (event.keyCode < 48 || event.keyCode > 57)) {
	                // ie6,7,8,opera,chrome管用
	                event.preventDefault();
	            }else{
                    if($(item).val().length<1&&event.keyCode==48){
                        event.preventDefault();
                    }
                }
	        } else {
	        if (event.charCode && (event.charCode < 48 || event.charCode > 57)) {
	                // firefox管用
	                event.preventDefault();
	            }else{
                    if($(item).val().length<1&&event.charCode==48){
                                            event.preventDefault();
                                        }
                }
	        }
    	});
}

function no_space(item){
    $(item).keypress(function(event) {
        if (!$.browser.mozilla) {
            if (event.keyCode && (event.keyCode ==32)) {
                // ie6,7,8,opera,chrome管用
                event.preventDefault();
            }
        } else {
        if (event.charCode && (event.charCode ==32 )) {
                // firefox管用
                event.preventDefault();
            }
        }
    });
}


