// Script: Search panel selection and validation functions


var asc_departureDateElId;
var asc_arrivalDateElId;

var asc_defaultStayLength;

// Summary: Called externally to initialise ids of date select controls
function asc_setDateMenuElementIds(arrivalDateElId, departureDateElId)
{
    asc_arrivalDateElId = arrivalDateElId;
    asc_departureDateElId = departureDateElId;
}

function asc_onDepDateChanged(date)
{
	
}

function asc_onArrDateChanged(date)
{
	asc_checkValidDepartureDate();
}

// Summary: Changes departure date to be 1 day after arrival date when newly
// selected arrival date is later than current departure date or far in
// advance of departure date

function asc_checkValidDepartureDate()
{
	// Get currently selected dates via datepicker.js
	var arrivalDate = getSelectedDate(asc_arrivalDateElId);
	var departureDate = getSelectedDate(asc_departureDateElId);
	
	if (arrivalDate >= departureDate)
	{
		asc_updateSelectedDepartureDate(arrivalDate);
	}
	else
	{
		var inRangeDepartureDate = new Date(departureDate);
		inRangeDepartureDate.setDate(inRangeDepartureDate.getDate() -14);
		if (arrivalDate < inRangeDepartureDate)
		{
			// Arrival date more than 2 weeks in advance of departure date
			asc_updateSelectedDepartureDate(arrivalDate);
		}
	}
}

function asc_updateSelectedDepartureDate(arrivalDate)
{
	var defaultIntervalDays = asc_defaultStayLength;
	var newDepartureDate = new Date(arrivalDate);
	newDepartureDate.setDate(newDepartureDate.getDate() + defaultIntervalDays);
	
	// Update selected date via datepicker.js
	setSelectedDate(newDepartureDate, asc_departureDateElId);
}

var asc_searchClicked = false;
	 
 // Summary: The return value of this function should be returned
 // within the onclick event of the button on the search panel. It 
 // will return true the first time it is called to indicate that
 // the search button has not yet been clicked. It will return false
 // in subsequent calls to indicate that the search button has already
 // been clicked.
 function asc_isSearchEnabled()
 {
	var enabled = !asc_searchClicked;
	if (!enabled)
	{
		alert("Search in progress. Please wait...\n\nPlease refresh the page if you have been waiting for more than 45 seconds.");
	}
	// Track that button has been clicked
	asc_searchClicked = true;
	return enabled;
 }
 
 function LoadTimer()
 {
 	if(document.getElementById('preloadpanel') && document.getElementById('backingDiv')) {
			document.getElementById('preloadpanel').style.display = "block";
			var object = document.getElementById('backingDiv').style;
			var iereg = new RegExp('MSIE [^7-9]', 'g');
		    	if(iereg.exec(navigator.appVersion))
		    	{
		    	  object.height = document.body.offsetHeight + "px";
		    	}
		    	else
		    	{
		    	  object.position = "fixed";
	    		}
			object.display = "block";
			object.opacity = (30 / 100);
			object.MozOpacity = (30 / 100);
			object.KhtmlOpacity = (30 / 100);
			object.filter = "alpha(opacity=30)";
			
			document.getElementById('preloadframe').contentWindow.location.reload(true);
	}
}


// Script: Search panel selection and validation functions

function validateSearchForm (form) {


	var strArrDateMenuPrefix;
	var strDepDateMenuPrefix;
	var dtArr;
	var dtDep;

	// Base names of 3-menu date selection
	strArrDateMenuPrefix = 'ctl00_Main_ctlSearch_ctlArrDate_ddl';
	strDepDateMenuPrefix = 'ctl00_Main_ctlSearch_ctlDepDate_ddl';

	// Date menus
	dtArr = form_getDateFromHTMLDateMenu(asc_arrivalDateElId);
	dtDep = form_getDateFromHTMLDateMenu(asc_departureDateElId);
	if (dtArr == null) {
		alert('Please select a valid arrival date.');
		frmSimpleSearch.arrDateDay.focus();
		return false;
	}
	if (dtDep == null) {
		alert('Please select a valid departure date.');
		frmSimpleSearch.depDateDay.focus();
		return false;
	}
	if (dtDep <= dtArr) {
		alert('Please select a departure date that is later than the arrival date.');
		frmSimpleSearch.depDateDay.focus();
		return false;
	}

	// Adult / child count
	if (frmSimpleSearch.adultCount1.options[frmSimpleSearch.adultCount1.selectedIndex].value == 0
		&& frmSimpleSearch.childCount1.options[frmSimpleSearch.childCount1.selectedIndex].value == 0) {
		alert('Please select one or more guests.');
		frmSimpleSearch.adultCount.focus();
		return false;
	}
	


	return true;
}

function onArrivalDateChange (select) {
	ensureDepDateValid();
}

function onDepartureDateChange (select, dateElId) {
	return true;
}

function sdoToolTip () {
	return true;
}

function shideToolTip () {
	return true;
}


// Purpose: Sets departure date to valid date
function ensureDepDateValid () {
	var strArrDateMenuPrefix;
	var strDepDateMenuPrefix;
	var dtArr;
	var dtDep;
	var dtNewDep;
	var frmSearch;

	// Base names of 3-menu date selection
	strArrDateMenuPrefix = 'ctl00_Main_ctlSearch_ctlArrDate_ddl';
	strDepDateMenuPrefix = 'ctl00_Main_ctlSearch_ctlDepDate_ddl';
	frmSearch = document.aspnetForm;

	// Get dates from menus
	dtArr = form_getDateFromHTMLDateMenu(asc_arrivalDateElId);
	dtDep = form_getDateFromHTMLDateMenu(asc_departureDateElId);
	if (dtDep <= dtArr || dtDep == null) {
		// Departure date earlier or same as arrival so set 1 day ahead
		dtNewDep = new Date();
		dtNewDep.setYear(dtArr.getFullYear());
		dtNewDep.setMonth(dtArr.getMonth());
		dtNewDep.setDate(dtArr.getDate() + asc_defaultStayLength);
		form_setHTMLDateMenuDate(asc_departureDateElId, dtNewDep);
	}
}

// Sets date menus to default dates
function initDateMenus () {
	var dtArrDate;
	var dtDepDate;

	dtArrDate = new Date(); // current date
	dtDepDate = new Date();
	dtDepDate.setYear(dtArrDate.getFullYear());
	dtDepDate.setMonth(dtArrDate.getMonth());
	dtDepDate.setDate(dtArrDate.getDate() + asc_defaultStayLength);
	form_setHTMLDateMenuDate(document.frmSimpleSearch, "arrDate", dtArrDate);
	form_setHTMLDateMenuDate(document.frmSimpleSearch, "depDate", dtDepDate);
}

//relies on getFieldDate in datepicker.js
function form_getDateFromHTMLDateMenu (dateElId)
{
	ctlDate = document.getElementById(dateElId);
	var dateString = ctlDate.value;
	var objDate;
	if (dateString == '')
	{
	    objDate = null;
	}
	else
	{
		objDate = getFieldDate(dateString);
	}
	return objDate;
}

// Purpose: Updates date menu to given date
// Remarks: Expects date menu generated by server side 
// form_createHTMLDateMenu function
// Output: None
//relies on getDateString  in datepicker.js
function form_setHTMLDateMenuDate (dateElId, date)
{

	var ctlDate = document.getElementById(dateElId);

	if (date == null)
	{
        ctlDate.value = '';
	}
	else
	{
        ctlDate.value = getDateString(date);
	}
}



//Rooms

function showRoomOptions(roomCountElId) {
	var selRooms = document.getElementById(roomCountElId);
	var roomCount = selRooms.options[selRooms.selectedIndex].value;
	for(var i=1; i<=5; i++)
	{
		var display;
		if (i <= roomCount)
		{
			display = "";
		}
		else
		{
			display = "none";
		}
		
		document.getElementById('room'+i).style.display = display;
	}
}

//Property functions

function propertyList()
{
    this.properties = [];
}

function propertyInfo(code, name)
{
    this.code  = code;
    this.name = name;
    this.searchRegions = [];
}

propertyList.prototype.addProperty = function(property) {
    this.properties.push(property);
}

propertyInfo.prototype.addRegion = function(regionCode)
{
	var region = new Object();
    region.code = regionCode;
    this.searchRegions.push(region);
}

populateProperties = function(locationElId, propertyElId)
{
    var selectLocation = document.getElementById(locationElId);
    var selectProperty = document.getElementById(propertyElId);
    var regionCode = selectLocation.options[selectLocation.selectedIndex].value;
    var regionName = selectLocation.options[selectLocation.selectedIndex].text;
    var propertyCode = selectProperty.options[selectProperty.selectedIndex].value;
    if (regionCode != '')
    {
        
        selectProperty.options.length = 0;
        selectProperty.options[selectProperty.options.length] = new Option('All Macdonald Hotels in ' + regionName, '');
        for(var index = 0; index < properties.properties.length; index++)
	    {
	        currentProperty = properties.properties[index];
            for(var i = 0; i < currentProperty.searchRegions.length; i++)
            {
                if (currentProperty.searchRegions[i].code == regionCode)
                {
                    //add this property to drop down list
                    selectProperty.options[selectProperty.options.length] = new Option(currentProperty.name, currentProperty.code);
                }
            }
	    }
	    if(selectProperty.options.length == 2)
	    {
		    selectProperty.remove(0);	
	    }
    }
    else
    {
        selectProperty.options.length = 0;
        selectProperty.options[selectProperty.options.length] = new Option('Any Macdonald Hotel', '');
        for(var index = 0; index < properties.properties.length; index++)
	    {
	        currentProperty = properties.properties[index];
            selectProperty.options[selectProperty.options.length] = new Option(currentProperty.name, currentProperty.code);
	    }
    
    }
    
    if (propertyCode != '')
    {
        for(var i = 0; i < selectProperty.options.length; i++)
        {
            if (selectProperty.options[i].value == propertyCode)
            {
                selectProperty.selectedIndex = i;
                break;
            }
        }
    }
    
    
}

var populatedAdults = true;
function populateAdults() {

    var properties = document.getElementById('ctl00_Main_ctlSearch_ddlPropertyCode');
    var propCode = properties.options[properties.selectedIndex].value;
    var maxAdults = 3;
    var maxChildren = 2;
    if(propCode == 'ERMITA') {
        maxAdults = 8;
        maxChildren = 8;
    } else if(propCode == 'AVIEHI') {
		maxAdults = 6;
        maxChildren = 6;
	}
    var i = 1;
    while(document.getElementById('ctl00_Main_ctlSearch_ddlAdultCount' + i))
    {
        var adultCount = document.getElementById('ctl00_Main_ctlSearch_ddlAdultCount' + i);
        var childCount = document.getElementById('ctl00_Main_ctlSearch_ddlChildCount' + i);
        var numberAdults = adultCount.options[adultCount.selectedIndex].value;
        var numberChildren = childCount.options[childCount.selectedIndex].value;
        
        adultCount.options.length = 0;
        for(var j=0; j <= maxAdults; j++) {
            adultCount[j] = new Option(j);
            adultCount[j].value = j;
            if (j == numberAdults)
            {
                adultCount.selectedIndex = j;
            }

        }
        childCount.options.length = 0;
        for(var j=0; j <= maxChildren; j++) {
            childCount[j] = new Option(j);
            childCount[j].value = j;
            if (j == numberChildren)
            {
                childCount.selectedIndex = j;
             }
        }
        if(numberAdults >= adultCount.length)
        {
        	if(i == 1) {
        		adultCount.selectedIndex = 2;
        	}
        	else {
        		adultCount.selectedIndex = 0;
        	}
        }
        i++;
    }
    
    if(!populatedAdults)
    {
        var GETDATA = new Array();
	    var sGet = window.location.search;
	    if (sGet) // if has a value...
	    {
		    sGet = sGet.substr(1);
		    var sNVPairs = sGet.split("&");
		    for (var i = 0; i < sNVPairs.length; i++)
		    {
			    var sNV = sNVPairs[i].split("=");
			    var sName = sNV[0];
			    var sValue = sNV[1];
    			
			    if(sName.indexOf("adultCount") >= 0 || sName.indexOf("childCount") >= 0)
			    {
			        sName = sName.replace(/adultCount/, 'AdultCount');
			        sName = sName.replace(/childCount/, 'ChildCount');
			        var thisItem = document.getElementById("ctl00$Main$ctlSearch$ddl"+sName);
			        thisItem.selectedIndex = sValue;
			    }
		    }
	    }
    }
    populatedAdults = true;
}
