//**************************************************************** 
// 下面幾個function主要功能都在檢查Web Form的欄位，是否有不合法
// 的值？ 
// 
// 作者：sylvia
//**************************************************************** 


    // 刪除字串前後之空白字元
    // 例如：Str='ABC ' --> Str='ABC', Str1='  ABC' --> Str1='ABC'   
    
    function Trim (InString)
    {
	  var aFlag;
	  
	  aFlag = 0;	  
	  MidString=InString;
	  for (Count=0; Count < InString.length; Count++)  
	  {
		TempChar=InString.substring (Count, Count+1);
		if (TempChar!=" ") 
		{
		   MidString=InString.substring (Count, InString.length)
		   aFlag = 1;
		   break;
		}	   
	  }
	  if (aFlag == 1)
		OutString=MidString;
	  else
	  {
	    MidString="";
	    OutString="";	
	  }  
      
	  for (Count=MidString.length; Count > 0; Count--)  
	  {
		TempChar=MidString.substring (Count-1, Count);
		if (TempChar!=" ") 
		{
			OutString=MidString.substring (0, Count)
			break;
		}
	  }
	  
	  return (OutString);
    }
    
    //刪除字串內空白的字元
    //例如：Str='  ABC D' --> Str='ABCD', Str1='ABCD  EF' --> Str1='ABCDEF'
    
    function stripSpaces (InString)  
    {
	  OutString="";
	  for (Count=0; Count < InString.length; Count++)  {
		TempChar=InString.substring (Count, Count+1);
		if (TempChar!=" ")
			OutString=OutString+TempChar;
	  }
  	  return (OutString);
    }
    
    //檢查輸入的值是否超過欄位大小，若是則傳回true，反之則為false
    //Form為<Form>的名稱，Field為所要檢查欄位的名稱，Size為欄位所能容許的size，Desc當檢查欄位為空白時你希望顯示警告視窗內容
    
    function CheckSize(Form,Field,Size,Desc)
    {
    	  i = parseInt(Size,10);
    	  ActiveForm = eval(Form)
    	  if( Trim(window.ActiveForm.item(Field).value.length) > i)
    	  {
    	    window.alert(Desc);   //顯示警告
            window.ActiveForm.item(Field).focus();           //游標移至空白處
            window.ActiveForm.item(Field).select();
            return true;      	 	
    	  }
    	  else 
    	    return false;
    }	
    
    //檢查欄位是否為空白，若是則傳回true，反之則為false 
    //Form為<Form>的名稱，Field為所要檢查欄位的名稱，Desc當檢查欄位為空白時你希望顯示警告視窗內容
       
    function CheckEmpty(Form,Field,Desc)
    {
      
          ActiveForm = eval(Form);
          if( Trim(window.ActiveForm.item(Field).value) == "")
          {            
            window.alert(Desc);   //顯示警告
            window.ActiveForm.item(Field).focus();           //游標移至空白處
            window.ActiveForm.item(Field).select();
            return true;     
          }
          else
            return false;
    }
    
    //檢查欄位是否為數字，若是則傳回true，反之則為false
    //Form為<Form>的名稱，Field為所要檢查欄位的名稱，Desc當檢查欄位不為數字時你希望顯示警告視窗內容
    
    function CheckNumeric(Form,Field,Desc)
    {
          ActiveForm = eval(Form);
          if (isNaN(Trim(window.ActiveForm.item(Field).value)))
          {   
            window.alert(Desc);   //顯示警告
            window.ActiveForm.item(Field).focus();           
            window.ActiveForm.item(Field).select();         
            return false;     
          }
          else
            return true;
    } 
    
    // 根據所傳入之比較方式來比較兩個日期欄位，例如我要比較'2002/09/01'是否大於'2002/08/31', 
    //  若是則傳回true，反之則傳回false
    // Form為<Form>的名稱，Field1為第一個日期欄位，Field2為第二個日期欄位 
    // Sign: ">"  mean date1 >  date2 
    //       ">=" mean date1 >= date2
    //       "="  mean date1 =  date2
    //       "<=" mean date1 <= date2
    //       ">"  mean date1 <  date2 
    // Desc1用來描述第一個日期欄位，Desc2用來描述第二個日期欄位，用在出現警告視窗用
    
    function CompDate(Form, Field1, Field2, Sign, Desc1, Desc2)
    {  
       ActiveForm = eval(Form);
       
       var sdate1 = new Date(window.ActiveForm.item(Field1).value); 
       var sdate2 = new Date(window.ActiveForm.item(Field2).value);
       
       //若要比較 sdate1 是否> sdate2 
       if(Sign == ">")
       { 
          if((sdate1-sdate2) <= 0)
          {
            window.alert(Desc1 + "必須大於" + Desc2);
            window.ActiveForm.item(Field1).focus();           
            window.ActiveForm.item(Field1).select();         
            return false;
          }  
       } 
       //若要比較 sdate1 是否>= sdate2  
       else if(Sign == ">=")
       {    
          if((sdate1-sdate2) < 0)
          { window.alert(Desc1 + "必須大於等於" + Desc2);
            window.ActiveForm.item(Field1).focus();           
            window.ActiveForm.item(Field1).select();         
            
            return false;
          } 
       }
       //若要比較 sdate1 是否等於 sdate2 
       else if(Sign == "=")
       { 
          if( (sdate1-sdate2) != 0) 
          {
            window.alert(Desc1 + "必須等於" + Desc2);
            window.ActiveForm.item(Field1).focus();           
            window.ActiveForm.item(Field1).select();         
            return false;
          }  
       }
       //若要比較 sdate1 是否<= sdate2
       else if(Sign == "<=")
       {  
          if( (sdate1-sdate2) > 0) 
          {
            window.alert(Desc1 + "必須小於等於" + Desc2);
            window.ActiveForm.item(Field1).focus();           
            window.ActiveForm.item(Field1).select();         
            return false;
          }  
       }
       //若要比較 sdate1 是否< sdate2
       else if(Sign == "<")
       { 
          if( (sdate1-sdate2) >= 0) 
          {
            window.alert(Desc1 + "必須小於" + Desc2);
            window.ActiveForm.item(Field1).focus();           
            window.ActiveForm.item(Field1).select();         
            return false;
          }  
       }
       else
       {
         alert("請輸入正確的比較方式！如：「＞」，「＞＝」，「＝」，「＜＝」及「＜」");
         return false;
       }  
       return true;
    }       
    
    //檢查EMail格式是否正確，若是則傳回true，反之則為false
    //Form為<Form>的名稱，Field為所要檢查欄位的名稱，Desc當檢查欄位不為正確EMail格式時你希望顯示警告視窗內容
    
    function CheckMail(Form,Field,Desc)
    {
	ActiveForm = eval(Form);
        var MailStr = window.ActiveForm.item(Field).value;
    
        i=MailStr.indexOf("@");
        j=MailStr.indexOf(".",i);
        k=MailStr.indexOf(",");
        kk=MailStr.indexOf(" ");
        jj=MailStr.lastIndexOf(".") + 1;
        len=MailStr.length;

        if ((i <= 0) || (j <= (i+1)) || (k != -1) || (kk != -1) || (len-jj < 2) || (len-jj > 3)) {
          alert(Desc);
          window.ActiveForm.item(Field).focus();
          window.ActiveForm.item(Field).select(); 
          return false;
        }
        else
          return true;
    }
    
    //檢查身份證字號格式是否正確，若是則傳回true，反之則為false
    //Form為<Form>的名稱，Field為所要檢查欄位的名稱，Desc當檢查欄位不為正確身份證字號格式時你希望顯示警告視窗內容
    
    function CheckID(Form,Field,Desc)
    {
	ActiveForm = eval(Form);
        var IDStr = window.ActiveForm.item(Field).value+"$";
        var firstStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        var letter = IDStr.substring(0,1);
        var letter1 = letter.toUpperCase();
        sumval = 0;
        modval = -1;
        rc = true;
        al = 0;
        a2 = 0;

        if ((firstStr.indexOf(letter1,0)==-1) || (IDStr.length<11)) {
          alert(Desc);
          window.ActiveForm.item(Field).focus();
          window.ActiveForm.item(Field).select(); 
          return false;
        } else {	
           if (letter1=="A") {
             a1 = 1;
             a2 = 0;
           }
           if (letter1=="B") {
             a1 = 1;
             a2 = 1;
           }  
           if (letter1=="C") {
             a1 = 1;
             a2 = 2;
           }
           if (letter1=="D") {
             a1 = 1;
             a2 = 3;
           }	
           if (letter1=="E") {
             a1 = 1;
             a2 = 4;
           }	
           if (letter1=="F") {
             a1 = 1;
             a2 = 5;
           }
           if (letter1=="G") {
             a1 = 1;
             a2 = 6;
           }
           if (letter1=="H") {
             a1 = 1;
             a2 = 7;
           }
           if (letter1=="J") {
             a1 = 1;
             a2 = 8;
           }
           if (letter1=="K") {
             a1 = 1;
             a2 = 9;
           }
           if (letter1=="L") {
             a1 = 2;
             a2 = 0;
           }
           if (letter1=="M") {
             a1 = 2;
             a2 = 1;
           }
           if (letter1=="N") {
             a1 = 2;
             a2 = 2;
           }
           if (letter1=="P") {
             a1 = 2;
             a2 = 3;
           }
           if (letter1=="Q") {
             a1 = 2;
             a2 = 4;
           }
           if (letter1=="R") {
             a1 = 2;
             a2 = 5;
           }
           if (letter1=="S") {
             a1 = 2;
             a2 = 6;
           }
           if (letter1=="T") {
             a1 = 2;
             a2 = 7;
           }
           if (letter1=="U") {
             a1 = 2;
             a2 = 8;
           }
           if (letter1=="V") {
             a1 = 2;
             a2 = 9;
           }
           if (letter1=="X") {
             a1 = 3;
             a2 = 0;
           }
           if (letter1=="Y") {
             a1 = 3;
             a2 = 1;
           }
           if (letter1=="W") {
             a1 = 3;
             a2 = 2;
           }
           if (letter1=="Z") {
             a1 = 3;
             a2 = 3;
           }
           if (letter1=="I") {
             a1 = 3;
             a2 = 4;
           }
           if (letter1=="O") {
             a1 = 3;
             a2 = 5;
           }
           
           d1 = IDStr.substring(1,2);
           d2 = IDStr.substring(2,3);
           d3 = IDStr.substring(3,4);
           d4 = IDStr.substring(4,5);
           d5 = IDStr.substring(5,6);
           d6 = IDStr.substring(6,7);
           d7 = IDStr.substring(7,8);
           d8 = IDStr.substring(8,9);
           d9 = IDStr.substring(9,10);
           
           NoStr = "0123456789";
           
           if ((NoStr.indexOf(d1,0) == -1) || (NoStr.indexOf(d2,0) == -1) || (NoStr.indexOf(d3,0) == -1) || (NoStr.indexOf(d4,0) == -1)
             || (NoStr.indexOf(d5,0) == -1) || (NoStr.indexOf(d6,0) == -1) || (NoStr.indexOf(d7,0) == -1) || (NoStr.indexOf(d8,0) == -1)
             || (NoStr.indexOf(d9,0) == -1)) {
             
             alert(Desc);
             window.ActiveForm.item(Field).focus();
             window.ActiveForm.item(Field).select(); 
             return false;
             
           } else {
             sumval = eval(a1*1+a2*9+d1*8+d2*7+d3*6+d4*5+d5*4+d6*3+d7*2+d8*1+d9*1);
             modval = sumval % 10;
             if (modval!=0) {	
               alert(Desc);
               window.ActiveForm.item(Field).focus();
               window.ActiveForm.item(Field).select(); 
               return false;	
             }
           }  	
        }	
        return true;
    }
    
    //檢查所輸入的字串值是否含有SQL保留字，如：「'」，「_」，另外「"」則是防止與Java String符號混淆
    //若有保留字傳回true，反之則傳回false
    //Form為<Form>的名稱，Field為所要檢查欄位的名稱，Desc用來描述Field之欄位
    function CheckSQLKey(Form,Field,Desc){

	var checkstr = "'_\"";			//  判斷在SQL搜尋時, 不可輸入哪些保留字  ' _ "  共三個字
	var str = new String ;
	var len;
	
	ActiveForm = eval(Form);
	str = window.ActiveForm.item(Field).value;		// 取得整個字串
	checklen=checkstr.length;
	len=str.length;						// 字元長度
	i=0;
	j=0;
	
	for(i=0;i<len;i++) { 
	  for(j=0;j<checklen;j++) {
	    if (str.charAt(i) == checkstr.charAt(j)) {
		  alert(Desc+"不可輸入"+str.charAt(i)+" ，因為是SQL特殊字元");
		  window.ActiveForm.item(Field).focus();
		  window.ActiveForm.item(Field).select();
	          return true;
	    }
	  }
	} 
	return false;
    } 
    
    // 根據所傳入之比較方式來比較兩個字串欄位，例如我要比較'abc'是否大於'def', 
    //  若是則傳回true，反之則傳回false
    // Form為<Form>的名稱，Field1為第一個字串欄位，Field2為第二個字串欄位 
    // Sign: ">"  mean str1 >  str2 
    //       ">=" mean str1 >= str2
    //       "="  mean str1 =  str2
    //       "<=" mean str1 <= str2
    //       ">"  mean str1 <  str2 
    // Desc1用來描述第一個字串欄位，Desc2用來描述第二個字串欄位，用在出現警告視窗用
    
    function CompStr(Form, Field1, Field2, Sign, Desc1, Desc2)
    {
       ActiveForm = eval(Form);
       
       var str1 = window.ActiveForm.item(Field1).value;
       var str2 = window.ActiveForm.item(Field2).value;
       
       //若要比較 str1 是否> str2 
       if(Sign == ">")
       { 
          if (str1 <= str2)
          {
            window.alert(Desc1 + "必須大於" + Desc2);
            window.ActiveForm.item(Field1).focus();           
            window.ActiveForm.item(Field1).select();         
            return false;
          }   
       } 
       //若要比較 sdate1 是否>= sdate2  
       else if(Sign == ">=")
       {   
          if (str1 < str2)
          { window.alert(Desc1 + "必須大於等於" + Desc2);
            window.ActiveForm.item(Field1).focus();           
            window.ActiveForm.item(Field1).select();            
            return false;
          } 
       }
       //若要比較 sdate1 是否等於 sdate2
       if(Sign == "=")
       { 
          if (str1 != str2) 
          {
            window.alert(Desc1 + "必須等於" + Desc2);
            window.ActiveForm.item(Field1).focus();           
            window.ActiveForm.item(Field1).select();         
            return false;
          }  
       } 
       //若要比較 sdate1 是否<= sdate2
       else if(Sign == "<=")
       { 
          if (str1 > str2) 
          {
            window.alert(Desc1 + "必須小於等於" + Desc2);
            window.ActiveForm.item(Field1).focus();           
            window.ActiveForm.item(Field1).select();         
            return false;
          }  
       }
       //若要比較 str1 是否< str2
       else if(Sign == "<")
       { 
          if (str1 >= str2)  
          {
            window.alert(Desc1 + "必須小於" + Desc2);
            window.ActiveForm.item(Field1).focus();           
            window.ActiveForm.item(Field1).select();         
            return false;
          }  
       } 
       else
       {
         alert("請輸入正確的比較方式！如：「＞」，「＞＝」，「＝」，「＜＝」及「＜」");
         return false;
       }  
       return true; 
    }
    