/*
 * 去除首尾空白
 */
function trim(strObj){ 
  while ( strObj.value.charAt(0) == " " ){	
    strObj.value = strObj.value.substring(1,strObj.value.length);
  }
  while ( strObj.value.charAt(strObj.value.length-1) == " " ){
    strObj.value = strObj.value.substring(0,strObj.value.length-1);
  }		
}


/*
 * 檢查欄位是否為空值
 */
function checkNull(obj,mess){
  trim(obj);
  if(obj.value == null || obj.value.length == 0){
  	alert("請輸入"+mess+" !!");
    obj.focus();
    return false;
  }
  return true;
}

  
/*
 * 檢查欄位是否為合法整數
 */
function checkInt(obj,mess){
  trim(obj);
  if ( obj.value.length == 0 ) return true;
     num = new Number(obj.value);
     if( num != obj.value || obj.value.indexOf(".") >= 0) {
     	if ( mess.length > 0 ) {
     	   alert(mess+"必須輸入合法的整數 !!");
     	   obj.focus();
     	}   
     	return false;
     } 
  return true;
} 


function checkDecimalPoint(obj,pos,mess){
   var CompPos = obj.value.length - (obj.value.indexOf(".")+1);
   if( obj.value.indexOf(".") >= 0 ){
      if( CompPos > pos ){
   	alert(mess+"欄位之數值只能有"+pos+"位小數 !!");
   	obj.focus();
   	return false;
      }
   }
   return true;
}


function checkLength(obj,maxlength,mess){
	//alert(getLength(obj));
   if( maxlength < getLength(obj) ) {
    alert(mess+"長度不可超過"+maxlength+"個半形字或"+Math.floor(maxlength/2)+"個全形(中文)字 !!");
    obj.focus();
    return false;
   }
   return true;
}

function checkLengthForUnicode(obj,maxlength,mess){
	//alert(getLength(obj));
   if( maxlength < getLengthForUnicode(obj) ) {
   	alert(mess+"長度不可超過"+maxlength+"個半形字或"+Math.floor(maxlength/3)+"個全形(中文)字 !!");
   	obj.focus();
   	return false;
   }
   return true;
}

function checkLengthForOnlyHalf(obj,maxlength,mess){
	//alert(getLength(obj));
   if( maxlength < getLength(obj) ) {
    alert(mess+"長度不可超過"+maxlength+"個字 !!");
    obj.focus();
    return false;
   }
   return true;
}

function getLengthForUnicode(strObj){
     var length = 0;
     for(var i=0; i< strObj.value.length ; i++){
     	if(escape(strObj.value.charAt(i)).indexOf("%u") == 0)
     	   length += 3;
     	else
     	   if( strObj.value.charAt(i) > '' )
     	      length += 3;
     	   else
     	      length += 1;

     }
     return length;
}


function getLength(strObj){
     var length = 0;
     for(var i=0; i< strObj.value.length ; i++){
     	if(escape(strObj.value.charAt(i)).indexOf("%u") == 0)
     	   length += 2;
     	else
     	   if( strObj.value.charAt(i) > '' )
     	      length += 2;
     	   else
     	      length += 1;

     }
     return length;
}


function checkDate(obj_year,obj_month,obj_day,mess){
	   var year = obj_year.value;
	   var month = obj_month.value - 1;
	   var day = obj_day.value;

           var date = new Date(year,month,day);
           var mm = date.getMonth();
           if( mm != month ){
               alert(mess+"日期格式錯誤");
               obj_day.focus();
               return false;
           }
           return true;
}

function checkDateTimeString(obj,mess){
       trim(obj);
	   var len = obj.value.length;
	   if( len != 14 ) {
	       alert(mess+" 日期時間格式錯誤 !!");
               obj.focus();
               return false;
	   }
	   
	   if(len > 0){
     	   for(var i =0; i<len; i++){
     	        if( obj.value.charAt(i) < "0" || obj.value.charAt(i) > "9" ){
     	             alert(mess+" 日期時間格式錯誤 !!");
               obj.focus();
     	             return false;
     	        }
     	   }
       }
	   
	   var year = eval(obj.value.substring(len-10,0));
	   var month = eval(obj.value.substring(len-8,len-10)) - 1;
	   var day = obj.value.substring(len-6,len-8);
	    var hh = eval(obj.value.substring(len-4,len-6));
	   var mm = eval(obj.value.substring(len-2,len-4));
	   var ss = eval(obj.value.substring(len,len-2));

           var date = new Date(year,month,day);
           var mm = date.getMonth();
           if( mm != month ){
               alert(mess+"之日期格式錯誤 !!");
               obj.focus();
               return false;
           }
           	   
	   if( hh >= 0 && hh <= 23 && mm >= 0 && mm <= 59 && ss >=0 && ss <= 59 ){
	     return true;
	   }else{
	     alert(mess+"之日期格式錯誤 !!");
	     obj.focus();
	     return false;
	   }
           return true;
}

function checkDateString(obj,mess){
	   trim(obj);
	   var len = obj.value.length;
	   if( len < 8 ) {
	       alert(mess+"日期格式錯誤");
               obj.focus();
               return false;
	   }
	   
	   if(len > 0){
     	   for(var i =0; i<len; i++){
     	        if( obj.value.charAt(i) < "0" || obj.value.charAt(i) > "9" ){
     	             alert(mess+"日期格式錯誤");
               obj.focus();
     	             return false;
     	        }
     	   }
       }
	   
	   var year = eval(obj.value.substring(len-4,0));
	   var month = eval(obj.value.substring(len-2,len-4)) - 1;
	   var day = obj.value.substring(len,len-2);

           var date = new Date(year,month,day);
           var mm = date.getMonth();
           if( mm != month ){
               alert(mess+"日期格式錯誤");
               obj.focus();
               return false;
           }
           return true;
}


function checkChinDateString(obj,mess){
	   trim(obj);
	   var len = obj.value.length;
	   if( len < 6 ) {
	       alert(mess+"日期格式錯誤");
               obj.focus();
               return false;
	   }
	   
	   if(len > 0){
     	   for(var i =0; i<len; i++){
     	        if( obj.value.charAt(i) < "0" || obj.value.charAt(i) > "9" ){
     	             alert(mess+"日期格式錯誤");
               obj.focus();
     	             return false;
     	        }
     	   }
       }
	   
	   var year = eval(obj.value.substring(len-4,0)) + 1911;
	   var month = eval(obj.value.substring(len-2,len-4)) - 1;
	   var day = obj.value.substring(len,len-2);

           var date = new Date(year,month,day);
           var mm = date.getMonth();
           if( mm != month ){
               alert(mess+"日期格式錯誤");
               obj.focus();
               return false;
           }
           return true;
}

function checkTimeString(obj,mess){
	   trim(obj);
	   var len = obj.value.length;
	   if( len < 6 ) {
	       alert(mess+"時間格式錯誤");
               obj.focus();
               return false;
	   }
	   
	   if(len > 0){
     	   for(var i =0; i<len; i++){
     	        if( obj.value.charAt(i) < "0" || obj.value.charAt(i) > "9" ){
     	             alert(mess+"時間格式錯誤");
               obj.focus();
     	             return false;
     	        }
     	   }
       }
	   
	   var hh = eval(obj.value.substring(len-4,0));
	   var mm = eval(obj.value.substring(len-2,len-4));
	   var ss = eval(obj.value.substring(len,len-2));
	   
	   if( hh >= 0 && hh <= 23 && mm >= 0 && mm <= 59 && ss >=0 && ss <= 59 ){
	     return true;
	   }else{
	     alert(mess+"時間格式錯誤");
	     obj.focus();
	     return false;
	   }
}


function checkNumNotZero(obj,mess){
     trim(obj);
     if ( obj.value.length == 0 ) return true;
     num = new Number(obj.value);
     if( num != obj.value ) {
     	alert(mess+"必須輸入合法的數值 !!");
     	obj.focus();
     	return false;
     } else {
     	if ( num == 0 ){
     	   alert(mess+"不得為0 !!");
     	   obj.focus();
     	   return false;
     	}
     	if ( num < 0 ){
     	   alert(mess+"不得為負值 !!");
     	   obj.focus();
     	   return false;
     	}
     }
     return true;
}


function checkNum(obj,mess){
     trim(obj);
     if ( obj.value.length == 0 ) return true;
     num = new Number(obj.value);
     if( num != obj.value ) {
     	alert(mess+"必須輸入合法的數值 !!");
     	obj.focus();
     	return false;
     }
     return true;
}


function checkIntNotZero(obj,mess){
     trim(obj);
     if ( obj.value.length == 0 ) return true;
     num = new Number(obj.value);
     if( num != obj.value || obj.value.indexOf(".") >= 0) {
     	if ( mess.length > 0 ) {
     	   alert(mess+"必須輸入合法的整數 !!");
     	   obj.focus();
     	}
     	return false;
     }
     if ( num == 0 ){
     	   alert(mess+"不得為0 !!");
     	   obj.focus();
     	   return false;
     }
     	if ( num < 0 ){
     	   alert(mess+"不得為負值 !!");
     	   obj.focus();
     	   return false;
     	}
     return true;
}


function checkDigit(strObj,mess){
     //去除首尾空白
     trim(strObj);

     if(strObj.value.length > 0){
     	   for(var i =0; i<strObj.value.length; i++){
     	        if( strObj.value.charAt(i) < "0" || strObj.value.charAt(i) > "9" ){
     	             alert(mess+"欄位必須為數字 !!");
     	             strObj.focus();
     	             return false;
     	        }
     	   }
     }
     return true;
}


function checkDigitNotZero(strObj,mess){
     //去除首尾空白
     trim(strObj);

     if(strObj.value.length > 0){
     	   for(var i =0; i<strObj.value.length; i++){
     	        if( strObj.value.charAt(i) < "0" || strObj.value.charAt(i) > "9" ){
     	             alert(mess+"欄位必須為數字 !!");
     	             strObj.focus();
     	             return false;
     	        }
     	   }
     }

     if ( strObj.value.length == 0 ) return true;
     num = new Number(strObj.value);
     if ( num == 0 ){
     	   alert(mess+"不得為0 !!");
     	   strObj.focus();
     	   return false;
     }
     return true;
}


function checkHalf(strObj,mess){
     for(var i=0; i< strObj.value.length ; i++){
     	if(escape(strObj.value.charAt(i)).indexOf("%u") == 0){
     	   alert(mess+"欄位必須半型字 !!");
     	   strObj.focus();
     	   return false;
     	}
     }
     return true;
}

function checkLetterValue(strObj,strValue,mess){
     for(var i=0; i< strValue.length ; i++){
     	if( !(
     	      (strValue.charAt(i) >= "A" && strValue.charAt(i) <= "Z") || 
     	      (strValue.charAt(i) >= "a" && strValue.charAt(i) <= "z"))   ){
     	   alert(mess+"必須為英文 !!");
     	   strObj.focus();
     	   return false;
     	}
     }
     return true;
}

function checkDigitValue(strObj,strValue,mess){
     for(var i=0; i< strValue.length ; i++){
     	if( !(strValue.charAt(i) >= "0" && strValue.charAt(i) <= "9")  ){
     	   alert(mess+"必須為數字 !!");
     	   strObj.focus();
     	   return false;
     	}
     }
     return true;
}

function checkLetterOrDigit(strObj,mess){
     for(var i=0; i< strObj.value.length ; i++){
     	if( !((strObj.value.charAt(i) >= "0" && strObj.value.charAt(i) <= "9") || 
     	      (strObj.value.charAt(i) >= "A" && strObj.value.charAt(i) <= "Z") || 
     	      (strObj.value.charAt(i) >= "a" && strObj.value.charAt(i) <= "z"))   ){
     	   alert(mess+"欄位必須為英文或數字 !!");
     	   strObj.focus();
     	   return false;
     	}
     }
     return true;
}


function checkTel(strObj,mess){
     trim(strObj);

     if(strObj.value.length > 0){
     	   for(var i =0; i<strObj.value.length; i++){
     	   	 if( (strObj.value.charAt(i) < "0" || strObj.value.charAt(i) > "9") &&
     	   	      strObj.value.charAt(i) != "(" &&
     	   	      strObj.value.charAt(i) != ")" &&
     	   	      strObj.value.charAt(i) != "-" &&
     	   	      strObj.value.charAt(i) != "#"&&
     	   	      strObj.value.charAt(i) != " "){
     	   	      alert(mess+"欄位不可輸入(,),-,#及數字以外的字元");
     	   	      strObj.focus();
     	   	      return false;
     	   	 }
     	   }
     }
     return true;
}


function trimValue(value,direction,trimChar){ //去除某字元
     var returnValue = value;

     if( direction.indexOf("l") >=0 || direction.indexOf("L") >=0 ) {
        while ( returnValue.charAt(0) == trimChar ){
  	   returnValue = returnValue.substring(1,returnValue.length);
        }
     }

     if( direction.indexOf("r") >=0 || direction.indexOf("R") >=0 )   {
        while ( returnValue.charAt(returnValue.length-1) == trimChar ){
           returnValue = returnValue.substring(0,returnValue.length-1);
        }
     }

     return returnValue;

}


function padValue(value,direction,length,padChar){ //補足某字元
     var returnValue = value;
     if( !(direction=="l" || direction=="L" || direction=="r" || direction=="R") ){
         	alert( "direction error !" );
         	return false;
     }

     if( padChar.length != 1 ){
     	alert( "the length of padChar must be 1 !");
     	return false;
     }

     if( direction=="l" || direction=="L" ) {
        for( var i = value.length ; i< length ; i++ ) {
  	   returnValue = padChar + returnValue ;
        }
     }

     if( direction=="r" || direction=="R" )   {
        for( var i = value.length ; i< length ; i++ ) {
           returnValue = returnValue + padChar;
        }
     }

     return returnValue;

}


function jumpNext(form,obj,maxlength){ //跳往下一個TextField
        if(event.shiftKey) return; //shift
        if( event.keyCode == 16 ) return; //shift + tab
        if( event.keyCode == 9 ) return; //tab
        if( event.keyCode == 13) maxlength = obj.value.length; //按Enter
   	for( i=0; i<form.length; i++){
   	    if( form.elements[i].name == obj.name ){
   	      if( maxlength == obj.value.length) {
   	      	i++;
   	      	for( ;i<form.length;i++ ){
   	      	   if( form.elements[i].type == "text" ){
   	      	   	form.elements[i].focus(); break;
   	      	   }
   	      	}
   	      }
   	      break;
   	    }
   	}
}


function cescape(strObj){ //除中文外皆作escape
     var returnValue = "";
     for(var i=0; i< strObj.value.length ; i++){
     	  if( escape(strObj.value.charAt(i)).indexOf("%u") == 0){
     	  	returnValue = returnValue + strObj.value.charAt(i);
     	  }
     	  else{
     	        returnValue = returnValue + escape(strObj.value.charAt(i));
     	  }
     }
     return returnValue;
}


function cvescape(strValue){ //除中文外皆作escape
     var returnValue = "";
     for(var i=0; i< strValue.length ; i++){
     	  if( escape(strValue.charAt(i)).indexOf("%u") == 0){
     	  	returnValue = returnValue + strValue.charAt(i);
     	  }
     	  else{
     	        returnValue = returnValue + escape(strValue.charAt(i));
     	  }
     }
     return returnValue;
}


function setCookie(name,value){
    var expires = new Date();
    expires.setTime(expires.getTime() + (1000*60*60*24*1));
    document.cookie = name + "=" + escape(value) + "; path=/" + ((expires == null) ? "" : "; expires=" + expires.toGMTString());
}


function getCookie(name){
    var cname = name + "=";
    var dc = document.cookie;

    if (dc.length>0){
	begin = dc.indexOf(cname);
	if (begin!=-1){
		begin += cname.length;
		end = dc.indexOf(";",begin);
		if  (end == -1)
			end = dc.length;
		return unescape(dc.substring(begin,end));
	}
    }
    return null;
}

function chkEmail(obj, mess){
  var len = obj.value.length;
  if((obj.value.indexOf("@")==-1)||(obj.value.indexOf("@")==0)||(obj.value.indexOf("@")==(len-1))){ alert(mess + "輸入不合法..!!"); obj.focus(); return false; }
  if((obj.value.indexOf("@")!=-1)&&(obj.value.substring(obj.value.indexOf("@")+1,len).indexOf("@")!=-1)){ alert(mess + "輸入不合法..!!"); obj.focus(); return false; } 
  if((obj.value.indexOf(".")==-1)||(obj.value.indexOf(".")==0)||(obj.value.lastIndexOf(".")==(len-1))){ alert(mess + "輸入不合法..!!"); obj.focus(); return false; }
  return true;
}

function chkid(obj, mess){
	var c, n, i;
	var t= "ABCDEFGHJKLMNPQRSTUVXYWZIO";
    var s= obj.value;
	c= s.substring(0,1);
	c= t.indexOf(c.toUpperCase());
	if((s.length!= 10) || (c<0)) {
	 alert(mess + "不正確..!!");
	 obj.focus();
	 return false;
	}

	n= parseInt(c/10)+ c%10*9+ 1;
	for(i=1; i<9; i++) n= n+ parseInt(s.substring(i,i+1))* (9-i);
	n= (10- (n% 10))% 10;
	if(n!= parseInt(s.substring(9,10))){
	 alert(mess + "不正確..!!");
	 obj.focus();
	 return false;
	} 
	return true;

}

   function getObj(name){
	  if (document.getElementById)	{
	     return document.getElementById(name);
	  }else if (document.all)	{
	     return document.all[name];
	  }else if (document.layers){
	     return document.layers[name];
	  }
   }
