//检验离店日期
function checkDateTemp() {
    var tm1=document.getElementById("O_IndateTemp");
    var tm1Value=document.getElementById("O_IndateTempValue");
    var tm2=document.getElementById("O_OutdateTemp");
    var tm2Value=document.getElementById("O_OutdateTempValue");
    if(tm1 != null && tm1Value != null && tm2 != null && tm2Value != null) {
        var v_tm1=tm1.value;
        var v_tm1Value=tm1Value.value;
        var v_tm2=tm2.value;
        var v_tm2Value=tm2Value.value;
        if(v_tm1.length>0 && v_tm2.length==0){
            WdatePicker({minDate:'%y-%M-#{%d}',el:'O_OutdateTemp'});
        }
        if(v_tm1.length>0 && v_tm2.length>0) {
            //比较日期
            var d1 = new Date(v_tm1.replace(/-/g,"/"));
            var d2 = new Date(v_tm2.replace(/-/g,"/"));
            if(Date.parse(d1) - Date.parse(d2)>=0) {
                if(v_tm1!=v_tm1Value || v_tm2!=v_tm2Value) { 
                    if(Date.parse(d1) - Date.parse(d2)>0) { alert("入住日期"+v_tm1+"晚于离店日期"+v_tm2); }
                    if(Date.parse(d1) - Date.parse(d2)==0) { alert("离店日期至少要比入住日期晚1天"); }
					if(v_tm1!=v_tm1Value) { tm1.focus();WdatePicker({minDate:'%y-%M-#{%d}',el:'O_IndateTemp'});}
					if(v_tm2!=v_tm2Value) { tm2.focus();WdatePicker({minDate:'%y-%M-#{%d}',el:'O_OutdateTemp'});}
					tm1Value.value=v_tm1;tm2Value.value=v_tm2; 
                }
            } else {
				if(v_tm1!=v_tm1Value || v_tm2!=v_tm2Value) {
					var O_MemberIdTemp = $("#O_MemberIdTemp");
					var O_HotelRoomTemp = $("#O_HotelRoomTemp");
					var O_BookNumTemp = $("#O_BookNumTemp");
					var O_IndateTemp = $("#O_IndateTemp");
					var O_OutdateTemp = $("#O_OutdateTemp");
					var memberId = O_MemberIdTemp.val();
					var roomId = O_HotelRoomTemp.val();
					var bookNum = O_BookNumTemp.val();
					var indate = O_IndateTemp.val();
					var outdate = O_OutdateTemp.val();
					getHotelBookSelectArea(memberId, roomId, bookNum, indate, outdate);
				}
			}
        }
    }
}
//酒店客房更换
function hotelRoomChange() {
	getHotelBookSelectArea();
}
//预订间数更换
function bookNumChange() {
	getHotelBookSelectArea();
}
//获取酒店预订区html
function getHotelBookSelectArea() {
	var basePath = $($("body")[0]).attr("base");
	$("#O_LoadingTemp").show();
	var O_MemberIdTemp = $("#O_MemberIdTemp");
	var O_HotelRoomTemp = $("#O_HotelRoomTemp");
	var O_BookNumTemp = $("#O_BookNumTemp");
	var O_IndateTemp = $("#O_IndateTemp");
	var O_OutdateTemp = $("#O_OutdateTemp");
	var memberId = O_MemberIdTemp.val();
	var roomId = O_HotelRoomTemp.val();
	var bookNum = O_BookNumTemp.val();
	var indate = O_IndateTemp.val();
	var outdate = O_OutdateTemp.val();
	var O_HotelBookSelectArea = $("#O_HotelBookSelectArea");
	$.post(basePath + "?m=order.setHotelBookSession", { memberId:memberId, roomId:roomId, bookNum:bookNum, indate:indate, outdate:outdate }, function(data) {
		if(data[0] == "success") {
			$.post(basePath + "?m=order.getHotelBookSelect", { roomId:roomId }, function(data) {//alert(data);
				O_HotelBookSelectArea.replaceWith(data);
			});
		}
	}, "json");
}
//获取酒店预订用户信息区html
function getHotelBookMemberInfoArea(){
	var basePath = $($("body")[0]).attr("base");
	$("#O_LoadingBookLogin").show();
	var O_PersonNum = $("#O_PersonNum");
	var O_PerName = $("#O_PerName");
	var O_Mobile = $("#O_Mobile");
	var O_Cert = $("#O_Cert");
	var O_Email = $("#O_Email");
	var O_IntimeMin = $("#O_IntimeMin");
	var O_IntimeMax = $("#O_IntimeMax");
	var O_Details = $("#O_Details");
	
	var personNum = O_PersonNum.val();
	var name = O_PerName.val();
	var mobile = O_Mobile.val();
	var cert = O_Cert.val();
	var email = O_Email.val();
	var inTimeMin = O_IntimeMin.val();
	var inTimeMax = O_IntimeMax.val();
	var details = O_Details.val();
	
	var O_HotelBookMemberInfoArea = $("#O_HotelBookMemberInfoArea");
	$.post(basePath + "?m=order.setHotelBookMemberInfoSession", { personNum:personNum, name:name, mobile:mobile, cert:cert, email:email, inTimeMin:inTimeMin, inTimeMax:inTimeMax, details:details }, function(data) {
		if(data[0] == "success") {
			$.post(basePath + "?m=order.getHotelBookMemberInfo", {}, function(data) {
				O_HotelBookMemberInfoArea.replaceWith(data);
			});
		}
	}, "json");
}
//酒店预订区登录
function bookLogin() {
	var basePath = $($("body")[0]).attr("base");
	var O_BookLoginAccout = $("#O_BookLoginAccout");//帐号
	var O_BookLoginPsd = $("#O_BookLoginPsd");//密码
	var v_account = $.trim(O_BookLoginAccout.val().replace(/ /g,'').replace(/'/g,''));
	var v_psd = $.trim(O_BookLoginPsd.val().replace(/ /g,'').replace(/'/g,''));
	if(v_account == "") {
		alert("帐号不能为空！");
		O_BookLoginAccout.val("");
		O_BookLoginAccout.focus();
	} else if(v_psd == "") {
		alert("密码不能为空！");
		O_BookLoginPsd.val("");
		O_BookLoginPsd.focus();
	} else {
		$("#O_LoadingBookLogin").show();
		$.post(basePath + "?m=login.check", { account:v_account, password:v_psd }, function(data) {//alert(data);
			if(data[0] == "success") {
				window.location.reload();
			} else {
				alert(data[1]);
				if(data[2] == "account is null") {
					O_BookLoginAccout.val("");
					O_BookLoginAccout.focus();
				} else if(data[2] == "password is null") {
					O_BookLoginPsd.val("");
					O_BookLoginPsd.focus();
				} else if(data[2] == "member is not exists") {
					O_BookLoginAccout.focus();
				} else if(data[2] == "member is stopped") {
					O_BookLoginAccout.focus();
				} else if(data[2] == "password error") {
					O_BookLoginPsd.val("");
					O_BookLoginPsd.focus();
				}
			}
		}, "json");
	}
}
//提交酒店客房预订
function book(){
	var basePath = $($("body")[0]).attr("base");
	var O_Book = $("#O_Book");
	var O_LoadingBookSubmit = $("#O_LoadingBookSubmit");
	var O_BookPersonInfoShow = $("#O_BookPersonInfoShow");
	var O_HotelRoomTemp = $("#O_HotelRoomTemp");//客房类型
	var O_IndateTemp = $("#O_IndateTemp");//入住日期
    var O_OutdateTemp = $("#O_OutdateTemp");//离店日期
	var O_BookNumTemp = $("#O_BookNumTemp");//预订间数
	var O_PersonNum = $("#O_PersonNum");//入住人数
    var O_PerName = $("#O_PerName");//入住人姓名
    var O_Cert = $("#O_Cert");//证件类型
    var O_Mobile = $("#O_Mobile");//入住人联系电话
    var O_Email = $("#O_Email");//入住人联系email
    var O_InTimeMin = $("#O_InTimeMin");//到店时间
    var O_InTimeMax = $("#O_InTimeMax");//到店时间
    var O_Details = $("#O_Details");//特殊要求
	
	//变量赋值
	var roomId = O_HotelRoomTemp.val();//房间类型
	var indate = O_IndateTemp.val();//入住日期
	var outdate = O_OutdateTemp.val();//离店日期
	var bookNum = O_BookNumTemp.val();//预订间数
	
	var personNum = O_PersonNum.val();//入住人数
	//入住人姓名
	var perName = O_PerName.val().replace(/ /g,'').replace(/'/g,'');
	O_PerName.val(perName);
	if(perName==""){alert("请填写入住人姓名!");O_PerName.focus();return false;}
	
	var cert=O_Cert.find("option:selected").val();//证件类型
	//手机号码
	var mobile = O_Mobile.val().replace(/ /g,'').replace(/'/g,'');
	if(mobile==""){alert("请填写入住人联系手机号码!");O_Mobile.focus();return false;}
	if(mobile != "" && !/^(13|15|18)\d{9}$/.test(mobile)){
		alert("请输入正确的手机号码!");O_Mobile.focus();return false;
	}
	//检验邮箱
	var email = O_Email.val().replace(/ /g,'').replace(/'/g,'');
	if(email != "" && !/.+@.+\.[a-zA-Z]{2,4}$/.test(email)){
		alert("请输入正确的Email地址!");O_Email.focus();return false;
	}
	var inTimeMin = O_InTimeMin.val();//到店时间
	var inTimeMax = O_InTimeMax.val();//离店时间
	var details = O_Details.val().replace(/ /g,'').replace(/'/g,'');//特殊要求
	O_LoadingBookSubmit.show();//显示Loading
	O_Book.attr("disabled", true);
	if(O_BookPersonInfoShow.val() == "1") {
		//判断登录或注册
		$.post(basePath + "?m=login.checkAccount", { account:mobile }, function(data) {
			if(data[0] == "error") {
				var msg = data[1];
				if(data[2] == 'account is exists') { msg += "请先登录！"; };
				alert(msg);
				$("#O_BookLoginAccout").val(mobile);
				$("#O_BookLoginPsd").focus();
				O_Book.attr("disabled", false);
			} else {
				$.post(basePath + "?m=login.register",{ account:mobile, password:mobile, name:perName, mobile:mobile, email:email }, function(data) {
					O_Book.attr("disabled", false);
					if(data[0] == "success") {
						//下单
						$.post(basePath + "?m=order.createHotelRoomOrder", { roomId:roomId, indate:indate, outdate:outdate, bookNum:bookNum, personNum:personNum, perName:perName, cert:cert, mobile:mobile, email:email, inTimeMin:inTimeMin, inTimeMax:inTimeMax, details:details }, function(data) {//alert(data);
							if(data[0] == "success") {
								$.post(basePath + "?m=login.check", { account:mobile, password:mobile }, function(data2) {
									if(data2[0] == "success") {
										var goUrl = $("#O_BookSuccessUrl").attr("action").replace('@orderId', data[2]);
										$("#O_BookSuccessUrl").attr("action", goUrl);
										$("#isNew").val("1");
										$("#O_BookSuccessUrl").submit();
									} else {
										window.location.reload();
									}
								}, "json");
							} else {
								alert(data[1]);
							}
						}, "json");
					} else {
						alert("注册帐号" + mobile + "失败！");
					}
				}, "json");
			}
		}, "json");
	} else {		
		//直接下单
		$.post(basePath + "?m=order.createHotelRoomOrder", { roomId:roomId, indate:indate, outdate:outdate, bookNum:bookNum, personNum:personNum, perName:perName, cert:cert, mobile:mobile, email:email, inTimeMin:inTimeMin, inTimeMax:inTimeMax, details:details }, function(data) {//alert(data);
			O_Book.attr("disabled", false);
			alert(data[1]);
			if(data[0] == "success") {
				var goUrl = $("#O_BookSuccessUrl").attr("action").replace('@orderId', data[2]);
				$("#O_BookSuccessUrl").attr("action", goUrl);
				$("#isNew").val("0");
				$("#O_BookSuccessUrl").submit();
			}
		}, "json");
	}
}
//注册用户
function bookReg() {
	//启动浏览模式
	var range = getRange();
	var obj_doing = document.getElementById("doing");
	if(range != null && obj_doing != null) {
		obj_doing.style.width = range.width + "px";
		obj_doing.style.height = (range.height + 100) + "px";
		obj_doing.style.display = "block";
	}
	var divReg = document.getElementById("divReg");
	if(divReg != null) { divReg.style.display = ""; }
	var divRegF = document.getElementById("divRegF");
	if(divRegF != null) { divRegF.style.display = ""; }
}
setInterval('checkDateTemp()', 1000);