// Version 1.0
// bmtrvlib - Travel Library Services

// CLASS: TileSearch
// - Purpose: Request a search for Suppliers for a given trip details
// - Needs: The details of a Trip
// - Returns: The list of supplier tiles
function bmtrvlib_TileSearch() {
    // Basic Properties   
    this.process_id = 10;
    this.product_id = "";
    this.search_type = "";

	// air properties	
	this.trip_type = "";
	this.from_location1 = "";
	this.to_location1 = "";
	this.departure_date1 = "";	
	this.return_date1 = "";
	this.from_location2 = "";
	this.to_location2 = "";
	this.departure_date2 = "";				
	this.from_location3 = "";
	this.to_location3 = "";
	this.departure_date3 = "";
	this.no_adults = "";
	this.no_children = "";
	this.no_rooms = "";

	// hotel properties
    this.destination_city = "";
	this.checkin_date = "";
	this.checkout_date = "";
	this.hotel_name = "";
	this.no_adults = "";
	this.no_children = "";
	this.no_rooms = "";

	//car properties
	this.from_location = "";
	this.pickup_date = "";
	this.pickup_time = "";
	this.dropoff_date = "";
	this.dropoff_time = "";

	//cruise properties
	this.to_location = "";
	this.departure_date = "";
	this.number_day = "";
	this.age55 = "";

    
    this.response = "";    
	this.onSearchCompleted = function(){}; 

                     
    // initialize the member function references 
    // for the class prototype
    if (typeof (_bmtrvlib_TileSearch_prototype_called) == 'undefined') {
        _bmtrvlib_TileSearch_prototype_called = true;
        bmtrvlib_TileSearch.prototype.requestUrl = buildRequestUrl;
        bmtrvlib_TileSearch.prototype.handleResponse = handleResponse;
        bmtrvlib_TileSearch.prototype.callService = callService;
    }

    // Prepare the partial URL by concatenating required properties together
    function buildRequestUrl() {
        var url = "";
		 url = url + "process_id=" + this.process_id;
		 url = url + "&product_id=" + this.product_id;
		 url = url + "&search_type=" + this.search_type;
		if ((this.search_type == "air") || (this.search_type == "air_car")  || (this.search_type == "air_hotel")  || (this.search_type == "air_hotel_car"))
		{
			url = url + "&trip_type=" + this.trip_type;
			url = url + "&from_location1=" + this.from_location1;
			url = url + "&to_location1=" + this.to_location1;
			url = url + "&departure_date1=" + this.departure_date1;
			url = url + "&return_date1=" + this.return_date1;
			url = url + "&from_location2=" + this.from_location2;
			url = url + "&to_location2=" + this.to_location2;
			url = url + "&departure_date2=" + this.departure_date2;
			url = url + "&from_location3=" + this.from_location3;
			url = url + "&to_location3=" + this.to_location3;
			url = url + "&departure_date3=" + this.departure_date3;
			url = url + "&no_adults=" + this.no_adults;
			url = url + "&no_children=" + this.no_children;
			url = url + "&no_rooms=" + this.no_rooms;
		}
		else if  ((this.search_type == "hotel") || (this.search_type == "hotel_car"))
		{					
			url = url + "&destination_city=" + this.destination_city;
			url = url + "&checkin_date=" + this.checkin_date;
			url = url + "&checkout_date=" + this.checkout_date;
			url = url + "&hotel_name=" + this.hotel_name;
			url = url + "&no_adults=" + this.no_adults;
			url = url + "&no_children=" + this.no_children;
			url = url + "&no_rooms=" + this.no_rooms;
		}
		else if (this.search_type == "car")
		{
			url = url + "&from_location=" + this.from_location;
			url = url + "&pickup_date=" + this.pickup_date;
			url = url + "&pickup_time=" + this.pickup_time;
			url = url + "&dropoff_date=" + this.dropoff_date;
			url = url + "&dropoff_time=" + this.dropoff_time;
		}
		else if (this.search_type == "cruise")
		{
			url = url + "&to_location=" + this.to_location;
			url = url + "&departure_date=" + this.departure_date;
			url = url + "&number_day=" + this.number_day;
			url = url + "&age55=" + this.age55;
		}       
		return url;
    }

    // Handle Response
    function handleResponse(response) {		
        this.response = response;
    }

    // Execute the service
    function callService(Async) {
		var srvctrl = new bmtrvlib_ServiceController();
		if (Async)
		{			
	        srvctrl.callServiceAsync(this.requestUrl(), this);
		}
		else 
		{		
			srvctrl.callService(this.requestUrl(), this);
		}
    }
   
}

// CLASS: TileClick
// - Purpose: Request a search for Suppliers URL
// - Needs: The details of Supplier
// - Returns: The DeeplinkOfSupplier
function bmtrvlib_TileClick() {
    // Basic Properties       
	this.process_id = 20;
	this.product_id = "";
	this.supplier_id = "";
	this.click_id = "";
    
    this.response = "";    
	this.onSearchCompleted = function(){}; 

                     
    // initialize the member function references 
    // for the class prototype
    if (typeof (_bmtrvlib_TileClick_prototype_called) == 'undefined') {
        _bmtrvlib_TileClick_prototype_called = true;
        bmtrvlib_TileClick.prototype.requestUrl = buildRequestUrl;
        bmtrvlib_TileClick.prototype.handleResponse = handleResponse;
        bmtrvlib_TileClick.prototype.callService = callService;
    }

    // Prepare the partial URL by concatenating required properties together
    function buildRequestUrl() {
        var url = "";
		url = url + "process_id=" + this.process_id;
		url = url + "&product_id=" + this.product_id;
		url = url + "&spid=" + this.supplier_id;
		url = url + "&cid=" + this.click_id;
		return url;
    }

    // Handle Response
    function handleResponse(response) {		
        this.response = response;
    }

    // Execute the service
    function callService(Async) {
		var srvctrl = new bmtrvlib_ServiceController();
		if (Async)
		{			
	        srvctrl.callServiceAsync(this.requestUrl(), this);
		}
		else 
		{		
			srvctrl.callService(this.requestUrl(), this);
		}
    }   
}

// CLASS: DealClick
// - Purpose: Request a search for Suppliers URL
// - Needs: The details of Supplier
// - Returns: The DeeplinkOfSupplier
function bmtrvlib_DealClick() {
    // Basic Properties       
	this.process_id = 50;
	this.offer_id = "";
    
    this.response = "";    
	this.onSearchCompleted = function(){}; 

                     
    // initialize the member function references 
    // for the class prototype
    if (typeof (_bmtrvlib_DealClick_prototype_called) == 'undefined') {
        _bmtrvlib_DealClick_prototype_called = true;
        bmtrvlib_DealClick.prototype.requestUrl = buildRequestUrl;
        bmtrvlib_DealClick.prototype.handleResponse = handleResponse;
        bmtrvlib_DealClick.prototype.callService = callService;
    }

    // Prepare the partial URL by concatenating required properties together
    function buildRequestUrl() {
        var url = "";
		url = url + "process_id=" + this.process_id;
		url = url + "&offer_id=" + this.offer_id;
		return url;
    }

    // Handle Response
    function handleResponse(response) {		
        this.response = response;
    }

    // Execute the service
    function callService(Async) {
		var srvctrl = new bmtrvlib_ServiceController();
		if (Async)
		{			
	        srvctrl.callServiceAsync(this.requestUrl(), this);
		}
		else 
		{		
			srvctrl.callService(this.requestUrl(), this);
		}
    }   
}


// CLASS: CityTypeAhead 
// - Purpose: Provides all the functionality to support City/Airports Type-Ahead on forms
// - Needs: A partial search string entered by the user 
// - Returns: The list of cities or airports matching the given user partial search string
function bmtrvlib_CityTypeAhead(searchString) {
    // Basic Properties         
    this.response = "";                     // Contains the service response after it has been called
    this.process_id = 230;
	this.userinputstring = "";				 // The partial string value entered by the user
	this.businessobject = ""; 
	this.mode = ""; 
	this.usepaging = "N"; 
	this.pagenumber = "-1"; 
	this.itemsperpage = "-1"; 

    // initialize the member function references 
    // for the class prototype
    if (typeof (_bmtrvlib_CityTypeAhead_prototype_called) == 'undefined') {
        _bmtrvlib_CityTypeAhead_prototype_called = true;
        bmtrvlib_CityTypeAhead.prototype.requestUrl = buildRequestUrl;
        bmtrvlib_CityTypeAhead.prototype.handleResponse = handleResponse;
        bmtrvlib_CityTypeAhead.prototype.callService = callService;
    }
    
    // Prepare the partial URL by concatenating required properties together
    function buildRequestUrl() {
         var url = "";
		url = url + "process_id=" + this.process_id;
		url = url + "&q=" + this.userinputstring;
		url = url + "&businessobject=" + this.businessobject;
		url = url + "&mode=" + this.mode;
		url = url + "&usepaging=" + this.usepaging;
		url = url + "&pagenumber=" + this.pagenumber;
		url = url + "&itemsperpage=" + this.itemsperpage;
		return url;
    }

    // Handle Response
    function handleResponse(response) {	

        this.response = response;
    }

    // Execute the service
	function callService(Async) {
		var srvctrl = new bmtrvlib_ServiceController();
		if (Async)
		{			
	        srvctrl.callServiceAsync(this.requestUrl(), this);
		}
		else 
		{		
			srvctrl.callService(this.requestUrl(), this);
		}
    }
    /*function callService() {
        var srvctrl = new bmtrvlib_ServiceController();
        srvctrl.callService(this.requestUrl(), this);

    }*/
}

// CLASS: TileClick
// - Purpose: Request a search for Suppliers URL
// - Needs: The details of Supplier
// - Returns: The DeeplinkOfSupplier
function bmtrvlib_DealSearch() {
    // Basic Properties       
	this.process_id = 304;
	this.deal_search = "";
	this.category_number = "";
	this.total_record = "";
	this.deal_cat_no = "";
    
    this.response = "";    
	this.onSearchCompleted = function(){}; 

                     
    // initialize the member function references 
    // for the class prototype
    if (typeof (_bmtrvlib_DealSearch_prototype_called) == 'undefined') {
        _bmtrvlib_DealSearch_prototype_called = true;
        bmtrvlib_DealSearch.prototype.requestUrl = buildRequestUrl;
        bmtrvlib_DealSearch.prototype.handleResponse = handleResponse;
        bmtrvlib_DealSearch.prototype.callService = callService;
    }

    // Prepare the partial URL by concatenating required properties together
    function buildRequestUrl() {
        var url = "";
		url = url + "process_id=" + this.process_id;
		url = url + "&deal_search=" + this.deal_search;
		url = url + "&category_number=" + this.category_number;
		url = url + "&total_record=" + this.total_record;
		url = url + "&deal_cat_no=" + this.deal_cat_no;
		return url;
    }

    // Handle Response
    function handleResponse(response) {	
        this.response = response;
    }

    // Execute the service
    function callService(Async) {
		var srvctrl = new bmtrvlib_ServiceController();
		if (Async)
		{			
	        srvctrl.callServiceAsync(this.requestUrl(), this);
		}
		else 
		{		
			srvctrl.callService(this.requestUrl(), this);
		}
    }
   
}



// CLASS: Service Controller
// - Purpose: Handles the calls to the service proxy using AJAX
function bmtrvlib_ServiceController() {
    // Properties
    this.responseFormat = "xml";         // Property containing the
    this.response = "";                  // Contains the response string from service
    this.queryUrl = "";                  // The partial Query URL from one of the service CLASS
    this.callback;                       // Hook to the callback function
    //this.xmlhttp;


    // initialize the member function references 
    // for the class prototype
    if (typeof (_bmtrvlib_ServiceController_prototype_called) == 'undefined') {
        _bmtrvlib_ServiceController_prototype_called = true;
        bmtrvlib_ServiceController.prototype.callService = callService;
		bmtrvlib_ServiceController.prototype.callServiceAsync = callServiceAsync;
    }     
     
    // Method: CallService:
    function callServiceAsync(requestUrl, callBack)
    {
        this.queryUrl = requestUrl;
        this.callback = callBack;
        var xmlhttp = GetXmlHttpObject();     // @@@@@@@@@@@@  ENHANCE THIS FUNCTION!!!! THIS IS NOT YET CROSS-BROWSER COMPATIBLE!!!!
        if (xmlhttp == null)
        {
            alert ("Browser does not support HTTP Request");
            return;
        }
         
        var proxyUrl = "/bmtrvlib_proxy.php?" + this.queryUrl;     // Prepare the URL with partial query string
	
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState==4)
			{				

				 callBack.onSearchCompleted(xmlhttp.responseText);        
			}
		}
		
        xmlhttp.open("GET", proxyUrl, true);               // Prepare the Proxy call      @@@@@@@@@@@@@@@@ Should be synchronous here!!!
        xmlhttp.send(null);                                 // Execute the service
    }

	 // Method: CallService:
    function callService(requestUrl, callBack)
    {
        this.queryUrl = requestUrl;
        this.callback = callBack;
        this.xmlhttp = GetXmlHttpObject();     // @@@@@@@@@@@@  ENHANCE THIS FUNCTION!!!! THIS IS NOT YET CROSS-BROWSER COMPATIBLE!!!!

        if (this.xmlhttp == null)
        {
            alert ("Browser does not support HTTP Request");
            return;
        }
         
        var proxyUrl = "/bmtrvlib_proxy.php?" + this.queryUrl;    // Prepare the URL with partial query string	
        this.xmlhttp.open("GET", proxyUrl, false);               // Prepare the Proxy call      @@@@@@@@@@@@@@@@ Should be synchronous here!!!
        this.xmlhttp.send(null);                                 // Execute the service		
		callBack.handleResponse(this.xmlhttp.responseText); 

    }

	function GetXmlHttpObject()
	{
		if (window.XMLHttpRequest)
		{
			// code for IE7+, Firefox, Chrome, Opera, Safari
			return new XMLHttpRequest();
		}
		if (window.ActiveXObject)
		{
			// code for IE6, IE5
			return new ActiveXObject("Microsoft.XMLHTTP");
		}
		return null;
	} 
}



