﻿/// <reference path="jquery.js" />
/// <reference path="jquery-vsdoc.js" />

var isPageLoad = true;
var fileName;
var ratesViewDisplay = false;
var monthsLong = ["يناير", "فبراير", "مارس", "ابريل", "مايو", "يونيو", "يولو", "اغسطس", "سبتمبر", "اكتوبر", "نوفمبر", "ديسمبر"];
var weekdaysShort = ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"];
var calTitle = "Choose a date:";
var myLabelYearPosition = 2;
var myLabelMonthPosition = 1;
var myLabelYearSuffix = "";
var myLabelMonthSuffix = " ";
var currentTabId = "Details";
var mapAlreadyShown = false;

//reload page with the new querystring name-value pair


function isFieldEmpty(fieldOneId, fieldTwoId) {
    if (document.getElementById(fieldOneId) != null && document.getElementById(fieldTwoId) != null) {
        if (document.getElementById(fieldOneId).value == 0 || document.getElementById(fieldTwoId).value == 0) {
            alert(typeof (JavaScriptEnterCheckinCheckout) == 'undefined' ? 'Please enter your checkin and checkout date.' : JavaScriptEnterCheckinCheckout)
            return true
        }
    }
    return false;
}

function getDate(dateString) {
    var year = dateString.substr(0, 4)
    var month = dateString.substr(5, 2) - 1 // 0 - 11
    var day = dateString.substr(8, 2)
    return new Date(year, month, day)
}

// validate dates
function ValidateDates() {
    var inDate = getDate(document.getElementById("checkinValue").value)
    var outDate = getDate(document.getElementById("checkoutValue").value)
    var currentDate = new Date();

    //validate checkin - checkout difference (date range too big)
    if ((outDate - inDate) / 86400000 >= 31) {  //86400000 is one days in milliseconds
        alert(typeof (JavaScriptPeriodOfStay) == 'undefined' ? 'Your period of stay should be no longer than 30 nights.' : JavaScriptPeriodOfStay)
        return false
    }

    // validate checkout <= checkin
    if (outDate - inDate <= 0) {
        alert(typeof (JavaScriptEnsureCheckoutAfterCheckin) == 'undefined' ? 'Please ensure that the check-out date is after the check-in date.' : JavaScriptEnsureCheckoutAfterCheckin)
        return false
    }

    //validate checkin/checkout is less than one year in advance
  if ((outDate - currentDate) / 86400000 >= 363) {
      alert(typeof (JavaScriptBookWithinOneYear) == 'undefined' ? 'You cannot book more than 1 year in advance.' : JavaScriptBookWithinOneYear)
     return false;
 }
    return true
}


function getCookie(cookieName) {
    if (document.cookie != null && document.cookie.length > 0) {
        var start = document.cookie.indexOf(cookieName + "=");
        if (start != -1) {
            start = start + cookieName.length + 1;
            var end = document.cookie.indexOf(";", start);
            if (end == -1) {
                end = document.cookie.length;
            }
            return unescape(document.cookie.substring(start, end));
        }
    }
    return ""
}


function DoSearch() {

    var redirection;
    var affiliateParam;
    var brandParam;
    var host;
    var isAutoComplete = false;

    if (document.getElementById('hotelCheckin') != null) {
        if (document.getElementById('hotelCheckin').value == 0 || document.getElementById('hotelCheckout').value == 0) {
            alert(typeof (JavaScriptEnterCheckinCheckout) == 'undefined' ? 'Please enter your checkin and checkout date.' : JavaScriptEnterCheckinCheckout)
            return false
        }
    }

    if (!ValidateDates())
        return false


    return false;
}

function DoCitySearchBoxSearch(languageCode, target, affiliateId, fileName, domain, brandId, applyFilters) {
    var redirection;
    var affiliateParam;
    var brandParam;
    var host;

    if (isFieldEmpty('hotelCheckin', 'hotelCheckout')) {
        return false;
    }
    else {
        if (!ValidateDates())
            return false




        return false;
    }
}


//Yahoo Calendar
function setupCal(checkin, checkout, checkinId, checkoutId, checkinCalId, checkoutCalId, numOfCal) {
    var theMindate = new Date();
    var min_date = theMindate.getDate();
    var min_month = theMindate.getMonth();
    min_month++;
    var min_year = theMindate.getFullYear();
    	theMindate = min_month + "/" + min_date + "/" + (min_year - 1);
    var theMaxdate = min_month + "/" + min_date + "/" + min_year;

    //make yesterday selectable for checkin calendar(due to time zone differnce)
//    var earlyMindate = new Date();
 //   earlyMindate.setDate(min_date - 1);
 //   var early_month = earlyMindate.getMonth();
 //   early_month++;
 //   earlyMindate = early_month + "/" + earlyMindate.getDate() + "/" + earlyMindate.getFullYear();

    if (checkin == null || checkout == null) {
        checkin = dateFormat(min_month) + "/" + dateFormat(min_date) + "/" + min_year;
        checkout = checkin;
    }

    //get calendar selected date
    var checkinSelection = document.getElementById(checkinId).value == 0 ? checkout : reFormatDate(document.getElementById(checkinId).value);
    var checkoutSelection = document.getElementById(checkoutId).value == 0 ? checkout : reFormatDate(document.getElementById(checkoutId).value);

    //get calendar seleted month
    var checkinCalPage = checkin.substr(0, 2) + checkin.substr(5, 5);
    var checkoutCalPage = checkout.substr(0, 2) + checkout.substr(5, 5);

    // setup checkin calendar
    var checkinCal = new YAHOO.widget.CalendarGroup("checkinCal", checkinCalId,
                                                { pages: numOfCal,
                                                    mindate: theMindate,
                                                    maxdate: theMaxdate,
                                                    navigator: true
                                                });
    checkinCal.cfg.setProperty("pagedate", checkinCalPage, false);
    checkinCal.cfg.setProperty("selected", checkinSelection + ", " + checkoutSelection, false);

    //add international features
    checkinCal.cfg.setProperty("MONTHS_LONG", monthsLong);
    checkinCal.cfg.setProperty("WEEKDAYS_SHORT", weekdaysShort);
    checkinCal.cfg.setProperty("MY_LABEL_YEAR_POSITION", myLabelYearPosition);
    checkinCal.cfg.setProperty("MY_LABEL_MONTH_POSITION", myLabelMonthPosition);
    checkinCal.cfg.setProperty("MY_LABEL_YEAR_SUFFIX", myLabelYearSuffix);
    checkinCal.cfg.setProperty("MY_LABEL_MONTH_SUFFIX", myLabelMonthSuffix);
    checkinCal.render();

    //set the input field as the calendar trigger        
    var checkinShow = document.getElementById(checkinId);
    YAHOO.util.Event.addListener(checkinShow, "click", checkinCal.show, checkinCal, true);

    //handle calendar select event
    checkinCal.selectEvent.subscribe(checkinHandleSelect, checkinCal, true);
    checkinCal.hide();

    function checkinHandleSelect(type, args, obj) {
        var dates = args[0];
        var date = dates[0];
        var year = date[0], month = dateFormat(date[1]), day = dateFormat(date[2]);
        var checkinDate = year + "-" + month + "-" + day;
        checkinSelection = month + "/" + day + "/" + year;
        document.getElementById(checkinId).value = checkinDate;
        document.getElementById("checkinValue").value = checkinDate;
        document.getElementById("checkoutValue").value = document.getElementById(checkoutId).value;
        checkinCal.hide();

        //update checkout calendar start month according to checkin date
        checkinCalPage = date[1] + "/" + year;
        checkoutCal.cfg.setProperty("pagedate", checkinCalPage, false);

        //update checkout calendar highlight dates
        if (document.getElementById(checkoutId).value != 0) {
            checkoutSelection = reFormatDate(document.getElementById(checkoutId).value);
        }
        checkinCal.cfg.setProperty("selected", checkinSelection + ", " + checkoutSelection, false);
        checkoutCal.cfg.setProperty("selected", checkinSelection + ", " + checkoutSelection, false);
        checkinCal.render();
        checkoutCal.render();
    }

    // setup checkout calendar    
    var checkoutCal = new YAHOO.widget.CalendarGroup("checkoutCal", checkoutCalId,
                                                { pages: numOfCal,
                                                    mindate: theMindate,
                                                    maxdate: theMaxdate,
                                                    navigator: true
                                                });
    checkoutCal.cfg.setProperty("pagedate", checkoutCalPage, false);
    checkoutCal.cfg.setProperty("selected", checkinSelection + ", " + checkoutSelection, false);

    //add international features 
    checkoutCal.cfg.setProperty("MONTHS_LONG", monthsLong);
    checkoutCal.cfg.setProperty("WEEKDAYS_SHORT", weekdaysShort);
    checkoutCal.cfg.setProperty("MY_LABEL_YEAR_POSITION", myLabelYearPosition);
    checkoutCal.cfg.setProperty("MY_LABEL_MONTH_POSITION", myLabelMonthPosition);
    checkoutCal.cfg.setProperty("MY_LABEL_YEAR_SUFFIX", myLabelYearSuffix);
    checkoutCal.cfg.setProperty("MY_LABEL_MONTH_SUFFIX", myLabelMonthSuffix);
    checkoutCal.render();

    //set the input field as the calendar trigger
    var checkoutShow = document.getElementById(checkoutId);
    YAHOO.util.Event.addListener(checkoutShow, "click", checkoutCal.show, checkoutCal, true);

    //handle calendar select event
    checkoutCal.selectEvent.subscribe(checkoutHandleSelect, checkoutCal, true);
    checkoutCal.hide();

    function checkoutHandleSelect(type, args, obj) {
        var dates = args[0];
        var date = dates[0];
        var year = date[0], month = dateFormat(date[1]), day = dateFormat(date[2]);
        var checkoutDate = year + "-" + month + "-" + day;
        checkoutSelection = month + "/" + day + "/" + year;
        document.getElementById(checkoutId).value = checkoutDate;
        document.getElementById("checkinValue").value = document.getElementById(checkinId).value;
        document.getElementById("checkoutValue").value = checkoutDate;

        //update checkout calendar highlight dates
        if (document.getElementById(checkinId).value != 0) {
            checkinSelection = reFormatDate(document.getElementById(checkinId).value);
        }
        checkinCal.cfg.setProperty("selected", checkinSelection + ", " + checkoutSelection, false);
        checkoutCal.cfg.setProperty("selected", checkinSelection + ", " + checkoutSelection, false);
        checkinCal.render();
        checkoutCal.render();

        checkoutCal.hide();
    }

    //hide calendar when user click anywhere outside the calendar
    function hideDiv(e) {
        var target = (e ? e.target : event.srcElement);
        var checkinCalDiv = document.getElementById(checkinCalId);
        var checkinDiv = document.getElementById(checkinId);
        var checkoutCalDiv = document.getElementById(checkoutCalId);
        var checkoutDiv = document.getElementById(checkoutId);
        if (checkinCalDiv != null && checkinDiv != null && checkoutCalDiv != null && checkoutDiv != null) {
            (isChild(target, checkinCalDiv) || target == checkinDiv) ? null : checkinCalDiv.style.display = 'none';
            (isChild(target, checkoutCalDiv) || target == checkoutDiv) ? null : checkoutCalDiv.style.display = 'none';
        }

        //if it is hotel page, set additional visibility control for the calendars on rate tab
        if (document.getElementById("detailTabCheckin") != null || document.getElementById("rateTabCheckin") != null) {
            hideHotelDiv(e)
            hideRateDiv(e)
        }

        //if it is city page, set additional visibility control for both calendars
        if (document.getElementById("popupCheckin") != null) {
            hideHotelDiv(e)
            //hidePopupDiv(e)
        }
    }
    document.onclick = hideDiv
}

// visibility control for hotel.aspx page rate tab first calendar and city.aspx first calendar
function hideHotelDiv(e) {
    var target = (e ? e.target : event.srcElement);
    var checkinCalDiv = document.getElementById("checkinCalContainer");
    var checkinDiv = document.getElementById("hotelCheckin");
    var checkoutCalDiv = document.getElementById("checkoutCalContainer");
    var checkoutDiv = document.getElementById("hotelCheckout");
    if (checkinCalDiv != null && checkinDiv != null && checkoutCalDiv != null && checkoutDiv != null) {
        (isChild(target, checkinCalDiv) || target == checkinDiv) ? null : checkinCalDiv.style.display = 'none';
        (isChild(target, checkoutCalDiv) || target == checkoutDiv) ? null : checkoutCalDiv.style.display = 'none';
    }
}
// visibility control for hotel.aspx page rate tab second calendar.
function hideRateDiv(e) {
    var target = (e ? e.target : event.srcElement);
    var checkinCalDiv = document.getElementById("rateTabCheckinCalContainer");
    var checkinDiv = document.getElementById("rateTabCheckin");
    var checkoutCalDiv = document.getElementById("rateTabCheckoutCalContainer");
    var checkoutDiv = document.getElementById("rateTabCheckout");
    if (checkinCalDiv != null && checkinDiv != null && checkoutCalDiv != null && checkoutDiv != null) {
        (isChild(target, checkinCalDiv) || target == checkinDiv) ? null : checkinCalDiv.style.display = 'none';
        (isChild(target, checkoutCalDiv) || target == checkoutDiv) ? null : checkoutCalDiv.style.display = 'none';
    }
}
// visibility control for city.aspx page popup calendar.
function hidePopupDiv(e) {
    var target = (e ? e.target : event.srcElement);
    var checkinCalDiv = document.getElementById("popupCheckinCalContainer");
    var checkinDiv = document.getElementById("popupCheckin");
    var checkoutCalDiv = document.getElementById("popupCheckoutCalContainer");
    var checkoutDiv = document.getElementById("popupCheckout");
    if (checkinCalDiv != null && checkinDiv != null && checkoutCalDiv != null && checkoutDiv != null) {
        (isChild(target, checkinCalDiv) || target == checkinDiv) ? null : checkinCalDiv.style.display = 'none';
        (isChild(target, checkoutCalDiv) || target == checkoutDiv) ? null : checkoutCalDiv.style.display = 'none';
    }
}

//change date format from yyyy-mm-dd to mm/dd/yyyy
function reFormatDate(date) {
    var year = date.substr(0, 4);
    var month = date.substr(5, 2);
    var day = date.substr(8, 2);
    return month + "/" + day + "/" + year;
}

function dateFormat(date) {
    return date.toString().length == 1 ? "0" + date : date;
}

//check if child is a childNode of parent
function isChild(child, parent) {
    while (child) {
        if (child == parent)
            return true;
        child = child.parentNode;
    }
    return false;
}
/***************** End of yahoo calendar ************************/


