var tel_chk=false,mobile_chk=false,c=0,tel_re=/^[\d\(\)\-\#]{1,20}$/,email_re=/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
$(function(){
/*
	$('input').focus(function(){ //活動結束
    alert('活動已結束。');
    return false;
	});*/

	$('#city').change(function(){ //選擇鄉鎮
		if($(this).val()==''){
			$('#town_box').html('');
		}
		else{
			$.ajax({
				type:"post",
				url:"index.php?mode=ajax&act=town",
				data:'k='+$(this).val(),
				dataType:"json",
				success: function(data){
					$('#town_box').html(data.content);
				}
			});
		}
	});

	$('#search_submit').click(function(){
//		if(!on){alert(err_msg);return false;}
		if($.trim($("#sn").val())==''){alert('請填寫序號');return false;}
		if($.trim($("#chk").val())==''){alert('請填寫驗證碼');return false;}
		if(!confirm('為保障您的中獎權利，請確認輸入的為真實姓名，以避免權益受損')){return false;}
		$('#search_submit').hide().next().show();
		$.ajax({
			type:"post",
			url:"index.php?mode=ajax&act=search",
			data:$('#pre_form').serialize(),
			dataType:"json",
			success: function(data){
				$('#chkc').val(data.a2);
				$('#chkc2').val(data.a3);
				$('#chk').val('');
				$('#auth_img').attr('src',data.a1);
				$('#insert_hint').html(data.msg).parent().parent().fadeIn('fast');
				$('#search_submit').show().next().hide();
			}
		});
	});

	$('#pre_submit').click(function(){
		if(!on){alert(err_msg);return false;}
		if($.trim($("#sn").val())==''){alert('請填寫序號');return false;}
		if($.trim($("#chk").val())==''){alert('請填寫驗證碼');return false;}
		if(!confirm('為保障您的中獎權利，請確認輸入的為真實姓名，以避免權益受損')){return false;}
		$('#pre_submit').hide().next().show();
		$.ajax({
			type:"post",
			url:"index.php?mode=ajax&act=pre_chk",
			data:$('#pre_form').serialize(),
			dataType:"json",
			success: function(data){
				c++;
				if(data.done){
					$('#name').val(data.name);
					$('#insert_hint').html(data.msg2).parent().parent().fadeIn('fast');
					setTimeout("$('#pre_box').hide().next().fadeIn('fast');$.scrollTo('#box',1200);",2000);
					$('#done_sn').val(data.k);
					$('#done_msg').html(data.msg);
				}
				else{
					$('#chkc').val(data.a2);
					$('#chkc2').val(data.a3);
					$('#chk').val('');
					$('#auth_img').attr('src',data.a1);
					$('#insert_hint').html(data.msg).parent().parent().fadeIn('fast');
					$('#pre_submit').show().next().hide();
				}
				if(c==6){
					location.href=this_page;
				}
				if(data.re){
					$('#sn').select();
				}
			}
		});
	});

	$('#qa_submit').click(function(){
		if(!on){alert(err_msg);return false;}
		if($.trim($("#name").val())==''){alert('請填寫姓名');return false;}
		if($("#qa_form input[name=gender]:checked").size()<1){alert('請選擇性別');return false;}
		if($.trim($("#birthday_y").val())==''){alert('請選擇出生年');return false;}
		if($.trim($("#birthday_m").val())==''){alert('請選擇出生月');return false;}
		if($.trim($("#birthday_d").val())==''){alert('請選擇出生日');return false;}
		if($("#qa_form input[name=married]:checked").size()<1){alert('請選擇婚姻狀況');return false;}
		if($.trim($("#myid").val())==''){alert('請填寫身分證字號');return false;}
		else if(!checkID($("#myid").val())){alert('請填寫正確的身分證字號');return false;}
		if($.trim($("#email").val())==''){alert('請填寫電子信箱');return false;}
		else if(!$("#email").val().match(email_re)){alert('請填寫正確的電子信箱格式');return false;}
		if($.trim($("#city").val())==''){alert('請選擇縣市');return false;}
		else if($.trim($("#town").val())==''){alert('請選擇鄉鎮市區');return false;}
		if($.trim($("#addr").val())==''){alert('請填寫聯絡地址');return false;}

		if($.trim($("#tel1").val())==''&&$.trim($("#tel2").val())==''&&$.trim($("#tel3").val())==''&&$.trim($("#tel4").val())==''){tel_chk=true;}
		else{//tel
			tel_chk=false;
			if(!$("#tel1").val().match(/^[\d]{2,3}$/) || !$("#tel2").val().match(/^[\d]{3,4}$/) || !$("#tel3").val().match(/^[\d]{3,4}$/) || ($.trim($("#tel4").val())!=''&&!$("#tel4").val().match(/^[\d]{1,10}$/))){alert('請填寫正確的電話格式');return false;}
		}
		if($.trim($("#mobile1").val())==''&&$.trim($("#mobile2").val())==''&&$.trim($("#mobile3").val())==''){mobile_chk=true;}
		else{//mobile
			mobile_chk=false;
			if(!$("#mobile1").val().match(/^[\d]{4}$/) || !$("#mobile2").val().match(/^[\d]{3}$/) || !$("#mobile3").val().match(/^[\d]{3}$/)){alert('請填寫正確的手機號碼格式');return false;}
		}
		if(tel_chk&&mobile_chk){
			alert('請擇一填寫市內電話或是手機號碼');return false;
		}
		if(confirm('確定要送出了嗎？')){
			$('#qa_submit').hide().next().show();
			$.ajax({
				type:"post",
				url:"index.php?mode=ajax&act=get_info",
				data:$('#qa_form').serialize(),
				dataType:"json",
				success:function(data){
					if(data.done){
						$('#finish_msg').html(data.msg);
						$('#box').hide().next().show();
						$.scrollTo('#finish_box',1200);
					}
					else{
						$('#qa_submit').show().next().hide();
					}
				}
			});
		}
	});

//	$.scrollTo('#mid',1200);
});

function checkID(id){
  var i,sum=0,result=false;letter="ABCDEFGHJKLMNPQRSTUVXYWZIO",A1=new Array(1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3),A2=new Array(0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5),Mx=new Array(9,8,7,6,5,4,3,2,1,1);
	if(/^[A-Z]{1}(1|2)[\d]{8}$/.test(id)){
  	i=letter.indexOf(id.charAt(0));
  	sum=A1[i]+A2[i]*9;
	  for(i=1;i<10;i++){
	    sum=sum+parseInt(id.charAt(i))*Mx[i];
	  }
	  if(sum%10==0){
			result=true;
		}
	}
  return result;
}

