﻿// JScript 文件
// validate200712.js
// 脚本验证相关
// 创建日期 2007年12月13日
// 创建人 杨晓波
// 需要同时依赖 prototype1.5.1.js

    function getActiveElementId(e) // e  = event
    {
        var target = e.explicitOriginalTarget || document.activeElement;
        return target.id; 
    }

    var passImageUrl = "<img src='http://img2.ucar.cn/ucar/image/ucar/ucar2009/sell2_tu4.gif' align='absmiddle'/>";
    var unPassImageUrl = "<img src='http://img1.ucar.cn/ucar/image/ucar/ucar2009/sell2_tu3.gif' align='absmiddle'/>";

    var CarPriceFilter=/^(([0-9]{1,3})|([0-9]{1,3}\.[0-9]{1,2}))$/; //价格正则表达式
    var MobileNumFilter=/^13[0-9]{9}$|^0{0,1}15[0-9]{9}$/;//手机号正则表达式
    var TelephoneFilter=/^(0\d{2,3})?-?(\d{7,8})(-\d{3,6})?$/; //Email正则表达式
    var TelephoneOrMobileFilter=/^(0\d{2,3})?-?(\d{7,8})(-\d{3,6})?$|^13[0-9]{9}$|^0{0,1}15[0-9]{1}[0-9]{8}$/;//电话或者手机好正则表达式
    var EmailNumFilter=/^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/; //Email正则表达式
    var PostCodeFilter=/^\d{6}$/;//邮政编码
    var EngChsFilter=/^[A-Za-z0-9_\u4E00-\u9FA5]+$/;//中文或者英文没有非法字符
    var EngFilter=/^[A-Za-z0-9_]+$/;//中文或者英文没有非法字符
    var TelAteaNo=/^0\d{2,3}$/;//区号
    var TelTelNo=/^\d{7,8}$/;//座机
    var MileFilter=/^\d{1,8}$/;//邮政编码
    var ExhaustFilter=/^(([0-9]{1,2})|([0-9]{1,2}\.[0-9]{1}))$/; //排气量
    
    
    //判断控件内容是不是为空　置焦点
    //ControlId 控件Id
    //AlertId 显示内容的Id
    //AlertMessage 显示的内容
    function CheckNullWithFocus(ControlId,AlertId,AlertMessage)
    {       
      return  CheckNullAll(ControlId,AlertId,AlertMessage,true);
    }
    
    //判断控件内容是不是为空　不置焦点
    //ControlId 控件Id
    //AlertId 显示内容的Id
    //AlertMessage 显示的内容
    function CheckNullNoFocus(ControlId,AlertId,AlertMessage)
    {       
     return CheckNullAll(ControlId,AlertId,AlertMessage,false);
    }
    
    //判断控件值的长度 有焦点
    function CheckLengthWithFocus(control, span, message, max)
    {
        return CheckOverLength(control, span, message, 0, max, true);
    }
    
    //判断控件值的长度 无焦点
    function CheckLengthWithoutFocus(control, span, message, max)
    {
        return CheckOverLength(control, span, message, 0, max, false);
    }
    
    //判断控件内容是不是符合正则表达式　置焦点
    //ControlId 控件Id
    //AlertId 显示内容的Id
    //AlertMessage 显示的内容
    //正则表达式
    function CheckExpWithFocus(ControlId,AlertId,AlertMessage,RegularExpression)
    {
       return  CheckExpAll(ControlId,AlertId,AlertMessage,RegularExpression,true)
    }
    
    //判断控件内容是不是符合正则表达式 不置焦点
    //ControlId 控件Id
    //AlertId 显示内容的Id
    //AlertMessage 显示的内容
    //正则表达式
     function CheckExpNoFocus(ControlId,AlertId,AlertMessage,RegularExpression)
    {
       return  CheckExpAll(ControlId,AlertId,AlertMessage,RegularExpression,false)
    }
    
    
     //判断控件内容是不是符合手机号 置焦点
    //ControlId 控件Id
    //AlertId 显示内容的Id
    //AlertMessage 显示的内容
     function CheckMobileExpWithFocus(ControlId,AlertId,AlertMessage)
     {
        return CheckExpAll(ControlId,AlertId,AlertMessage,MobileNumFilter,true)
     }
     
    //判断控件内容是不是符合手机号 不置焦点
    //ControlId 控件Id
    //AlertId 显示内容的Id
    //AlertMessage 显示的内容
     function CheckMobileExpNoFocus(ControlId,AlertId,AlertMessage)
     {
        return CheckExpAll(ControlId,AlertId,AlertMessage,MobileNumFilter,false)
     }
     
    //判断控件内容是不是符合价格 置焦点
    //ControlId 控件Id
    //AlertId 显示内容的Id
    //AlertMessage 显示的内容
     function CheckCarPriceExpWithFocus(ControlId,AlertId,AlertMessage)
     {
       return CheckExpAll(ControlId,AlertId,AlertMessage,CarPriceFilter,false)
     }
    
     //判断控件内容是不是符合价格 不置焦点
     //ControlId 控件Id
     //AlertId 显示内容的Id
     //AlertMessage 显示的内容
     function CheckCarPriceExpNoFocus(ControlId,AlertId,AlertMessage)
     {
       return CheckExpAll(ControlId,AlertId,AlertMessage,CarPriceFilter,false)
     }
     
     
     //判断控件内容是不是符合Email 置焦点
    //ControlId 控件Id
    //AlertId 显示内容的Id
    //AlertMessage 显示的内容
     function CheckEmailExpWithFocus(ControlId,AlertId,AlertMessage)
     {
       return CheckExpAll(ControlId,AlertId,AlertMessage,EmailNumFilter,false)
     }
    
     //判断控件内容是不是符合Email 不置焦点
     //ControlId 控件Id
     //AlertId 显示内容的Id
     //AlertMessage 显示的内容
     function CheckEmailExpNoFocus(ControlId,AlertId,AlertMessage)
     {
       return CheckExpAll(ControlId,AlertId,AlertMessage,EmailNumFilter,false)
     }
   
    
    /***********************************************以下为私有方法请使用上面的方法*************************************************/
    
    
    //判断控件内容是不是为空
    //ControlId 控件Id
    //AlertId 显示内容的Id
    //AlertMessage 显示的内容
    //IsFous 是否置焦点
    function CheckNullAll(ControlId,AlertId,AlertMessage,IsFocus)
    {
      if(ControlId==undefined || ControlId=="" || ControlId==null)
      {
         alert("CheckNull 方法ControlId为必选参数;");
         return false;
      }
      if($(ControlId).value.strip()=="")
      {
          if(AlertMessage==undefined)
          {
             AlertMessage=ControlId+"的不能为空！";
          }
          if(AlertId==undefined)
          {
             alert(AlertMessage);
          }
        　else
        　{
        　　 $(AlertId).update(unPassImageUrl + "&nbsp;" + AlertMessage);
          }
          if(IsFocus)//如果置焦点
          {
             $(ControlId).focus();
          }
            return false;
       }
       else 
       {
       　 if(AlertId!=undefined)
       　 {
        　　 $(AlertId).update(passImageUrl);
          }
          return true;
       }
    }
    
    //判断控件内容是不是为空
    //ControlId 控件Id
    //AlertId 显示内容的Id
    //AlertMessage 显示的内容
    //IsFous 是否置焦点
    function CheckOverLength(ControlId,AlertId,AlertMessage,MinLength,MaxLength,IsFocus)
    {
      if(ControlId==undefined || ControlId=="" || ControlId==null)
      {
         alert("CheckNull 方法ControlId为必选参数;");
         return false;
      }
      var Length=$(ControlId).value.strip().len();

      if(Length<MinLength||Length>MaxLength)
      {
          if(AlertMessage==undefined)
          {
             AlertMessage=ControlId+"的不能为空！";
          }
          if(AlertId==undefined)
          {
             alert(AlertMessage);
          }
        　else
        　{
        　　 $(AlertId).update(unPassImageUrl + "&nbsp;" + AlertMessage);
          }
          if(IsFocus)//如果置焦点
          {
             $(ControlId).focus();
          }
            return false;
       }
       else 
       {
       　 if(AlertId!=undefined)
       　 {
        　　 $(AlertId).update(passImageUrl);
          }
          return true;
       }
    }
   
    //判断控件内容是不是符合正则表达式
    //ControlId 控件Id
    //AlertId 显示内容的Id
    //AlertMessage 显示的内容
    //RegularExpression 正则表达式
    //IsFocus 是否设置焦点
    function CheckExpAll(ControlId,AlertId,AlertMessage,RegularExpression,IsFocus)
    {       
      if(ControlId==undefined || ControlId=="" || ControlId==null)
      {
         alert("CheckNull 方法ControlId为必选参数;");
         return false;
      }
       if(RegularExpression==undefined || RegularExpression=="" || RegularExpression==null)
      {
         alert("CheckNull 方法RegularExpression为必选参数;");
         return false;
      }
      if(!RegularExpression.test($(ControlId).value)&& $(ControlId).value.strip()!="")
      {
          if(AlertMessage==undefined)
          {
             AlertMessage=ControlId+"的格式不正确！";
          }
          if(AlertId==undefined)
          {
             alert(AlertMessage);
          }
        　else
        　{
        　　 $(AlertId).update(unPassImageUrl + "&nbsp;" + AlertMessage);
          }
          if(IsFocus)
          {
            $(ControlId).focus();
          }
            return false;
       }
       else 
       {
       　 if(AlertId!=undefined)
       　 {
        　　 $(AlertId).update(passImageUrl);
          }
          return true;
       }
    }
    
     
   //增加String类的len方法，返回中文为2，应为为1
     
   String.prototype.len=function(){ 
                  return this.replace(/[^\x00-\xff]/g,"**").length; 
   } 
   
    //判断控件内容是不是超过长度，返回中文为2，英文为1
    //arguments 字符串
    //strlen 长度
   function CheckLength(arguments,strlen) 
   { 
     var ValidStrLength=strlen;                  
     if (arguments.len()<=ValidStrLength) 
           return true;
     else 
           return false; 
   } 

