function vldid(s) {
	var c, n, i;
	var t= "ABCDEFGHJKLMNPQRSTUVXYWZIO";

	c= s.substring(0,1);
	c= t.indexOf(c.toUpperCase());
	if((s.length!= 10) || (c<0)) 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))) return false;

	return true;
	}

function checkid(s) {
	var v= vldid(s);
	if(v) alert("身份證號碼正確無誤！");
	else alert("身份證號碼有問題！");
	}
	
function iseng(a) {
  var big = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  var x='abcdefghijklmnopqrstuvwxyz';
  if ((a.length == 1) && ((big.indexOf(a) != -1) || (x.indexOf(a) != -1))) {
    return true
  } else {
    return false
  }
}

function isnum(d) {
  var digit = "0123456789"

  if ((d.length == 1) && (digit.indexOf(d) != -1)) {
    return true
  } else {
    return false
  }
}

function id_check(p) {
  id=p.value;
  if (id.length == 10) {
    if ((iseng(id.charAt(0))) && 
	    ((id.charAt(1) == "1") || (id.charAt(1) == "2")) &&
		(isnum(id.charAt(2))) &&
		(isnum(id.charAt(3))) &&
		(isnum(id.charAt(4))) &&
		(isnum(id.charAt(5))) &&
		(isnum(id.charAt(6))) &&
		(isnum(id.charAt(7))) &&
		(isnum(id.charAt(8))) &&
		(isnum(id.charAt(9)))) {
	  return true
	} else {
          alert("身分證字號錯誤");
          p.focus();
	  return false
	}
  } else {
    alert("身分證字號錯誤");
    p.focus();
    return false
  }
}
function isEmail(q) {
  if (q.value.indexOf("@") != "-1" &&
      q.value.indexOf(".") != "-1" &&
      q.value != "")
  return true;
  else 
  {
  alert("請填寫完整E-MAIL資料");
  q.focus();
  return false;
  }
}
function checkNull(p) {
  if (btnTrimAll(p.value)=='') {
    alert("請填寫完整資料");
    p.value=btnTrimAll(p.value);
    p.focus();
    return false;
  }
}

function checkNullS(p) {
  if (p.options[p.selectedIndex].text=='' || p.options[0].selected) {
    alert("此欄位必須選取");
    p.focus();
    return false;
  }
}

function checkRadioNull(p) {
  for (var i=0; i<p.length;i++) {
    if (p[i].checked=="1") {
      return true;
    }
  }
  alert("請填寫完整資料");
  p[0].focus();
  return false;
}

function chk_content(p) {
  var str = p.value;
  var newstr = "";
  var cut=0;
  if (p.value && p.value != ' ') {
    for (var i = 0; i < str.length; i++) {
      ch = str.substring(i, i + 1)
      if (ch != "'") {
        newstr += ch;
      }
      else {
        newstr += "`";
        cut++
      }
    }
  var errmsg = "請勿使用單引號\n\n→ 為您替換 "+cut+" 個單引號 ←";
  if(cut>0) {
    alert(errmsg);
    p.value = newstr;
    p.focus();
    return false;
  }
  else return true
  }
  return true
}

function checkTime(p,Plen) {
  var str = p.value;
  var newstr = "";
  var cut=0;
  var errmsg = "時間格式錯誤!!!\n此欄位值必需為四位阿拉伯數字\n請以廿四時制時間表示\n例如：下午兩點十分請填1410";
  if (p.value && p.value != ' ') {
    for (var i = 0; i < str.length; i++) {
      ch = str.substring(i, i + 1)
      if ((ch >= "0" && ch <= "9")) {
        newstr += ch;
      }
      else cut++;
    }
    if(cut>0) {
      p.value = newstr;
      alert(errmsg);
      p.focus();
      return false;
    }
    p.value = newstr;
    if (!(str.length==Plen)) {
      alert("此欄位必需填滿"+Plen+"碼數字");
      p.focus();
      return false;
    }
    if (str.substr(0,2)>"24") {
      alert(errmsg+"\n\nError Hour : "+str.substr(0,2));
      p.focus();
      return false;
    }
    if (str.substr(2,2)>"59") {
      alert(errmsg+"\n\nError Minute : "+str.substr(2,2));
      p.focus();
      return false;
    }
  }
  else {
    return false;
  }
}

function checkNumber(p,Plen,MaxValue) {
  var str = p.value;
  var newstr = "";
  var cut=0;
  var errmsg = "數字格式錯誤!!!\n此欄位值必需為"+Plen+"位阿拉伯數字\n其值必須小於"+MaxValue;
  if (p.value && p.value != ' ') {
    for (var i = 0; i < str.length; i++) {
      ch = str.substring(i, i + 1)
      if ((ch >= "0" && ch <= "9")) {
        newstr += ch;
      }
      else cut++;
    }
    if(cut>0) {
      p.value = newstr;
      alert(errmsg);
      p.focus();
      return false;
    }
    p.value = newstr;
    if (!(str.length==Plen)) {
      alert("此欄位必需填滿"+Plen+"碼數字");
      p.focus();
      return false;
    }
    if (str>MaxValue) {
      alert(errmsg+"\n\nError Number : "+str);
      p.focus();
      return false;
    }
  }
  else {
    return false;
  }
}

function checkNumber1(p) {
  var str = p.value;
  var newstr = "";
  var cut=0;
  var errmsg = "數字格式錯誤!!!\n此欄位值必需為整數阿拉伯數字\n";
  if (p.value && p.value != ' ') {
    for (var i = 0; i < str.length; i++) {
      ch = str.substring(i, i + 1)
      if ((ch >= "0" && ch <= "9")) {
        newstr += ch;
      }
      else cut++;
    }
    if(cut>0) {
      p.value = newstr;
      alert(errmsg);
      p.focus();
      return false;
    }
    p.value = newstr;
  }
  else {
    return true;
  }
}
function checkNullNumber(p) {//不得輸入非數字以外
  var str = p.value;
  var newstr = "";
  var cut=0;
  var errmsg = "數字格式錯誤!!!\n此欄位值必需為整數阿拉伯數字\n";
  if (p.value) {
    for (var i = 0; i < str.length; i++) {
      ch = str.substring(i, i + 1)
      if ((ch >= "0" && ch <= "9")) {
        newstr += ch;
      }
      else cut++;
    }
    if(cut>0) {
      p.value = newstr;
      alert(errmsg);
      p.focus();
      return false;
    }
    p.value = newstr;
  }
  else {
    alert(errmsg);
    p.focus();
    return false;
  }
}
function checkNumberFloat(p) {
  var str = p.value;
  var newstr = "";
  var cut=0;
  var errmsg = "數字格式錯誤!!!\n此欄位值必需為阿拉伯數字\n";
  if (p.value && p.value != ' ') {
    for (var i = 0; i < str.length; i++) {
      ch = str.substring(i, i + 1)
      if ((ch >= "0" && ch <= "9") || ch == ".") {
        newstr += ch;
      }
      else cut++;
    }
    if(cut>0) {
      p.value = newstr;
      alert(errmsg);
      p.focus();
      return false;
    }
    p.value = newstr;
  }
  else {
    return true;
  }
}

function checkNullNumberFloat(p,len) {//不允許NULL,可輸入數字(包含小數點)
  p.value=btnTrimAll(p.value);
  if (p.value.length>0) {
    if (isNaN(p.value)) {
      alert("請輸入數字");
      p.focus();
      return false;
    }
    else {
      if (len!="") { return chkFloatFormat(p,len); }
    }
  }
  else {
    alert("請輸入數字");
    p.focus();
    return false;
  }
}

function chkFloatFormat(x, y) {
  var i = x.value.indexOf(".", 0);
  // 如果有小數點才做檢核
  if (i != -1) {
    var j = x.value.substring(i+1);
    if (j.length>y) {
      alert("小數點限制" + y + "位數、請重新輸入");
      x.focus();
      return false;
    }
  }
}

function checkNumber0(p) {
  if (p.value=='0') {
    p.value='';
    alert("不得為0!!!");
    p.focus();
    return false;
  }
}

function checkHalfNumer(p,NType) {//p欄位,NType:是否允許Null(Y:允許,N:不允許)
  p.value=btnTrimAll(p.value);
  if (p.value=="") {
    if (NType=="N") {
      p.focus();
      alert("請填寫完整資料");
      return false;
    }
  }
  else {
    for (i=0;i<p.value.length;i++)
    {
      c=p.value.charAt(i);
      if("0123456789".indexOf(c,0)<0) {
        alert("請輸入半形數字");
        p.focus();
        return false;
      }
    }
  }
}
function checkLength(p,Plen) {
  var str = p.value;
  var newstr = "";
  var cut=0;
  var errmsg = "格式錯誤!!!\n此欄位值必需填滿"+Plen+"位";
  if (p.value && p.value != ' ') {
    for (var i = 0; i < str.length; i++) {
      ch = str.substring(i, i + 1)
      newstr += ch;
    }
    p.value = newstr;
    if (!(str.length==Plen)) {
      alert(errmsg);
      p.focus();
      return false;
    }
  }
  else {
    alert(errmsg);
    p.focus();
    return false;
  }
}
function chk_day(year,month,day)  {
 var days;
  if(month.value=='1') { days=31; }
  if(month.value=='2') {
    if(((year.value*1) % 4)==0) {
      days=29;
      if(((year.value*1) % 100)==0) {
        days=28;
        if(((year.value*1) % 400)==0) {
          days=29;
        }
      }
    }
    else {
      days=28;
    }
  }
  if(month.value=='3') { days=31; }
  if(month.value=='4') { days=30; }
  if(month.value=='5') { days=31; }
  if(month.value=='6') { days=30; }
  if(month.value=='7') { days=31; }
  if(month.value=='8') { days=31; }
  if(month.value=='9') { days=30; }
  if(month.value=='10') { days=31; }
  if(month.value=='11') { days=30; }
  if(month.value=='12') { days=31; }
  if(day.value*1>days) {
    alert('沒有'+year.value+'年'+month.value+'月'+day.value+'日');
    day.focus();
    return false;
  }
}
function chk_dayen(year,month,day)  {
 var days;
  if(month.value=='1') { days=31; }
  if(month.value=='2') {
    if(((year.value*1) % 4)==0) {
      days=29;
      if(((year.value*1) % 100)==0) {
        days=28;
        if(((year.value*1) % 400)==0) {
          days=29;
        }
      }
    }
    else {
      days=28;
    }
  }
  if(month.value=='3') { days=31; }
  if(month.value=='4') { days=30; }
  if(month.value=='5') { days=31; }
  if(month.value=='6') { days=30; }
  if(month.value=='7') { days=31; }
  if(month.value=='8') { days=31; }
  if(month.value=='9') { days=30; }
  if(month.value=='10') { days=31; }
  if(month.value=='11') { days=30; }
  if(month.value=='12') { days=31; }
  if(day.value*1>days) {
    alert('沒有'+year.value+'年'+month.value+'月'+day.value+'日');
    day.focus();
    return false;
  }
}
function chk_day1(year,month,day)  {
 var days;
  if(month.value=='1') { days=31; }
  if(month.value=='2') {
    if(((year.value*1) % 4)==0) {
      days=29;
      if(((year.value*1) % 100)==0) {
        days=28;
        if(((year.value*1) % 400)==0) {
          days=29;
        }
      }
    }
    else {
      days=28;
    }
  }
  if(month.value=='3') { days=31; }
  if(month.value=='4') { days=30; }
  if(month.value=='5') { days=31; }
  if(month.value=='6') { days=30; }
  if(month.value=='7') { days=31; }
  if(month.value=='8') { days=31; }
  if(month.value=='9') { days=30; }
  if(month.value=='10') { days=31; }
  if(month.value=='11') { days=30; }
  if(month.value=='12') { days=31; }
  if(day.value*1>days) {
    alert('沒有'+(year.value*1-1911)+'年'+month.value+'月'+day.value+'日');
    day.focus();
    return false;
  }
  else {
    return true;
  }
}
function chk_daych(year,month,day)  {
 var days;
  if(month.value=='1') { days=31; }
  if(month.value=='2') {
    if(((year.value*1) % 4)==0) {
      days=29;
      if(((year.value*1) % 100)==0) {
        days=28;
        if(((year.value*1) % 400)==0) {
          days=29;
        }
      }
    }
    else {
      days=28;
    }
  }
  if(month.value=='3') { days=31; }
  if(month.value=='4') { days=30; }
  if(month.value=='5') { days=31; }
  if(month.value=='6') { days=30; }
  if(month.value=='7') { days=31; }
  if(month.value=='8') { days=31; }
  if(month.value=='9') { days=30; }
  if(month.value=='10') { days=31; }
  if(month.value=='11') { days=30; }
  if(month.value=='12') { days=31; }
  if(day.value*1>days) {
    alert('沒有'+(year.value*1-1911)+'年'+month.value+'月'+day.value+'日');
    day.focus();
    return false;
  }
  else {
    return true;
  }
}
function chk_dayCHTXT(year,month,day)  {
 var days,tmp_year;
 ChkYearValue_TW(year);
 tmp_year=year.value*1+1911;
  if(month.value=='1') { days=31; }
  if(month.value=='2') {
    if(((tmp_year) % 4)==0) {
      days=29;
      if(((tmp_year) % 100)==0) {
        days=28;
        if(((tmp_year) % 400)==0) {
          days=29;
        }
      }
    }
    else {
      days=28;
    }
  }
  if(month.value=='3') { days=31; }
  if(month.value=='4') { days=30; }
  if(month.value=='5') { days=31; }
  if(month.value=='6') { days=30; }
  if(month.value=='7') { days=31; }
  if(month.value=='8') { days=31; }
  if(month.value=='9') { days=30; }
  if(month.value=='10') { days=31; }
  if(month.value=='11') { days=30; }
  if(month.value=='12') { days=31; }
  if(day.value*1>days) {
    alert('沒有民國'+year.value+'年'+month.value+'月'+day.value+'日');
    day.focus();
    return false;
  }
  else {
    return true;
  }
}
function ChkYearValue_TW(Year_cols) {
  btnTrimAll(Year_cols.value);
  if(checkNumber1(Year_cols) == false) return false;
  if(Year_cols.value.length>0) {
    if(Year_cols.value*1<0) {
      alert("請輸入民國年請勿小於0！");
      Year_cols.focus();
      return false;
    }
    if(Year_cols.value*1>168) {
      alert("輸入民國年請勿超過168！");
      Year_cols.focus();
      return false;
    }
  }
}
function chk_Chineseday_OnlyValue_Fix(year,month,day,showyear,columname)  {
 var days;
  if(month*1==1) { days=31; }
  else if(month*1==2) {
    if(((year*1) % 4)==0) {
      days=29;
      if(((year*1) % 100)==0) {
        days=28;
        if(((year*1) % 400)==0) {
          days=29;
        }
      }
    }
    else {
      days=28;
    }
  }
  else if(month*1==3) { days=31; }
  else if(month*1==4) { days=30; }
  else if(month*1==5) { days=31; }
  else if(month*1==6) { days=30; }
  else if(month*1==7) { days=31; }
  else if(month*1==8) { days=31; }
  else if(month*1==9) { days=30; }
  else if(month*1==10) { days=31; }
  else if(month*1==11) { days=30; }
  else if(month*1==12) { days=31; }
  else { days=0; }

  if(day*1>days || month*1>12 || day*1==0) {
    alert('沒有'+showyear+'年'+month+'月'+day+'日');
    columname.focus();
    return false;
  }
  else {
    return true;
  }
}
function chk_oneday_ch(day) {
  var str = day.value;
  var days;
  var input_day;
  var input_month;
  var input_year;
  var real_year;

  if ((str.length!=6) && (str.length!=7)) {
    alert('日期輸入錯誤');
    day.focus();
    return false;
  }
  else {
    if (str.length==6) {
      input_year=str.substring(0,2);
      input_month=str.substring(2,4);
      input_day=str.substring(4,6);
    }
    if (str.length==7) {
      input_year=str.substring(0,3);
      input_month=str.substring(3,5);
      input_day=str.substring(5,7);
    }
    real_year=input_year*1+1911;
    if((input_month*1<1) || (input_month*1>12)) {
      alert('日期輸入錯誤');
      day.focus();
      return false;
    }
    if(input_month=='01') { days=31; }
    if(input_month=='02') {
     if(((real_year*1) % 4)==0) {
       days=29;
       if(((real_year*1) % 100)==0) {
         days=28;
         if(((real_year*1) % 400)==0) {
           days=29;
         }
       }
     }
     else {
       days=28;
     }
    }
    if(input_month=='03') { days=31; }
    if(input_month=='04') { days=30; }
    if(input_month=='05') { days=31; }
    if(input_month=='06') { days=30; }
    if(input_month=='07') { days=31; }
    if(input_month=='08') { days=31; }
    if(input_month=='09') { days=30; }
    if(input_month=='10') { days=31; }
    if(input_month=='11') { days=30; }
    if(input_month=='12') { days=31; }
    if(input_day*1>days) {
     alert('沒有'+input_year+'年'+input_month+'月'+input_day+'日');
     day.focus();
     return false;
    }
  }
}

function checkStrLen(str,limitlen)
{//計算字串長度
    var len=0;
    for(var i=0;i<str.value.length;i++)
    {
      len+=str.value.charCodeAt(i)>0xff?2:1; 
    }
    if (len>limitlen) {
      alert("欄位超過預設長度，請重新輸入！");
      str.focus();
      return false;
    }
    else {
      return true;
    }
}
function btnTrimLeft(x) {//去除左邊的空白
while(x.substring(0,1)==" ") {
x = x.substring(1,x.length);
}
return x;
}
function btnTrimRight(x) {//去除右邊的空白
while(x.substring(x.length-1,x.length)==" ") {
x = x.substring(0,x.length-1);
}
return x;
}
function btnTrimAll(x) {//去除二邊的空白
x = btnTrimLeft(x);
x = btnTrimRight(x);
return x;
}
function Fill0(p,q) {//前面補0,p欄位,q位數
  var GotID,tot,TempKey;
  p.value=btnTrimAll(p.value);
  //TempKey=btnTrimAll(p.value);
  if (p.value.length>0) {
    tot=p.value.length;
    GotID=p.value;
    if(tot*1<q*1) {
      for(i = tot; i <q*1; i++){
        GotID="0"+GotID;
      }
    }
    p.value=GotID;
  }
}
function Fill0New(p,q,r) {//前面補0,p欄位,q位數,r是否補0
  if (r.checked) {
    var GotID,tot;
    tot=p.value.length;
    GotID=p.value;
    if(tot*1<q*1) {
      for(i = tot; i <q*1; i++){
        GotID="0"+GotID;
      }
    }
    p.value=GotID;
  }
  else {
    if (p.value=="00000") {
      p.value="";
    }
  }
}
