﻿function getJson() {
    var CheckInDate = FormatDate(document.lianbo.sdate.value);
    var CheckOutDate = FormatDate(document.lianbo.edate.value);
    JsonUrl = "http://" + window.location.hostname + "/stanley/api/elong_api.asp?hotelid=" + hotelid + "&CheckInDate=" + CheckInDate + "&CheckOutDate=" + CheckOutDate + "&language=" + language;
    $.ajax({
        async: true,
        cache: true,
        type: "get",
        timeout: "20000",
        dataType: "json",
        url: JsonUrl,
        beforeSend: function() {
            ErrHtml = "<div align=center><p>&nbsp;</p><p><img src=\"http://www.hotelmr.com/images/loading.gif\"></p><p>正在讀取"+CheckInDate+"到"+CheckOutDate+"實時數據，請稍候…………</p><p>&nbsp;</p></div>";
			$("#FillPrice").html(ErrHtml);
        },
        success: function(json) {
            FormartJson(json, CheckInDate, CheckOutDate);
        },
        error: function(a, b, c) {
            ErrHtml = "<div align=center><p>&nbsp;</p><p>很抱歉！獲取數據過程網絡超時，請點擊 <a href='javascript:getJson();'>這裡</a> 重新載入。</p><p>&nbsp;</p></div>";
            $("#FillPrice").html(ErrHtml);
        }
    });
}
function FormartJson(json, CheckInDate, CheckOutDate) {
	var Date_Diff_InDate = new Date(Date.parse(CheckInDate.replace(/-/g,"/"))); 
    var Date_Diff_OutDate = new Date(Date.parse(CheckOutDate.replace(/-/g,"/"))); 
	var Date_Diff = (Date_Diff_OutDate - Date_Diff_InDate)/(1000 * 60 * 60 * 24);
    if (json.Result != "true") {
        ErrHtml = "<div align=\"center\"><br /><br />此酒店暫無價格，請預訂其他酒店，謝謝！<br /><br /><br /><br /></div>";
        $("#FillPrice").html(ErrHtml);
        return false;
    }
    var TT, successHtml;
    var successHtml = "<form id=\"PriceForm\" method=\"post\" action=\"https://" + window.location.hostname + "/asp/bookhotel"+language+".asp\"><table class=\"fxtab1\">";
    successHtml = successHtml + "<tbody><tr><th width=\"175\" class=\"thindent\">房  型</th><th width=\"150\" class=\"thindent-Explain\">價格說明</th><th width=\"80\">門市價</th>";
    successHtml = successHtml + "<th width=\"160\">網上價（均價）</th>";
    successHtml = successHtml + "<th width=\"50\">早餐</th><th width=\"60\">&nbsp;</th></tr>";
    for (i = 0; i < parseInt(json.RoomCount); i++) {
        m = parseInt(json.Room[i].RateCount);
        for (t = 0; t < m; t++) {
            successHtml = successHtml + "<tr>";
            if (t == 0) {
                successHtml = successHtml + "<td><div class=\"booktitle1 openWin\"><a href=\"javascript:ViewRoomIntroduction('" + json.Room[i].RoomId + "')\">" + json.Room[i].RoomName + "<img src=\"../../images/ic_d.gif\" width=\"13\" height=\"10\" align=\"absmiddle\"></a>";
                successHtml = successHtml + "</div></td>";
            } else {
                successHtml = successHtml + "<td>&nbsp;</td>";
            }
            successHtml = successHtml + "<td><div class=\"booktitle openWin\">";
            if (json.Room[i].Rate[t].Rules != "") {
                successHtml = successHtml + "<span onMouseOver=\"ViewDiv('OtherDiv" + json.Room[i].RoomId + "_" + json.Room[i].Rate[t].RateId + "',this);\" onMouseOut=\"CloseDiv('OtherDiv" + json.Room[i].RoomId + "_" + json.Room[i].Rate[t].RateId + "',this);\"><a href=\"javascript:void(0)\">" + json.Room[i].Rate[t].RatePlanIntro + "</a></span>";
                successHtml = successHtml + "<div id=\"OtherDiv" + json.Room[i].RoomId + "_" + json.Room[i].Rate[t].RateId + "\" class=\"ViewNo\" style=\"top:20px; left:-3000px; display:block;\">" + json.Room[i].Rate[t].Rules + "</div>";
            } else {
                successHtml = successHtml + json.Room[i].Rate[t].RatePlanIntro;
            }
            successHtml = successHtml + "</div></td>";
            successHtml = successHtml + "<td>" + json.Room[i].Rate[t].PriceType + "&nbsp;<s>" + json.Room[i].Rate[t].RetailRate + "</s></td>";
            if (parseInt(Date_Diff) > 1) {
                successHtml = successHtml + "<td><span class=\"AvgPrice\"><a href=\"javascript:void(0)\" onMouseOver=\"ViewDiv('PriceDiv" + json.Room[i].RoomId + "_" + json.Room[i].Rate[t].RateId + "',this);\" onMouseOut=\"CloseDiv('PriceDiv" + json.Room[i].RoomId + "_" + json.Room[i].Rate[t].RateId + "',this);\">" + json.Room[i].Rate[t].AvgPrice + "</a></span>";
                successHtml = successHtml + json.Room[i].Rate[t].RoomPriceDivHtml + "</td>";
            } else {
                successHtml = successHtml + "<td>" + json.Room[i].Rate[t].PriceType + "&nbsp;" + json.Room[i].Rate[t].FirstDayPrice + "</td>";
            }
            successHtml = successHtml + "<td>" + json.Room[i].Rate[t].BreakFast + "</td>";
            successHtml = successHtml + "<td><div align=\"center\">";
            if (json.Room[i].Rate[t].BookState == "true") {
                successHtml = successHtml + "<input type=\"submit\" class=\"YuDing\" value=\"預訂\" title=\"有房\"  onclick=\"document.getElementById('PriceForm').ServiceCode.value='" + json.Room[i].RoomId + "';document.getElementById('PriceForm').ServiceName.value='" + json.Room[i].RoomName + "';document.getElementById('PriceForm').RateId.value='" + json.Room[i].Rate[t].RateId + "';\" onmouseover=\"this.style.backgroundPosition='right top'\" onmouseout=\"this.style.backgroundPosition='left top'\" />";
            } else {
				if (json.Room[i].Rate[t].AllMemberPrice.indexOf("無價") == -1) {
                    successHtml = successHtml + "<span>滿房</span>";
				} else {
                    successHtml = successHtml + "<span>無價</span>";
				}
            }
            successHtml = successHtml + "</div></td></tr>";
        }
		successHtml = successHtml + "<tr class=\"Hidden-Room-Introduction\" id=\"Room-Introduction-" + json.Room[i].RoomId + "\"><td colspan=\"7\"><div class=\"Room-Introduction\">" + json.Room[i].RoomIntro + "<p id=\"YinCang\"><a href=\"javascript:void(0);\" onClick=\"ViewRoomIntroduction('" + json.Room[i].RoomId + "')\"> 隱藏 </a></p></div></td></tr>";
    }
    successHtml = successHtml + "</tbody></table><input type=\"hidden\" name=\"hotelid\" value=\"" + hotelid + "\" /><input type=\"hidden\" name=\"hotelname\" value=\"" + hotelName + "\" /><input type=\"hidden\" name=\"hotelcode\" value=\"" + hotelcode + "\" /><input type=\"hidden\" name=\"sdate_book\" value=\"" + CheckInDate + "\" /><input type=\"hidden\" name=\"edate_book\" value=\"" + CheckOutDate + "\" /><input type=\"hidden\" name=\"ServiceCode\" /><input type=\"hidden\" name=\"ServiceName\" /><input type=\"hidden\" name=\"RateId\" /></form>";
    $("#FillPrice").html(successHtml);
    successHtml = null;
}
