
var resort_id_array = new Array();
var resort_starting_date_array = new Array();
var resort_ending_date_array = new Array();
var resort_durations_array = new Array();

resort_id_array.push("2");
resort_starting_date_array.push(new Date(2012,4,1));
resort_ending_date_array.push(new Date(2012,9,5));
resort_durations_array.push("7,14");

resort_id_array.push("3");
resort_starting_date_array.push(new Date(2012,4,1));
resort_ending_date_array.push(new Date(2012,9,31));
resort_durations_array.push("7,14");

resort_id_array.push("4");
resort_starting_date_array.push(new Date(2012,3,1));
resort_ending_date_array.push(new Date(2012,9,31));
resort_durations_array.push("7,14");

resort_id_array.push("5");
resort_starting_date_array.push(new Date(2012,4,1));
resort_ending_date_array.push(new Date(2012,9,13));
resort_durations_array.push("7,14");

resort_id_array.push("7");
resort_starting_date_array.push(new Date(2012,4,1));
resort_ending_date_array.push(new Date(2012,9,5));
resort_durations_array.push("7,14");

resort_id_array.push("8");
resort_starting_date_array.push(new Date(2012,4,1));
resort_ending_date_array.push(new Date(2012,9,7));
resort_durations_array.push("7,14");

resort_id_array.push("9");
resort_starting_date_array.push(new Date(2012,4,1));
resort_ending_date_array.push(new Date(2012,9,5));
resort_durations_array.push("7,14");

resort_id_array.push("10");
resort_starting_date_array.push(new Date(2012,4,1));
resort_ending_date_array.push(new Date(2012,9,13));
resort_durations_array.push("7,14");

resort_id_array.push("11");
resort_starting_date_array.push(new Date(2012,4,1));
resort_ending_date_array.push(new Date(2012,9,7));
resort_durations_array.push("7,14");

resort_id_array.push("12");
resort_starting_date_array.push(new Date(2012,4,1));
resort_ending_date_array.push(new Date(2012,9,31));
resort_durations_array.push("7,14");

resort_id_array.push("13");
resort_starting_date_array.push(new Date(2012,4,1));
resort_ending_date_array.push(new Date(2012,9,31));
resort_durations_array.push("7,14");


var deviceAgent = navigator.userAgent.toLowerCase()
var agentID = deviceAgent.match(/(iphone|ipod|ipad)/);

function selectResortAirports(resort_id, empty_dep_date, airport_id)
{
    document.getElementById("airport_id_box").length = 1;
    
    $.get("/xml/resort_airports.asp?resort_id=" + resort_id + "&t=" + Math.random(), {}, function(xml) {
	    $('airport',xml).each(function(i) {
		    AddItem($(this).text(), $(this).attr('id'), 'airport_id_box');

			if (airport_id != '')
			{
				if ($(this).attr('id') == airport_id)
				{
					 document.getElementById("airport_id_box").selectedIndex = (document.getElementById("airport_id_box").length - 1);
				}
			}
		    
	    });

		if (airport_id != '')
		{
			selectFlightDays(document.getElementById("airport_id_box").value, document.getElementById("resort_id_box").value, document.getElementById('departure_month').value);
		}
    });

	if (empty_dep_date)
	{
		document.getElementById("departure_date_box").value = "";
	}
}

function selectResortProperties(resort_id, property_id)
{
	var odd_durations = false;

	document.getElementById("property_id_box").length = 1;
	
    $.get("/xml/resort_properties.asp?resort_id=" + resort_id + "&t=" + Math.random(), {}, function(xml) {
	    $('property',xml).each(function(i) {
		    AddItem($(this).text(), $(this).attr('id'), 'property_id_box');

			if ($(this).attr('odd') == 'True')
			{
				odd_durations = true;
			}

			if (property_id != '')
			{
				if ($(this).attr('id') == property_id)
				{
					 document.getElementById("property_id_box").selectedIndex = (document.getElementById("property_id_box").length - 1);
				}
			}
	    });  
		
		populateDurations(odd_durations, resort_id);
    });	
}

function populatePropertyDurations(resort_id, property_id)
{
	var odd_durations = false;

	if (property_id != "")
	{
		$.get("/xml/resort_properties.asp?resort_id=" + resort_id + "&property_id=" + property_id + "&t=" + Math.random(), {}, function(xml) {
			$('property',xml).each(function(i) {
				if ($(this).attr('odd') == 'True')
				{
					odd_durations = true;
				}
			});  
		
			populateDurations(odd_durations, resort_id);
		});			
	}
	else
	{
		selectResortProperties(resort_id, "");
	}
}

function populateDurations(odd_durations, resort_id)
{
	var resort_durations;
	var durations = new Array();
	var durations_elem = new Array();
	
	document.getElementById("no_nights_box").length = 0;

	if (odd_durations)
	{
		for (var i=0; i < resort_id_array.length; i++)
		{
			if (resort_id_array[i] == resort_id)
			{
				resort_durations = jQuery.trim(resort_durations_array[i]);
				break;
			}
		}

		if (resort_durations.indexOf(",") > -1)
		{
			durations_elem = resort_durations.split(",");
		}
		else
		{
			durations_elem.push(resort_durations);
		}

		for (var i=0; i < durations_elem.length; i++)
		{
			if(durations_elem[i].indexOf("-") > -1)
			{
				var durations_range_split = durations_elem[i].split("-");

				if(durations_range_split.length == 2)
				{
					durations_range_split[0] = jQuery.trim(durations_range_split[0]);
					durations_range_split[1] = jQuery.trim(durations_range_split[1]);

					if (isNaN(durations_range_split[0]) == false && isNaN(durations_range_split[1]) == false)
					{
						if (parseInt(durations_range_split[1]) > parseInt(durations_range_split[0]))
						{
							for (var j = parseInt(durations_range_split[0]); j <= parseInt(durations_range_split[1]); j++)
							{
								durations.push(j);
							}
						}
					}
				}
			}
			else
			{
				durations.push(parseInt(durations_elem[i]));
			}
		}

		durations.sort(function(a,b){return a - b});

		for (var i = 0; i < durations.length; i++)
		{
			AddItem(durations[i], durations[i], "no_nights_box");
		}
	}
	else
	{
		AddItem('7', '7', "no_nights_box");
		AddItem('14', '14', "no_nights_box");
	}
}

function daysInMonth(month, year)
{
	var m = [31,28,31,30,31,30,31,31,30,31,30,31];
	if (month != 2) return m[month];
	if (year%4 != 0) return m[1];
	if (year%100 == 0 && year%400 != 0) return m[1];
	return m[1] + 1;
}

function returnDay(weekday)
{
	var weekday_literal;

	switch(weekday)
	{
		case 0:
			weekday_literal = "Sun";
			break;

		case 1:
			weekday_literal = "Mon";
			break;

		case 2:
			weekday_literal = "Tue";
			break;

		case 3:
			weekday_literal = "Wed";
			break;

		case 4:
			weekday_literal = "Thu";
			break;

		case 5:
			weekday_literal = "Fri";
			break;

		case 6:
			weekday_literal = "Sat";
			break;
	}

	return weekday_literal;
}

function returnSuffix(i)
{
	var suffix = "";
	
	var ones = i % 10;
	var tens = Math.floor(i / 10);
	tens = tens % 10;

	if (tens == 1)
	{
		suffix = 'th';
	}
	else
	{
		switch (ones)
		{
			case 1: 
				suffix = 'st';
				break;
			
			case 2:
				suffix = 'nd';
				break;
			
			case 3:
				suffix = 'rd';
				break;

			default:
				suffix = 'th';
		}
	}

	return suffix;
}

function selectFlightDays(airport_id_from, resort_id, month_date)
{
	document.getElementById("departure_day").length = 0;
	var weekdays = new Array('0','0','0','0','0','0','0');

	if (airport_id_from != '0' && resort_id != '0' && month_date != "0" && month_date != "")
	{
		$.get("/xml/airport_departure_day.asp?airport_id_from=" + airport_id_from + "&resort_id=" + resort_id + "&t=" + Math.random(), {}, function(xml) 
		{
			$('day',xml).each(function(i) 
			{
				weekdays[$(this).text()] = '1';
			});

			month_date_split = month_date.split('/');

			var this_date = new Date();
			var selected_date = new Date(month_date_split[2], (parseInt(month_date_split[1]) - 1), month_date_split[0]);

			var no_days = daysInMonth(selected_date.getMonth(), selected_date.getFullYear());

			for (var i = 1; i <= no_days; i++)
			{
				if (weekdays[selected_date.getDay()] == '1' && selected_date.getTime() > this_date.getTime())
				{
					AddItem(returnDay(selected_date.getDay()) + " " + i + returnSuffix(i), i, "departure_day");
				}

				selected_date.setDate(selected_date.getDate() + 1);
			}
			
		}); 
	}
	else
	{
		AddItem("Select...", "0", "departure_day");
	}
}

function getFlightDatesNo(airport_id_from, resort_id, month_date)
{
	var no_dates;
	var weekdays = new Array('0','0','0','0','0','0','0');
	var month_date_split;
	var selected_date;

	if (airport_id_from != '0' && resort_id != '0' && month_date != "0" && month_date != "")
	{
		$.ajax({
			url: "/xml/airport_departure_day.asp?airport_id_from=" + airport_id_from + "&resort_id=" + resort_id + "&t=" + Math.random(),
			async: false,
			success: function(xml) {
				$('day',xml).each(function(i) 
				{
					weekdays[$(this).text()] = '1';
				});
			}
		});

		month_date_split = month_date.split('/');

		var this_date = new Date();
		var selected_date = new Date(month_date_split[2], (parseInt(month_date_split[1]) - 1), month_date_split[0]);

		no_dates = 0;

		var no_days = daysInMonth(selected_date.getMonth(), selected_date.getFullYear());

		for (var i = 1; i <= no_days; i++)
		{
			if (weekdays[selected_date.getDay()] == '1' && selected_date.getTime() > this_date.getTime())
			{
				no_dates++;
			}

			selected_date.setDate(selected_date.getDate() + 1);
		}


		return no_dates;
	}
	else
	{
		return 0;
	}
}

function get_starting_date(elem_id)
{
	var default_date = new Date(2012,4,1);

	if (resort_id_array.length > 0 && document.getElementById(elem_id).value != '0')
	{
		for (var i=0; i < resort_id_array.length; i++)
		{
			if (resort_id_array[i] == document.getElementById(elem_id).value)
			{
				return resort_starting_date_array[i];
			}
		}
		
		return default_date;
	}
	else
	{
		return default_date;
	}
}

function get_ending_date(elem_id)
{
	var default_date = new Date(2012,9,31);

	if (resort_id_array.length > 0 && document.getElementById(elem_id).value != '0')
	{
		for (var i=0; i < resort_id_array.length; i++)
		{
			if (resort_id_array[i] == document.getElementById(elem_id).value)
			{
				return resort_ending_date_array[i];
			}
		}	
		
		return default_date;
	}
	else
	{
		return default_date;
	}
}

function checkSearchBox()
{
	var today = new Date(2012, 1, 23);

	if (document.getElementById("resort_id_box").value == "0")
	{
		alert ("Please select a resort");
		document.getElementById("resort_id_box").focus();

		return false;
	}

	if (document.getElementById("airport_id_box").value == "0")
	{
		alert ("Please select the departure airport");
		document.getElementById("airport_id_box").focus();

		return false;
	}

	if (document.getElementById("departure_month").value == "0")
	{
		alert ("Please select the departure month");
		document.getElementById("departure_month").focus();

		return false;
	}

	if (document.getElementById("departure_day").value == "0" || document.getElementById("departure_day").value == "")
	{
		alert ("Please select the departure day");
		document.getElementById("departure_day").focus();

		return false;
	}

	var date_split = document.getElementById("departure_month").value.split("/");
	var selected_date = new Date(date_split[2], parseInt(date_split[1]) - 1, date_split[0]);
	selected_date.setDate(document.getElementById("departure_day").value);

	document.getElementById("departure_date_box").value = selected_date.getDate() + "/" + (selected_date.getMonth() + 1) + "/" + selected_date.getFullYear();

	blockSearch();

	return true;
}

function move_handler(e)
{
	e.stopPropagation();
	e.preventDefault();
}

function blockSearch()
{
	$.fn.center = function () {
		this.css("position","absolute");
		this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
		this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
		return this;
	}

	if (agentID)
	{
		scroll(0,0);

		document.addEventListener("touchmove",  move_handler, false);
	}

	$.blockUI({ 
		message: '<div id="wait_message">Please wait while we access live availability and the latest prices.</div><object id="wait_animation" title="Searching, please wait..." classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"><param name="movie" value="/swf/waiting.swf" /><embed id="wait_animation_embed" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" quality="high" src="/swf/waiting.swf"></object>',
		css: {
			width: "650px",
			height: "140px",
			backgroundColor: "#989898"
		}
	});

	$('.blockUI.blockMsg').center();
}


function monthDiff(d1, d2) 
{    
	var diff_date = d2 - d1;
	var months = (diff_date % 31536000000)/2628000000;

	return Math.round(months);
}

function populateMonthsDropdown(resort_id, airport_id_from)
{
	var this_date = new Date();
	var starting_date;
	var ending_date;
	var current_date = new Date();

	document.getElementById("departure_month").length = 1;

	if (resort_id != '0' && resort_id_array.length)
	{

		for (var j=0; j < resort_id_array.length; j++)
		{
			if (resort_id_array[j] == resort_id)
			{
				starting_date = resort_starting_date_array[j];
				ending_date = resort_ending_date_array[j];
			}
		}

		starting_date.setDate(1);
		ending_date.setDate(28);
		current_date.setDate(starting_date.getDate());
		current_date.setMonth(starting_date.getMonth());
		current_date.setFullYear(starting_date.getFullYear());
		this_date.setDate(1);

		var date_diff = monthDiff(starting_date, ending_date);

		for (var i = 0; i < date_diff; i++)
		{
			current_date.setMonth(starting_date.getMonth() + i);

			if (monthDiff(this_date, current_date) >= 0)
			{
				if (airport_id_from == undefined || getFlightDatesNo(airport_id_from, resort_id, '1/' + (current_date.getMonth() + 1) + '/' + current_date.getFullYear()) > 0)
				{
					AddItem(returnMonth(current_date) + " " + current_date.getFullYear(), '1/' + (current_date.getMonth() + 1) + '/' + current_date.getFullYear(), "departure_month");
				}
			}
		}
	}
}

function returnMonth(this_date)
{
	var month_literal;

	switch (this_date.getMonth())
	{
		case 0:
			month_literal = "January";
			break;

		case 1:
			month_literal = "February";
			break;

		case 2:
			month_literal = "March";
			break;

		case 3:
			month_literal = "April";
			break;

		case 4:
			month_literal = "May";
			break;

		case 5:
			month_literal = "June";
			break;

		case 6:
			month_literal = "July";
			break;

		case 7:
			month_literal = "August";
			break;

		case 8:
			month_literal = "September";
			break;

		case 9:
			month_literal = "October";
			break;

		case 10:
			month_literal = "November";
			break;

		case 11:
			month_literal = "December";
	}

	return month_literal;
}

function checkContents(element)
{
	if (element.length == 1)
	{
		switch(element.id)
		{
			case "property_id_box":
				alert ("Please select an island");
				break;

			case "airport_id_box":
				alert ("Please select an island");
				break;

			case "departure_month":
				alert ("Please select an island");
		}
	}
}

$(document).ready(function () {
	if (document.getElementById("resort_id_box"))
	{
		selectResortAirports(document.getElementById("resort_id_box").value, true); 
		selectResortProperties(document.getElementById("resort_id_box").value);
		populateMonthsDropdown(document.getElementById("resort_id_box").value);
	}
});

