﻿//依赖prototype.1.6

var Ucar_Validate = Class.create({
    //构造函数*****
    initialize : function(ControlId, MessageContainerId) { //ControlId：被验证控件的Id；MessageContainerId：显示提示信息容器的Id
		this.Id = ControlId;
		this.Object = $(this.Id);
		if (!!MessageContainerId){
		    this.MessageContainerId = MessageContainerId;
		    this.AutoShowMessage = true; //如果MessageContainerId属性被赋值，则认为需要自动显示提示信息
		}
	}, 
	
	//属性*****
    Id:null, //被验证控件的Id
    Object:null,//被验证控件的对象
    MessageContainerId:null, //显示提示信息容器的Id
    EmptyMessage:null, //非空验证的错误提示信息
    LengthMessage:null, //长度验证的错误提示信息
    RangeMessage:null, //范围验证的错误提示信息
    FormatMessage:null, //格式验证的错误提示信息
    AutoShowMessage:false, //是否自动显示提示信息
    MinLength:0, //最小长度，长度验证时用，默认为0
    MaxLength:null, //最大长度，长度验证时用
    MinValue:0, //最小值，范围验证时用，默认为0
    MaxValue:null, //最大值。范围验证时用
    RegularExpression:null, //正则表达式，格式验证时用
    Result:{Empty:false, Length:false, Range:false, Format:false}, //验证结果
    
    //方法*****
    Check:function(callback){
        this.CheckEmpty();
        this.CheckLength();
        this.CheckRange();
        this.CheckFormat();
        var fn = callback || function(){}
        fn.call(this,this.Result);
    },
    CheckEmpty:function(msg){ //进行非空验证的方法，传入参数：非空验证提示信息
        if (!!msg){
            this.EmptyMessage = msg;
        }
        
        if ($F(this.Id).strip().length > 0){
            this.Result.Empty = true;
            if (this.AutoShowMessage){
                this.ShowSuccessMessage();
            }
        }
        else{
            this.Result.Empty = false;
            if (this.AutoShowMessage){
                this.ShowFailMessage(this.EmptyMessage);
            }
        }
        
        return this.Result.Empty;
    },
    
     CheckEmptyforAlert:function(msg){ //进行非空验证的方法，传入参数：非空验证提示信息

        if ($F(this.Id).strip().length > 0){
            this.Result.Empty = true;  
            if ($F(this.Id) =="请输入电话号码")
            {alert (msg );}         
        }
        else{
            this.Result.Empty = false;
               alert (msg);
        }
        return this.Result.Empty;
    }
    ,
    CheckLength:function(max, msg){ //进行长度验证的方法，传入参数：最大字符串长度，长度验证提示信息
        if (!!max){
            this.MaxLength = max;
        }
        if (!!msg){
            this.LengthMessage = msg;
        }
    
        if(!!this.MaxLength){
            var length = $F(this.Id).strip().len();
            if(length >= (this.MinLength * 2) && length <= (this.MaxLength * 2)){
                this.Result.Length = true;
                if (this.AutoShowMessage){
                    this.ShowSuccessMessage();
                }
            }
            else{
                this.Result.Length = false;
                if (this.AutoShowMessage){
                    this.ShowFailMessage(this.LengthMessage);
                }
            }
        }
        
        return this.Result.Length;
    },
    CheckTrueLength:function(max, msg){ //进行长度验证的方法，传入参数：最大字符串长度，长度验证提示信息
        if (!!max){
            this.MaxLength = max;
        }
        if (!!msg){
            this.LengthMessage = msg;
        }
    
        if(!!this.MaxLength){
            var length = $F(this.Id).strip().len();
            if(length >= this.MinLength && length <= this.MaxLength){
                this.Result.Length = true;
                if (this.AutoShowMessage){
                    this.ShowSuccessMessage();
                }
            }
            else{
                this.Result.Length = false;
                if (this.AutoShowMessage){
                    this.ShowFailMessage(this.LengthMessage);
                }
            }
        }
        
        return this.Result.Length;
    },
    CheckRange:function(min, msg){ //进行范围验证的方法，传入参数：最小值，范围验证提示信息
        if (!!min){
            this.MinValue = min;
        }
        if (!!msg){
            this.RangeMessage = msg;
        }
        
        if(!!this.MinValue && !!this.MaxValue){
            var strValue = $F(this.Id).strip();
            if(strValue >= this.MinValue && strValue <= this.MaxValue){
                this.Result.Range = true;
                if (this.AutoShowMessage){
                    this.ShowSuccessMessage();
                }
            }
            else{
                this.Result.Range = false;
                if (this.AutoShowMessage){
                    this.ShowFailMessage(this.RangeMessage);
                }
            }
        }
        else if(!!this.MinValue && !this.MaxValue){
            var strValue = $F(this.Id).strip();
            if(strValue >= this.MinValue){
                this.Result.Range = true;
                if (this.AutoShowMessage){
                    this.ShowSuccessMessage();
                }
            }
            else{
                this.Result.Range = false;
                if (this.AutoShowMessage){
                    this.ShowFailMessage(this.RangeMessage);
                }
            }
        }
        else if(!this.MinValue && !!this.MaxValue){
            var strValue = $F(this.Id).strip();
            if(strValue <= this.MaxValue){
                this.Result.Range = true;
                if (this.AutoShowMessage){
                    this.ShowSuccessMessage();
                }
            }
            else{
                this.Result.Range = false;
                if (this.AutoShowMessage){
                    this.ShowFailMessage(this.RangeMessage);
                }
            }
        }
        
        return this.Result.Range;
    },
    CheckFormat:function(exp, msg){ //进行格式验证的方法，传入参数：正则表达式，格式验证提示信息
        if (!!exp){
            this.RegularExpression = exp;
        }
        if (!!msg){
            this.FormatMessage = msg;
        }
    
        if(!!this.RegularExpression){    
            if(this.RegularExpression.test($F(this.Id).strip()) || $F(this.Id).strip().empty()){
                this.Result.Format = true;
                if (this.AutoShowMessage){  
                    this.ShowSuccessMessage();
                }
            }
            else{
                this.Result.Format = false;
                if (this.AutoShowMessage){
                    this.ShowFailMessage(this.FormatMessage);
                }
            }
        }
        
        return this.Result.Format;
    },
     CheckFormatforAlert:function(exp, msg){ //进行格式验证的方法，传入参数：正则表达式，格式验证提示信息
        if (!!exp){
            this.RegularExpression = exp;
        }    
        if(!!this.RegularExpression){    
            if(this.RegularExpression.test($F(this.Id).strip()) || $F(this.Id).strip().empty()){
                this.Result.Format = true;
            }
            else {
                this.Result.Format = false;
                if ($F(this.Id) !="请输入电话号码")
                    alert(msg);
            }
        }
        
        return this.Result.Format;
    },
    
    ShowSuccessMessage:function(){ //显示验证成功信息的方法
        $(this.MessageContainerId).className = "alertOk";
        $(this.MessageContainerId).update("");
    },
    ShowFailMessage:function(msg){ //显示验证失败信息的方法
        $(this.MessageContainerId).className = "alertMsg";
        $(this.MessageContainerId).update(msg);
    },
    ClearMessage:function(){ //清空验证信息的方法
        $(this.MessageContainerId).className = "";
        $(this.MessageContainerId).update("");
    },
    SetFocus:function(){ //为被验证设置焦点的方法
       if (!$(this.Id).disabled){
            $(this.Id).focus();
       } 
    } 
});

//正则表达式
var RegExpr = {
    CarPrice:/^(([1-9]{1}[0-9]{0,2})|([1-9]{1}[0-9]{0,2}\.[0-9]{1,2})|([0]{1}\.[0-9]{1,2}))$/, //车辆价格
    Mobile:/^1[0-9]{10}$/, //手机号
    Telephone:/^(0\d{2,3})?-?(\d{7,8})(-\d{3,6})?$/, //座机号
    TelOrMobile:/^(0\d{2,3})?-?(\d{7,8})(-\d{3,6})?$|^1[0-9]{10}$/, //手机号或座机号
    Email:/^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/, //Email
    PostCode:/^\d{6}$/, //邮政编码
    EngOrChs:/^[A-Za-z0-9_\u4E00-\u9FA5]+$/, //中文或者英文没有非法字符
    English:/^[A-Za-z0-9_]+$/, //英文没有非法字符
    Tel_Area:/^0\d{2,3}$/, //座机区号
    Tel_Number:/^\d{7,8}$/, //座机号码
    Mileage:/^[1-9]{1}[0-9]{0,7}$/, //行驶里程
    Exhaust:/^(([0-9]{1,2})|([0-9]{1,2}\.[0-9]{1}))$/ //排气量
}

//获取当前活动状态的控件的Id的方法
function getActiveElementId(e)
{
    var target = e.explicitOriginalTarget || document.activeElement;
    return target.id;
}

//增加String类的len方法，返回中文为2，英文为1     
String.prototype.len=function(){ 
    return this.replace(/[^\x00-\xff]/g,"**").length; 
}
