/*<!---  
JAVASCRIPT FUNCTIONS FOR THE DYNAMIC RENTAL RATES.

The way this BOOLEAN is set can be tweaked as required. 
Currently this returns true except for NS4.
http://www.dhtmlcentral.com/projects/lib/examples.html?m=56 has a way to make NS4 work, but
its all a bit OTT for the time allowed
  --->*/
bDynamicCurSupported = (document.layers && !document.getElementById)?false:true;

/* This is a document.write() that only works if the dynamic rates calculation is supported */
function fnWriteIfDRR(txt) {
	if (bDynamicCurSupported) {
		document.write(txt);
	}
}
/* Add a rate to the js array, and create the TD */
function fnAddTd(rownum, amount, tdStub, aRates) {
	aRates[rownum] = amount;
	fnWriteIfDRR('<td align="center" id="' + tdStub + rownum + '">&nbsp;</td>');
}
/* This function does the conversion and writes the result to the table */
function fnSetFields(aPos, tdStub, aRates) {
	var i,thisid,mytd;
	
	if (bDynamicCurSupported) {
		for(i=0; i< aRates.length; i++) {
			thisid = tdStub + i;
			mytd = (document.all)? document.all[thisid] : document.getElementById(thisid);
			mytd.innerHTML = fnDisplayRate(aPos, aRates[i]);
		}
	}
}
/* This does more or less the same as t2_numericDisplayFormat(),  i.e does the rounding and adds some commas */
function fnDisplayRate(curPos, amount) {
	var thisAmount = amount * aCurs[curPos][0];
	var dspTxt;
	var separator = ',';
	var pos;
	if ((thisAmount == 0) || isNaN(thisAmount)) {
		return 'n/a';
	} else {	
		// the js calculation can result in 123.00000000001, which should round down.
		// therefore need to use this rather convoluted method 
		dspTxt = Math.ceil((Math.round(thisAmount * 1000))/1000);

		// Now format it with commas.
		dspTxt 	= dspTxt.toString();
		pos 	= dspTxt.length;

		while (pos > 0)	{
			pos -= 3;
			if (pos <= 0) break;
	
			dspTxt = dspTxt.substring(0, pos)
				+ separator
				+ dspTxt.substring(pos, dspTxt.length);
		}

		return aCurs[curPos][1] + '&nbsp;' + dspTxt;
	}
}
