
///////////////////////////////////////////////////////////////////////////////////////////////////
// AUTOMATICALLY COMPUTES THE SERVICE FEE IF IT IS PERCENTAGE (PERCENTS/VALUES/CURRENCIES) ////////
// INPUTS: idOffer and idOfferItem, trigger (supplierValue, supplierCurrency, commissionPercent, //
//         commissionValue, commissionCurrency, discountPercent, discountValue, discountCurrency) /
// OUTPUTS: FUNCTION DOES THE FOLLOWING: (for every trigger - it does something else) /////////////
//   - 'supplierValue', 'supplierCurrency', 'commissionPercent', 'commissionValue', ///////////////
//     'commissionCurrency', 'serviceFeePercent', 'serviceFeeCurrency', 'taxValue', ///////////////
//     'taxCurrency' //////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////

function computeOffer_serviceFee( idOffer, idOfferItem, trigger, offDet) {


	///////////////////////////////////////////////////////////////////////////////////////////////
	// INDICATES IF TAX FIELDS ARE DISPLAYED //////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////////////////////
	var taxFields=true;
	if (!offDet.taxValue || !offDet.taxCurrency) taxFields=false;



	///////////////////////////////////////////////////////////////////////////////////////////////
	// CHECK IF THE VALUE OF THE SERVICE FEE SHOULD BE CALCULATED FROM ITS PERCENT ////////////////
	///////////////////////////////////////////////////////////////////////////////////////////////
	calculatePercent=false;

	// IF SERVICE FEE MODE IS 'percent' ///////////////////////////////////////////////////////////
	if (offDet.serviceFeeMode=='percent') {

		// IF SERVICE FEE SOURCE IS 'servicePrice' ------------------------------------------------
		if (offDet.serviceFeePercentSource=='servicePrice') {

			// COMMISSIONABLE PRICE ---------------------------------------------------------------
			if (offDet.commissionable) {
				if (offDet.serviceFeeCurrency.value==offDet.supplierCurrency.value)
					calculatePercent=true;
				else
					calculatePercent=false;
			}

			// NET PRICE --------------------------------------------------------------------------
			else {
				if (offDet.serviceFeeCurrency.value==offDet.supplierCurrency.value && offDet.serviceFeeCurrency.value==offDet.commissionCurrency.value)
					calculatePercent=true;
				else
					calculatePercent=false;
			}

		}

		// IF SERVICE FEE SOURCE IS 'servicePrice+tax' --------------------------------------------
		if (offDet.serviceFeePercentSource=='servicePrice+tax') {

			// COMMISSIONABLE PRICE ---------------------------------------------------------------
			if (offDet.commissionable) {
				// if tax fields are visible
				if (taxFields) {
					if (offDet.serviceFeeCurrency.value==offDet.supplierCurrency.value && offDet.taxCurrency.value==offDet.supplierCurrency.value)
						calculatePercent=true;
					else
						calculatePercent=false;
				}
				// if tax fields are not visible
				else {
					if (offDet.serviceFeeCurrency.value==offDet.supplierCurrency.value)
						calculatePercent=true;
					else
						calculatePercent=false;
				}
			}

			// NET PRICE --------------------------------------------------------------------------
			else {
				// if tax fields are visible
				if (taxFields) {
					if (offDet.serviceFeeCurrency.value==offDet.supplierCurrency.value && offDet.serviceFeeCurrency.value==offDet.commissionCurrency.value && offDet.taxCurrency.value==offDet.supplierCurrency.value)
						calculatePercent=true;
					else
						calculatePercent=false;
				}
				// if tax fields are not visible
				else {
					if (offDet.serviceFeeCurrency.value==offDet.supplierCurrency.value && offDet.serviceFeeCurrency.value==offDet.commissionCurrency.value)
						calculatePercent=true;
					else
						calculatePercent=false;
				}
			}

		}

	}
	// IF SERVICE FEE MODE IS 'percent' - don't calculate anything ////////////////////////////////
	else {
		calculatePercent=false;
	}

	///////////////////////////////////////////////////////////////////////////////////////////////
	// ENABLE / DISABLE SERVICE FEE PERCENT FOR THIS OFFER ITEM ///////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////////////////////
	if (calculatePercent)
		offDet.serviceFeePercent.className='formFields';
	else
		offDet.serviceFeePercent.className='formFieldsDisabled';

	// IF PERCENT AND VALUE ARE NOT CORRELATED - EXIT FUNCTION ------------------------------------
	if (!calculatePercent) return ;

	///////////////////////////////////////////////////////////////////////////////////////////////
	// ######################################################################################### //
	// ######################################################################################### //
	// # TRIGGER = 'supplierValue', 'supplierCurrency', 'commissionPercent', ################### //
	// #           'commissionValue', 'commissionCurrency', 'serviceFeePercent', ############### //
	// #           'serviceFeeCurrency', 'taxValue', 'taxCurrency' ############################# //
	// ######################################################################################### //
	// ######################################################################################### //
	///////////////////////////////////////////////////////////////////////////////////////////////

	// CALCULATE SERVICE FEE VALUE ////////////////////////////////////////////////////////////////
	if (
		trigger=='supplierValue' || trigger=='supplierCurrency' || trigger=='commissionPercent' || trigger=='commissionValue' || trigger=='commissionCurrency' ||
		trigger=='serviceFeePercent' || trigger=='serviceFeeCurrency' ||
		trigger=='taxValue' || trigger=='taxCurrency'
	) {

		// PERCENT SOURCE = servicePrice //////////////////////////////////////////////////////////
		if (offDet.serviceFeePercentSource=='servicePrice') {
			// COMMISSIONABLE PRICE ---------------------------------------------------------------
			if (offDet.commissionable) {
				offDet.serviceFeeValue.value=round(offDet.supplierValue.value*offDet.serviceFeePercent.value/100, precision);
			}
			// NET PRICE --------------------------------------------------------------------------
			else {
				offDet.serviceFeeValue.value=round((transformToPrice(offDet.supplierValue.value)+transformToPrice(offDet.commissionValue.value))*offDet.serviceFeePercent.value/100, precision);
			}
		}

		// PERCENT SOURCE = servicePrice+tax //////////////////////////////////////////////////////
		if (offDet.serviceFeePercentSource=='servicePrice+tax') {
			// COMMISSIONABLE PRICE ---------------------------------------------------------------
			if (offDet.commissionable) {
				// if tax fields are visible
				if (taxFields) {
					offDet.serviceFeeValue.value=round( (transformToPrice(offDet.supplierValue.value)+transformToPrice(offDet.taxValue.value))*offDet.serviceFeePercent.value/100, precision);
				} else {
					offDet.serviceFeeValue.value=round( (transformToPrice(offDet.supplierValue.value))*offDet.serviceFeePercent.value/100, precision);
				}
			}
			// NET PRICE --------------------------------------------------------------------------
			else {
				// if tax fields are visible
				if (taxFields) {
					offDet.serviceFeeValue.value=round( (transformToPrice(offDet.supplierValue.value)+transformToPrice(offDet.commissionValue.value)+transformToPrice(offDet.taxValue.value))*offDet.serviceFeePercent.value/100, precision);
				} else {
					offDet.serviceFeeValue.value=round( (transformToPrice(offDet.supplierValue.value)+transformToPrice(offDet.commissionValue.value))*offDet.serviceFeePercent.value/100, precision);
				}
			}
		}

	}


	///////////////////////////////////////////////////////////////////////////////////////////////
	// ######################################################################################### //
	// ######################################################################################### //
	// # TRIGGER = 'serviceFeeValue' ########################################################### //
	// ######################################################################################### //
	// ######################################################################################### //
	///////////////////////////////////////////////////////////////////////////////////////////////

	// CALCULATE SERVICE FEE PERCENT //////////////////////////////////////////////////////////////
	if ( trigger=='serviceFeeValue' ) {


		// PERCENT SOURCE = servicePrice //////////////////////////////////////////////////////////
		if (offDet.serviceFeePercentSource=='servicePrice') {
			// COMMISSIONABLE PRICE ---------------------------------------------------------------
			if (  offDet.commissionable ) {
				if (offDet.supplierValue.value)
					offDet.serviceFeePercent.value=round( transformToPrice(offDet.serviceFeeValue.value/offDet.supplierValue.value*100), precision);
				else
					offDet.serviceFeePercent.value=0;
			}
			// NET PRICE --------------------------------------------------------------------------
			else {
				if ( transformToPrice(offDet.supplierValue.value)+transformToPrice(offDet.commissionValue.value) )
					offDet.serviceFeePercent.value=round( transformToPrice( offDet.serviceFeeValue.value / (transformToPrice(offDet.supplierValue.value) + transformToPrice(offDet.commissionValue.value)) * 100 ) , 2 );
				else
					offDet.serviceFeePercent.value=0;
			}
		}

		// PERCENT SOURCE = servicePrice+tax //////////////////////////////////////////////////////////
		if (offDet.serviceFeePercentSource=='servicePrice+tax') {
			// COMMISSIONABLE PRICE ---------------------------------------------------------------
			if (  offDet.commissionable ) {
				// if tax fields are visible
				if (taxFields) {
					if (transformToPrice(offDet.supplierValue.value)+transformToPrice(offDet.taxValue.value))
						offDet.serviceFeePercent.value=round( offDet.serviceFeeValue.value / (transformToPrice(offDet.supplierValue.value)+transformToPrice(offDet.taxValue.value))*100, precision);
					else
						offDet.serviceFeePercent.value=0;
				} else {
					if (transformToPrice(offDet.supplierValue.value))
						offDet.serviceFeePercent.value=round( offDet.serviceFeeValue.value / (transformToPrice(offDet.supplierValue.value))*100, precision);
					else
						offDet.serviceFeePercent.value=0;
				}
			}
			// NET PRICE --------------------------------------------------------------------------
			else {
				// if tax fields are visible
				if (taxFields) {
					if ( transformToPrice(offDet.supplierValue.value)+transformToPrice(offDet.commissionValue.value)+transformToPrice(offDet.taxValue.value) )
						offDet.serviceFeePercent.value=round( offDet.serviceFeeValue.value / (transformToPrice(offDet.supplierValue.value)+transformToPrice(offDet.commissionValue.value)+transformToPrice(offDet.taxValue.value))*100, precision);
					else
						offDet.serviceFeePercent.value=0;
				} else {
					if (transformToPrice(offDet.supplierValue.value)+transformToPrice(offDet.commissionValue.value))
						offDet.serviceFeePercent.value=round( offDet.serviceFeeValue.value / (transformToPrice(offDet.supplierValue.value)+transformToPrice(offDet.commissionValue.value))*100, precision);
					else
						offDet.serviceFeePercent.value=0;
				}
			}
		}


	}

}
