/**
* trim left space
*/
function lTrim(str)
{
    var whitespace = new String(" \t\n\r");
    var s = new String(str);
    if (whitespace.indexOf(s.charAt(0)) != -1)
    {
        var j = 0, i = s.length;
        while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
        {
            j++;
        }
        s = s.substring(j, i);
    }

    return s;
}

/**
 * trim right space
 */
function rTrim(str)
{
    var whitespace = new String(" \t\n\r");
    var s = new String(str);

    if (whitespace.indexOf(s.charAt(s.length-1)) != -1)
    {
        var i = s.length - 1;
        while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
        {
            i--;
        }
        s = s.substring(0, i+1);
    }

    return s;
}

/**
 * check if has space
 */
function checkSpace(str)
{
    var whitespace = new String(" \t\n\r\\?@#$!&*<>=\/~^()|");
    var s = new String(str);
    var i = s.length - 1;
    while (i >= 0)
    {
        if(whitespace.indexOf(s.charAt(i)) != -1){
            return false;
        }
        i--;
    }


    return true;
}


/**
 * trim space
 */
function trim(str)
{
    return rTrim(lTrim(str));
}

/**
 * check str
 */
function isValid(msg)
{
    var str = trim(msg);
	if(str == null || str.length == 0){
		return false;
	}
	return true;
}

/**
 * check string whether empty
 */
function checkEmpty(msg)
{
    var str = trim(msg);
	if(str == null || str.length == 0){
		return false;
	}
	return true;
}

/**
 * check fielid
 */
function checkFieldsEmpty(fields)
{
	for(var i = 0;i < fields.length;i++){
		if(checkEmpty(fields[i]) == false){
			return false;
		}
	}
	return true;
}

/**
 * check the str whether digit
 */
function checkDigit(numStr)
{
	if(!isValid(numStr)){
		return false;
	}

	if(isNaN(numStr)){
		return false;
	}
  	return true;
}

/**
 * check num range
 */
function checkNumRange(numStr,minNum,maxNum)
{
	if(!checkDigit(numStr)){
		return false;
	}
	var num = parseFloat(numStr);
	if(num < minNum || num > maxNum){
		return false;
	}else{
		return true;
	}
}

/**
 * checkj string length
 */
function checkLen(str,minLen,maxLen)
{
	if(!isValid(str)){
		return false;
	}

	var len = str.length;
	if(len < minLen || len > maxLen){
		return false;
	}else{
		return true;
	}
}

function ipcheck(ips,sep)
{
   ips=ips.split(sep);
   for(var i=0;i<ips.length;i++){
     if(!checkEmpty(ips[i])){
       return false;
     }else if(!isip(ips[i])){
       return false;
     }
   }
   return true;
}

function isip(s)
{
  var check=function(v){try{return ((v<=255 && v>=0) || v=='*')}catch(x){return false}};
  var re=s.split(".")
  if(re.length==4){
    if(check(re[0]) && check(re[1]) && check(re[2]) && check(re[3]))
       return true;
    else return false;
  }else return false;
}

/**
* check the email format
*/
function checkEmail(strEmail)
{
	if (!isValid(strEmail)){
    	return false;
	}

	if (strEmail.search(/^\w+((-\w+)|(\.\w+))*\@([A-Za-z0-9]+\.)+[A-Za-z0-9]+$/) != -1){
  		return true;
	}else{
		 return false;
	}
}


function checkLowercase(strValue)
{
	var inculde = "abcdefghijklmnopqrstuvwxyz";
	var valid = checkInclude(strValue,inculde);
	return valid;
}

function checkUppercase(strValue)
{
	var inculde = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var valid = checkInclude(strValue,inculde)
	return valid;
}

/**
 *
 * check the string whether inlcude the giving string
 */
function checkInclude(strValue,include)
{
	if (!isValid(strValue)){
    	return false;
	}
	var i,j;
    for (i = 0; i < strValue.length; i++)
    {
       j=include.indexOf(strValue.charAt(i));
       if(j == -1)
       {
         return false;
       }
    }
    return true;
}

/**
 * check date format
 */
function checkDate(datestr)
{
	var lthdatestr
 	if (datestr != ""){
  		lthdatestr= datestr.length ;
	}else{
  		lthdatestr=0;
	}

 	var tmpy="";
 	var tmpm="";
 	var tmpd="";
 	//var datestr;
 	var status;
 	status=0;
 	if (lthdatestr == 0 ){
  		return false;
	}

 	for (i = 0;i < lthdatestr;i++)
 	{
		if (datestr.charAt(i)== '-')
  		{
   			status++;
  		}
  		if (status > 2)
  		{
   			return false;
  		}

  		if ((status == 0) && (datestr.charAt(i) != '-'))
  		{
		   tmpy=tmpy+datestr.charAt(i)
  		}
		if ((status == 1) && (datestr.charAt(i) != '-'))
  		{
			tmpm=tmpm+datestr.charAt(i)
  		}

  		if ((status == 2) && (datestr.charAt(i) != '-'))
  		{
			tmpd=tmpd+datestr.charAt(i)
		}
	}
	var year=new String (tmpy);
	var month=new String (tmpm);
	var day=new String (tmpd);

 	if ((tmpy.length!=4) || (tmpm.length>2) || (tmpd.length>2))
 	{
  		return false;
 	}
 	if (!((1<=month) && (12>=month) && (31>=day) && (1<=day)) )
 	{
  		return false;
 	}
 	if (!((year % 4)==0) && (month==2) && (day==29))
 	{
  		//alert ("This is not a leap year!");
  		return false;
 	}
 	if ((month<=7) && ((month % 2)==0) && (day>=31))
 	{
  		//alert ("This month is a small month!");
  		return false;
 	}
 	if ((month>=8) && ((month % 2)==1) && (day>=31))
 	{
  		//alert ("This month is a small month!");
  		return false;
 	}
 	if ((month==2) && (day==30))
 	{
  		//alert("The Febryary never has this day!");
  		return false;
 	}

 	return true;
}

/**
 * disable submit button
 */
function disableForm(theform)
{
	if (document.all || document.getElementById) {
		for (i = 0; i < theform.length; i++) {
			var tempobj = theform.elements[i];
			if (tempobj.type.toLowerCase() == "submit" || tempobj.type.toLowerCase() == "reset"){
				tempobj.disabled = true;
			}
		}
		//setTimeout('alert("Your form has been submitted.  Notice how the submit and reset buttons were disabled upon submission.")', 2000);
		return true;
	}else {
		alert("The form has been submitted.  But, since you're not using IE 4+ or NS 6, the submit button was not disabled on form submission.");
		return false;
   }
}

/**
*
*  check browser
* b browser name
* v version
* if browser match and version great than the giving version,return true
*/
function isBrowser(b,v)
{
    browserOk = false;
    versionOk = false;

    browserOk = (navigator.appName.indexOf(b) != -1);
    if (v == 0) {
        versionOk = true;
    }else{
         versionOk = (v <= parseInt(navigator.appVersion));
    }
    return browserOk && versionOk;
}

/**
*
*/
function isChar(str)
{
    if (str.search(/^[A-Za-z0-9]+$/) != -1){
        return true;
    }else{
         return false;
    }
}



