﻿
$(document).ready(function()
{

});



// function is called to "control" two datePicker values, from and to.
// if "to" date is earlier than the "from" date, the "to" date is set a week later than the "from" date
function updateFromTo(fromId, toId)
{
    //    var fromElement = document.getElementById(fromId);
    //    var toElement = document.getElementById(toId);

    var fromDate = $('#' + fromId).datepicker("getDate");
    var toDate = $('#' + toId).datepicker("getDate");

    if (toDate <= fromDate)
    {
        toDate.setTime(fromDate.getTime() + 24 * 3600 * 1000);
        $('#' + toId).datepicker("setDate", toDate);
    }
}


// function Contains, returns true if the array contains the element "element", otherwise returns false.
Array.prototype.contains = function(element)
{
    for (var i = 0; i < this.length; i++)
    {
        if (this[i] == element)
        {
            return true;
        }
    }
    return false;
}

// function formates a date to a specified format.
// if format is not specified, MM/dd/yyyy is used.
Date.prototype.formatDate = function(format)
{
    var date = this;
    if (!format)
        format = "MM/dd/yyyy";

    var month = date.getMonth() + 1;
    var year = date.getFullYear();
    format = format.replace("MM", month.toString().padL(2, "0"));
    if (format.indexOf("yyyy") > -1)
        format = format.replace("yyyy", year.toString());
    else if (format.indexOf("yy") > -1)
        format = format.replace("yy", year.toString().substr(2, 2));
    format = format.replace("dd", date.getDate().toString().padL(2, "0"));
    var hours = date.getHours();
    if (format.indexOf("t") > -1)
    {
        if (hours > 11)
            format = format.replace("t", "pm")
        else
            format = format.replace("t", "am")
    }
    if (format.indexOf("HH") > -1)
        format = format.replace("HH", hours.toString().padL(2, "0"));
    if (format.indexOf("hh") > -1)
    {
        if (hours > 12) hours - 12;
        if (hours == 0) hours = 12;
        format = format.replace("hh", hours.toString().padL(2, "0"));
    }
    if (format.indexOf("mm") > -1)
        format = format.replace("mm", date.getMinutes().toString().padL(2, "0"));
    if (format.indexOf("ss") > -1)
        format = format.replace("ss", date.getSeconds().toString().padL(2, "0"));
    return format;
}

// function that creates a string with a length of "count" constitng of "chr" characters
String.repeat = function(chr, count)
{
    var str = "";
    for (var x = 0; x < count; x++) { str += chr };
    return str;
}

// padL - returns a string with a total length of "width" beginning with an appropriate number of "pad" chars. If pad not specified, ' ' is used.
String.prototype.padL = function(width, pad)
{
    if (!width || width < 1)
        return this;

    if (!pad) pad = " ";
    var length = width - this.length
    if (length < 1) return this.substr(0, width);

    return (String.repeat(pad, length) + this).substr(0, width);
}

// padR - returns a string with a total length of "width" ending with an appropriate number of "pad" chars. If pad not specified, ' ' is used.
String.prototype.padR = function(width, pad)
{
    if (!width || width < 1)
        return this;

    if (!pad) pad = " ";
    var length = width - this.length
    if (length < 1) this.substr(0, width);

    return (this + String.repeat(pad, length)).substr(0, width);
}

// helpful functions for trimming!
// ---------------------------------
String.prototype.trim = function()
{
    return this.replace(/^\s+|\s+$/g, "");
}
String.prototype.ltrim = function()
{
    return this.replace(/^\s+/, "");
}
String.prototype.rtrim = function()
{
    return this.replace(/\s+$/, "");
}
//---------------------------------



/// function is called when select period button is clicked on package tour search results
///     packageTourID - ID of a package tour, used to find correct drop down list
function selectPeriodButtonOnClick(packageTourID, currencyID, detailsURL) {
    redirectToPackageTourDetailWithSelectedPeriod(packageTourID, currencyID, detailsURL);
}
/// function is called when period is changed in package our details. Request is made to the server and new accommodation units are shown
function departuresSelectOnChange(packageTourID, bookingAddressDisplayURL, setDirectLink)
{
    getAccommodationUnitsForPackageTour(packageTourID, bookingAddressDisplayURL, setDirectLink);
}

function getAccommodationUnitsForPackageTour(packageTourID, bookingAddressDisplayURL, setDirectLink)
{
    $("div[reservation-loading-panel='1']").show();

    //int numberOfPersons, int packageTourID, int[] objectIDList, long periodStartDateString, int numberOfDays, string currencyID, string languageID)
    var selectedPeriodOption = $("#departuresSelect option:selected");

    var numberOfPersons = 1;
    var objectID = $("#accommodationObjectsSelect").val();
    if (objectID == null)
    {
        objectID = 0;
    }

    var numberOfDays = selectedPeriodOption.attr("numberofdays");
    var periodStartDateString = selectedPeriodOption.attr("startdate");

    var currencyIDSetting = $("#currencyIDSettingPackageTourHidden").val();
    var languageIDSetting = $("#languageIDSettingPackageTourHidden").val();
    var galleryPath = $("#galleryPathHidden").val();

    $.post(
        "/XSLTControls/Proxy/ProxyWebService.asmx/GetAccommodationUnitsForPackageTour",
        { 'numberOfPersons': numberOfPersons, 'packageTourID': packageTourID, 'objectIDList': objectID, 'periodStartDateString': periodStartDateString, 'numberOfDays': numberOfDays, 'currencyID': currencyIDSetting, 'languageID': languageIDSetting, 'bookingAddressDisplayURL': bookingAddressDisplayURL, 'galleryPath': galleryPath, 'setDirectLink': setDirectLink },
        function(data) {
            $("div.packaget-tour-accommodation-holder").html($(data).text());
            $("div[reservation-loading-panel='1']").hide();
            $(function() {
                $($.fn.nyroModal.settings.openSelector).nyroModal();
            });
        }
    );
}

// function is called on accommodationObjecSelect element onchange event
// it filters out the departuresSelect element and calls the web service with the new info!
function accommodationObjectOnChange(packageTourID, bookingAddressDisplayURL, setDirectLink)
{
    getAccommodationUnitsForPackageTour(packageTourID, bookingAddressDisplayURL, setDirectLink);
}


function packageTourObjectPriceListChanged(packageTourID) {
    $("div[price-list-loading-panel='1']").show();

    var objectID = $("#accommodationObjectsPriceListSelect").val();
    if (objectID == null) {
        objectID = 0;
    }
    
    var currencyIDSetting = $("#currencyIDSettingPackageTourHidden").val();
    var languageIDSetting = $("#languageIDSettingPackageTourHidden").val();

    $.post(
        "/XSLTControls/Proxy/ProxyWebService.asmx/GetPriceListForPackageTour",
        { 'packageTourID': packageTourID, 'objectIDList': objectID, 'currencyID': currencyIDSetting, 'languageID': languageIDSetting },
        function(data) {
            $("div.package-tour-price-list-holder").html($(data).text());
            $("div[price-list-loading-panel='1']").hide();
        }
    );
}
