/* 
    Leasing-Calculator
   (c)2005, Gruppe5 - Gesellschaft fuer Unternehmenskommunikation mbH
    by Udo Pasch, Date: 07-31-2005
    http://www.gruppe5.net
*/
    

// Globale Variablen fuer Leasingfaktoren, Beispiel y3 = Leasing Faktor fuer 3 Jahre
var y3 = 4.70; y4 = 3.25; y5 = 2.49;

// Globale Variablen fuer Dezimaltrenner und Rundung 
var laenge = 2; sep = ","; th_sep = ".";

// pruefe auf Ganzzahl in der Eingabe
function testZahl(tt) {
     if (tt == "") {
        return false;
     }
        if (isNaN(tt)) {
            return false;
        }
    return true;
}

// formatiert Zahlenausgabe mit Dezimaltrenner und Nachkommastellen
function /*out: String*/ number_format( /* in: float   */ number,
                                        /* in: integer */ laenge,
                                        /* in: String  */ sep,
                                        /* in: String  */ th_sep ) {

  number = Math.round( number * Math.pow(10, laenge) ) / Math.pow(10, laenge);
  str_number = number+"";
  arr_int = str_number.split(".");
  if(!arr_int[0]) arr_int[0] = "0";
  if(!arr_int[1]) arr_int[1] = "";
  if(arr_int[1].length < laenge){
    nachkomma = arr_int[1];
    for(i=arr_int[1].length+1; i <= laenge; i++){  nachkomma += "0";  }
    arr_int[1] = nachkomma;
  }
  if(th_sep != "" && arr_int[0].length > 3){
    Begriff = arr_int[0];
    arr_int[0] = "";
    for(j = 3; j < Begriff.length ; j+=3){
      Extrakt = Begriff.slice(Begriff.length - j, Begriff.length - j + 3);
      arr_int[0] = th_sep + Extrakt +  arr_int[0] + "";
    }
    str_first = Begriff.substr(0, (Begriff.length % 3 == 0)?3:(Begriff.length % 3));
    arr_int[0] = str_first + arr_int[0];
  }
  return arr_int[0]+sep+arr_int[1];
}

// gibt Zeichenkette x,y,z in entsprechenden DIVs aus
function divOut(x,y,z)
{
    ausgabefeld1 = document.getElementById("ausgabebereich1"); 
    ausgabefeld2 = document.getElementById("ausgabebereich2"); 
    ausgabefeld3 = document.getElementById("ausgabebereich3");
    ausgabefeld2.removeChild(ausgabefeld2.lastChild); 
    ausgabefeld3.removeChild(ausgabefeld3.lastChild);
    ausgabefeld1.removeChild(ausgabefeld1.lastChild);
    ausgabefeld1.appendChild(document.createTextNode(x));
    ausgabefeld2.appendChild(document.createTextNode(y));
    ausgabefeld3.appendChild(document.createTextNode(z));
}

// berechnete Leasingrate/Monat, rundet Ergebnis und übergibt an divOut
function outcalc()
{
    var calc3 = ((x * y3)/100); calc4 = ((x * y4)/100); calc5 = ((x * y5)/100);
    
    calc3 = number_format(calc3,laenge,sep,th_sep);
    calc4 = number_format(calc4,laenge,sep,th_sep);
    calc5 = number_format(calc5,laenge,sep,th_sep);
    divOut(calc3,calc4,calc5);
}

// Ein-/Ausgabe-Funktionen
function incalc()
{   
    x = document.query.calc.value;
    if (testZahl(x)==false) { 
       document.query.calc.value = "Bitte nur ganze Zahlen!";
       return false;
    }
    else
        {
    	x = parseInt(x);
    	outcalc();
	}
    return false
}