﻿//// Declare Parameters/Variables needed to be set in parent file
//var pickupDays = <%= ETS.Settings.PickupDays %>;
//var dropoffDays = <%= ETS.Settings.DropoffDays %>;
//var onewayDropOffDays = <%= ETS.Settings.OnewayDropOffDays %>;
//var imagesDirectory = "<%= ETS.Settings.ImagesDirectory %>";
//var pickupDateID = <%= txtPickupDate.ClientID %>;
//var dropoffDateID = <%= txtDropoffDate.ClientID %>;
//var culture = "<%= GetCulture() %>";
//var rbDomesticLocationID = <%= rbDomesticLocation.ClientID %>;
//var rbInternationalLocation = <%= rbInternationalLocation.ClientID %>;
//var chkHasPromoCodeID = <%= chkHasPromoCode.ClientID %>;
//var reservationWidgetPromoCodeInputID = <%= ReservationWidgetPromoCodeInput.ClientID %>;
//var reservationWidgetCorporateCodeInputID = <%= ReservationWidgetCorporateCodeInput.ClientID %>;
//var txtCorporateCodeID = <%= txtCorporateCode.ClientID %>;
//var txtPromoCodeID = <%= txtPromoCode.ClientID %>;
//var ddlPickupTimeID = <%= ddlPickupTime.ClientID %>;
//var ddlDropoffTimeID = <%= ddlDropoffTime.ClientID %>;
//var showReservationWidgetID = <%= ShowReservationWidget.ClientID %>;
//var showViewModifyCancelID = <%= ShowViewModifyCancel.ClientID %>;
//var txtLastNameID = <%= txtLastName.ClientID %>;
//var txtConfirmationNumberID = <%= txtConfirmationNumber.ClientID %>;
//var ddlPickupLocationID = <%= ddlPickupLocation.ClientID %>;
//var loadingText = '<%= ResourceController.GetResource("Loading...").BestTranslation %>';
//var bookingVehicleID = <%= BookingVehicleID %>;
//var affiliateID = <%= CommonObjects.CurrentAffiliate.AffiliateId %>;
//var defaultPickupLocationValue = '<%= ResourceController.GetResource("DefaultPickup").BestTranslation %>';
//var ddlDropoffLocationID = <%= ddlDropoffLocation.ClientID %>;
//var cultureName = "<%= GetCultureName() %>";
//var defaultDropoffLocationValue = '<%= ResourceController.GetResource("DefaultDropoff").BestTranslation %>';
//var lnkEditReservation = "lnkEditReservation";
//var guideImage = "GuideImage";
//var bookingGuideImageTop1 = "<%= CommonObjects.CurrentSkinSettings.BookingGuideImageTop(1, Context.Request.UserAgent) %>";
//var bookingGuideImageTop2 = "<%= CommonObjects.CurrentSkinSettings.BookingGuideImageTop(2, Context.Request.UserAgent) %>";
//var bookingGuideImageTop3 = "<%= CommonObjects.CurrentSkinSettings.BookingGuideImageTop(3, Context.Request.UserAgent) %>";
//var bookingGuideImageTop4 = "<%= CommonObjects.CurrentSkinSettings.BookingGuideImageTop(4, Context.Request.UserAgent) %>";
//var bookingGuideImageTop5 = "<%= CommonObjects.CurrentSkinSettings.BookingGuideImageTop(5, Context.Request.UserAgent) %>";
//var bookingGuideImageTop6 = "<%= CommonObjects.CurrentSkinSettings.BookingGuideImageTop(6, Context.Request.UserAgent) %>";
//var bookingGuideImageTop7 = "<%= CommonObjects.CurrentSkinSettings.BookingGuideImageTop(7, Context.Request.UserAgent) %>";
//var imgModifyCancelGuide = "imgModifyCancelGuide";
//var ddlMakeID;
//var ddlCountryID;
//var usingVehicleMake;
//var usingCountryRadioList;
var affiliateDefaultSelectedPickupLocation;

// Initialize Global Vars
var locationType; //international vs. domestic
var datePickerClicked;

function rePostionDiv()
{
    if (document.location.href.toLowerCase().indexOf('reservationwidget.aspx') >= 0 && isIE)
    {
        var dp = document.getElementById('ui-datepicker-div');
        dp.style.margin = 0;
        dp.style.left = (parseInt(datePickerClicked.offsetLeft) + parseInt(datePickerClicked.parentNode.offsetLeft));
        dp.style.top = (parseInt(datePickerClicked.offsetTop) + parseInt(datePickerClicked.parentNode.offsetTop) + 20);
    }
}

// Run this code after all DOM elements have loaded.
$(document).ready(function(){      
    
    // set country type
    if (usingCountryRadioList)
    {
        locationType = 1;
    }
    else
    {
        locationType = 0;
    }
   
    // Initialize the datepicker controls.
    $("#" + pickupDateID).datepicker($.extend({}, $.datepicker.regional[culture],{
        defaultDate: + pickupDays,
        minDate: 0, 
        maxDate: 366,
        showAnim: "slideDown",
        showStatus: true,
        mandatory: true,
        closeAtTop: false,
        showOn: "both", 
        buttonImage: imagesDirectory + "Content/calendar.gif",
        buttonImageOnly: true,
        buttonText: "Select Date" ,                        
        beforeShow: function() {   
            datePickerClicked = this;
            setTimeout(rePostionDiv,50);                    
        }   

    }));            
    $("#" + dropoffDateID).datepicker($.extend({}, $.datepicker.regional[culture],{ 
        defaultDate: + pickupDays + dropoffDays,
        minDate: 0, 
        maxDate: 366,
        showAnim: "slideDown",
        showStatus: true,
        mandatory: true,
        closeAtTop: false,
        showOn: "both", 
        buttonImage: imagesDirectory + "Content/calendar.gif",
        buttonImageOnly: true,
        buttonText: "Select Date",
        beforeShow: function() {   
            datePickerClicked = this;
            setTimeout(rePostionDiv,50);
        }
    }));
    $("#" + rbDomesticLocationID).click(function () { locationType = 1; fillPickupLoc(locationType); fillDropoffLoc(locationType) });
    $("#" + rbInternationalLocationID).click(function () { locationType = 2; fillPickupLoc(locationType); fillDropoffLoc(locationType) });
    cAutocomplete.IE_FIX = isBrowserEarlyIE();
    cAutocomplete.CN_NUMBER_OF_LINES = 8;
    cAutocomplete.CN_HEIGHT_FIX = 15
    cAutocomplete.autoInit();
    // Toggle the promo code box.
    if($("#" + chkHasPromoCodeID)[0] != null && !$("#" + chkHasPromoCodeID)[0].checked)
    {
        $("#" + reservationWidgetPromoCodeInputID).hide(); // Start off hidden
        $("#" + reservationWidgetCorporateCodeInputID).hide();
    }
    $("#" + chkHasPromoCodeID).click(function () { moveGuideImage(6); $("#" + reservationWidgetPromoCodeInputID).toggle();$("#" + reservationWidgetCorporateCodeInputID).toggle(); });
    $("#" + txtCorporateCodeID).change(function(){ moveGuideImage(6); clearPromotionCode(); });
    $("#" + txtCorporateCodeID).focus(function(){ moveGuideImage(6); });
    $("#" + txtPromoCodeID).change(function(){ moveGuideImage(7); clearCorporateCode(); });
    $("#" + txtPromoCodeID).focus(function(){ moveGuideImage(7); });
    // Set up control events
    $("#" + pickupDateID).change(function () { fillPickupHoursList();setDropoffDate();fillDropoffHoursList(); });
    $("#" + pickupDateID).focus(function () { moveGuideImage(2); });
    $("#" + ddlPickupTimeID).focus(function () { moveGuideImage(3); });
    $("#" + dropoffDateID).change(function () { fillDropoffHoursList(); });
    $("#" + dropoffDateID).focus(function () { moveGuideImage(4); });
    $("#" + ddlDropoffTimeID).focus(function () { moveGuideImage(5); });
    $("#" + lnkEditReservation).click(function () {
      $("#" + guideImage).hide();
      $("#" + showReservationWidgetID).slideToggle("fast");
      $("#" + showViewModifyCancelID).slideToggle("fast");
      $("#" + imgModifyCancelGuide).show();
    });
    $("#lnkMakeReservation").click(function () {
      $("#" + imgModifyCancelGuide).hide();
      $("#" + showReservationWidgetID).slideToggle("fast");
      $("#" + showViewModifyCancelID).slideToggle("fast");
      $("#" + guideImage).show();
    });
    $("#" + txtLastNameID).focus(function () { moveModifyCancelGuideImage(1); });
    $("#" + txtConfirmationNumberID).focus(function () { moveModifyCancelGuideImage(2); });
    // Set dropoff location and dates with Javascript for back button compatibility.
    //setDropoffLocation();
    setDatesWithSessionData();
           
    //checkBrowser();
    if (usingVehicleMake)
    {
        initializeMakesList();
    }
    else
    {
        fillPickupLoc(locationType);
    }
});

function initializeMakesList()
{
    fillMakesList();
}



function getInternetExplorerVersion()
{
    var rv = -1;    // assumes failure
    if (navigator.appName == 'Microsoft Internet Explorer')
    {
        var ua = navigator.userAgent;
        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
        {
            rv = parseFloat(RegExp.$1);
        }
    }
    return rv;
}   

function moveGuideImage(toPosition)
{   
    switch(toPosition)
    {
        case 1:
            $("#" + guideImage).animate({top: bookingGuideImageTop1}, 1000);
            break;
        case 2:
            $("#" + guideImage).animate({top: bookingGuideImageTop2}, 1000);
            break;
        case 3:
            $("#" + guideImage).animate({top: bookingGuideImageTop3}, 1000);
            break;
        case 4:
            $("#" + guideImage).animate({top: bookingGuideImageTop4}, 1000);
            break;
        case 5:
            $("#" + guideImage).animate({top: bookingGuideImageTop5}, 1000);
            break;
        case 6:
            if($("#" + chkHasPromoCodeID)[0].checked)
                $("#" + guideImage).animate({top: bookingGuideImageTop6}, 1000);
            else
                $("#" + guideImage).animate({top: bookingGuideImageTop5}, 1000); 
            break;
        case 7:
            $("#" + guideImage).animate({top: bookingGuideImageTop7}, 1000);
            break;
    }
} 
function moveModifyCancelGuideImage(toPosition)
{
    switch(toPosition)
    {
        case 1:
            $("#" + imgModifyCancelGuide).animate({top: "20px"}, 1000);
            break;
        case 2:
            $("#" + imgModifyCancelGuide).animate({top: "70px"}, 1000);
            break;
    }
}
function vehicleMakeChange()
{
    fillPickupLoc(locationType);
}
function pickupLocationChange()
{
    fillPickupHoursList();
    fillDropoffLoc(locationType);
    setDropoffDate();
    fillDropoffHoursList();
    moveGuideImage(2);
}
function getSelectedPickupLocationId()
{
    var value = document.getElementById(ddlPickupLocationID).value;
    if(value == undefined)
    {
        return -1;
    }
    
    return value;
}

function setDropDownListLoadingText(objectId)
{
    $('#' + objectId).find('option').remove();
    $('#' + objectId).append("<option value=\"-1\">" + loadingText + "</option>");
}

function setAffiliateDefaultMake()
{
    ETS.UI.Services.VehicleService.GetAffiliateDefaultManufacturer(affiliateID, setAffiliateDefaultMake_CallBack);
}
function setAffiliateDefaultMake_CallBack(response)
{
    //alert(response);
    $('#' + ddlMakeID).val(response);
    fillPickupLoc(locationType);
}

function fillMakesList()
{
    setDropDownListLoadingText($('#' + ddlMakeID).attr('id'));
    ETS.UI.Services.VehicleService.GetMakeListItems(fillMakesList_CallBack);
}
function fillMakesList_CallBack(response)
{
    var makes = document.getElementById(ddlMakeID);
    fillDropDownList(response, $('#' + ddlMakeID).attr('id'));
    setAffiliateDefaultMake();
}

function pickupCountryChange()
{
    locationType = document.getElementById(ddlCountryID).value * 1;
    
    fillPickupLoc(locationType);
}

function fillPickupLoc(countryGrouping)
{
    affiliateDefaultSelectedPickupLocation = $('#' + ddlPickupLocationID).val();
    setDropDownListLoadingText($('#' + ddlPickupLocationID).attr('id'));
    
    if (usingVehicleMake)
    {
        manufacturerSelectedValue = 0;
        manufacturerSelectedValue = document.getElementById(ddlMakeID).value * 1;        
        
        ETS.UI.Services.LocationService.GetPickupLocationsListItemsFromVehicleManufacturer(countryGrouping, bookingVehicleID, manufacturerSelectedValue, fillPickupLoc_CallBack);
    }
    else
    {
        ETS.UI.Services.LocationService.GetPickupLocationsListItems(countryGrouping, bookingVehicleID, affiliateID, fillPickupLoc_CallBack);
    }
}
function fillPickupLoc_CallBack(response)
{
    var pickupLocations = document.getElementById(ddlPickupLocationID);
    fillLocationDropDownList(response, $('#' + ddlPickupLocationID).attr('id'), defaultPickupLocationValue);
    $('#' + ddlPickupLocationID).val(affiliateDefaultSelectedPickupLocation);
    fillDropoffLoc(locationType);
}

function getSelectedDropoffLocationId()
{
    var value = document.getElementById(ddlDropoffLocationID).value;
    if(value == undefined)
    {
        return -1;
    }
    
    return value;
}

function dropoffLocationChange()
{
    setDropoffDate();
    fillDropoffHoursList();
}        
function resetDropOffList()
{
    fillLocationDropDownList("", $('#' + ddlDropoffLocationID).attr('id'), defaultDropoffLocationValue);
}
function fillDropoffLoc(locationType)
{
    var pickupLocationID = getSelectedPickupLocationId();
    
    if (pickupLocationID > 0)
    {
        setDropDownListLoadingText($('#' + ddlDropoffLocationID).attr('id'));
        ETS.UI.Services.LocationService.GetValidDropoffLocationsListItems(pickupLocationID, $("#" + pickupDateID).val(), locationType, cultureName, fillDropoffLoc_CallBack);
    }
    else
    {
        resetDropOffList();
    }
}
function fillDropoffLoc_CallBack(response)
{
    var dropoffLocations = document.getElementById(ddlDropoffLocationID);
    fillLocationDropDownList(response, $('#' + ddlDropoffLocationID).attr('id'), defaultDropoffLocationValue);
}

function fillDropDownList(response, ddlID)
{
    $('#' + ddlID).find('option').remove();
    var options = "";
    
    $.each(response, function(key, value)
    {
        var optionValue = value.Text;
        var optionKey = value.Value;
        
        options += "<option value=\"" + optionKey + "\">" + optionValue + "</option>";
   })
   $('#' + ddlID).append(options);
}

function fillLocationDropDownList(response, ddlID, defaultValue)
{
    $('#' + ddlID).find('option').remove();
    $('#' + ddlID).find('optgroup').remove();
    var options;
    options = "<optgroup>" + "<option value=\"-1\">" + defaultValue + "</option>";
    
    $.each(response, function(key, value)
    {
        var optionValue = value.Text;
        var optionKey = value.Value;
            
        if (optionKey < 0)
        {
            var optgroupText = "</optgroup><optgroup label=\"" + optionValue + "\">";
            options += optgroupText;
	    }
        else
        {
            options += "<option value=\"" + optionKey + "\">" + optionValue + "</option>";
        }
    })
    options += "<optgroup>";
    $('#' + ddlID).append(options);
}

function fillPickupHoursList()
{
    var pickupLocationID = getSelectedPickupLocationId();
    if(pickupLocationID > 0) 
    {
        setDropDownListLoadingText($('#' + ddlPickupTimeID).attr('id'));
        ETS.UI.Services.LocationService.GetLocationTimes(pickupLocationID,$("#" + pickupDateID).val(), cultureName, fillPickupHoursList_CallBack);
    }
}
function fillPickupHoursList_CallBack(response)
{
    var selectedTime = $("#" + ddlPickupTimeID).val();
    fillListItems(ddlPickupTimeID, response);
    //selectFromList($("#" + ddlPickupTimeID)[0], selectedTime);
}
function fillDropoffHoursList()
{
    var dropoffLocationID = (getSelectedDropoffLocationId() == -1) ?  getSelectedPickupLocationId() : getSelectedDropoffLocationId();
    if(dropoffLocationID > 0)
    {
        setDropDownListLoadingText($('#' + ddlDropoffTimeID).attr('id'));
        ETS.UI.Services.LocationService.GetLocationTimes(dropoffLocationID, $("#" + dropoffDateID).val(), cultureName, fillDropoffHoursList_CallBack);
    }
}
function fillDropoffHoursList_CallBack(response)
{
    var selectedTime = $("#" + ddlDropoffTimeID).val();
    fillListItems(ddlDropoffTimeID, response);
    //selectFromList($("#" + ddlDropoffTimeID)[0], selectedTime);
}

function setDatesWithSessionData()
{
    ETS.UI.Services.ReservationService.GetDatesFromSession(cultureName, setDatesWithSessionData_CallBack);
}
function setDatesWithSessionData_CallBack(response)
{
    $("#" + pickupDateID).val(response[0]);
    $("#" + dropoffDateID).val(response[1]);
}
// Sets the drop off date based on drop-off location and pick-up date.
function setDropoffDate()
{
    var dropoffLocationID = getSelectedDropoffLocationId();
    var daysToAdd = (dropoffLocationID == -1) ? dropoffDays : onewayDropOffDays;
    var pickupDate = new Date($("#" + pickupDateID).val());
	var dropoffDate = new Date($("#" + dropoffDateID).val());
	
	var daysCount = daysInMonth(pickupDate.getMonth(), pickupDate.getFullYear());
	if(Date.parse(dropoffDate.toDateString()) <= Date.parse(pickupDate.toDateString()))
	{
	    if((pickupDate.getDate() + daysToAdd) > daysCount) 
	    {   // Roll over to next month.
            if(pickupDate.getMonth() >= 11) 
            {
                $("#" + dropoffDateID).val(1 + "/" + (pickupDate.getDate() + daysToAdd - daysCount) + "/" + (pickupDate.getFullYear() + 1));
            }
            else 
                $("#" + dropoffDateID).val((pickupDate.getMonth() + 2) + "/" + (pickupDate.getDate() + daysToAdd - daysCount) + "/" + pickupDate.getFullYear());
        }
        else 
        {
            $("#" + dropoffDateID).val(pickupDate.getMonth() + 1 + "/" + (pickupDate.getDate() + daysToAdd) + "/" + pickupDate.getFullYear());
        }
	} 
}

/*
 iMonth is zero based - January is 0
 */
function daysInMonth(iMonth, iYear)
{
    return 32 - new Date(iYear, iMonth, 32).getDate();
}


function clearPromotionCode()
{
    var corporateCode = $("#" + txtCorporateCodeID).val();
    if(corporateCode.length > 0)
    {
        $("#" + txtPromoCodeID).val("");
    }
}
function clearCorporateCode()
{
    var corporateCode = $("#" + txtPromoCodeID).val();
    if(corporateCode.length > 0)
    {
        $("#" + txtCorporateCodeID).val("");
    }
}