﻿function checkValue(oObject)
{   var theValue = oObject.value.replace(/^\s+|\s+$/g,"");//trim
	oObject.value = theValue;

    if (oObject.checkName == "" || oObject.checkName == undefined)
	   oObject.checkName = oObject.name;
	   
    try
    {
		if (oObject.isNotNull == "true" && theValue == "") 
        {
            MsgBox(oObject.checkName+"conld not null!");
            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+"is not DateTime standard!(e.g.: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+"is invalid DateTime format!");
                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+"is not DateTime format!(e.g.: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+"is not Mobile format!(e.g.:13123456789)");
                try{oObject.select();}catch(e){}
	    	     return false;  
           }
           result = theValue.match(reg);
            if(result == null)
            {
			    MsgBox(oObject.checkName+"is not Mobile format!(e.g.: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+"is not email format!(e.g.:admin@changeol.com)");
                try{oObject.select();}catch(e){}
	    	    return false;
            }
        }
        
		if (oObject.checkLen && theValue.length != parseInt(oObject.checkLen)) 
        {
		    MsgBox(oObject.checkName+"length should be "+oObject.checkLen+"characters!");
            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+"length should be"+oObject.checkBLen+"characters!(comment:1Chinese=2English)");
                try{oObject.select();}catch(e){}
	    	    return false;
            }
        }
               
	if (oObject.checkMaxLen && theValue.length > parseInt(oObject.checkMaxLen)) 
        {
		    MsgBox(oObject.checkName+"length could not longer than "+oObject.checkMaxLen+"characters!");
            try{oObject.select();}catch(e){}
    	    return false;
        }
               
	 if (oObject.checkMinLen && theValue.length < parseInt(oObject.checkMinLen)) 
        {
		    MsgBox(oObject.checkName+"length could not shorter than "+oObject.checkMinLen+"characters!");
            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+"length longer than"+oObject.checkMaxBLen+"characters!(comment:1Chi=2Eng)");
                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+"length shorter than "+oObject.checkMinBLen+"characters!(comment:1Chi=2Eng)");
                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+" should be number!");
                try{oObject.select();}catch(e){}
	    	    return false;
    	    }
        	
    	    if (oObject.isPlus == "true" && parseFloat(theValue) <= 0) 
            {
			    MsgBox(oObject.checkName+"should be plus!");
                try{oObject.select();}catch(e){}
    		    return false;
    	    }
        	
    	    if (oObject.isNegative == "true" && parseFloat(theValue) >= 0) 
            {
			    MsgBox(oObject.checkName+"should be negative!");
                try{oObject.select();}catch(e){}
                return false;
    	    }
    	    if (oObject.isNotPlus == "true" && parseFloat(theValue) > 0)  
            {
			    MsgBox(oObject.checkName+"should not be plus!");
                try{oObject.select();}catch(e){}
                return false;
    	    }
        	 	
    	    if (oObject.isNotNegative == "true" && parseFloat(theValue) < 0)  
            {
			    MsgBox(oObject.checkName+"should not be negative!");
                try{oObject.select();}catch(e){}
    	    }
        	
    	    if (oObject.checkIntMaxLen && theValue.split(".")[0].length>oObject.checkIntMaxLen)
            {
			    MsgBox(oObject.checkName+"integer part is too long ,the max length is "+oObject.checkIntMaxLen);
                try{oObject.select();}catch(e){}
	    	    return false;
    	    }
    	    if (oObject.checkIntMinLen && theValue.split(".")[0].length < oObject.checkIntMinLen)
            {
			    MsgBox(oObject.checkName+"integer part is too short ,the min length is "+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+"decimal part is too long ,the max length is "+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+"decimal part is too short ,the min length is "+oObject.checkDecMinLen);
                    try{oObject.select();}catch(e){}
		    	    return false;
    		    }
    	    }
    	    if (oObject.checkMaxNum && parseFloat(theValue)>parseFloat(oObject.checkMaxNum))
            {
			    MsgBox(oObject.checkName+"should greater than "+oObject.checkMaxNum);
                try{oObject.select();}catch(e){}
	    	    return false;
    	    }
        	
    	    if (oObject.checkMinNum && parseFloat(theValue)<parseFloat(oObject.checkMinNum))
            {
			    MsgBox(oObject.checkName+"should less than "+oObject.checkMinNum);
                try{oObject.select();}catch(e){}
	    	    return false;
    	    }
    	    
    	    if (oObject.checkInt)
            {
                if(isNaN(oObject.checkInt -1)) 
                {
                    MsgBox("checkName is "+oObject.checkName+" control's checkInt attribute should be puls integer!");
                    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+"must be integer,and between "+ maxInt +" with "+ minInt +"!");
                    try{oObject.select();}catch(e){}
	                return false;
                }
            }
    	    if (oObject.checkPlusInt)
            {
                if(isNaN(oObject.checkPlusInt -1)) 
                {
                    MsgBox("checkName is "+oObject.checkName+"control's checkPlusInt attribute should be puls integer!");
                    return false;
                }
                var maxInt=Math.pow(2,oObject.checkPlusInt - 1) - 1;
                if(theValue.indexOf(".") != -1 || (theValue > maxInt)|| (theValue <=0)) 
                {
				    MsgBox(oObject.checkName+"should be puls integer!and less than "+ maxInt);
                    try{oObject.select();}catch(e){}
		    	    return false;
    		    }
    	    }
    	    if (oObject.checkNegativeInt)
            {
                if(isNaN(oObject.checkNegativeInt -1)) 
                {
                    MsgBox("checkName is "+oObject.checkName+"control's checkNegativeInt attribute should be puls integer!");
                    return false;
                }
                var maxInt=Math.pow(2,oObject.checkNegativeInt - 1);
                if(theValue.indexOf(".") != -1 || (theValue < -maxInt) || (theValue >=0)) 
                {
				    MsgBox(oObject.checkName+"value should be negative integer!and greater than " + -maxInt);
                    try{oObject.select();}catch(e){}
		    	    return false;
    		    }
    	    }
        }
        
        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+"character type has not been validated!");
                else
                    MsgBox(oObject.checkName+oObject.checkFormatMsg);
                try{oObject.select();}catch(e){}
	    	    return false;
            }
        }
        return true;
    }   
    catch(e)
    {
        alert("checkValue "+e);
        return false;
    }    
}

function checkAllValue(oObject) 
{    
    var strErrorInfo="checkAllValue input parameter should be controls,include form,table etc!";
    try
    {
        if(typeof(oObject)!="object") 
        {
            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() error.error message:"+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.replace(/\s/g, "");
    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.parentElement;

	if(gotoid!=null&&gotoid!="")
	{
		while(parentObj!=null)
		{
			if(parentObj.id!=""&&parentObj.id==gotoid)
			{
				break;
			}
			else{parentObj=parentObj.parentElement;}
		}
	}
	else
	{
		while(parentObj!=null)
		{
			if((parentObj.tagName=="DIV"||parentObj.tagName=="div") && parentObj.className=="section")
			{
				break;
			}
			else{parentObj=parentObj.parentElement;}
		}
	}
    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")
 		{return "("+previousobj.nodeValue+")";}
		else
		{
			return getParentElementText(obj);
		}
   }
   else
   {
		return getParentElementText(obj);
   }
   return "";
}
function getParentElementText(obj)
{	
	var parentobj=obj.parentElement;
	while(parentobj!=null)
	{
		var parentobjPreviousSli=parentobj.previousSibling;
		while(parentobjPreviousSli!=null)
		{
			if(parentobjPreviousSli.innerText!="")
			{
				return "("+parentobjPreviousSli.innerText+")";
			}
			else{parentobjPreviousSli=parentobjPreviousSli.previousSibling;}
		}
		parentobj=parentobj.parentElement;
	}
	return "";
}
//end               