﻿var moreResults = true;
var totalHotels;


function fetchHotels(firstTimeCall)
{    

    var paramList = "MethodToCall=GetAvailableHotels";
    paramList += "&FirstTimeCall=" + encodeURI(firstTimeCall);
    var async = true;
    var method = "POST";
    getDataFromServer(requestUrl, callBackFetchHotels, async, method, paramList);
}

function callBackFetchHotels()
{

    var value = this.req.responseText;    
    var data;        
    var xDoc = loadXMLDoc(value);

    var xmlNodes = xDoc.getElementsByTagName("hotels");
    var errorMessage;

    if(xmlNodes.length == 0)
    {
		xmlNodes = xDoc.getElementsByTagName("error");
        if (xmlNodes.length > 0)
        {
			if (xmlNodes[0].getAttribute("redirect") == "true")
	        {
	            pageurl = xmlNodes[0].getAttribute("pageurl");
	            window.location.href = pageurl;
	        }
			else
			{
	            $fn(_endsWith("divNoAvailabilityMessage")).innerHTML = xmlNodes[0].firstChild.nodeValue;
	            if(null !=$fn(_endsWith("subHeader")))
	            {
	                $fn(_endsWith("subHeader")).innerHTML = "";
	            }
	            if(null != $fn(_endsWith("allHotels")))
	            {
	                $fn(_endsWith("allHotels")).innerHTML = "";
	            }
	            if(null != $fn(_endsWith("footerContainer")))
	            {
	                $fn(_endsWith("footerContainer")).innerHTML = "";
	            }
	            hidePaginationSearch();
			}
        }
    }
    else
    {
        xmlNodes = xDoc.getElementsByTagName("hotel");
        totalHotels = xDoc.getElementsByTagName("hotels")[0].getElementsByTagName('totalhotels')[0].firstChild.nodeValue;
        
        // Display the error message if returned in the hotel response in the top block of the page
		var errorTag = xDoc.getElementsByTagName("hotels")[0].getElementsByTagName('error')[0]
        if (errorTag)
        {
			var errorMessage = errorTag.firstChild.nodeValue;
            $fn(_endsWith("divNoAvailabilityMessage")).innerHTML = errorMessage;
        }
        
        // R1.4 Marketting City
        // if alCityHotelTag is true then hide the divAvailability of ReservationInformationContainer  
        var altCityHotelTag = xDoc.getElementsByTagName("hotels")[0].getElementsByTagName('altCityHotelsfound')[0]
        if (altCityHotelTag)
        {
			var errorMessage = errorTag.firstChild.nodeValue;
            $fn(_endsWith("divAvailability")).innerHTML = "";
        }

        var hotelCount = xmlNodes.length;
	    if ( hotelCount > 0 )
	    {
			hotelParent  = $fn(_endsWith('allHotels'));
			if(hotelParent != null)
			{
			    hotelParent.innerHTML = '';
			}
	        for(ctr=0; ctr<hotelCount && ctr<pageSize; ctr++)
	        {
	            if(xmlNodes[ctr].childNodes.length > 0)
	            {
	                if(xmlNodes[ctr].getElementsByTagName("displayed")[0].firstChild.nodeValue == "0")
	                {
	                    hotel = GetHotel(xmlNodes[ctr]);
	                    AddHotel(hotel, hotelParent);
	                }
	            }
	        }

	        if (ctr==pageSize) {
	            showPaginationSearch();
            }
	    }    	

	    if(xDoc.getElementsByTagName("hotels")[0].getElementsByTagName('morehotelstofetch')[0].firstChild.nodeValue == "true")
	    {	
	        setTimeout('fetchHotels(false)', 500);
	    }
	    else 
	    {
            data = "<div class=\"clear\">&nbsp;</div>";
            addData(data);
	        SetPageStrs();
	        SetPagination();
	        hidePaginationSearch();
			RefreshGoogleMap();
	    }
    }
}

function GetHotel(hotelXML)
{
    hotel = new HotelDetails();

    hotel.ID            = getStringFromNode(hotelXML.getElementsByTagName("code")[0]);
    hotel.imageSrc      = getStringFromNode(hotelXML.getElementsByTagName("image")[0]);
    if (!hotel.imageSrc) 
        hotel.imageSrc = "/Templates/Booking/Images/noimage.gif";
    hotel.imageAlt      = getStringFromNode(hotelXML.getElementsByTagName("name")[0]);
	hotel.hotelUrl		= getStringFromNode(hotelXML.getElementsByTagName("url")[0]);
	hotel.hotelName		= getStringFromNode(hotelXML.getElementsByTagName("name")[0]);
	hotel.rateTitle		= getStringFromNode(hotelXML.getElementsByTagName("rateTitle")[0]);
	hotel.price			= getStringFromNode(hotelXML.getElementsByTagName("rate")[0]);
	hotel.perNight		= getStringFromNode(hotelXML.getElementsByTagName("prpn")[0]);
	hotel.selectHotel	= getStringFromNode(hotelXML.getElementsByTagName("sH")[0]);;
	hotel.address		= getStringFromNode(hotelXML.getElementsByTagName("address")[0]);
	hotel.teaser		= getStringFromNode(hotelXML.getElementsByTagName("description")[0]);
	hotel.distance		= getStringFromNode(hotelXML.getElementsByTagName("distance")[0]);
	hotel.direction		= getStringFromNode(hotelXML.getElementsByTagName("direction")[0]);
	hotel.drivingTime	= getStringFromNode(hotelXML.getElementsByTagName("drivingTime")[0]);
	// R1.4 Marketting city
	hotel.distanceText		= getStringFromNode(hotelXML.getElementsByTagName("distanceText")[0]);
	hotel.directionText		= getStringFromNode(hotelXML.getElementsByTagName("directionText")[0]);
	hotel.drivingTimeText	= getStringFromNode(hotelXML.getElementsByTagName("drivingTimeText")[0]);
	return hotel;
}

function HotelDetails()
{
	this.ID				= null;
	this.imageSrc		= null;
	this.imageAlt		= null;
	this.hotelUrl		= null;
	this.hotelName		= null;
	this.rateTitle		= null;
	this.price			= null;
	this.perNight		= null;
	this.selectHotel	= null;
	this.address		= null;
	this.teaser			= null;
	this.distance = null;
	this.direction = null;
	this.drivingTime = null;
	// R1.4 Marketting City
	this.distanceText = null;
	this.directionText = null;
	this.drivingTimeText = null;
	
}

function AddHotel(hotel, parent)
{
	hotelDetailBody		= $.DIV({className:'hotelDetailBody'});
	
	// Column one
	columnOne		= $.DIV({className:'hotelDetail_columnOne'});
	// Round corners
	roundcornerTop		= $.DIV({className:'RoundedCornersTop226'});
	roundcornerImage		= $.DIV({className:'RoundedCornersImage226'});
	image		= $.IMG({'src':hotel.imageSrc, 'alt':hotel.imageAlt, 'width':'226', 'height':'148'});
	
	roundcornerBottom		= $.DIV({className:'RoundedCornersBottom226'});
	
	
	// Adding image to roundcornerImage div
	$(roundcornerImage).append(image);
	
	// Adding roundcornerTop div to columnOne
	$(columnOne).append(roundcornerTop);	
	// Adding roundcornerImage div to columnOne
	$(columnOne).append(roundcornerImage);
	// Adding roundcornerBottom div to columnOne
	$(columnOne).append(roundcornerBottom);
		
	// Adding Column one to hotelsParent
	$(hotelDetailBody).append(columnOne);

	//Column two
	columnTwo		= $.DIV({className:'hotelDetail_columnTwo'});
	var hotelName;
	if (hotel.hotelUrl)
	    hotelName		= $.P({className:'link'}, $.A({href:hotel.hotelUrl},hotel.hotelName));
	else
	    hotelName		= $.P({},hotel.hotelName);

	$(columnTwo).append(hotelName);
	//Column two: hotelDetails
	hotelDetails		= $.DIV({className:'hotelDetail_info'});
	//Price container
	if (hotel.rateTitle)
	{
		hotelPrice		= $.DIV({className:'hotelDetail_price rateTitleContainer'});
		rateTitle		= $.DIV({className:'rateTitle'},$.SPAN({},hotel.rateTitle));
		$(hotelPrice).append(rateTitle);
	}
	else
	{
		hotelPrice		= $.DIV({className:'hotelDetail_price'});
	}

	priceContainer		= $.DIV({className:'inner'}, $.DIV({className:'rate'}, $.STRONG({},hotel.price), $.BR({}), $.SPAN({},hotel.perNight)));
	//Submit button
	buttonContainer		= $.DIV({className:'btnContainer'}, $.SPAN({className:'btnSubmit'},$.A({href:'javascript:RedirectToSelectRate('+hotel.ID+');'},$.SPAN({},hotel.selectHotel))));
    
	$(priceContainer).append(buttonContainer);
	$(hotelPrice).append(priceContainer);
	$(hotelDetails).append(hotelPrice);
	//Address
	address		= $.P({},hotel.address);
	
	$(hotelDetails).append(address);
	//Teaser
	teaser		= $.P({},hotel.teaser);
	
	$(hotelDetails).append(teaser);
	
	//distance, drivingtime and direction for R1.4 Marketting City


	if (hotel.distance)
	{
	    distance = $.P({},$.STRONG({}, hotel.distanceText,': '), hotel.distance);
	    $(hotelDetails).append(distance);
	}
	if (hotel.drivingTime)
	{
    	drivingTime = $.P({},$.STRONG({}, hotel.drivingTimeText,': '),hotel.drivingTime);
        $(hotelDetails).append(drivingTime);
    }
    if (hotel.direction)
	{
	    direction = $.P({},$.STRONG({}, hotel.directionText,': '),hotel.direction);
	    $(hotelDetails).append(direction);
	}
    
	$(columnTwo).append(hotelDetails);
	clearAll		= $.DIV({className:'clear'},$.NBSP);
	
	$(columnTwo).append(clearAll);
	$(hotelDetailBody).append(columnTwo);
    $(hotelDetailBody).append(clearAll);

	$(parent).append(hotelDetailBody);

}

function addData(data)
{
    $fn("hotelDetailContainer").innerHTML = $fn("hotelDetailContainer").innerHTML + data;
}

var totalPages;
var pageDetails = new Array();
function SetPageStrs()
{
    if (totalHotels > pageSize)
        totalPages = Math.ceil(totalHotels/pageSize)
    else
        totalPages = 1;   

    var startVal = 1;
    for (i = 0; i < totalPages; i++)
    {
        var endVal = startVal + pageSize - 1;
        if (endVal > totalHotels)
            endVal = totalHotels;
        if (startVal == endVal)
            pageDetails[i] = startVal;
        else
            pageDetails[i] = startVal + "-" + endVal;
        startVal = endVal + 1;
    }
    return pageDetails;
}

var currentPage;
var paginationDiv; 
function SetPagination()
{
    if(totalPages == 1)
    {
        var hotelFooter = $fn("hotelFooterContainer");
        if (hotelFooter)
        {
            hotelFooter.innerHTML = "";
        }   
    }
    else
    {
        currentPage = $fn(_endsWith("txtPageNo")).value;
        paginationDiv = $fn("pageListing"); 
        var pD = "";

        pD = pD + "<span class=\"prevLink\">";
        if (currentPage != 1)
        {
	        pD = pD + "<a href=\"javascript:SelectPagination(1);\">" + prv + "</a>";
        } 
        else
        {
            pD = pD + prv;
        }
        pD = pD + "</span>";

        for (i=0; i<pageDetails.length; i++)
        {
            pD = pD + " | ";
            pD = pD + "<span class=\"pageItem\">";
            if (currentPage != i+1) {
                pD = pD + "<a href=\"javascript:SelectPagination(" + (i+1) + ");\">" + pageDetails[i] + "</a>";
            }
            else {
                pD = pD + "<span class=\"pageItemSelected\">" + pageDetails[i] + "</span>"
            }
            pD = pD + "</span>"
        }

        pD = pD + " | ";
        pD = pD + "<span class=\"nextLink\">";
        if (currentPage != totalPages)
        {
	        pD = pD + "<a href=\"javascript:SelectPagination(" + ((currentPage*1)+1) + ");\">" + nxt + "</a>";
        }
        else
        {
            pD = pD + nxt;
        }
        pD = pD + "</span>";

        paginationDiv.innerHTML = pD;
    }
}

function SelectPagination(pn)
{
    $fn(_endsWith("txtPageNo")).value = pn;
    var SelectPagination = $fn(_endsWith('SelectPagination'));
    eval(SelectPagination.href);
    
}
function RefreshGoogleMap()
{
	var googleMapIframe = document.getElementById('SelectHotelGoogleMapIframe');
	var source = googleMapIframe.src
	googleMapIframe.contentWindow.location.reload(true)
}

function RedirectToSelectRate(hotelCode)
{
    var TxtHotelId = $fn(_endsWith('txtHotelId'));
    TxtHotelId.value = hotelCode;
    var SelectOneHotel = $fn(_endsWith('SelectOneHotel'));
    eval(SelectOneHotel.href);
}

function RedirectToBookDetail(roomCategoryID, rateCategory)
{
    $fn(_endsWith("txtRoomCategoryID")).value = roomCategoryID;
    $fn(_endsWith("txtRateCategory")).value = rateCategory;
    theForm.submit();
}

function FindDestinationByName(InputDestination,flag)
{
    var destArray;
    var matchingCityDestinations;
    var totalCities;    
    //If non bookable hotels need to be included in type ahead dropDown then following will execute.
    if(flag)
    {
        matchingCityDestinations = getMatchingDestinations(nonBookableHotelList, InputDestination, true);
    }
    else
    {
        //This will execute in normal flow.
        matchingCityDestinations = getMatchingDestinations(masterCityList, InputDestination, true);
    }
    if (matchingCityDestinations != null)
    {
        totalCities = matchingCityDestinations.length;
        destArray = new Array(totalCities);
        
        for(ctr = 0; ctr < totalCities; ctr++)
        {
            destArray[ctr] = new Array(2);
            
            destArray[ctr][0] = matchingCityDestinations[ctr].ID + "#$:~" + matchingCityDestinations[ctr].name;
            if(matchingCityDestinations[ctr].hotels != null)
            {
                destArray[ctr][1] = new Array(matchingCityDestinations[ctr].hotels.length);
                for(ctrHotel = 0; ctrHotel < matchingCityDestinations[ctr].hotels.length; ctrHotel++)
                {
                    destArray[ctr][1][ctrHotel] = matchingCityDestinations[ctr].hotels[ctrHotel].ID + "#$:~" + matchingCityDestinations[ctr].hotels[ctrHotel].name;
                }
            }
        }    
    }
    return destArray;
}

// Please do not delete this block for now. -Himansu
//***********************************************************************************************************
//var destArray;
//function FindDestinationByName(InputDestination)
//{
//    //destArray = new Array();
//    var paramList = "MethodToCall=FindDestinationByName";
//    paramList +=  "&InputDestination=" + encodeURI(InputDestination);
//    var async = false;
//    var method = "POST";

//    getDataFromServer(requestUrl, callBackFindDestinationByName, async, method, paramList);
//    return destArray;
//}

//function callBackFindDestinationByName()
//{
//    var value = this.req.responseText;    

//    var xDoc = loadXMLDoc(value);
//    var xmlNodes = xDoc.getElementsByTagName("error");
//    var errorMessage;

//    if(xmlNodes.length > 0)
//    {
//        /*if (xmlNodes[0].getAttribute("redirect") == "true")
//            window.location.href = "Error.aspx?ErrMsg=" + xmlNodes[0].firstChild.nodeValue;
//        else        */
//            $fn(_endsWith(errorDivID)).innerHTML = xmlNodes[0].text;
//    }
//    else
//    {
//        xmlNodes = xDoc.getElementsByTagName("destination");
//        destArray = new Array(xmlNodes.length);
//        
//        for(ctr=0; ctr < xmlNodes.length; ctr++)
//	    {	        
//	        destArray[ctr] = new Array(2);
//	        var xmlCityNode = xmlNodes[ctr].getElementsByTagName("city");
//	        destArray[ctr][0] = xmlCityNode[0].getAttribute("code") + ":" + getStringFromNode(xmlCityNode[0]);
//	        
//	        var xmlHotelNode = xmlNodes[ctr].getElementsByTagName("hotel");
//	        destArray[ctr][1] = new Array(xmlHotelNode.length);
//	        for(ctrHotel=0; ctrHotel < xmlHotelNode.length; ctrHotel++)
//	        {	            
//	            destArray[ctr][1][ctrHotel] = xmlHotelNode[ctrHotel].getAttribute("code") + ":" + getStringFromNode(xmlHotelNode[ctrHotel]);
//	        }
//	    }
//	}
//	return destArray;
//}
//***********************************************************************************************************


function SetHideUnHide()
{
    var sA = $fn(_endsWith("txtShowAll"));
    if (sA.value == "TRUE") sA.value = "FALSE";
    else sA.value = "TRUE";
    //artf1045294 -Start
    if(null != $fn(_endsWith("txtRoomCategoryID")))
    {
        $fn(_endsWith("txtRoomCategoryID")).value = "";
    }
    if(null != $fn(_endsWith("txtRateCategory")))
    {    
        $fn(_endsWith("txtRateCategory")).value = "";
    }
    //artf1045294 -End
}

function SetHideUnHideListingControl()
{
    var sA = $fn(_endsWith("txtShowAll"));
    if (sA.value == "TRUE") sA.value = "FALSE";
    else sA.value = "TRUE";    
}

function SetViewPricePerStay()
{
    var perStay = $fn(_endsWith("txtPerStay"));
    if (perStay.value == "TRUE") perStay.value = "FALSE";
    else perStay.value = "TRUE";
    
    $fn(_endsWith("txtRoomCategoryID")).value = "";
    $fn(_endsWith("txtRateCategory")).value = "";
}