﻿var id = '';
var check_array = [];

function validator(bk_link){
	    
	check_array = [];
    
    //由 *_msg 來判斷需要驗證的欄位並加入 check = false
	$('div[id$=_msg]').each(function(){
		//alert(this.id+'\n'+this.name+'\n'+$.isArray(this.id));
		eval('check_array.push({id:"'+this.id.substring(0,this.id.length-4)+'",flag:false})');
        document.getElementById(this.id).innerHTML = '';
	})
	
    //=======================================================
    //  補捉所有 input, textarea, select, button 元素
    //=======================================================
	//alert($("#form1 :input").size());
	//$("form1 input").each(
	$("input,textarea").each(
		function(){
            //*
            //========================================
			//  一般文字輸入情況
            //========================================
			if(this.id.match(/company|department|contact/)){
                //不能為空值
				if(!this.value){
                    document.getElementById(this.id+"_msg").innerHTML = '<span style="color:red;">'+JS_OL.CAN_NOT_BE_NULL_OL+'</span>';
                    document.getElementById(this.id).focus();
				}
                //長度太短
                else if(this.value.length<2){
                    document.getElementById(this.id+"_msg").innerHTML = '<span style="color:red;">'+JS_OL.THE_INPUT_STRING_IS_TOO_SHORT_OL+'</span>';
                    document.getElementById(this.id).focus();
				}
                //通過
                else{
					set_check_true(this.id);
				}
			}
            //====================================
            // 必需為數字，且不能為空值
            //====================================
            else if(this.id.match(/1234/)){
                //不通過 (不能為空值)
                if(!$('#'+this.id).val()){
                    document.getElementById(this.id+"_msg").innerHTML = '<span style="color:red;">'+JS_OL.CAN_NOT_BE_NULL_OL+'</span>';
                    document.getElementById(this.id).focus();
                }
                //不通過 (排序必需是數字)
                else if(isNaN($('#'+this.id).val())){
                    document.getElementById(this.id+"_msg").innerHTML = '<span style="color:red;">'+JS_OL.MUST_BE_NUMBERIC_OL+'</span>';
                    $('#'+this.id).focus();
                }
                //通過
                else{
                    set_check_true(this.id);
                }
            }
            //==================================================
			//  mail (必需驗證格式)
            //==================================================
			else if(this.id.match(/email$/)){
				//不通過
				if(!this.value){
                    document.getElementById(this.id+"_msg").innerHTML = '<span style="color:red;">'+JS_OL.CAN_NOT_BE_NULL_OL+'</span>';
                    document.getElementById(this.id).focus();
				}
                //不通過
                else if(!this.value.match(/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/)){
                    document.getElementById(this.id+"_msg").innerHTML = '<span style="color:red;">'+JS_OL.EMAIL_FORMAT_ERROR_OL+'</span>';
                    document.getElementById(this.id).focus();
				}
                //通過
                else{
					set_check_true(this.id);
				}
			}
            //====================================
            // 連絡電話
            //====================================
            else if(this.id.match(/tel/)){
                //不能為空值
				if(!this.value){
                    document.getElementById(this.id+"_msg").innerHTML = '<span style="color:red;">'+JS_OL.CAN_NOT_BE_NULL_OL+'</span>';
                    document.getElementById(this.id).focus();
				}
                //長度太短
                else if(this.value.length<8){
                    document.getElementById(this.id+"_msg").innerHTML = '<span style="color:red;">'+JS_OL.THE_INPUT_STRING_IS_TOO_SHORT_OL+'(<8)</span>';
                    document.getElementById(this.id).focus();
				}
                //通過
                else{
					set_check_true(this.id);
				}
            }
            //====================================
            // 內容
            //====================================
            else if(this.id.match(/question/)){
                //不能為空值
				if(!this.value){
                    document.getElementById(this.id+"_msg").innerHTML = '<span style="color:red;">'+JS_OL.CAN_NOT_BE_NULL_OL+'</span>';
                    document.getElementById(this.id).focus();
				}
                //長度太短
                else if(this.value.length<10){
                    document.getElementById(this.id+"_msg").innerHTML = '<span style="color:red;">'+JS_OL.THE_INPUT_STRING_IS_TOO_SHORT_OL+'(<10)</span>';
                    document.getElementById(this.id).focus();
				}
                //通過
                else{
					set_check_true(this.id);
				}
            }
            //----------------------------------------------------------------------------
            //  以下為 特定 bk_link 
            //----------------------------------------------------------------------------
            /*
            //====================================
            //  活動的年齡限制
            //====================================
            else if(this.id.match(/structure_act_age/)){
                if(!$('#'+this.id).val()){
                //不通過 (最低年齡不能為空值)
                    document.getElementById(this.id+"_msg").innerHTML = '<span style="color:red;">'+JS_OL.CAN_NOT_BE_NULL_OL+'</span>';
                    $(this.id).focus();
                }
                //不通過 (年齡必需是數字)
                else if(isNaN($('#'+this.id).val())){
                    document.getElementById(this.id+"_msg").innerHTML = '<span style="color:red;">'+JS_OL.MUST_BE_NUMBERIC_OL+'</span>';
                    $(this.id).focus();
                }
                //通過
                else{
                    set_check_true(this.id);
                }
            }
            //*/
            //====================================
			//  其餘未設定限制的頁面與欄位
            //====================================
			else{
                //通過
                set_check_true(this.id);
			}
		}
	);
	
	var text = '';
	for(var i=0;i<check_array.length;i++){
		text += check_array[i].id+'='+check_array[i].flag+'\n';
	}
    //==================
    //  debug
    //==================
	//alert(text);
	
	//送出表單
	submitform();
}
//
function set_check_true(id){
	for(var i=0;i<check_array.length;i++){
		if(check_array[i].id==id){
			check_array[i].flag=true;
			//alert(check_array[i].id+'='+check_array[i].flag);
		}
	}
}
//
function submitform(){
	//有一個以上的欄位需要驗證
	if(check_array.length > 0){
		check_ok = true;
		for(var i=0;i<check_array.length;i++){
			//alert(check_array[i].id+','+check_array[i].flag);
			if(!check_array[i].flag){
				check_ok = false;
			}
		}
		if(check_ok){
			with(document.form1){
				submit();
			}
		}
	}
	//完全沒有欄位需要驗證
	else{
		with(document.form1){
			submit();
		}
	}
}