﻿function BranchClass(id, name,address)
{
    this.id = id;
    this.name = name;
    this.address = address;
}
function RoomClass(id,branchid,type,retailPrice,membersPrice,emptyAmount)
{
    this.id = id;
    this.branchid = branchid;
    this.type = type;
    this.retailPrice = retailPrice;
    this.membersPrice = membersPrice;
    this.emptyAmount = emptyAmount;//剩余房间数，总房间数减去已经预订的房间数。
}
function RoomStateClass(branchid,roomid,startDate,endDate,amount)
{
    this.branchid = branchid;
    this.roomid = roomid;
    this.startDate = startDate;
    this.endDate = endDate;
    this.amount = amount;
}
function GetEmptyRoomAmount(room,startDate,endDate)
{
    var total = 0;
    if (room)
    {
        var yuyueDate1 = new Date(startDate.replace("-","/"));
        var yuyueDate2 = new Date(endDate.replace("-","/"));
        var total = room.emptyAmount;
        for (var i=0;i<arrRoomState.length;i++)
        {
            if (arrRoomState[i].roomid == room.id)
            {
                var d1 = new Date(arrRoomState[i].startDate.replace("-","/"));
                var d2 = new Date(arrRoomState[i].endDate.replace("-","/"));
                //(yuyueDate1>=d1 && yuyueDate1<d2) 在指定时间段内入住
                //(yuyueDate1<d1 && yuyueDate2>d1) 在指定时间段之前入住，在时间段内离开                
                if ( (yuyueDate1>=d1 && yuyueDate1<d2) || (yuyueDate1<d1 && yuyueDate2>d1) )
                {
                    total = total - arrRoomState[i].amount;
                }
            }
        }
    }
    return total;
}
//roomstate start
var arrRoomState = new Array();
arrRoomState[0] = new RoomStateClass(7, 13, '2010-08-30', '2010-08-31', 18);
arrRoomState[1] = new RoomStateClass(7, 12, '2009-09-29', '2009-09-30', 30);
arrRoomState[2] = new RoomStateClass(5, 8, '2009-09-18', '2009-10-31', 2);
arrRoomState[3] = new RoomStateClass(7, 0, '2009-09-29', '2009-09-30', 0);
arrRoomState[4] = new RoomStateClass(7, 0, '2009-09-29', '2009-09-30', 0);
arrRoomState[5] = new RoomStateClass(5, 24, '2009-12-31', '2010-01-01', 10);
arrRoomState[6] = new RoomStateClass(5, 24, '2010-03-14', '2010-03-20', 14);
arrRoomState[7] = new RoomStateClass(5, 8, '2010-03-14', '2010-03-20', 30);
arrRoomState[8] = new RoomStateClass(5, 8, '2010-03-20', '2010-03-27', 21);
arrRoomState[9] = new RoomStateClass(5, 24, '2010-03-20', '2010-03-27', 14);
arrRoomState[10] = new RoomStateClass(5, 8, '2010-04-27', '2010-05-02', 31);
arrRoomState[11] = new RoomStateClass(5, 24, '2010-04-27', '2010-05-02', 14);
arrRoomState[12] = new RoomStateClass(6, 9, '2010-05-01', '2010-05-03', 100);
arrRoomState[13] = new RoomStateClass(6, 10, '2010-05-01', '2010-05-03', 100);
arrRoomState[14] = new RoomStateClass(6, 27, '2010-05-01', '2010-05-03', 100);
arrRoomState[15] = new RoomStateClass(9, 20, '2010-05-01', '2010-05-03', 100);
arrRoomState[16] = new RoomStateClass(9, 21, '2010-05-01', '2010-05-03', 100);
arrRoomState[17] = new RoomStateClass(9, 22, '2010-05-01', '2010-05-03', 100);
arrRoomState[18] = new RoomStateClass(9, 30, '2010-05-01', '2010-05-03', 100);
arrRoomState[19] = new RoomStateClass(9, 20, '2010-05-01', '2010-05-03', 200);
arrRoomState[20] = new RoomStateClass(7, 12, '2010-05-01', '2010-05-03', 100);
arrRoomState[21] = new RoomStateClass(7, 13, '2010-05-01', '2010-05-03', 100);
arrRoomState[22] = new RoomStateClass(7, 28, '2010-05-01', '2010-05-03', 100);
arrRoomState[23] = new RoomStateClass(7, 29, '2010-05-01', '2010-05-03', 100);
arrRoomState[24] = new RoomStateClass(8, 15, '2010-09-02', '2010-09-30', 59);
arrRoomState[25] = new RoomStateClass(8, 16, '2010-09-02', '2010-09-30', 6);
arrRoomState[26] = new RoomStateClass(8, 17, '2010-09-02', '2010-09-30', 5);
arrRoomState[27] = new RoomStateClass(8, 18, '2010-09-02', '2010-09-30', 26);
arrRoomState[28] = new RoomStateClass(8, 19, '2010-09-02', '2010-09-30', 5);
arrRoomState[29] = new RoomStateClass(8, 33, '2010-09-02', '2010-09-30', 3);
arrRoomState[30] = new RoomStateClass(8, 34, '2010-09-02', '2010-09-30', 24);
arrRoomState[31] = new RoomStateClass(5, 31, '2010-08-26', '2010-09-30', 2);
arrRoomState[32] = new RoomStateClass(5, 4, '2010-09-01', '2010-09-04', 29);
arrRoomState[33] = new RoomStateClass(5, 5, '2010-09-01', '2010-09-04', 69);
arrRoomState[34] = new RoomStateClass(5, 7, '2010-09-01', '2010-09-04', 4);
arrRoomState[35] = new RoomStateClass(5, 25, '2010-09-01', '2010-09-04', 26);
arrRoomState[36] = new RoomStateClass(5, 26, '2010-09-01', '2010-09-04', 25);
arrRoomState[37] = new RoomStateClass(5, 4, '2010-09-02', '2010-09-03', 31);
arrRoomState[38] = new RoomStateClass(5, 7, '2010-09-02', '2010-09-03', 4);
arrRoomState[39] = new RoomStateClass(5, 26, '2010-09-02', '2010-09-03', 27);
arrRoomState[40] = new RoomStateClass(5, 25, '2010-09-02', '2010-09-03', 25);
arrRoomState[41] = new RoomStateClass(5, 5, '2010-09-02', '2010-09-03', 70);
//roomstate end
//branch start
var arrBranch = new Array();
arrBranch[0] = new BranchClass(5, '潘家园店', '北京市朝阳区潘家园武圣路武圣东里43号');
arrBranch[1] = new BranchClass(6, '周家庄路店', '北京市朝阳区周家庄路（三环医院西侧）');
arrBranch[2] = new BranchClass(7, '丰益桥店', '北京市丰台区丰益桥东管头前街108号');
arrBranch[3] = new BranchClass(8, '分中寺店', '北京市丰台区方庄桥东分中寺302号');
arrBranch[4] = new BranchClass(9, '十里河店', '北京市朝阳区十里河桥南东三环南路9号.');
//branch end
//room start
var arrRoom = new Array();
arrRoom[0] = new RoomClass(4, 5, '商务大床房', 179,179, 31);
arrRoom[1] = new RoomClass(5, 5, '标准间', 179,179, 72);
arrRoom[2] = new RoomClass(7, 5, '商务标准间', 199,199, 4);
arrRoom[3] = new RoomClass(9, 6, '标准间', 138,138, 25);
arrRoom[4] = new RoomClass(10, 6, '大床房', 138,138, 27);
arrRoom[5] = new RoomClass(12, 7, '浪漫大床房', 158,158, 5);
arrRoom[6] = new RoomClass(13, 7, '舒适标准间', 138,138, 8);
arrRoom[7] = new RoomClass(15, 8, '标准间', 158,150, 69);
arrRoom[8] = new RoomClass(16, 8, '商务标准间', 178,170, 9);
arrRoom[9] = new RoomClass(17, 8, '大床房', 178,170, 10);
arrRoom[10] = new RoomClass(18, 8, '浪漫大床房A', 178,170, 36);
arrRoom[11] = new RoomClass(19, 8, '温馨家庭房', 178,170, 7);
arrRoom[12] = new RoomClass(20, 9, '标准间南', 199,199, 50);
arrRoom[13] = new RoomClass(21, 9, '大床房', 199,199, 43);
arrRoom[14] = new RoomClass(22, 9, '套房', 399,399, 4);
arrRoom[15] = new RoomClass(25, 5, '大床房A', 138,138, 26);
arrRoom[16] = new RoomClass(26, 5, '大床房B', 179,179, 27);
arrRoom[17] = new RoomClass(27, 6, '大床房B', 158,158, 5);
arrRoom[18] = new RoomClass(28, 7, '单人间', 138,138, 2);
arrRoom[19] = new RoomClass(29, 7, '时尚标准间', 138,138, 2);
arrRoom[20] = new RoomClass(30, 9, '商务大床', 218,218, 25);
arrRoom[21] = new RoomClass(31, 5, '三人间', 219,219, 2);
arrRoom[22] = new RoomClass(32, 7, '三人间', 168,168, 0);
arrRoom[23] = new RoomClass(33, 8, '浪漫大床房B', 198,198, 4);
arrRoom[24] = new RoomClass(34, 8, '标准间B', 158,150, 34);
arrRoom[25] = new RoomClass(35, 9, '标准间西', 199,199, 27);
arrRoom[26] = new RoomClass(36, 9, '特价大床房', 179,179, 3);
arrRoom[27] = new RoomClass(37, 9, '标准间北', 199,199, 28);
//room end

$('#branch').click(function()
{
    var vStartDate = $('#startDate').val();
    var vEndDate = $('#endDate').val();
    if (vStartDate=='' || vEndDate=='')
    {
        alert('请先选日期');
        $(this).blur();
        if (vStartDate=='')
        {
            $('#startDate').click();
        }
        else
        {
            $('#endDate').click();
        }
    }
    else
    {
        var myhtml = '<div class="head"><b>选择分店</b></div><ul>';
        for (var i=0;i<arrBranch.length;i++)
        {
            myhtml += '<li name="' + arrBranch[i].name + '"><b>' + arrBranch[i].name + '</b><br />地址：' + arrBranch[i].address + '</li>\n';
        }
        myhtml += '</ul><div class="foot">关闭窗口</div>';
        var div;
        if (!document.getElementById('branchList'))
        {
            div = document.createElement('div');
            div.id = 'branchList';
        }
        else
        {
            div = document.getElementById('branchList');
        }
        $(div).html(myhtml);
        var offset = $(this).offset();
        $(div).css({position:'absolute',width:'200px',left:offset.left+'px',top:offset.top+20+'px',zIndex:'102'});
        $(div).appendTo($('body'));
        $(div).show();
        $('#CalFrame').hide();
        $('#roomlist').hide();
        
        //关闭窗口
        $('#branchList .foot').click(function()
        {
            $('#branchList').hide();
        });
        
        //选择
        $('#branchList ul li').hover(function()
        {
            $(this).css({background:'#ececec'});
        },function()
        {
            $(this).css({background:'#ffffff'});
        }
        ).click(function()
        {
            $('#branch').val($(this).attr('name')).attr('title', $(this).text());
            $('#branchList').hide();
            $('#room').click();
        });
    }
});

$('#room').click(function(){
    var branchName = $('#branch').val();
    if (branchName=='')
    {
        alert('请先选分店');
        $(this).blur();
        $('#branch').click();
    }
    else
    {
        for (var i=0;i<arrBranch.length;i++)
        {
            if (arrBranch[i].name==branchName)
            {
                //显示房间列表
                var vStartDate = $('#startDate').val();
                var vEndDate = $('#endDate').val();
                ShowRoomList(arrBranch[i].id,vStartDate,vEndDate);
            }
        }
        if (i<arrBranch.length)
        {
            alert('暂无此分店的信息');
        }
    }
});


function ShowRoomList(branchid, vStartDate, vEndDate)
{
    var myhtml = '<div class="head"><b>选择房型</b></div><ul>';
    myhtml += '<table width="260" border="0" cellspacing="1" cellpadding="2"><tr class="thead"><td>房型</td><td>门市价</td><td>会员价</td><td>空房</td><td>&nbsp;</td></tr>';
    for (var i=0;i<arrRoom.length;i++)
    {
        if (arrRoom[i].branchid==branchid)
        {
            myhtml += '<tr><td>' + arrRoom[i].type + '</td>';
            myhtml += '<td>' + arrRoom[i].retailPrice + '</td>';
            myhtml += '<td>' + arrRoom[i].membersPrice + '</td>'; 
            var emptyCount = GetEmptyRoomAmount(arrRoom[i], vStartDate, vEndDate);
            if (emptyCount <= 0)
            {
                myhtml += '<td>客满</td>';
                myhtml += '<td>&nbsp;</td>';
            }
            else
            {
                myhtml += '<td>' + emptyCount + '间</td>';
                myhtml += '<td><span class="btnSel" type="' + arrRoom[i].type + '">[选中]</span></td>';
            }
            myhtml += '</tr>';
        }
    }
    myhtml += '</table></ul><div class="foot">关闭窗口</div>';
    var div;
    if (!document.getElementById('roomlist'))
    {
        div = document.createElement('div');
        div.id = 'roomlist';
    }
    else
    {
        div = document.getElementById('roomlist');
    }
    $(div).html(myhtml);
    var offset = $('#room').offset();
    $(div).css({position:'absolute',width:'260px',left:offset.left+'px',top:offset.top+20+'px',zIndex:'101'});
    $(div).appendTo($('body'));
    $(div).show();
    $('#CalFrame').hide();
    
    //关闭窗口
    $('#roomlist .foot').click(function()
    {
        $('#roomlist').hide();
    });
    
    //选择
    $('#roomlist tr').hover(function()
    {
        $(this).css({background:'#ececec'});
    },function()
    {
        $(this).css({background:'#ffffff'});
    }
    )
    
    $('#roomlist .btnSel').css("cursor", "pointer").click(function()
    {
        $('#room').val($(this).attr('type'))
            .attr('title',$(this).attr('type'));            
        $('#roomlist').hide();
    });
}
