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

function convert_humidity_js_init() {

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

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

	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(table1);
	container.appendChild(div2);
	container.appendChild(table2);

	var txt1 = document.createElement('input');
	var txt2 = document.createElement('input');
	var btn  = document.createElement('button');

	var tr, td, span;

	tr = table1.insertRow(-1);
	td = tr.insertCell(-1);
	td.setAttribute("class", "label");
	td.className = "label";
	td.innerHTML = "Temperature:";
	td = tr.insertCell(-1);
	td.setAttribute("class", "value");
	td.className = "value";
	td.appendChild(txt1);
	(span = document.createElement('span')).innerHTML = "&deg;C";
	td.appendChild(span);

	tr = table1.insertRow(-1);
	td = tr.insertCell(-1);
	td.setAttribute("class", "label");
	td.className = "label";
	td.innerHTML = "Relative humidity:";
	td = tr.insertCell(-1);
	td.setAttribute("class", "value");
	td.className = "value";
	td.appendChild(txt2);
	(span = document.createElement('span')).innerHTML = "%RH";
	td.appendChild(span);

	div2.appendChild(btn);
	btn.innerHTML = "Convert";
	btn.onclick = function() {

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

		var tr, td;
		tr = table2.insertRow(-1);

		td = tr.insertCell(-1);
		td.setAttribute("class", "label");
		td.className = "label";
		td.innerHTML = "Absolute capacity humidity";

		td = tr.insertCell(-1);
		td.setAttribute("class", "value");
		td.className = "value";
		td.innerHTML = calc1() + "<span>g/m&sup3;</span>";

		tr = table2.insertRow(-1);

		td = tr.insertCell(-1);
		td.setAttribute("class", "label");
		td.className = "label";
		td.innerHTML = "Dew point temperature";

		td = tr.insertCell(-1);
		td.setAttribute("class", "value");
		td.className = "value";
		td.innerHTML = calc2() + "<span>&deg;C</span>";

		function calc1() {
			var temperature = parseFloat(txt1.value);
			var relativity_temperature = parseFloat(txt2.value);
			var c = temperature + 273;
			var d = 10.79586 * (1 - 273.16 / c);
			var e = -5.02808 * (Math.log(c / 273.16) / Math.log(10));
			var f = 1 - c / 273.16;
			var g = 1 - Math.pow(10, (8.29692 * f));
			var h = g * 1.50474 * Math.pow(10, (-4));
			var i = 1 - 273.16 / c;
			var j = Math.pow(10, (4.76955 * i)) - 1;
			var k = 0.42873 * Math.pow(10, (-3)) * j;
			var l = -2.2195983;
			var m = Math.pow(10, (d + e + h + k + l)) * 1013.25;
			var n = 0.794 * relativity_temperature * m / 100;
			return n / (1 + 0.00366 * temperature);
		}

		function calc2() {
			var temperature = parseFloat(txt1.value);
			var relativity_temperature = parseFloat(txt2.value);
			var a = 18.7509;
			var b = 4075.16;
			var c = 236.516;
			var d = relativity_temperature / 100;
			var es = Math.exp(a - (b / (temperature + c)));
			var d = relativity_temperature / 100;
			return b / (a - Math.log(d * es)) - c;
		}
	};
}
