// 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 Baseprice=parseFloat(baseprice.value.replace(/\,/g,"").replace(/\$/g,""));
if(isNaN(Baseprice)) Baseprice=0;

var Type=parseFloat(type[type.selectedIndex].value);
if(isNaN(Type)) Type=0;

var SilverCost=(Baseprice + Type) ;
// silvercost.value = "$" + SilverCost;

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

var WeightType=(weighttype.value);

silvercost.value = Weight + " " + WeightType;

if(WeightType=="dwt") {
var DWTcost=(roundNumber(SilverCost/20,2)) ;

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

if(WeightType=="gram") {
var Gramcost=(roundNumber(SilverCost/31.1,2)) ;

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

if(WeightType=="ozT") {
var TOcost = (roundNumber(SilverCost,2));

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