﻿var frmAction = "";
var c_def = ['-请选择-', ''];
var C_MAXDAYTIP = '如果您需要在酒店入住30天以上，请和柚子旅行网预订中心联系：400-666-8811';
var C_MINDAYTIP = '入住日期必须小于离店日期。';

var c_Province = null;
var c_city = null;

var showMode = ['index', 'list'];
var c_list = ['province', 'city', 'ChkIn', 'ChkOut', 'hotelstar', 'priceScope', 'hotelname', 'AreaValue', 'AreaType', 'OrderBy', 'PageIndex', 'PageCount', 'Acr'];
var f = "#qs_";
var frm = null;
// 订单更新频率 默认:5分钟
var HOTELORDERTIMEOUT = 300000;

$(document).ready(function() {
	frm = $("#frmSQ").get(0);
	c_Province = $("#province");
	c_city = $("#city");
	LoadValue();
	$("#btnQSearch").click(function() { 
		var chkIn, chkOut, HotelName;
		chkIn = $("#qChkIn").val();
		chkOut = $("#qChkOut").val();
		$(f + c_list[6]).val($("#qHotelName").val());
		$(f + c_list[12]).val($("#qAcr").val());
		$(f + c_list[4]).val(0);
		$(f + c_list[7]).val(-1);
		$(f + c_list[8]).val(-1);
		$(f + c_list[5]).val("0-99999");
		
		SetCookie('ChkIn', chkIn);
		SetCookie('ChkOut', chkOut);
		//$.cookie('ChkIn', chkIn, {expires: 7, path: '/', domain: 'yoostrip.com', secure: true});
		//$.cookie('ChkOut', chkOut, {expires: 7, path: '/', domain: 'yoostrip.com', secure: true});
	
		var d = DiffDate(chkOut, chkIn);
		if (d < 0) {
			alert(C_MINDAYTIP);
			return false;
		} else if (d >= 20) {
			alert(C_MAXDAYTIP);
			return false;
		}
		
		$(f + c_list[11]).val(0);
		$(f + c_list[2]).val(chkIn);
		$(f + c_list[3]).val(chkOut);

		Submit(frm);
									
	});
});

$.fn.MorePrice = function() {
	var chkIn = new Date(ChkIn.replace(/-/g, "/"));
	var chkOut = new Date(ChkOut.replace(/-/g, "/"));
	var days = DiffDate(ChkOut, ChkIn);
	var tableHtml = '<table><thead>{head}</thead><tbody>{body}</tbody></table>';
	var dvar = {d:["周日","周一","周二","周三","周四","周五","周六"], w:['第一周', '第二周', '第三周', '第四周']};
	var week = chkIn.getDay();
	var tpl = new Object(); tpl.head = '';
	for(var i = 0; i <= 6; i++){
		tpl.head = tpl.head + "<th>" + dvar.d[(week + i) % 7] + "</th>";
	}
	var weekCount = Math.floor(days / 7) <= 0 ? 1 : (days % 7) == 0 ? Math.floor(days / 7) : Math.floor(days / 7) + 1;
	tpl.body = '<tr>';
	for (var i = 1; i <= days; i++) {
		tpl.body = tpl.body + "<td>" + (DateAdd(i - 1, ChkIn).Format('dd日')) + "<br />{p" + i + "}</td>";
		if ((i % 7) == 0 && (i + 1) <= days) {
			var aa = Math.floor(i / 7);
			tpl.body = tpl.body + "</tr><tr>";
		}
	}
	for (; i <= (weekCount * 7); i++) {
		tpl.body = tpl.body + "<td></td>";
	}
	tpl.body = tpl.body + '</tr>'
	tableHtml = tableHtml.oReplace(tpl);

	this.mouseover(function(e) {
		if(!this.rel && !this.MPset) return;
		if(!this.MPDiv) {
			var div = document.createElement("div");
			this.MPDiv = div;
			// 解析价格数据
			var p = this.rel.split(',');
			var divtpl = new Object();;
			for (var i = 1; i < p.length; i++) {
				eval('divtpl.p' + i + ' = "' + p[i - 1] + '"');
			}
			$(div).addClass('morePriceDiv').html(tableHtml.oReplace(divtpl));
			$("body").append(div);
			this.MPset = true;
		}
		$(this.MPDiv).show();
		var mouseX = e.pageX || (e.clientX ? e.clientX + document.body.scrollLeft : 0);
		var mouseY = e.pageY || (e.clientY ? e.clientY + document.body.scrollTop : 0);
		mouseX -= $(this.MPDiv).width() + 10;
		mouseY -= 10;
		$(this.MPDiv).css({left: mouseX + "px", top: mouseY + 3 + "px"});
	}).mouseout(
		function() {
			if (this.MPDiv) {
				$(this.MPDiv).hide();
			}
		}
	);
};
var MorePriceTpl = ['<table><thead>{head}</thead><tbody>{body}</tbody></table>', '<th>{$d}</th>', '<td>{$p}</td>'];
var MorePriceTable = 'sdfsdfsdfsdf';
function ShowMorePrice(t, p) {
	if (MorePriceTable.length == 0) { GenMorePriceF(); }
	var genHtml = MorePriceTable;
	var o = $('#morePrice');
	o.html(genHtml);
	// 位置
	var offset = $(t).offset();
	var l = offset.left + $(t).width();
	var t = offset.top + $(t).height();
	o.css(
	{
		position:'absolute',
		left: l + "px", 
		top: t + "px"
	})
}
function GenMorePriceF() {
	var chkIn = new Date($.cookie('ChkIn').replace(/-/g, "/"));
	var chkOut = new Date($.cookie('ChkOut').replace(/-/g, "/"));
	var days = DiffDate($.cookie('ChkOut'), $.cookie('ChkIn'));
	
}

function ChangeStar(val) {
	$(f + c_list[4]).val(val);
	$(f + c_list[11]).val(0);
	$(f + c_list[5]).val("0-99999");
	$(f + c_list[6]).val("");
	$(f + c_list[12]).val("");
	Submit(frm);
}

function ChangeOrder(val) {
	$(f + c_list[9]).val(val);
	$(f + c_list[11]).val(0);
	Submit(frm);
}

function ChangeArea(t, v) {
	$(f + c_list[8]).val(t);
	$(f + c_list[7]).val(v);
	$(f + c_list[11]).val(0);
	$(f + c_list[12]).val("");
	Submit(frm);
}

function ChangePrice(val) {
	$(f + c_list[5]).val(val);
	$(f + c_list[4]).val(0);
	$(f + c_list[11]).val(0);
	$(f + c_list[6]).val("");
	$(f + c_list[12]).val("");
	Submit(frm);
}

function LoadValue() {
	for(var i = 0; i < 7; i++) {
		$("#" + c_list[i]).val($(f + c_list[i]).val());
	}
}

function ChangeHotelType(t) {
	if (t == 1)
		frmAction = "/hotel/hotel-list-list.asp";
	else
		frmAction = "/hotel/hotel-list.asp";
		
	Submit(frm);
}

/* 搜索酒店 */
function SearchHotel(url) {
	var frm = $("#frmHS");
	frmAction = url || "/hotel/hotel-list.asp";
	var chkIn, chkOut, city;
	chkIn = $("#ChkIn").val();
	chkOut = $("#ChkOut").val();
	city = parseInt($("#city").val());
	
	if (city < 1) {
		alert("请选择要查询的城市");
		//$("#city").focus();
		return false;
	}

	//, {expires: 7, path: '/', domain: 'yoostrip.com', secure: true}
	$.cookie('ChkIn', chkIn);
	$.cookie('ChkOut', chkOut);
	$.cookie('city', city);

	var d = DiffDate(chkOut, chkIn);
	if (d < 0) {
		alert(C_MINDAYTIP);
		return false;
	} else if (d >= 20) {
		alert(C_MAXDAYTIP);
		return false;
	}

	Submit(frm.get(0));
}

function UpdateProvince(d) {
	var ids = [];
	for(var i in search_data.p) ids.push(i);
	
	UpdateSelect(c_Province.get(0), ids, search_data.p, c_def[0]);
	if (d != undefined) c_Province.val(d);
	UpdateCity(1);
}

function UpdateCity(d) {
	var pid = c_Province.val();
	UpdateSelect(c_city.get(0), search_data.c[pid], search_data.c_n, c_def[0]);
	if (d != undefined) {
		c_city.val(d);
	}
}

function UpdateLocOrArea() {
	var cid = c_city.val();
	var areaType = $("#rdoAreaTypeLoc");
	var d = $("#ddlLA");
	if (areaType[0].checked == true)
		UpdateSelect(d.get(0), search_data.l[cid], search_data.l_n, c_def[0]);
	else
		UpdateSelect(d.get(0), search_data.z[cid], search_data.z_n, c_def[0]);
}

function ChangePage(sender, e) {
	var frm = $("#frmSQ");
	$("#sq_PageIndex").val(e);
	frm.action = window.location.href;
	frm.get(0).submit();
}

/*
 * submit data by post
 */
function Submit(frm) {
	frm.action = frmAction.length > 0 ? frmAction : frm.action;
	frm.submit();
}

function BookHotel(id) {
	window.location.href = '/hotel/bookroom_' + id + '.html';
}

function ShowHotelInfo(id) {
	window.location.href = '/' + id + '.html';
}

/*
 * 更新Select控件
 */
function UpdateSelect(ddl, ids, datas, def) {
	var a = ddl.options;
	a.length = 0;
	a.add(new Option(def, -1));
	if (!ids) return;
	
	for(var i = 0; i < ids.length; i++) {
		a.add(new Option(datas[ids[i]], ids[i]));
	}
}


function OnlyNumber(c) {
	c.keyup(function() {
		$(this).val($(this).val().replace(/[^\d]/g,''));
	});
}

function DiffDate(d1, d2) {
	d1 = d1.replace(/-/g, "/");
	d2 = d2.replace(/-/g, "/");
	d1 = new Date(d1);
	d2 = new Date(d2);
	
	return parseInt((d1.getTime() - d2.getTime()) / (1000 * 60 * 60 * 24));
}

function DateAdd(number,date){
	date = new Date(date.replace(/-/g, "/"));
	date = new Date(date.setDate(date.getDate() + number));
	return date;
}
Date.prototype.Format = function(formatStr) {
	var str = formatStr;
	str = str.replace(/yyyy/, this.getFullYear());
	str = str.replace(/MM/, (this.getMonth() + 1).toString());
	str = str.replace(/dd/, this.getDate().toString());
	return str;
}

function WaitOrderState(oid, info) {
	function run() {
		$.getJSON('/hotel/HotelOrderAPI.asp?action=hos&oid=' + oid + '&r=' + Math.random(), 'json', function(d) {
			if (d.r) {
				$('#uID').html('可入住');
				$('#ShowInfo').html(info).css('color', '#00f');
				if (runTimeer) window.clearInterval(runTimeer);
			}
		});
	}
	var runTimeer = window.setInterval(run, HOTELORDERTIMEOUT);
}

function HotelRoom(hid, eid, cin, cout, cid, hn, cc) {
	var hasmem = true;
	var _ = this;
	var days = DiffDate(cout, cin), showDays = 3;
	showDays = days > showDays ? showDays : days;
	var h = '';
	for (i = 0; i < showDays; i++) h += '<th>' + (DateAdd(i, cin).Format('MM月dd日')) + '</th>';
	if (days > showDays) h += '<th />';
	var tpl = {
		TableFrame: '<table id="roomList"><thead><th style="width: 150px;">房型</th><th>详情</th><th>门市价</th><th>早餐</th>' + h + (hasmem ? '<th><B>返现金[<a href="/Public/dpjj.asp" target="_blank" style="color:#0033FF">？</a>]</B></th>' : '') + '<th>前台现付</th></thead><tbody>{0}</tbody></table>',
		RoomFrame: '<tr><td>{HotelLink}</td><td><a style="color: rgb(255, 136, 34);" title="{D}" class="tipDesc" href="javascript:void(0)">详情</a></td><td style="width: 70px;"><span class="doorprice">{DP}</span></td><td style="width: 70px;">{B}</td>{Price}' + (hasmem ? '<td{DPTip}><b class="dpjj">{JJ}</b></td>' : '') + '<td>{YD}</td></tr>',
		NotData: '<p style=""height:50px; line-height:60px; text-align:center;"">此酒店的价格不对外公开，请致电400-666-8811查询，我们会竭诚为您服务。<p>',
		DPTip: ' class="tipDesc" title="点击右侧“预订”提交订单，入住后发表点评可获{JJ}元<br>奖金，奖金满200元即可提现！电话预订无点评奖。"',
		PData: '<td>{0}</td>',
		FillRoom: '<font color=red>X</font>',
		BYD: '<a class="yd" href="/hotel/room_{ID}' + (eid.length > 0 ? '_' + hid : '') + '/"' + (hasmem ? ' title="预订该房间可获{JJ}元点评奖金"': '') + '>预订</a>',
		BNLYD: '客满',
		HotelLink: '<a style="color:#04e;" href="/hotel/room_{ID}' + (eid.length > 0 ? '_' + hid : '') + '/">{N}</a>',
		HotelNotLink: '{N}',
		MorePrice: '<td><a href="javascript:void(0);" class="morePrice" rel="{0}" title="所有日期的价格">更多</a></td>'
	};
	
	function Process(d) {
		var roomhtml = '';
		for (var r in d) {
			var hasYD = true, j = 0;
			var phtml = '', morePrice = '';
			for (var i = 0; i < d[r].P.length; i++) {
				var tmpP = parseFloat(d[r].P[i]);
				if (tmpP == -1) {
					hasYD = false;
					tmpP = tpl.FillRoom;
				}
				morePrice += tmpP + ',';
				if (i < showDays) {
					phtml += tpl.PData.xReplace([tmpP]);
					j++;
				}
			}
			if (days > j) phtml += tpl.MorePrice.xReplace([morePrice]);
			if (hasYD) {
				d[r].YD = tpl.BYD.oReplace(d[r]);
				d[r].DPTip = tpl.DPTip.oReplace(d[r]);
				d[r].HotelLink = tpl.HotelLink.oReplace(d[r]);
			} else {
				d[r].YD = tpl.BNLYD;
				d[r].JJ = "0";
				d[r].DPTip = "";
				d[r].HotelLink = tpl.HotelNotLink.oReplace(d[r]);
			}
			d[r].Price = phtml;
			roomhtml += tpl.RoomFrame.oReplace(d[r]);
		}
		cc.html(tpl.TableFrame.xReplace([roomhtml]));
		$('.morePrice').MorePrice();
		$(".tipDesc").ToolTip("#FFFFDF", "#000");
	}
	$.getJSON('/hotel/HotelOrderAPI.asp?action=gr&hid=' + hid + '&eid=' + eid + '&cin=' + cin + '&cout=' + cout + '&cid=' + cid + '&hn=' + hn + '&r=' + Math.random(), 'json', function(d) {
		if (d == null || !d.HotelPriceList || !d.HotelPriceList.result || d.HotelPriceList.roomlist[0])
			cc.html(tpl.NotData);
		else
			window.setTimeout(function() { Process(d.HotelPriceList.roomlist); }, 10); // IE
	});
}