
/*
	函数名称：trim
	函数功能: 去除字符串头部和尾部的空格
	传入参数：字符串变量
	传出结果：处理后的子串
*/
function trim(str){
	return str.replace(/(^\s*)|(\s*$)/g, "");
}

function strlen(str)
{
    var len;
    var i;
    len = 0;
    for (i=0;i<str.length;i++)
    {
        if (str.charCodeAt(i)>160) len += 2; 
        else len++;
    }
    return len;
}

function isChn(d) {
 actlen=d.length;  
 for(i=0;i<d.length;i++)
 if (d.substr(i,1)>"~")
    actlen+=1;
 if( actlen>d.length )
   return true;
 return false;
}


function isDate(strDate){
	var regYear = /\d{4}[-/]/g;	//year pattern
	var regMonth;			
	var regDay =  /\d{1,2}/g;;
	var chrSeperator;
	var arr,str;
	if ((arr = regYear.exec(strDate)) == null) 
		return false;
	var intYearlen = arr.lastIndex - arr.index - 1;
	if (arr.index != 0 || (intYearlen != 4 && intYearlen != 2))
		return false;
	str = arr[0];
	chrSeperator = str.charAt(str.length - 1);  // get the seperator ('-' or '/') 
	intYear = parseInt(str.substr(0, str.length - 1));	// get the year
	if (intYear < 1900 || intYear > 2099)  //Error Year
		return false;

	strDate = strDate.substr(arr.lastIndex);
	if (chrSeperator == "-")
		regMonth = /\d{1,2}[-]/g;
	else
		regMonth = /\d{1,2}[/]/g;
	if ((arr = regMonth.exec(strDate)) == null) 
		return false;
	if (arr.index != 0)
		return false;
	str = arr[0];
	if (str.charAt(0) == '0') {
		intMonth = parseInt(str.substr(1, str.length - 2)); // get the month
	} else {
		intMonth = parseInt(str.substr(0, str.length - 1)); // get the month
	}
	if (intMonth < 1 || intMonth > 12) //Error Month
		return false;

	strDate = strDate.substr(arr.lastIndex);

	if ((arr = regDay.exec(strDate)) == null) 
		return false;
	if (arr.index != 0 || arr.lastIndex != strDate.length)
		return false;
	str = arr[0];
	if (str.charAt(0) == '0') {
		intDay = parseInt(str.substr(1, str.length - 1)); // get the day
	} else {
		intDay = parseInt(str); // get the day
	}	
	if (intDay < 1 || intDay > 31)  //Error Day
		return false;

	datDate = new Date(intYear, intMonth - 1, intDay); //Test the Date
	if (isNaN(datDate))  //Error Date Format
		return false;
	if (datDate.getMonth() != intMonth - 1 || datDate.getDate() != intDay)  //invalid date such as '1999/02/29' and '1999/04/31'
		return false;
	return datDate;  //Return the Date in parsed format
}

function isBirthDate(d) {
	var first,second,yy,mm,dd;
	var today = new Date();
	if(d.indexOf("/")!=-1)
	{
		first=d.indexOf("/");
		second=d.lastIndexOf("/");
		if(second==first) return false;
		yy=parseInt(d.substring(0,first));
		if ( d.substr(first + 1, 1) == '0' )
			mm=parseInt(d.substring(first+2,second));
		else
			mm=parseInt(d.substring(first+1,second));
		if ( d.substr(second + 1, 1) == '0' )
			dd=parseInt(d.substring(second+2,d.length));
		else
			dd=parseInt(d.substring(second+1,d.length));
		//alert("yy="+yy);
		//alert("mm="+mm);
		//alert("dd="+dd);
		if (isNaN(yy)) { //Error Year Format
			return false;			
		}
		if (yy<30) 
			yy += 2000;
		else if (yy <100 && yy >= 30)
			yy += 1900;
		if( yy < 1900 || yy>2069) return false;
		if (isNaN(mm) || mm < 1 || mm > 12) { //Error Month Format
			return false;
		}
		if (isNaN(dd) || dd < 1 || dd > 31) { //Error Month Format
			return false;
		}
		d1 = new Date(yy, mm - 1, dd); //Test the Date
		if (isNaN(d)) { //Error Date Format
			return false;
		}
		if (d.getMonth() != mm - 1 || d.getDate() != dd) { //invalid date such as '1999/02/29' and '1999/04/31'
			return false;
		}
		if ( yy + 16 > today.getFullYear() ) return false;
		return d.toLocaleString();  //Return the Date in parsed format
	}
	else if(d.indexOf("-")!=-1)
	{		
		first=d.indexOf("-");
		second=d.lastIndexOf("-");
		if(second==first) return false;
		yy=parseInt(d.substring(0,first));
		if ( d.substr(first + 1, 1) == '0' )
			mm=parseInt(d.substring(first+2,second));
		else
			mm=parseInt(d.substring(first+1,second));
		if ( d.substr(second + 1, 1) == '0' )
			dd=parseInt(d.substring(second+2,d.length));
		else
			dd=parseInt(d.substring(second+1,d.length));
		if (isNaN(yy)) { //Error Year Format
			return false;			
		}
		if (yy<30) 
			yy += 2000;
		else if (yy <100 && yy >= 30)
			yy += 1900;
		if( yy < 1950 || yy>2069) return false;
		if (isNaN(mm) || mm < 1 || mm > 12) { //Error Month Format
			return false;
		}
		if (isNaN(dd) || dd < 1 || dd > 31) { //Error Month Format
			return false;
		}
		d = new Date(yy, mm - 1, dd); //Test the Date
		if (isNaN(d)) { //Error Date Format
			return false;
		}
		if (d.getMonth() != mm - 1 || d.getDate() != dd) { //invalid date such as '1999/02/29' and '1999/04/31'
			return false;
		}
		if ( yy + 16 > today.getFullYear() ) return false;
		return d.toLocaleString();  //Return the Date in parsed format
	}
	else
		return false;
}

function isInt(n) {
	var i = parseInt(n);
	if (i == NaN) {
		return false;
	}
	if (i != n){
		return false;
	}
	return true;
}

function isPositiveInt(n) {
	var i = parseInt(n);
	if (i == NaN) {
		return false;
	}
/*	if (i != n){
		return false;
	}*/
	if (i <= 0){
		return false;
	}
	return true;
}

function isDecimal(str,f,n) {
    var p=str.indexOf(".");
    var int,flt;
    if (p<0) { p=str.length ;}
    int=str.substr(0,p);
    flt=str.substr(p+1);
    if (isInt(int)==false) {
       return false;
    }
    if (flt!='') {
       if (isInt(flt)==false) {
          return false;
       }
    }
    if ((int.length > f-n) || (flt.length > n)) {
       return false;
    }
    return true; 
}

function isMail(str) {
    var a=str.indexOf("@")+1;
    var p=str.indexOf(".")+1;
    if(str.indexOf("'") > 0)
		return false;
	if(str.indexOf('"') > 0)
		return false;
    if (a<2)
       return false;    
    if (p<1)
       return false;    
    if (p<a+2)
       return false;    
    if (str.length==p)
       return false;		
    return true; 
}

function isFloat(str) {
	var ch=str.charAt(0);
	if( ch == "." ) return false;
    var flag = 0;
    for (var i=0; i < str.length; i++)
	{	ch=str.charAt(i);
		if (ch == ".") {
            flag ++;
            if (flag > 1) {
                return false;
            }
        }
		if ((ch != ".") && (ch != "0") && (ch != "1") && (ch != "2") && (ch != "3") && (ch != "4") && (ch != "5") && (ch != "6") && (ch != "7") && (ch != "8") && (ch != "9"))	
			return false;
	}
    return true; 
}

function isNumber(str) {
    for (var i=0; i < str.length; i++)
	{	var ch=str.charAt(i);
		if ((ch != "0") && (ch != "1") && (ch != "2") && (ch != "3") && (ch != "4") && (ch != "5") && (ch != "6") && (ch != "7") && (ch != "8") && (ch != "9"))	
			return false;
	}
    return true; 
}

function CheckUserInput(vstrInput) {
	var intIndex;
	var intCharCount;
	for(intIndex = 0; intIndex < vstrInput.length; intIndex++){
		if (vstrInput.charCodeAt(intIndex) < 30) 
			return false;
		if ((vstrInput.charCodeAt(intIndex) > 57) && (vstrInput.charCodeAt(intIndex) < 64)) 
			return false;
		if ((vstrInput.charCodeAt(intIndex) > 90) && (vstrInput.charCodeAt(intIndex) < 97)) 
			return false;
		if (vstrInput.charCodeAt(intIndex) > 122) 
			return false;
	}
	return true;
}
function isPhone(str){
	var intIndex;
	var intCharCount;
	for(intIndex = 0; intIndex < str.length; intIndex++){
		if(str.charCodeAt(intIndex) < 32)
			return false;
		if(str.charCodeAt(intIndex) == 34)
			return false;
		if(str.charCodeAt(intIndex) == 39)
			return false;
		if(str.charCodeAt(intIndex) > 126)
			return false;
	}
	return true;
}


function checkString(str){
	var strChar = str;
	var isValid = true;
	for (var i = 0; i < str.length; i++){
		if ( (str.charCodeAt(i) == 32) || ((str.charCodeAt(i) >= 30) && (str.charCodeAt(i) <= 57)) || ((str.charCodeAt(i) >= 65) && (str.charCodeAt(i) <= 90)) || (str.charCodeAt(i) == 95) || ((str.charCodeAt(i) >= 97) && (str.charCodeAt(i) <= 122)) || (str.charCodeAt(i) > 127) ) {
			// do nothing				
		} else {
			isValid = false;
			break;
		}
	}
	return isValid;
}

/*-----------------------------------------------------------------*/
/*  Yanlk 2006-8-25 begin                                */

    function txt_up(doc,nam,val){
        if(!IsInt1(val)){
            eval(doc+"."+nam).value="";
            return false;
        }
        return true;
    }


    function IsInt1(strTemp){
        var iLeg=strTemp.length; 
        var tChar='';            
        var i;
        if(iLeg>0){
            for (i=1; i<=iLeg;i++){
                tChar=strTemp.substring(i-1,i);
                if(tChar>'9' || tChar<'0') return false;
            }
        }
        return true;
    }


    /**
    * Script Name: openWin()
    * Function:    打开新窗口
    * @param       url     打开的链接地址
    * @param       id      窗口标志
    * @param       width   打开的窗口的宽度
    * @param       height  打开的窗口的高度
    * @param       left    打开的窗口的左边距
    * @param       top     打开的窗口的上边距
    * @return      void
    * Note         当left和top为空时，窗口显示在屏幕的中央
    */
    function openWin(url,id,width,height,left,top){
        var config;
        config = "toolbar=no,menubar=no";
        if(width!=null)
            config += ",width="+width;
        if(height!=null)
            config += ",height="+height;
        if(left!=null&&left!="")
            config += ",left="+left;
        if(top!=null&&top!="")
            config += ",top="+top;
        if(width!=null&&height!=null&&left==null&&top==null){
            config += ",left="+(window.screen.availWidth-width)/2;
            config += ",top="+(window.screen.availHeight-height)/2;
        }
            config += ",resizable=yes,status=yes,scrollbars=yes";
        window.open(url,"",config);
    }

    /**
    * Script Name: openWindow()
    * Function:    打开新窗口(带菜单和工具条)
    * @param       url     打开的链接地址
    * @param       id      窗口标志
    * @param       width   打开的窗口的宽度
    * @param       height  打开的窗口的高度
    * @param       left    打开的窗口的左边距
    * @param       top     打开的窗口的上边距
    * @return      void
    * Note         当left和top为空时，窗口显示在屏幕的中央
    */
    function openWindow(url,id,width,height,left,top){
        var config;
        config = "toolbar=yes,menubar=yes,location=yes";
        if(width!=null)
            config += ",width="+width;
        if(height!=null)
            config += ",height="+height;
        if(left!=null&&left!="")
            config += ",left="+left;
        if(top!=null&&top!="")
            config += ",top="+top;
        if(width!=null&&height!=null&&left==null&&top==null){
            config += ",left="+(window.screen.availWidth-width)/2;
            config += ",top="+(window.screen.availHeight-height)/2;
        }
            config += ",resizable=yes,status=yes,scrollbars=yes";
        window.open(url,"",config);
    }
/*  Yanlk 2006-8-25  end                                */
