$(function() {
	var $indate = $("#indate");
	var $outdate = $("#outdate");
	$indate.click(function() {
		WdatePicker({ el:this, isShowClear:false, readOnly:true, minDate:'%y-%M-#{%d}', onpicked:function(dp) {
			var d1 = new Date(dp.cal.getNewDateStr().replace(/-/g, "/"));
            var d2 = new Date($outdate.val().replace(/-/g, "/"));
			if(Date.parse(d1) - Date.parse(d2) >= 0) {
				$outdate.focus().click();
			} else {
				getPrice();
			}
		}});
	});
	$outdate.click(function() {
		WdatePicker({ el:this, isShowClear:false, readOnly:true, minDate:'%y-%M-#{%d}', onpicked:function(dp) {
			var d1 = new Date($indate.val().replace(/-/g, "/"));
            var d2 = new Date(dp.cal.getNewDateStr().replace(/-/g, "/"));
			if(Date.parse(d1) - Date.parse(d2) >= 0) {
				alert("离店日期必须要比入住日期晚！");
				this.click();
			} else {
				getPrice();
			}
		}});
	});
	$(".form-room select").change(function() {
		getPrice();
	});
	$btnSubmit = $("#btn-submit");
	$btnSubmit.click(function() {
		var $perName = $("#perName"); // 客人姓名
		var $mobile = $("#mobile"); // 联系手机
		var $personNum = $("#personNum"); // 入住人数
		var $email = $("#email"); // Email
		var $inTimeMin = $("#inTimeMin"); // 房间保留时间
		var $inTimeMax = $("#inTimeMax");
		var $details = $("#details"); // 特殊要求
		
		var memberId = $("#memberId").val();
		var perName = $perName.val().replace(/ /g, '').replace(/'/g, '');
		var mobile = $mobile.val().replace(/ /g, '').replace(/'/g, '');
		var personNum = $personNum.val().replace(/ /g, '').replace(/'/g, '');
		var email = $email.val().replace(/ /g, '').replace(/'/g, '');
		var inTimeMin = $inTimeMin.val();
		var inTimeMax = $inTimeMax.val();
		var details = $details.val();
		
		if(perName.length == 0) {
			alert("请填写客人姓名！");
			$perName.focus();
		} else if(mobile.length == 0) {
			alert("请填写联系手机！");
			$mobile.focus();
		} else if(mobile.length > 0 && !/^(13|15|18)\d{9}$/.test(mobile)) {
			alert("手机号码格式错误！");
			$mobile.focus();
		} else if(personNum.length == 0) {
			alert("请填写入住人数！");
			$personNum.focus();
		} else if(personNum.length > 0 && !/^\d$/.test(personNum)) {
			alert("入住人数只能是数字！");
			$personNum.focus();
		} else if(email.length > 0 && !/.+@.+\.[a-zA-Z]{2,4}$/.test(email)) {
			alert("E-mail格式错误！");
			$email.focus();
		} else if(inTimeMin.length == 0 && inTimeMax.length > 0) {
			alert("请选择房间保留最早时间！");
			$inTimeMax.focus();
		} else if(inTimeMin.length > 0 && inTimeMax.length == 0) {
			alert("请选择房间保留最晚时间！");
			$inTimeMax.focus();
		} else {
			$btnSubmit.attr("disabled", true);
			$bookForm = $("#book-form");
			if(memberId == 0) {
				// 判断登录或注册
				$.post(root + "?m=login.checkAccount", { account:email }, function(data) {
					if(data[0] == "error") {
						var msg = data[1];
						if(data[2] == 'account is exists') msg += "请先登录！";
						alert(msg);
						$btnSubmit.attr("disabled", false);
					} else {
						$.post(root + "?m=login.register",{ account:email, password:email, name:perName, mobile:mobile, email:email }, function(data) {
							if(data[0] == "success") {
								$("#isNew").val("1");
								$bookForm.attr("action", root + "?m=order.createHotelRoomOrder").ajaxSubmit({
									success: function(data2) {
										var ary = eval("(" + data2 + ")");
										if(ary[0] == "success") {
											$.post(root + "?m=login.check", { account:mobile, password:mobile }, function(data3) {
												if(data3[0] == "success") {
													window.location.href = "info-" + ary[2] + ".html";
												} else {
													alert(data3[1]);
													$btnSubmit.attr("disabled", false);
												}
											}, "json");
										} else {
											alert(ary[1]);
											$btnSubmit.attr("disabled", false);
										}
									}
								});
							} else {
								alert("注册帐号" + mobile + "失败！");
								$btnSubmit.attr("disabled", false);
							}
						}, "json");
					}
				}, "json");
			} else {
				$bookForm.attr("action", root + "?m=order.createHotelRoomOrder").ajaxSubmit({
					success: function(data) {
						var ary = eval("(" + data + ")");

						if(ary[0] == "success") {
							window.location.href = "info-" + ary[2] + ".html";
						} else {
							alert(data[1]);
							$btnSubmit.attr("disabled", false);
						}
					}
				});
			}
		}
	});
});

function getPrice() {
	var memberId = $("#memberId").val();
	var roomId = $("#roomId").val();
	var bookNum = $("#bookNum").val();
	var indate = $("#indate").val();
	var outdate = $("#outdate").val();
	$.post(root + "?m=order.setHotelBookSession", { memberId:memberId, roomId:roomId, bookNum:bookNum, indate:indate, outdate:outdate }, function(data) {
		if(data[0] == "success") {
			$.post(root + "?m=order.getHotelBookSelect", { roomId:roomId, json:true }, function(data) {
				var $priceList = $("#priceList").empty();
				$("#totalPrice").html("￥" + data.totalPrice);
				$("#returnMoney").html(data.returnMoney + "元");
				$.each(data.priceList, function(id, item) {
					var date = new Date(item.date.replace(/-/g, "/"));
					var month = date.getMonth() + 1;
					var day = date.getDate();
					$priceList.append("<li><strong>" + ("00" + month).substr(("" + month).length) + "-" + ("00" + day).substr(("" + day).length) + "</strong><span>" + parseInt(item.price) + "</span></li>");
				});
			}, "json");
		}
	}, "json");
}