function funStrFormat(iLen,sStr)
{  
    var bFormat = true;
    for (var iStr=1;iStr<=iLen;iStr++)
    {
      var sChar = sStr.substring(iStr,iStr+1);
		if(sChar.charCodeAt(0)<48 || sChar.charCodeAt(0)>57)
		{
   			bFormat = false;
   			break;
		}
	}
	return bFormat;
}

//檢查身分證字號
function check_sn ( sn )
{	var len = sn.length;
		if (len != "")
		{	NO = sf.sn.value;
			if (NO.charCodeAt(0) < 65 || NO.charCodeAt(0) > 90)
			{	
				return "* 身分證字號不正確，身分證字號的\n　第一碼應為大寫半形的英文字母 !\n";

				return false;
			}		
			else if (NO.length != 10) 
			{	
				return "* 身分證字號不正確，要有 10 個數字 !\n";
				return false;
			}
			else
			{	
				var flag = funStrFormat(NO.length,NO);
				if(flag == false)
				{	
					return "* 身分證字號錯誤，第二碼後，要有 9 個 0-9 數字組合 !\n";
					return false;
				}
			}
		}
		else
		{	
			return "* 身分證字號 !\n";
			return false;
		}
	return "";
}

function check_img_width(img_obj){
    var img_width = img_obj.width;
    if(img_width > (screen.width - 600)){
      img_width = (screen.width - 600);
    }
    img_obj.width = img_width;
    return true;
}

function check_null ( column, name )
{
    if( column.length == 0 )
        return name + "\n";
    return "";
}

function check_checkbox (box,name)
{
	var ck = document.tform.elements["b_no[]"]; 
    if (ck == null) return false; 
    if (isNaN(ck.length)) { 
      if (ck.checked) return "";
    } else {    
      for (var i=0;i<ck.length;i++) 
         if (ck[i].checked) return "";
    }        
    return name + "\n"; 
    return false
}

function check_radio ( radio, name )
{
	var error = true;
	for( i=0; i <radio.length; i++ )
	if( radio[i].checked == true ) {
		error = false;
		break;
	}
	if( error == true )
		return name + "\n";
		return "";
}

function check_select ( select, name )
{
    if( select.options[0].selected == true )
        return name + "\n";
    return "";
}

function check_email ( email )
{
var len = email.length;
if(len==0)
return "* 電子郵件\n";
for(var i=0;i<len;i++)
{ var c= email.charAt(i);
if(!((c>="A"&&c<="Z")||(c>="a"&&c<="z")||(c>="0"&&c<="9")||(c=="-")||(c=="_")||(c==".")||(c=="@")))
return "電子郵件地址只能是數字,英文字母及'-','_'等符號,其他的符號都不能使用 !\n";
}
if((email.indexOf("@")==-1)||(email.indexOf("@")==0)||(email.indexOf("@")==(len-1)))
return "* 電子郵件地址不合法 !\n";
if((email.indexOf("@")!=-1)&&(email.substring(email.indexOf("@")+1,len).indexOf("@")!=-1))
return "* 電子郵件地址不合法 !\n";
if((email.indexOf(".")==-1)||(email.indexOf(".")==0)||(email.lastIndexOf(".")==(len-1)))
return "* 電子郵件地址不完全 !\n";
return "";
}

//---------------萬年曆----------------------
var sMon = new Array(12);
	sMon[0] = "Jan"
	sMon[1] = "Feb"
	sMon[2] = "Mar"
	sMon[3] = "Apr"
	sMon[4] = "May"
	sMon[5] = "Jun"
	sMon[6] = "Jul"
	sMon[7] = "Aug"
	sMon[8] = "Sep"
	sMon[9] = "Oct"
	sMon[10] = "Nov"
	sMon[11] = "Dec"

function calendar(t) {
	var sPath = "../../include/calendar1.htm";
	strFeatures = "dialogWidth=245px;dialogHeight=206px;center=yes;help=no;status=no;border=thin;scrollbars=no";

	st = t.value;
	sDate = showModalDialog(sPath,st,strFeatures);
	t.value = formatDate(sDate, 0);
	
}

function checkDate(t) {
	dDate = new Date(t.value);
	if (dDate == "NaN") {t.value = ""; return;}

	iYear = dDate.getFullYear()

	if ((iYear > 1899)&&(iYear < 1950)) {

		sYear = "" + iYear + ""
		if (t.value.indexOf(sYear,1) == -1) {
			iYear += 100
			sDate = (dDate.getMonth() + 1) + "/" + dDate.getDate() + "/" + iYear
			dDate = new Date(sDate)
		}
	}



	t.value = formatDate(dDate);
}

function formatDate(sDate) {
	var sScrap = "";
	var dScrap = new Date(sDate);
	if (dScrap == "NaN") return sScrap;
	
	iDay = dScrap.getDate();
	iMon = dScrap.getMonth();
	iYea = dScrap.getFullYear();

	sScrap = iYea + "/" + (iMon + 1) + "/" + iDay ;
	return sScrap;
}

/*檢查日期的格式
/**********************************************************************/
/*Function name           :isDate(s,f)                                */
/*Usage of this function  :To check s is a valid format               */ 
/*Input parameter required:s=input string                             */
/*                           f=input string format                    */ 
/*                            =1,in mm/dd/yyyy format                 */
/*                            else in dd/mm/yyyy                      */
/*Return value                  :if is a valid date return 1          */ 
/*                           else return 0                            */ 
/*Function required          :isPositiveInteger()                     */
/**********************************************************************/   
   function isDate(s,f)
   {var a1=s.split("/");
    var a2=s.split("-");
    var e=true;
    if ((a1.length!=3) && (a2.length!=3))
      { 
        e=false;
      }
    else
      {if (a1.length==3)
         var na=a1;
       if (a2.length==3)
         var na=a2;  
       if (isPositiveInteger(na[0]) && isPositiveInteger(na[1]) && isPositiveInteger(na[2]))
         {  if (f==1)
              {var d=na[2],m=na[1];
              }
             else
              {var d=na[1],m=na[2];
              }  
             var y=na[0]; ;
             if ((e) && (y<1000))
                e=false
             if (e) 
                {
                  v=new Date(y+"/"+m+"/"+d);
                  if (v.getMonth()!=m-1)
                     e=false; 
                } 
         }
       else
        { 
          e=false;
        }        
      }   
    return e
   }

/**********************************************************************/
/*Function name           :isleapyear(iyear)                          */
/*Usage of this function  :To check whether iyear is leap year        */ 
/*Input parameter required:iyear                                      */
/*Return value                  :if is a leap year return 1           */ 
/*                           else return 0                            */ 
/**********************************************************************/
    function isleapyear(iyear)
    {         var l=new Array(100,400,4),leap=0,c;
         for (c=0;c<3;c++)
          {if (iyear%l[c]==0)
            leap++;
          }
        if ((leap==1)||(leap==3))//if leap year
         return(1);
        else
         return(0);  
    }

/**********************************************************************/
/*Function name           :isDigit(theDigit)                          */
/*Usage of this function  :test for an digit                          */ 
/*Input parameter required:thedata=string for test whether is digit   */
/*Return value                  :if is digit,return true              */ 
/*                           else return false                        */
/**********************************************************************/
function isDigit(theDigit)
{
  var digitArray = new Array('0','1','2','3','4','5','6','7','8','9'),j;
   
      for (j = 0; j < digitArray.length; j++)
         {if (theDigit == digitArray[j])
            return true
         }   
   return false

}
/*************************************************************************/
/*Function name           :isPositiveInteger(theString)                  */
/*Usage of this function  :test for an +ve integer                       */ 
/*Input parameter required:thedata=string for test whether is +ve integer*/
/*Return value                  :if is +ve integer,return true           */ 
/*                           else return false                           */
/*function require        :isDigit                                       */
/*************************************************************************/
function isPositiveInteger(theString)
{
   var theData = new String(theString)
 
   if (!isDigit(theData.charAt(0)))
      if (!(theData.charAt(0)== '+'))
         return false

   for (var i = 1; i < theData.length; i++)
      if (!isDigit(theData.charAt(i)))
         return false
   return true
}
function chkDate(cdate,message)
{
 var date_data=cdate.value;
 
    if (isDate(date_data,1)!=1)
      {
       alert(message);
       cdate.focus();
       return true;     
    }else{
       return false;	
      }
  
}

//----判斷是否為數字-----------------是：true，否：false
function isInt(num,message){
 var str=num.value;
 var count=0;
 for (var i=0;i<str.length;i++){
  	if((str.charAt(i)>="0" && str.charAt(i)<="9") || (str.charAt(i)=='-')){
  	}else{
		count++;
  	}
 }
 
 if(count>0){
 	alert(message);
	num.focus(); 
 	return false;
 }else {
 	return true;
 }
}

