﻿function checkValue(oObject)
{
	//过滤空格并回填    
	var theValue = oObject.value.replace(/^\s+|\s+$/g,"");//trim
	oObject.value = theValue;
    
    //警告类，目前暂使用alert
    //if(MsgBox == null || MsgBox == undefined)
    //{
    //    MsgBox = new MessageBox();
    //}
    
    //如果未设checkName，那么直接取name
    if (oObject.checkName == "" || oObject.checkName == undefined)
	   oObject.checkName = oObject.name;
	   
    try
    {
		if (oObject.isNotNull == "true" && theValue == "") 
        {
            MsgBox(oObject.checkName+"不能为空！");
            try{oObject.select();}catch(e){}
	        return false;
        }
        if (oObject.isDate == "true" && theValue != "") 
        {
            var reg = /^(\d{4})(-)(\d{1,2})\2(\d{1,2})$/;
            result = theValue.match(reg);
            if(result == null)
            {
			    MsgBox(oObject.checkName+"不是标准的日期格式！(例：yyyy-mm-dd)");
                try{oObject.select();}catch(e){}
	    	    return false;
            }
            
            var d = new Date(result[1],result[3]-1,result[4])
            if(result[1]!=d.getFullYear() || result[3]!=d.getMonth()+1 || result[4]!=d.getDate())
            {
			    MsgBox(oObject.checkName+"是无效的日期格式！");
                try{oObject.select();}catch(e){}
	    	    return false;
            }
        }
        if (oObject.isTime == "true" && theValue != "") 
        {
           var reg = /^(\d{4})(-)(\d{1,2})\2(\d{1,2}) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$/;
            result = theValue.match(reg);
            if(result == null)
            {
			    MsgBox(oObject.checkName+"不是标准的时间格式！(例：yyyy-mm-dd hh:mm:ss)");
                try{oObject.select();}catch(e){}
	    	    return false;
            }
        }
        if (oObject.isMobile == "true" && theValue != "") 
        {
           var reg ;
           if(theValue.substring(0,1) = "1") 
                reg = /^1[3|5][0-9]{9}$/;
           else if(theValue.substring(0,1) = "0") 
               reg = /^0[0-9]{9,11}$/;
           else
           {
                MsgBox(oObject.checkName+"不是标准手机号码格式！(例：13123456789)");
                try{oObject.select();}catch(e){}
	    	     return false;  
           }
           result = theValue.match(reg);
            if(result == null)
            {
			    MsgBox(oObject.checkName+"不是标准手机号码格式！(例：13123456789)");
                try{oObject.select();}catch(e){}
	    	     return false;
            }
        }        
        if (oObject.isEmail == "true" && theValue != "") 
        {
            var reg = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
            result = theValue.match(reg);
            if(result == null)
            {
			    MsgBox(oObject.checkName+"不是标准的Email格式！(例：admin@changeol.com)");
                try{oObject.select();}catch(e){}
	    	    return false;
            }
        }
        
		//验证长度
		if (oObject.checkLen && theValue.length != parseInt(oObject.checkLen)) 
        {
		    MsgBox(oObject.checkName+"长度必须为"+oObject.checkLen+"个字符！");
            try{oObject.select();}catch(e){}
    	    return false;
        }
        
        //验证长度，处理中英文
        if (oObject.checkBLen) 
        {
		    var iLen = 0;
            var tmpValue = theValue;
            for(var j=0; j<tmpValue.length; j++)
            {
                iLen += (tmpValue.charCodeAt(j) > 127) ? 2 : 1;
            }
            if(iLen != parseInt(oObject.checkBLen))
            {
			    MsgBox(oObject.checkName+"长度必须为"+oObject.checkBLen+"个字符！(注:1中文字符=2英文字符)");
                try{oObject.select();}catch(e){}
	    	    return false;
            }
        }
        
        //验证最长        
	if (oObject.checkMaxLen && theValue.length > parseInt(oObject.checkMaxLen)) 
        {
		    MsgBox(oObject.checkName+"长度不能超出"+oObject.checkMaxLen+"个字符！");
            try{oObject.select();}catch(e){}
    	    return false;
        }
        
        //验证最短       
	 if (oObject.checkMinLen && theValue.length < parseInt(oObject.checkMinLen)) 
        {
		    MsgBox(oObject.checkName+"长度不能少于"+oObject.checkMinLen+"个字符！");
            try{oObject.select();}catch(e){}
    	    return false;
        }
        
        //验证最长，处理中英文       
	 if (oObject.checkMaxBLen) 
        {
            var iLen = 0;
            var tmpValue = theValue;
            for(var j=0; j<tmpValue.length; j++)
            {
                iLen += (tmpValue.charCodeAt(j) > 127) ? 2 : 1;
            }
            if(iLen > parseInt(oObject.checkMaxBLen))
            {
			    MsgBox(oObject.checkName+"长度超出"+oObject.checkMaxBLen+"个字符！(注:1中文字符=2英文字符)");
                try{oObject.select();}catch(e){}
	    	    return false;
            }
        }
        
        //验证最短，处理中英文       
	 if (oObject.checkMinBLen) 
        {
            var iLen = 0;
            var tmpValue = theValue;
            for(var j=0; j<tmpValue.length; j++)
            {
                iLen += (tmpValue.charCodeAt(j) > 127) ? 2 : 1;
            }
            if(iLen < parseInt(oObject.checkMinBLen))
            {
			    MsgBox(oObject.checkName+"长度小于"+oObject.checkMinBLen+"个字符！(注:1中文字符=2英文字符)");
                try{oObject.select();}catch(e){}
	    	    return false;
            }
        }
        ///////////////////////////////////////////////////////////////
        //数字验证内容
        if (oObject.isNumber == "true") 
        {
    	    if (isNaN(theValue) || theValue.indexOf("E")!=-1 || theValue.indexOf("e")!=-1)
            {
			    MsgBox(oObject.checkName+"的值必须为数字！");
                try{oObject.select();}catch(e){}
	    	    return false;
    	    }
        	
    	    if (oObject.isPlus == "true" && parseFloat(theValue) <= 0) 
            {
			    MsgBox(oObject.checkName+"的值必须为正数！");
                try{oObject.select();}catch(e){}
    		    return false;
    	    }
        	
    	    if (oObject.isNegative == "true" && parseFloat(theValue) >= 0) 
            {
			    MsgBox(oObject.checkName+"的值必须为负数！");
                try{oObject.select();}catch(e){}
                return false;
    	    }
    	    if (oObject.isNotPlus == "true" && parseFloat(theValue) > 0) //非正 
            {
			    MsgBox(oObject.checkName+"的值必须为非正数！");
                try{oObject.select();}catch(e){}
                return false;
    	    }
        	 	
    	    if (oObject.isNotNegative == "true" && parseFloat(theValue) < 0) //非负 
            {
			    MsgBox(oObject.checkName+"的值必须为非负数！");
                try{oObject.select();}catch(e){}
    	    }
        	
    	    if (oObject.checkIntMaxLen && theValue.split(".")[0].length>oObject.checkIntMaxLen)
            {
			    MsgBox(oObject.checkName+"整数部分超长，其最大长度为 "+oObject.checkIntMaxLen);
                try{oObject.select();}catch(e){}
	    	    return false;
    	    }
    	    if (oObject.checkIntMinLen && theValue.split(".")[0].length < oObject.checkIntMinLen)
            {
			    MsgBox(oObject.checkName+"整数部分过短，其最小长度为 "+oObject.checkIntMinLen);
                try{oObject.select();}catch(e){}
	    	    return false;
    	    }
    	    if (oObject.checkDecMaxLen && theValue.split(".").length>1)
            {
    		    if(theValue.split(".")[1].length>oObject.checkDecMaxLen)
                {
				    MsgBox(oObject.checkName+"小数部分超长，其最大长度为 "+oObject.checkDecMaxLen);
                    try{oObject.select();}catch(e){}
		    	    return false;
    		    }
    	    }
        	
    	    if (oObject.checkDecMinLen && theValue.split(".").length>1)
            {
    		    if(theValue.split(".")[1].length < oObject.checkDecMinLen)
                {
				    MsgBox(oObject.checkName+"小数部分过短，其最小长度为 "+oObject.checkDecMinLen);
                    try{oObject.select();}catch(e){}
		    	    return false;
    		    }
    	    }
    	    if (oObject.checkMaxNum && parseFloat(theValue)>parseFloat(oObject.checkMaxNum))
            {
			    MsgBox(oObject.checkName+"值不能大于 "+oObject.checkMaxNum);
                try{oObject.select();}catch(e){}
	    	    return false;
    	    }
        	
    	    if (oObject.checkMinNum && parseFloat(theValue)<parseFloat(oObject.checkMinNum))
            {
			    MsgBox(oObject.checkName+"值不能小于 "+oObject.checkMinNum);
                try{oObject.select();}catch(e){}
	    	    return false;
    	    }
    	    
    	    if (oObject.checkInt)
            {
                if(isNaN(oObject.checkInt -1)) 
                {
                    MsgBox("checkName为"+oObject.checkName+"的控件的checkInt属性必须为正整数！");
                    try{oObject.select();}catch(e){}
                    return false;
                }
                var maxInt=Math.pow(2,oObject.checkInt - 1) - 1;
                var minInt = Math.pow(-2,oObject.checkInt - 1);
                if(theValue.indexOf(".") != -1 || (theValue > maxInt || theValue < minInt)) 
                {
	                MsgBox(oObject.checkName+"的值必须为整数，且介于 "+ maxInt +" 与 "+ minInt +"之间！");
                    try{oObject.select();}catch(e){}
	                return false;
                }
            }
    	    if (oObject.checkPlusInt)//正整数最大值(2^n-1)-1
            {
                if(isNaN(oObject.checkPlusInt -1)) 
                {
                    MsgBox("checkName为"+oObject.checkName+"的控件的checkPlusInt属性必须为正整数！");
                    return false;
                }
                var maxInt=Math.pow(2,oObject.checkPlusInt - 1) - 1;
                if(theValue.indexOf(".") != -1 || (theValue > maxInt)|| (theValue <=0)) 
                {
				    MsgBox(oObject.checkName+"的值必须为正整数！且小于 "+ maxInt);
                    try{oObject.select();}catch(e){}
		    	    return false;
    		    }
    	    }
    	    if (oObject.checkNegativeInt)//负整数，最小值(-2^n-1)
            {
                if(isNaN(oObject.checkNegativeInt -1)) 
                {
                    MsgBox("checkName为"+oObject.checkName+"的控件的checkNegativeInt属性必须为正整数！");
                    return false;
                }
                var maxInt=Math.pow(2,oObject.checkNegativeInt - 1);
                if(theValue.indexOf(".") != -1 || (theValue < -maxInt) || (theValue >=0)) 
                {
				    MsgBox(oObject.checkName+"的值必须为负整数！且大于 " + -maxInt);
                    try{oObject.select();}catch(e){}
		    	    return false;
    		    }
    	    }
        }//End of oObject.isNumber
        
        if (oObject.checkFormat && theValue !="") 
        {
    	    var re = oObject.checkFormat;
    	    var ss = new RegExp(re,"g");
    	    if (!ss.test(theValue))
            {
			    if (oObject.checkFormatMsg==undefined || oObject.checkFormatMsg=="")
                    MsgBox(oObject.checkName+"的字符模式不匹配！");
                else
                    MsgBox(oObject.checkName+oObject.checkFormatMsg);
                try{oObject.select();}catch(e){}
	    	    return false;
            }
        }
        //以上验证通过返回true
        return true;
    }   
    catch(e)
    {
        alert("checkValue "+e);
        return false;
    }    
}
function checkAllValue(oObject) 
{    
    var strErrorInfo="checkAllValue 传入参数必须是容器，包括form、table等！";
    try
    {
        if(typeof(oObject)!="object")//如果传入对象的类型不是对象或者对象的tagName不存在 
        {
            alert(strErrorInfo);
            return false;
        }
	    for (var i=0;i<oObject.all.length;i++) 
        {
            var theObject = oObject.all[i];
            if(theObject.tagName=="INPUT" || theObject.tagName=="TEXTAREA" || theObject.tagName=="SELECT")
            {
                var E = checkValue(theObject)
                if (E != true) return E;
            }
        }
	    return true;
    }
    catch(e)
    {
        alert("checkAllValue "+e);
        return false;
    }
    
}
function MsgBox(_sMsg)
{
    try
    {
        alert(_sMsg);
    }
    catch(e)
    {
        alert("MessageBox.show() 错误。错误信息："+e);
    }
}
//add by hexj   show the length of input texts and set max length     begin
function showNumber(obj, maxlength)
{
    var objid = obj.id;
    var show_num = $(obj.id+"show_write_sum");
    obj.title = " <= " + maxlength;
    if(!show_num)
    {
        var span = document.createElement("span");
        span.innerText = LimitNum + ":" + maxlength;
        span.id = obj.id+"show_write_sum";
        span.style.color = "black";
        obj.parentNode.appendChild(span);
    }
    else
    {
        if(obj.value.length > maxlength)
        {
            obj.value = obj.value.substring(0, maxlength);
        }
        else
        {
            show_num.innerHTML = "<font color=black>" + LimitNum + ":<b>" + parseInt(maxlength - obj.value.length) + "</b></font>";
        }
    }
}
//end
//add by hexj check is Int
function checkIsInt(value, notvalue)
{
    var newvalue = value;
    if(newvalue.match(/^[0-9]+$/) == newvalue)
    { 
	if(notvalue != null)
	{
	   if(newvalue == notvalue)
	   {
		return false;
           }
	}
	return true; 
    }
    else 
	return false;
}
//end
//don't display msg hexj 2007-11-24
function checkMsgShow(id)
{
    try
    {
        document.getElementById(id).style.display = "none";
        clearInterval(__msgIntervalTime_hexj);
    }catch(e){}
}
//end
//check the contains of section input element beg   hexj 2007/12/11
//the [id] is button's id. because every section while contain a div with classname=section
//the [gotoid] is end parent obj
//need modify - -.
function checkAllInputValues(id,gotoid)
{
    var fromObj=document.getElementById(id);
	var parentObj=fromObj.parentNode;

	if(gotoid!=null&&gotoid!="")
	{
		while(parentObj!=null)
		{
			if(parentObj.id!=""&&parentObj.id==gotoid)
			{
				break;
			}
			else{parentObj=parentObj.parentNode;}
		}
	}
	else
	{
		while(parentObj!=null)
		{
			if((parentObj.tagName=="DIV"||parentObj.tagName=="div") && parentObj.className=="section")
			{
				
				break;
			}
			else{parentObj=parentObj.parentNode;}
		}
	}
    if(parentObj==null)return false;
	
    var inputtexts=parentObj.getElementsByTagName("input");
    for(var i=0;i<inputtexts.length;i++)
    {
        var c=inputtexts[i];
        if(c.type=="text" || c.type=="textarea"  || c.type=="password")
        {
            if(c.getAttribute("isnotnull")=="")
            {
                if(c.value.replace(/\s/g,"")=="")
                {
					var txt=GetSiblingText(c);
                    return alertMsg(txt+NotNull, function(){c.style.backgroundColor="#FFE4E4";c.focus();});
                }
            }
            if(c.getAttribute("isnumber")=="")
            {
                if(isNaN(c.value))
                {
					var txt=GetSiblingText(c);
                    return alertMsg(txt+NotNumTip,function(){c.style.backgroundColor="#FFE4E4";c.focus();});
                }
            }
            if(c.getAttribute("isint")=="")
            {
                if(c.value.match(/^[0-9]+$/) != c.value)
                {
					var txt=GetSiblingText(c);
					return alertMsg(txt+IsInt, function(){c.style.backgroundColor="#FFE4E4";c.focus();});}
            	}
            }
    }
    var selects=parentObj.getElementsByTagName("select");
	for(var j=0;j<selects.length;j++)
	{ 
		var c = selects[j];
	   	if(c.getAttribute("isnotnull")=="")
           	{
		if(c.value.replace(/\s/g,"")=="")
		{
			var tip="";
			if(c.getAttribute("tip")=="")
			{tip=c.tip;}
			else {tip=NotNull;}
			var itemtext=GetSiblingText(c);
			return alertMsg(itemtext+tip,function(){c.style.backgroundColor="#FFE4E4";c.focus();});
		}
	   	}
	}
    return true;
}

function GetSiblingText(obj)
{
   var previousobj=obj.previousSibling;
   if(previousobj!=null)
   { 
	   if(typeof(previousobj.tagName)=="undefined" && previousobj.nodeValue.replace(/\s*/g, "")!="")
 		{return "【"+previousobj.nodeValue+"】";}
		else
		{
			return getParentElementText(obj);
		}
   }
   else
   {
		return getParentElementText(obj);
   }
   return "";
}
function getParentElementText(obj)
{	
	var parentobj=obj.parentNode;
	while(parentobj!=null)
	{
		var parentobjPreviousSli=parentobj.previousSibling;
		while(parentobjPreviousSli!=null)
		{
			var innertext=getInnerText(parentobjPreviousSli).Trim();
			if(innertext!="")
			{
				return "【"+innertext+"】";
			}
			else{parentobjPreviousSli=parentobjPreviousSli.previousSibling;}
		}
		parentobj=parentobj.parentNode;
	}
	return "";
}
//end
/*delete all space*/
String.prototype.Trim = function()
{
	return this.replace(/\s*/g, "");
}
/*get innertext of element .*/
function getInnerText(obj)
{
	return window.ActiveXObject?obj.innerText:obj.textContent;    
}
//check grid is empty
function checkGridIsEmpty(gridId,tipMsg)
{
	var obj=document.getElementById(gridId);
	if(obj!=null && obj.rows.length<=1)
	{ 
		if(typeof tipMsg!='undefined') return alertMsg(tipMsg);
		else return alertMsg(gridEmptyText);
	}
	else {return true;}
}
/*products title ellisText .if more than length , use "...".
note:now, only use in product.
*/
function ellisText(id, length)
{
	try
	{
		var obj = document.getElementById(id);
		if(obj==null) return;
		for(var i=0; i < obj.rows.length * 2; i++)
		{
			var ccid = id + '_ctl';
			if(i < 10)
			{
				ccid += '0';
			}
			var cc= document.getElementById(ccid + i + '_lnkShowProductDetail');
			var content=cc.innerHTML.replace(/^\s*|\s*$/g, "");
			if(content.length > length)
			{
				cc.title=content;
				cc.innerHTML = content.substring(0,length) + "...";
			}
		}
	}catch(e){}
}
/*fix event*/
function fixE(e)
{
	if(typeof e=='undefined') {e=window.event;}
	return e;
}
/*article click count*/
function clickCount(articleId) {
    var parameters="articleId="+articleId;
    var myoption={method:"get",parameters:parameters};
    //new Ajax.Request("/wsp/WdServiceForSelf.asmx/UpdateArticleClickCount", myoption);
    new Ajax.Request(WSPCore.siteUrl + "/WdServiceForSelf.asmx/UpdateArticleClickCount", myoption);
}
/*can send SBC case number*/
function getNumber(num,blallowzero)
{
	var newnum="";
	if(num.Trim().length==0){return '';}
	if(blallowzero)
	{
		var re = new RegExp("(0|０)\.?(０|0)*").exec(num);   
		if(re!=null&&re.toString().split(',')[0]==num)
		{return 0;}
	}
	for(var i=0;i<num.length; i++)
	{
		var charitem=num.charCodeAt(i);
		if(charitem>=65296&&charitem<=65305)
		{
			if(!(i==0&&charitem==65296))
			newnum=newnum+String.fromCharCode(charitem-65248);
		}
		else if(charitem>=48&&charitem<=57)
		{
			if(!(i==0&&charitem==48))
			newnum=newnum+String.fromCharCode(num.charCodeAt(i));
		}
	}
	return newnum;
}
function checkInputLength(obj)
{
	//alert(obj.id);
	var newvalue="";
	var len=0;
	var arrvalue=obj.value.toArray();
	var length=arrvalue.length;
	//debugger;
	for(var i=0;i<length;i++)
	{
		if(escape(arrvalue[i].charAt(0)).length>4)
		{
			len=len+2;
		}
		else
		{
			len++;
		}
				if(len<obj.maxLength)
		{
			newvalue=newvalue+arrvalue[i];
		}
	}
	obj.value=newvalue;
}

function initTextBoxEvent(obj)
{
	var inputElements=obj.getElementsByTagName("input");
	var len=inputElements.length;
	for(var i=0;i<len;i++)
	{
		var el=inputElements[i];
		
		if(el.type=="text"&&el.maxLength!=2147483647)
		{
			el.onkeyup=function(){checkInputLength(this);}
			el.onkeydown=function(){checkInputLength(this);}
		}
	}
}
