
//This function extracts all properties name and values from an object 
//to build a query string expression in the form [propertyName]=[propertyValue]&(...)&[propertyName(n)]=[propertyValue(n)].
function buildQueryFromObjectProperties(Object)
{    
    var Query ='';
    for(var property in Object)
    {              
        if(typeof(Object[property])!='function' && Object[property]!=null)
        {          
            Query+=(property+'='+Object[property]+'&');           
        }
    }    
     return (Query.length!=0 ? Query.substring(0,Query.length-1):Query);
}   

String.prototype.endsWith = function(str)
{
    return (this.match(str+"$")==str)
}

//Available operations for Loews netbooker. 
//singlePropertySearch: Will send the user to the search form.Parameters can be set to pre select form items 
//singlePropertySearchResult:Will send the user to the result page based on the paramters provided
//RetrieveReservationRV:Will open the form to enter credentials and consult reservations
netOperation=
{
    singlePropertySearch:'SinglePropertySearch',
    singlePropertySearchResult:'SinglePropertySearchResult',
    RetrieveReservationRV:'RetrieveReservationRV'
}

//Available modules for Loews netbooker.
//Either do a property search or view an existing reservation
netModule=
{
    PropertySearch:'PropertySearch',
    Reservation:'Reservation'
}

//Query object used in search operations and preset of search form parameters
netSearchQuery=
{        
   adults:null,
   children:null,
   numberOfRooms:null,
   numberOfNights:null,
   specialRate:null,
   rateCode:null,
   arriveDate:null,
   arriveMonth:null,
   arriveYear:null,
   departDate:null,
   departMonth:null,
   departYear:null  
}

//This object is the single point of access to the netbooker API. 
netBookerManager={
    //Unless otherwise specified,all the following properties should be set in page using configuration section in web.config
    //The netBookerUrl for Loews
    //netBookerUrl:'https://loewshotels.ibe.netbooker.com/web/FrontController.nb4?',
    // send request to the gateway instead of netbooker
    netBookerUrl: typeof(BaseAbsolutePath) != 'undefined' ?  BaseAbsolutePath + '/' + Culture + '/Gateway.aspx?' : Culture + '/Gateway.aspx?',
        
    //The Loews specific parameters for each request
    queryParameters:
        {
            chainCode:'LZ',
            propertyCodeType:'RV',            
            instanceId:28,
            locale:'en',
            lookAndFeelId:'78',
            propertyCode:null,
            //Theses properties should be set at runtime based on the operation being executed
            module:netModule.PropertySearch,
            operation:netOperation.singlePropertySearchResult,
            //this property, according to Netbooker API documentation, must always be set to 'yes'
            execute:'yes'
        },
      
    //sets hotel specific query paramters for the netbooker search query. Most hotels share the same
    //default paramaters except Orlando's hotels and Canada's
    InitQueryParameters:function(selectedHotel)
    {        
        // Send the request to the gateway instead of netBooker directly
        //this.netBookerUrl=selectedHotel.getAttribute('netBookerUrl');        
        
        //this.queryParameters.instanceId=selectedHotel.getAttribute('instanceId');     
        //locale is set server side based on the user preferred language. If not available on net booker for the 
        //selected property it falls back to default(en)        
        //this.queryParameters.locale=selectedHotel.getAttribute('locale');
        //this.queryParameters.lookAndFeelId=selectedHotel.getAttribute('lookAndFeelId');
        
        //this.queryParameters.propertyCode=selectedHotel.value;
        this.queryParameters.propertyCode=selectedHotel;
    },
    // Internal methods used by the ViewSearchResult and ViewSearchForm to open a window to netbooker for the user
    SearchForAvailability:function(searchQuery, link)
    {        
        if (link != null)
        {
            //link.target = "_blank";
            link.href = this.netBookerUrl+buildQueryFromObjectProperties(this.queryParameters)+'&'+buildQueryFromObjectProperties(searchQuery);
        }
    },  
    
    
    
    //Used by the reservation object to open a search result window at Netbooker.com to display results based on 
    //gathered parameters.
    ViewSearchResult:function(dateArrival,dateDeparture,nbrOfNights,nbrOfAdults,nbrOfChildren,nbrOfRooms, link)
    {    
       //set operation 
       this.queryParameters.module=netModule.PropertySearch;
       this.queryParameters.operation=netOperation.singlePropertySearchResult;        
       //set search parameters       
       netSearchQuery.adults=nbrOfAdults;
       netSearchQuery.children=nbrOfChildren;
       netSearchQuery.numberOfRooms=nbrOfRooms;              
       netSearchQuery.arriveDate=dateArrival.getDate();
       netSearchQuery.arriveMonth=dateArrival.getMonth()+1;
       netSearchQuery.arriveYear=dateArrival.getFullYear();
       netSearchQuery.departDate=dateDeparture.getDate();
       netSearchQuery.departMonth=dateDeparture.getMonth()+1;
       netSearchQuery.departYear=dateDeparture.getFullYear();
       netSearchQuery.numberOfNights=nbrOfNights;
       this.SearchForAvailability(netSearchQuery, link)
    },

    //Used in special offers page to access the Search form page with special rates and codes pre selected
    ViewSearchForm:function(propertyCode,rateCode,specialRate,link)
    {       
           //SetProperty Search
           this.queryParameters.module=netModule.PropertySearch;
           this.queryParameters.operation=netOperation.singlePropertySearch; 
           //set search parameters           
           this.propertyCode=propertyCode;
           if (specialRate.endsWith(',S'))
           {
               netSearchQuery.specialRate=specialRate;
           }
           else
           {
               netSearchQuery.specialRate=specialRate + ',S';
           }
           netSearchQuery.rateCode=rateCode; 
           //if a property code(hotel selected) was supplied then open the search form from netbooker
           //otherwise open the reservation panel to allow the user to choose an hotel
           if(propertyCode!='')
           {
//                var options=document.getElementById('destination').options;
//                for(var nb=0;nb<options.length;nb++)
//                {
//                    if(options[nb].value==propertyCode)
//                    {                    
                       this.InitQueryParameters(propertyCode);                                              
//                    }
//                }
           }
           else
           {
            //TO DO: Open the panel
			Reservation.BookNow();
            return;
           }
           
            
           this.SearchForAvailability(netSearchQuery, link);
    },
    
    //Used to access the "retrieve your booking screen" at netbooker.com for reservation viewing and online 
    //cancellation
    ViewReservation:function(link)
    {
        this.queryParameters.module=netModule.Reservation;
        this.queryParameters.operation=netOperation.RetrieveReservationRV;        
        var Query=buildQueryFromObjectProperties(this.queryParameters);
        if (link != null)
        {
            //link.target = "_blank";
            link.href = this.netBookerUrl+Query;
        }        
        return true;
    }    
}



