﻿var frmAction = "";
var c_def = ['-请选择-', ''];
var C_MAXDAYTIP = '如果您需要在酒店入住30天以上，请和柚子旅行网预订中心联系：4006-076-176';
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 = '<th />';
	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><td>' + dvar.w[0] + '</td>';
	for (var i = 1; i <= days; i++) {
		tpl.body = tpl.body + "<td>{p" + i + "}</td>";
		if ((i % 7) == 0 && (i + 1) <= days) {
			var aa = Math.floor(i / 7);
			tpl.body = tpl.body + "</tr><tr><td>" + dvar.w[aa] + "</td>";
		}
	}
	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 = '/hotel/hotel_' + 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 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);
}
