// (c)2002-2009 Arrack Tsengvar 
d=document;function checkField(str1,msg,type,str2,notaddstr,minlength,maxlength){var d=document;var obj=d.getElementById(str1);var var1='';var var2='';if(str2!=null){var obj2=d.getElementById(str2);}if(obj==null){alert("function error01:obj1 null:"+str1);return'';}if(type=="and"||type=="or"||type=="equal"){if(obj2==null){alert("function error01:obj2 null:"+str2);return'';}var2=getvalue(obj2);}var1=getvalue(obj);if(type==1||typeof(type)=="undefined"||typeof(type)=="null"||type==''){if(var1==''){return return_value(str1,obj,str2,msg,type,notaddstr);}}if(type=="and"){if(var1==''||var2==''){return return_value(str1,obj,str2,msg,type,notaddstr);}}if(type=="or"){if(var1==''&&var2==''){return return_value(str1,obj,str2,msg,type,notaddstr);}}if(type=="equal"){if(var1==var2){}else{return return_value(str1,obj,str2,msg,type,notaddstr);}}if(type=="email"){if(msg==""){msg="未填寫或是格式錯誤";}if(!is_valid_email(var1)||var1==''){return return_value(str1,obj,str2,msg,type,notaddstr);}}if(obj.value!=""){if(typeof(minlength)!="undefined"&&typeof(minlength)!="null"){if(obj.value.length<minlength){return return_value(str1,obj,str2,msg,type,notaddstr);}}if(typeof(maxlength)!="undefined"&&typeof(maxlength)!="null"){if(obj.value.length>maxlength){return return_value(str1,obj,str2,msg,type,notaddstr);}}}return'';}function return_value(str1,obj,str2,msg,type,notaddstr){if(msg==""){if(obj.type=="select-one"||obj.type=="select-multiple"){msg="未選取";}else if(obj.type=="radio"){msg="未點選";}else if(obj.type=="checkbox"){msg="未勾選";}else{msg="未填寫";}}if(typeof(notaddstr)=="undefined"||typeof(notaddstr)=="null"){if(type=="and"){return str1+'和'+str2+msg+'\n';}if(type=="or"){return str1+'或'+str2+msg+'\n';}if(type=="equal"){return str1+'和'+str2+'不相同\n';}return str1+msg+'\n';}else{return msg+'\n';}}function getvalue(objs){var type=objs.type;var value_temp='';if(type=="text"||type=="textarea"||type=="password"||type=="file"){return objs.value;}if(type=="radio"||type=="checkbox"){var objs=d.getElementsByName(objs.id);for(var s=0;s<objs.length;s++){if(objs[s].checked){if(value_temp==""){value_temp=objs[s].value;}else{value_temp=value_temp+','+objs[s].value;}}}return value_temp;}if(type=="select-one"||type=="select-multiple"){for(var s=0;s<objs.length;s++){if(objs[s].selected){if(value_temp==""){value_temp=objs[s].value;}else{value_temp=value_temp+','+objs[s].value;}}}return value_temp;}alert('getvalue function error:unknown type');return"";}function is_valid_email(email){return/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email);}function availCheck(obj,opt){if(opt){obj.value=obj.value.toUpperCase();}obj.value=obj.value.replace(/[^a-z0-9]/gi,'');}
// New
function a$(id){return document.getElementById(id);}
function checkTWID(idStr){
  var letters = new Array('A', 'B', 'C', 'D','E', 'F', 'G', 'H', 'J', 'K', 'L', 'M',
      'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V','X', 'Y', 'W', 'Z', 'I', 'O');
  var multiply = new Array(1, 9, 8, 7, 6, 5, 4, 3, 2, 1);
  var nums = new Array(2);
  var firstChar;
  var firstNum;
  var lastNum;
  var total=0;

  var regExpID=/^[a-zA-Z](1|2)\d{8}$/i;
  if (idStr.search(regExpID)==-1) {
	return "身份證號碼格式錯誤\n";
  } else {
	firstChar = idStr.charAt(0).toUpperCase();
	lastNum = idStr.charAt(9);
  }
  
  for (var i=0; i<26; i++) {
	if (firstChar == letters[i]) {
	  firstNum = i + 10;
	  nums[0] = Math.floor(firstNum / 10);
	  nums[1] = firstNum - (nums[0] * 10);
	  break;
	}
  }
  for(var i=0; i<multiply.length; i++){
    if (i<2) {
      total += nums[i] * multiply[i];
    }else{
      total += parseInt(idStr.charAt(i-1)) * multiply[i];
    }
  }
  tn=10 - (total % 10);
  if(tn==10){tn=0;}
  if (tn!=lastNum) {
	return "身份證號碼寫錯了!\n";
  }
  return "";
}

function chstr(str){
   var subStr = "";
   for (i=0,n=str.length;i<n;i++){
      subStr = str.charCodeAt(i);
      if ((subStr < 256)){
         return false;
      }
   }
   return true;
}

function enstr(str){
	var re=/^[a-z0-9]{4,12}$/;   
	if (re.test(str)){
		return true;		
	}else{
		return false;		
	}
}


function resetForm(formname){
	document.getElementById(formname).reset();
	return true;
}
