/*values used by the calculator*/

var weeks_Factor = 52;
var	fortnights_Factor = 26;

/* constants for the new calculator */
/* Updated 01-04-2009 */
	var annualPlaAbatement = 3400.00; //parents living apart abatement (annual)
	var annualOdsAbatement = 7000.00; //other dependent students abatement (annual)
var annualPill = 52019.24;  //Parental income lower limit (annual)
var annualPiulAtHome   = 78418.34;  //parental income upper limit for student At home (annual)
var annualPiulAway	  = 85017.66;   //parental income upper limit for student away from home (annual)
	var weeklyBasicAlloAway =  181.31;     //Age_16_to_24_living_away from home Basic allowance (weekly)
	var weeklyBasicAlloAtHome =  145.05; //Age_16_to_24_living_at home Basic allowance (weekly)
	var weeklyStuIncomeUppThreshold = 192.01; // student weekly income upper threshold
	var tax_Rate = 0.125; //tax rate 15% 
	var mFactor = 0.2857143; //the rate at which the allowance decreases with increasing parental income


/* 
generic function to return the net value 
param1 - the value representing the gross (Before tax)
taxRate - the current tax rate eg. 0.15 if the tax rate is 15%
return - the computed net value
*/

function getSimpleNet(grossVal,taxRate){
	return grossVal * (1 - taxRate);
}

/* 
generic function to round off to 2 decimal places 
param1 - the value to be rounded off
return - the rounded off value
*/

function to2DecimalPlaces (number){  
    
   var numStr = number.toString();     
   var index = numStr.indexOf("."); 
   if (index > 0 ){    
     if (( numStr.length - index ) < 2){
       numStr = numStr + "0";
     }else{
       //find the number oz     
       var dp = parseFloat( numStr.substring(0 , (index + 3)));       
       var z;
       var thirdDp = parseFloat( numStr.substring(index + 3 , (index + 4)));
       if (thirdDp >= 5){        
         z = dp + 0.011;
         numStr = z.toString(); 
       }       
       numStr = numStr.substring(0 , (index + 3));       
     }
   }else{
     numStr = numStr + ".00";
   }   
   return numStr;
 }


/*************************	CALCULATOR OBJECT *****************************************
*	September 2005 Student Allowance calculator 
*   This is an OBJECT with fields and methods
***************************************************************************************/

function studentAllowanceCalculator(){
	
/*
*fields for Business specific values initialised to defaults
*/
	
	//String constants for form elements
	this.freqWeekly = 'weekly';
	this.freqFortnightly = 'fortnightly';
	this.freqYearly = 'yearly';
	this.locAtHome = 'atHome';
	this.locAway = 'awayFromHome';
	this.livTogether = 'together'; //parents living together
	this.livApart = 'apart'; //parents living apart

	//integer constants
	this.weeksFactor = weeks_Factor;
	this.fortnightsFactor = fortnights_Factor;
	
	//the new calculator constants
	this.annualPlaAbatement = annualPlaAbatement; //parents living apart abatement (annual)
	this.annualOdsAbatement = annualOdsAbatement; //other dependent students abatement (annual)
	this.annualPill = annualPill; //Parental income lower limit (annual)
	this.annualPiulAtHome   = annualPiulAtHome;   //parental income upper limit for student At home (annual)
	this.annualPiulAway	  = annualPiulAway;       //parental income upper limit for student away from home (annual)
	this.weeklyBasicAlloAtHome = weeklyBasicAlloAtHome; //Age_16_to_24_living_at home Basic allowance (weekly)
	this.weeklyBasicAlloAway = weeklyBasicAlloAway;     //Age_16_to_24_living_away from home Basic allowance (weekly)
	this.weeklyStuIncomeUppThreshold = weeklyStuIncomeUppThreshold; // student weekly income upper threshold
	this.taxRate = tax_Rate; //the M_Tax_Code_Rate 
	this.mFactor = mFactor; //the rate at which the allowance decreases with increasing parental income
	/*
	*fields for input values initialised to defaults
	*/
	this.incomeParent1Gross = 0.00; // income of parent1 gross
	this.incomeParent2Gross = 0.00; // income of parent2 gross
	this.incomeFrequencyParent1 = this.freqWeekly; // frequency of payment for parent1
	this.incomeFrequencyParent2 = this.freqWeekly; // frequency of payment for parent2
	this.studentLoc = this.locAtHome; // student location
	this.numOfDepStudents = 0; // number of other dependent students

	this.parentLiv = this.livTogether;//parent living together or apart
	this.incomeStudentGross = 0.00; // income of student gross
	this.incomeFrequencyStudent = this.freqWeekly; // frequency of payment for student

	this.combinedIncome = this.incomeParent1Gross + this.incomeParent2Gross - this.annualPlaAbatement - (this.numOfDepStudents * this.annualOdsAbatement); 
	
    
	/*
	* 	methods to set field values of the calculator 
	*/
	
	//parents living apart abatement (annual)
	function setAnnualPlaAbatement(plabate){
		this.annualPlaAbatement = plabate; 
	}
	this.setAnnualPlaAbatement=setAnnualPlaAbatement;

	//other dependent students abatement (annual)
	function setAnnualOdsAbatement(odsabate){
		this.annualOdsAbatement = odsabate;
	}
	this.setAnnualOdsAbatement=setAnnualOdsAbatement;

	//parental income lower limit (annual)
	function setAnnualPill(pill){
		this.annualPill   = pill;   
	}
	this.setAnnualPill=setAnnualPill;
	
	//parental income upper limit for student At home (annual)
	function setAnnualPiulAtHome(piulathome){
		this.annualPiulAtHome   = piulathome;   
	}
	this.setAnnualPiulAtHome=setAnnualPiulAtHome;

	//parental income upper limit for student away from home (annual)
	function setAnnualPiulAway(piulaway){
		this.annualPiulAway	  = piulaway;       
	}
	this.setAnnualPiulAway=setAnnualPiulAway;

	//Age_16_to_24_living_at home Basic allowance (weekly)
	function setWeeklyBasicAlloAtHome(wbaah){
		this.weeklyBasicAlloAtHome = wbaah; 
	}
	this.setWeeklyBasicAlloAtHome=setWeeklyBasicAlloAtHome;
	
	//Age_16_to_24_living_away from home Basic allowance (weekly)
	function setWeeklyBasicAlloAway(wbaaway){
		this.weeklyBasicAlloAway = wbaaway;     
	}
	this.setWeeklyBasicAlloAway=setWeeklyBasicAlloAway;

	//sets income threshold value for away student
	function setWeeklyStuIncomeUppThreshold(wsiut){
		this.weeklyStuIncomeUppThreshold = wsiut; // student weekly income upper threshold	  
	}
	this.setWeeklyStuIncomeUppThreshold=setWeeklyStuIncomeUppThreshold;

	//sets taxrate
	function setTaxRate(tRate){
		this.taxRate = tRate;
	}
	this.setTaxRate=setTaxRate;

	//sets mFactor
	function setMFactor(mfacto){
		this.mFactor  = mfacto;
	}
	this.setMFactor=setMFactor;
	
    //sets the parent1 gross income
	function setIncomeParent1Gross(incPar1Gross){
		this.incomeParent1Gross = incPar1Gross;
	}
	this.setIncomeParent1Gross=setIncomeParent1Gross;

	//sets the parent2 gross income
	function setIncomeParent2Gross(incPar2Gross){
		this.incomeParent2Gross = incPar2Gross;
	}
	this.setIncomeParent2Gross=setIncomeParent2Gross;

	//sets the parent1 income frequency
	function setIncomeFreqParent1(incPar1Freq){
		this.incomeFrequencyParent1 = incPar1Freq;
	}
	this.setIncomeFreqParent1=setIncomeFreqParent1;

	//sets the parent2 income frequency
	function setIncomeFreqParent2(incPar2Freq){
		this.incomeFrequencyParent2 = incPar2Freq;		
	}
	this.setIncomeFreqParent2=setIncomeFreqParent2;

	//sets the number of other dependent students
	function setNumDepStudents(noDepStuds){
		this.numOfDepStudents = noDepStuds;		
	}
	this.setNumDepStudents=setNumDepStudents;

	//sets the student location away/atHome
	function setStudentLoc(stuLoc){
		this.studentLoc = stuLoc;
	}
	this.setStudentLoc=setStudentLoc;

	//sets the parent living together/away
	function setParentLiv(parLiv){
		this.parentLiv = parLiv;
	}
	this.setParentLiv=setParentLiv;

	//sets the student gross income
	function setIncomeStudentGross(incStuGross){
		this.incomeStudentGross = incStuGross;
	}
	this.setIncomeStudentGross=setIncomeStudentGross;
	
	//sets the student income frequency
	function setIncomeFreqStudent(incStuFreq){
		this.incomeFrequencyStudent = incStuFreq;		
	}
	this.setIncomeFreqStudent=setIncomeFreqStudent;

	/* method to return the 
	* parents living apart abatement (annual)
	*/
	function computeAnnualPlaAbatement(){

		var vplaAbatement = this.annualPlaAbatement;
		if(this.parentLiv == this.livTogether){
			vplaAbatement = 0;
		}
		return vplaAbatement;
	}
	this.computeAnnualPlaAbatement=computeAnnualPlaAbatement;

	/* method to Calculate weekly Combined Income
	*
	*
	*/
	function getAnnualCombinedIncome(){
		
		/*recompute abatement*/
		var plaAbatement = this.computeAnnualPlaAbatement();	

		this.combinedIncome  = this.incomeParent1Gross + this.incomeParent2Gross - plaAbatement - (this.numOfDepStudents * this.annualOdsAbatement); 

		if(this.combinedIncome <0)
			this.combinedIncome = 0;
		
		return this.combinedIncome;
	}
	this.getAnnualCombinedIncome=getAnnualCombinedIncome;
	
	/* method returns weekly Combined Income
	*/
	
	function getWeeklyCombinedIncome(){
		
		var weeklyCombinedIncome = this.combinedIncome / this.weeksFactor;
		
		return weeklyCombinedIncome;
	}
	this.getWeeklyCombinedIncome=getWeeklyCombinedIncome;
	
	/* method returns weekly Combined Income
	*/
	function getWeeklyStudentIncome(){
		
		var weeklyStudentIncome = this.incomeStudentGross / this.weeksFactor;
		
		return weeklyStudentIncome;
	}
	this.getWeeklyStudentIncome=getWeeklyStudentIncome;
	
	
	/* method to calculate the abated student allowance 
	for student living at Home 
	@returns the weekly abated allowance for HOme student */
	
	function getAbatedWeeklyAllowanceAtHome(){
		
		var grossAdjustedParentalIncome = this.getAnnualCombinedIncome();	
		
		//calculate abated student allowance
		var abatedAllowAtHome =  this.weeklyBasicAlloAtHome - (this.mFactor * (grossAdjustedParentalIncome - this.annualPill)/52);
		
		if(grossAdjustedParentalIncome <= this.annualPill){
			abatedAllowAtHome = 	this.weeklyBasicAlloAtHome;
		}
		
		// if negative return 0
        if(abatedAllowAtHome < 0){
			abatedAllowAtHome = 0;
		}
		
		return abatedAllowAtHome;
	}
	this.getAbatedWeeklyAllowanceAtHome=getAbatedWeeklyAllowanceAtHome;
	
	/* method to calculate the abated student allowance 
	for student living at Home 
	@returns the weekly abated allowance for HOme student */
	
	function getAbatedWeeklyAllowanceAway(){
		
		var grossAdjustedParentalIncome = this.getAnnualCombinedIncome();		
		
		var abatedAllowAway =  this.weeklyBasicAlloAway - (this.mFactor * (grossAdjustedParentalIncome - this.annualPill)/52);
		
		if(grossAdjustedParentalIncome <= this.annualPill){
			abatedAllowAway = 	this.weeklyBasicAlloAway;
		}
		
		// if negative return 0
        if(abatedAllowAway < 0){
			abatedAllowAway = 0;
		}
		
		return abatedAllowAway;
	}
	this.getAbatedWeeklyAllowanceAway=getAbatedWeeklyAllowanceAway;
	
	/* method to Calculate and return weekly gross 
	allowance for student living at Home
	
	@returns the weekly gross allowance for Home student*/	
	function getWeeklyAllowanceAtHome(){

		//calculate basic student 
		var parentAnnualIncome = this.getAnnualCombinedIncome();
		var weeklyStuIncome = this.getWeeklyStudentIncome();
		var abatedAllow = this.getAbatedWeeklyAllowanceAtHome();
		
		//default computation when weekly student income is greater than the weekly student income threshold
		var allowAtHome =  abatedAllow - (weeklyStuIncome - this.weeklyStuIncomeUppThreshold);
		
		
		//if the weekly student Income <= the week income threshold
		if(weeklyStuIncome <= this.weeklyStuIncomeUppThreshold){
			allowAtHome = 	 abatedAllow;
		}		
		
		// if negative return 0
        if(allowAtHome < 0){
			allowAtHome = 0;
		}
		
		//if the combined income is greater than specified parent income threshold
	    if(parentAnnualIncome > this.annualPiulAtHome){
			allowAtHome = 0;
		}

		return allowAtHome;
	}
	this.getWeeklyAllowanceAtHome=getWeeklyAllowanceAtHome;
	
	/* method to Calculate and return weekly 
	gross for for student living away

	@returns the weekly gross allowance for away student
	*/	
	function getWeeklyAllowanceAway(){
		
		var parentAnnualIncome = this.getAnnualCombinedIncome();
		var weeklyStuIncome = this.getWeeklyStudentIncome();
		var abatedAllow = this.getAbatedWeeklyAllowanceAway(); 
		
		//default computation when weekly student income is greater than the weekly student income threshold
		var allowAway = abatedAllow  - (weeklyStuIncome - this.weeklyStuIncomeUppThreshold);
				
		//if the weekly student Income <= the week income threshold
		if(weeklyStuIncome <= this.weeklyStuIncomeUppThreshold){
			allowAway = 	 abatedAllow;
		}		
		
		// if negative return 0
        if(allowAway < 0){
			allowAway = 0;
		}
		
		//if the combined income is greater than specified parent income threshold
	    if(parentAnnualIncome > this.annualPiulAway){
			allowAway = 0;
		}

		return allowAway;
	}
	this.getWeeklyAllowanceAway=getWeeklyAllowanceAway;

   /* method to Calculate and 
   return weekly gross allowance */	
	function getWeeklyAllowance(){
		
		
		var allowance = 0;
		
		if(this.studentLoc == this.locAway){
			allowance = this.getWeeklyAllowanceAway();
		}else{
			allowance = this.getWeeklyAllowanceAtHome();		
		}		

		
		var netAllowance = getSimpleNet(allowance,this.taxRate);

		var dispAllowance = allowance;
		var dispNetAllowance = netAllowance;

		/* Is this required??*/
		if(netAllowance < 2){
			netAllowance = 0;
			allowance = 0;
		}
		
		return new Array(to2DecimalPlaces(netAllowance),to2DecimalPlaces(allowance),to2DecimalPlaces(dispNetAllowance),to2DecimalPlaces(dispAllowance));
		
	}
	this.getWeeklyAllowance=getWeeklyAllowance;
};

/** Initializing the calculator object */
var alloCaltr = new studentAllowanceCalculator();


/** 

setupCalculator 

MAKE CHANGES TO THE RATES,MAX VALUES HERE

Method to over-ride the calculator initial values by setting up new values

*/
function setupCalculator(){
	
	//the new calculator constants
	alloCaltr.annualPlaAbatement = annualPlaAbatement; //parents living apart abatement (annual)
	alloCaltr.annualOdsAbatement = annualOdsAbatement; //other dependent students abatement (annual)
	alloCaltr.annualPiulAtHome   = annualPiulAtHome;   //parental income upper limit for student At home (annual)
	alloCaltr.annualPiulAway	 = annualPiulAway;     //parental income upper limit for student away from home (annual)
	alloCaltr.annualPill 		 = annualPill; 		   //parental income lower limit (annual)
		
	alloCaltr.weeklyBasicAlloAtHome = weeklyBasicAlloAtHome; //Age_16_to_24_living_at home Basic allowance (weekly)
	alloCaltr.weeklyBasicAlloAway = weeklyBasicAlloAway;     //Age_16_to_24_living_away from home Basic allowance (weekly)
	alloCaltr.weeklyStuIncomeUppThreshold = weeklyStuIncomeUppThreshold; // student weekly income upper threshold	  
	
	alloCaltr.taxRate = tax_Rate; //tax rate 15% 
	alloCaltr.mFactor = mFactor; //the rate at which the allowance decreases with increasing parental income			
	  
}