/*-----------------------------------------------------------------------*/
/*- Currency formatting													-*/
/*-																		-*/
/*- Provides the functions to apply currency formatting to price values -*/
/*-																		-*/
/*- To use, apply the following methods in this order:					-*/
/*- addDecimals															-*/
/*- addThousand															-*/
/*- addCurrSymbol														-*/
/*-																		-*/
/*- The reason for this order is that the thousand function also checks -*/
/*- for a decimal separator and not vica-versa. Therefore, calling the	-*/
/*- functions in the incorrect order will cause errors.					-*/
/*-																		-*/
/*- Written By Matt Hall												-*/
/*- Conchango Plc for Figleaves											-*/
/*- 17-Sep-2001															-*/
/*-																		-*/
/*- Updates: (please indicate any modifications below)					-*/
/*-----------------------------------------------------------------------*/


function addDecimals(thisValue, hasDecimalSep, decimalSep, displayDecimals) {
/* Adds the decimal places and symbol to a numeric value and returns the result	*/
/*																				*/
/* thisValue: integer to format												*/
/* hasDecimalSep: boolean of whether currency has decimal separator				*/
/* decimalSep: char(1) of the decimal separator to apply						*/
/* displayDecimals: integer of how many decimal places are to apply				*/
/*																				*/
/* 17-Sep-2001 Matt Hall														*/
/* 23-Oct-2001 Owen Cutajar - changed to check that we need to deal with		*/
/*			   decimals															*/
/* 05-Dec-2001 Matt Hall - trim any excessive decimal places					*/

	if (hasDecimalSep) {

		// Check to see whether value already has a decimal point
		var decimalPos = thisValue.indexOf(decimalSep);

		// If no decimal point exists, append one to end and note this position
		if (decimalPos == -1) {	
			thisValue = thisValue + decimalSep;
			decimalPos = thisValue.length - 1;
		}

		// If the number of decimal places is more than required, trim
		if (thisValue.length - decimalPos - 1 > displayDecimals ) 
			thisValue = thisValue.slice(0,decimalPos + displayDecimals + 1);
		else {

			// Append zeros if we are not using the correct decimal places
			while (thisValue.length - decimalPos - 1 < displayDecimals ) {
				thisValue = thisValue + '0';
			}
		}		
	}
		
	return thisValue;
}


function addThousand(thisValue, hasThousandSep, thousandSep, decimalSep) {
/* Adds the thousand separators to the value if required.						*/
/*																				*/
/* thisValue: integer to format												*/
/* hasThousandSep: boolean of whether currency has thousand separator			*/
/* thousandSep: char(1) of the thousand separator to apply						*/
/* decimalSep: char(1) of thedecimal separator that already exists in value		*/
/*																				*/
/* 17-Sep-2001 Matt Hall														*/

	// Check that we need to deal with thousand separators
	if (hasThousandSep) {

			
		// Check to see whether the thousand separator has already been
		// inserted at some point. This should not be affetced even if the
		// decimal symbol is the same as the thousand.
		
		var thousandPos = thisValue.indexOf(thousandSep);
		

		if (thousandPos != -1) {
			
			// Set next slice postition
			var sliceAt = thousandPos - 3;
			if (sliceAt > 0) {

				currencyValue = currencyValue.slice(0,sliceAt) + thousandSep + currencyValue.slice(sliceAt);
						
				// Rercurse this function to add any more thousand separators
				currencyValue = addThousand(currencyValue);
				return currencyValue;
			}
				
		// Otherwise no thousand separator has been inserted
		} else {

			// We must check whether a decimal separator has been inserted
			var decimalPos = thisValue.indexOf(decimalSep);

			// If the decimal separator has not been added, use the length of
			// the value as the starting point
			if (decimalPos == -1) decimalPos = thisValue.length;
					
			// Position to start thousand insertion is 3 chars prior to either
			// the deimcal point, or end of value
			var sliceAt = decimalPos - 3;
					
			// If the slice position is valid
			if (sliceAt > 0) {

				thisValue = thisValue.slice(0,sliceAt) + thousandSep + thisValue.slice(sliceAt);
					
				// Rercurse this function to add any more thousand separators
				thisValue = addThousand(thisValue);
			}

			return thisValue;
		}
	}
	return thisValue;
}

function addCurrSymbol(currencyValue, currPos, currSymbol) {
/* Adds the currency symbol in the correct position for the value				*/
/*																				*/
/* currencyValue: integer to format												*/
/* currPos: 'left' or 'right' to indicate where the symbol appears				*/
/* currSymbol: the currency symbol to apply										*/
/*																				*/
/* 17-Sep-2001 Matt Hall														*/

	if (currPos == 'L') 
		currencyValue = trim(currSymbol) + currencyValue;
	else
		currencyValue = currencyValue + '&#160;' + trim(currSymbol);
	
	return currencyValue;
}


function trim(str)
/* Trims spaces from a string. For some reason (probably back-end) the currency */
/* symbol is being returned with padding.										*/
/*																				*/
/* str: String to trim															*/
/*																				*/
/* 06-Dec-2001 Matt Hall														*/

{
	var i = 0;
	var j = str.length - 1;

	while(str.charAt(i) == ' ') i++;
	while(str.charAt(j) == ' ') j--;
	j++;

	return str.substring(i,j);

}
