/**
 * @author buenger
 */

var firstRun = true;
var pageFirstRun = true;
var leasingOptions = {
	
	//Input
	customerTypeId: 'kundenart',
	carKindId: 'fahrzeugart',
	carTypeId: 'fahrzeugtyp',
	makeId: 'marke',
	priceId: 'barkaufpreis',
	runtimeId: 'laufzeit_monate',
	kmId: 'km_pro_jahr',
	depositId: '1_leasingrate',
	decliningBalanceKindId: 'restwert',
	decliningBalanceId: 'restwert_1',
	PPIDeathId: 'versicherung_todesfall',
	PPIUnemploymentId: 'versicherung_erwerbslosigkeit',	
	
	//Results
	nomInterestId: 'jahreszinsen_nominal',
	effInterestId: 'jahreszinsen_effektiv',
	rateId: 'leasingrate_ohne_absicherung',
	rateInclPPIId: 'leasingrate_inkl_absicherung',
	securityId: 'kaution',
	resultBox1Id: 'box_leasing_resultat_1',
	resultBox2Id: 'box_leasing_resultat_2'
}


var leasingRequest = new LeasingService ('BanknowMiniCalcLeaseRequest');
var leasingCalculator = new Leasing ();

function addLeasingEvents () {
	$E(leasingOptions.carTypeId).addEvent("propChange", handleCarType);
	$E(leasingOptions.makeId).addEvent("propChange", handleMake);
	$E(leasingOptions.runtimeId).addEvent("propChange", handleRuntime);
	$E(leasingOptions.priceId).addEvent("propChange", handlePrice);
	/*$E(leasingOptions.depositId).addEvent("propChange", handleDeposit);
	$E(leasingOptions.decliningBalanceKindId).addEvent("propChange", handleDecliningBalanceKind);
	$E(leasingOptions.PPIDeathId).addEvent("propChange", handlePPIDeath);
	$E(leasingOptions.PPIUnemploymentId).addEvent("propChange", handlePPIUnemployment);	*/
}

function handleCarType () {
	
	Settings.setLeasingMinPrice($E(leasingOptions.carTypeId).formValue());
	Settings.setLeasingMaxPrice();
	
	leasingRequest.setCarType($E(leasingOptions.carTypeId).formValue());
	leasingRequest.addEvent("success", handleMakeResponse);
	leasingRequest.getMake();
}

function handleMake () {
	
	leasingRequest.setMake($E(leasingOptions.makeId).formValue());
	leasingRequest.addEvent("success", handleCarDataResponse);
	//leasingRequest.getCarData();
	
}

function handlePrice () {
		
	leasingRequest.setPrice($E(leasingOptions.priceId).numValue);
	leasingCalculator.setPrice($E(leasingOptions.priceId).numValue);
	/*if ($E(leasingOptions.runtimeId).value() != 0) {
		if (firstRun == true) {
			$E(leasingOptions.kmId).addEvent("propChange", getDecliningBalance);
			getDecliningBalance();
			firstRun = false;
		}
	}*/
}

function handleDeposit () {
	leasingRequest.setDepositExclVAT(Utilities.exclVAT($E(leasingOptions.depositId).numValue));
	leasingCalculator.setDeposit($E(leasingOptions.depositId).numValue);
}

function handleKm () {
	leasingRequest.setKm($E(leasingOptions.decliningBalanceId).formValue());
	getDecliningBalance();
}

function handleDecliningBalanceKind () {
	
	if ($E(leasingOptions.decliningBalanceKindId).value() == 1) {
		$E(leasingOptions.decliningBalanceId).addEvent("propChange", handleDecliningBalance);
		$E(leasingOptions.decliningBalanceId).setReadOnly(false);
	} else {
		$E(leasingOptions.decliningBalanceId).removeEvent("propChange", handleDecliningBalance);
		$E(leasingOptions.decliningBalanceId).setNumValue(leasingCalculator.getStandardDecliningBalance());
		leasingCalculator.setDecliningBalance(leasingCalculator.getStandardDecliningBalance());
		$E(leasingOptions.decliningBalanceId).setReadOnly(true);
		
	}
}

function handleDecliningBalance () {
	leasingRequest.setDecliningBalance(Utilities.exclVAT($E(leasingOptions.decliningBalanceId).numValue));
	leasingCalculator.setDecliningBalance($E(leasingOptions.decliningBalanceId).numValue);
}

function handleRuntime () {
	leasingCalculator.setRuntime($E(leasingOptions.runtimeId).formValue());
	leasingRequest.setRuntime($E(leasingOptions.runtimeId).formValue());
			
	/*if ($E(leasingOptions.priceId).numValue != undefined && $E(leasingOptions.priceId).numValue != 0) {
		if (firstRun == true) {
			$E(leasingOptions.kmId).addEvent("propChange", getDecliningBalance);
			firstRun = false;
		}
		getDecliningBalance();
	}*/
	$('calculateButton').style.display = "block";
}

function handlePPIDeath () {
	leasingCalculator.setPPIDeath($E(leasingOptions.PPIDeathId).value());
}

function handlePPIUnemployment () {
	leasingCalculator.setPPIUnemployment($E(leasingOptions.PPIUnemploymentId).value());
}

function handleCarTypeResponse  () {
	leasingRequest.removeEvent("success", handleCarTypeResponse);
	var options = [];
	$A(arguments).each(function (a_item, a_index ){
		var option = new Element ("option");
		option.innerHTML = a_item.value;
		option.value = a_item.id;
		options.push(option);
	})
	$E(leasingOptions.carTypeId).replaceOptions(options);
	//initReadOnlys();	
}

function handleMakeResponse  () {
	
	
	leasingRequest.removeEvent("success", handleMakeResponse);
	var options = [];
	$A(arguments).each(function (a_item, a_index ){
		var option = new Element ("option");
		option.innerHTML = a_item.value;
		option.value = a_item.id;
		options.push(option);
	})
	$E(leasingOptions.makeId).replaceOptions(options);	
}

function handleCarDataResponse (a_obj) {
	leasingRequest.removeEvent("success", handleCarDataResponse);
	leasingRequest.setDecliningBalanceType(a_obj.decliningBalanceId);
	
	var options = [];
	for (var i = Number(a_obj.minKilometers); i <= Number(a_obj.maxKilometers); i += Number(a_obj.step)) {
		var option = new Element ("option");
		option.innerHTML = i;
		option.value = i;
		options.push(option);
	}
	$E(leasingOptions.kmId).replaceOptions(options, 2);
	leasingRequest.setKm(options[1].value);
	if ($E(leasingOptions.runtimeId).value() != 0) {
		if (firstRun == true) {
			$E(leasingOptions.kmId).addEvent("propChange", getDecliningBalance);
			firstRun = false;
		}
		else getDecliningBalance();
	}
}

function handleDecliningBalanceResponse  (a_obj) {
	leasingRequest.removeEvent("success", handleDecliningBalanceResponse);
	leasingCalculator.setMinDecliningBalance($E(leasingOptions.priceId).numValue * Number (a_obj.minDecliningBalance)/100);
	leasingCalculator.setMaxDecliningBalance($E(leasingOptions.priceId).numValue * Number (a_obj.maxDecliningBalance)/100);
	leasingCalculator.setStandardDecliningBalance($E(leasingOptions.priceId).numValue * Number (a_obj.decliningBalance)/100);
		
	if ($E(leasingOptions.decliningBalanceKindId).value() != 1) {
		var decliningBalance = $E(leasingOptions.priceId).numValue * Number(a_obj.decliningBalance) / 100;
		leasingCalculator.setDecliningBalance(decliningBalance);
		$E(leasingOptions.decliningBalanceId).setNumValue(decliningBalance);
	}
	
	$('berechnenButton').style.display = "inline";
	$E(leasingOptions.resultBox1Id).setVisibility(true);
}

function handleEffInterestResponse  () {
	leasingRequest.removeEvent("success", this);
}

function getDecliningBalance () {
	leasingRequest.addEvent("success", handleDecliningBalanceResponse);
	leasingRequest.getDecliningBalance();
}

function setLeasingResults() {

	var nomInterest = LeasingInterest.getInterest($E(leasingOptions.carTypeId).formValue(), $E(leasingOptions.priceId).numValue, $E(leasingOptions.depositId).numValue);
	var effInterest = Utilities.getEffInterest (nomInterest);
	$E(leasingOptions.nomInterestId).setValue (nomInterest.toFixed(2)+"%");
	$E(leasingOptions.effInterestId).setValue (effInterest.toFixed(2)+"%"); 
	
	leasingCalculator.setNomInterest(nomInterest);
	leasingCalculator.calculate();
}

function handleResults(){
	$E(leasingOptions.rateId).setNumValue(leasingCalculator.rate);
	$E(leasingOptions.rateInclPPIId).setNumValue(leasingCalculator.rateInclPPI);
	$E(leasingOptions.securityId).setNumValue(0);
	if ($E(leasingOptions.depositId).numValue == undefined || $E(leasingOptions.depositId).numValue == 0) {
		$E(leasingOptions.depositId).setNumValue (leasingCalculator.rateInclPPI);
	}
	
	$E(leasingOptions.resultBox2Id).setVisibility(true);
}

function handleError (a_obj) {
	debug (a_obj)
}

function initReadOnlys () {
	$E(leasingOptions.nomInterestId).setReadOnly(true);
	$E(leasingOptions.effInterestId).setReadOnly(true);
	$E(leasingOptions.rateId).setReadOnly(true);
	$E(leasingOptions.rateInclPPIId).setReadOnly(true);
	$E(leasingOptions.decliningBalanceId).setReadOnly(true);
	
}

function leasingInit () {
	window.removeEvent ("load", this);	
	addLeasingEvents();
	
	leasingCalculator.addEvent("error", handleError);
	leasingCalculator.addEvent("success", handleResults);
	
	leasingRequest.addEvent("success", handleCarTypeResponse);
	leasingRequest.getCarType();
	$('calculateButton').style.display = "none";
}

window.onload = leasingInit;

