/**
* 'better' parse function
* 
* examples how js parses strings to ints (tested in ie6 js1.5)
*   "10a"    => 10
*   "10a10"  => 10
*   "10 000" => 10
*   "10'000" => 10
*   "10,000" => 10
* 
* examples how this function does the same
*   "10a"    => 10
*   "10a10"  => 10
*   "10 000" => 10000
*   "10'000" => 10000
*   "10,000" => 10
* 
* the trick is to remove "'" and " " and then call parseInt(). 
* decide for yourself.
* 
* ---
* got this from http://jibbering.com/faq/#FAQ4_6 :
* Why does parseInt('09') give an error? 
* parseInt decides what base the number is by looking at the number. By convention 
* it assumes any number beginning with 0 is Octal, and any number beginning with 0x 
* Hexadecimal. To force use of base 10 add a second parameter parseInt("09",10) 
* http://msdn.microsoft.com/library/en-us/script56/html/js56jsmthparseint.asp 
* http://developer.netscape.com/docs/manuals/js/client/jsref/toplev.htm#1064173 
* ---
* 
* @author    andrej.arn@arn.li <andrej@arn.li>
* @copyright blueshoes
* @param     mixed s (string or whatever)
* @return    int
* @throws    NaN
*/
function bs_number_parseInt(s) {
  s = s.replace(/ /, '');
  s = s.replace(/'/, '');
  return parseInt(s);
}


//function Sign(y) { return (y>0?'+':y<0?'-':' ') }
function Sign(y) { return (y<0?'-':'') }


function Prepend(Q, L, c) { var S = Q+'' // ??
   // if (!c) var c = ' '
   if (c.length>0) while (S.length<L) { S = c+S }
   return S }

  function StrU(X, M, N) { // X>=0.0
   var T, S=new String(Math.round(X*Number("1e"+N)))
   // if (S.search && S.search(/\D/)!=-1) { return ''+X } // was search(/e/)
   if (/\D/.test(S)) { return ''+X }
   with (new String(Prepend(S, M+N, '0')))
    return substring(0, T=(length-N)) + '.' + substring(T) }

  function StrT(X, M, N) { return Prepend(StrU(X, 1, N), M+N+2, ' ') }



/**
* rounds numbers.
* 
* example:
* bs_number_strS(55.5, 'n', 2) =>  55.50
* bs_number_strS(55.5, 3, 'n') => 055.5
* 
* @param mixed value (int, double, whatever.)
* @param int   charsLeft  (the (min!)   number of chars to the left of the dot. or 'n'.)
* @param int   charsRight (the (exact!) number of chars to the right of the dot. or 'n'.)
* @see http://www.merlyn.demon.co.uk/js-round.htm
*/
//function bs_number_strS(value, charsLeft, charsRight) { 
  function bs_number_strS(X, M, N) { //StrS
    return Sign(X) + StrU(Math.abs(X), M, N);
  }

  function StrW(X, M, N) { return Prepend(StrS(X, 1, N), M+N+2, ' ') } 


    /**
    * 'better' parse function
    * 
    * examples how js parses strings to ints (tested in ie6 js1.5)
    *   "10a"    => 10
    *   "10a10"  => 10
    *   "10 000" => 10
    *   "10'000" => 10
    *   "10,000" => 10
    * 
    * examples how this function does the same
    *   "10a"    => 10
    *   "10a10"  => 10
    *   "10 000" => 10000
    *   "10'000" => 10000
    *   "10,000" => 10
    * 
    * the trick is to remove "'" and " " and then call parseInt(). 
    * decide for yourself.
    * 
    * @author    andrej.arn@arn.li <andrej@arn.li>
    * @copyright blueshoes
    * @param     mixed s (string or whatever)
    * @return    int
    * @throws    NaN
    */
    function bs_number_parseInt(s) {
      s = s.replace(/ /, '');
      s = s.replace(/'/, '');
      return parseInt(s);
    }
    
    function tarifrechnerCalculate() {
      var resultBase  = 0;
      var resultAdd   = 0;
      var resultTotal = 0;
      
      setOutput('', '', '');
      
      var vValue = bs_number_parseInt(document.forms.myForm.elements.vValue.value);
      if (isNaN(vValue)) {
        alert("Bitte geben Sie eine gültige Zahl ein.");
        return;
      }
   
      if (document.getElementById) {
        var t = document.getElementById('vValue');
        if (t) t.value = vValue;
      }
   
      vValue = vValue / 100;
      
      if (document.forms.myForm.elements.massiv[0].checked) {
        var baseTaxRate = 34;
      } else {
        var baseTaxRate = 66;
      }
      resultBase = vValue * baseTaxRate / 1000;
      
      if (document.forms.myForm.elements.zusatz[1].checked) {
        var addTaxRate = 10;
      } else if (document.forms.myForm.elements.zusatz[2].checked) {
        var addTaxRate = 15;
      } else if (document.forms.myForm.elements.zusatz[3].checked) {
        var addTaxRate = 20;
      } else { //0
        var addTaxRate = 0;
      }
      if (addTaxRate > 0) resultAdd = vValue * addTaxRate / 1000;
      
      resultTotal = resultBase + resultAdd;
      
      //resultBase  = resultBase / 100;
      //if (resultAdd > 0) resultAdd  = resultAdd  / 100;
      //resultTotal = resultTotal / 100;
      
      //runden
      resultBase  = myRound(resultBase);
      resultAdd   = myRound(resultAdd);
      resultTotal = myRound(resultTotal);
      
      setOutput(resultBase, resultAdd, resultTotal);
    }
    
 /**
 * extends the number of 'stellen' after the dot.
 * @param double number (or string or whatever)
 * @param int    stellen (number of 'stellen' after the dot :)
 * @todo  make use of the stellen param.
 */
 function myStretch(number, stellen) {
   var myNumber = number.toString();
   var pos = myNumber.indexOf('.');
   if (pos == -1) {
     return number += ".00";
   } else {
     var strLength = myNumber.length;
     var zeichenNachPunkt = strLength -1 -pos;
     switch (zeichenNachPunkt) {
       case 0:
        return number += "00";
       case 1:
        return number += "0";
       case 2:
        return number;
     }
   }
 }
 
 /**
 * rounds the given number to 0.05
 */
 function myRound(price) {
   price = price * 20; //55.8
   price = Math.round(price); //56
   price = price / 20; //5.60
   return myStretch(price, 2);
 }

     
    function setOutput(base, add, total) {
      var done = false;
      
      base  = bs_number_strS(base,  'n', 2);
      add   = bs_number_strS(add,   1, 2);
      total = bs_number_strS(total, 'n', 2);


HM_NS6 = (navigator.vendor == ("Netscape6") || navigator.product == ("Gecko"));
      if (document.getElementById) {
        var t = document.getElementById('resultBase');
        if (t) {
          t.innerText = base;
          document.getElementById('resultAdd').innerText  = add;
          document.getElementById('resultTotal').innerText = total;
          done = true;
        }
      }

if( HM_NS6 )
{
  done = false;
}

/*      
      if (document.getElementById) {
        var t = document.getElementById('resultBase');
        if (t) {
          t.innerText = base;
          document.getElementById('resultAdd').innerText  = add;
          document.getElementById('resultTotal').innerText = total;
          done = true;
        }
      }
*/

      if ((!done) && (total != '') && (total > 0)) {
        alert('Grundprämie/Jahr: ' + base + '\n' + 'Zusatzprämie/Jahr: ' + add + '\n' + 'Total Jahresprämie: ' + total + '\n' + '(unverbindlich)');
      }
    }
    
