// 分页初始化
//var _page = {hasPrePage:false,hasNextPage:false,pageSize:"-1",totalPage:"",totalRecord:"",currentPage:"-1",beginIndex:""};
var _pages = new Array();
/* is empty? */
function isEmpty(value){
	if(trim(value).length == 0) 
	  return true;
	else return false;
}
/*
 *remove all blanks
 *trim(" df ")==df
 *add by tony
 */
function trim(str) {
    return str.replace(/(^\s*)|(\s*$)/g, "");
}
/*
 *validate date(include bissextile)
 *format: dd-mm-yyyy
 *add by tony
 */
function isValidDate(sText) {
	//yyyy-mm-dd
    var reDate = /^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29))$/ 
    //dd-mm-yyyy
	//var reDate = /^(((0?[1-9]|[12]\d|3[01])-(0?[13578]|1[02])-((1[6-9]|[2-9]\d)\d{2}))|((0?[1-9]|[12]\d|30)-(0?[13456789]|1[012])-((1[6-9]|[2-9]\d)\d{2}))|((0?[1-9]|1\d|2[0-8])-0?2-((1[6-9]|[2-9]\d)\d{2}))|(29-0?2-((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$/ 
    return reDate.test(sText);
}
/*
 *validate date(include bissextile)
 *format: YYYY-MM-DD HH24:Mi:SS
 *add by nicky
 */
function isValidAllDate(sText) {
	//yyyy-mm-dd
    var regexDate=/^((\d{2}(([02468][048])|([13579][26]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|([1-2][0-9])))))|(\d{2}(([02468][1235679])|([13579][01345789]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\s(((0?[0-9])|(1?[0-9])|(2?[0-3]))\:([0-5]?[0-9])((\:([0-5]?[0-9])))))$/;  
    //dd-mm-yyyy
    //var regexDate=new RegExp("^((\d{2}(([02468][048])|([13579][26]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|([1-2][0-9])))))|(\d{2}(([02468][1235679])|([13579][01345789]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\s(((0?[0-9])|([1-2][0-3]))\:([0-5]?[0-9])((\s)|(\:([0-5]?[0-9])))))?$");  
	//var reDate = /^(((0?[1-9]|[12]\d|3[01])-(0?[13578]|1[02])-((1[6-9]|[2-9]\d)\d{2}))|((0?[1-9]|[12]\d|30)-(0?[13456789]|1[012])-((1[6-9]|[2-9]\d)\d{2}))|((0?[1-9]|1\d|2[0-8])-0?2-((1[6-9]|[2-9]\d)\d{2}))|(29-0?2-((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$/ 
    return regexDate.test(sText);
}
/*
 *validate Email(right format x@x.x)
 *add by tony
 */
function isValidEmail(sText) {
    var sText_t=trim(sText);
    if(sText_t.length==0){
		 return false;
    } else {
      var reEmail = /^(?:\w+\.?)*\w+@(?:\w+\.)+\w+$/;
      return reEmail.test(sText_t);
    }
}
/*
 *Round real
 *Round(58.2385, 2)  =58.24
 *add by tony
 */
function Round(a_Num, a_Bit) {
    return (Math.round(a_Num * Math.pow(10, a_Bit)) / Math.pow(10, a_Bit));
}
/*
 *validate 0 or 5 bit Number(Integer)
 *isValidInteger("d") =fase
 *isValidInteger("2") =true
 *add by tony
 */
function isValidInteger(sText) {
	var reg = /^\d{1,}$/;
    return reg.test(sText);
}

/*
 *验证是否为正整数
 *isValidPositiveInteger("0") =fase
 *isValidPositiveInteger("2") =true
 *add by lucya
 */
function isValidPositiveInteger(sText) {
	var reg = /^[0-9]*[1-9][0-9]*$/;
    return reg.test(sText);
}

/*
 *validate 32 UUID
 *add by tony
 */
function isValidUUID(sText) {
	var reg = /^[a-fA-F0-9ï½-ï½ï¼¡-ï¼¦ï¼-ï¼]{32}$/;
    return reg.test(sText);
}

/*
 *split text from text-value
 *splitValue("ddd-fff") =ddd
 *add by tony
 */
function splitText(value){
	var temp = new Array();
	temp = value.split("|");
	return temp[0];
}
/*
 *split value from text-value
 *splitValue("ddd-fff") =fff
 *add by tony
 */
function splitValue(value){
	var temp = new Array();
	temp = value.split("|");
	return temp[1];
}
/* 校验是否全由数字组成 */
function isDigit(s) { 
	var patrn=/^[0123456789１２３４５６７８９０]+$/; 
	if (!patrn.exec(s)) return false 
	return true 
} 
/* 校验电话号码是否符合格式 */
function isPhone(s) { 
	var patrn=/^[()\-*+0123456789１２３４５６７８９０＋（）＊－]+$/; 
	if (!patrn.exec(s)) return false 
	return true 
} 
/*
 * 全角转半角
 * add by tony
*/
function DBC2SBC(str){
 	var result = '';
 	for (i=0 ; i<str.length; i++){
 		// 获取当前字符的unicode编码
  		code = str.charCodeAt(i);
		// 在这个unicode编码范围中的是所有的英文字母已经各种字符
  		if (code >= 65281 && code <= 65373){
  			// 把全角字符的unicode编码转换为对应半角字符的unicode码
   			result += String.fromCharCode(str.charCodeAt(i) - 65248);
  		}else if (code == 12288) {// 空格处理
   			result += String.fromCharCode(str.charCodeAt(i) - 12288 + 32);
  		}else {
   			result += str.charAt(i);
  		}
 }
 return result;
}
 
/************************************************
DESCRIPTION: Validates that a string contains only
    valid integer number.
PARAMETERS:
   strValue - String to be tested for validity
RETURNS:
   True if valid, otherwise false.
   
Add by tonylee
**************************************************/
function validateInteger( strValue ) {
  var objRegExp  = /(^-?\d\d*$)/;
  //check for integer characters
  return objRegExp.test(strValue);
}

/*****************************************************************
DESCRIPTION: Validates that a string contains only valid numbers.
PARAMETERS:
   strValue - String to be tested for validity
RETURNS:
   True if valid, otherwise false.
   
Add by tonylee
******************************************************************/
function  validateNumeric( strValue ) {
  var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
  //check for numeric characters
  return objRegExp.test(strValue);
}

/************************************************
DESCRIPTION: Validates that a string contains valid
  US phone pattern.
  Ex. (999) 999-9999 or (999)999-9999
PARAMETERS:
   strValue - String to be tested for validity
RETURNS:
   True if valid, otherwise false.
   
Add by tonylee
*************************************************/
function validateUSPhone( strValue ) {
  var objRegExp  = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;
  //check for valid us phone with or without space between area code
  return objRegExp.test(strValue);
}

/************************************************
DESCRIPTION: Validates that a string is not all
  blank (whitespace) characters.
PARAMETERS:
   strValue - String to be tested for validity
RETURNS:
   True if valid, otherwise false.
   
Add by tonylee
*************************************************/
function validateNotEmpty( strValue ) {
   var strTemp = strValue;
   strTemp = trimAll(strTemp);
   if(strTemp.length > 0){
     return true;
   }
   return false;
}

/************************************************
DESCRIPTION: Validates that a string a United
  States zip code in 5 digit format or zip+4
  format. 99999 or 99999-9999
PARAMETERS:
   strValue - String to be tested for validity
RETURNS:
   True if valid, otherwise false.
   
Add by tonylee
*************************************************/
function validateUSZip( strValue ) {
	var objRegExp  = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
  	//check for valid US Zipcode
  	return objRegExp.test(strValue);
}

function validateUSDate( strValue ) {
/************************************************
DESCRIPTION: Validates that a string contains only
    valid dates with 2 digit month, 2 digit day,
    4 digit year. Date separator can be ., -, or /.
    Uses combination of regular expressions and
    string parsing to validate date.
    Ex. mm/dd/yyyy or mm-dd-yyyy or mm.dd.yyyy
PARAMETERS:
   strValue - String to be tested for validity
RETURNS:
   True if valid, otherwise false.
REMARKS:
   Avoids some of the limitations of the Date.parse()
   method such as the date separator character.
   
Add by tonylee
*************************************************/
  var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
  //check to see if in correct format
  if(!objRegExp.test(strValue))
    return false; //doesn't match pattern, bad date
  else{
    var strSeparator = strValue.substring(2,3) 
    var arrayDate = strValue.split(strSeparator); 
    //create a lookup for months not equal to Feb.
    var arrayLookup = { '01' : 31,'03' : 31, 
                        '04' : 30,'05' : 31,
                        '06' : 30,'07' : 31,
                        '08' : 31,'09' : 30,
                        '10' : 31,'11' : 30,'12' : 31}
    var intDay = parseInt(arrayDate[1],10); 
    //check if month value and day value agree
    if(arrayLookup[arrayDate[0]] != null) {
      if(intDay <= arrayLookup[arrayDate[0]] && intDay != 0)
        return true; //found in lookup table, good date
    }  
    var intMonth = parseInt(arrayDate[0],10);
    if (intMonth == 2) { 
       var intYear = parseInt(arrayDate[2]);
       if (intDay > 0 && intDay < 29) {
           return true;
       }
       else if (intDay == 29) {
         if ((intYear % 4 == 0) && (intYear % 100 != 0) || 
             (intYear % 400 == 0)) {
              // year div by 4 and ((not div by 100) or div by 400) ->ok
             return true;
         }   
       }
    }
  }  
  return false; //any other values, bad date
}

/************************************************
DESCRIPTION: Trims trailing whitespace chars.
PARAMETERS:
   strValue - String to be trimmed.
RETURNS:
   Source string with right whitespaces removed.
   
Add by tonylee
*************************************************/
function rightTrim( strValue ) {
	var objRegExp = /^([\w\W]*)(\b\s*)$/;
    if(objRegExp.test(strValue)) {
       //remove trailing a whitespace characters
       strValue = strValue.replace(objRegExp, '$1');
    }
  return strValue;
}

/************************************************
DESCRIPTION: Trims leading whitespace chars.
PARAMETERS:
   strValue - String to be trimmed
RETURNS:
   Source string with left whitespaces removed.
   
Add by tonylee
*************************************************/
function leftTrim( strValue ) {
	var objRegExp = /^(\s*)(\b[\w\W]*)$/;
    if(objRegExp.test(strValue)) {
       //remove leading a whitespace characters
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}
/************************************************
DESCRIPTION: Removes leading and trailing spaces.
PARAMETERS: Source string from which spaces will
  be removed;
RETURNS: Source string with whitespaces removed.

Add by tonylee
*************************************************/
function trimAll( strValue ) {
 var objRegExp = /^(\s*)$/;
    //check for all spaces
    if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '');
       if( strValue.length == 0)
          return strValue;
    }
   //check for leading & trailing spaces
   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
   if(objRegExp.test(strValue)) {
       //remove leading and trailing whitespace characters
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}
//è®¾ç½®checkboxå¨éé¡¹æå¨ä¸é   op:selectä¸º å¨éï¼op:'unselect'å¨ä¸é
function dealAllCheckBox(op,dealObjName){
	var dealObjs = document.getElementsByName(dealObjName);	
	if(op == 'select'){
		for(var i=0;i<dealObjs.length;i++)
			dealObjs[i].checked = true;
	}else if(op == 'unselect'){
		for(var i=0;i<dealObjs.length;i++)
			dealObjs[i].checked = false;
	}	
}
 /*control textarea maxlength*/
function doKeypress(event,obj,maxLength){
    	//var isChar = String.fromCharCode(window.event.keyCode);
     	 //isChar = obj.value + isChar; 
		// Allow user to type character if at least one character is selected 
      if(obj.value.length < maxLength)  //oTR.text.length
           event.returnValue = true;
      else 
         event.returnValue = false;
          // obj.value = obj.value.substring(0,maxLength);
      }
function doCopy(event,obj,maxLength){
   	  //var isChar = String.fromCharCode(window.event.keyCode);
      //isChar = obj.value + isChar;
	  // Allow user to type character if at least one character is selected 
      if(obj.value.length < maxLength)  //oTR.text.length
           event.returnValue = true;
      else {
          event.returnValue = false;
              obj.value = obj.value.substring(0,maxLength);
           }
      }
        
/*end*/
/**
 *when page no form you can convenient using
 * add by frank 
/* select all checkbox no form */
function chooseAll(allName, listName, allStatus) {
    var selectObjs = document.getElementsByName(listName);
    for (i = 0; i < selectObjs.length; i = i + 1) {
    if(selectObjs[i].disabled==""){
        selectObjs[i].checked = allStatus;
        }
    }
    document.getElementsByName(allName)[0].checked = allStatus;
}
/* change  single checkbox status , then change  the all selected checkbox status */
function changeChoosed(allName, checkbut) {
    var v = checkbut.checked;
    var listName = checkbut.name;
    if (v) {
        checkbut.checked = true;
        if (isAllChoosed(listName)) {
            document.getElementsByName(allName)[0].checked = true;
        }
    } else {
        document.getElementsByName(allName)[0].checked = false;
        checkbut.checked = false;
    }
}
/* checkbox is all chooced? */
function isAllChoosed(listName) {
    var selectcount = 0;
    var selectObjs = document.getElementsByName(listName);
    var allcount = selectObjs.length;
    for (i = 0; i < selectObjs.length; i = i + 1) {
        if (selectObjs[i].checked) {
            selectcount = selectcount + 1;
        }
    }
    if (selectcount == allcount) {
        return true;
    } else {
        return false;
    }
}
/* check checkbox have been selected at least one */
function isChoosed(listName){  
    var selectObjs = document.getElementsByName(listName);
    for (i=0; i<selectObjs.length; i=i+1){
        if (selectObjs[i].checked){       
            return true;            
        }
    }
    return false;
}
//æ­£æ´æ°éªè¯
function isPositiveInt(value){
    var temp = trim(value);  //trim all the space
    temp = trimZeroLeft(temp); //trim all the zero on left
    var reg = /^[1-9]+[0-9]*$/ ;    //check whether is positive integer(exclude zero)
    var result = reg.test(temp);
    return result;
}
function trimZeroLeft(value){
    var temp = trim(value);  //trim all the space
    var result = temp.replace(/^0+/,"");    //trim all zero on left 
    return result;
}

//-----------------以下是全选/全不选------------------------------------
//检查是否有被选中的
function isExistSelected(listName){	
	var f = document.getElementsByName(listName);	
	for (var i=0;i<f.length;i++){
		if (f[i].checked){
		    return true;			
		}
	}
	return false;
}
//设置全选项
function dealAll(obj,dealObjName){
	var dealObjs = document.getElementsByName(dealObjName);	
	if(obj.checked){
		for(var i=0;i<dealObjs.length;i++)
			dealObjs[i].checked = true;
	}else{
		for(var i=0;i<dealObjs.length;i++)
			dealObjs[i].checked = false;
	}	
}
//当单个checkbox被点击检查是否要选中全选项
function dealAllButton(obj,dealObjId){	
	var dealObjs = document.getElementsByName(obj.name);	
	if(obj.checked){
		$(dealObjId).checked = true;
		for(var i=0;i<dealObjs.length;i++){
			if(!dealObjs[i].checked){
				$(dealObjId).checked = false;
			}
		}
	}else{
			$(dealObjId).checked = false;
	}	
}
//------------以下是老的全选/全不选---------------------------------
/* check a string's length,lowLength can be 0,means this strng can be empty,but when input,its length can't be more than highLength. */
function strLengthBetween(name,value,lowLength,highLength){
	var value_new =trim(value);
	if(lowLength==0){
		if(!isBetween(value_new.length,lowLength,highLength)){
			return(false);
		}
	}
	else{
		if(!isBetween(value_new.length,lowLength,highLength)){
			return(false);
		}
	}
	return(true);
}
function isBetween(value,lo,hi){
	var value_new=parseInt(value,10);		
	if((value_new<lo)||(value_new>hi)){	
	  return(false);
	}else{
	  return(true);
	} 
}
/* select all checkbox */
function selectAll(allName,listName,allStatus){
	var f = document.forms[0];
	for (i=0; i<f.elements.length; i=i+1){
		if (f.elements[i].name==listName){
			f.elements[i].checked = allStatus;		
		} 
	}
	document.forms[0].elements[allName].checked = allStatus;
}
/* change  signle checkbox status , then change  the all selected checkbox status */
function changeChecked(allName,checkbut){
	var v =checkbut.checked;
	var listName = checkbut.name;
	if (v) {
		checkbut.checked = true;
		if(isAllSelected(listName)){
			document.forms[0].elements[allName].checked = true;
		}
	}else{
		document.forms[0].elements[allName].checked = false;
		checkbut.checked = false;
	}
}
/* check checkbox have been selected at least one */
function isSelected(listName){	
	var f = document.forms[0];	
	for (i=0; i<f.elements.length; i=i+1){
		if (f.elements[i].name==listName&&f.elements[i].checked){		
		    return true;			
		}
	}
	return false;
}
/* checkbox is all selected? */
function isAllSelected(listName){
	var selectcount = 0;
	var allcount=0;
	var f = document.forms[0];
	for(i=0;i<f.elements.length;i=i+1){
		if (f.elements[i].name==listName&&f.elements[i].checked){
			selectcount = selectcount + 1;
	    }
	}
	for(i=0;i<f.elements.length;i=i+1){
		if (f.elements[i].name==listName){
			allcount = allcount+1;
		}
	}	
	if(selectcount==allcount){ 
		return true;
	}else{
		return false;
	}
}
//------------------------------------------------------------------------
