//initialize variables
num_of_years=5;
values=new Array(num_of_years);
cumulative=new Array(num_of_years);
scale_factor=250;

//defined section
premium=4;
lapse_rate=3;
commission=27.5;

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

//this script performs calculations based on the data provided in the web form
function recalculate() {
//form variables section
var monthly_sales=document.forms.setvars.monthly_sales.value;
if (monthly_sales>20) { monthly_sales=20; document.forms.setvars.monthly_sales.value=20; }
var average_benefit=document.forms.setvars.average_benefit.value;

//calculated variables
premium_calculate=average_benefit*(premium/100);
lapse_calculate=(100-lapse_rate)/100;
commission_calculate=commission/100;
benefit=premium_calculate*lapse_calculate*commission_calculate;
start=0; end=0; this_sales=0; cum_sales=0;

//10 year loop to work out graph - this calculates the values and puts them into an array
for (y=0; y<num_of_years; y++) {
start=(y*12)+1; end=start+12; this_sales=0;
for (i=start; i<end; i++) { this_sales+=i*monthly_sales; }
values[y]=this_sales*benefit;
for (i=start; i<end; i++) { cum_sales+=i*monthly_sales; }
cumulative[y]=cum_sales*benefit;
}

//get window size
var myWidth = 0, myHeight = 0;
if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement &&
      ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  
var graphwidth=myWidth-130;
var graphheight=300;
var markerwidth=Math.ceil(graphwidth/4)-1;
var markerheight=Math.ceil(graphheight/4);
var biggestcum=cumulative[num_of_years-1];
var scale_break=125000;
//test thing - set the biggest number on the scale to 3 million
maxbiggestcum=1000000;
biggestcum=Math.ceil(cumulative[num_of_years-1]/scale_break)*scale_break;

if (biggestcum>maxbiggestcum) biggestcum=maxbiggestcum;

scale_factor=biggestcum/graphheight;

//we have worked out the values, now to draw them
for (y=0; y<num_of_years; y++) {
document.getElementById('bar_year'+y).title="Year "+(y+1)+": £"+addCommas(Math.ceil(values[y]));
if (values[y]<biggestcum) { document.getElementById('bar_year'+y).style.height=(Math.ceil(values[y]/scale_factor)+1)+"px";
document.getElementById('bar_year'+y).style.marginTop=(graphheight-(Math.ceil(values[y]/scale_factor)))+"px"; }
else { document.getElementById('bar_year'+y).style.height=(Math.ceil(biggestcum/scale_factor)+1)+"px";
document.getElementById('bar_year'+y).style.marginTop="0px"; }
document.getElementById('bar_cum'+y).title="Rolling Total: £"+addCommas(Math.ceil(cumulative[y]));
if (cumulative[y]<biggestcum) { document.getElementById('bar_cum'+y).style.height=(Math.ceil(cumulative[y]/scale_factor)+1)+"px";
document.getElementById('bar_cum'+y).style.marginTop=(graphheight-(Math.ceil(cumulative[y]/scale_factor)))+"px"; }
else { document.getElementById('bar_cum'+y).style.height=(Math.ceil(biggestcum/scale_factor)+1)+"px";
document.getElementById('bar_cum'+y).style.marginTop="0px"; }
document.getElementById('y'+y).innerHTML="<strong>Year "+(y+1)+":</strong><br><br>£"+addCommas(Math.ceil(values[y]));
document.getElementById('c'+y).innerHTML="<strong>Rolling<br>Total:</strong><br>£"+addCommas(Math.ceil(cumulative[y]));
} //thus endeth the for statement

//quick quarters on graph
for (q=1; q<5; q++) {
document.getElementById('marker'+q).style.height=markerheight+"px";
document.getElementById('marker'+q).innerHTML="£"+addCommas((Math.ceil(biggestcum/4)*q));
}

return false;
}