var material_fee     = 0.048;
var handling_fee     = 0.50;
var min_handling_fee = 0.01;
var setup_fee        = 100.00;
var turnaround_time  = 3;

var last_update = "June 30, 2006 - April 1, 2009";


function is_integer(input)
{
	var text = "" + input;

	if (text.length <= 0)
		return false;

	var i           = 0;
	var valid_chars = "0123456789";
	var c;

	for (i=0; i<text.length; i++)
	{
		c = text.charAt(i);
		if (valid_chars.indexOf(c) == -1)
			return false;
	}

	return true;
}

function is_numeric(input)
{
	var text = "" + input;

	if (text.length <= 0)
		return false;

	var i           = 0;
	var valid_chars = "0123456789.";
	var c;

	for (i=0; i<text.length; i++)
	{
		c = text.charAt(i);
		if (valid_chars.indexOf(c) == -1)
			return false;
	}

	return true;
}

function index_of_decimal(input)
{
	var text = "" + input;
	var i    = 0;

 	for (i=0; i<text.length; i++)
	{
		if (text.charAt(i) == ".")
			return i;
	}

	return -1;
}

function format_currency(input)
{
	var text = "" + input;

	// If the input has no decimal point...
	var index = index_of_decimal(text);
	if (index < 0)
		return text + ".00";

	var remain = text.length - index - 1;
	if (remain == 0)
		return text + "00";
	else if (remain == 1)
		return text + "0";
	else if (remain == 2)
		return text;

	text = "" + (input + 0.005);
	return text.substr(0,index+3);
}

function format_currency_3(input)
{
	var text = "" + input;

	// If the input has no decimal point...
	var index = index_of_decimal(text);
	if (index < 0)
		return text + ".000";

	var remain = text.length - index - 1;
	if (remain == 0)
		return text + "000";
	else if (remain == 1)
		return text + "00";
	else if (remain == 2)
		return text + "0";
	else if (remain == 3)
		return text;

	text = "" + (input + 0.0005);
	return text.substr(0,index+4);
}

function calculate()
{
	form = document.forms[0];

	// Make sure a shape was selected and determine which one
	var shape;
	if (form.shape[0].checked)
		shape = 'Rectangle';
	else if (form.shape[1].checked)
		shape = 'Circle';
	else if (form.shape[2].checked)
		shape = 'Oval';
	else
	{
		alert("You must select a shape.");
		return false;
	}
	
	// Make sure a valid width/diameter was entered
	var width = form.width.value;
	if (!is_numeric(width) || width < 0.2 || width > 30)
	{
		alert("Missing or invalid width/diameter.");
		return false;
	}

	// Unless the shape is a circle, make sure a valid height was entered
	var height = form.height.value;
	if (shape != "Circle" && (!is_numeric(height) || height < 0.2 || height > 30))
	{
		alert("Missing or invalid height.");
		return false;
	}
	
	// Make sure a valid number of labels per sheet was specified
	var lps = form.lps.value;
	if (!is_integer(lps) || lps < 1 || lps > 999)
	{
		alert("Missing or invalid number of labels per sheet.");
		return false;
	}

	// Make sure a valid number of labels per sheet was specified
	var raw_qtys = form.qty.value.split(" ");
	var qtys     = new Array();
	
	// Check each quantity
	var i = 0;
	var j = 0;
	for (i=0; i<raw_qtys.length; i++)
	{
		// If it's numeric
		if (is_integer(raw_qtys[i]))
		{
			// ...check if it's within range
			if (raw_qtys[i] < 1 || raw_qtys[i] > 1000000)
			{
				alert("The specified quantity is invalid.");
				return false;
			}

			// Else, copy it
			qtys[j++] = raw_qtys[i];
		}
	}

	// Calculate the square inches
	var sq_in;
	if (shape == "Circle")
		sq_in = width * width / 4 * 3.14159265358;
	else
		sq_in = width * height;

	// Calculate the material fee
	var mat_fee = sq_in * material_fee;
	
	// Calculate the handling fee, clamping to the minimum
	var hand_fee = handling_fee / lps;
	if (hand_fee < min_handling_fee)
		hand_fee = min_handling_fee;

	// Calculate the total price per label
	var label_fee = 1 * format_currency_3(mat_fee + hand_fee);

	// Calculate the total prices
	var qty_totals = new Array(qtys.length);
	for (i=0; i<qtys.length; i++)
		qty_totals[i] = qtys[i] * label_fee + setup_fee;
	
	// Open a result window and get its document
	var wnd = window.open("","formWindow","menubar=1,toolbar=1,width=500,height=400");
	var doc = wnd.document;

	doc.write(
		"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"
		+ "<html xmlns=\"http://www.w3.org/1999/xhtml\">"
		+ "<head>\n"
		+ "<title>HyDome Quote Result</title>\n"
		+ "<link rel=\"stylesheet\" type=\"text/css\" href=\"../css/formstyle.css\">\n"
		+ "</head>\n"
		+ "<body class=\"quote\">\n"
		+ "<font class=\"page_title\">HyDome Product Quotation</font><br>\n"
		+ "<font class=\"page_title_notice\">Prices are for doming only, not for printing."
		+ "<table style=\"border: 1px solid black;\">\n\n"
		+ "<tr>\n<td><b>Label Shape:</b></td>\n"
		+ "<td>"
		+ shape
		+ "</td>\n"
		+ "</tr>\n"
	);

	if (shape == "Circle")
		doc.write(
			"<tr>\n"
			+ "<td><b>Dimensions:</b></td>\n"
			+ "<td>"	+ width + "\" diameter</td>\n"
			+ "</tr>\n"
		);
	else
		doc.write(
			"<tr>\n"
			+ "<td><b>Dimensions:</b></td>\n"
			+ "<td>" + width + "\" wide x "	+ height + "\" high</td>\n"
			+ "</tr>\n"
		);

	doc.write(
		"<tr>\n"
		+ "<td><b>Labels per Sheet:</b></td>\n"
		+ "<td>" + lps + "</td>\n"
		+ "</tr>\n"
		+ "</table>\n"
		+ "<table width=\"100%\" style=\"border: 1px solid black;\">\n"
	);
	
	doc.write(
		"<tr>\n"
		+ "<th align=\"left\" width=\"25%\">Quantity</th>\n"
		+ "<th align=\"right\" width=\"25%\">Set-Up Fee</th>\n"
		+ "<th align=\"right\" width=\"25%\">Price per Label</th>\n"
		+ "<th align=\"right\" width=\"25%\">Total Price*</th>\n"
		+ "</tr>\n\n"
	);
	
	for (i=0; i<qty_totals.length; i++)	{
		doc.write(
			"<tr>\n"
			+ "<td align=\"left\">" + qtys[i] + "</td>\n"
			+ "<td align=\"right\">$" + format_currency(setup_fee) + "</td>\n"
			+ "<td align=\"right\">$" + format_currency_3(label_fee) + "</td>\n"
			+ "<td align=\"right\">$" + format_currency(qty_totals[i]) + "</td>\n"
			+ "</tr>\n\n"
		);
	}

	doc.write(
		"</table>\n\n"
		+ "<font size=\"-2\">* Total price includes set-up fee</font>\n"
		+ "Current turnaround time is " + turnaround_time + " working day(s) from receipt of labels.<br>\n"
		+ "Prices last updated on " + last_update + "."
		+ "</body>\n"
		+ "</html>\n"
	);
	
	doc.close();

	return false;
}