
function getOfferFields(idImportedService)
{

	// collect offer profile fields
	var fields = {
		idImportedService:		idImportedService,
		supplierId:				$F('importedServicesList_'+idImportedService+'__supplier_'),
		//customerId:				$F('importedServicesList_'+idImportedService+'__passengers__0_'),
		commission: {},
		discount: {},
		serviceFee: {}
	};

	if ($('prices_commission__'+idImportedService+'_')) {
		fields.commission.value = $F('prices_commission__'+idImportedService+'__value_');
		fields.commission.currency = $F('prices_commission__'+idImportedService+'__currency_');
		fields.commission.commissionable = $F('prices_commission__'+idImportedService+'__commissionable_');
		fields.commission.checkboxProfile = $F('prices_commission__'+idImportedService+'__checkboxProfile_');
	}

	if ($('prices_discount__'+idImportedService+'_')) {
		fields.discount.value = $F('prices_discount__'+idImportedService+'__value_');
		fields.discount.currency = $F('prices_discount__'+idImportedService+'__currency_');
		fields.discount.checkboxProfile = $F('prices_discount__'+idImportedService+'__checkboxProfile_');
	}

	if ($('prices_serviceFee__'+idImportedService+'_')) {
		fields.serviceFee.value = $F('prices_serviceFee__'+idImportedService+'__value_');
		fields.serviceFee.currency = $F('prices_serviceFee__'+idImportedService+'__currency_');
		fields.serviceFee.checkboxProfile = $F('prices_serviceFee__'+idImportedService+'__checkboxProfile_');
	}
	return fields;

}


function displayOfferTotal(transport)
{

	// parse the data
	offerTotalData = transport.responseJSON;

	// check json
	if (!offerTotalData) {
		alert('The response is not JSON valid!');
		return 0;
	}

	//commission
	if ($('prices_commission__'+offerTotalData.idImportedService+'__profileDisplay') && offerTotalData.commissionValue) {
		$('prices_commission__'+offerTotalData.idImportedService+'__value_').value 		= offerTotalData.commissionValue;
		$('prices_commission__'+offerTotalData.idImportedService+'__currency_').value 	= offerTotalData.totalCurrency;
	}

	//discount
	if ($('prices_discount__'+offerTotalData.idImportedService+'__profileDisplay') && offerTotalData.discountValue) {
		$('prices_discount__'+offerTotalData.idImportedService+'__value_').value 		= offerTotalData.discountValue;
		$('prices_discount__'+offerTotalData.idImportedService+'__currency_').value 	= offerTotalData.totalCurrency;
	}

	//serviceFee
	if ($('prices_serviceFee__'+offerTotalData.idImportedService+'__profileDisplay') && offerTotalData.serviceFeeValue) {
		$('prices_serviceFee__'+offerTotalData.idImportedService+'__value_').value 		= offerTotalData.serviceFeeValue;
		$('prices_serviceFee__'+offerTotalData.idImportedService+'__currency_').value 	= offerTotalData.totalCurrency;
	}

	// total price
	$('prices_total__'+offerTotalData.idImportedService+'__value_').value 		= offerTotalData.totalValue;
	$('prices_total__'+offerTotalData.idImportedService+'__currency_').value 	= offerTotalData.totalCurrency;

	// update discount, serviceFee and commission profile display
	if ($('prices_discount__'+offerTotalData.idImportedService+'__profileDisplay')) {
		$('prices_discount__'+offerTotalData.idImportedService+'__profileDisplay').update(offerTotalData.discountProfileDisplay);
	}
	if ($('prices_serviceFee__'+offerTotalData.idImportedService+'__profileDisplay')) {
		$('prices_serviceFee__'+offerTotalData.idImportedService+'__profileDisplay').update(offerTotalData.serviceFeeProfileDisplay);
	}
	if ($('prices_commission__'+offerTotalData.idImportedService+'_')) {
		$('prices_commission__'+offerTotalData.idImportedService+'__profileDisplay').update(offerTotalData.commissionProfileDisplay);
	}

}

function refreshOfferTotal(idImportedService)
{
	var fields = getOfferFields(idImportedService);

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

	return fields;
}
