﻿
//Search box:
function ShowAdvSearch(type,bExpand) 
{
    var hSearch = document.getElementById('HSearch'+type);
    var searchCol = document.getElementById('searchCol' + type);
    bExpand = typeof (bExpand) == 'undefined' ? searchCol.style.display == "none" : bExpand;
    searchCol.style.display = bExpand ? "" : "none";
    hSearch.className = bExpand ? "titleH3SearchExpand" : "titleH3Search";
}
function Amenities_Click(obj) 
{
    var amenitiesAll = document.getElementById("AmenitiesAll");
    if (obj.id != 'AmenitiesAll') {
        if (obj.checked == false)
            amenitiesAll.checked = false;
        return;
    }
    var checkboxes = document.getElementsByName("cbAmenities");
    for (var k = 0; k < checkboxes.length; k++)
        checkboxes[k].checked = amenitiesAll.checked;
}
function AdvancedSearch_Click(e) 
{
    e = e || event;
    var url = "/Search.aspx?city="+ encodeURIComponent(document.getElementById('frmInputcity').value);
    var checkin = document.getElementById("Checkin");
    var checkout = document.getElementById("Checkout");
    var area = document.getElementById("inputDirection");
    var keyWord = document.getElementById("SearchKeywords");
    var OAmenitiesAll = document.getElementById("AmenitiesAll");
    var OAmenities = document.getElementsByName("cbAmenities");
    var OHotelTypeSel = document.getElementsByName("HotelTypeSel");
    if (document.getElementById('frmInputcity').value == "input city name or first words" || document.getElementById('frmInputcity').value == "") {
        document.getElementById('frmInputcity').onclick();
        return false;
    }
    if (checkin.value != "yyyy-mm-dd")
        url += "&checkin=" + checkin.value;
    if (checkout.value != "yyyy-mm-dd")
        url += "&checkout=" + checkout.value;
    if (checkin.value == "yyyy-mm-dd") {
        showCalendar('Checkin', false, 'Checkin');
        cancelBubble(e);
        return false;
    }
    if (checkout.value == "yyyy-mm-dd") {
        showCalendar('Checkout', false, 'Checkout');
        cancelBubble(e);
        return false;
    }
    if (Date.parse(checkout.value.replace(/-/g, '/')) <= Date.parse(checkin.value.replace(/-/g, '/'))) {
        showCalendar('Checkout', false, 'Checkout');
        cancelBubble(e);
        return false;
    }
    if (area.value != "")
        url += "&area=" + encodeURIComponent(area.value);
    if (keyWord.value != "" && keyWord.value != keyWord.title)
        url += "&keywords=" +encodeURIComponent( keyWord.value);
    for (var i = 0; i < OHotelTypeSel.length; i++) 
    {
        if (OHotelTypeSel[i].checked) 
        {
            if (OHotelTypeSel[i].value == "0")
                break;
            url += "&hotelTypeID=" + OHotelTypeSel[i].value;
        }
    }
    var OPriceFrom = document.getElementById("PriceFrom");
    var OPriceTo = document.getElementById("PriceTo");
    var ORatings = document.getElementById("Ratings");
    if (OPriceFrom.value != "0")
        url += "&fromprice=" + OPriceFrom.value;
    if (OPriceTo.value != "0")
        url += "&ToPrice=" + OPriceTo.value;
    if (ORatings.value != "0")
        url += "&rating=" + ORatings.value;
    for (var m = 0; m < OAmenities.length; m++)
        if (OAmenities[m].checked)
            url += "&" + OAmenities[m].value + "=1";
    document.location.href = url;
}
function HomeSearch_Click(e) {
    e = e || event;
    var url = "/Search.aspx";
    var checkin = document.getElementById("Checkin");
    var checkout = document.getElementById("Checkout");
    var keyWord = document.getElementById("SearchKeywords");
    if (document.getElementById('frmInputcity').value == "input city name or first words" || document.getElementById('frmInputcity').value == "") {
        document.getElementById('frmInputcity').onclick();
        return false;
    }
    var city = "?city=" +encodeURIComponent( document.getElementById('frmInputcity').value);
    url += city;
    if (checkin.value != "yyyy-mm-dd")
        url += "&checkin=" + checkin.value;
    if (checkout.value != "yyyy-mm-dd")
        url += "&checkout=" + checkout.value;
    if (checkin.value == "yyyy-mm-dd") {
        showCalendar('Checkin', false, 'Checkin');
        cancelBubble(e);
        return false;
    }
    if (checkout.value == "yyyy-mm-dd") {
        showCalendar('Checkout', false, 'Checkout');
        cancelBubble(e);
        return false;
    }
    if (Date.parse(checkout.value.replace(/-/g, '/')) <= Date.parse(checkin.value.replace(/-/g, '/'))) {
        showCalendar('Checkout', false, 'Checkout');
        cancelBubble(e);
        return false;
    }
    if (keyWord.value != "" && keyWord.value != keyWord.title)
        url += "&keywords=" + encodeURIComponent(keyWord.value);
    document.location.href = url;
}
function LoadSearchBox() 
{
    var url = (document.location).toString();
    var arr = url.split(/[?&=]/);
    for (i = 1; i + 1 < arr.length; i += 2) 
    {
        switch (arr[i].toLowerCase()) {
            case 'checkin':
                document.getElementById("Checkin").value = arr[i + 1];
                break;
            case 'checkout':
                document.getElementById("Checkout").value = arr[i + 1];
                break;
            case 'keywords':
                document.getElementById("SearchKeywords").value = decodeURIComponent( arr[i + 1]);
                break;
            case 'hoteltypeid':
                ShowAdvSearch(1,true);
                if (arr[i + 1] == 12)
                    document.getElementById("HotelTypeApartment").checked = true;
                if (arr[i + 1] == 13)
                    document.getElementById("HotelTypeResort").checked = true;
                if (arr[i + 1] == 14)
                    document.getElementById("HotelTypeInternationalBrand").checked = true;
                if (arr[i + 1] == 15)
                    document.getElementById("HotelTypeEconomicChain").checked = true;
                break;
            case 'fromprice':
                ShowAdvSearch(2, true);
                document.getElementById("PriceFrom").value = arr[i + 1];
                break;
            case 'toprice':
                ShowAdvSearch(2, true);
                document.getElementById("PriceTo").value = arr[i + 1];
                break;
            case 'rating':
                ShowAdvSearch(2, true);
                document.getElementById("Ratings").value = arr[i + 1];
                break;
            case 'binternet':
                ShowAdvSearch(3, true);
                document.getElementById("AmenitiesInternet").checked = true;
                break;
            case 'bairportshuttle':
                ShowAdvSearch(3, true);
                document.getElementById("AmenitiesAirportShuttle").checked = true;
                break;
            case 'brestaurant':
                ShowAdvSearch(3, true);
                document.getElementById("AmenitiesRestaurant").checked = true;
                break;
            case 'bswimmingpool':
                ShowAdvSearch(3, true);
                document.getElementById("AmenitiesSwimmingPool").checked = true;
                break;
            case 'bparking':
                ShowAdvSearch(3, true);
                document.getElementById("AmenitiesParking").checked = true;
                break;
            case 'bfitnessroom':
                ShowAdvSearch(3, true);
                document.getElementById("AmenitiesFitnessRoom").checked = true;
                break;
        }
    } 
} 
//show the Locations of the city:
function showDirection()
{
    var MSG_SelectDestination = "Please select Destination first!";
    var MSG_CityNotArea = "There is no area in this city!";
    var TextAreaCommercial = "Commercial area";
    var TextAreaAdministrative = "Administrative area";
    
	var directiondiv = document.getElementById('DirectionDiv');	
	var iframearea = document.getElementById('IframeArea');
	
	var strCommercialLi,strAdministrativeLi;
	strCommercialLi='';
	strAdministrativeLi='';
	if( typeof(arrayCityArea)=='undefined' || arrayCityArea==null )
	{
		alert(MSG_SelectDestination );
		return;
	}
	if( arrayCityArea[0].length==0 && arrayCityArea[1].length==0 )
	{
		alert(MSG_CityNotArea);
		return;
	}
	document.getElementById('adSearchAreaBox').innerHTML ='';
	var areaCount1 = 0, areaCount2 = 0;
	if (arrayCityArea[0].length>0){		
		strCommercialLi += "<div class='directionBoxTop'><h3>"+TextAreaCommercial+"</h3><ul id='CommercialArea' class='commercialArea'>";
		for (i=0 ;i<arrayCityArea[0].length; i++)
		    strCommercialLi += "<li class='areaListLi'><a href='javascript: void 0;' onclick='writeAreaInput(this);' title=\"" + arrayCityArea[0][i] + "\">" + arrayCityArea[0][i] + "</a></li>";
		strCommercialLi += "</ul><div class='clear'></div></div>";
		document.getElementById('adSearchAreaBox').innerHTML += strCommercialLi;
		areaCount1 = arrayCityArea[0].length;
		areaCount1 = (areaCount1%3 ==0 ? ((areaCount1/3)*20) : (((areaCount1/3)+1)*20)) +19;
	}
	if(arrayCityArea[1].length>0 ){
		strAdministrativeLi +="<div class='directionBoxBottom'><h3>"+TextAreaAdministrative+"</h3><ul id='AdministrativeArea' class='administrativeArea'>";
		for (i=0 ;i<arrayCityArea[1].length; i++)
		    strAdministrativeLi += "<li class='areaListLi'><a href='javascript: void 0;' onclick='writeAreaInput(this);' title=\"" + arrayCityArea[1][i] + "\">" + arrayCityArea[1][i] + "</a></li>";
		"</ul><div class='clear'></div></div>";
		document.getElementById('adSearchAreaBox').innerHTML += strAdministrativeLi;
		areaCount2 = arrayCityArea[1].length;
		areaCount2 = (areaCount2%6 ==0 ? ((areaCount2/6)*20) : (((areaCount2/6)+1)*20)) +19;
	}
	iframearea.height = 25 + areaCount1 + areaCount2;
	directiondiv.style.display = '';
	iframearea.style.display = '';
}
function hiddenDirection()
{
	var directiondiv = document.getElementById('DirectionDiv');
	var iframearea = document.getElementById('IframeArea');
	if( iframearea!=null && typeof(iframearea)!='undefined' )
		iframearea.style.display = 'none';
	if( directiondiv!=null && typeof(directiondiv)!='undefined' )
	directiondiv.style.display = 'none';
}
function writeAreaInput(obj) {
    document.getElementById('inputDirection').value = typeof(obj)=='undefined' ? '' : obj.title;
	hiddenDirection();
}

// show viewed history
function ShowViewedHotels() 
{
    var viewedHotels = GetCookie('viewedHotels');
    var arrHotels = viewedHotels.split('"');
    var html = '';
    for( var i=0; i<20 && i+1<arrHotels.length; i+=2 )
        html += "<li><a href='" + arrHotels[i] + "' title=\""+arrHotels[i+1] + "\">" + arrHotels[i+1] + "</a></li>";
    document.getElementById('ViewedHistoryUL').innerHTML = html;
}

//click the room name to show the room detail:
function ShowRoomDetail(roomID) 
{
    var div = document.getElementById('roomDetail_' + roomID);
    div.style.display = div.style.display == 'none' ? '' : 'none';
    var tr = document.getElementById('RoomDetailTR_' + roomID);
    tr.className = tr.className == 'rtd_TR2' ? 'rtd_TR2Line' : 'rtd_TR2';
}

function Search_PressEnter(e) 
{
    e = e || event;
    if( e.keyCode==13 )
    {
        AdvancedSearch_Click(event);
        return false;
    }
}

function Search_HomePressEnter(e) 
{
    e = e || event;
    if( e.keyCode==13 )
        HomeSearch_Click(e);
}
