// JavaScript Document

///////////////////////////////////////////////////////////////////////////////////////////////
//Currency and Commas

function CurrencyFormatted(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}

function CommaFormatted(amount)
{
	var delimiter = ","; // replace comma if desired
	amount = new String(amount);
	var a = amount.split('.',2)
	var d = a[1];
	var i = parseInt(a[0]);
	if(isNaN(i)) { return ''; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
	if(d.length < 1) { amount = n; }
	else { amount = n + '.' + d; }
	amount = minus + amount;
	return amount;
}

///////////////////////////////////////////////////////////////////////////////////////////////
//Paper Consumption Calculator

function calc()
{
	f = document.paperneeds;

	if (isNaN(f.pagew.value)) {
		width = 0;
		f.pagew.value = 0;
	} else {
		width = f.pagew.value;
	}
	if (isNaN(f.pageh.value)) {
		height = 0;
		f.pageh.value = 0;
	} else {
		height = f.pageh.value;
	}
	if (isNaN(f.weight.value)) {
		weight = 0;
		f.weight.value = 0;
	} else {
		weight = f.weight.value;
	}
	if (isNaN(f.pages.value)) {
		pages = 0;
		f.pages.value = 0;
	} else {
		pages = f.pages.value;
	}
	if (isNaN(f.pieces.value)) {
		pieces = 0;
		f.pieces.value = 0;
	} else {
		pieces = f.pieces.value;
	}
	if (isNaN(f.waste.value)) {
		waste = 0;
		f.waste.value = 0;
	} else {
		waste = 1 + (f.waste.value / 100);
	}
	if (isNaN(f.cwt.value)) {
		cwt = 0;
		f.cwt.value = 0;
	} else {
		cwt = f.cwt.value;
	}
	basis_size = f.size.value

	result = ((width * height)/ basis_size ) * (weight / 1000) * pages * pieces * waste;
	result = Math.ceil(result * 100);
	result = result / 100
	document.getElementById("total_paper").childNodes[0].nodeValue = result
	
	result2 = result * cwt / 100;
	var result2 = CurrencyFormatted(result2);
	var result2 = CommaFormatted(result2);
    result2 = result2.toString();
	document.getElementById("total_cost").childNodes[0].nodeValue = result2
}

///////////////////////////////////////////////////////////////////////////////////////////////
//Paper Consumption Calculator Side By Side

function calculatePaper()
{
	f = document.paper;

	if (isNaN(f.pagew.value)) {
		width = 0;
		f.pagew.value = 0;
	} else {
		width = f.pagew.value;
	}
	if (isNaN(f.pageh.value)) {
		height = 0;
		f.pageh.value = 0;
	} else {
		height = f.pageh.value;
	}
	if (isNaN(f.weight.value)) {
		weight = 0;
		f.weight.value = 0;
	} else {
		weight = f.weight.value;
	}
	if (isNaN(f.pages.value)) {
		pages = 0;
		f.pages.value = 0;
	} else {
		pages = f.pages.value;
	}
	if (isNaN(f.pieces.value)) {
		pieces = 0;
		f.pieces.value = 0;
	} else {
		pieces = f.pieces.value;
	}
	if (isNaN(f.waste.value)) {
		waste = 0;
		f.waste.value = 0;
	} else {
		waste = 1 + (f.waste.value / 100);
	}
	if (isNaN(f.cwt.value)) {
		cwt = 0;
		f.cwt.value = 0;
	} else {
		cwt = f.cwt.value;
	}
	basis_size = f.size.value

	result = ((width * height)/ basis_size ) * (weight / 1000) * pages * pieces * waste;
	result = Math.ceil(result * 100);
	result = result / 100
	document.getElementById("total_paper2").childNodes[0].nodeValue = result
	
	result2 = result * cwt / 100;
	var result2 = CurrencyFormatted(result2);
	var result2 = CommaFormatted(result2);
	result2 = result2.toString();
	document.getElementById("total_cost2").childNodes[0].nodeValue = result2
}

///////////////////////////////////////////////////////////////////////////////////////////////
//Basis Weight Converter

function convertBasis()
{
	f = document.basisweight;

	if (isNaN(f.weight.value)) {
		weightcalc = 0;
		f.weight.value = 0;
	} else {
		weightcalc = f.weight.value;
	}
	
	weight_type = f.type.value

	resultcover = weightcalc * 520 / weight_type;
	resultcover = Math.round(resultcover);
	document.getElementById("cover_weight").childNodes[0].nodeValue = resultcover
	
	resulttext = weightcalc * 950 / weight_type;
	resulttext = Math.round(resulttext);
	document.getElementById("text_weight").childNodes[0].nodeValue = resulttext
	
	resultgsm = weightcalc * 1406.46721311 / weight_type;
	resultgsm = Math.round(resultgsm);
	document.getElementById("gsm_weight").childNodes[0].nodeValue = resultgsm
	
}

