
function f (e) {

   if (e != null) e.style.backgroundColor = e.value != parseFloat(e.value) ? "#FF9999" : "white";

   var r = Math.PI/180;

   var fl = parseFloat(document.getElementById("fl").value);
   var ha = parseFloat(document.getElementById("ha").value)*r;
   var sa = parseFloat(document.getElementById("sa").value)*r;
   var wb = parseFloat(document.getElementById("wb").value);
   var tt = parseFloat(document.getElementById("tt").value);
   var bb = parseFloat(document.getElementById("bb").value);
   var cs = parseFloat(document.getElementById("cs").value);
   var ins = parseFloat(document.getElementById("in").value)*r;
   var fl2 = parseFloat(document.getElementById("fl2").value);

   var hh = parseFloat(document.getElementById("hh").value);


   var a = Math.sqrt(wb*wb+fl*fl-2*wb*fl*Math.cos(ha));
   var fi = Math.asin(Math.sin(ha)*fl/a);

   var ha_ = ha+ins;

   var fi_ = Math.asin(Math.sin(ha_)*fl/a);
   var hs_ = Math.PI-ha_-fi_;

   var ha2 = Math.atan (a*Math.sin(hs_)/(fl2-a*Math.cos(hs_)));
   var fi2 = Math.asin(Math.sin(ha2)*fl2/a);
   var wb2 = a*Math.cos(fi2)+fl2*Math.cos(ha2);

   var dfi = fi2-fi;

   var bb2 = bb+Math.tan(dfi)*Math.cos(dfi)*cs;
   var sa2 = sa-dfi;
   var tt2 = Math.sin(sa)*tt/Math.sin(sa2);
   
   if (document.getElementById("auto").checked) {
   	 var off = 42/(isImperial ? inmm : 1);
	   var re = wb-cs-(fl+hh)*Math.cos(ha)-Math.sin(ha)*off;
	   var st = (fl+hh)*Math.sin(ha)- bb-Math.cos(ha)*off/2;
	   
	   document.getElementById("re").value = n( re );
 	   document.getElementById("st").value = n( st );

   } else {
     var re = parseFloat(document.getElementById("re").value);
     var st = parseFloat(document.getElementById("st").value);
   }
   
   var c = Math.sqrt(re*re+st*st);
   var tht = Math.atan(re/st);
   
   var re2 = c*Math.sin(tht-dfi);
   var st2 = c*Math.cos(tht-dfi);

   document.getElementById("ha2").value = n(ha2/r,true);
   document.getElementById("sa2").value = n(sa2/r,true);
   document.getElementById("wb2").value = n(wb2);
   document.getElementById("tt2").value = n(tt2);
   document.getElementById("bb2").value = n(bb2);

   document.getElementById("re2").value = n( re2 );
   document.getElementById("st2").value = n( st2 );

   document.getElementById("dfl").value = d(fl2-fl);
   document.getElementById("dha").value = d((ha2-ha)/r,true);
   document.getElementById("dsa").value = d((sa2-sa)/r,true);
   document.getElementById("dwb").value = d(wb2-wb);
   document.getElementById("dtt").value = d(tt2-tt);
   document.getElementById("dbb").value = d(bb2-bb);

   document.getElementById("dre").value = d( re2-re );
   document.getElementById("dst").value = d( st2-st );


}

function n(i, d) {
   d = d || isImperial ? 100 : 10;
   return !isNaN(i) ? Math.round (d*i)/d : " - ";
}

function d(i, d) {
   i = n(i, d);
   return i > 0 ? "+"+i : i;
}