/*
 * display_children
 * hides/shows the children input boxes on the search page
 */
function display_children (amount, i)
{
	var child_ages = document.getElementById('child_ages' + i);
	var child_age1 = document.getElementById('child_age1' + i);
	var child_age2 = document.getElementById('child_age2' + i);
	var child_age3 = document.getElementById('child_age3' + i);
	var child_age4 = document.getElementById('child_age4' + i);

	// make them hidden by default
	child_ages.style.display = 'none';
	child_age1.style.display = 'none';
	child_age2.style.display = 'none';
	child_age3.style.display = 'none';
	child_age4.style.display = 'none';

	switch (amount)
	{
		case '4':
			child_age4.style.display = '';
		case '3':
			child_age3.style.display = '';
		case '2':
			child_age2.style.display = '';
		case '1':
			child_age1.style.display = '';
		default:
			child_ages.style.display = '';
	}
}


/*
 * display_rooms
 * hides/shows the rooms on the hotel search page
 */
function display_rooms (number)
{
	for (i = 1; i <= 8; i++)
	{
		var room = document.getElementById('room' + i);
		room.style.display = i > number ? 'none' : '';
	}
}


/*
 * open_window
 *  simple script to open a new pop-up window
 *   will return false when window blocked and true when successfully opened
 */
function open_window (url, name, width, height, check_if_opened)
{
	var the_window = window.open(url, name, 'width=' + width + ',' + 'height=' + height + 'top=50,left=50,scrollbars=1,titlebar=1,resizable=1');

	if (check_if_opened)
	{
		return (the_window == null || typeof(the_window) == 'undefined') ? false : true;
	}
	return false;
}


/*
 * close_window
 *  this closes a window and if given will change the parent window's location
 *
 */
function close_window (location)
{
	window.close();
	window.opener.location.href = location;
}


/*
 * show_by_id
 *  takes the id and puts the content of text into it
 */
function show_by_id (text, id)
{
	element = document.getElementById(id);

	element.innerHTML = eval(text);
}


/*
 * Shows a please wait image. Make sure you have a wait and page content div in your page
 */
function show_wait(wait_image)
{
	scroll(0,0);
	document.getElementById('page_content').style.display = 'none';
	document.getElementById('page_wait').style.visibility = 'visible';
	document.getElementById('page_wait').style.width = '450px';
	document.getElementById('page_wait').style.height = '150px';
	document.getElementById('page_wait').style.display = '';

	// IE fix
	if (document.all)
	{
		setTimeout("document.getElementById('wait_image').src = '" + wait_image + "';", 20);
	}
}


/*
 * hides the please wait image
 */
function hide_wait()
{
	if (document.getElementById('page_content'))
	{
		document.getElementById('page_content').style.display = '';
		document.getElementById('page_wait').style.display = 'none';
	}
}


/*
 * updates the lead passenger on the booking page
 */
lead_pax_edited = false;
function update_lead_pax(pax_name_el_root)
{
	if (lead_pax_edited)
	{
		return;
	}
	lead_pax = document.getElementById('lead_pax');
	if (! lead_pax)
	{
		return;
	}
	first_name = document.getElementById(pax_name_el_root + '[first_name]');
	if (! first_name)
	{
		return;
	}
	last_name = document.getElementById(pax_name_el_root + '[last_name]');
	if (! last_name)
	{
		return;
	}
	lead_pax.value = first_name.value + ' ' + last_name.value;
}


/*
 * updates the payment total on the booking page
 */
function update_payment_total (currency_code, itinerary_id)
{
	var total = 0;

	for (i = 0; i < itinerary_ids.length; i++)
	{
		var base = document.getElementById('base[' + itinerary_ids[i] + ']');
		var markup = document.getElementById('markups[' + itinerary_ids[i] + ']');
		var delivery = document.getElementById('delivery[' + itinerary_ids[i] + ']');
		var commissions = document.getElementById('commissions[' + itinerary_ids[i] + ']');
		if (base && base.value > 0)
		{
			total += parseFloat(base.value);
		}
		if (markup && markup.value > 0)
		{
			total += parseFloat(markup.value);
		}
		if (delivery && delivery_ids[itinerary_id][delivery.value] > 0)
		{
			total += parseFloat(delivery_ids[itinerary_id][delivery.value]);
		}
		if (commissions && commissions.value > 0)
		{
			total += parseFloat(commissions.value);
		}
	}

	// check for "single" commission field
	var commission = document.getElementById('commission');
	if (commission && commission.value > 0)
	{
		total += parseFloat(commission.value);
	}

	var payment_total = document.getElementById('payment_total');
	payment_total.innerHTML = '';
	payment_total.innerHTML = '$' + total.toFixed(2) + ' ' + currency_code;
}


/*
 * shows or hides the id's provided
 */
function show_hide (show_id, hide_id)
{
	if (show_id)
	{
		document.getElementById(show_id).style.display = '';
	}
	if (hide_id)
	{
		document.getElementById(hide_id).style.display = 'none';
	}
}


/*
 * will disable/enable the destination row on the air b2b page
 * toggled by the one way checkbox
 */
var toggle_one_way = false;
function air_toggle_one_way (toggle)
{
	if (!toggle)
	{
		toggle = toggle_one_way ? false : true;
	}

	document.getElementById('inbound_gateway').disabled = toggle;
	document.getElementById('inbound_destination').disabled = toggle;
	document.getElementById('inbound_day').disabled = toggle;
	document.getElementById('inbound_month[M]').disabled = toggle;
	document.getElementById('inbound_year[y]').disabled = toggle;
	document.getElementById('inbound_time[H]').disabled = toggle;

	toggle_one_way = toggle;
}


function check_toggle_status (type)
{
	if (type == 'b2b')
	{
		var element = document.getElementById('direction');
		if (element && element.checked)
		{
			air_toggle_one_way(true);
		}
	}
	else if (type == 'b2c')
	{
		var flight_types = new Array('roundtrip', 'oneway', 'multicity');
		for (i = 0; i < flight_types.length; i++)
		{
			var element = document.getElementById(flight_types[i]);
			if (element && element.checked)
			{
				refresh_b2c_air_search(element.value);
			}
		}
	}
}


/*
 * will update the field values on key down
 */
var active_field = null;
function air_b2b_onkeydown(field)
{
	active_field = field;
	field.lastValue = field.value;
}


/*
 * will move focus to the next field if what was typed has reached the number of characters provided
 */
function air_b2b_onkeyup(field, next_field, characters)
{
	next_field = document.getElementById(next_field);

	if (field == active_field && field.value != field.lastValue && field.value.length >= characters)
	{
		next_field.focus();
	}
	active_field = null;
}


/*
 * updates the next air field with airport code you have just typed
 */
function air_b2b_update_field(value, update_field)
{
	document.getElementById(update_field).value = value;
}


/*
 * take a given date and increase it by the number of days given
 */
function update_date(date, field_b, days, date_format)
{
	if (date_format == 'm/d/Y')
	{
		var field_date = parse_mmddyyyy(date);
		if (field_date)
		{
			field_date = field_date.setDate(field_date.getDate() + days);
			document.getElementById(field_b).value = calendar_generate_mmddyyyy(new Date(field_date));
		}
	}
	else if (date_format == 'd/m/Y')
	{
		var field_date = parse_ddmmyyyy(date);
		if (field_date)
		{
			field_date = field_date.setDate(field_date.getDate() + days);
			document.getElementById(field_b).value = calendar_generate_ddmmyyyy(new Date(field_date));
		}
	}
}


/*
 * based on the move command that is sent change the order of a select element
 */
function select_sort (move, select_id)
{
	var sort_select = document.getElementById(select_id);

	var items = new Array;
	var values = new Array;
	var index = sort_select.selectedIndex;

	var total = sort_select.options.length - 1;

	if (sort_select.selectedIndex == -1)
	{
		return false;
	}
	if (move == +1 && sort_select.selectedIndex == total)
	{
		return false;
	}
	if (move == -1 && sort_select.selectedIndex == 0)
	{
		return false;
	}

	for (i = total; i >= 0; i--)
	{
		items[i] = sort_select.options[i].text;
		values[i] = sort_select.options[i].value;
	}

	for (i = total; i >= 0; i--)
	{
		if (sort_select.selectedIndex == i)
		{
			sort_select.options[i + move] = new Option(items[i], values[i]);
			sort_select.options[i] = new Option(items[i + move], values[i + move]);

			i--;
		}
		else
		{
			sort_select.options[i] = new Option(items[i], values[i]);
		}
	}

	sort_select.selectedIndex = index + move;
	sort_select.focus();
}


/*
 * will remove any selected options of the passed in select element
 */
function select_remove (select_id)
{
	var remove_select = document.getElementById(select_id);

	for (var i = remove_select.length - 1; i >= 0; i--)
	{
		if (remove_select.options[i].selected)
		{
			remove_select.remove(i);
		}
	}
}


/*
 * will move the selected elements from one select to another
 */
function select_move (select_from_id, select_to_id)
{
	var from_select = document.getElementById(select_from_id);
	var to_select = document.getElementById(select_to_id);

	for (var i = from_select.length - 1; i >= 0; i--)
	{
		if (from_select.options[i].selected)
		{
			var new_option = document.createElement('option');
			new_option.text = from_select.options[i].text;
			new_option.value = from_select.options[i].value;

			var exists = false;
			for (var j = to_select.length - 1; j >= 0; j--)
			{
				if (from_select.options[i].value == to_select.options[j].value)
				{
					exists = true;
				}
			}

			if (!exists)
			{
				try
				{
					// standards compliant; doesn't work in IE
					to_select.add(new_option, null);
				}
				catch (ex)
				{
					// IE only
					to_select.add(new_option);
				}
			}
		}
	}
}


/*
 * will check all the option elements of the enabled and priority select boxes
 */
function check_all_options (options)
{
	for (var i = 0; i < options.length; i++)
	{
		var select_list = document.getElementById(options[i]);
		for (var j = select_list.length - 1; j >= 0; j--)
		{
			select_list.options[j].selected = true;
		}
	}

	return true;
}


/*
 * toggles the published fares on/off based on the fetch availability checkbox
 */
function toggle_published (checked, id)
{
	var element = document.getElementById('pub_consolidators_' + id);
	if (checked)
	{
		element.disabled = false;
	}
	else
	{
		element.disabled = true;
	}
}


/*
 * will expand the list of cities on the hotel search page when searching by country name
 */
function unhide_results()
{
	var element = document.getElementById('cities_list');

	for (var i = 0; i < element.childNodes.length; i++)
	{
		if (element.childNodes[i].tagName == 'DT')
		{
			element.childNodes[i].style.display = '';
		}
	}

	document.getElementById('expand_results').style.display = 'none';
}


/*
 * will hide the outbound/inbound search and show the multi-city search
 */
function refresh_b2c_air_search(flight_type)
{
	// make sure the search type is displaying correctly
	if (document.getElementById('advanced_search'))
	{
		if (document.getElementById('advanced_search').style.display == '')
		{
			document.getElementById('advanced_search').style.display = '';
			document.getElementById('basic_search').style.display = 'none';
		}
		else
		{
			document.getElementById('advanced_search').style.display = 'none';
			document.getElementById('basic_search').style.display = '';
		}
	}

	// one-way
	if (flight_type == 1)
	{
		document.getElementById('inbound').style.display = 'none';
		document.getElementById('outbound').style.display = 'none';
		document.getElementById('one_way').style.display = '';
		document.getElementById('multi_city').style.display = 'none';
	}
	// round trip
	else if (flight_type == 2)
	{
		document.getElementById('inbound').style.display = '';
		document.getElementById('outbound').style.display = '';
		document.getElementById('one_way').style.display = 'none';
		document.getElementById('multi_city').style.display = 'none';
	}
	// multi-city
	else if (flight_type == 3)
	{
		document.getElementById('inbound').style.display = 'none';
		document.getElementById('outbound').style.display = 'none';
		document.getElementById('one_way').style.display = 'none';
		document.getElementById('multi_city').style.display = '';
		// never display search switch link if doing multi-city
		if (document.getElementById('advanced_search'))
		{
			document.getElementById('advanced_search').style.display = 'none';
			document.getElementById('basic_search').style.display = 'none';
		}
	}

	return false;
}


/*
 * toggle between the two search types
 */
function toggle_search_type (type, origin, flights)
{
	// if we have no flights then they aren't usng the dropdown airport search
	if (flights == null)
	{
		return false;
	}

	var values = new Array;
	values['rt_from'] = origin.getElementById('flights[rt][from]').value;
	values['rt_to'] = origin.getElementById('flights[rt][to]').value;
	values['ow_from'] = origin.getElementById('flights[ow][from]').value;
	values['ow_to'] = origin.getElementById('flights[ow][to]').value;

	origin.getElementById('fields[rt][from]').innerHTML = flights[type]['rt']['from'];
	origin.getElementById('fields[rt][to]').innerHTML = flights[type]['rt']['to'];
	origin.getElementById('fields[ow][from]').innerHTML = flights[type]['ow']['from'];
	origin.getElementById('fields[ow][to]').innerHTML = flights[type]['ow']['to'];

	if (type == 'advanced')
	{
		origin.getElementById('advanced_search').style.display = 'none';
		origin.getElementById('basic_search').style.display = '';

		origin.getElementById('flights[rt][from]').value = values['rt_from'];
		origin.getElementById('flights[rt][to]').value = values['rt_to'];
		origin.getElementById('flights[ow][from]').value = values['ow_from'];
		origin.getElementById('flights[ow][to]').value = values['ow_to'];
	}
	else if (type == 'basic')
	{
		origin.getElementById('advanced_search').style.display = '';
		origin.getElementById('basic_search').style.display = 'none';
	}

	return false;
}


/*
 * show more rates on hotel results page
 */
function show_all_results(property_identifier)
{
	result = document.getElementById(property_identifier + '_more_results');
	if (result)
	{
		result.style.display = 'none';
	}

	result_index = 0;
	more_results = true;
	while (more_results)
	{
		result_index++;
		result = document.getElementById(property_identifier + '_' + result_index);
		if (result)
		{
			result.style.display = '';
		}
		else
		{
			more_results = false;
		}
	}
}


/*
 * add_table_row
 */
function add_table_row (table_id, field_label)
{
	var table = document.getElementById(table_id);
	var row_number = table.rows.length;

	var iteration = 0;
	for (i = 0; i <= table.rows.length; i++)
	{
		if (table.rows[i])
		{
			if (parseInt(table.rows[i].id.substring(3)) >= iteration)
			{
				iteration = parseInt(table.rows[i].id.substring(3)) + 1;
			}
		}
	}

	var new_row = table.insertRow(row_number);
	new_row.id = 'row' + iteration;

	// city mappings
	if (table_id == 'mappings')
	{
		new_row.className = iteration % 2 ? 'even' : 'odd';

		var left_cell = new_row.insertCell(0);
		var left_input = document.createElement('input');
		left_input.type = 'text';
		left_input.name = field_label + '[' + iteration + '][original_city_name]';
		left_input.size = 30;
		left_cell.appendChild(left_input);

		var middle_cell = new_row.insertCell(1);
		var middle_input = document.createElement('select');
		middle_input.name = field_label + '[' + iteration + '][correct_country_code]';
		middle_input.id = field_label + '[' + iteration + '][correct_country_code]';
		if (document.all)
		{
			middle_input.onchange = function(){ajax_get_cities(this.value, 'city_mappings[' + iteration + '][correct_city_id]', '?a=lookups&b=cities', 'GET');};
		}
		else
		{
			middle_input.setAttribute('onchange', "ajax_get_cities(this.value, 'city_mappings[" + iteration + "][correct_city_id]', '?a=lookups&b=cities', 'GET');");
		}
		middle_input.style.width = '250px';
		middle_cell.appendChild(middle_input);
		middle_cell.appendChild(document.createElement('br'));
		ajax_get_countries(field_label + '[' + iteration + '][correct_country_code]', '?a=lookups&b=countries', 'GET', false);

		var middle_input = document.createElement('select');
		middle_input.name = field_label + '[' + iteration + '][correct_city_id]';
		middle_input.id = field_label + '[' + iteration + '][correct_city_id]';
		middle_input.style.width = '250px';
		middle_cell.appendChild(middle_input);
		middle_cell.appendChild(document.createElement('br'));
	}
	// airport search dropdown
	else
	{
		new_row.className = table_id =='inbound' ? 'even' : 'odd';

		var left_cell = new_row.insertCell(0);
		left_cell.className = 'compress';
		var left_input = document.createElement('input');
		left_input.type = 'text';
		left_input.name = field_label + '[' + iteration + '][code]';
		left_input.size = 3;
		left_cell.appendChild(left_input);

		var middle_cell = new_row.insertCell(1);
		var middle_input = document.createElement('input');
		middle_input.type = 'text';
		middle_input.name = field_label + '[' + iteration + '][text]';
		middle_input.size = 25;
		middle_input.style.width = '98%';
		middle_cell.appendChild(middle_input);
	}

	var right_cell = new_row.insertCell(2);
	right_cell.className = 'icons';
	right_cell.innerHTML = '<a href="javascript: void(0);" onclick="remove_table_row(\'' + table_id + '\', \'row' + iteration + '\'); return false;"><img src="../images/minus.png" alt="Remove" /></a>';
}


/*
 * remove_table_row
 */
function remove_table_row (table_id, row_id)
{
	var table = document.getElementById(table_id);
	for (i = 0; i <= table.rows.length; i++)
	{
		if (table.rows[i] && table.rows[i].id == row_id)
		{
			table.deleteRow(i);
		}
	}
}

/**
 * give focus to an element by id, if it exists
 */
function focusEl(id)
{
	var el = document.getElementById(id);
	if (el)
	{
		el.focus();
	}
}

