/*
##############################################################################################
##############################################################################################
##########                                                                           #########
########## THIS CODE IS COPYRIGHT OF IWOLRD CREATIVE - http://www.iworldcreative.com #########
########## USE OR REPLICATION OF THIS CODE IN WHOLE OR IN PART WITHOUT THE PRIOR     #########
########## WRITTEN PERMISSION OF IWORLD CREATIVE IS STRICTLY FORBIDDEN.              #########
##########                                                                           #########
##############################################################################################
##############################################################################################
*/

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

<!-- Begin
function stringFilter (price) {
	s = price;
	filteredValues = "1234567890";     // Characters stripped out
	var i;
	var returnString = "";
	for (i = 0; i < s.length; i++) {  // Search through string and append to unfiltered values to returnString.
		var c = s.charAt(i);
		if (filteredValues.indexOf(c) != -1) returnString += c;
	}
	if (returnString.length > 0){
		document.calculator.sale_price.value = '$'+ addCommas(parseFloat(returnString));
		
		}else{
		document.calculator.sale_price.value = '$'+  addCommas(returnString);
		}
		return returnString;
}
//  End -->


function calc_fees(){

	var sellerprice = document.calculator.sale_price.value;
	sellerprice = stringFilter(sellerprice);
	//alert(sellerprice);
	
	
	//calculate the seller's fees
	var sellertotalcalc = 566;
	if (sellerprice <= 150000){
		sellertotalcalc = sellertotalcalc;
		}
	if (sellerprice > 150000 && sellerprice <= 500000){
		sellertotalcalc = 566+((sellerprice-150000)*0.00143);
		}
	if (sellerprice > 500000 && sellerprice <= 1000000){
		sellertotalcalc = 1066.5+((sellerprice-500000)*0.00110);
		}
	if (sellerprice > 1000000){
		sellertotalcalc = 1616.5+((sellerprice-1000000)*0.00077);
		}
	
	//calculate the buyer's fees	
	var buyertotalcalc = 849;
	if (sellerprice <= 150000){
		buyertotalcalc = buyertotalcalc;
		}
	if (sellerprice > 150000 && sellerprice <= 500000){
		buyertotalcalc = 849+((sellerprice-150000)*0.0022);
		}
	if (sellerprice > 500000 && sellerprice <= 1000000){
		buyertotalcalc = 1619+((sellerprice-500000)*0.00165);
		}
	if (sellerprice > 1000000){
		buyertotalcalc = 2444+((sellerprice-1000000)*0.0011);
		}
	
	//calculate the transfer fees	
	var transfercalc = 110;
	if (sellerprice <= 85000){
		transfercalc = transfercalc;
		}
	if (sellerprice > 85000 && sellerprice <= 120000){
		transfercalc = 120;
		}
	if (sellerprice > 120000 && sellerprice <= 200000){
		transfercalc = 140;
		}
	if (sellerprice > 200000){
		var roundsellerprice = Math.floor(sellerprice/100000)*100000;
		transfercalc = 160+((roundsellerprice-200000)/5000);
		}
	
	
	
	
	
	if(document.calculator.strata.checked == true){
	sellertotalcalc += 55;
	buyertotalcalc += 110;
	
	}
	
	//calculate duty for FHO
	if(document.calculator.fho.checked == true){
		var dutycalc = 0;
		//for vacant land
		if(document.calculator.vl.checked == true){
			var dutycalc = (Math.ceil((sellerprice-300000)/100))*13.01;
			
		}
		else{
		//for house and land
			var dutycalc = (Math.ceil((sellerprice-500000)/100))*22.51;
		}
		
		if (dutycalc <0){ dutycalc = 0;}
	}
	//calculate non-FHO fees 
	else{
		//for occupiers
		if(document.calculator.occupier.checked == true){
			var dutycalc = sellerprice*0.019;

			if (sellerprice <= 120000){
				dutycalc = dutycalc;
				}
			if (sellerprice > 120000 && sellerprice <= 150000){
				dutycalc = 2280+((sellerprice-120000)*0.0285);
				}
			if (sellerprice > 150000 && sellerprice <= 360000){
				dutycalc = 3135+((sellerprice-150000)*0.0380);
				}
			if (sellerprice > 360000 && sellerprice <= 725000){
				dutycalc = 11115+((sellerprice-360000)*0.0475);
				}
			if (sellerprice > 725000){
				dutycalc = 28452.5+((sellerprice-725000)*0.0515);
				}
			
		}
		else{
		//for non-occupiers
			var dutycalc = sellerprice*0.019;

			if (sellerprice <= 80000){
				dutycalc = dutycalc;
				}
			if (sellerprice > 80000 && sellerprice <= 100000){
				dutycalc = 1520+((sellerprice-80000)*0.0285);
				}
			if (sellerprice > 100000 && sellerprice <= 250000){
				dutycalc = 2090+((sellerprice-100000)*0.0380);
				}
			if (sellerprice > 250000 && sellerprice <= 500000){
				dutycalc = 7790+((sellerprice-250000)*0.0475);
				}
			if (sellerprice > 500000){
				dutycalc = 19665+((sellerprice-500000)*0.0515);
				}
		}
		
		if (dutycalc <0){ dutycalc = 0;}
	}
	
	
		var buyergst=buyertotalcalc * 0.1;
		var sellergst=sellertotalcalc * 0.1;
		
		document.calculator.sellertotal.value = '$' + addCommas(sellertotalcalc.toFixed(2));
		document.calculator.buyertotal.value = '$' + addCommas(buyertotalcalc.toFixed(2));
		document.calculator.transferfees.value = '$' + addCommas(transfercalc.toFixed(2));
		document.calculator.dutytotal.value = '$' + addCommas(dutycalc.toFixed(2));
		document.calculator.buyergst.value = '$' + addCommas(buyergst.toFixed(2));
		document.calculator.sellergst.value = '$' + addCommas(sellergst.toFixed(2));
		document.calculator.sellergrandtotal.value = '$' + addCommas((sellertotalcalc + sellergst).toFixed(2));
		document.calculator.buyergrandtotal.value = '$' + addCommas((buyertotalcalc + transfercalc + dutycalc + buyergst).toFixed(2));
	
}