﻿

 
String.prototype.format = function() {
    var arg = arguments;
    return this.replace(/\{(\d+)\}/g, function(i, m) {
        return arg[m];
    });
}

//加载事件
$().ready(function() {
    //城市自动完成
    $("#txtcity").autocomplete(citiesData,
	{
	    caption: "使用↑↓可以选择",
	    minChars: 0, //最小字符
	    mustMatch: true, //如果设置为true,autoComplete只会允许匹配的结果出现在输入框,所有当用户输入的是非法字符时将会得不到下拉框.Default: false
	    height: 100,
	    scrollHeight: 80,
	    width: 135, //显示框宽度
	    scroll: false, //显示框是否出现滚动条
	    matchContains: true, //决定比较时是否要在字符串内部查看匹配,如ba是否与foo bar中的ba匹配.使用缓存时比较重要.不要和autofill混用.Default: false
	    max: 10, //最多显示10条
	    autoFill: false, //选择是否自动填充
	    formatItem: function(row, i, max) {//显示框显示的内容
	        return '<span class="ac_code">{0}</span><span class="ac_Name">{1}</span>'.format(row.jp, row.n);
	    },

	    formatMatch: function(row, i, max) {//匹配的内容
	        //只选择编号为4位的城市
	        if (row.c.length == 4) {
	            return "{5}{4} {0} {1} {2} {3}".format(row.jp, row.c, row.p, row.n, i, row.S);
	        } else {
	            return "";
	        }

	    },
	    formatResult: function(row) {	  //选择显示的结果
	        return "{0}".format(row.n);

	    }
	}).result(function(event, data, formatted) {//选中后执行的事件
	    $("#txtcity").attr("code", data.c);
	});
    //增加城市选择清空赋值
    $("#txtcity").bind("blur", function() {
        var cityValue = $("#txtcity").val();

        if (cityValue.length == 0) {
            $("#txtcity").attr("code", '');
        } else {

        }
        $("#txtHotelName").attr('cityvalue', cityValue);
        //清空酒店选择
        showNOName();
        $("#ErrorDiv").attr({ style: "display: none" });
    });

    //酒店失去焦点绑定数据
    $("#txtcity").bind("blur", function() {
        var txtHotelName = $("#txtHotelName").attr('hotelcode');
        $("#ErrorDiv").attr({ style: "display: none" });
    });

    //选择日期失去焦点
    $("#Stime").bind("blur", function() {
        $("#ErrorDiv").attr({ style: "display: none" });
    });

    //选择日期失去焦点
    $("#Etime").bind("blur", function() {
        $("#ErrorDiv").attr({ style: "display: none" });
    });

    //    //加载loadingJS
    InitLoadingJS();
    
    SetLStime();

});

function SetLStime() {
    if (document.getElementById('LStime') != undefined && document.getElementById('LStime') != null) {
        var timeStr = ['06:00-9:00', '07:00-10:00', '08:00-11:00', '09:00-12:00', '10:00-13:00', '11:00-14:00', '12:00-15:00', '13:00-16:00', '14:00-17:00', '15:00-18:00', '16:00-19:00', '17:00-20:00', '18:00-21:00', '19:00-22:00', '20:00-23:00', '21:00-23:59', '23:59-06:00'];
        var checkinday= document.getElementById('StimeDetail').value.split('-');
        var d = new Date();
        var hours = (d.getHours() + 2).toString();
        document.getElementById('LStime').options.length = 0;
        for (var i = 0; i < timeStr.length; i++) {
            var value = timeStr[i];
            value = value.substr(0, 4);
            if (d.getFullYear() == checkinday[0] && ReturnStr(d.getMonth()+1) == checkinday[1] && ReturnStr(d.getDate()) == checkinday[2]) {
                if (value > hours) {
                    var op = new Option(timeStr[i], timeStr[i]);
                    document.getElementById('LStime').options.add(op);
                }
            } else {
                var op = new Option(timeStr[i], timeStr[i]);
                document.getElementById('LStime').options.add(op);
            }
        }
    }

}

//换算小于10自动加0
function ReturnStr(i) {
    var Str = i;
    if (i < 10) {
        Str = '0' + i;
    }
    return Str;
}

//加载loadingjs
function InitLoadingJS() {
    var new_element = document.createElement("script");

    new_element.setAttribute("type", "text/javascript");

    new_element.setAttribute("src", "/JS/Loading.js"); // 在这里引入了a.js

    document.body.appendChild(new_element);
}

//双击隐藏酒店查询数据
function showNOName() {
    $("#txtHotelName").val('');
    $("#txtHotelName").attr('hotelcode', '');
    $("#SearchHdivText").attr({ style: 'display:none' });
}

//重置
function SetReset() {
    showNOName();
    $("#txtcity").attr("code", '');
    $("#txtcity").val('');
    $("#txtHotelName").attr("cityvalue", '');
    ResetDate();
    $("#ctl00_ContentPlaceHolder1_SearchHotelControl1_DropHotelLevel").attr("selectedIndex", 0);
    $("#ErrorDiv").attr({ style: "display: none" });
}

//重置日期
function ResetDate() {
    var sdate = $("#HiddenKey").attr('sdate');
    var edate = $("#HiddenKey").attr('edate');
    $("#Stime").val(sdate);
    $("#Etime").val(edate);
}


//提交表单
function PostHotelPage(price, shop, arear, HotelType) {
    var key = Soltrip.Hotel.HotelWeb.Index.GetKey().value;
    if (CheckPage()) {
        var url = "http://" + window.location.host + "/Index.aspx";
        var tempForm = CreateForm(url, "_self");
        //加入隐藏域
        AddInput(tempForm, "txtcity", document.getElementById('txtcity').value);
        //加入隐藏域
        AddInput(tempForm, "Stime", document.getElementById('Stime').value);
        //加入隐藏域
        AddInput(tempForm, "Etime", document.getElementById('Etime').value);
        //加入隐藏域
        AddInput(tempForm, "txtHotelName", document.getElementById('txtHotelName').value);
        //加入隐藏域
        AddInput(tempForm, "hotelcode", document.getElementById('txtHotelName').hotelcode);
        //酒店星级
        var HotelLevel = $("#ctl00_ContentPlaceHolder1_SearchHotelControl1_DropHotelLevel").val();
        //加入隐藏域
        AddInput(tempForm, "HotelLevel", HotelLevel);
        //页面提交 //加入隐藏域价格
        AddInput(tempForm, "price", price);
        //加入隐藏域商业区
        AddInput(tempForm, "shop", shop);
        //加入隐藏域行政区
        AddInput(tempForm, "arear", arear);
        //加入隐藏域酒店类型
        AddInput(tempForm, "HotelType", HotelType);
        //加入隐藏域
        AddInput(tempForm, "Key", key);
        //提交表单
        SubmitForm(tempForm);
    } else {
        return false;
    }
}



function PostPage(PageType) {
    var key = Soltrip.Hotel.HotelWeb.Index.GetKey().value;
    if (CheckPage()) {
        var url = "http://" + window.location.host + "/Index.aspx";

        var tempForm = CreateForm(url, "_self");
        //加入隐藏域
        AddInput(tempForm, "txtcity", document.getElementById('txtcity').value);
        //加入隐藏域
        AddInput(tempForm, "Stime", document.getElementById('Stime').value);
        //加入隐藏域
        AddInput(tempForm, "Etime", document.getElementById('Etime').value);
        //加入隐藏域
        AddInput(tempForm, "txtHotelName", document.getElementById('txtHotelName').value);
        //加入隐藏域
        AddInput(tempForm, "hotelcode", document.getElementById('txtHotelName').hotelcode);

        var HotelLevel = '';
        
        if (PageType == 1) {
//            //加入隐藏域
//            AddInput(tempForm, "IsSoluxe", '1');
        } else {
            HotelLevel = $("#ctl00_ContentPlaceHolder1_SearchHotelControl1_DropHotelLevel").val();
        }
        //加入隐藏域
        AddInput(tempForm, "HotelLevel", HotelLevel);
        //加入隐藏域
        AddInput(tempForm, "Key", key);
        //打开遮罩层
        openGlobalDiv();
        //closeGlobalDiv() 
        
        //提交表单
        SubmitForm(tempForm);
    } else {
        return false;
    }
}








//显示全部信息
function showAllInfo(ATarget, info) {
    if (info.length > 0) {
        //取得坐标位置
        var coordinate = GetClickLocation(ATarget);
        //得到层
        var PolicyDiv = document.getElementById("ErrorDiv");
        var score = document.getElementById("scoreInfo");
        PolicyDiv.style.left = coordinate[0] + "px";
        PolicyDiv.style.top = coordinate[1] + 22 + "px";

        score.innerHTML = info;
        PolicyDiv.style.display = "block";
      

    }
}

//页面提交验证
function CheckPage() {
    var error = '';
    var city = $('#txtcity').val();
    var hotelName = $('#txtHotelName').val();
    if (city.length == 0 ) {
        error = '请输入酒店所在城市';
        showAllInfo(document.getElementById('txtcity'), error);
        return false;
    }
    var Stime = $("#Stime").val();
    var Etime = $('#Etime').val();
    if (error.length == 0 && Stime.length == 0) {
        error = '请输入入住日期';
        //        document.getElementById('Stime').focus();
        showAllInfo(document.getElementById('Stime'), error);
        return false;
    } else if (error.length == 0 && Etime.length == 0) {
        error = '请输入离店日期';
        //        document.getElementById('Etime').focus();
        showAllInfo(document.getElementById('Etime'), error);
        return false;
    } else if (error.length == 0 && Stime == Etime) {
        error = '离店日期不能早于或等于入住日期';
        //        document.getElementById('Etime').focus();
        showAllInfo(document.getElementById('Etime'), error);
        return false;
    }
    if (error.length > 0) {

        return false;
    } else {
        return true;
    }
}

//查询酒店
function SearchHotel(type) {
    PostPage(type);
}

//根据酒店ID提交到酒店详细页面
function ShowHotelDetail(HotelCode) {
    var key = Soltrip.Hotel.HotelWeb.Index.GetKey().value;
    var url = "http://" + window.location.host + "/Booking/HotelDetail.aspx";
    var tempForm = CreateForm(url, "_blank");
    //加入隐藏域酒店类型
    AddInput(tempForm, "HotelCode", HotelCode);
    //加入隐藏域
    AddInput(tempForm, "Key", key);
    //提交表单
    SubmitForm(tempForm);
}



//跳转到某城市列表页面
function GoHotelList(city,Stime,Etime) {
    var url = "http://" + window.location.host + "/Index.aspx";
    var key = Soltrip.Hotel.HotelWeb.Index.GetKey().value;

    var tempForm = CreateForm(url, "_self");
    //加入隐藏域
    AddInput(tempForm, "txtcity", city);
    //加入隐藏域
    AddInput(tempForm, "Stime", Stime);
    //加入隐藏域
    AddInput(tempForm, "Etime", Etime);
    //加入隐藏域
    AddInput(tempForm, "txtHotelName", '');
   
    //加入隐藏域
    AddInput(tempForm, "hotelcode", '');
    var HotelLevel = '';   
    //加入隐藏域
    AddInput(tempForm, "HotelLevel", HotelLevel);
    //加入隐藏域
    AddInput(tempForm, "Key", key);
    //提交表单
    SubmitForm(tempForm);
}

//去预订页面
function GoBooking(HotelCode, roomid) {
    var key = Soltrip.Hotel.HotelWeb.Index.GetKey().value;
    //去登录页
    if (document.getElementById('member').style.display == "none" && document.readyState.toLowerCase() != "complete") {
        var thisurl = "http://" + window.location.host + "/Booking/HotelResult.aspx";
        var url = 'http://member.40061-40061.com/Member/Login.aspx';
        var tempForm = CreateForm(url, "_self");
        AddInput(tempForm, "returnUrl", thisurl);
        AddInput(tempForm, "PostStr", "");
        AddInput(tempForm, "key", key);
        //打开遮罩层
        openGlobalDiv();
        SubmitForm(tempForm);
        return false;

    } 
    
    var url = "http://" + window.location.host + "/Booking/BookingDetail.aspx";
    var tempForm = CreateForm(url, "_self");
    //加入隐藏域酒店id
    AddInput(tempForm, "HotelCode", HotelCode);
    //加入隐藏域酒店id
    AddInput(tempForm, "roomid", roomid);
    //加入隐藏域
    AddInput(tempForm, "Key", key);
    //提交表单
    SubmitForm(tempForm);
}




