//// Search form presets handling	////

// Show individual airports after an airport group has been selected
function changeAirports(element, type, keepFirst)
{
	// Get element for sub-menu
	var subElement;
	var subList;
	if (type == 'dep_airport_groups')
	{
		subElement = eval('document.forms.' + element.form.name + '.airportfromid');
		subList = arrDepAirports;
	}
	else if (type == 'dest_airport_groups')
	{
		subElement = eval('document.forms.' + element.form.name + '.airporttoid');
		subList = arrDestAirports;
	}
	else if (type == 'dest_resort_groups')
	{
		subElement = eval('document.forms.' + element.form.name + '.resorttoid');
		subList = arrDestResorts;
	}

	// Id for the current main menu selection, e.g. airport group id
	var elementId = element.value;

	// Clear out elements (if keepFirst is 1, the first item, probably "Any", will be kept)
	subElement.options.length = keepFirst;

	// Create select options for sub-elements matching current selection
	for (var i = 0; i < subList.length; i++)
	{
		if (subList[i][2] == elementId)
		{
			subElement[subElement.length] = new Option
			(
				subList[i][1],
				subList[i][0]
			);
			
		}
	}

	if(subElement.length > 1)
	{
		subElement.disabled = false;
	}
	else 
	{
		subElement.disabled = true;
	}
	

}

function checkQuickSearch(objForm, searchType)
{
	var len = 0;
	
	// This checks the user is not simply moving between 'packages' and 'flights' search
	if(searchType != "fullsearch_fo")
	{
		if(objForm.boardbasis)
		{
			if(objForm.boardbasis.value == -1)
			{
				alert("Please select a board basis.");
				return false;	
			}
		}
	}
	
	if(objForm.airportgrouptoid)
	{
		if(objForm.airportgroupfromid)
		{
			len = objForm.airportgroupfromid.value.length;
		}
		else if(objForm.airportcombinedfromid)
		{
			len = objForm.airportcombinedfromid.value.length;
		}	
		
		if((len + objForm.airportgrouptoid.value.length) == 0 )
		{
			alert("Please select a departure or destination area");
			return false;
		}
		
		if ( objForm.airportgrouptoid.value == '-1' )
		{
			alert("Please select a destination");
			return false;
		}
	}
	else if(objForm.resortgroupid)
	{
		len = objForm.resortgroupid.value.length + objForm.resorttoid.value.length;
		if(len == 0)
		{
			alert("Please select a country or resort");
			return false;
		}
	}
	
	return true;
}


// Store form values in a cookie for when user clicks 'back'
function saveFormValues(objForm, searchType, ignoreFormErrors)
{
	var airportfromgroup;
	var airporttogroup;
	var day;
	var monthyear;
	var daysplusminus;
	var nights;
	var airportto;
	var airportfrom;
	var price = "";
	var boardbasis = "";
	var rating = "";
	var includeaoa = "";
	var selectedlifestyles = "";
	
	if(!ignoreFormErrors && !checkQuickSearch(objForm, searchType)) return false;
	
	switch ( searchType )
	{
		case "packagesearch":
			airportfromgroup = objForm.airportgroupfromid.value;
			/*
			if(objForm.airportgrouptoid)
			{
				airporttogroup = objForm.airportgrouptoid.value;
			}
			if(objForm.airporttoid)
			{
				airportto = objForm.airporttoid.value;
			}
			*/
			if(objForm.resortgroupid)
			{
				airporttogroup = objForm.resortgroupid.value;
			}
			if(objForm.resorttoid)
			{
				airportto = objForm.resorttoid.value;
			}
			
			if(objForm.boardbasis)
			{
				boardbasis = objForm.boardbasis.value;	
			}
			airportfrom = objForm.airportfromid.value;
			day = objForm.departure_day.value;
			monthyear = objForm.departure_monthyear.value;
			if(objForm.duration)
			{
				nights = objForm.duration.value;
			}
			if(objForm.price)
			{
				price = objForm.price.value;
			}
			if(objForm.rating)
			{
				rating = objForm.rating.value;
			}
			if(objForm.incaoa)
			{
				includeaoa = objForm.incaoa.checked ? 'true' : 'false';
			}
			for(var el in objForm.elements)
			{
				if (
					objForm.elements[el]
					&& objForm.elements[el].name
					&& typeof(objForm.elements[el].name) == "string"
					&& name.indexOf('lifestyle_') != -1
					&& objForm.elements[el].checked
				)
				{
					selectedlifestyles = selectedlifestyles + ';' + objForm.elements[el].name;
				}
			}
			break;		
		case "skisearch":
			airportfromgroup = objForm.airportgroupfromid.value;
			airportfrom = objForm.airportfromid.value;
			
			if(objForm.resortgroupid)
			{
				airporttogroup = objForm.resortgroupid.value;
			}
			if(objForm.resorttoid)
			{
				airportto = objForm.resorttoid.value;
			}
			
			day = objForm.departure_day.value;
			monthyear = objForm.departure_monthyear.value;
			
			if(objForm.boardbasis)
			{
				boardbasis = objForm.boardbasis.value;	
			}
			if(objForm.duration)
			{
				nights = objForm.duration.value;
			}
			if(objForm.price)
			{
				price = objForm.price.value;
			}
			if(objForm.rating)
			{
				rating = objForm.rating.value;
			}
			if(objForm.incaoa)
			{
				includeaoa = objForm.incaoa.checked ? 'true' : 'false';
			}
			break;
		// This is the "full search flights only"
		case "fullsearch_fo":		
			airportfromgroup = objForm.airportgroupfromid.value;
			if(objForm.airportgrouptoid)
			{	
				airporttogroup = objForm.airportgrouptoid.value;
			}
			if(objForm.airporttoid)
			{
				airportto = objForm.airporttoid.value;
			}
			if(objForm.resorttoid)
			{
				airportto = objForm.resorttoid.value;	
			}
			if(objForm.resortgroupid)
			{
				airporttogroup = objForm.resortgroupid.value;	
			}
			day = objForm.departure_day.value;
			monthyear = objForm.departure_monthyear.value;
			nights = objForm.duration.value;
			airportfrom = objForm.airportfromid.value;
			
			daysplusminus = objForm.depvariance.value;			
			break;
	};

	var cookieString = escape(''
		+ airportfromgroup + ':'
		+ airportfrom + ':'
		+ airporttogroup + ':'
		+ airportto + ':'
		+ day + ':'
		+ monthyear + ':'
		+ daysplusminus + ':'
		+ nights  + ':'
		+ boardbasis + ':'
		+ price + ':'
		+ rating + ':'
		+ includeaoa + ':'
		+ selectedlifestyles);
	
	document.cookie = searchType + objForm.name + '=' + cookieString;
	return true;
}

// Restore form values from cookie
function restoreFormValues(objForm, searchType)
{
	var cookies = document.cookie;
	var indexSearch = searchType + objForm.name + '=';

	var pos = cookies.indexOf(indexSearch);
	if (pos == -1)
	{
		return false;
	}

	var start = pos + searchType.length + objForm.name.length + 1;
	
	var end = cookies.indexOf(';', start);
	if (end == -1)
	{
		end = cookies.length;
	}

	var cookie = unescape(cookies.substring(start, end));
	var parts = cookie.split(':');
	
	var showExtraNav = false;

	switch ( searchType )
	{
		case "packagesearch":			
			objForm.airportgroupfromid.value = parts[0];
			
			if(objForm.resortgroupid)
			{
				objForm.resortgroupid.value = parts[2];
				changeAirports(objForm.resortgroupid, 'dest_resort_groups', true);
			}
			if(objForm.resorttoid)
			{
				objForm.resorttoid.value = parts[3];	
			}
			/*
			if(objForm.airportgrouptoid)
			{
				objForm.airportgrouptoid.value = parts[2];
				changeAirports(objForm.airportgrouptoid, 'dest_airport_groups', true);
			}
			objForm.airporttoid.value = parts[3];
			*/
			changeAirports(objForm.airportgroupfromid, 'dep_airport_groups', true);			
			objForm.airportfromid.value = parts[1];
			objForm.departure_monthyear.value = parts[5];
			changeMonth(objForm, objForm.departure_monthyear.value);
			objForm.departure_day.value = parts[4];
			
			if(objForm.duration)
			{
				objForm.duration.value = parts[7];
			}
			if(objForm.boardbasis)
			{
				objForm.boardbasis.value = parts[8];
				showExtraNav = (showExtraNav || parts[8]);
			}
			if(objForm.price)
			{
				objForm.price.value = parts[9];
				showExtraNav = (showExtraNav || parts[9]);
			}
			if(objForm.rating)
			{
				
				objForm.rating.value = parts[10];
				showExtraNav = (showExtraNav || parts[10]);
			}
			if(objForm.incaoa)
			{
				objForm.incaoa.checked = parts[11] == 'true';
			}
			var lifestyle_parts = parts[12].split(';');
			for (var i in lifestyle_parts)
			{
				if (!lifestyle_parts[i]) continue;
				eval(	'if(objForm.' + lifestyle_parts[i] + ')'
					+ 'objForm.' + lifestyle_parts[i] + '.checked=true;'
				);
				showExtraNav = true;
			}
			
			if (showExtraNav && typeof(leftNavSearchExtra) != 'undefined')
			{
				toggleLeftNav('leftNavSearchExtra', 'leftnavsearchextra', false, 'nav_search_extra_img', 'leftNavSearchExtraImageToggle', false);
			}
			break
		case "skisearch":
			
			objForm.airportgroupfromid.value = parts[0];
			changeAirports(objForm.airportgroupfromid, 'dep_airport_groups', true);
			objForm.airportfromid.value = parts[1];
			
			if(objForm.resortgroupid)
			{
				objForm.resortgroupid.value = parts[2];
				changeAirports(objForm.resortgroupid, 'dest_resort_groups', true);
			}
			if(objForm.resorttoid)
			{
				objForm.resorttoid.value = parts[3];	
			}
		
			objForm.departure_monthyear.value = parts[5];
			changeMonth(objForm, objForm.departure_monthyear.value);
			objForm.departure_day.value = parts[4];
			
			objForm.depvariance.value = parts[6];
			objForm.duration.value = parts[7];
			 
			objForm.boardbasis.value = parts[8];
			objForm.rating.value = parts[10];
			objForm.price.value = parts[9];
			
			if(objForm.incaoa)
			{
				objForm.incaoa.checked = parts[11] == 'true';
			}
			
				
			break;
		case "fullsearch_fo":		
			if(objForm.airportgrouptoid)
			{	
				objForm.airportgrouptoid.value = parts[2];
				changeAirports(objForm.airportgrouptoid, 'dest_airport_groups', true);
			}
			if(objForm.airporttoid)
			{
				objForm.airporttoid.value = parts[3];
			}
			if(objForm.resortgroupid)
			{
				objForm.resortgroupid.value = parts[2];	
			}
			if(objForm.resorttoid)
			{
				changeAirports(objForm.resortgroupid, 'dest_resort_groups', true);
				objForm.resorttoid.value = parts[3];	
			}			
			objForm.airportgroupfromid.value = parts[0];		
			objForm.departure_monthyear.value = parts[5];
			changeMonth(objForm, objForm.departure_monthyear.value);
			objForm.departure_day.value = parts[4];
			
			objForm.depvariance.value = parts[6];
			objForm.duration.value = parts[7];

			changeAirports(objForm.airportgroupfromid, 'dep_airport_groups', true); 

			objForm.airportfromid.value = parts[1];		
			//objForm.price.value = parts[9];
			break;
	};
	
	return true;
}


function changeMonth(objForm, date)
{
	var month, year;
	var today = new Date();

	// Get the existing day of month, to preserve where possible
	var daySelection = objForm.departure_day.value;
	
	date = date.split('-');
	month = date[1];
	year = date[0];


	var start = (today.getMonth()+1 == month && year == today.getFullYear()) ? today.getDate() : 1;
	var max = 31;
	
	if(2 == month)
	{
		max = 28;
		if((year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)))
		{
			max = 29;
		}
	}
	else if(4 == month || 6 == month || 9 == month || 11 == month)
	{
		max = 30;
	}
	
	objForm.departure_day.options.length = 0;
	var foundSelected = false;
	var selectedIndex; 

	for(var i = start; i < (max+1); i++)
	{
		var txt = '' + i;
		var isSelected = false;

		if(txt.length == 1)
		{
			txt = "0" + txt;
		}
		
		if (i == daySelection)
		{
			selectedIndex = i;
			foundSelected = true;
		}
		
		objForm.departure_day.options[objForm.departure_day.options.length]
			= new Option(txt, txt, false);
	}

	// Set selection if not done so already
	if ((! foundSelected) && objForm.departure_day.options.length)
	{
		if (daySelection < start)
		{
			objForm.departure_day.options[0].selected = true;
		}
		else if (daySelection > max)
		{
			objForm.departure_day.options[objForm.departure_day.options.length-1].selected = true;
		}
	}
	else if (selectedIndex)
	{
		//objForm.departure_day.options[selectedIndex-start].selected = true;
		objForm.departure_day.selectedIndex = selectedIndex-start;
	}
}

function update_dates_from_calendar(dateval)
{
	var date = dateval.split("-");

	var monthyear = document.getElementById("departure_monthyear");
	monthyear.value = date[0] + "-" + date[1];
	
	// update the days drop down
	changeMonth(monthyear.form, monthyear.value);
	
	var day = document.getElementById("departure_day");
	day.value = date[2];
}

function update_calendar_from_dates()
{
	var monthyear = document.getElementById("departure_monthyear");
	var day = document.getElementById("departure_day");
	var calendar = document.getElementById("f_date_c");
	
	calendar.value = monthyear.value + "-" + day.value;
}
