<!--

function return_flight_status(p_index, p_status)
{
	var return_day = document.getElementById('flight_day_of_return_travel[' + p_index + ']');
	var return_month = document.getElementById('flight_month_of_return_travel[' + p_index + ']');
	var return_year = document.getElementById('flight_year_of_return_travel[' + p_index + ']');

	if (p_status == 'true')
	{
		return_day.disabled = true;
		return_month.disabled = true;
		return_year.disabled = true;
	}
	else
	{
		return_day.disabled = false;
		return_month.disabled = false;
		return_year.disabled = false;
	}
}

function update_air_text(p_index, p_type)
{
	var type_text = document.getElementById(p_type + '_text[' + p_index + ']');
	var type_dd = document.getElementById(p_type + '_dd[' + p_index + ']');

	var dd_value = type_dd.options[type_dd.selectedIndex].value;

	if (!parseInt(dd_value))
	{
		type_text.value = dd_value;
	}

}

// Function not currently used <- called from the drop downs when created in dynamic_stop_offs()
function update_air_text_for_stop_offs(p_index, i, p_type)
{
	//var type_text = document.getElementById(p_type + '_text[' + p_index + '][' + i + ']');
	//var type_dd = document.getElementById(p_type + '_dd[' + p_index + '][' + i + ']');

	var position = p_index;

	var aValue = document.getElementById('stop_off_text[' + position + '][' + i + ']');

	if (aValue)
		alert("YES");
	else
		alert("NO " + i + " " + p_index + " " + p_type);

	if (!parseInt(i))
		alert(i + " is not an int");
	else
		alert(i + " is in fact " + i);

}


function add_component(p_component, p_result_row)
{
	var form_enquiry = document.getElementById('form_enquiry');

	var inputs = document.createElement('input');
	inputs.setAttribute('type', 'hidden');
	inputs.setAttribute('name', 'enquiry-type');
	inputs.setAttribute('value', 'advanced');

	form_enquiry.appendChild(inputs);

	var component_input = document.createElement('input');
	component_input.setAttribute('id', 'component');
	component_input.setAttribute('type', 'hidden');
	component_input.setAttribute('name', 'component');
	component_input.setAttribute('value', p_component);

	form_enquiry.appendChild(component_input);

	var enquiry_input = document.createElement('input');
	enquiry_input.setAttribute('type', 'hidden');
	enquiry_input.setAttribute('name', 'option');
	enquiry_input.setAttribute('value', 'enquiry');

	form_enquiry.appendChild(enquiry_input);

	var res_row_input = document.createElement('input');
	res_row_input.setAttribute('id', 'result_row');
	res_row_input.setAttribute('type', 'hidden');
	res_row_input.setAttribute('name', 'result_row');
	res_row_input.setAttribute('value', p_result_row);

	form_enquiry.appendChild(res_row_input);

	var command = document.getElementById('command_value');


	command.value = 'customer-lookup';

	form_enquiry.submit();

}

function dynamic_stop_offs(p_index)
{
	var position = p_index;

	var labelCell = document.getElementById('stop_off_label[' + position + ']');
	var airportsCell = document.getElementById('stop_off_airports[' + position + ']');

	var stopOffDropDown = document.getElementById('number_of_stop_offs[' + position + ']');
	var stopOffNumber = parseInt(stopOffDropDown.options[stopOffDropDown.selectedIndex].value);

	// Before Removing Blocks need to get all current values
	// loop needed

	var stopOffArray = new Array(stopOffNumber);

	for (var i = 0; i < stopOffNumber; i++)
	{
		var aValue = document.getElementById('stop_off_text[' + position + '][' + i + ']');
		if (aValue)
			stopOffArray[i] = aValue.value;
		else
			stopOffArray[i] = '';
	}


	var div_block = document.getElementById('stop_off_block[' + position + ']');

	// Clearing existing blocks
	airportsCell.removeChild(div_block);

	div_block = document.createElement('div');
	div_block.setAttribute('id', 'stop_off_block[' + position + ']');

	for (var i = 0; i < stopOffNumber; i++)
	{
		var input = document.createElement('input');
		input.setAttribute('id', 'stop_off_text[' + position + '][' + i + ']');
		input.setAttribute('name', 'stop_off_text[' + position + '][' + i + ']');
		input.setAttribute('type','text');
		input.setAttribute('value', '');
		input.setAttribute('maxlength', '3');
		input.setAttribute('size', '1');
		input.setAttribute('value', stopOffArray[i]);
		div_block.appendChild(input);

		// Currently not using the Drop down as well as wouldn't work right with update_airport_text_stop_off
		//var dd = document.createElement('select');
		//dd.setAttribute('id','stop_off_dd[' + position + '][' + i + ']');
		//dd.setAttribute('name','stop_off_dd[' + position + '][' + i + ']');
      //
		//for (var j = 0; j < 10; j++)
		//{
		//	var options = document.createElement('option');
		//	options.setAttribute('value', j);
		//	//options.createTextNode(j);
      //
		//	var optionText = document.createTextNode(j);
      //
		//	options.appendChild(optionText);
		//	dd.appendChild(options);
		//}
      //
		////dd.onclick = function () { update_air_text_for_stop_offs(position, i, 'stop_off');};
		//div_block.appendChild(dd);

		var breaker = document.createElement('br');
		breaker.setAttribute('id', position + '_break[' + i + ']');
		div_block.appendChild(breaker);
	}

	airportsCell.appendChild(div_block);

	if (!document.getElementById('stopOffLabel[' + position + ']'))
	{
		var label = document.createElement('label');
		label.setAttribute('id','stopOffLabel[' + position + ']');
		var labelText = document.createTextNode("Stop off places");

		label.appendChild(labelText);

		labelCell.appendChild(label);
	}
	else
	{
		if (stopOffNumber == 0)
		{
			labelCell.removeChild(document.getElementById('stopOffLabel[' + position + ']'));
		}
	}
}

function remove_component(p_index)
{
	var form_enquiry = document.getElementById('form_enquiry');

	var inputs = document.createElement('input');
	inputs.setAttribute('type', 'hidden');
	inputs.setAttribute('name', 'enquiry-type');
	inputs.setAttribute('value', 'advanced');

	form_enquiry.appendChild(inputs);

	var component_input = document.createElement('input');
	component_input.setAttribute('id', 'component');
	component_input.setAttribute('type', 'hidden');
	component_input.setAttribute('name', 'component');
	component_input.setAttribute('value', 'remove');

	form_enquiry.appendChild(component_input);

	var remove_index = document.createElement('input');
	remove_index.setAttribute('id', 'remove_index');
	remove_index.setAttribute('type', 'hidden');
	remove_index.setAttribute('name', 'remove_index');
	remove_index.setAttribute('value', p_index);

	form_enquiry.appendChild(remove_index);

	var enquiry_input = document.createElement('input');
	enquiry_input.setAttribute('type', 'hidden');
	enquiry_input.setAttribute('name', 'option');
	enquiry_input.setAttribute('value', 'enquiry');

	form_enquiry.appendChild(enquiry_input);

	var command = document.getElementById('command_value');

	command.value = 'customer-lookup';

	form_enquiry.submit();
}

function process_duration(p_index, p_from_duration)
{
	var start_day   = document.getElementById('cruise_start_day[' + p_index + ']');
	var start_month = document.getElementById('cruise_start_month[' + p_index + ']');
	var start_year  = document.getElementById('cruise_start_year[' + p_index + ']');

	var end_day   = document.getElementById('cruise_end_day[' + p_index + ']');
	var end_month = document.getElementById('cruise_end_month[' + p_index + ']');
	var end_year  = document.getElementById('cruise_end_year[' + p_index + ']');

	var duration  = document.getElementById('cruise_duration[' + p_index + ']');

	var start_date = new Date(start_year.value, start_month.value - 1, start_day.value);
	var end_date   = new Date(end_year.value, end_month.value - 1, end_day.value);

	var one_day=1000*60*60*24;

	var num_days = Math.ceil((end_date.getTime() - start_date.getTime()) / (one_day));

	if (!p_from_duration)
	{
		if (num_days < 0)
		{
			alert('Cannot select a date before the start date');
			num_days = 0;
			end_day.value   = start_day.value;
			end_month.value = start_month.value;
			end_year.value  = start_year.value;

			//set_end_date(start_day, start_month, start_year, start_date, 0);
		}
	}
	else
	{
		if (isNaN(duration.value))
		{
			alert('Must enter a number for the duration');
			num_days = 0;

			end_day.value   = start_day.value;
			end_month.value = start_month.value;
			end_year.value  = start_year.value;
		}
		else
		{
			end_date.setTime(start_date.getTime());
			end_date.setTime(end_date.getTime() + duration.value * one_day);

			num_days = duration.value;
			set_end_date(end_day, end_month, end_year, end_date);
		}
	}

	duration.value = num_days;
}

function set_end_date(p_end_day, p_end_month, p_end_year, p_obj_date)
{
	var m_obj_date = p_obj_date;
	p_end_day.value   = m_obj_date.getDate();
	p_end_month.value = m_obj_date.getMonth() + 1;
	p_end_year.value  = m_obj_date.getFullYear();
}

function change_block(p_type, p_index, p_num_of_type)
{
	//alert(p_type + ' ' + p_index + ' ' + p_num_of_type)

	for (var j = 1; j <= p_num_of_type; j++)
	{
		var m_block      = document.getElementById(p_type + '_display[' + j + ']');
		var m_block_tab  = document.getElementById(p_type + '_tab[' + j + ']');

		if (m_block && m_block_tab)
		{
			if (j == p_index)
			{
				if (navigator.appVersion.indexOf('MSIE') != -1)
					var display_type = 'block';
				else
					var display_type = 'table';

				m_block.style.display = display_type;
				m_block_tab.className = 'select-' + p_type + '-tab';
			}
			else
			{
				m_block.style.display = 'none';
				m_block_tab.className = 'unselect-' + p_type + '-tab';
			}
		}
	}
}

function select_pax_for_component(p_img, p_type, p_pax_id, p_component_id)
{
	var pax_component = document.getElementById('passenger_selected[' + p_pax_id + '][' + p_type + '][' + p_component_id + ']');

	if (pax_component)
	{
		if (pax_component.value == 0) // No for component
		{
			pax_component.value = 1;
			p_img.src = 'asset/images/kestrel/default/v2/selected.gif';
		}
		else
		{
			pax_component.value = 0;
			p_img.src = 'asset/images/kestrel/default/v2/unselected.gif';
		}
	}
	else
	{
		alert('cannot be found');
	}

}

function display_passenger_block()
{
	var passengerBlock = document.getElementById('passenger_block');

	if (passengerBlock)
	{
		if (passengerBlock.style.display == 'none')
			passengerBlock.style.display = 'block';
		else
			passengerBlock.style.display = 'none';
	}
}

function check_status(p_select)
{
	var reasons_label = document.getElementById('reasons_label');
	var reasons       = document.getElementById('reasons');

	if (p_select.value == 'Dead')
	{
		reasons_label.style.display = 'inline';
		reasons.style.display = 'inline';
	}
	else
	{
		reasons_label.style.display = 'none';
		reasons.style.display = 'none';
	}
}

function save_and_forward(p_url)
{
	var form_enquiry = document.getElementById('form_enquiry');

	var urlInput = document.createElement('input');
	urlInput.setAttribute('type', 'hidden');
	urlInput.setAttribute('name', 'url');
	urlInput.setAttribute('value', p_url);

	form_enquiry.appendChild(urlInput);
	form_enquiry.submit();
}

// --- Searching Functions ----
var mArrCounter  = new Array();
var mArrTimer    = new Array();
var mArrSearches = new Array();

function search(p_extra, pId)
{
	var mObjLiveSearch = new LiveSearch(p_extra, pId);
	mObjLiveSearch.buildParams();
	mObjLiveSearch.search();

	mExtra = p_extra;
	mId    = pId;

	mArrCounter[mId]  = 1;
	mArrSearches[mId] = mObjLiveSearch;
	mArrTimer[mId]    = setInterval('checkResults(mExtra, mId)', 1000);

}

function checkResults(pExtra, pId)
{
	var mObjLiveSearch = mArrSearches[pId];

	if (mObjLiveSearch.hasResults() || mArrCounter[pId] >= 15)
	{
		clearInterval(mArrTimer[pId]);

		if (mObjLiveSearch.hasResults())
		{
			var mResults        = mObjLiveSearch.getResponse();
			var mObjLiveResults = new LiveResult(mResults, pExtra, pId, mObjLiveSearch.getResponseType());
			mObjLiveResults.display();
		}
		else
		{
			var mObjLiveResults = new LiveResult('', pExtra, pId, 'text');
			mObjLiveResults.display();
		}
	}
	else
	{
		mArrCounter[pId]++;
	}
}

function calculate_total_price(pExtra, pId)
{
	var cost        = document.getElementById(pExtra + '_cost[' + pId + ']');
	var discount    = document.getElementById(pExtra + '_discount[' + pId + ']');
	var ins         = document.getElementById(pExtra + '_insurance[' + pId + ']');
	var booking_fee = document.getElementById(pExtra + '_booking_fee[' + pId + ']');
	var ccCharge    = document.getElementById(pExtra + '_cc_charge[' + pId + ']');

	var totalCost   = document.getElementById(pExtra + '_total_cost[' + pId + ']');

	if (cost && discount && ins && booking_fee && ccCharge && totalCost)
	{
		var costValue       = !isNaN(cost.value) ? Number(cost.value) : 0;
		var discountValue   = !isNaN(discount.value) ? Number(discount.value) : 0;
		var insValue        = !isNaN(ins.value) ? Number(ins.value) : 0;
		var bookingFeeValue = !isNaN(booking_fee.value) ? Number(booking_fee.value) : 0;
		var ccChargeValue   = !isNaN(ccCharge.value) ? Number(ccCharge.value) : 0;

		var totalValue = costValue + insValue + bookingFeeValue + ccChargeValue;
		totalValue = totalValue - discountValue;
		totalCost.value = totalValue;
	}
}

function forward_quote(pCustomerId, pEnquiryId, pMethod, pNumComponents)
{
	var mUrl = 'index.phtml?command=confirmation-setup&customer_id=' + pCustomerId + '&enquiry_id=' + pEnquiryId + '&method=' + pMethod + '&enquiry-type=advanced';

	for (var j = 0; j < pNumComponents; j++)
	{
		var mQuoteBlock = document.getElementById('quote_display[' + j + ']');
		var mUseForQuote = false;

		if (mQuoteBlock)
		{
			if (mQuoteBlock.checked == true)
				mUseForQuote = true;
		}

		mUrl += '&quote_display[' + j + ']=' + mUseForQuote;
	}

	window.location = mUrl;
}

var mArrShips = new Array();
var mArrShipNames = new Array();

function addShip(pCruiseLine, pShip, pShipName)
{
	if (!mArrShips[pCruiseLine])
	{
		mArrShips[pCruiseLine] = new Array();
	}

	mArrShips[pCruiseLine][mArrShips[pCruiseLine].length] = pShip;
	mArrShipNames[pShip] = pShipName;
}

function filterShips(pEnquiryId)
{
	var mObjCruiseLine = document.getElementById('cruise_line[' + pEnquiryId + ']');
	var mObjShip       = document.getElementById('cruise_ship[' + pEnquiryId + ']');

	if (mObjCruiseLine && mObjShip)
	{
		var mCruiseId = mObjCruiseLine.value;
		mObjShip.options.length = 0;

		if (mCruiseId)
		{
			if (mArrShips[mCruiseId])
			{
				if (mArrShips[mCruiseId].length > 0)
				{
					for (var j = 0; j < mArrShips[mCruiseId].length; j++)
					{
						mObjShip.options[j] = new Option(mArrShipNames[mArrShips[mCruiseId][j]], mArrShips[mCruiseId][j]);
					}
				}
			}
		}
	}

	document.getElementById('cruise_itinerary[' + pEnquiryId + ']').focus();
}

var mArrSources = new Array();
var mArrMaterials = new Array();

function addSource(pSource, pMaterial, pMaterialName)
{
	if (!mArrSources[pSource])
	{
		mArrSources[pSource] = new Array();
	}

	mArrSources[pSource][mArrSources[pSource].length] = pMaterial;
	mArrMaterials[pMaterial] = pMaterialName;
}

function filterSources()
{
	var mObjEnqSource   = document.getElementById('enquiry_source');
	var mObjEnqMaterial = document.getElementById('enquiry_material');

	if (mObjEnqSource && mObjEnqMaterial)
	{
		var mSourceId = mObjEnqSource.value;
		mObjEnqMaterial.options.length = 0;

		if (mSourceId)
		{
			if (mArrSources[mSourceId])
			{
				if (mArrSources[mSourceId].length > 0)
				{
					for (var j = 0; j < mArrSources[mSourceId].length; j++)
					{
						mObjEnqMaterial.options[j] = new Option(mArrMaterials[mArrSources[mSourceId][j]], mArrSources[mSourceId][j]);
					}
				}
			}
		}
	}
}

-->

