 //javascript for stamp duty calculations on calc_stamp.html page
 
 
 //round value based on passed in precision value
 function stamp_rnd(value, precision)
 {
	value = "" + value;  // convert value to string
	precision = parseInt(precision); 					
	var whole = "" + Math.round(value * Math.pow(10, precision));
	var dPoint = whole.length - precision;
	
	if(dPoint != 0) 
	{
		result  = whole.substring(0, dPoint);
		result += ".";                         
		result += whole.substring(dPoint, whole.length);
	}                                                     
	else 
	{
		result = whole;
	}

	return result;      
}

				
function stamp_checkInput(str) 
{
	//  take out characters not [0-9] or fullstop
	var digits = "0123456789.";
	var s  = "";
	
	for (i = 0;i < str.length;i++)
	{
		var c = str.charAt(i);

		if (digits.indexOf(c) > -1)						 
			s = s + c;
		else
			errorMsg = str + " is not a number.";
    }

	return parseFloat(s);
}


//validate fields and do calculations
function stamp_calculate()
{
	var propertyPrice;
	var constant = 5400;	
	var temp = 0;				
	var errorMsg = "";
						
					
	if (document.getElementById('propertyPrice'))
		propertyPrice = document.getElementById('propertyPrice').value;
	else
		errorMsg = "Property price could not be found.\n";
						
		
	//if not errors do calculations	
	//formula is 3 percent of property prices minus $5200				
	if (errorMsg == "")					
	{			
		var stampAmountValue = document.getElementById('stampAmountValue');
						
		temp = ((propertyPrice * .03) - 5400) + 2;
		
		if (temp > 0)
			stampAmountValue.innerText = temp;
		else
			stampAmountValue.innerText = 0.00		   
		                
        //format values
        stampAmountValue.innerText = stamp_formatCurrency(stampAmountValue.innerText);
               
		return false;					
	}
	else
	{
		alert(errorMsg);
	}
}


function stamp_formatCurrency(inputNum) 
{
	inputNum = inputNum.toString().replace(/\$|\,/g,'');
	
	//check if a number or not
	if(isNaN(inputNum))
		num = "0";
		
	sign = (inputNum == (inputNum = Math.abs(inputNum)));
	inputNum = Math.floor(inputNum*100+0.50000000001);
	cents = inputNum%100;
	inputNum = Math.floor(inputNum/100).toString();
	
	if(cents < 10)
		cents = "0" + cents;
		
	for (var i = 0; i < Math.floor((inputNum.length-(1+i))/3); i++)
		inputNum = inputNum.substring(0,inputNum.length-(4*i+3))+','+ inputNum.substring(inputNum.length-(4*i+3));
		
	return (((sign)?'':'-') + '$' + inputNum + '.' + cents);
}