	function toCurrency(value) {
		value = value.replace(",",".");
		value = parseFloat(value);
		value = value.toFixed(2);
		if (isNaN(value) || value == 0) {
			return "";
		} else {
			return value;
			}
	}
	
	function toInteger(value) {
		value = parseInt(value);
		if (isNaN(value)) {
			return "";
		} else {
			return value;
		}
	}
	
	function calculate() {
		var error = false;
		var form = document.finanzierung;
		var theKaufpreis = form.kaufpreis;
		var theAnzahlung = form.anzahlung;
		var theLaufzeit = form.laufzeit;
		var theWunschrate = form.wunschrate;
		var theOutput = document.getElementById("jsOutput");
		var theRestpreis;
		var theEndpreis;
		var theWunschlaufzeit;
		var theCalcOutput;
		var theZinsen = 1.064; /*Zinsen 100% + 6.4%*/
		
		theKaufpreis.value  = toCurrency(theKaufpreis.value);
		theAnzahlung.value  = toCurrency(theAnzahlung.value);
		theLaufzeit.value   = toInteger(theLaufzeit.value);
		theWunschrate.value = toCurrency(theWunschrate.value);
		
		if (theKaufpreis.value == "") {
			if (!error) { 
				error = true;
				alertMsg = "Der Kaufpreis scheint nicht korrekt zu sein.";
			}
		}
		
		if (theAnzahlung.value == "") {
			if (!error) { 
				error = true;
				alertMsg = "Die Anzahlung scheint nicht korrekt zu sein.";
			}
		}
		
		if ((theWunschrate.value == "" && theLaufzeit.value == "") || (theWunschrate.value != "" && theLaufzeit.value != "")) {
			if (!error) { 
				error = true;
				alertMsg = "Bitte geben Sie entweder eine Laufzeit <strong>oder</strong> eine Wunschrate an.";
			}
		}
		
		if (theLaufzeit.value != "" && (Number(theLaufzeit.value) < 6 || Number(theLaufzeit.value) > 60)) {
			if (!error) { 
				error = true;
				alertMsg = "Die Laufzeit ist zu kurz oder zu lang.";
			}
		}
		
		if (!error) {
			theRestpreis = toCurrency(theKaufpreis.value) - toCurrency(theAnzahlung.value);
			theRestpreis = theRestpreis.toString();
			theRestpreis = toCurrency(theRestpreis);
			if (theLaufzeit.value != "") {
				/*Nach Laufzeit berechnen*/
				theEndpreis = (theRestpreis * theZinsen) / theLaufzeit.value;
				theEndpreis = toCurrency(theEndpreis.toString());
				theCalcOutput = "Ihre monatliche Rate beträgt <strong>" + theEndpreis + " &euro;</strong>.";
			} else {
				/*Nach Wunschrate rechnen*/
				theEndpreis = (theRestpreis * theZinsen) / theWunschrate.value;
				theWunschlaufzeit = Math.round(theEndpreis * 10) / 10;
				if (parseFloat(theWunschlaufzeit) % parseInt(theWunschlaufzeit) > 0) {
					theWunschlaufzeit = parseInt(theWunschlaufzeit) + 1;
				} else {
					theWunschlaufzeit = parseInt(theWunschlaufzeit);
				}
				if (theWunschlaufzeit == 0) { theWunschlaufzeit = 1; }
				theCalcOutput = "Ihre Laufzeit beträgt <strong>" + theWunschlaufzeit + " Monate</strong>.";		
			}			
			theOutput.innerHTML = theCalcOutput;
		}	else {
			theOutput.innerHTML = alertMsg;
		}	
		
		if (error === 0) {
		return true;
	} else {
		document.getElementById("jsOutput").style.display = 'block';
		window.location.href = "#jsOutput";
		return false;
	}
		
	}
	
	