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

	var iteration = 0;
	for (var 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(cell_count++);
		var left_input = document.createElement('input');
		left_input.type = 'text';
		left_input.name = 'city_mappings[' + iteration + '][original_city_name]';
		left_input.size = 30;
		left_cell.appendChild(left_input);

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

		var middle_input = document.createElement('select');
		middle_input.name = 'city_mappings[' + iteration + '][correct_city_id]';
		middle_input.id = 'city_mappings[' + iteration + '][correct_city_id]';
		middle_input.style.width = '250px';
		middle_cell.appendChild(middle_input);
		middle_cell.appendChild(document.createElement('br'));
	}
	// airline platings
	else if (table_id == 'platings')
	{
		new_row.className = iteration % 2 ? 'even' : 'odd';

		var airline_dropdown = document.getElementById('airlines');
		var airline_code = airline_dropdown.options[airline_dropdown.selectedIndex].value

		var left_cell = new_row.insertCell(cell_count++);
		var left_input = document.createElement('input');
		left_input.type = 'hidden';
		left_input.value = airline_code;
		left_input.name = 'general_settings[air][airline_plating][airlines][]';
		left_cell.appendChild(left_input);
		left_cell.appendChild(document.createTextNode(airline_code));
	}
	// airport search dropdown
	else
	{
		new_row.className = table_id == 'inbound' ? 'even' : 'odd';

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

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

	var right_cell = new_row.insertCell(cell_count++);
	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/buttons/delete.png" alt="Remove" title="Remove" /></a>';
}


/*
 * check_all_options
 *  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;
}


/*
 * 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;
}


/*
 * 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_passenger_birthdates
 *  hides/shows the passegner birthdates on the insurance search page
 */
function display_passenger_birthdates (number)
{
	for (var i = 1; i <= 9; i++)
	{
		document.getElementById('pax' + i).style.display = i > number ? 'none' : '';
	}
}


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


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


/*
 * hide_wait
 *  hides the please wait image
 */
function hide_wait ()
{
	window.onpageshow = function (event)
	{
		if (event.persisted)
		{
			window.onload();
		}
	}

	if (document.getElementById('page_content'))
	{
		document.getElementById('page_content').style.display = '';
		document.getElementById('page_wait').style.display = '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;
}


/*
 * refresh_air_search
 *  this will toggle the display of the different flight types
 */
function refresh_air_search (flight_type, date_format, use_advanced)
{
	// toggle the advanced/basic search
	var use_basic_html = false;
	if (document.getElementById('basic_search') && document.getElementById('advanced_search'))
	{
		if (flight_type == 3)
		{
			document.getElementById('advanced_search').style.display = 'none';
			document.getElementById('basic_search').style.display = 'none';
		}
		else if (use_advanced || document.getElementById('flights[1][from][name]').type == 'select-one' || document.getElementById('flights[1][to][name]').type == 'select-one')
		{
			document.getElementById('advanced_search').style.display = 'none';
			document.getElementById('basic_search').style.display = '';
		}
		else
		{
			document.getElementById('advanced_search').style.display = '';
			document.getElementById('basic_search').style.display = 'none';
			use_basic_html = true;
		}
	}

	// set the flights html
	document.getElementById('flights').innerHTML = '';
	if (flight_type == 1) // one-way
	{
		document.getElementById('flights').innerHTML = use_basic_html ? oneway_select_html : oneway_html;
	}
	else if (flight_type == 2) // round trip
	{
		document.getElementById('flights').innerHTML = use_basic_html ? roundtrip_select_html : roundtrip_html;
	}
	else if (flight_type == 3) // multi-city
	{
		document.getElementById('flights').innerHTML = multi_city_html;
	}


	// create our calendar elements
	calendar_flight1 = new create_calendar_from_input(document.getElementById('flights[1][date]'), document.getElementById('flights[2][date]') ? document.getElementById('flights[2][date]') : null, date_format);
	if (document.getElementById('flights[2][date]'))
	{
		calendar_flight2 = new create_calendar_from_input(document.getElementById('flights[2][date]'), document.getElementById('flights[3][date]') ? document.getElementById('flights[3][date]') : null, date_format);
	}
	if (document.getElementById('flights[3][date]'))
	{
		calendar_flight3 = new create_calendar_from_input(document.getElementById('flights[3][date]'), document.getElementById('flights[4][date]') ? document.getElementById('flights[4][date]') : null, date_format);
	}
	if (document.getElementById('flights[4][date]'))
	{
		calendar_flight4 = new create_calendar_from_input(document.getElementById('flights[4][date]'), document.getElementById('flights[5][date]') ? document.getElementById('flights[5][date]') : null, date_format);
	}
	if (document.getElementById('flights[5][date]'))
	{
		calendar_flight5 = new create_calendar_from_input(document.getElementById('flights[5][date]'), document.getElementById('flights[6][date]') ? document.getElementById('flights[6][date]') : null, date_format);
	}
	if (document.getElementById('flights[6][date]'))
	{
		calendar_flight6 = new create_calendar_from_input(document.getElementById('flights[6][date]'), null, date_format);
	}

	return false;
}


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


/*
 * select_move
 *  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);
				}
			}
		}
	}
}


/*
 * select_remove
 *  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);
		}
	}
}


/*
 * select_sort
 *  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 (var i = total; i >= 0; i--)
	{
		items[i] = sort_select.options[i].text;
		values[i] = sort_select.options[i].value;
	}

	for (var 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();

	return true;
}


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

	var result_index = 0;
	var more_results = true;
	while (more_results)
	{
		result_index++;
		// check if we have a pricing row
		var price = document.getElementById(property_identifier + '_' + result_index + '_price');
		if (price)
		{
			price.style.display = '';
		}
		else
		{
			more_results = false;
		}
		// check for a spacer row
		var spacer = document.getElementById(property_identifier + '_' + result_index + '_spacer');
		if (spacer)
		{
			spacer.style.display = '';
		}
	}
}


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

	element.innerHTML = eval(text);
}


/*
 * show_hide
 *  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';
	}
}


/*
 * show_wait
 *  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';
	if (wait_image == 'images/wait.gif')
	{
		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);
	}
}


/*
 * toggle_hotels_search_type
 *  toggle between the two hotels search types
 */
function toggle_hotels_search_type (type, origin, destination)
{
	var destination_value = origin.getElementById('destination').value;

	origin.getElementById('destinations').innerHTML = destination;

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

		origin.getElementById('destination').value = destination_value;
	}
	else if (type == 'basic')
	{
		origin.getElementById('advanced_search').style.display = '';
		origin.getElementById('basic_search').style.display = 'none';
	}

	return false;
}


/*
 * toggle_published
 *  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;
	}
}


/*
 * unhide_results
 *  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';
}


/*
 * update_date
 *  take a given date and increase it by the number of days given
 */
function update_date (date, field_b, days, date_format)
{
	if (document.getElementById(field_b))
	{
		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));
			}
		}
	}
}


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


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

	if (payment_type == 'full')
	{
		for (var 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;
	}
	else if (payment_type == 'partial')
	{
		var commission_total = 0;
		var payment_total = 0;

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

		document.getElementById('total_payment').value = payment_total.toFixed(2);
		document.getElementById('total_commission').value = commission_total.toFixed(2);
	}
}
