function replace(txt,cut_str,paste_str){ 
	var f=0;
	var ht='';
	ht = ht + txt;
	f=ht.indexOf(cut_str);
	while (f!=-1){ 
	f=ht.indexOf(cut_str);
	if (f>0){
	ht = ht.substr(0,f) + paste_str + ht.substr(f+cut_str.length);
	}
    }
	return ht
}
function calc1(form, sel) {
   var firstres  = 0;
   for(var n = sel.firstChild; n; n = n.nextSibling) {
     if(n.selected) {
		var firstres = (replace((document.getElementById('val1').value), ',', '.')) / (n.getAttribute('value'));
		if(isNaN(firstres)) {
     		 return false;
    		}
		document.getElementById('atm').firstChild.nodeValue = (Math.round(firstres * 100) / 100) + ' atm';
		document.getElementById('kpa').firstChild.nodeValue = (Math.round((firstres * 101.325) * 100) / 100) + ' KPa'; 
		document.getElementById('mpa').firstChild.nodeValue = (Math.round((firstres * 0.101325) * 100) / 100) + ' MPa';
		document.getElementById('psi').firstChild.nodeValue = (Math.round((firstres * 14.696) * 100) / 100) + ' psi';
		document.getElementById('kgf').firstChild.nodeValue = (Math.round((firstres * 10332.275) * 100) / 100) + ' kgf/m2';
		document.getElementById('bar').firstChild.nodeValue = (Math.round((firstres * 1.01325) * 100) / 100) + ' bar';
	 }
   }
     return false;
 }
 function calc2(form, sel) {
   var firstress = 0;
   for(var num = sel.firstChild; num; num = num.nextSibling) {
     if(num.selected) {
		var firstress = (replace((document.getElementById('val2').value), ',', '.')) / (num.getAttribute('value'));
		if(isNaN(firstress)) {
     		 return false;
    		}
		document.getElementById('l').firstChild.nodeValue = (Math.round(firstress * 100) / 100) + ' l';
		document.getElementById('gtl').firstChild.nodeValue = (Math.round((firstress * 0.01) * 100) / 100) + ' gtl';
		document.getElementById('dal').firstChild.nodeValue = (Math.round((firstress * 0.1) * 100) / 100) + ' dal';
		document.getElementById('ml').firstChild.nodeValue = (Math.round((firstress * 1000) * 100) / 100) + ' ml';
		document.getElementById('m3').firstChild.nodeValue = (Math.round((firstress * 1e-3) * 100) / 100) + ' m3';
		document.getElementById('cm3').firstChild.nodeValue = (Math.round((firstress * 1e+3) * 100) / 100) + ' cm3';
		document.getElementById('mm3').firstChild.nodeValue = (Math.round((firstress * 1e+6) * 100) / 100) + ' mm3';
		document.getElementById('yds').firstChild.nodeValue = (Math.round((firstress * 1.3072e-3) * 100) / 100) + ' yds3';
		document.getElementById('cf').firstChild.nodeValue = (Math.round((firstress * 0.03531) * 100) / 100) + ' cf3';
		document.getElementById('in').firstChild.nodeValue = (Math.round((firstress * 61.024) * 100) / 100) + ' in3';
		document.getElementById('gallon').firstChild.nodeValue = (Math.round((firstress * 0.2642) * 100) / 100) + ' gallon';
	 }
   }
   	 return false;
 }
window.onload = function() {
     	 var val1  = document.getElementById('val1');
     	 var val2  = document.getElementById('val2');
	 var sel1  = document.getElementById('sel1');
	 var sel2  = document.getElementById('sel2');

 val1.onkeyup = function() {
   calc1(val1, sel1);
 }
 val2.onkeyup = function() {
   calc2(val2, sel2);
 }
 sel1.onchange = function() {
   calc1(val1, sel1);
 }
 sel2.onchange = function() {
   calc2(val2, sel2);
 }
}