// Vinyl Material Cost Estimate

// returns the actual available width given a requested width.
function get_nwidth(width)
{
	var in_sums = 0;
	
	// count in increments of six untill requested width is met or exceded
	for (var i = 0; true; i++)
	{
		in_sums += 6;
		
		if (width == in_sums)
			break;
		else if(width < in_sums){
			in_sums -= 6;
			break;
		}
		else
			continue;
	}
	return in_sums;
}

function get_cutcharge(num_pieces, length)
{
	if (length < 60)
		return (num_pieces * 50);
	else
		return 0;
}

function calculate_all(length, g_width, length_m, g_width_m, mOption)
{
	var width = get_nwidth(g_width_m); // g_width
	
	if (mOption == 1) {
		var actual_coverage = length_m + "x" + width;
	}else{
	    var actual_coverage = f2m(length_m) + "x" + f2m(width);
	}
	
	// var actual_coverage = length_m + "x" + width; // length
	var actual_sqr_yards = (length_m * width) / 9; // length
	
	var num_pieces = width / 6;
	
	var cut_charge = get_cutcharge(num_pieces, length_m);
	
	//var total_floor_cost = Math.round((cut_charge + (17.50 * actual_sqr_yards))*100)/100;
	var total_floor_cost = (cut_charge + (17.50 * actual_sqr_yards));

	//var tape_cost = Math.round((14.950 * num_pieces)*100)/100;
	//var shipping = Math.round((1.80 * actual_sqr_yards)*100)/100;
	var tape_cost = (14.95 * num_pieces);
	var shipping = (1.80 * actual_sqr_yards);
	var sub_total = total_floor_cost + tape_cost + shipping;
	var tax = Math.round((sub_total * 0.0825)*100)/100;
	
	//var total = Math.round((sub_total + tax)*100)/100
	//var total = Math.round(sub_total * 100)/100;
	var total = (sub_total);
	
	document.calc.actual_coverage.value = actual_coverage;
	document.calc.num_pieces.value = num_pieces;
	document.calc.cut_charge.value = "$"+cut_charge.toFixed(2);
	
	document.calc.total_floor_cost.value = "$"+total_floor_cost.toFixed(2);
	document.calc.tape_cost.value = "$"+tape_cost.toFixed(2);
	document.calc.shipping.value = "$"+shipping.toFixed(2);
	//document.calc.tax.value = "$"+tax;
	document.calc.total.value = "$"+total.toFixed(2);
}

function isNumeric(s)
{
	for (var c = 0; c < s.length; c++)
		{
		var d = s.charAt(c);
		if (d != '0' && d != '1' && d != '2' && d != '3' && d != '4' && d != '5' && d != '6' && d != '7' && d != '8' && d != '9')
			return false
		}
	return true
}

function m2f(size) {
	return (size * 3.28)
}

function f2m(size) {
	var num = size / 3.28;
	var res = num.toFixed(2);
	return res;
}

function calculate(length, g_width)
{
	var mOption = -1;
    for (i = document.calc.system.length - 1; i > -1; i--) {
        if (document.calc.system[i].checked) {
            mOption = i; 
			i = -1;
        }
    }
    if (mOption == -1) {
        alert("You must select a system of measurement");
        return false;
    }
	
	if (mOption == 1) {
		var length_m = length;
		var g_width_m = g_width;
	}else{
	    var length_m = Math.ceil(m2f(length));
		var g_width_m = Math.ceil(m2f(g_width));
	}
	
	if((!isNumeric(length)|| !isNumeric(g_width)) && mOption != 0)
		alert("Enter whole numbers only\nRound to the nearest whole number");
	else if(length_m > 60 || length_m < 1)
		alert("Length must be between 1 and 60 feet (0.30 and 18.28 meters)");
	else if(g_width_m < 6)
		alert("Width must be at least 6 feet (1.82 meters)")
	else
		calculate_all(length, g_width, length_m, g_width_m, mOption);
}
