var defaultText = "Please complete the figures above.";
var disposableIncome = 0;
var disposableCapital = 0;

var max_disposableIncome = 25450;
var min_disposableIncome = 3415;

var disposableIncomeScale = new Array(3415, 11193, 15270, 25450);
var disposableIncomeValue = new Array(33,    50,    100);

var disposableCapitalScale = new Array(7617, 12626);

var installmentsScale = new Array(0, 500, 1000, 1500, 2000);
var installmentsValue = new Array(20, 30, 36, 42, 48);

var max_disposableCapital = 12439;
var min_disposableCapital = 7504;

//vars for allowance
var allowance_partner = 1945;
var allowance_child = 3002;
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;
}

calculateNDisplay = function (){
	document.getElementById('p_answer').innerHTML = calculate();		
}

calculate = function(){
	calcTotalAllowances();
	calcTotalIncome();
	calcTotalCapital();
	if (isNaN(disposableIncome) || isNaN(disposableCapital)) {
		return defaultText;
	}	
	var contribution = 0;
	var text = "";
	var benefits = document.getElementById("chk_benefits").checked;
	if (benefits) {
		return "You <b>qualify</b> for civil legal aid and you do <b>not have to pay a contribution</b>."
	}
	var income = calcIncome();
	var capital = calcCapital();	
	if (isNaN(income) || isNaN(capital) || disposableIncome > max_disposableIncome) {
		text = "You do <b>NOT qualify</b> for civil legal aid.";
	}
	else {
		text = "You <b>qualify</b> for civil legal aid";
		contribution = income + capital;
		if (contribution == 0) {
			text += " and you do <b>not have to pay a contribution</b>.";
		}
		else {
			if (income > 0 && !isNaN(income)) {
				text += ".<br/><br />You will have to pay a contribution based on <b>income</b> of <br/>&pound;" + roundPoint(income,2);
				var installmentsRange = getElemIndex(income, installmentsScale);
				text += ".<br/><br />Normally you will pay this in " + installmentsValue[installmentsRange] + " monthly instalments of <br/>&pound;" + roundPoint(contribution/installmentsValue[installmentsRange],2);
			}
			if (capital > 0 && !isNaN(capital)) {
				text += ".<br/><br />You will have to pay a contribution based on <b>capital</b> of &pound;" + roundPoint(capital,2) + ", which is normally paid in one lump sum.";
			}
		}
	}
	return text;
}

doBenefits = function(){	
	if (document.getElementById('chk_benefits').checked) {
		document.getElementById('txt_grossIncome').value = 0;
		document.getElementById('txt_expenses').value = 0;
		document.getElementById('txt_totalCapital').value = 0;		
		document.getElementById('txt_grossIncome').disabled=true;
		document.getElementById('txt_expenses').disabled=true;
		document.getElementById('txt_totalCapital').disabled=true;
	}
	else {
		document.getElementById('txt_grossIncome').disabled=false;
		document.getElementById('txt_expenses').disabled=false;
		document.getElementById('txt_totalCapital').disabled=false;
	}
	
}

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;
}

calcTotalIncome = function(){	
	//get values
	var _grossIncome = document.getElementById('txt_grossIncome').value;
	if(!checkNo(_grossIncome,'Please fill in your gross annual income at step 3.')) {
		disposableIncome = NaN;
		return;
	}
	var _expenses = document.getElementById('txt_expenses').value;
	if(!checkNo(_expenses,'Please fill in your expenses at step 3.')) {
		disposableIncome = NaN;
		return;
	}
	//calc
	disposableIncome = _grossIncome - _expenses - _totalAllowances;
	if (disposableIncome < 0) disposableIncome =0;
	//display results
	document.getElementById('txt_disposableIncome').value = disposableIncome;
}


calcIncome = function(){
	disposableIncome = document.getElementById('txt_disposableIncome').value;
	var tmp = disposableIncome;
	var contribution = 0;
	var currBandSum = 0;
	if (disposableIncome > disposableIncomeScale[disposableIncomeScale.length-1]) {
		
		return contribution;
	
	}
	for (var i=disposableIncomeScale.length-1; i>=0; i--) {
		if (disposableIncome > disposableIncomeScale[i]) {
			currBandSum = disposableIncome - disposableIncomeScale[i];
			disposableIncome -= currBandSum;
			contribution = contribution + currBandSum * disposableIncomeValue[i]/100;
		}
	}
	return contribution;
}

calcTotalCapital = function(){
	disposableCapital = document.getElementById('txt_totalCapital').value;
	if(!checkNo(disposableCapital,'Please fill in your total disposable capital at step 4.')) {
		disposableCapital = NaN;
		return;
	}
}

calcCapital = function(){
	var contribution = 0;
	if (disposableCapital > disposableCapitalScale[1]) {
		contribution = NaN;
	}
	else if (disposableCapital < disposableCapitalScale[0]) {
		contribution = 0;
	}
	else {
		contribution = disposableCapital - disposableCapitalScale[0];
	}
	return contribution;
}

// Returns index of an element in the array into which range the value falls
getElemIndex = function(elem, array) {
	var result = -1;
	var i=0;
	for(i = 0; i < array.length-1; i++){
		if( (array[i] < elem && array[i+1] >= elem)) {
		     	result = i;
		     	break;
		}	
	}
	if (i==array.length-1) {
		result = i;
	}
	return result;
}

setUpElements = function(){
	document.getElementById("but_calculate").onclick = calculateNDisplay;
	document.getElementById("chk_benefits").onclick = doBenefits;		
	document.getElementById('chk_partner').onclick = calcTotalAllowances;
	document.getElementById('sel_children').onchange = calcTotalAllowances;
	//calcWeekly
	for( var i=0;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();
	}

