var disposableIncome = 0;
var disposableCapital = 0;

var max_disposableIncome = 10306;
var min_disposableIncome = 3156;

var max_disposableCapital = 11847;
var min_disposableCapital = 7147;

//vars for allowance
var allowance_partner = 1796;
var allowance_child = 2742;
var _totalAllowances = 0;

//holder for contribution
_totalContributionIncome = 0;

//vars for question popups
 var totalQuestions = 7;


//functions

popupWindow = function(){
	var _pg = 'help_civilaid.html#' + this.pgNo; 
	window.open(_pg,'','scrollbars=yes,width=400,height=500');
	return false;
}

checkNo = function(_no,_message){ //number, message, element	
	if (_no == "" ||  isNaN(_no) == true) {
		alert(_message);
		return false;
	}
	else return true;
}

calcAllowances = function(){
	calcTotalAllowances();
}

calcTotalAllowances  = function(){
	var _tempVal;
	_tempVal = 0;
	var partnerBoolean = document.getElementById('chk_partner').checked;
	var noChildren = document.getElementById('sel_children').value;
	if(partnerBoolean) _tempVal += allowance_partner;
	_tempVal += noChildren*allowance_child;
	_totalAllowances = roundPoint(_tempVal,2);
	document.getElementById('txt_Allowances').value = _totalAllowances;
}

calcTotalDisposable = function(){	
	//get values
	var _grossIncome = document.getElementById('txt_grossIncome').value;
	if(!checkNo(_grossIncome,'please fill in your gross income')) return;
	var _expenses = document.getElementById('txt_expenses').value;
	if(!checkNo(_expenses,'please fill in your expenses')) return;
	
	//calc
	disposableIncome = _grossIncome - _expenses - _totalAllowances;
	if (disposableIncome < 0) disposableIncome =0;
	
	//display results
	document.getElementById('txt_disposableIncome').value = disposableIncome;
	displayFinalResult();
}



displayFinalResult = function(){

	var tempT = ""; // var for holding the HTML output
	
	//set disposable income figure
	disposableCapital = document.getElementById('txt_totalCapital').value;
	
	//calculate total contribution
	_totalContributionIncome = (disposableIncome - min_disposableIncome)/3;
	
	//check if both sections of form have been completed
	 if(document.getElementById('txt_disposableIncome').value != "" && document.getElementById('txt_totalCapital').value !=""){
		//compile results for income
		if (disposableIncome > min_disposableIncome && disposableIncome <= max_disposableIncome){
			tempT = "You are eiligble on income but will have to pay a contribution of &pound;" + roundPoint(_totalContributionIncome,2) + ".";
			tempT +="<p> </p>";
			}
		else if (disposableIncome <= min_disposableIncome){
			tempT = "You are eligible on income and will not have to pay a contribution from income.";
			tempT +="<p> </p>";

			}
		else {
			tempT = "You are not eligible on income and do NOT qualify for civil legal aid. (Even if you are told below you are eligible on capital, as you do not qualify on income, you are NOT eligible for civil legal aid.) ";
			tempT +="<p> </p>";
		}
	}//end of if values exist
	
	
	//compile results for capital
	if (disposableCapital > min_disposableCapital && disposableCapital <= max_disposableCapital){
		tempT += "You are eligible on capital but will have to pay a contribution - this will be equal to the difference between your capital figure and &pound;" + min_disposableCapital;
		}
	else if (disposableCapital <= min_disposableCapital){
		tempT += "You are eligible on capital and will not have to pay a contribution from capital.";
		}
	else if (disposableCapital >  max_disposableCapital){
		tempT += "You are not eligible on capital and therefore do NOT qualify for civil legal aid.  (Even if you are told above you are eligible on income, as you do not qualify on capital, you are NOT eligible for civil legal aid.) ";
	}

		document.getElementById('p_answer').innerHTML = tempT;

}


//Based on the above figures, you are likely to qualify for advice and assistance.

setUpElements = function(){
	document.getElementById("but_grossIncome").onclick = calcTotalDisposable;
	document.getElementById("but_disposableIncome").onclick = displayFinalResult;
	document.getElementById('chk_partner').onclick = calcAllowances;
	document.getElementById('sel_children').onchange = calcAllowances;
	//calcWeekly
	for( var i=1;i<totalQuestions + 1;i++){
		var _t = "question" + i;
		document.getElementById(_t).pgNo = i;
		document.getElementById(_t).onclick = popupWindow;
	}
}

function roundPoint(_number,decPlaces){
			var _result = (Math.round (_number*Math.pow(10,decPlaces)))/Math.pow(10,decPlaces);
			return _result;
		}

window.onload = function(){
	setUpElements();
	}

