// Load Dojo's code relating to the Button widget 
dojo.require("dojo.parser");
dojo.require("dojo.fx");
dojo.require("dijit.form.NumberSpinner");
dojo.require("dijit.form.CheckBox");
dojo.require("dijit.form.ComboBox");
dojo.require("dijit.form.NumberTextBox");
dojo.require("dijit.form.ValidationTextBox");
dojo.require("dijit.form.FilteringSelect");
dojo.require("dijit.form.Button");
dojo.require("dijit.TitlePane");


dojo.declare("InvestmentCalculator", null, {


		constants:  {
			MINERST    : 0,
			MAXERST    : 99999999,
			MINREGELM  : 0,     
			MAXREGELM  : 999999,     
			MINRENDITE : 0,
			MAXRENDITE : 50,      
			MINLAUFZ   : 1,     
			MAXLAUFZ   : 99,        
			MINENDBETR : 0,         
			MAXENDBETR : 999999999 
			
		},

		indexation : false,
		// 1 = %, 2 = euro
		indexationOption : 1,
		indexationValue : 0,
			
		constructor: function(calculateOption,primalDeposit,constantDeposit,constantDepositInterval,
			 returnRate,duration,finalAmount){
			this.primalDeposit=primalDeposit;
			this.constantDeposit=constantDeposit;
			this.constantDepositInterval=constantDepositInterval;
			this.returnRate=returnRate;
			this.duration=duration;
			this.finalAmount=finalAmount;
			this.calculateOption=calculateOption;
		},
		
		calcFinalAmount: function() {
			this.finalAmount = this._doCalcFinalAmount(this.returnRate);
			return this.finalAmount;
		},

		_doCalcFinalAmount: function(returnRate) {
			var lvI=returnRate/100;
			var lvQ=1+lvI;
			var lvM=12/this.constantDepositInterval;
			var lvT1=lvM+(lvI*((lvM+1)/2));			
			var lvT2=(Math.pow(lvQ,this.duration)-1)/(lvQ-1);

			var result = this.primalDeposit*Math.pow(lvQ,this.duration);

			if (this.indexation && this.indexationValue>0 && this.constantDeposit>0) {

				if (1==this.indexationOption) {

					result += this.constantDeposit * lvT1 * (lvT2 + this._calcIndexationValue());
					
				} else if (2==this.indexationOption) {

					result += lvT1 *(this.constantDeposit * lvT2 + this.indexationValue * this._calcIndexationValue());
				}

			} else {
				result += this.constantDeposit*lvT1*lvT2;
			}
			
			if (result > this.constants.MAXENDBETR) {
				result = this.constants.MAXENDBETR+1;
			} else if (result < this.constants.MINENDBETR ) {
				result = this.constants.MINENDBETR;
			}

			return Math.round(result);
					
		},

		calcPrimalDeposit: function() {
			lvI=this.returnRate/100;
			lvQ=1+lvI;
			lvM=12/this.constantDepositInterval;
			// macht die Berechnung Sinn?
			if ((this.constantDeposit*lvM*this.duration*lvQ)>this.finalAmount)
			  return false;
			lvT1=lvM+(lvI*((lvM+1)/2));
			lvT2=(Math.pow(lvQ,this.duration)-1)/(lvQ-1);


			// indexation selected?
			if (this.indexation && this.indexationValue>0 && this.constantDeposit>0) {

				// 1 means indexation value in percent (%)
				if (1==this.indexationOption) {

					this.primalDeposit=(this.finalAmount - this.constantDeposit * lvT1 * 
								(lvT2 + this._calcIndexationValue()))*Math.pow(lvQ,-this.duration);

				} else if (2==this.indexationOption) {

					this.primalDeposit=(this.finalAmount - lvT1 *
							(this.constantDeposit * lvT2 + this.indexationValue * this._calcIndexationValue()))
								* Math.pow(lvQ,-this.duration);
				}

			} else {
				this.primalDeposit =  (this.finalAmount-(this.constantDeposit*lvT1*lvT2))/Math.pow(lvQ,this.duration);
			}

			if (this.primalDeposit < this.constants.MINERST) {
				this.primalDeposit = this.constants.MINERST;
			} else if (this.primalDeposit > this.constants.MAXERST) {
				this.primalDeposit = this.constants.MAXERST;
			}  

			return Math.round(this.primalDeposit);
		},

		calcConstantDeposit: function() {
			
			lvI=this.returnRate/100;
			lvQ=1+lvI;
			lvM=12/this.constantDepositInterval;
			// macht die Berechnung Sinn?
			if ((this.primalDeposit*Math.pow(lvQ,this.duration))>this.finalAmount || this.primalDeposit>=this.finalAmount)
				return false;

			lvT1=lvM+(lvI*((lvM+1)/2));
			lvT2=(Math.pow(lvQ,this.duration)-1)/(lvQ-1);
			

			// indexation selected?
			if (this.indexation && this.indexationValue>0 && this.constantDeposit>0) {

				// 1 means indexation value in percent (%)
				if (1==this.indexationOption) {

					this.constantDeposit = (this.finalAmount-this.primalDeposit*Math.pow(lvQ,this.duration)) / 
						(lvT1 *(lvT2 + this._calcIndexationValue()));

				} else if (2==this.indexationOption) {

					this.constantDeposit = (((this.finalAmount-this.primalDeposit*Math.pow(lvQ,this.duration))/lvT1)
								-this.indexationValue*this._calcIndexationValue())/lvT2;

				} 

			} else {
				
				this.constantDeposit= (this.finalAmount-(this.primalDeposit*Math.pow(lvQ,this.duration)))/(lvT1*lvT2);
			
			}

			if (this.constantDeposit<this.constants.MINREGELM) {
				this.constantDeposit=this.constants.MINREGELM;
			} else if (this.constantDeposit>this.constants.MAXREGELM) {
				this.constantDeposit=this.constants.MAXREGELM;
			}

			return Math.round(this.constantDeposit);
		
		},

		calcReturnRate: function() {
			lvPerfAct=5.0; // Startwert
			lvDelta=0.001;
			lvWert=1.0;
			for (lvZaehler=0; Math.abs(lvWert-this.finalAmount)>lvDelta; lvZaehler++) {
			  lvWert=this._doCalcFinalAmount(lvPerfAct);
			  lvWert1=this._doCalcFinalAmount(lvPerfAct+lvDelta);
			  lvWert2=this._doCalcFinalAmount(lvPerfAct-lvDelta);
			  lvSteigung=(lvWert1-lvWert2)/(2*lvDelta);
			
			  if (lvSteigung==0)
				lvPerfAct=-1; //break;
			  else
				lvPerfAct=lvPerfAct-((lvWert-this.finalAmount)/lvSteigung);

			  if (lvZaehler>999 || lvPerfAct<0 || lvPerfAct>100) { // damit sich das Programm nicht aufhaengt!
				lvPerfAct=-1;
				break;
			  }
			}


			this.returnRate = Math.round(lvPerfAct*1000)/1000;

			if (this.returnRate<this.constants.MINRENDITE) {
				this.returnRate=this.constants.MINRENDITE;
			} else if (this.returnRate>this.constants.MAXRENDITE) {
				this.returnRate=this.constants.MAXRENDITE;
			}

			
			return this.returnRate;
						
		},

		calcDuration: function() {
					  
			lvI=this.returnRate/100;
			lvQ=1+lvI;
			lvM=12/this.constantDepositInterval;
			lvT1=this.constantDeposit*(lvM+(lvI*((lvM+1)/2)));
			if(this.primalDeposit==0 && lvT1==0)
			  lvT2=0.00001;
			else
			  lvT2=((this.finalAmount*lvI)+lvT1)/(lvT1+(this.primalDeposit*lvQ)-this.primalDeposit);
			if (lvT2<=0)
			  lvT2=0.00001;
			if (lvQ<=0)
			  lvQ=0.00001;
			this.duration  = Math.log(lvT2)/Math.log(lvQ);
			this.duration =  Math.round(this.duration*100)/100;			  

			if (this.duration<this.constants.MINLAUFZ) {
				this.duration=this.constants.MINLAUFZ;
			} else if (this.duration > this.constants.MAXLAUFZ) {
				this.duration = this.constants.MAXLAUFZ;
			}

			return Math.round(this.duration);

		},
		
		_calcIndexationValue: function() {
							  
			var result = 0;

			if (1==this.indexationOption) {
				// in %
				var lvIP = this.indexationValue/100; 

				for (lve=0; lve < this.duration; lve++) {
					result += (( Math.pow(1 + lvIP,lve)- Math.pow(1+lvIP,lve-1))*(( Math.pow(lvQ,this.duration-lve)-1)/(lvQ-1)));
					 
				}

			} else if (2==this.indexationOption) {

				for (lve=0; lve < this.duration; lve++) {
					result += ((Math.pow(lvQ,lve)-1)/(lvQ-1));
				}
			}					  

			return result;
		}


});

dojo.declare("AllocationCalculator", null, {
	
		constants:  {

			MINKAPITAL : 0,
			MAXKAPITAL : 999999999,
			MINRENDITE : 0.1,
			MAXRENDITE : 50,
			MINLAUFZ : 1,
			MAXLAUFZ : 99,
			MINREST : 0,
			MAXREST : 999999999,
			MINENTNAHME : 0,
			MAXENTNAHME : 999999
		},

		constructor: function(calculateOption,capital,duration,durationOption,returnRate,residualCapital,withdrawal,withdrawalInterval){
				this.capital=capital;
				this.duration=duration;
				this.durationOption=durationOption;
				this.returnRate=returnRate;
				this.residualCapital=residualCapital;
				this.withdrawal=withdrawal;
				this.withdrawalInterval=withdrawalInterval
				this.calculateOption=calculateOption;
		},

		calcWithdrawal: function() {
			
			lvI=this.returnRate/100;
			lvQ=1+lvI;
			lvM=12/this.withdrawalInterval;
			lvT1=this.capital*(Math.pow(lvQ,this.duration));
			lvT2=(Math.pow(lvQ,this.duration)-1)/(lvQ-1);
			lvT3=lvM+(lvI*((lvM+1)/2));
			this.withdrawal=Math.round(((lvT1-this.residualCapital)/(lvT2*lvT3)));
			if (this.withdrawal<this.constants.MINENTNAHME) {
				this.withdrawal=this.constants.MINENTNAHME;
			} else if (this.withdrawal > this.constants.MAXENTNAHME) {
				this.withdrawal = this.constants.MAXENTNAHME;
			}
			return this.withdrawal;
		},

		calcDuration: function() {
			lvI=this.returnRate/100;
			lvQ=1+lvI;
			lvM=12/this.withdrawalInterval;
			lvT1=this.withdrawal*(lvM+(lvI*((lvM+1)/2)));
			if (this.capital==0 && lvT1==0)
			  lvT2=0.00001;
			else
			  lvT2=((this.residualCapital*lvI)-lvT1)/((this.capital*lvQ)-this.capital-lvT1);
			if (lvT2<=0)
			  lvT2=0.00001;
			if (lvQ<=0)
			  lvQ=0.00001;
			lvErgebnis= Math.log(lvT2)/Math.log(lvQ);
			this.duration= Math.round(lvErgebnis*100)/100;
			if (this.duration<this.constants.MINLAUFZ) {
				this.duration=this.constants.MINLAUFZ;
			} else if (this.duration > this.constants.MAXLAUFZ) {
				this.duration = this.constants.MAXLAUFZ;
			}
			return this.duration;
		},

		calcCapital: function() {
			this.capital = this._doCalcCapital(this.returnRate);	

			if (this.capital > this.constants.MAXKAPITAL) {
			   this.capital  = this.constants.MAXKAPITAL ;
			} else if (this.capital < this.constants.MINKAPITAL){
			   this.capital  = this.constants.MINKAPITAL ;
			}
			return this.capital;
		},

		calcReturnRate: function() {
			lvPerfAct=5.0;
			lvDelta=0.001;
			lvWert=1.0;
			for (lvZaehler=0; Math.abs(lvWert-this.capital)>lvDelta; lvZaehler++) {
			  lvWert= this._doCalcCapital(lvPerfAct);
			  lvWert1= this._doCalcCapital(lvPerfAct+lvDelta);
			  lvWert2= this._doCalcCapital(lvPerfAct-lvDelta);
			  lvSteigung=(lvWert1-lvWert2)/(2*lvDelta);
			  if (lvSteigung==0) {
				lvPerfAct=-1;
				break;
			  } else
				lvPerfAct=lvPerfAct-((lvWert-this.capital)/lvSteigung);

			  if (lvZaehler>99 || lvPerfAct<0 || lvPerfAct>100) { // damit sich das Programm nicht aufhaengt!
				lvPerfAct=-1;
				break;
			  }

			}
		
			this.returnRate = (Math.round(lvPerfAct*1000))/1000;		

			if (this.returnRate<this.constants.MINRENDITE) {
				this.returnRate=this.constants.MINRENDITE;
			} else if (this.returnRate>this.constants.MAXRENDITE) {
				this.returnRate=this.constants.MAXRENDITE;
			}

			return this.returnRate;
		},

		calcResidualCapital: function() {

			lvI=this.returnRate/100;
			lvQ=1+lvI;
			lvM=12/this.withdrawalInterval;
			lvT1=(this.capital*Math.pow(lvQ,this.duration));
			if (lvI==0)
			  lvT2=0;
			else
			  lvT2=(Math.pow(lvQ,this.duration)-1)/lvI;
			lvT3=lvM+(lvI*((lvM+1)/2));

			// this.durationOption true means 'Aufzehrung'
			if (!this.durationOption)
			  lvErgebnis=((this.withdrawal*lvT2*lvT3)/(Math.pow(lvQ,this.duration)-1));
			else
			  lvErgebnis=(lvT1-(this.withdrawal*lvT2*lvT3));
			
			this.residualCapital =  Math.round(lvErgebnis);

			if (this.residualCapital<this.constants.MINREST) {
				this.residualCapital=this.constants.MINREST;
			} else if (this.residualCapital > this.constants.MAXREST) {
				this.residualCapital = this.constants.MAXREST;
			}

			return this.residualCapital;
		},


		_doCalcCapital: function(pReturnRate) {
			lvI=pReturnRate/100;
			lvQ=1+lvI;
			lvM=12/this.withdrawalInterval;
			lvT1=(Math.pow(lvQ,this.duration)-1)/(lvQ-1);
			lvT2=lvM+(lvI*((lvM+1)/2));
			// this.durationOption true means 'Aufzehrung'
			if (!this.durationOption)
			  //checkboxVorschuessig.getState() 
			  if (true)
				lvErgebnis=((this.withdrawal*lvT1*lvT2)/(Math.pow(lvQ,this.duration)-1));
			  else
				lvErgebnis=((this.withdrawal*lvT1*lvT2)/(Math.pow(lvQ,this.duration+1)-1));
			else
			  //checkboxVorschuessig.getState() 
			  if (true)
				lvErgebnis=(this.residualCapital+(this.withdrawal*lvT1*lvT2))/(Math.pow(lvQ,this.duration));
			  else
				lvErgebnis=(this.residualCapital+(this.withdrawal*lvT1*lvT2))/(Math.pow(lvQ,this.duration+1));

			

			return lvErgebnis;				
						
		}

});

dojo.declare("ViewController", null, {
		
	constructor: function(investmentCalculator,allocationCalculator) {
		this.investmentCalculator=investmentCalculator;	
		this.allocationCalculator=allocationCalculator;	
	},

	handleOptionSwitch: function(event) {

		if (/^investmentOption/.test(event.target.id)) {
			this.investmentCalculator.calculateOption = Number(event.target.value); 
			this.allocationCalculator.calculateOption = 1;
			dijit.byId('allocationOption1').setAttribute('checked',true);
		} else if (/^allocationOption/.test(event.target.id)) {
			this.allocationCalculator.calculateOption = Number(event.target.value); 
			this.investmentCalculator.calculateOption = 2;
			dijit.byId('investmentOption2').setAttribute('checked',true);
		}	
		this.highlightCurrentInvestmentOption(this.investmentCalculator.calculateOption);
		this.highlightCurrentAllocationOption(this.allocationCalculator.calculateOption);

		this.handleInput();
	},

	handleInput: function(widgetId) {


		// console.debug('widgetId is '+widgetId);
		// update model
		this.investmentCalculator.finalAmount 			= dijit.byId('investmentInput1').getValue();		
		this.investmentCalculator.primalDeposit 		= dijit.byId('investmentInput2').getValue();		
		this.investmentCalculator.constantDeposit 		= dijit.byId('investmentInput3').getValue();		
		this.investmentCalculator.constantDepositInterval= dijit.byId('investmentInput31').getValue();		
		this.investmentCalculator.returnRate 			= dijit.byId('investmentInput4').getValue();		
		this.investmentCalculator.duration 				= dijit.byId('investmentInput5').getValue();		
		this.investmentCalculator.indexation            = dijit.byId('investmentInput32').checked;
			
		if (this.investmentCalculator.indexation) {
			if (dijit.byId('investmentInput331').checked) {
				this.investmentCalculator.indexationOption  = dijit.byId('investmentInput331').getValue();  
			} else {
				this.investmentCalculator.indexationOption  = dijit.byId('investmentInput332').getValue();  
			}
			this.investmentCalculator.indexationValue      = dijit.byId('investmentInput34').getValue(); 
		}

		
		
		this.allocationCalculator.capital 			    = dijit.byId('allocationInput2').getValue();
		this.allocationCalculator.duration 			    = dijit.byId('allocationInput3').getValue();
		this.allocationCalculator.durationOption 	    = dijit.byId('allocationInput311').checked;

		this.allocationCalculator.returnRate 	 		= dijit.byId('allocationInput4').getValue();
		this.allocationCalculator.residualCapital 		= dijit.byId('allocationInput5').getValue();
		this.allocationCalculator.withdrawal 			= dijit.byId('allocationInput1').getValue();
		this.allocationCalculator.withdrawalInterval 	= dijit.byId('allocationInput11').getValue();

		if (/^allocationInput/.test(widgetId)) {

			if (dijit.byId('allocationInput312').checked) {
				dijit.byId('allocationInput3').setAttribute('disabled',true);
				this.allocationCalculator.residualCapital 		= this.allocationCalculator.capital;
			} else {
				this.allocationCalculator.residualCapital 		= 0; 
				dijit.byId('allocationInput3').setAttribute('disabled',false);
			}

			this.setValueWithoutTriggeringChangeEvent('allocationInput5', this.allocationCalculator.residualCapital);


			this.investmentCalculator.calculateOption = 2;
			dijit.byId('investmentOption2').setAttribute('checked',true);
			this.highlightCurrentInvestmentOption(this.investmentCalculator.calculateOption);

			this._doCalcAllocation();

			if (2==this.allocationCalculator.calculateOption ||
					/allocationInput2/.test(widgetId)) {
				this.investmentCalculator.finalAmount =  this.allocationCalculator.capital;
				this.setValueWithoutTriggeringChangeEvent('investmentInput1', this.allocationCalculator.capital);
				this._doCalcInvestment();
			}

		
		} else if (/^investmentInput/.test(widgetId)) {

			this.allocationCalculator.calculateOption = 1;
			dijit.byId('allocationOption1').setAttribute('checked',true);
			this.highlightCurrentAllocationOption(this.allocationCalculator.calculateOption);
		
			this._doCalcInvestment();

			if (1==this.investmentCalculator.calculateOption || /investmentInput1/.test(widgetId)) {
				this.allocationCalculator.capital =  this.investmentCalculator.finalAmount;
				this.setValueWithoutTriggeringChangeEvent('allocationInput2', this.investmentCalculator.finalAmount);
				this._doCalcAllocation();
			}

		
		}

		
	},

	toggleIndexFields: function(widgetId) {

		/**
		var anim = dijit.byId('investmentInput32').checked ? 
		dojo.fadeIn({node: 'investmentInput33Container', duration:500, 
			beforeBegin: function() {
				var node = dojo.byId('investmentInput33Container');
				dojo.style(node, "display", "block");
			}
		}) :
		dojo.fadeOut({node: 'investmentInput33Container', duration:500 ,
			onEnd: function() {
				var node = dojo.byId('investmentInput33Container');
				dojo.style(node, "display", "none");
			}
			
		});
		anim.play();
		**/

		dijit.byId('investmentInput331').setAttribute('disabled',!dijit.byId('investmentInput32').checked);
		dijit.byId('investmentInput332').setAttribute('disabled',!dijit.byId('investmentInput32').checked);
		dijit.byId('investmentInput34').setAttribute('disabled',!dijit.byId('investmentInput32').checked);
		this.handleInput(widgetId);
	},

	printInvestmentCalculatorResult: function() {
		
		var urlParameters =
		"erst=${erst}&regelm=${regelm}&rendite=${rendite}&laufz=${laufz}&endbetr=${endbetr}&einintervall=${einintervall}&rechenart=${rechenart}";
	
		// append indexation values
		if (this.investmentCalculator.indexation) {
			urlParameters += "&index=${index}&indexwert=${indexValue}";
		}

		var urlParameterString = dojo.string.substitute(urlParameters,
			 {  erst  		: this.investmentCalculator.primalDeposit,
				regelm 		: this.investmentCalculator.constantDeposit,
				rendite 	: this.investmentCalculator.returnRate,
				laufz       : this.investmentCalculator.duration,
				endbetr     : this.investmentCalculator.finalAmount,
				einintervall: this.investmentCalculator.constantDepositInterval,
				rechenart   : this.investmentCalculator.calculateOption,
				index       : this.investmentCalculator.indexationOption,
				indexValue  : this.investmentCalculator.indexationValue
			 }	
		); 

		popUp('ergebnis_anrechner_popup.jsp?'+urlParameterString,'Ergebnis','630','450','No','Yes');
							 
	},

	printAllocationCalculatorResult: function() {
		// alert(dijit.byId('allocationInput311').checked);
		this.allocationCalculator.durationOption = dijit.byId('allocationInput311').checked;
							 
		var urlParameterString = dojo.string.substitute(
		"kapital=${kapital}&laufz=${laufz}&rendite=${rendite}&rest=${rest}&entnahme=${entnahme}&einintervall=${einintervall}&rechenart=1&enttyp=${enttyp}",
			 {  kapital  	: this.allocationCalculator.capital,
				laufz 		: this.allocationCalculator.duration,
				rendite 	: this.allocationCalculator.returnRate,
				rest 		: this.allocationCalculator.residualCapital,
				entnahme    : this.allocationCalculator.withdrawal,
				einintervall: this.allocationCalculator.withdrawalInterval,
				rechenart 	: this.allocationCalculator.calculateOption,
				enttyp   	: (this.allocationCalculator.durationOption ? 1 : 2)
			 }	
		); 

		popUp('ergebnis_entrechner_popup.jsp?'+urlParameterString,'Ergebnis','630','450','No','Yes');
					
							 
	},


	setValueWithoutTriggeringChangeEvent: function(dijitId,value) {
		dijit.byId(dijitId).intermediateChanges=false;
		dijit.byId(dijitId).setValue(value,false);
		dijit.byId(dijitId).intermediateChanges=true;
	},

	highlightCurrentInvestmentOption: function(elementPostFix) {


		// reset gui widgets
		dojo.query("label[id^='investmentInputLabel']").style("color","black");
		dojo.query("input[id^='investmentInput']").forEach(
			function(inputElement) {
				
				dijit.byId(inputElement.id).setAttribute('disabled',false);
			}
		);

		// highlight gui widgets for the current option
		dojo.query('#investmentInputLabel'+elementPostFix).style('color','red');
		dijit.byId('investmentInput'+elementPostFix).setAttribute('disabled',true);
	
	},

	highlightCurrentAllocationOption: function(elementPostFix) {


		// reset gui widgets
		dojo.query("label[id^='allocationInputLabel']").style("color","black");
		dojo.query("input[id^='allocationInput']").forEach(
			function(inputElement) {
				
				dijit.byId(inputElement.id).setAttribute('disabled',false);
			}
		);

		// highlight gui widgets for the current option
		dojo.query('#allocationInputLabel'+elementPostFix).style('color','red');
		dijit.byId('allocationInput'+elementPostFix).setAttribute('disabled',true);
	
	},

	_doCalcInvestment: function() {

		switch(this.investmentCalculator.calculateOption) {
			
			case 1:
				this.setValueWithoutTriggeringChangeEvent('investmentInput1',this.investmentCalculator.calcFinalAmount());
				break;

			case 2:
				 this.setValueWithoutTriggeringChangeEvent('investmentInput2',this.investmentCalculator.calcPrimalDeposit());
				break;

			case 3:
				 this.setValueWithoutTriggeringChangeEvent('investmentInput3',this.investmentCalculator.calcConstantDeposit());
				break;

			case 4:
				 this.setValueWithoutTriggeringChangeEvent('investmentInput4',this.investmentCalculator.calcReturnRate());
				break;

			case 5:
				 this.setValueWithoutTriggeringChangeEvent('investmentInput5',this.investmentCalculator.calcDuration());
				break;

			default:
		}
			   
	},

	_doCalcAllocation: function() {

		switch(this.allocationCalculator.calculateOption) {

			case 1:
				this.setValueWithoutTriggeringChangeEvent('allocationInput1',this.allocationCalculator.calcWithdrawal());
				break;

			case 2:
				this.setValueWithoutTriggeringChangeEvent('allocationInput2',this.allocationCalculator.calcCapital());

				break;
			
			case 3:
				this.setValueWithoutTriggeringChangeEvent('allocationInput3',this.allocationCalculator.calcDuration());
				break;

			case 4:
				this.setValueWithoutTriggeringChangeEvent('allocationInput4',this.allocationCalculator.calcReturnRate());
				break;

			case 5:
				this.setValueWithoutTriggeringChangeEvent('allocationInput5',this.allocationCalculator.calcResidualCapital());
				break;
			
			default:
		
		}
	}

	
			   
});

var investmentCalculator = new InvestmentCalculator(1,0,0,1,4,10,0);
var allocationCalculator = new AllocationCalculator(1,0,10,false,4,0,0,1);
var viewController 		 = new ViewController(investmentCalculator,allocationCalculator);



// Function form
dojo.addOnLoad(function() {
		
	viewController.highlightCurrentInvestmentOption(1);
	viewController.highlightCurrentAllocationOption(1);

	dojo.query("input[id^='investmentOption']").forEach(
			function(inputElement) {
				dojo.connect(dojo.byId(inputElement.id), 'onclick',viewController,"handleOptionSwitch");
			}
		);

	dojo.query("input[id^='allocationOption']").forEach(
			function(inputElement) {
				dojo.connect(dojo.byId(inputElement.id), 'onclick',viewController,"handleOptionSwitch");
			}
		);

	// by default disable indexation input fields on start
	dijit.byId('investmentInput331').setAttribute('disabled',true);
	dijit.byId('investmentInput332').setAttribute('disabled',true);
	dijit.byId('investmentInput34').setAttribute('disabled',true);
});
