var fileNo=0;
var member_num=0;

var form_id_note=new Array();
form_id_note['pid']='身分證字號';
form_id_note['name']='姓名';
form_id_note['sex']='性別';
form_id_note['birthyear']='身分證字號';
form_id_note['tel']='電話';
form_id_note['cid']='地址:縣市';
form_id_note['tid']='地址:鄉鎮';
form_id_note['address']='地址';
form_id_note['email']='E-mail信箱';
form_id_note['m_name[]']='姓名';
form_id_note['m_pid[]']='身分證字號/護照號碼';
form_id_note['m_country[]']='國別';
form_id_note['m_sex[]']='性別';
form_id_note['m_birthyear[]']='出生年';
form_id_note['m_tel[]']='電話';
form_id_note['m_cid[]']='現居縣市';
form_id_note['m_name']='姓名';
form_id_note['m_pid']='身分證字號/護照號碼';
form_id_note['m_country']='國別';
form_id_note['m_sex']='性別';
form_id_note['m_birthyear']='出生年';
form_id_note['m_tel']='電話';
form_id_note['m_cid']='現居縣市';

function check_submit(form_id)
{
	form_id='#'+form_id;
	
	if(document.getElementById('agree').checked==true)
	 	$(form_id).submit();
	else
		alert('請先詳讀相關規範並勾選【我已經詳讀並且同意福山植物園入園】，謝謝您！');
}

function check_form(form_id)
{
	form_id='#'+form_id;
	select_element=form_id+' select';
	
	var error_msg='';
	var hidden_error=false;
	
	var member_count=0;
	var temp_name='';
	var temp_array_name='';
	
	var check_PID_repeat_array=new Array();
	
	var temp=new Array();
	temp[0]=form_id+' input[type=hidden]';
	temp[1]=form_id+' input[type=text]';
	temp[2]=form_id+' input[type=radio]:checked';
		
	var m_country_array=new Array();
	
	for(i=0;i<temp.length;i++)
	{
		$(temp[i]).each(function(){

			temp_name=$(this).attr('name');
			if(temp_name.substring(temp_name.length-2,temp_name.length)=='[]')
			{
				if(temp_array_name=='')
					temp_array_name=temp_name;
						
				if(temp_array_name==temp_name)
					member_count++;
			}

			if(i==0 && $(this).attr('value')=='')
			{
				hidden_error=true;
			}
			else if($(this).attr('value')=='')
			{
				if(temp_name.substring(temp_name.length-2,temp_name.length)=='[]')
				{
					if(temp_name=='m_pid[]')
						check_PID_repeat_array.push($(this).attr('value').toUpperCase());
					error_msg+='您第 '+member_count+' 位隊員的 '+form_id_note[$(this).attr('name')]+" 欄位未填寫\n";
				}
				else
					error_msg+='您的 '+form_id_note[$(this).attr('name')]+" 欄位未填寫\n";
			}
			else
			{
				if(temp_name.substring(temp_name.length-2,temp_name.length)=='[]')
				{
					if(temp_name=='m_pid[]')
					{
						//如果國別資料陣列尚未產生則產生
						if(m_country_array.length==0)
						{
							$(select_element).each(function(){				//下拉式選單式的國別資料
								if($(this).attr('name')=='m_country[]')
								{									
									m_country_array.push($(this).find(':selected').attr('value'));
								}								
							});
							if(m_country_array.length==0)					//下拉式的沒抓到改抓INPUT的
							{
								$(temp[0]).each(function(){
									if($(this).attr('name')=='m_country[]')
									{									
										m_country_array.push($(this).attr('value'));
									}								
								});
							}
						}
						
						check_PID_repeat_array.push($(this).attr('value').toUpperCase());
						
						//由國別資料判斷要驗證的是身分證字號還是護照號碼
						if(m_country_array.length!=0)
						{
							if(m_country_array[member_count-1]==2)			//外籍人士
							{
								if(!check_opid($(this).attr('value')))
									error_msg+='您第 '+member_count+' 位隊員的 '+form_id_note[$(this).attr('name')]+" 欄位填寫錯誤\n";
							}
							else if(!check_pid($(this).attr('value')))		//本國人士
								error_msg+='您第 '+member_count+' 位隊員的 '+form_id_note[$(this).attr('name')]+" 欄位填寫錯誤\n";
						}
						else if(!check_pid($(this).attr('value')))
							error_msg+='您第 '+member_count+' 位隊員的 '+form_id_note[$(this).attr('name')]+" 欄位填寫錯誤\n";
					}
				}					
				else
				{
					if($(this).attr('name')=='email')
					{
						if(!check_email($(this).attr('value')))
							error_msg+='您的 '+form_id_note[$(this).attr('name')]+" 格式錯誤\nE-mail格式範例：fushanenter@serv.tfri.gov.tw";
					}
					else if($(this).attr('name')=='pid')
					{
						if(!check_pid($(this).attr('value')))
							error_msg+='您的 '+form_id_note[$(this).attr('name')]+" 填寫錯誤\n";
					}
				}
			}
			
			//alert($(this).attr('name')+'-'+$(this).attr('value'));
		});
	};
	
	if(check_PID_repeat_array.length>0)
	{
		var leader_pid=$('#pid').attr('value').toUpperCase();
		var temp_member_count_msg='';
		var k=0;
		
		for(i=0;i<check_PID_repeat_array.length;i++)	//先跟聯絡人的PID比對
		{			
			if(check_PID_repeat_array[i]==leader_pid)
			{
				temp_member_count_msg+=(i+1)+', ';
				check_PID_repeat_array[i]='';
			}				
		}
		
		if(temp_member_count_msg!='')
			error_msg+='您的第 '+temp_member_count_msg.substring(0,(temp_member_count_msg.length-2))+' 隊員與聯絡人的 '+form_id_note['m_pid[]']+" 欄位填寫重複\n";
		
		for(i=0;i<check_PID_repeat_array.length;i++)	//跟其他隊員做比對
		{
			k=0;
			temp_member_count_msg='';
			for(j=i+1;j<check_PID_repeat_array.length;j++)
			{
				if(check_PID_repeat_array[i]==check_PID_repeat_array[j] && check_PID_repeat_array[j]!='')
				{
					if(k==0)
					{
						temp_member_count_msg+=(i+1)+', ';
						temp_member_count_msg+=(j+1)+', ';
						k++;
					}
					else
						temp_member_count_msg+=(j+1)+', ';
							
					check_PID_repeat_array[j]='';
				}							
			}
			check_PID_repeat_array[i]='';
			if(temp_member_count_msg!='')
				error_msg+='您的第 '+temp_member_count_msg.substring(0,(temp_member_count_msg.length-2))+' 隊員 '+form_id_note['m_pid[]']+" 欄位填寫重複\n";
		}
	}
	
	member_count=0;
	temp_array_name='';
	
	$(select_element).each(function(){
		
		temp_name=$(this).attr('name');
		if(temp_name.substring(temp_name.length-2,temp_name.length)=='[]')
		{
			if(temp_array_name=='')
				temp_array_name=temp_name;
						
			if(temp_array_name==temp_name)
				member_count++;
		}
		
		if($(this).find(':selected').attr('value')=='' || $(this).find(':selected').attr('value')=='0')
		{
			if(temp_name.substring(temp_name.length-2,temp_name.length)=='[]')
				error_msg+='您第 '+member_count+' 位隊員的 '+form_id_note[$(this).attr('name')]+" 欄位未選取\n";
			else
				error_msg+='您的 '+form_id_note[$(this).attr('name')]+" 欄位未選取\n";
		}
		//alert($(this).attr('name')+'-'+$(this).find(':selected').attr('value'));		
	});
	
	if(error_msg!='')
	{
		alert(error_msg);
	}
	else
	{
		$(form_id).submit();
	}
}

function check_form1(form_id)
{
	var form_id_msg='#'+form_id+' [name=othermember]:checked';
	if($(form_id_msg).attr('value')=='Y')
	{
		$('#func').attr({value:'memberdata'});
		check_form(form_id);
	}
	else
	{
		if(confirm('您所選擇的是【沒有其他隊員】\n如果無誤，請點選【確定】，謝謝您！'))
		{
			$('#func').attr({value:'applydata_act'});
			check_form(form_id);
		}
	}
}

function apply_submit(form_id)
{
	if(confirm('您即將完成入園申請作業\n如果確定要申請此筆資料，請點選【確定】，謝謝您！'))
	{
		check_form(form_id);
	}
}

function apply_send_submit(form_id)
{
	if(confirm('您即將完成入園申請作業\n如果確定要申請此筆資料，請點選【確定】，謝謝您！'))
	{
		form_id='#'+form_id;
		$(form_id).submit();
	}
}

function cancel_submit(form_id)
{
	if(confirm('您即將取消本次申請資料，取消後的申請資料將【無法回復及查詢】\n如果確定要取消，請點選【確定】，謝謝您！'))
	{
		check_form(form_id);
	}
}

function change_submit(form_id)
{
	if(confirm('您即將依您所填入的資料作修改\n如果確定要修改，請點選【確定】，謝謝您！'))
	{
		check_form(form_id);
	}
}

function contact_submit(form_id)
{
	var error_msg='';
	form_id='#'+form_id;
	
	if($('#func').attr('value')!='sendmail_act')
	{
		alert('參數錯誤！');
		return false;
	}
	
	if($('#subject').attr('value')=='')
		error_msg+='您的 標題 欄位尚未填寫！\n';
	
	if($('#content').attr('value')=='')
		error_msg+='您的 意見回覆 欄位尚未填寫！\n';
		
	if($('#contact_name').attr('value')=='')
		error_msg+='您的 聯絡人 欄位尚未填寫！\n';
		
	if($('#contact_email').attr('value')=='')
		error_msg+='您的 聯絡信箱 欄位尚未填寫！\n';
	else if(!check_email($('#contact_email').attr('value')))
		error_msg+='您的 聯絡信箱 欄位格式填寫錯誤！\nE-mail格式範例：fushanenter@serv.tfri.gov.tw';
		
	
	if(error_msg!='')
		alert(error_msg);
	else if(confirm('您即將發送意見回覆信函給管理者\n如果確定要發送，請點選【確定】，謝謝您！'))
		$(form_id).submit();
}

function change_func_select(form_id,func)
{
	form_id='#'+form_id;
	$("#func").attr({value:func});
	$(form_id).submit();
}

function upload_file(form_id,file_type)
{
	form_id='#'+form_id;
	form_id_msg=form_id+' input[type=file]';
	file_name=$(form_id_msg).attr('value');
	if(file_name=='')
		alert('請先選取檔案！');
	else
	{
		file_name_type=file_name.substring(file_name.length-3,file_name.length).toLowerCase();
		if(file_name_type!=file_type)
			alert('很抱歉，只允許上傳 '+file_type+' 檔案！\n如該檔案已被開啟，請先關閉檔案再行上傳，謝謝您！')
		else
			$(form_id).submit();
	}
}

function download_file(form_id)
{
	form_id='#'+form_id;
	$(form_id).submit();
}

function reset_member_form()
{
	$("#member_number").html('0');
	$("#member_count").attr({"value":"0"});
	$("#member_table").html('');
	
	$("#delete_last_member").hide();
	
	add_new_member('');
}

function remove_last_member()
{
	m_count=$("#member_count").attr("value");
	m_number=$("#member_number").html();	
	
	m_count=parseInt(m_count);
	m_number=parseInt(m_number);
	
	if(m_count > 1 && m_number > 1)
	{
		fileNo=m_count-1;
		member_num=m_number-1;
		
		tempID='input_button_'+fileNo;
		tempJID='#input_button_'+fileNo;
		lastID='#member_number_'+(fileNo+1);
		
		html='<img src="images/next.jpg" alt="新增下一筆資料" width="110" height="24" style="cursor:pointer" onkeypress="javascript:add_new_member(\''+tempID+'\');" onclick="javascript:add_new_member(\''+tempID+'\');" />';
		$(tempJID).html(html);
		
		$(lastID).remove();
		
		$("#member_number").html(member_num);
		$("#member_count").attr({"value":member_num});
		
		if(member_num > 1)
			$("#delete_last_member").show();
		else
			$("#delete_last_member").hide();
	}
}

function delete_member_input(table_id)
{
	table_id='#'+table_id;
	$(table_id).html('');
	$("#member_table").find("span").each(function(){alert($(this).html())});
	
}

function check_email(email)
{
	var reg=/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/; //設定信箱格式
	
	if(reg.test(email))
		return true;
		
	return false;
}

function check_opid(opid)
{
	var reg=/^[A-Za-z0-9]+$/; //設定英數字串格式
	
	if(reg.test(opid))
		return true;
		
	return false;
}

function check_pid(pid)
{
	//第一碼英文字，其對應之二位數字為(10, 11, 12, 13, ... 35)
	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');
  	
	//將第一碼英文轉為二位數數字後，所得11碼數字之前10碼所相乘之數值，如A123456789 => 10123456789 => 1*1 + 0*9 + 1*8 + ... + 8*1
	var multiply = new Array(1, 9, 8, 7, 6, 5, 4, 3, 2, 1);
  
	var firstChar;
	var total = 0;
	
	//身分證字串正規表達式
  	var reg=/^[A-Z](1|2)\d{8}$/;
  
  	pid=pid.toUpperCase();
  
  	if(!reg.test(pid))
	{
		return false;
	}
  	else
	{
		firstChar=pid.charAt(0).toUpperCase();
		
		//將第一碼英文字轉換成對應的數值
  		for(var i=0; i<26; i++)
		{
			if (firstChar == letters[i])
			{
				pid=(i+10)+pid.substring(1,10);
				
	  			break;
			}
  		}
		
		for(var i=0; i<multiply.length; i++)
		{
      		total+=parseInt(pid.charAt(i))*multiply[i];
		}
		
		total+=parseInt(pid.charAt(10));
		
		if(total%10==0)
		{
			return true;
		}
    }
	
	return false;
}


function back_form(form_id,func)
{
	if(func!='')
	{
		form_id='#'+form_id;
		
		$(form_id).find('input[name=func]').attr({value:func});
		$(form_id).submit();
	}
}

function show_member(member_id)
{
	member_id='#'+member_id;
	$(member_id).toggle();
}

function leaderdata_submit()
{
	document.forms[0].submit();	
}

function leaderdata_submit2(aid)
{
	$("#aid").attr({value:aid});
	document.forms[0].submit();
}

function leaderdata_submit3(aid,func)
{
	$("#aid").attr({value:aid});
	$("#func").attr({value:func});
	document.forms[0].submit();
}

function change_member_submit(form_id,func,mbid)
{
	if(confirm('您即將更換該隊員資料，更換後將【無法再回復】，如確認無誤請點選【確定】，謝謝！'))
	{
		form_id='#'+form_id;
			
		$('#func').attr({value:func});
		$('#mbid').attr({value:mbid});
		$(form_id).submit();
	}
}
