/* ======================================================================
FUNCTION:  	IsInt
 
INPUT:  		numstr (string/number) 	 - the string that will be tested to ensure 
      						   that each character is a digit
====================================================================== */
function IsInt( numstr) 
{
	// Return immediately if an invalid value was passed in
	if (numstr+"" == "undefined" || numstr+"" == "null" || numstr+"" == "")	
        {
           alert("請輸入統一編號");
           return false;
        }

	var isValid = true;

	// convert to a string for performing string comparisons.
	numstr += "";	

	// Loop through string and test each character. If any
	// character is not a number, return a false result.
 	// Include special case for negative numbers (first char == '-').   
	for (i = 0; i < numstr.length; i++) 
       {
       	if (!((numstr.charAt(i) >= "0") && (numstr.charAt(i) <= "9"))) 
            {
       	     isValid = false;
              alert("統一編號必須為數字");
       	     break;
	    } 	         	       
       } // END for      
   return isValid;
}  // end IsInt



/*======================================================================
	businesscode()
	
	Description: 
	     business Code Check - JavaScript version
	
====================================================================== */
function businesscode(code) 
{
     var c1,c2,c3,c4,
         a1,a2,a3,a4,a5,
         b1,b2,b3,b4,b5=0,Y=0;
 
     if(! IsInt(code))
       {
         return false; 
       } 
     c1=parseInt(code.charAt(0),10);
     c2=parseInt(code.charAt(2),10);
     c3=parseInt(code.charAt(4),10);
     c4=parseInt(code.charAt(7),10);

   
     a1=Math.floor(((code.charAt(1)-'0')*2)/10);
     b1=((code.charAt(1)-'0')*2)%10;
   
     a2=Math.floor(((code.charAt(3)-'0')*2)/10);
     b2=((code.charAt(3)-'0')*2)%10;

     a3=Math.floor(((code.charAt(5)-'0')*2)/10);
     b3=((code.charAt(5)-'0')*2)%10;

     a4=Math.floor(((code.charAt(6)-'0')*4)/10);
     b4=((code.charAt(6)-'0')*4)%10;
    
     Y=a1+b1+c1+a2+b2+c2+a3+b3+c3+a4+b4+c4;

     if ((Y % 10)==0)
        return true;
     else
         {
          if (code.charAt(6)=='7')
              {
                a5=Math.floor((a4+b4)/10);
		          b5=(a4+b4)%10;
                Y=a1+b1+c1+a2+b2+c2+a3+b3+c3+a5+c4;
                if ((Y %10)==0)
                    return true;
                else
                    {
                      alert("統一編號不正確");
		                 return false;
                    }
               }
          else
               {
                  alert("統一編號不正確");
                  return false;
               }
         }
}

/* ======================================================================
FUNCTION:  	IsInt_org
====================================================================== */
function IsInt_org(teststring) 
{
	// Return immediately if an invalid value was passed in
	if (teststring+"" == "undefined" || teststring+"" == "null" || teststring+"" == "")	
		return false;

	var isValid = true;

	// convert to a string for performing string comparisons.
	teststring += "";	

	for (i = 0; i < teststring.length; i++) 
       {
    	   if (!((teststring.charAt(i) >= "0") && (teststring.charAt(i) <= "9"))) 
            {
             	isValid = false;
       	      break;
		       }      
        } // END for   
   
  return isValid;
}  // end IsInt_org


/* ======================================================================
FUNCTION:  	確認密碼複雜度
====================================================================== */

function checkPassword(username, password)
{
	
	var point = 0;
	
	if (password == "")
	{
		return "";
	}
	
	if (password.length < 8)
	{
		return "密碼至少需要 8 碼長";
	}
	
	if (password.indexOf(username) != -1)
	{
		return "密碼不可含有使用統編";
	}
	if (password.match(/[a-z]+/)) //"有小寫"
	{  
	   ++point;
	}
	if (password.match(/[A-Z]+/)) //"有大寫"
	{
	   ++point;
	}
	if (password.match(/[0-9]+/)) //"有數字"
	{
	   ++point;
	}
	if (password.match(/[^a-zA-Z0-9]+/)) //"有符號"
	{
	   ++point;
	}
	if (point < 3)
	{
		return  "【密碼】強度不足!至少包含下列三種條件\n"
			  + "   條件:\n"
			  + "   1.至少含一個大寫英文字母\n"
			  + "   2.至少含一個小寫英文字母\n"
			  + "   3.至少含一個數字\n"
			  + "   4.至少含一特殊符號(非1-9，非A-Z，非a-z)\n"
			  + "請重新輸入!";
		
	}
	
	return "";
}