// JavaScript Document
var destinationArray = ['Argentina','Belize','Brazil','Cambodia','Canada','Chile','Costa Rica','Croatia','Ecuador','Galapagos','Guatemala','India','Kilimanjaro','Laos','Mexico','Morocco','Nepal','New Zealand','Nicaragua','Panama','Peru','Romania','Slovenia','Tanzania','Thailand','Turkey','Venezuela','Vietnam'];
var ActivityArray = ['Canoeing','Canyoning','Caving','Hiking','Horseback Riding','Ice Trekking','Inner-Tubing','Kayaking','Mountain Biking','Rappelling','Rock Climbing','Safari','Snorkeling','Trekking','Whitewater Rafting','Zip-Lining','Volunteering','Elephant Riding','Yoga','Whale Watching','Camel Riding','Llama Trekking','Hot Air Ballooning','Sailing','Jet Boating','Skiing/Snowboarding','Wine Tasting','Fishing'];
var RatingArray = ['Easy','Moderate','Challenging','Strenuous'];

var OptionArray = [[0,3,2],
[0,3,0],
[0,5,2],
[0,5,0],
[0,7,2],
[0,7,0],
[0,8,2],
[0,8,0],
[0,9,2],
[0,9,0],
[0,14,2],
[0,14,0],
[1,2,2],
[1,4,2],
[1,6,2],
[1,7,2],
[1,8,2],
[1,12,2],
[2,3,2],
[2,7,2],
[2,8,2],
[2,9,2],
[2,14,2],
[3,3,2],
[3,3,1],
[3,7,2],
[3,7,1],
[3,8,2],
[3,8,1],
[3,23,2],
[3,23,1],
[4,0,2],
[4,0,3],
[4,3,2],
[4,3,3],
[4,7,2],
[4,7,3],
[4,8,2],
[4,8,3],
[4,13,2],
[4,13,3],
[4,19,2],
[4,19,3],
[4,14,2],
[4,14,3],
[4,26,2],
[4,26,3],
[4,15,2],
[4,15,3],
[5,3,0],
[5,3,2],
[5,5,0],
[5,5,2],
[5,7,0],
[5,7,2],
[5,8,0],
[5,8,2],
[5,13,0],
[5,13,2],
[5,14,0],
[5,14,2],
[6,3,0],
[6,3,1],
[6,3,2],
[6,4,0],
[6,4,1],
[6,4,2],
[6,7,0],
[6,7,1],
[6,7,2],
[6,8,0],
[6,8,1],
[6,8,2],
[6,16,0],
[6,16,1],
[6,16,2],
[6,14,0],
[6,14,1],
[6,14,2],
[6,18,0],
[6,18,1],
[6,18,2],
[6,15,0],
[6,15,1],
[6,15,2],
[7,3,2],
[7,7,2],
[7,8,2],
[7,14,2],
[8,3,2],
[8,3,1],
[8,4,2],
[8,4,1],
[8,6,2],
[8,6,1],
[8,7,2],
[8,7,1],
[8,8,2],
[8,8,1],
[8,12,2],
[8,12,1],
[8,16,2],
[8,16,1],
[8,14,2],
[8,14,1],
[8,15,2],
[8,15,1],
[9,3,2],
[9,4,2],
[9,7,2],
[9,8,2],
[9,12,2],
[10,3,2],
[10,7,2],
[10,8,2],
[10,15,2],
[11,3,2],
[11,8,2],
[11,13,2],
[11,14,2],
[11,17,2],
[12,8,3],
[12,11,3],
[12,13,3],
[12,22,3],
[13,7,2],
[13,8,2],
[13,13,2],
[13,17,2],
[14,3,2],
[14,7,2],
[14,12,2],
[14,18,2],
[14,19,2],
[15,8,0],
[15,8,2],
[15,13,0],
[15,13,2],
[15,20,0],
[15,20,2],
[16,13,3],
[17,2,2],
[17,2,1],
[17,3,2],
[17,3,1],
[17,24,2],
[17,24,1],
[17,7,2],
[17,7,1],
[17,8,2],
[17,8,1],
[17,25,2],
[17,25,1],
[17,14,2],
[17,14,1],
[18,3,2],
[18,7,2],
[18,8,2],
[18,15,2],
[18,16,2],
[19,3,2],
[19,7,2],
[19,8,2],
[19,12,2],
[19,14,2],
[19,15,2],
[20,3,0],
[20,3,2],
[20,4,0],
[20,4,2],
[20,8,0],
[20,8,2],
[20,10,0],
[20,10,2],
[20,14,0],
[20,14,2],
[20,21,0],
[20,21,2],
[21,3,2],
[21,4,2],
[21,8,2],
[22,3,2],
[22,7,2],
[22,8,2],
[22,14,2],
[23,8,3],
[23,11,3],
[23,13,3],
[23,22,3],
[24,3,2],
[24,8,2],
[24,16,2],
[24,17,2],
[25,3,2],
[25,4,2],
[25,7,2],
[25,8,2],
[25,22,2],
[26,3,0],
[26,4,0],
[26,8,0],
[27,3,2],
[27,3,1],
[27,7,2],
[27,7,1],
[27,8,2],
[27,8,1],
[27,23,2],
[27,23,1],
];

function update_on_load() {
	document.select_entry.destination_value.selectedIndex = 0;
	update_destination( -1 );
}

function update_destination(destination_value) {

	var activities = new Array();

	destination_index = getIndex( destination_value, destinationArray );
 
	for (i in OptionArray) 
	{
		if ((OptionArray[i][0] == destination_index) || (destination_value == -1)) 
		{
			actIndex = getIndex( OptionArray[i][1], activities );
			activities[actIndex] = OptionArray[i][1];
		}
	}
    
	clearSelect(document.select_entry.activity_value);
	document.select_entry.activity_value.options[0] = new Option('Any Activity', -1);
	if( destination_value == -1 )
	{
		for(i in ActivityArray)
		{
			document.select_entry.activity_value.options[Number(i)+1] = new Option(ActivityArray[i], ActivityArray[i]);
		}
	}
	else
	{
		for (i in activities) 
		{
			document.select_entry.activity_value.options[Number(i)+1] = new Option(ActivityArray[activities[i]], ActivityArray[activities[i]]);
		}
	}
	document.select_entry.activity_value.selectedIndex = 0;
   update_activity(destination_value, -1);
}


function update_activity(destination_value, activity_value)  {
	var ratings = new Array();

	destination_index = getIndex( destination_value, destinationArray );
	activity_index = getIndex( activity_value, ActivityArray );

	document.select_entry.rating_value.selectedIndex = 0;

	for( option in OptionArray )
	{
		if ((OptionArray[option][0] == destination_index) || (destination_value == -1)) 
		{
			if ((OptionArray[option][1] == activity_index) || (activity_value == -1)) 
			{
				ratingIndex = getIndex( OptionArray[option][2], ratings );
				ratings[ratingIndex] = OptionArray[option][2];	
			}
		}
	}

	clearSelect(document.select_entry.rating_value);
	document.select_entry.rating_value.options[0] = new Option('Any Rating', -1);
	if( destination_value == -1 && activity_value == -1 )
	{
		for (i in RatingArray) 
		{
			document.select_entry.rating_value.options[Number(i)+1] = new Option(RatingArray[i], RatingArray[i]);
		}
	}
	else
	{
		for (i in ratings) 
		{
			document.select_entry.rating_value.options[Number(i)+1] = new Option(RatingArray[ratings[i]], RatingArray[ratings[i]]);
		}
	}
	document.select_entry.rating_value.selectedIndex = 0;
		
}
//-----------------------------------------//
function getIndex(value, array) {
	for (ii in array) {
		if (array[ii] == value) {
			return(ii);
		}
	}
	return(array.length);
}
//-----------------------------------------//
function clearSelect(pulldown){
	// empty the rating list
	rating_list = document.select_entry.rating_value; 
	for (m=pulldown.options.length-1;m>=0;m--) 
		pulldown.options[m]=null;
}
