/*******************************************
 *  copyright (c) by rochsoft
 * 
 *  author : potato
 * 
 *  created date : 2008-8-25
 * ****************************************/
 

// JScript 文件

//验证结果类----------------------------------------------------------
function ValidatingResult(success, message)
{
    this.m_success = success;
    this.m_message = message;
}

ValidatingResult.prototype.getSuccess = function()
{
    return this.m_success;    
}

ValidatingResult.prototype.getMessage = function()
{
    return this.m_message;
}
//------------------------------------------------------------------

function SetTextBoxErrorStyle(txt)
{
    txt.style.backgroundColor = "#fff";
    txt.style.border = "#3B82C5 1px solid";
    txt.focus();
}

function SetTextBoxNormalStyle(txt)
{
    txt.style.backgroundColor = "#fff";
    txt.style.border = "#3B82C5 1px solid";
    txt.focus();
}

function SetTextBoxRequiredStyle(txt)
{
    txt.style.backgroundColor = "#fff";
    txt.style.border = "#3B82C5 1px solid";
}

//验证项,抽象类-----------------------------------------------------------------
function ValidatingItem(itemName)
{
    this.m_itemName = itemName;
}

ValidatingItem.prototype.validate = function()
{        
    return new ValidatingResult(true, "");
}
//--------------------------------------------------------------------------------


//文本框验证项,继承验证项----------------------------------------------------------------
//参数说明:
//itemName 项名称。如：姓名、部门
//txt 文本框对象。
//isRequired 此项是否必填。
//maxLength 文本框的最大长度
//regex 验证正则表达式，若没则传入null
//regexErrorMessage 正则验证失败的错误信息
function TextBoxValidatingItem(itemName, txt, isRequired, maxLength, regex, regexErrorMessage)
{
    ValidatingItem.call(this, itemName);    
   
    if(txt==null){
        return;
    }   
   
    this.m_txt = txt;    
    this.m_isRequired = isRequired;
    this.m_maxLength = maxLength;
    this.m_regex = regex;
    this.m_regexErrorMessage = regexErrorMessage;
    
    if (this.m_isRequired)
    {        
       SetTextBoxRequiredStyle(this.m_txt);
    }
    
    if (maxLength)
    {
        this.m_txt.maxLength = maxLength;
    }   
    
}

TextBoxValidatingItem.prototype = new ValidatingItem();

//私有方法，处理验证文本框内容失败 
TextBoxValidatingItem.prototype.m_responseError = function()
{
    SetTextBoxErrorStyle(this.m_txt);
}

//验证文本框
TextBoxValidatingItem.prototype.validate = function()
{    
    if(this.m_txt==null){
        return new ValidatingResult(true, "");
    }      
    
    //验证必填
    if (this.m_isRequired && trim(this.m_txt.value) == "")
    {
        this.m_responseError();
        return new ValidatingResult(false, "必须填写" + this.m_itemName);
    }
    //验证最大长度，若文框是单行，则在构造函数中已经将maxLength设置，则不可能会出现超长情况
    //若文本框是多行，则此验证会生效   
    if (this.m_maxlength != undefined && (this.m_txt.value.length > this.m_maxlength))
    {
        this.m_responseError();
        return new ValidatingResult(false, this.m_itemName + "最多只能填写" + this.m_maxLength.toString());
    }
    if (this.m_regex!=null)
    {  
        var re=new RegExp(this.m_regex);
        var tempTxtValue=trim(this.m_txt.value);
        if(tempTxtValue!="")
        {
            if (!re.test(this.m_txt.value))
            {
                this.m_responseError();
                return new ValidatingResult(false, this.m_regexErrorMessage);
            }            
        }
        else if(this.m_txt.value.length!=tempTxtValue.length)
        {
           return new ValidatingResult(false, this.m_regexErrorMessage);
        }
       else
       {
            SetTextBoxNormalStyle(this.m_txt);
       }
    }
    
    //处理验证成功
    if (this.m_isRequired)
    {
        SetTextBoxRequiredStyle(this.m_txt);
    }
    else
    {
        this.m_txt.style.backgroundColor = "white";       
    }
    
    return new ValidatingResult(true, "");
}
//------------------------------------------------------------------------------------


//验证外观类--------------------------------------------------------------------
function Validator()
{
    this.m_items = new Array();
}

Validator.prototype.addItem = function(item)
{
    this.m_items.push(item);
}   

Validator.prototype.validate = function()
{
    for (var i = 0; i < this.m_items.length; i++)
    {
        var result = this.m_items[i].validate();
        if (!result.getSuccess())
        {
            alert(result.getMessage());
            return false;
        }
    }
    return true;
}

Validator.checkMaxInput=function(textbox,length){ 
    if (textbox.value.length > length){
       textbox.value = textbox.value.substring(0, length);
    }
}

//检查键盘输入的数字(48-57)下划线(95)英文(65-90)(97-122)
Validator.checkNumberUnderlineInput = function(e)
{
    var KeyCode=e.keyCode;
    if((KeyCode>47 && KeyCode<58) || (KeyCode>64 && KeyCode<91) || (KeyCode>96 && KeyCode<123) || (KeyCode==95))
    {
       return true;
    }
    else
    {
      return false;
    } 
}

//检查键盘输入的数字　-(45)　/(47)
Validator.checkDateCharacterInput = function(e)
{
    var KeyCode=e.keyCode;
    if((KeyCode>47 && KeyCode<58) || (KeyCode==45) || (KeyCode==47))
    {
       return true;
    }
    else
    {
      return false;
    } 
}

//检查键盘输入的字符是否为数字
Validator.checkNumberInput = function(e)
{
     var charCode = e.keyCode;
     if(charCode>31 && (charCode<48 || charCode > 57))
     {   
         return false;  
     }   
     return true;   
}
//检查键盘输入为小数或数字
Validator.checkFloatInput=function(e)
{ 
   var KeyCode=e.keyCode;
    if((KeyCode>47 && KeyCode<58) || (KeyCode==46))
    {
       return true;
    }
    else
    {
      return false;
    }
}
//检查键盘输入为小数或数字或负号
Validator.checkFloatOrNegativeInput=function(e)
{
 var KeyCode=e.keyCode;

    if((KeyCode>47)&&(KeyCode<58)||(KeyCode==8)||(KeyCode==45)||(KeyCode==46))
    {
       return true;
    }
    else
    {
      return false;
    }
}

//检查输入“,"无效
Validator.CheckCommaInput=function(e)
{
    var keyCode=e.keyCode;
    if(keyCode==44)
    {
        return false;
    }
    else
    {
        return true;
    }    
}

//检测textbox多行文本框的长度是否越界
Validator.checkTextboxLength=function(textbox,maxLength){
    if(textbox==null){
        return;
    }
    
    if (textbox.value.length > maxLength){
       textbox.value=textbox.value.substring(0, maxLength);
    }
}

//检查键盘输入的任意字符，即不能输入任意字符
Validator.checkArbitrarinessCharacterInput = function(e)
{   
      return false; 
}
//-----------------------------------------------------------------------------------

function isDate(value)
{
   var r = value.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/); 
   if(r==null){
        r=value.match(/^(\d{1,2})(-|\/)(\d{1,2})\2(\d{1,4})$/);
        if(r==null){
            return false;
        }
        
        var dt=new Date(r[4], r[1]-1, r[3]);
        return (dt.getFullYear()==r[4]&&(dt.getMonth()+1)==r[1]&&dt.getDate()==r[3]);
   }

   var d = new Date(r[1], r[3]-1, r[4]); 
   return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]);
}

//验证日期 
//格式：2007-8-15、2007/8/15、8/15/2007
function Validator.validateDate(text,setTextBoxBackground){
    if(text.value!='')            
    {
        if(isDate(text.value))
        {
            setTextBoxBackground(text);
            return true;
        }
        else
        {
            SetTextBoxErrorStyle(text);
            return false;
        }
    }            
    
    setTextBoxBackground(text);
    
    return true;
}

function SetVisible(linkbutton)
{
  linkbutton.visible=false;
}

