// JavaScript Document

function roundNumber(number,decimal_points) {
	if(!decimal_points) return Math.round(number);
	if(number == 0) {
		var decimals = "";
		for(var i=0;i<decimal_points;i++) decimals += "0";
		return "0."+decimals;
	}

	var exponent = Math.pow(10,decimal_points);
	var num = Math.round((number * exponent)).toString();
	return num.slice(0,-1*decimal_points) + "." + num.slice(-1*decimal_points)
}

function FU_Calculate(){
with(document.calc){

var Weight=parseFloat(weight.value.replace(/\,/g,"").replace(/\$/g,""));
if(isNaN(Weight)) Weight=0;

var WeightType=(weighttype.value);

var Type=parseFloat(type[type.selectedIndex].value);
if(isNaN(Type)) Type=0;
var TTLWeight=(roundNumber(Weight*Type,2)) ;

ttlweight.value = (roundNumber(TTLWeight,2)) + " " + WeightType;

var Baseprice=parseFloat(baseprice.value.replace(/\,/g,"").replace(/\$/g,""));
if(isNaN(Baseprice)) Baseprice=0;

if(WeightType=="dwt") {
var DWTcost_temp=((Baseprice+160)/20) ;
var DWTcost = (roundNumber(DWTcost_temp,2));

dwtcost.value = "$" + DWTcost;
gramcost.value = "";
tocost.value = "";
ttlcost.value = "$" + (roundNumber(TTLWeight * DWTcost,2));
}

if(WeightType=="gram") {
var Gramcost_temp=((Baseprice+160)/31.1);
var Gramcost= (roundNumber(Gramcost_temp,2));

dwtcost.value = "";
gramcost.value = "$" + Gramcost;
tocost.value = "";
ttlcost.value = "$" + (roundNumber(TTLWeight * Gramcost,2));
}

if(WeightType=="ozT") {
var TOcost_temp = (Baseprice+160);
var TOcost = (roundNumber(TOcost_temp,2));

dwtcost.value = "";
gramcost.value = "";
tocost.value = "$" + TOcost;
ttlcost.value = "$" + (roundNumber(TTLWeight * TOcost,2));
}}
return true;
}
