var notgiven = "";

function validateForm(form){
	validation = true;
	for (var i=0; i < required.length; i+=2){
		ele = eval("form."+required[i]);
		text= required[i+1]
		//validate(ele.name, ele.value, )
		validate(text, ele.value)
	}
	if (notgiven != ""){
		notgiven += " 不能空白!"
		alert (notgiven);
		notgiven ="";
		validation = false;
	}
	for (i=0; i < form.length; i++){
		if ((form.elements[i].type == 'text') || (form.elements[i].type == 'textarea')){
			if (!chk_quote(form.elements[i].name,form.elements[i])){
				validation = false;
			}
		}
	}
	if (!validation){
		return false
	}
	else return true;
}
function validate(text,fieldValue){
	if (isBlank(fieldValue)){
		notgiven += text + ", " 
	}
}
function isNum(n){
	num = true;
	var nv=n.value;
	flag= isNaN(nv);
	if (flag==true){
		 alert('請填數值!');
		 num=false;
	}
	if (!num){
		 n.value="";
		 n.focus()
		 return false;
	}
}
//檢查欄位長度
function check_text_len_range(text,num1,num2,name){
	var len = text.value.length;
	if(len==0)
		return true;
	if(len<num1){
		alert(name+"必須大於"+num1+"個字");
		text.focus();
		return false;	
	}else if(len>num2){
		alert(name+"必須小於"+num2+"個字");
		text.focus();
		return false;
	}else{
		return true;
	}
		
}
//檢查欄位長度是否為num
function check_text_len(text,num,name){
	var len = text.value.length;
	if(len==0)	return true;
	if(len!=num){
		alert(name+"必須為"+num+"個字");
		text.focus();
		return false;	
	}else{
		return true;
	}
		
}
function check_link(text){
	mail = text.value ;
	if(mail.length==0) return true;

	if (mail.indexOf("http://")!=0){      
		alert("請輸入正確的連結位址") ; 
		text.value="http://";
		text.focus();
		return false;    
 	}
 	return true;
	
}
function check_mail(text){
	mail = text.value ;
	if(mail.length==0) return true;

	if (mail.indexOf("@")==-1){      
		alert("請輸入正確的E-mail位址") ;   
		text.focus();
		return false;    
 	}
 	if (mail.indexOf(".")==-1){      
		alert("E-mail格式錯誤")     
	   	text.focus();
	   	return false;
 	}
 	return true;
	
}
function checkBlank( txt )
{
    if(isBlank(txt.value)){
		alert('欄位不能為空白');
		txt.focus();
	}
}
function isBlank(s){
	var len=s.length;
	var i;
	for(i=0; i<len; ++i){
		if (s.charAt(i)!=" ") return false
	}
	return true;
}
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_password(Password,PasswordConfirm) {
   if (Password.value != PasswordConfirm.value){
      alert ('密碼與確認密碼不符');
 	  return false;
	}else{ 
		return true;
	}
}
function check_text ( text, name )
{
    if( text.value == "")
        return name+'\n' ;
    return "";
}
 
function check_select ( select, name ){
    if( select.options[0].selected == true )
        return name+'\n' ;
    return "";
}
//檢查欄位中的值是否為空值
function chk_notnull(title,pfield) {
  if (pfield.value=="")
  {
	pfield.focus();
	alert(title+"欄位必須填入資料！");
 	return false;
  }
  return true; 
} //function chk_notnull

//檢查欄位中的值是否有 ' 
function chk_quote(title,pfield) {
  //if (pfield.value.match(/'/)!=null)
  //{
//	pfield.focus();
//	alert("欄位中不能有單引號！");
// 	return false;
//  }
  return true; 
} //function chk_notnull

//檢查年月日欄位--->使用方法如：<select name="syear" Onchange='return ym_onchange(this.form.syear, this.form.smon, this.form.sday)'>
function ym_onchange(yy,mm,dd) {
     var month, year, day ;
     day = dd.selectedIndex ; 
     month = mm.options[mm.selectedIndex].value ;
     year = yy.options[yy.selectedIndex].value ;
     if (month == "1" || month == "3" || month == "5" || month == "7" || month == "8" || month == "10" || month == "12")     {
          for(i=1;i<=31;i++) {
              dd.options[i-1] = new Option(i);
              dd.options[i-1].value = i;
         }
          dd.length = "31" ;
     }
     if (month == "4" || month == "6" || month == "9" || month == "11") {
          for(i=1;i<=30;i++) {
              dd.options[i-1] = new Option(i);
              dd.options[i-1].value = i ;
         }
          dd.length = "30" ;
     }
     if (month == "2") {
         if (year%4 > 0)     {
              for(i=1;i<=28;i++) {
                   dd.options[i-1] = new Option(i);
                   dd.options[i-1].value = i;
              }
              dd.length = "28" ;
         }
         if (year%4 == 0)     {
              if ((year%100 == 0) && (year%400 > 0))    {
                   for(i=1;i<=28;i++) {
                        dd.options[i-1] = new Option(i);
                        dd.options[i-1].value = i;
                   }
                   dd.length = "28" ;                             
              }
              if ((year%100 > 0) || (year%400 == 0))  {
                   for(i=1;i<=29;i++) {
                        dd.options[i-1] = new Option(i);
                        dd.options[i-1].value = i;
                   }
                   dd.length = "29" ;           
              }
         }
     }
     if (dd.length < day + 1)      {
          dd.selectedIndex = dd.length-1 ;
     }
     else {
          dd.selectedIndex = day ;
     }
}
// 檢查勾選(Radio)欄位是否已被勾選
function chk_radio(msg, pfield) {
	var checker = 0;
	for (var i=0; i<pfield.length; i++)
		if (pfield[i].checked) checker = 1 ;
	if (checker==0) {
		alert(msg);
		return false ;
	}
	return true ;
} //function chk_radio

// 檢查勾選欄位是否已被勾選
function chk_checked(fieldname, pfield1, pfield2) {
  if (pfield1.checked==false && pfield2.checked==false)
  {
	alert(fieldname + "必須二擇一或全部勾選！");
 	return false;
  }
  return true; 
} //function chk_checked 