$(document).ready(function(){

		$("a[id*='qa_list_']").each(function(){
				$(this).click(function(){
						//find uid
						var str=$(this).attr('id').split("_");
						var uid=str.slice(str.length-1);
						$('tr[id*="qa_th"]').hide();
						$('tr[id*="qa_td"]').hide();
						$('#qa_th_'+uid).show();
						$('#qa_td_'+uid).show();
				});
		});	
		/*
		if($('#subMiddle').length > 0){
				
				uid=$('#content_uid').val();

				//adjust left area and right has same height 
				/*var leftH=$('#innerLeftList_'+uid).css('height');
				var rightH=$('#article').css('height');
				leftH=leftH.substring(0,leftH.length-2);
				rightH=rightH.substring(0,rightH.length-2);
				if(leftH < rightH) $('#innerLeftList_'+uid).css('height',rightH+'px');
				if(leftH > rightH) $('#article').css('height',leftH+'px');

				//get innerLeftList item
				get_innerLeftList_item(uid);
				
				//get articleContent item
				get_articleContent_item(uid);

				if($('#donateForm').length > 0){
					//append require syntax				
					$('th.require').prepend('<span>*<\/span>');
					$('tr:odd th').css({'background':'#ffffff'});
					$('tr:odd td').css({'background':'#ffffff'});

					//the fifth or sixth form hide some item
					if($('#whtclass').val()==5 || $('#whtclass').val()==6) change_form_format("hide");
					//else change_form_format("show");
					
					//donateForm submit action			
					donateForm_submit();
					
					//change to the fifth or sixth form and change form format
					donateForm_store_form();
				}
				
		    if($('#formBaseMatch').length > 0){

				    //不做submit的動作
						$('form').bind('submit',function(){
					  		return false;
						});
		    	
				    //get base pair param
				    get_baseMatch_form_param();
				    
				    //baseMatch form submit action
				    baseMatch_form_submit();
				    
				    
				}
		    
		}*/
		
		
});

//讀取左邊選單List
function get_innerLeftList_item(uid){

		$.getJSON("lib/Controller.php?mode=getinnerLeftList&uid="+uid,function(json){
				$('#innerLeftList_'+uid).html('');
				var html='<ul>';
				var depth=2;
				$.each(json,function(i,datajson){
						if(datajson['depth']!=depth){
								html=html.slice(0,html.length-5);
								if(datajson['depth']>depth) html=html+'<ul>';
								if(datajson['depth']<depth) html=html+'</ul></li>';
								depth=datajson['depth'];
						}
						if(datajson['link_type']==1 && datajson['link']!='' && datajson['link']!=null ) html=html+'<li><a href="'+datajson['link']+'">'+datajson['title']+'</a></li>';
						else html=html+'<li><a href="inside.php?r='+datajson['uid']+'&style_code='+datajson['style_color']+'">'+datajson['title']+'</a></li>';
				});
				if(depth==3) html=html+'</ul></li>';
				/*if(depth==2) html=html+'</ul></li>';
				else if(depth==3) html=html+'</ul></li></ul></li>'; */
				html=html+'</ul>';
				$('#innerLeftList_'+uid).append(html);
		});
}

//讀取網頁內容
function get_articleContent_item(uid){
		$.getJSON("lib/Controller.php?mode=getarticleContent&uid="+uid,function(json){
				
				$.each(json,function(i,datajson){
						$('#article_title').html(datajson['title']);
						if(datajson['link_type']==4) {//內容
								$('#articleContent_'+uid).html(datajson['content']);
						}
						else if(datajson['link_type']==3){	//網頁內容類別
						//alert(uid+'_'+datajson['category_form_type']+'_'+datajson['category_uid']);
								get_category_content(uid,datajson['category_form_type'],datajson['category_uid']);
						}
						else if(datajson['link_type']==2){//檔案下載
								
						}else if(datajson['link_type']==1 && $('#form_type').val()!=''){
								get_donateForm();
						}
				});
		});
}


function get_donateForm(form_type){
		$('#donateForm').show();
		$('#whtclass').attr("disabled","true");
}

function donateForm_submit() {
		$('#form_submit').mouseup( function(){	
				var cdate=$('#cdate').html();																			//填表日期
				$('#whtclass').attr("disabled","false");
				var whtclass=$('#whtclass').val();																//捐贈項目
				var donateBefore = $('input[name=donateBefore]:checked').val();		//是否曾經捐贈過
				var yourname=$('#yourname').val();																//姓名
				if($('input[name=sex]:checked').val()==undefined) 								//性別
						var sex_female='';
				else var sex_female=$('input[name=sex]:checked').val();
				var mensesMonth=$('#mensesMonth').val();													//上次月經日期--月
				var mensesMday=$('#mensesMday').val();														//上次月經日期--日
				var birthYear=$('#birthYear').val();															//出生年月日--年
				var birthMonth=$('#birthMonth').val();														//出生年月日--月
				var birthMday=$('#birthMday').val();															//出生年月日--日
				var socialNumber=$('#socialNumber').val();												//身份證字號
				var height=$('#height').val();																		//身高
				var weight=$('#weight').val();																		//體重
				var education = $('#education').val();														//最高學歷
				var marriage = $('input[name=marriage]:checked').val();						//婚姻狀態
				if($('input[name=race]:checked').val()==undefined)								//族群
						var race = '';
				else var race = $('input[name=race]:checked').val();
				var skinColor = $('input[name=skinColor]:checked').val();					//膚色
				var sexHistory = $('input[name=sexHistory]:checked').val();				//是否有過性行為
				var bloodType = $('input[name=bloodType]:checked').val();					//血型
				var email = $('#email').val();																		//email
				var telephone = $('#telephone').val();														//聯絡電話
				var mobile = $('#mobile').val();																	//手機號碼
				var address = $('#address').val();																//通訊住址
				var address2 = $('#address2').val();															//其他聯絡方式
				var appointmentMonth = $('#appointmentMonth').val();							//預約日期--月
				var appointmentMday = $('#appointmentMday').val();								//預約日期--日
				var appointmentPeriod = $('#ju_sec').val();												//時段
				if($('input[name=toknow]:checked').val()==undefined)							//您經由何種方式知道本服務
						var toknow = '';
				else var toknow = $('input[name=toknow]:checked').val();
				if($('input[name=agree_state]:checked').val()==undefined)					//請確定詳細閱讀過人工生殖法
						var agree_state = '';
				else var agree_state = $('input[name=agree_state]:checked').val();				
				$('#donateForm').validationEngine({
						ajaxSubmit: true,
						ajaxSubmitFile: "lib/Controller.php",
						ajaxSubmitExtraData:"mode=donateFormSubmit&cdate="+cdate+"&whtclass="+whtclass+"&donateBefore="+donateBefore+"&yourname="+yourname+"&sex_female="+sex_female+"&mensesMonth="+mensesMonth+"&mensesMday="+mensesMday+"&birthYear="+birthYear+"&birthMonth="+birthMonth+"&birthMday="+birthMday+"&socialNumber="+socialNumber+"&height="+height+"&weight="+weight+"&education="+education+"&marriage="+marriage+"&race="+race+"&skinColor="+skinColor+"&sexHistory="+sexHistory+"&bloodType="+bloodType+"&email="+email+"&telephone="+telephone+"&mobile="+mobile+"&address="+address+"&address2="+address2+"&appointmentMonth="+appointmentMonth+"&appointmentMday="+appointmentMday+"&appointmentPeriod="+appointmentPeriod+"&toknow="+toknow+"&agree_state="+agree_state,
						ajaxSubmitMessage: "謝謝您的填寫，我們會派專人與您連絡！",
						success :  false,
						failure : function() {}
				});	
		});
}

function donateForm_store_form(){
		$('#whtclass').change(function(){
				
				if($('#whtclass').val()==5 || $('#whtclass').val()==6){
						change_form_format("hide");
				}
				else{
						change_form_format("show");
				}
		});
}

function change_form_format(key){
//key is "hide" or "show"
		if(key=="hide"){
				$('#menses').hide();
				$('#donateBefore_tr').hide();
				$('#height_tr').hide();
				$('#weight_tr').hide();
				$('#education_tr').hide();
				$('#race_tr').hide();
				$('#skinColor_tr').hide();
				$('#bloodType_tr').hide();
				$('#appointment_tr').hide();
				$('#notice').hide();
		}
		else{
				$('#menses').show();
				$('#donateBefore_tr').show();
				$('#height_tr').show();
				$('#weight_tr').show();
				$('#education_tr').show();
				$('#race_tr').show();
				$('#skinColor_tr').show();
				$('#bloodType_tr').show();
				$('#appointment_tr').show();
				$('#notice').show();							
		}
}

function get_category_content(uid,category_form_type,category_uid){//nav uid,category form type,category uid
		//alert(uid+'_'+category_form_type+'_'+category_uid);
		$.ajax({
				type: "POST",
				url: "lib/Controller.php?mode=get_category_content",
				data: {nav_uid:uid,category_form_type:category_form_type,category_uid:category_uid},
				dataType: "json",
				async: false,
				success: function(json){
						jsondata=json;
				}
		});		
		
		if(category_form_type==2){//Q&A category
				get_qa_content(jsondata);
		}
		else if(category_form_type==4){//Download category
				get_download_content(jsondata);
		}
		else if(category_form_type==5){
				$('#articleContent_'+uid).html('關鍵字搜尋：<input id="txt_ga_search" type="text" >');
				$('#articleContent_'+uid).append('<input id="btn_qa_search" type="button" value="搜尋" >');
				$('#articleContent_'+uid).append('<p><hr>');
				$('#articleContent_'+uid).append('<div id="ga_list"></div>');
				get_ga_content(jsondata);
				//click action		
				$('#btn_qa_search').click(function(){
						var val=$('#txt_ga_search').val();
						
						$.ajax({
								type: "POST",
								url: "lib/Controller.php?mode=get_category_content",
								data: {nav_uid:uid,category_form_type:category_form_type,category_uid:category_uid,key:val},
								dataType: "json",
								async: false,
								success: function(json){
										jsondata=json;
								}
						});								
						get_ga_content(jsondata);
				});					
		}

}

function get_qa_content(qadata){
	
		//create qa content
		$('#articleContent_'+uid).html('');
		var html='';
		html=html+'<ul class="title_list">';
		$.each(qadata,function(idx,qalist){
				//html=html+'<li ><a id="qa_list_'+qalist['uid']+'" href="#'+qalist['uid']+'">'+qalist['question']+'</a></li>';
				html=html+'<li ><a id="qa_list_'+qalist['uid']+'" >'+qalist['question']+'</a></li>';
		});
		html=html+'</ul>';
		html=html+'<hr>';
		html=html+'<table id="answer_table">';
		$.each(qadata,function(idx,qalist){
				html=html+'<tr id="qa_th_'+qalist['uid']+'" class="hide">';
				html=html+'		<th><h3 class="question" name="'+qalist['uid']+'">'+qalist['question']+'</h3></th>';
				html=html+'</tr>';
				html=html+'<tr id="qa_td_'+qalist['uid']+'" class="hide">';
				html=html+'		<td><div class="answer">'+qalist['answer']+'</div></td>';
				html=html+'</tr>';
					
		});
		html=html+'</table>';
		$('#articleContent_'+uid).html(html);

		//click action		
		$("a[id*='qa_list_']").each(function(){
				$(this).click(function(){
						//find uid
						var str=$(this).attr('id').split("_");
						var uid=str.slice(str.length-1);
						$('tr[id*="qa_th"]').hide();
						$('tr[id*="qa_td"]').hide();
						$('#qa_th_'+uid).show();
						$('#qa_td_'+uid).show();
				});
		});				
	
}

function get_download_content(downloaddata){
		//create download content
		$('#articleContent_'+uid).html('');
		var html='';
		html=html+'<ul class="title_list">';
		$.each(downloaddata,function(idx,list){
				//html=html+'<li ><a id="qa_list_'+qalist['uid']+'" href="#'+qalist['uid']+'">'+qalist['question']+'</a></li>';
				html=html+'<li ><a href="getfile.php?mode=get_file&uid='+list['uid']+'" target="_Black" >'+list['title']+'</a></li>';
		});
		html=html+'</ul>';
		$('#articleContent_'+uid).html(html);
}

function get_ga_content(gadata){
		$('#ga_list').html('');
		var html='';
		html=html+'<table id="ga_table">';
		html=html+'<tr>';
		html=html+'		<th>名稱</th>';
		html=html+'		<th>英文名稱</th>';
		html=html+'		<th>縮寫</th>';
		html=html+'		<th></th>';
		html=html+'</tr>';
		$.each(gadata,function(idx,list){
				html=html+'<tr>';
				html=html+'		<td>'+list['name']+'</td>';
				html=html+'		<td>'+list['english_name']+'</td>';
				html=html+'		<td>'+list['abbreviation']+'</td>';
				html=html+'		<td>';
				if(list['url'].length >1){
						html=html+'				<a href="'+list['url']+'"  target="_blank"><div class="browser_IE_sprite"><img src="images/browser-icons.jpg"></div></a>';
				}
				html=html+'		</td>';
				html=html+'</tr>';
		});
		html=html+'</table>';
		$('#ga_list').append(html);
	
}

//簡易配對參數取得
function get_baseMatch_form_param(){

		$.ajax({
				type: "POST",
				url: "lib/Controller.php?mode=getBaseFormMatch",
				dataType: "json",
				async: false,
				success: function(match_data){
						json=match_data;
				}
		});		
	
		var html='';
		$('#formMatchItem').html('');
    html=html+'<span id="pair_type_title">配對項目︰</span>';
		html=html+'<input type="radio" id="pair_type_1" name="pair_type" value="卵子" />卵子';
		html=html+'<input type="radio" id="pair_type_2" name="pair_type" value="精子" />精子';
		html=html+'<br />';
    $.each(json,function(idx,list){
    		html=html+'<label id="label_'+idx+'" for="item_1">'+list['name']+'</label>';
    		html=html+'<select id="item_'+idx+'" >';
 				html=html+'<option value="">請選擇</option>';
    		$.each(list['item'],function(itdm_idx,item){
    				html=html+'<option value="'+item['uid']+'">'+item['name']+'</option>';
    		});
    		html=html+'</select>';
    });
    $('#formMatchItem').append(html);
    
    //do home content pair match search action		
    home_pair_action();
		
}

function baseMatch_form_submit(){

		$('#form_submit').mouseup( function(){
				var pair_type="";
				$("input:radio[id*='pair_type_']:checked").each(function(){
						if($(this).val().length > 0) pair_type=$(this).val();
				});
				
				if(pair_type.length > 0){ //pair_type checked
						var val="";
						$("select[id*='item_']").each(function(){
								if($(this).val().length > 0) val=val+$(this).val()+',';
						});
						get_baseMatch_search(pair_type,val);
				}
				else {
						alert("請選擇配對項目！");
				}
    });

}

function get_baseMatch_search(pair_type,param){

		$.ajax({
				type: "POST",
				url: "lib/Controller.php?mode=get_baseMatch_search",
				data: {pair_type:pair_type,pair_param:param},
				dataType: "json",
				async: false,
				success: function(json){
						jsondata=json;
				}
		});
		
		$('#pair_list').html('');
		var html='';
		html=html+'<table id="pair_table">';
		html=html+'<tr>';
		html=html+'		<th>捐贈項目</th>';
		html=html+'		<th>血型</th>';
		html=html+'		<th>身高</th>';
		html=html+'		<th>體重</th>';
		html=html+'		<th>學歷</th>';
		html=html+'</tr>';
		$.each(jsondata,function(idx,list){
				html=html+'<tr>';
				html=html+'		<td>'+list['Cus捐贈項目']+'</td>';
				html=html+'		<td>'+list['Cus血型']+'</td>';
				html=html+'		<td>'+list['Cus身高']+'cm</td>';
				html=html+'		<td>'+list['Cus體重']+'kg</td>';				
				html=html+'		<td>'+list['Cus學歷']+'</td>';
				html=html+'</tr>';
		});
		html=html+'</table>';
		$('#pair_list').append(html);		
		
}

function home_pair_action(){
	
		if($('#value_type').val().length >0 || $('#value_1').val().length >0 || $('#value_2').val().length >0 || $('#value_3').val().length >0 ){
				var value_type='';
				if($('#value_type').val().length >0) {
						value_type=$('#value_type').val();
				    				
    				//set pair_type radio checked
						$("input:radio[id*='pair_type_']").each(function(){
								if($(this).val()==value_type) $(this).attr("checked",true);;
						});
    		}
    		var val='';
    		if($('#value_1').val().length >0 ){
    				val=val+$('#value_1').val()+',';
    				
    				//set first select item selected
    				$("select[id*='item_']:eq(0)").children().each(function(){
    						if ($(this).val()==$('#value_1').val()){
										$(this).attr("selected",true); 
		    				}
    				});
    		}
    		if($('#value_2').val().length >0 ){
    				val=val+$('#value_2').val()+',';

						//set second select item selected
    				$("select[id*='item_']:eq(1)").children().each(function(){
    						if ($(this).val()==$('#value_2').val()){
										$(this).attr("selected",true); 
		    				}
    				});

    		}
    		if($('#value_3').val().length >0 ) {
    				val=val+$('#value_3').val()+',';

						//set third select item selected
    				$("select[id*='item_']:eq(2)").children().each(function(){
    						if ($(this).val()==$('#value_3').val()){
										$(this).attr("selected",true); 
		    				}
    				});
    				
    		}
    		if(val.length >0) get_baseMatch_search(value_type,val);
    }
}
