
var len = 7; //ilosc cyfr do pokazania

function pisz_wynik(s) {
   document.getElementById('kal_result').innerHTML= s;
}


function oblicz3(a,b,kwota) {

   if (isNaN(kwota)) { // for Netscape 3
      pisz_wynik("Wpisz warto&#347;&#263; do przeliczenia".bold() );
      return;
   }
   if ( ((kwota - 0) <= 0 ) || (isNaN(kwota)) ) {
      kwota = 1;
   }


   var value1 = a.options[a.selectedIndex].value ;

   if (value1.charAt(0) == "<")
   {
	   value1= value1.substring(value1.indexOf("'>")+2,value1.indexOf("</span>"));
   }
   
   if (value1.charAt(0) == "*") {
      if (value1.charAt(1) == "<")
	  {
		   value1= value1.substring(value1.indexOf("'>")+2,value1.indexOf("</span>"));
	  }
      value1 = 1/value1;
   }

   var value2 = b.options[b.selectedIndex].value ;

   if (value2.charAt(0) == "<")
   {
	   value2= value2.substring(value2.indexOf("'>")+2,value2.indexOf("</span>"));
   }

   if (value2.charAt(0) == "*") {
      if (value2.charAt(1) == "<")
	  {
		   value2= value2.substring(value2.indexOf("'>")+2,value2.indexOf("</span>"));
	  }
      value2 = 1/value2;
   }

   var c = kwota * (value2 / value1) + "";
   if (isNaN(c)) { // for Netscape 3
      pisz_wynik("Wybierz walut&#281;".bold() );
      return;
   }
   if ( (c == Number.POSITIVE_INFINITY) || (c == Number.NEGATIVE_INFINITY) || (c==0 )) {
      pisz_wynik("Wybierz walut&#281;".bold() );
      return;
   }

   // truncate to 6 or 7 digits w/o re
   var period = nozero = c.length;
   var numhasexp = 0;
   for  (i=0; i<c.length ; i++) { // find . and first digit
      if (c.charAt(i) == "e") numhasexp = 1;
      if (c.charAt(i) == ".") period = i;
      if ((nozero == c.length) && (c.charAt(i) != "0") && (c.charAt(i) !="."))  nozero = i;
   }
   if (numhasexp == 1) {
// don't do anything if exponent
   } else if ((c.length <= len) || (period == c.length)) {
// don't do anything if short or no decimal
   } else if ( period > nozero) {
// if decimal comes after first non-zero digi
         c = c.substring(0,Math.max(len,period));
         if (c.charAt(c.length-1) == ".") {
            c = c.substring(0,c.length - 1); // remove trailing .
         }
   } else if ( period < nozero) {
// truncation with lots of zeros after decimal

         c = c.substring(0,Math.min(nozero + len - 1,c.length));
   }

   var text1 = a.options[a.selectedIndex].text;
   var text2 = b.options[b.selectedIndex].text;

   pisz_wynik(kwota + " " + text1 + " = <b>" + c + "</b> " + text2);

}
