
/**
 * Updates the destination and the category fields after the supplier product is set.
 * @param autocompleteData
 * @returns {Boolean}
 */
function updateSupplierProductDetails(selectedItem)
{
	// if there is no autocomplete data - skip this step
	if (!selectedItem) {
		return true;
	}

	// UPDATE THE DESTINATION FIELD ---------------------------------------------------------------
	if (parseInt(selectedItem.destinationCityId) && $('destination')) {
		FormField.setValue(null, 'Engine/Form/Input/Autocomplete', null, 'destination', {
			displayValue: selectedItem.destinationCityName,
			value: selectedItem.destinationCountryCode+'|'+selectedItem.destinationCityId
		});

		_inputValidator.triggerRegisteredValidatorsForContainer($('destination').up());
	} else {
		if (selectedItem.destinationCountryCode && $('destination')) {
			FormField.setValue(null, 'Engine/Form/Input/Autocomplete', null, 'destination', {
				displayValue: selectedItem.destinationCountryName,
				value: selectedItem.destinationCountryCode+'|0'
			});

			_inputValidator.triggerRegisteredValidatorsForContainer($('destination').up());
		}
	}
	
	// UPDATE THE CATEGORY FIELD ------------------------------------------------------------------
	if (selectedItem.category && $('serviceCategory_'+selectedItem.category+'_')) {
		$('serviceCategory_'+selectedItem.category+'_').checked=true;
	}
}


/**
 * This is triggered anytime the departure/destination is changed and it makes a request to determine
 * the real location.
 */
function refreshOfferLocation()
{

	var serviceCode, dept, dest, idImportedService;

	// get service code
	if ($('serviceCode')) {
		serviceCode = $F('serviceCode');
	} else {
		serviceCode = '';
	}

	if ($('departure')) {
		dept = $F('departure');
	} else {
		dept='int';
	}

	if ($('destination')) {
		dest = $F('destination');
	} else {
		dest='int';
	}

	if ($('idImportedService')) {
		idImportedService = $F('idImportedService');
	} else {
		idImportedService = 0;
	}
	
	
	// collect the fields
	var fields = { serviceCode: serviceCode, departure: dept, destination: dest, idImportedService: idImportedService };

	// do the request
	offerLocationTransport = new Ajax.Request( MVC.appUrl('Orders/Services', 'offerLocation', fields) , {
		method: 'get',
		onSuccess:	updateOfferLocation.bind(null),
		parameters:	fields
	});

}

/**
 * The actual update of the offer location field; scans the selector options and selects the first
 * element on the criterion
 * @param transport
 */
function updateOfferLocation(transport)
{

	var responseJson = transport.responseJSON;

	// select the correct calculation
	if (responseJson.criterion) {
		$('offerCalculation').setValue( responseJson.criterion + '|' + responseJson.calculationCode );
	}
	
	// also loads the currencies for this selected currency
	updateOfferCurrenciesInForm(responseJson.offerCurrency, responseJson.serviceFeeCurrency, responseJson.totalCurrency, responseJson.currencyType);
	
}


/**
 * Refreshes offer currencies (if no values set).
 * @returns
 */
function refreshOfferCurrencies()
{
	
	var serviceCode, offerCriterion, offerCalculation;

	// get service code
	if ($('serviceCode')) {
		serviceCode = $F('serviceCode');
	} else {
		serviceCode = '';
	}

	if ($('offerCalculation')) {
		var calcComponents = $F('offerCalculation').split('|');
		offerCriterion = calcComponents[0];
		offerCalculation = calcComponents[1];
	}

	// collect the fields
	var fields = { serviceCode: serviceCode, offerCriterion: offerCriterion, offerCalculation: offerCalculation };

	// do the request
	offerCurrenciesTransport = new Ajax.Request( MVC.appUrl('Orders/Services', 'offerCurrencies', fields) , {
		method: 'get',
		onSuccess:	updateOfferCurrencies.bind(null),
		parameters:	fields
	});
	
}


/**
 * The actual update of the offer location fields. 
 * @param transport
 * @returns
 */
function updateOfferCurrencies(transport)
{
	updateOfferCurrenciesInForm(transport.responseJSON.offerCurrency, transport.responseJSON.serviceFeeCurrency, transport.responseJSON.totalCurrency, transport.responseJSON.currencyType);
}

/**
 * The actual update of the form. 
 * @param offerCurrency
 * @param totalCurrency
 * @returns
 */
function updateOfferCurrenciesInForm(offerCurrency, serviceFeeCurrency, totalCurrency, currencyType)
{
	// set total currency
	if (totalCurrency) {
		$('offerTotalCurrency').setValue(totalCurrency);
	}
	
	// set the currency for all components 
	if (offerCurrency) {
		if ($('offerServicePrice_currency_') && $('offerServicePrice_value_').value.length==0) {
			$('offerServicePrice_currency_').setValue(offerCurrency);
		}
		if ($('offerCommission_currency_') && $('offerCommission_value_').value.length==0) {
			$('offerCommission_currency_').setValue(offerCurrency);
		}
		if ($('offerDiscount_currency_') && $('offerDiscount_value_').value.length==0) {
			$('offerDiscount_currency_').setValue(offerCurrency);
		}
		if ($('offerServiceFee_currency_') && $('offerServiceFee_value_').value.length==0) {
			$('offerServiceFee_currency_').setValue(serviceFeeCurrency);
		}
		if ($('offerTax_currency_') && $('offerTax_value_').value.length==0) {
			$('offerTax_currency_').setValue(offerCurrency);
		}
	}
	
	// update the offer currency type
	if (currencyType) {
		$('offerCurrencyType').setValue(currencyType);
	}
	
	
}


/**
 * Returns the first defined passenger - to be used in GDS profiles.
 * @returns
 */
function getFirstDefinedPax()
{

	// get from existing passengers (chackboxes) 
	var existingPsg = $$('div#definedPax input[name^="definedPax["]:checked').first();
	if (existingPsg) {
		return existingPsg.value
	}
	
	// get from new passengers (autocomplete fields)
	var newPsg = $$('div.clonedAutocompleteCustomer input[name^="newCustomers["]').filter(elem=>elem.value>0).first();
	if (newPsg) {
		return newPsg.value
	}

	// nothing found - just return 0
	return 0;
	
}
