﻿
var arrDeparture = arrDeparture;

function setUpDepartureDestinationResort(departure,destination,resort) 
{
	populateDestination(0, destination, resort);
}

function destinationChanged() 
{
    if (typeof(ddDestinationList) == "undefined")
    {
        var ddDestinationList = document.getElementById('DTN');
    }
    if (typeof(ddResortList) == "undefined")
    {
        var ddResortList = document.getElementById('RST');
    }
	populateResort(0, ddDestinationList.selectedIndex, ddResortList.value);
}
function populateDeparture(index, destination, resort) 
{
    if (typeof(ddDestinationList) == "undefined")
    {
        var ddDestinationList = document.getElementById('DTN');
    }
    if (typeof(ddResortList) == "undefined")
    {
        var ddResortList = document.getElementById('RST');
    }	
	if ((index == null)||(index >= arrDeparture.length)||(arrDeparture[index][1] == null)) 
	{
		index = 0;
	}
	// clear destination list
	ddDepartureList.length = 0;	
	// iterate through destinations adding to combo
	for (var count = 0; count < arrDeparture.length; count++) 
	{
		ddDepartureList.options[count] = new Option(arrDeparture[count][1], arrDeparture[count][0]);				
	}
	ddDepartureList.selectedIndex = index;
	populateDestination(index, destination, resort);
}

function populateDestination(departureId, destination, resort) 
{
    if (typeof(ddDestinationList) == "undefined")
    {
        var ddDestinationList = document.getElementById('DTN');
    }
    if (typeof(ddResortList) == "undefined")
    {
        var ddResortList = document.getElementById('RST');
    }
	// clear destination list
	ddDestinationList.length = 0;
	// iterate through destinations adding to combo
	for (var count = 0; count < arrDeparture[departureId][3].length; count++) 
	{						
		if (arrDeparture[departureId][3][count] != null) 
		{
			if ((arrDeparture[departureId][2] != null) && (destination == '')) 
			{
				destination = arrDeparture[departureId][2];
			}
			ddDestinationList.options[count] = new Option(arrDeparture[departureId][3][count][0], arrDeparture[departureId][3][count][0]);
		}
	}
	// set defaults
	ddDestinationList.value = destination;
	if (ddDestinationList.selectedIndex == -1) 
	{
		ddDestinationList.selectedIndex = 0;
	}
	populateResort(departureId, ddDestinationList.selectedIndex, resort);
}

function populateResort(departureId, destinationId, resort) 
{
   if (typeof(ddDestinationList) == "undefined")
    {
        var ddDestinationList = document.getElementById('DTN');
    }
    if (typeof(ddResortList) == "undefined")
    {
        var ddResortList = document.getElementById('RST');
    }
// clear resort list
	ddResortList.length = 0;
	// iterate through resort array populating combo
	for (var count = 0; count < arrDeparture[departureId][3][destinationId][1].length; count++) 
	{					    					    
		ddResortList.options[count] = new Option(arrDeparture[departureId][3][destinationId][1][count].split('|')[0], arrDeparture[departureId][3][destinationId][1][count].split('|')[1]);	
	}
	// set defaults from persisted state.
	if ((resort != null)&&(resort.length != 0)) 
	{
		ddResortList.value = resort;
		if (ddResortList.selectedIndex == -1) 
		{
			ddResortList.selectedIndex = 0;
		}
	}
	// if there's only one option in resort array (i.e. any)
	// then disable resort combo
	if (arrDeparture[departureId][3][destinationId][1].length == 1) 
	{ 
		ResortCombo(true);
	}
	else 
	{
		ResortCombo(false);
	}			
}
function ResortCombo(disable) 
{
    if (typeof(ddDestinationList) == "undefined")
    {
        var ddDestinationList = document.getElementById('DTN');
    }
    if (typeof(ddResortList) == "undefined")
    {
        var ddResortList = document.getElementById('RST');
    }
    ddResortList.disabled = disable;
}
