// test for DOM; if not avalable, it's a noop
if (typeof document.getElementById != 'undefined') {

// ASSEMBLE HTML
var calculatorHTML = '<img src="/img/calculator_instant_quote_headline2.gif" alt="Instant Quote" width="150" height="23">'; // title image
calculatorHTML += '<label for="instantQuoteTurnover">Calculate how much working capital you could release from your unpaid invoices. Enter the value of your current outstanding debtor book.</label>'; // instructions
calculatorHTML += '<input onkeypress="if (((typeof event.keyCode != \'undefined\') && (13 == event.keyCode )) || ((typeof event.which != \'undefined\') && (13 == event.which ))) return calculatorCalculate(this.id);" id="instantQuoteTurnover" name="instantQuoteTurnover">'; // input for turnover
calculatorHTML += '<a onclick="return calculatorCalculate(\'instantQuoteTurnover\');" href="#"><img src="/img/calculator_button.gif" alt="Calculate" width="63" height="22"></a>'; // activator button
calculatorHTML += '<div id="instantQuoteResults" class="calculatorResults">'; // results wrapper
calculatorHTML += 'You could release up to&hellip;'; // first line of results copy
calculatorHTML += '<div id="instantQuoteDisplay">&nbsp;</div>'; // result box
calculatorHTML += 'If you&rsquo;re interested in finding out more about our cash flow solutions call us on <strong>1300 850 322</strong>.'; // post-results instructions
calculatorHTML += '</div>'; // close results wrapper

// FIND CONTAINER
var calculatorContainer = document.getElementById('instantQuoteCalculator');

// INSERT HTML INTO CALCULATOR
calculatorContainer.innerHTML = calculatorHTML;

// CORRECT CONTAINER CLASS
calculatorContainer.className = 'calculator';

}

// calculates 90% of a supplied form field's value
function calculatorCalculate(id) {
	// get the input field
	var inputField = document.getElementById(id);
	// get turnover amount using .replace() and a simple regex to cull any non-digit or
	// decimal point characters then we're using parseFloat() to cast the results to a
	// floating point number
	var turnover = parseFloat(inputField.value.replace(/[^0-9.]/g,''));
	// if they've not supplied a 'real' number, complain
	if (isNaN(turnover)) {
		alert('Please enter your business\' annual turnover in $AUD in the field provided.');
	// if they've given us a 'real' number, take 85% and stuff it in the display
	}	else {
		// show them the 'cleaned' input in case it's not what they expected (e.g. if they're using
		// continental number formats like 1.332.943,00)
		inputField.value = turnover;
		// the easiest way to round to two decimal places is to multiply by 90 (rather than 0.90),
		// round to the nearest integer and then multiply by 0.01; we add an empty string to cast
		// the result to a string so we can go about padding the output with the necessary number
		// of zeros
		var factorAmount = '' + (0.01 * Math.round(turnover * 90));
		// find the decimal point, if any
		var decimalPos = factorAmount.indexOf('.');
		// if there's no decimal point, we don't need to worry...
		if (-1 != decimalPos) {
			// occasionally, JS gives us a truly bizarre number (i.e., 2312.000000001) despite
			// having rounded it off, so we need to just loose the superfluous digits if any
			if (factorAmount.length > decimalPos + 3) factorAmount = factorAmount.substr(0,decimalPos + 3);
			// otherwise, see if we need to pad out the zeroes; since we can only have 1 or 2 zeroes (if 
			// we'd had none, we wouldn't have had a decimal place and wouldn't have entered the 'if' bit)
			// we know that if we need to add any zeroes, we'll have to add exactly one
			else if (factorAmount.length < decimalPos + 3) factorAmount = factorAmount + '0';
		}
		document.getElementById('instantQuoteResults').className += ' activatedResults';
		document.getElementById('instantQuoteDisplay').innerHTML = '$' + factorAmount;
	}
	return false;
}
