$(function(){
	$("#name, #textfield6").focus();
	$("#button").click(function(){
		sendMail();
	});

	$("#button5").click(function(){
		sendConfirmMail();
	});
	$jquid = "jq_shopping_items";
	if($.cookie("jq_shopping_items")!=null) $(".gcmsg").hide();
	else{
		$("#button5, #button6").attr("disabled", "disabled");
	}
	var _d = new Date();
	$("select[name*=selY]").val(_d.getYear());
	$("select[name*=selM]").val(_d.getMonth()+1);
	$("select[name*=selD]").val(_d.getDate()+2);
	
});

function sendMail(){
	if(!checkMailField()) return;

	var param = "subj=" + encodeURIComponent($("#subject").val()) + "&name=" + encodeURIComponent($("#name").val()) + "&mail=" + encodeURIComponent($("#email").val()) + "&msg=" + encodeURIComponent($("#content").val()) + "&action=sendmail";

	$.ajax({
		url: "admin/action.php",
		data: param,
		type: "POST",
		dataType: "text",
		success: function(msg){
			if(msg==0){
				alert("很感謝您的來信指導，我們會盡快處理回覆!!");
				location.href = "index.php";
			}else{
				alert("目前寄信系統發生錯誤，我們會盡快修復!!");
			}
		},
		error: function(err){
			resetLogin(err);
		}
	});
}

function checkMailField(){
	if($("#name").val() == ""){
		alert("姓名不可為空白!!");
		$("#name").focus();
		return false;
	}
	if($("#subject").val() == ""){
		alert("主題不可為空白!!");
		$("#subject").focus();
		return false;
	}	
	if($("#email").val() == ""){
		alert("信箱不可為空白!!");
		$("#email").focus();
		return false;
	}
	if($("#content").val() == ""){
		alert("內容不可為空白!!");
		$("#content").focus();
		return false;
	}
	return true;
}

function sendConfirmMail(){
	if(!confirmList()) return;
	var gd = $("select[name*=selY]").val()+"/"+$("select[name*=selM]").val()+"/"+$("select[name*=selD]").val();
	var param = "n=" + encodeURIComponent($("#textfield6").val()) + "&s=" + $("input[name=sex]:radio:checked").val() + "&m=" + encodeURIComponent($("#textfield").val()) + 
				"&p=" + encodeURIComponent($("#textfield7").val()) + "&t=" + encodeURIComponent($("#textfield2").val()) + "&z=" + encodeURIComponent($("#textfield4").val()) + 
				"&a=" + encodeURIComponent($("#textfield5").val()) + "&gt=" + $("input[name=gtime]:radio:checked").val() + "&gc=" + $("input[name=gcheck]:radio:checked").val() + 
				"&gct=" + $("input[name=gctime]:radio:checked").val() + "&gd=" + encodeURIComponent(gd) + "&mg=" + $("#textarea").val() + "&action=confirm";

	$.ajax({
		url: "admin/confirm.php",
		data: param,
		type: "POST",
		dataType: "text",
		success: function(msg){
			if(!isNaN(msg)&&msg>0){
				$.cookie($jquid, null);
				alert("很感謝您支持萬味軒的產品，我們會盡快處理派送!!");
				location.href = "index.php";
			}else{
				alert("目前線上訂購系統發生錯誤，我們會盡快修復!!");
			}
		},
		error: function(err){
			resetLogin(err);
		}
	});
}

function confirmList(){
	//$("input[name=sex]:radio:checked").val();
	if($("#textfield6").val() == ""){
		alert("姓名不可為空白!!");
		$("#textfield6").focus();
		return false;
	}
	if($("#textfield7").val() == "" && $("#textfield2").val() == ""){
		alert("請留下手機或家用電話以便聯絡!!");
		$("#textfield7").focus();
		return false;
	}
	if($("#textfield5").val() == ""){
		alert("送貨地址不可為空白!!");
		$("#textfield5").focus();
		return false;
	}
	if($("input[name=gtime]:radio:checked").size()<=0){
		alert("請選擇收貨時間!!");
		$("input[name=gtime]:radio:eq(0)").focus();
		return false;
	}
	if($("input[name=gcheck]:radio:checked").size()<=0){
		alert("請選擇訂單確認方式!!");
		$("input[name=gcheck]:radio:eq(0)").focus();
		return false;
	}
	if($("input[name=gcheck]:radio:checked").val()=="2"){
		if($("#textfield").val() == ""){
			alert("由於您勾選出貨時以 E-mail 方式確認，因此E-mail欄位不可為空白!!");
			$("#textfield").focus();
			return false;
		}
	}
	if($("input[name=gctime]:radio:checked").size()<=0){
		alert("請選擇方便聯絡時間!!");
		$("input[name=gctime]:radio:eq(0)").focus();
		return false;
	}

	return true;
}
