var defaultText = "Please complete the figures above.";
var disposableIncome = 0;
var disposableCapital = 0;

var max_disposableIncome = 238;
var max_disposableCapital = 1664;

//vars for weekly allowance
var allowance_partner = 37.30;
var allowance_child = 57.57;

//vars for weekly allowance
var t_allowance_first = 335;
var t_allowance_second = 200;
var t_allowance_third = 100;

var _weeklyAllowances = 0;
var _totalAllowances = 0;

//vars for question popups
 var totalQuestions = 9;
 
var contribScale		=new Array(0, 102, 109, 116, 123, 130, 137, 144, 151, 158, 165, 174, 180, 187, 194, 201, 208, 215, 222, 229, 238);
var contribScaleValues		=new Array(0, 7,   14,  21,  28,  35,  42,  49,  56,  63,  70,  77,  84,  91,  98,  105, 112, 119, 126, 135);
var diagnosticScale		=new Array(0, 102, 128, 156, 184, 212, 238);
var diagnosticScaleValues	=new Array(0, 7,   14,  21,  28,  35);

//functions

popupWindow = function(){
	var _pg = 'aa_help.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;
}

calculate = function(){
	calcAllowances();
	calcIncome();
	calcCapital();
	document.getElementById('p_answer').innerHTML = calcContribution();	
}

calcAllowances = function(){
	calcWeeklyAllowances();
	calcTotalAllowances();
}

doBenefits = function(){	
	if (document.getElementById('chk_benefits').checked) {
		document.getElementById('txt_netIncome').value = 0;
		document.getElementById('txt_maintenance').value = 0;
		document.getElementById('txt_netIncome').disabled=true;
		document.getElementById('txt_maintenance').disabled=true;
	}
	else {
		document.getElementById('txt_netIncome').disabled=false;
		document.getElementById('txt_maintenance').disabled=false;
	}
}

calcWeeklyAllowances  = 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;
	_weeklyAllowances = roundPoint(_tempVal,2);
	document.getElementById('txt_weeklyAllowances').value = _weeklyAllowances;
}

calcTotalAllowances  = function(){
	var _tempValT;
	_tempValT = 0;
	var partnerBoolean = document.getElementById('chk_partner').checked;
	var noChildren = document.getElementById('sel_children').value;
	if(partnerBoolean) _tempValT += 1;
	_tempValT += parseFloat(noChildren);
	var _tOut =0;;
	for(var i=0;i<_tempValT;i++){
		if(i==0)  _tOut += t_allowance_first;
		if(i==1) _tOut += t_allowance_second;
		else if(i>1)_tOut += t_allowance_third;
	}
	_totalAllowances = roundPoint(_tOut,2);;
	document.getElementById('txt_allowances').value = _totalAllowances;
}

calcIncome = function(){
	//get values
	var _weeklyIncome = document.getElementById('txt_netIncome').value;
	if(!checkNo(_weeklyIncome,'Please fill in your net weekly income at step 3.')) {
		disposableIncome = NaN;
		return;
	}
	var _maintenance = document.getElementById('txt_maintenance').value;
	if(!checkNo(_maintenance,'Please fill in your maintenance payments at step 3.')) {
		disposableIncome = NaN;
		return;
	}
	//calc
	disposableIncome = roundPoint( _weeklyIncome - _maintenance - _weeklyAllowances,2);
	//display results
	document.getElementById('txt_disposableIncome').value = disposableIncome;
}

calcCapital = function(){
	var _totalCapital = document.getElementById('txt_totalCapital').value;
	if(!checkNo(_totalCapital,'Please fill in your total capital at step 4.')) {
		disposableCapital = NaN;
		return;
	}
	disposableCapital = _totalCapital - _totalAllowances;
	document.getElementById('txt_disposableCapital').value = disposableCapital;
}


// Calculates contribution due
calcContribution = function(){
	var eligible = false;
	var benefits = document.getElementById("chk_benefits").checked;
	var text = "";
	if (isNaN(disposableIncome) || isNaN(disposableCapital)) {
		return defaultText;
	}
	// Receives income support - check capiatl and return with a verdict
	if (benefits) {
		if (disposableCapital > max_disposableCapital) {
			text = 	"Based on the above figures, you are <b>unlikely</b> to qualify for advice and assistance";
		}
		else {
			text = 	"Based on the above figures, you are <b>likely</b> to qualify for advice and assistance.<br/><br/>You will <b>not</b> have to pay any contribution.";
		}
		return text;
	}
	// Check if eligible on capital; if still eligible, continue. Return otherwise.
	if (disposableCapital > max_disposableCapital) {
		text = 	"Based on the above figures, you are <b>unlikely</b> to qualify for advice and assistance.";
		return text;
	}
	// Negative or 0 weekly income dont work.
	if (disposableIncome <= 0) disposableIncome = 1;
	var type = document.getElementById("sel_type").value;
	var array;
	var arrayValues; 
	// Full fee
	var array = contribScale;
	var arrayValues = contribScaleValues;
	var scaleRangeInd = getElemIndex(disposableIncome, array);
	var contribution = arrayValues[scaleRangeInd];
	// Not eligible
	if (scaleRangeInd > array.length-2) {	
		text = "Based on the above figures, you are <b>unlikely</b> to qualify for advice and assistance";
		return text;
	}
	// Eligible
	else {	
		text = "Based on the above figures, you are <b>likely</b> to qualify for advice and assistance.<br/><br/>"
		if (scaleRangeInd == 0) {
			text = text + "You will not have to pay any contribution.";
		}
		else {
			text = text + "However, you will have to pay a contribution to your solicitor of:<br/><br/> &pound; " + contribution + " of full fees";
		}
	}
	// Case type - children's, civil, etc...
	var type = document.getElementById("sel_type").value;
	var feeName = " of diagnostic fees.  <br/><br/>Your solicitor will tell you which amount you would have to pay, as this depends on what your legal problem is about. You can also read more in our FAQs - see below.";
	if (type == 3) { //children's
		return text + "."
	}
	else if (type == 2) { //criminal
		feeName = " of general fees.  <br/><br/>Your solicitor will tell you which amount you would have to pay, as this depends on what your legal problem is about.  You can also read more in our FAQs - see below."
	}
	// Diagnostic fee
	array = diagnosticScale;
	arrayValues = diagnosticScaleValues;		
	var scaleRangeInd = getElemIndex(disposableIncome, array);
	contribution = arrayValues[scaleRangeInd];
	// Not eligible. 
	// Eligible
	if (scaleRangeInd <= array.length-2) {
		if (scaleRangeInd > 0) {
			text = text + "<br/><b>OR</b><br/> &pound; "+ contribution + feeName;
		}
	}
	// Not eligible. NB the code should not get here if not eligible in the first place
	return text;
}

// 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 = calculate;
	document.getElementById("chk_benefits").onclick = doBenefits;	
	document.getElementById('chk_partner').onclick = calcAllowances;
	document.getElementById('sel_children').onchange = calcAllowances;
	//calcWeekly
	for( var i=1;i<totalQuestions + 1;i++){
		_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();
	}

