﻿





function slideDate()

	{

		var enteredCheckin = document.getElementById('hotelCheckin').value;

		if(isDate(enteredCheckin))

		{

			enableDateFrom = new Date(enteredCheckin.replace(/-/g, "/"));

			if (enableDateFrom.getMonth == 11)

			{

				enableDateTo = new Date(enableDateFrom.getFullYear() + 2, 0, enableDateFrom.getDate());

			}

			else

			{

				enableDateTo = new Date(enableDateFrom.getFullYear()+1, enableDateFrom.getMonth() + 1, enableDateFrom.getDate());

			}

		}

	}









function isDate(dateStr) {



	//var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;

	var datePat = /^(\d{4})(\-)(\d{1,2})(\-)(\d{1,2})$/;

	var matchArray = dateStr.match(datePat); // is the format ok?



	if (matchArray == null) {

		//alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");

		return false;

	}

	year = matchArray[1];

	month = matchArray[3]; // p@rse date into variables

	day = matchArray[5];

	

	if (month < 1 || month > 12) { // check month range

		//alert("Month must be between 1 and 12.");

		return false;

	}



	if (day < 1 || day > 31) {

		//alert("Day must be between 1 and 31.");

		return false;

	}



	if ((month==4 || month==6 || month==9 || month==11) && day==31) {

		//alert("Month "+month+" doesn`t have 31 days!")

		return false;

	}



	if (month == 2) { // check for february 29th

		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));

		if (day > 29 || (day==29 && !isleap)) {

			//alert("February " + year + " doesn`t have " + day + " days!");

			return false;

		}

	}

	return true; // date is valid

}





//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;    

    var theMaxdate = min_month + "/" + min_date + "/" + (min_year + 1);     

    

    //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:earlyMindate, 

                                                  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)

        }

    }

    document.onclick=hideDiv

}



//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;

}



// visibility control for hotel.aspx page rate tab 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';

    }

}



function DoSearch(languageCode, target, affiliateId, city, domain, brandId, customData) {



    var redirection;

    var affiliateParam;

    var brandParam;

    var host;

    if (document.getElementById('hotelCheckin') != null)

    {

        if(document.getElementById('hotelCheckin').value == 0 || document.getElementById('hotelCheckout').value == 0 || document.getElementById('hotelCheckin').value == 'Check-in' || document.getElementById('hotelCheckout').value == 'Check-out' || document.getElementById('hotelCheckin').value == 'Check in' || document.getElementById('hotelCheckout').value == 'Check out')

        {

		    alert(typeof(JavaScriptEnterCheckinCheckout) == 'undefined' ? 'Please enter your checkin and checkout date.' : JavaScriptEnterCheckinCheckout)

            return false

        }

        else

        {

			document.getElementById("checkinValue").value = document.getElementById("hotelCheckin").value;

			document.getElementById("checkoutValue").value = document.getElementById("hotelCheckout").value;

        }

    }

    

    if (!ValidateDates())

        return false

        

    city = escape (city)

    if (typeof(brandId) == 'undefined' || brandId == "0" || brandId == "")

    {

		brandParam = '';

    }

    else

    {

		brandParam = '&brandId=' + brandId

    }

    if (affiliateId == null || affiliateId == '')

    {

        affiliateParam = '';

        host = '';

    }

    else

    {

        affiliateParam = '&a_aid=' + affiliateId;

    }

	// backwards compatability for existing affiliate scripts

	if (typeof(domain) == 'undefined' || domain == '')

	{

		host = 'http://www.hotelscombined.com'	

	}

	else

	{

		host = 'http://' + domain;

	}

    

    var defaultSort = GetQSVal("sort");

    if (defaultSort.length > 0)

    {

		defaultSort = "&sort=" + defaultSort;

    }



    //if come from home page or come from city/searchResult pages with a new city name entered in the 'City' textbox

    //if ((document.getElementById("citySearchRadio") != null && document.getElementById("citySearchRadio").checked) || document.getElementById("M_C_SearchResultCity") != null) {

    if (document.getElementById("selectedFileName").value == null || document.getElementById("selectedFileName").value == ""){

     

        if (city == null || city.length < 3) {

            alert(typeof(JavaScriptEnterCityName) == 'undefined' ? 'Please enter a city name that is at least 3 characters in length' : JavaScriptEnterCityName);

            return false;

        }

        redirection = "/?area=Hotels2" + "&city=" + document.getElementById("citySearch").value + "&checkin_monthday=" + document.getElementById("checkin_day").value + "&checkin_year_month=" + document.getElementById("checkin_year_month").value + "&checkout_monthday=" + document.getElementById("checkout_monthday").value + "&checkout_year_month=" + document.getElementById("checkout_year_month").value + "&languageCode=" + defaultSort;

        

        //if come from city or search result pages, add query info

        if (document.getElementById("M_C_SearchResultCity") != null)

        {        

            var currency = document.getElementById("M_C_currencies");

            redirection += currency == null? "":"&currencyCode=" + currency.options[currency.selectedIndex].id;               

	

	        if (document.getElementById("M_C_LowRate") != null)

            {

                if(document.getElementById("M_C_LowRate").value != null && document.getElementById("M_C_LowRate").value != 0)

                {

                    redirection += "&lowRate=" + escape(document.getElementById("M_C_LowRate").value);                

                }            

            }

            if (document.getElementById("M_C_HighRate") != null)

            {

                if(document.getElementById("M_C_HighRate").value != null && document.getElementById("M_C_HighRate").value != 0)

                {

                    redirection += "&highRate=" + escape(document.getElementById("M_C_HighRate").value);               

                }            

            }            

                      

            redirection += GetStarValue(redirection, document.getElementById("M_C_Star5"), "star5");

            redirection += GetStarValue(redirection, document.getElementById("M_C_Star4"), "star4");

            redirection += GetStarValue(redirection, document.getElementById("M_C_Star3"), "star3");

            redirection += GetStarValue(redirection, document.getElementById("M_C_Star2"), "star2");

            redirection += GetStarValue(redirection, document.getElementById("M_C_Star1"), "star1");

            redirection += "&cityName=" + city; 

            

            if (document.getElementById("M_C_HotelName") != null)

            {

                if(document.getElementById("M_C_HotelName").value != null && document.getElementById("M_C_HotelName").value != 0)

                {

                    redirection += "&hotelName=" + escape(document.getElementById("M_C_HotelName").value);                

                }            

            }      

            

            if (document.getElementById("M_C_ShowSoldOut") != null) {

                redirection += document.getElementById("M_C_ShowSoldOut").checked? "":"&showSoldOut=false"; 

            }        

	         

	        if(document.getElementById("M_C_ShowOnRequest") != null){

	            redirection += document.getElementById("M_C_ShowOnRequest").checked? "":"&showOnRequest=false"; 

	        }	        

        }        

    } else {

        redirection = "/SearchResults.aspx?fileName=" + document.getElementById("selectedFileName").value + "&checkin=" + document.getElementById("checkinValue").value + "&checkout=" + document.getElementById("checkoutValue").value + "&languageCode=" + languageCode + affiliateParam + brandParam + defaultSort;

    }

        

    redirection += document.getElementById("guestValue") == null? "":"&Adults=" + document.getElementById("guestValue").value;  

    redirection += document.getElementById("roomValue") == null? "":"&Rooms=" + document.getElementById("roomValue").value;



    if (document.getElementById("M_C_HotelFacility1") != null) {

        var facility = "";

        for (var i=1; i<=11; i++){

            if(document.getElementById("M_C_HotelFacility"+ i).checked){

                facility += i + ',';

            }

        }	

        if(facility != ""){

            var strLen = facility.length;

            facility = facility.slice(0,strLen-1);

            redirection += "&facilities="+ facility;

        }



    }

    if (customData != "") {

        if (customData.indexOf("&label=") >= 0) {

            //The label parameter has already been added

        }

        else {

            customData = "&label=" + customData;

        }

        

    }

    redirection = host + redirection + customData;

    switch (target)

    {

        case "_blank":

            window.open(redirection).focus();

            break;

        case "_parent":

            window.parent.location = redirection;

            break;

        case "_top":

            window.top.location = redirection;

            break;

        default:

            window.location = redirection; // _self

    }

    

    return false;

}



function DoHotelSearch(languageCode, target, affiliateId, hotelFileName, domain, brandId, customData) {

    var affiliateParam;

    var host;

    var brandParam;

    

    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

        }

        else

        {

			document.getElementById("checkinValue").value = document.getElementById("hotelCheckin").value;

			document.getElementById("checkoutValue").value = document.getElementById("hotelCheckout").value;

        }

    }

    

    if (typeof(brandId) == 'undefined' || brandId == "0" || brandId == "")

    {

		brandParam = '';

    }

    else

    {

		brandParam = '&brandId=' + brandId

}



    if (customData != "") {

        customData = "&label=" + customData;

    }

    

    if (affiliateId == null || affiliateId == '')

    {

        affiliateParam = '';

        host = '';

    }

    else

    {

        affiliateParam = '&a_aid=' + affiliateId;   

		// backwards compatability for existing affiliate scripts

    }

    

    if (typeof (domain) == 'undefined' || domain == '') {

        host = 'http://www.hotelscombined.com'

    }

    else {

        host = 'http://' + domain;

    }

    

    var guest = document.getElementById('guestValue') == null? "":"&Adults=" + document.getElementById("guestValue").value;

    var room  = document.getElementById('roomValue') == null? "" :"&Rooms=" + document.getElementById('roomValue').value;

    

    var redirection = host + "/Hotel.aspx?tabId=Rates&fileName=" + hotelFileName + "&checkin=" + document.getElementById("checkinValue").value + "&checkout=" + document.getElementById("checkoutValue").value + "&languageCode=" + languageCode + affiliateParam + brandParam + guest + room + customData;

    

    switch (target)

    {

        case "_blank":

            window.open(redirection).focus();

            break;

        case "_parent":

            window.parent.location = redirection;

            break;

        case "_top":

            window.top.location = redirection;

            break;

        default:

            window.location = redirection; // _self

    }

    

    return false;

}



function GetStarValue (qString, starElement, starName)

{

	if (starElement == null || starElement.checked)

	{	    

	    return "";        

	}else

	{

	    return "&" + starName + "=false" ;

	}

}



// 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 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)

}



//extract (first!) value from querystring for the passed name

function GetQSVal(qsName) {

	try

	{

		var qsPair

		var i

		var qString = location.search.substr(1)

		

		if (qString == null || qString.length == 0)

			qString = query;

		    

		var arrNameVal = qString.split('&')

		//for (i in arrNameVal) {	

		for(i=0; i<arrNameVal.length; i++){

			qsPair = arrNameVal[i].split('=')

			if (URLDecode(qsPair[0]).toLowerCase() == qsName.toLowerCase()) return URLDecode(qsPair[1])

		}

	}

	catch(e)

	{

	}

	return ""

}



function setRoomValue(select, isAffiliate)

{

    if(select.value == "5" && isAffiliate == false)

    {

        select.selectedIndex = 0;

        var r = confirm(typeof(JavaScriptSearchMoreRoom) == 'undefined' ? 'We only support searching for up to four rooms at once, would you like to use our partner site Hotelplanner.com to search for more rooms?' : JavaScriptSearchMoreRoom);

        if (r== true)

        {

            var qString =  "?sc=HotelsCombined"; 

                city = "",

                checkin = "",

                checkout = "";

            

            //if on city or searchResults page

            if ( document.getElementById("M_C_SearchResultCity") != null && document.getElementById("M_C_SearchResultCity").value != 0) 

            {

                city = document.getElementById("M_C_SearchResultCity").value;

            }

            else

            {

                //if on hotel page

                if ( document.getElementById("cityNameValue") != null && document.getElementById("cityNameValue").value != 0) 

                {

                    city = document.getElementById("cityNameValue").value;

                }

            }

            

            //get user selected checkin and ckeckout value

            if (( document.getElementById("hotelCheckin") != null && document.getElementById("hotelCheckin").value != 0) ||

                ( document.getElementById("detailTabCheckin") != null && document.getElementById("detailTabCheckin").value != 0) ||

                ( document.getElementById("rateTabCheckin") != null && document.getElementById("rateTabCheckin").value != 0))

            {

                var checkinDom = document.getElementById("checkinValue");

                checkin = checkinDom == null? "" : checkinDom.value == 0? "" : reFormatDate (checkinDom.value);

            }              

            if (( document.getElementById("hotelCheckout") != null && document.getElementById("hotelCheckout").value != 0) ||

                ( document.getElementById("detailTabCheckout") != null && document.getElementById("detailTabCheckout").value != 0) ||

                ( document.getElementById("rateTabCheckout") != null && document.getElementById("rateTabCheckout").value != 0))

            {

                var checkoutDom = document.getElementById("checkoutValue");

                checkout = checkoutDom == null? "" : checkoutDom.value == 0? "" : reFormatDate (checkoutDom.value);

            }  

            

            qString += city == ""? "" : "&City=" + city;

            qString += checkin == ""? "" : "&Checkin=" + checkin;

            qString += checkout == ""? "" : "&Checkout=" + checkout;

            window.open("/ProviderRedirect.aspx?key=2.HPL.EN.USD.0.0.0&url=" + escape("http://www.hotelplanner.com" + qString), "_self");

            return false;

        }

    }

    else

    {

        document.getElementById('roomValue').value = select.options[select.selectedIndex].value;       

    }

     return false;

}



function setGuestValue(select)

{

    document.getElementById('guestValue').value = select.options[select.selectedIndex].value;

    return false;

}

