/*
方法索引
1、IsEmptyString(checkObject, errorObject)//判断输入内容是否为空
2、CheckLength(checkObject, length, errorObject)//检测字符串长度是否大于指定长度
3、IsValidString(checkObject, errorObject)//判断输入的字符串是否为:a-z,A-Z,0-9
4、IsValidEmail(checkObject, errorObject)//判断输入的EMAIL格式是否正确
5、IsInteger(checkObject, errorObject)//判断输入的字符是否为整数
6、IsMobile = function(checkObject, errorObject)//判断输入的是否为手机号
7、IsTel = function(checkObject, errorObject)//判断输入的是否为固话
8、IsIntegerLargeThanZero(checkObject, errorObject)//判断输入的字符是否大于0的整数
*/

function $(myobject)
{
    return document.getElementById(myobject);
}

function isIE()
{ 
	//判断是否IE的方法  
	if (window.navigator.userAgent.toLowerCase().indexOf("msie")>=1)    
		return true;    
	else    
		return false;    
}    
  
if(!isIE())
{ 
	//给firefox增加 innerText 属性   
    HTMLElement.prototype.__defineGetter__("innerText",    
    function(){   
        var anyString = "";   
        var childS = this.childNodes;   
        for(var i=0; i<childS.length; i++) {
            if(childS[i].nodeType==1)     
                anyString += childS[i].innerText;   
            else if(childS[i].nodeType==3)   
                anyString += childS[i].nodeValue;   
        }   
        return anyString;   
    }    
    );    
    HTMLElement.prototype.__defineSetter__("innerText",    
    function(sText){   
        this.textContent=sText;      
    }    
    );    
} 

//错误提示 JSON格式
var ErrorDefaultTips = {
    required : "请输入必填项",
    maxlength : "输入项最大长度不能超过",
    minlength : "输入项最小长度不能小于",
    alphanumeric : "输入项必须为字母和数字",
    numeric : "输入项必须为数字",
    alpha : "输入项必须为字母",
    alnumhyphen : "输入项必须为字母、数字、横线、下划线",
    email : "输入项必须为电子邮件格式",
    lessthan : "输入项必须为数字",
    lessthan_two : "输入项必须小于", 
    greaterthan : "输入项必须为数字",
    greaterthan_two : "输入项必须大于",
    regexp : "输入项必须符合自定义格式",
    doselect : "请选择下拉选项",
    telephone : "固定电话格式错误",
    mobile : "手机号码格式错误",
    real : "实数格式错误",
    http : "网址格式错误",
    notEqual : "两次密码输入不相同"  
}

var TextUtility = {};

//标识全部验证是否通过
TextUtility.IsAllValid = 1;

//1、判断输入内容是否为空
TextUtility.IsEmptyString = function(checkObject, errorObject)
{
    var strValue = $(checkObject).value;
    $(errorObject).style.display = "";

	//去除两边空格
    if(strValue.replace(/^\s+|\s+$/g,"").length==0)
    {    
        $(errorObject).innerText = ErrorDefaultTips.required;
        this.IsAllValid = this.IsAllValid & 0;
        return;
    }
    $(errorObject).style.display = "none";
    this.IsAllValid = this.IsAllValid & 1; 
}

//2、检测字符串长度是否大于指定长度
TextUtility.CheckLength = function(checkObject, length, errorObject)
{
    var strErrorMsg = $(checkObject).getAttribute("errormsg");
    var BlankErrorMsg;
    var MaxlengthErrorMsg;
    if(strErrorMsg != null)
    {
        var arrErrorMsg = strErrorMsg.split('|');
        BlankErrorMsg = arrErrorMsg[0];
        MaxlengthErrorMsg = arrErrorMsg[1];    
    }
    else
    {
        MaxlengthErrorMsg = ErrorDefaultTips.maxlength + length;
        BlankErrorMsg = ErrorDefaultTips.required;
    }

    $(errorObject).style.display = "";

    var strValue = $(checkObject).value;
    if( strValue.length == 0 )
    {
        $(errorObject).innerText = BlankErrorMsg;
        this.IsAllValid = this.IsAllValid & 0;
        return;
    }
    if( strValue.length > length)
    {
        $(errorObject).innerText = MaxlengthErrorMsg;
        this.IsAllValid = this.IsAllValid & 0;
        return;
    }
    $(errorObject).style.display = "none";
    this.IsAllValid = this.IsAllValid & 1;
}

//3_1、判断输入的字符串是否为:a-z,A-Z,0-9
TextUtility.IsValidString = function(checkObject, errorObject)
{
    var strValue = $(checkObject).value;
    $(errorObject).style.display = "";

    if(strValue.length!=0)
    {    
        var reg = /^[a-zA-Z0-9]+$/; 
        if(!reg.test(strValue))
        {
            $(errorObject).innerText = ErrorDefaultTips.alphanumeric;
            this.IsAllValid = this.IsAllValid & 0;
            return;
        }
        $(errorObject).style.display = "none";
        this.IsAllValid = this.IsAllValid & 1; 
    }
    else
    {
        $(errorObject).innerText = ErrorDefaultTips.required;
        this.IsAllValid = this.IsAllValid & 0;
        return;
    }
}

//3_2、判断输入的字符串是否为:a-z,A-Z,0-9，而且长度为6到30个字符
TextUtility.IsValidString6_30 = function(checkObject, errorObject)
{
    var strValue = $(checkObject).value;
    $(errorObject).style.display = "";

    if(strValue.length!=0)
    {    
        var reg = /^[a-zA-Z0-9]{6,30}$/; 
        if(!reg.test(strValue))
        {
            $(errorObject).innerText = "输入项格式不对或者长度不符合";
            this.IsAllValid = this.IsAllValid & 0;
            return;
        }
        $(errorObject).style.display = "none";
        this.IsAllValid = this.IsAllValid & 1; 
    }
    else
    {
        $(errorObject).innerText = ErrorDefaultTips.required;
        this.IsAllValid = this.IsAllValid & 0;
        return;
    }
}
//3_3、判断输入的字符串是否为:a-z,A-Z,0-9，而且长度为4到30个字符
TextUtility.IsValidString4_30 = function(checkObject, errorObject)
{
    var strValue = $(checkObject).value;
    $(errorObject).style.display = "";

    if(strValue.length!=0)
    {    
        var reg = /^[a-zA-Z0-9]{4,30}$/; 
        if(!reg.test(strValue))
        {
            $(errorObject).innerText = "输入项格式不对或者长度不符合";
            this.IsAllValid = this.IsAllValid & 0;
            return;
        }
        $(errorObject).style.display = "none";
        this.IsAllValid = this.IsAllValid & 1; 
    }
    else
    {
        $(errorObject).innerText = ErrorDefaultTips.required;
        this.IsAllValid = this.IsAllValid & 0;
        return;
    }
}
//3_4、判断输入的字符串是否为:不包含空格，而且长度为6到30个字符
TextUtility.IsValidStringNoBlank6_30 = function(checkObject, errorObject)
{
    var strValue = $(checkObject).value;
    $(errorObject).style.display = "";

    if(strValue.length!=0)
    {    
        var reg = /[^\'\&quot;\s]{6,30}$/;
        if(!reg.test(strValue))
        {
            $(errorObject).innerText = "输入项格式不对或者长度不符合";
            this.IsAllValid = this.IsAllValid & 0;
            return;
        }
        $(errorObject).style.display = "none";
        this.IsAllValid = this.IsAllValid & 1; 
    }
    else
    {
        $(errorObject).innerText = ErrorDefaultTips.required;
        this.IsAllValid = this.IsAllValid & 0;
        return;
    }
}

//4、判断输入的EMAIL格式是否正确
TextUtility.IsValidEmail = function(checkObject, errorObject)
{
    var strValue = $(checkObject).value;
    $(errorObject).style.display = "";
    
    if(strValue.length!=0)
    {    
        var reg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
        if(!reg.test(strValue)){    
            $(errorObject).innerText = ErrorDefaultTips.email;
            this.IsAllValid = this.IsAllValid & 0;
            return;  
        }
        $(errorObject).style.display = "none";
        this.IsAllValid = this.IsAllValid & 1;      
    } 
    else
    {
        $(errorObject).innerText = ErrorDefaultTips.required;
        this.IsAllValid = this.IsAllValid & 0;
        return;
    }
}

//5、判断输入的字符是否为整数
TextUtility.IsInteger = function(checkObject, errorObject)
{
    var strValue = $(checkObject).value;
    $(errorObject).style.display = "";

    if(strValue.length!=0)
    {    
        var reg = /^[-+]?\d*$/;
        if(!reg.test(strValue))
        {
            $(errorObject).innerText = ErrorDefaultTips.numeric;
            this.IsAllValid = this.IsAllValid & 0;
            return;
        }
        $(errorObject).style.display = "none";
        this.IsAllValid = this.IsAllValid & 1; 
    }
    else
    {
        $(errorObject).innerText = ErrorDefaultTips.required;
        this.IsAllValid = this.IsAllValid & 0;
        return;
    }
} 

//6、判断输入的是否为手机号
TextUtility.IsMobile = function(checkObject, errorObject)
{
    var strValue = $(checkObject).value;
    $(errorObject).style.display = "";

    if(strValue.length!=0)
    {    
        var reg = /^(?:13\d|15[89])-?\d{5}(\d{3}|\*{3})$/;
        if(!reg.test(strValue))
        {
            $(errorObject).innerText = ErrorDefaultTips.mobile;
            this.IsAllValid = this.IsAllValid & 0;
            return;
        }
        $(errorObject).style.display = "none";
        this.IsAllValid = this.IsAllValid & 1; 
    }
    else
    {
        $(errorObject).innerText = ErrorDefaultTips.required;
        this.IsAllValid = this.IsAllValid & 0;
        return;
    }
} 
//7、判断输入的是否为固话
TextUtility.IsTel = function(checkObject, errorObject)
{
    var strValue = $(checkObject).value;
    $(errorObject).style.display = "";

    if(strValue.length!=0)
    {    
        var reg = /^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/;
        if(!reg.test(strValue))
        {
            $(errorObject).innerText = ErrorDefaultTips.telephone;
            this.IsAllValid = this.IsAllValid & 0;
            return;
        }
        $(errorObject).style.display = "none";
        this.IsAllValid = this.IsAllValid & 1; 
    }
    else
    {
        $(errorObject).innerText = ErrorDefaultTips.required;
        this.IsAllValid = this.IsAllValid & 0;
        return;
    }
} 

//8、判断输入的字符是否大于0的整数
TextUtility.IsIntegerLargeThanZero = function(checkObject, errorObject)
{
    var strValue = $(checkObject).value;
    $(errorObject).style.display = "";

    if(strValue.length!=0)
    {    
        var reg = /^[-+]?\d*$/;
        if(!reg.test(strValue))
        {
            $(errorObject).innerText = ErrorDefaultTips.numeric;
            this.IsAllValid = this.IsAllValid & 0;
            return;
        }
        if(parseInt(strValue)<=0)
        {
            $(errorObject).innerText = ErrorDefaultTips.greaterthan_two + 0;
            this.IsAllValid = this.IsAllValid & 0;
            return;
        }
        $(errorObject).style.display = "none";
        this.IsAllValid = this.IsAllValid & 1; 
    }
    else
    {
        $(errorObject).innerText = ErrorDefaultTips.required;
        this.IsAllValid = this.IsAllValid & 0;
        return;
    }
}
//9、判断下拉列表是否选择
TextUtility.IsDropDownListSelected = function(checkObject, errorObject)
{
    $(errorObject).style.display = "";

    if($(checkObject).selectedIndex==0)
    {    
        $(errorObject).innerText = ErrorDefaultTips.doselect;
        this.IsAllValid = this.IsAllValid & 0;
        return;
    }
    else
    {
        $(errorObject).style.display = "none";
        this.IsAllValid = this.IsAllValid & 1; 
    }
} 
//10、判断两个对象的值是否相等，常用于密码确认输入框
TextUtility.IsEqual = function(checkObject1, checkObject2, errorObject)
{
    $(errorObject).style.display = "";

    if($(checkObject1).value!=$(checkObject2).value)
    {    
        $(errorObject).innerText = ErrorDefaultTips.notEqual;
        this.IsAllValid = this.IsAllValid & 0;
        return;
    }
    else
    {
        $(errorObject).style.display = "none";
        this.IsAllValid = this.IsAllValid & 1; 
    }
} 


//======================================

//判断日期类型是否为YYYY-MM-DD格式的类型 
TextUtility.IsValidDate = function(strValue)
{
    if(strValue.length!=0)
    {    
        var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/;     
        var r = strValue.match(reg);     
        if(r==null)    
            alert('对不起，您输入的日期格式不正确!'); //请将“日期”改成你需要验证的属性名称!  
        else alert('日期格式正确!');      
    } 
    else
    {
        alert('请输入日期');
    }
}



//判断输入的邮编(只能为六位)是否正确    
function IsZIP()     
{     
        var str = document.getElementById('str').value.trim();    
        if(str.length!=0){    
        reg=/^\d{6}$/;    
        if(!reg.test(str)){    
            alert("对不起，您输入的字符串类型格式不正确!");//请将“字符串类型”要换成你要验证的那个属性名称！    
        }    
        }    
}     
   
//判断输入的数字不大于某个特定的数字    
function MaxValue()     
{     
    var val = document.getElementById('str').value.trim();    
        if(str.length!=0){    
        reg=/^[-+]?\d*$/;     
        if(!reg.test(str)){//判断是否为数字类型    
            if(val>parseInt('123')) //“123”为自己设定的最大值    
            {     
                alert('对不起，您输入的数字超出范围');//请将“数字”改成你要验证的那个属性名称！    
            }     
        }    
    }    
}     


