today = new Date();

function process_air_form()
{
	var depart = fetch_object('air_depart_city');
	var depart_city = depart.options[depart.selectedIndex].value;
	var depart_text = fetch_object('air_depart_text');
	var arrive = fetch_object('air_arrive_city');
	var arrive_city = arrive.options[arrive.selectedIndex].value;
	var arrive_text = fetch_object('air_arrive_text');
	var roundtrip = fetch_object('air_roundtrip').checked;
	var thecookie = fetch_air_cookie();
	var adults = fetch_object('air_adults').selectedIndex;
	var children = fetch_object('air_children').selectedIndex;
	var infants = fetch_object('air_infants').selectedIndex;

	if (typeof(nocitysuggest) != 'undefined')
	{
		depart_text.value = depart_city + ', ' + depart.options[depart.selectedIndex].text;
		arrive_text.value = arrive_city + ', ' + arrive.options[arrive.selectedIndex].text;
	}
	if ((depart_text.value == '' || depart_city == '???') && thecookie[0] && thecookie[1])
	{
		depart_city = ( depart_text.value == thecookie[0] ? thecookie[1] : '???' );

		if (depart_city == '???')
		{
			if (confirm('Vous avez récemment choisi un départ de ' + thecookie[0] + '. Aimeriez-vous choisir cet endroit une autre fois?'))
			{
				depart_city = thecookie[1];
				depart_text.value = thecookie[0];
			}
			else
			{
				depart_text.focus();

				if (depart_text.value == '')
				{
					return false;
				}
			}
		}

		depart.options[depart.selectedIndex].value = depart_city;
	}
	if ((arrive_text.value == '' || arrive_city == '???') && thecookie[2] && thecookie[3])
	{
		arrive_city = ( arrive_text.value == thecookie[2] ? thecookie[3] : '???' );

		if (arrive_city == '???')
		{
			if (confirm('Vous avez récemment choisi d’arriver à ' + thecookie[2] + '. Aimeriez-vous choisir cet endroit une autre fois?'))
			{
				arrive_city = thecookie[3];
				arrive_text.value = thecookie[2];
			}
			else
			{
				arrive_text.focus();

				if (arrive_text.value == '')
				{
					return false;
				}
			}
		}

		arrive.options[arrive.selectedIndex].value = arrive_city;
	}
	if (depart_city == '' || depart_city == null || depart_city == '???' || depart_text.value == '')
	{
		depart_text.value == '';
		depart_text.focus();
		alert('Veuillez sélectionner un lieu de départ valable.');
		return false;
	}
	if (arrive_city == '' || arrive_city == null || arrive_city == '???' || arrive_text.value == '')
	{
		arrive_text.value == '';
		arrive_text.focus();
		alert('Veuillez sélectionner un lieu d’arrivée valable.');
		return false;
	}
	if (depart_city == arrive_city)
	{
		arrive_text.value == '';
		arrive_text.focus();
		alert('Vous ne pouvez pas faire votre départ et votre arrivée du même aéroport.');
		return false;
	}
	if (adults + children + infants == 0)
	{
		alert('Veuillez indiquer au moins un passager.');
		return false;
	}
	if (fetch_object('air_adults').selectedIndex == 0 && (fetch_object('air_children').selectedIndex + fetch_object('air_infants').selectedIndex) > 0)
	{
		alert('Please call our office to book travel for children under the age of 12 if they are traveling alone as airline travel restrictions vary.');
		return false;
	}

	var future = new Date();
	future.setFullYear(future.getFullYear(), future.getMonth(), future.getDate() + 330);
	var leave_date = read_date_dd('air_leave');
	var return_date = read_date_dd('air_return');

	if (leave_date > future || (roundtrip && return_date > future))
	{
		alert('Dates doivent être après aujourd’hui et ne peuvent pas dépasser 330 jours dans l’avenir.');
		return false;		
	}
	if (roundtrip && return_date < leave_date)
	{
		alert('Vous ne pouvez pas rentrer avant votre départ.');
		return false;		
	}

	var airsub = fetch_object('air_submit');
	airsub.value = ( fetch_object('collapseobj_tsg_air_advanced') ? 'Début Recherche – Veuillez Patienter' : 'Chargement' );
	airsub.disabled = true;
	fetch_object('json').value = '1';

	//create cookies
	thecookie[0] = depart_text.value;
	thecookie[1] = depart_city;
	thecookie[2] = arrive_text.value;
	thecookie[3] = arrive_city;
	thecookie[4] = leave_date;
	thecookie[5] = fetch_object('air_leave_time').selectedIndex;
	thecookie[6] = ( roundtrip ? 1 : 0 );
	thecookie[7] = return_date;
	thecookie[8] = fetch_object('air_return_time').selectedIndex;
	thecookie[9] = adults;
	thecookie[10] = children;
	thecookie[11] = infants;

	today = new Date();
	today.setDate(today.getDate() + 7); //7 days from now
	set_cookie('air_form', thecookie.join('|'), today);

	return true;
}

function toggle_air_roundtrip()
{
	fetch_object('air_roundtrip_a').style.visibility = fetch_object('air_roundtrip_b').style.visibility = fetch_object('air_roundtrip_c').style.visibility = ( fetch_object('air_roundtrip').checked ? 'visible' : 'hidden' );
}

function check_passengers(which)
{
	var agepicks = '';
	var filloptions = '';
	var agebox = fetch_object('air_ages');
	var adults = fetch_object('air_adults');
	var children = fetch_object('air_children');
	var infants = fetch_object('air_infants');
	var allpeople = adults.selectedIndex + children.selectedIndex + infants.selectedIndex;

	if (allpeople > 9)
	{
		var focused = fetch_object('air_' + which);
		focused.selectedIndex = focused.selectedIndex + 9 - allpeople;
		alert('Malheureusement, seulement neuf billets sont disponibles à la fois. S’il vous en faut plus, veuillez appeler gratuitement un de nos agents au 1-866-GO-4-CHEAP.');
	}	
	/*if (infants.options.length > adults.selectedIndex + 1)
	{
		infants.options.length = adults.selectedIndex + 1;
		infants.selectedIndex = adults.selectedIndex;
	}*/
	for (var x = infants.selectedIndex + 1; x < 5 && infants.options.length < adults.selectedIndex + 1; x++)
	{
		infants.options[x] = new Option(x, x);
	}
	if (which == 'children')
	{
		for (var x = 2; x < 12; x++)
		{
			filloptions += '<option value="' + x + '"' + ( x == 11 ? ' selected="selected"' : '' ) + '>' + x + '</option>';
		}
		for (var x = 1; x <= children.selectedIndex; x++)
		{
			if (x == 1 || x == 4 || x == 7)
			{
				agepicks += '<br />';
			}
			
			agepicks += '<select name="ages[]">' + filloptions + '</select> ';
		}
	
		agebox.innerHTML = 'Âges des enfants:' + agepicks;
	}
	if (which == 'infants')
	{
		var depart = fetch_object('air_depart_city');
		var arrive = fetch_object('air_arrive_city');
		depart = depart.options[depart.selectedIndex].value;
		arrive = arrive.options[arrive.selectedIndex].value;
		var thecookie = fetch_air_cookie();

		if (infants.selectedIndex > 0 && (((( depart == '???' ? '' : depart ) + thecookie[1]).substr(0, 1) == 'Y') != ((( arrive == '???' ? '' : arrive ) + thecookie[3]).substr(0, 1) == 'Y')))
		{
			alert('Selectionner "enfants" peut limiter les résultats de vols. Vous pouvez faire une réservation sans enfants et en appelant un agent le même jour, vous pouvez les ajouter à votre réservation.');
		}
	}

	agebox.style.display = ( children.selectedIndex > 0 ? 'block' : 'none' );
}

function airline_useall()
{
	var airlines = fetch_tags(fetch_object('collapseobj_tsg_airlines'), 'input');

	if (airlines[0].checked)
	{
		for (var x = 1; x <= 60; x++)
		{
			airlines[x].checked = false;
		}
	}
	else
	{
		airlines[0].checked = true;
	}
}

function airline_select()
{
	var checked = 0;
	var airlines = fetch_tags(fetch_object('collapseobj_tsg_airlines'), 'input');

	for (var x = 1; x <= 60; x++)
	{
		if (airlines[x].checked)
		{
			airlines[0].checked = false;
			checked++;
		}
	}
	if (checked == 0)
	{
		airlines[0].checked = true;
	}
}

function read_date_dd(field)
{
	var value = new Date();
	var t_mons = fetch_object(field + '_month');
	var t_days = fetch_object(field + '_day');
	value.setFullYear(today.getFullYear(), t_mons.options[t_mons.selectedIndex].value, t_days.options[t_days.selectedIndex].value);

	if (value < today)
	{
		value.setFullYear(value.getFullYear() + 1);
	}

	return value;
}

function fetch_air_cookie()
{
	return ( fetch_cookie('air_form') ? fetch_cookie('air_form').split('|') : ['', '???', '', '???', build_timestamp(today.getMonth(), today.getDate(), 7), 0, 1, build_timestamp(today.getMonth(), today.getDate(), 14), 0, 1, 0, 0] );
}

function init_air_form(skipdates)
{
	if (typeof(nocitysuggest) == 'undefined')
	{
		setAutoCompleteExt('air_depart_text', 'air_depart_drop', 'http://www.cheapticketscanada.com/forum/cities.php');
		setAutoCompleteExt('air_arrive_text', 'air_arrive_drop', 'http://www.cheapticketscanada.com/forum/cities.php');
	}

	var temp = fetch_object('collapseobj_tsg_airlines');

	if (temp)
	{
		temp.style.display = 'none';
	}

	temp = fetch_object('collapseobj_tsg_air_advanced');

	if (temp)
	{
		temp.style.display = 'none';
	}

	var usedate = new Date();
	var datetime = new Date();
	var thecookie = fetch_air_cookie();

	if (typeof(nocitysuggest) == 'undefined')
	{
		fetch_object('air_depart_text').value = thecookie[0];
		fetch_object('air_arrive_text').value = thecookie[2];
	}
	else
	{
		var citydd = fetch_object('air_depart_city');

		for (var selind = 0; selind < citydd.options.length; selind++)
		{
			if (citydd.options[selind].value == thecookie[1])
			{
				citydd.selectedIndex = selind;
				fetch_ws_cities();
				citydd = fetch_object('air_arrive_city');

				for (var selind = 0; selind < citydd.options.length; selind++)
				{
					if (citydd.options[selind].value == thecookie[3])
					{
						citydd.selectedIndex = selind;
						break;
					}
				}

				break;
			}
		}
	}
	if (!skipdates)
	{
		datetime.setTime(thecookie[4] * 1000);
	
		if (datetime > usedate)
		{
			usedate = datetime;
		}
	
		fetch_object('air_leave_month').selectedIndex = usedate.getMonth();
		build_drop_days('air_leave');
		fetch_object('air_leave_day').selectedIndex = usedate.getDate() - 1;
	
		datetime.setTime(thecookie[7] * 1000);
	
		if (datetime > usedate)
		{
			usedate = datetime;
		}
	
		fetch_object('air_return_month').selectedIndex = usedate.getMonth();
		build_drop_days('air_return');
		fetch_object('air_return_day').selectedIndex = usedate.getDate() - 1;	
	}

	fetch_object('air_leave_time').selectedIndex = thecookie[5];
	fetch_object('air_return_time').selectedIndex = thecookie[8];
	fetch_object('air_roundtrip').checked = ( thecookie[6] == 1 ? true : false );
	toggle_air_roundtrip();

	fetch_object('air_adults').selectedIndex = thecookie[9];
	check_passengers('adults');
	fetch_object('air_children').selectedIndex = thecookie[10];
	check_passengers('children');
	fetch_object('air_infants').selectedIndex = thecookie[11];
	check_passengers('infants');
}