

Form.prototype = {
		bind: function(){
			this.income 					= document.getElementById("income");
			this.age_next_birthday			= document.getElementById("age_next_birthday");
			this.retairment_age				= document.getElementById("retairment_age");
			this.income_on_retairment		= document.getElementById("income_on_retairment");
			this.mortgage_balance			= document.getElementById("mortgage_balance");
			this.monthly_mortgage_repayments= document.getElementById("monthly_mortgage_repayments");
			this.interest_rate				= document.getElementById("interest_rate");
			this.inflation_rate				= document.getElementById("inflation_rate");
			this.years_remaining_on_mortgage= document.getElementById("years_remaining_on_mortgage");
			this.vArray[this.vArray.length]=new Array(this.income,'IsNotEmpty,IsMoney','Please enter a valid value for Income');
			this.vArray[this.vArray.length]=new Array(this.age_next_birthday,'IsNotEmpty,IsNumber','Please enter a valid value for Age Next Birthday');
			this.vArray[this.vArray.length]=new Array(this.retairment_age,'IsNotEmpty,IsNumber','Please enter a valid value for Planned retairment Age');
			this.vArray[this.vArray.length]=new Array(this.retairment_age,'form.retairment_age.value > form.age_next_birthday.value','Please enter a valid Planned retairment Age bigger then Age Next Birthday');
			this.vArray[this.vArray.length]=new Array(this.income_on_retairment,'IsNotEmpty,IsPercent','Please enter a valid value for Planned income on retairment');
			this.vArray[this.vArray.length]=new Array(this.mortgage_balance,'IsNotEmpty,IsMoney','Please enter a valid value for Current mortgage balance owing');
			this.vArray[this.vArray.length]=new Array(this.monthly_mortgage_repayments,'IsNotEmpty,IsMoney','Please enter a valid value for Minimum monthly mortgage repayments');
			this.vArray[this.vArray.length]=new Array(this.interest_rate,'IsNotEmpty,IsPercent','Please enter a valid value for Mortgage interest rate');
			this.vArray[this.vArray.length]=new Array(this.inflation_rate,'IsNotEmpty,IsPercent','Please enter a valid value for Inflation Rate');
			this.vArray[this.vArray.length]=new Array(this.years_remaining_on_mortgage,'IsNotEmpty,IsNumber','Please enter a valid value for No. of years remaining on mortgage');
		},
	validate:function() {
		return validate(this)
	}
};

function Form() {
	this.income= null;
	this.age_next_birthday			= null;
	this.retairment_age				= null;
	this.income_on_retairment		= null;
	this.mortgage_balance			= null;
	this.monthly_mortgage_repayments= null;
	this.interest_rate				= null;
	this.inflation_rate				= null;
	this.years_remaining_on_mortgage= null;
	this.vArray						= new Array();
}

function FormValidator() {this.strAlert = "";this.valid = true;this.headAlert = "The form can not be submitted because it is not properly filled.\n";this.Check=Check;this.Set=Set;} function Set(msg) {this.strAlert += "- " + msg+"\n";this.valid = false;} function Check(val,rule,msg) {var cond = true;aRule = rule.split(",");for(var i=0;i<aRule.length;i++) {switch(aRule[i]) {case "IsNotEmpty"		:	{cond = IsNotEmpty(val);break;} case "IsNumber"			:	{	cond = IsNumber(val);break;} case "IsMoney"			:	{	cond = IsMoney(val);break;} case "IsPercent"		:	{	cond = IsPercent(val);break;} default					:	{	cond = eval(aRule[i]);break;} } if(!cond) break;} if(!cond) this.Set(msg);} function IsNotEmpty(val)  { return (trim(val) != "");} function IsNumber(val)  { if (isNaN(val)) return false;if (val < 0) return false;return true;} function IsMoney(val) { valid = true;if(trim(val).length>0) { allowed = "0123456789()-,.$";digits = "0123456789";numCount=0;for(i=0;i<val.length;i++) { if(allowed.indexOf(val.charAt(i))<0) valid=false;if(digits.indexOf(val.charAt(i))>-1) numCount++;} if(valid && numCount==0) valid=false;} return valid;} function IsPercent(val) { valid = true;if(trim(val).length>0) { allowed = "0123456789()-,.%";digits = "0123456789";numCount=0;for(i=0;i<val.length;i++) { if(allowed.indexOf(val.charAt(i))<0) valid=false;if(digits.indexOf(val.charAt(i))>-1) numCount++;} if(valid && numCount==0) valid=false;}		 return valid;} function validate(frm) { fv = new FormValidator();for(var i=0;i<frm.vArray.length;i++) { strEval = frm.vArray[i][0].value;fv.Check(strEval, frm.vArray[i][1],frm.vArray[i][2]);} if(!fv.valid) alert(fv.headAlert + fv.strAlert);return fv.valid;} function trim(str) { return str.replace(/^\s+/g, '').replace(/\s+$/g, '');}

function round(num,dec) { var roundOn = Math.pow(10,dec);
 return Math.round(num*roundOn)/roundOn;
 }
 function formatCurrency(value)  {	 num = value.toString().replace(/\$|\,/g,'');
 return format(round(num,0),false);
 }
 function formatPercentage(value)  {	 num = value.toString().replace(/\%|\,/g,'');
 return format(num,true);
 }
 function formatNumber(value)  {	 num = value.toString().replace(/\%|\$|\,/g,'');
 if(isNaN(num)) return  "0";
 return  num;
	 }
 function format(num,useCents) { if(num!='' ) { if(isNaN(num)) num = "0";
 sign = (num == (num = Math.abs(num)));
 num = Math.floor(num*100+0.50000000001);
 cents = num%100;
 num = Math.floor(num/100).toString();
 if(cents<10) cents = "0" + cents;
 for (var i = 0;
 i < Math.floor((num.length-(1+i))/3);
 i++) num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
 if (useCents)
 	cents= '.' + cents;
else
	cents='';
 return  (((sign)?'':'-') +  num +cents);
 }
 else return  '';
 }
 function strip_out(val) { return val.toString().replace(/\$|\,/g,'');
 }
 

function Calculator(){
	this.income= 0;
	this.age_next_birthday= 0;
	this.retairment_age= 0;
	this.income_on_retairment= 0;
	this.mortgage_balance= 0;
	this.monthly_mortgage_repayments= 0;
	this.interest_rate= 0;
	this.inflation_rate= 0;
	this.years_remaining_on_mortgage = 0;
}
Calculator.prototype = {
	calculate :function(form){
		this.income= strip_out(form.income.value);
		this.age_next_birthday= strip_out(form.age_next_birthday.value);
		this.retairment_age= strip_out(form.retairment_age.value);
		this.income_on_retairment= strip_out(form.income_on_retairment.value);
		this.mortgage_balance= strip_out(form.mortgage_balance.value);
		this.monthly_mortgage_repayments= strip_out(form.monthly_mortgage_repayments.value);
		this.interest_rate= strip_out(form.interest_rate.value);
		this.inflation_rate= strip_out(form.inflation_rate.value);
		this.years_remaining_on_mortgage = strip_out(form.years_remaining_on_mortgage.value);
		this.calculate_tax_payable_annually();
		this.calculate_average_tax_rate();
		this.calculate_num_of_years();
		this.calculate_factor();
		this.calculate_total_tax();
		this.calculate_total_mortgage_repayments();
		this.calculate_taxable_income_for_mortgage();
		this.calculate_planned_year_of_retirement();
		this.calculate_income_required_at_retirement();
	},

	calculate_tax_payable_annually:function(){
		var percentage= 0;
		var less= 0;
		if (this.income >= 0 && this.income < 6000){
			percentage= 0;
			less= 0;
		}
		else if (this.income >= 6000 && this.income < 30000){
			percentage= 16.5;
			less= 900;
		}
		else if (this.income >= 30000 && this.income < 75000){
			percentage= 31.5;
			less= 5400;
		}
		else if (this.income >= 75000 && this.income < 150000){
			percentage= 41.5;
			less= 12900;
		}
		else if (this.income >= 150000){
			percentage= 46.5;
			less= 20400;
		}
		this.tax_payable_annually = this.income*percentage/100 - less;
		return  this.tax_payable_annually;
	},
	
	calculate_average_tax_rate:function(){
		this.average_tax_rate = this.tax_payable_annually*100/this.income;
		return  this.average_tax_rate;
	},
	
	calculate_total_tax:function(){
		this.total_tax = this.tax_payable_annually*this.num_of_years*this.factor;
		return this.total_tax;
	},
	calculate_num_of_years:function(){
		this.num_of_years = this.retairment_age - this.age_next_birthday;
		return this.num_of_years;
	},
	calculate_total_mortgage_repayments:function(){
		this.total_mortgage_repayments = this.monthly_mortgage_repayments*12*this.years_remaining_on_mortgage;
		return this.total_mortgage_repayments;
	},
	calculate_taxable_income_for_mortgage:function(){
		this.taxable_income_for_mortgage = Math.round(this.total_mortgage_repayments/(1-this.average_tax_rate/100));
		return this.taxable_income_for_mortgage;
	},
	calculate_planned_year_of_retirement:function(){
		var d = new Date();
		this.planned_year_of_retirement = d.getFullYear() + this.num_of_years;
		return this.planned_year_of_retirement;
	},
	calculate_income_required_at_retirement:function(){
		this.income_required_at_retirement = (this.income*this.income_on_retairment/100) *this.factor;
		return this.income_required_at_retirement;
	},
	calculate_factor:function(){
		this.factor = Math.pow((1+ this.inflation_rate/100),this.num_of_years);
		return this.factor;
	}
}
 
