
if (window.onload) {
	var onload__ = window.onload;
	window.onload = function(){
		onload__();
		convert_js_init();
	};
} else
	window.onload = convert_js_init;

function convert_js_init() {

	if (arguments.callee.done) return;
	arguments.callee.done = true;

	// parent container
	var container_id = "convert_js_container";	///required///
	var container = document.getElementById(container_id);
	if (! container) return;

	var unit_type = [
		"Length", "Surface", "Volume", "Mass", "Speed", "Temperature"
	];

	var unit = [
		["Centimeters", "Meters", "Kilometers", "Inches", "Feet", "Yards", "Miles", "Nautical miles"],
		["Square centimeters", "Square meters", "Square kilometers", "Square inches", "Square feet", "Square yards", "Acres", "Ares", "Hectares", "Square miles"],
		["Cubic centimeters", "Cubic meters", "Cubic inches", "Cubic feet", "Cubic yards", "Liters", "Gallons (USA)", "Gallons (UK)", "Barrels"],
		["Grams", "Kilograms", "Ounces", "Pounds", "Short tons", "Long tons", "Metric tons"],
		["m/sec", "ft/sec", "m/min", "ft/min", "knots", "miles/hr", "km/hr"],
		["C: Celsius", "F: Fahrenheit"]
	];

	var slope = [
		[1.0, 0.01, 0.00001, 0.393700787401575, 0.0328083989501312, 0.0109361329833771, 0.00000621371192237334, 0.00000539956803455724],
		[1.0, 0.0001, 0.0000000001, 0.155038759689923, 0.00107642626480086, 0.000119599056124249, 0.0000000247105163015276, 0.000001, 0.00000001, 0.0000000000386101876841223],
		[1.0, 0.000001, 0.061012812690665, 0.0000353107344632768, 0.0000013079506193144, 0.001, 0.000264172052358148, 0.000219969248299088, 0.00000628981077043211],
		[1.0, 0.001, 0.0352739907229404, 0.00220463414096431, 0.00000110231099500102, 0.000000984203533290685, 0.000001],
		[1.0, 3.28083989501312, 60.0, 196.850393700787, 1.94384617178935, 2.2369362920544, 3.6],
		[1.0, 1.8]
	];

	var intercept = [
		[0, 0, 0, 0, 0, 0, 0, 0],
		[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
		[0, 0, 0, 0, 0, 0, 0, 0, 0],
		[0, 0, 0, 0, 0, 0, 0],
		[0, 0, 0, 0, 0, 0, 0],
		[0, 32]
	];

	var selected_type = -1;

	var div1 = document.createElement("div");
	var div2 = document.createElement("div");
	var table1 = document.createElement("table");	// in
	var table2 = document.createElement("table");	// out

	table1.setAttribute("class", "i");
	table1.className = "i";
	table2.setAttribute("class", "o");
	table2.className = "o";

	container.appendChild(div1);
	container.appendChild(table1);
	container.appendChild(div2);
	container.appendChild(table2);

	var sel = document.createElement("select");
	var txt = document.createElement("input");
	var btn = document.createElement("button");

//	for (var i = 0; i < unit_type.length; ++i) {
//		var rdo = document.createElement("input");
//		rdo.type = "radio";
//		rdo.name = "unit_type";
//		rdo.value = i;
//		rdo.onclick = function() {
//			var ary = unit[this.value];
//			sel.options.length = ary.length;
//			for (var i = 0; i < ary.length; ++i)
//				sel.options[i].text = ary[i];
//			sel.selectedIndex = 0;
//			selected_type = this.value;
//		};
//		div1.appendChild(rdo);
//		div1.appendChild(document.createTextNode(unit_type[i]));
//	}

	var str = "";
	for (var i = 0; i < unit_type.length; ++i)
		str += '<input type="radio" name="unit_type" value="' + i + '" />' + unit_type[i] + '&nbsp;&nbsp;' ;
	div1.innerHTML = str;
	var rdo = div1.firstChild;
	for (var i = 0; i < unit_type.length; ++i) {
		rdo.onclick = function() {
			var ary = unit[this.value];
			sel.options.length = ary.length;
			for (var i = 0; i < ary.length; ++i)
				sel.options[i].text = ary[i];
			sel.selectedIndex = 0;
			selected_type = this.value;
		};
		rdo = rdo.nextSibling.nextSibling;
	}

	var tr, td;
	tr = table1.insertRow(-1);
	td = tr.insertCell(-1);
	td.setAttribute("class", "label");
	td.className = "label";
	td.innerHTML = "Before conversion:";
	td = tr.insertCell(-1);
	td.setAttribute("class", "value");
	td.className = "value";
	td.appendChild(sel);

	tr = table1.insertRow(-1);
	td = tr.insertCell(-1);
	td.setAttribute("class", "label");
	td.className = "label";
	td.innerHTML = "Value:";
	td = tr.insertCell(-1);
	td.setAttribute("class", "value");
	td.className = "value";
	td.appendChild(txt);

	div2.appendChild(btn);
	btn.innerHTML = "Convert";
	btn.onclick = function() {
		var i = selected_type, j = sel.selectedIndex;
		if (i == -1 || j == -1) return;

		var norm = (parseFloat(txt.value) - intercept[i][j]) / slope[i][j];

		for (var k = table2.rows.length - 1; k >= 0; --k)
			table2.deleteRow(k);

		var tr, td;
		for (var k = 0; k < slope[i].length; ++k) {
			tr = table2.insertRow(-1);

			td = tr.insertCell(-1);
			td.setAttribute("class", "label");
			td.className = "label";
			td.innerHTML = unit[i][k];

			td = tr.insertCell(-1);
			td.setAttribute("class", "value");
			td.className = "value";
			td.innerHTML = (norm * slope[i][k] + intercept[i][k]).toString();
		}
	};

	// initial value
	div1.firstChild.click();
}
