//PCS change service Calculator
//JavaScript Code
var saved =0;

function calculate()  {

				var thisform=document.charges;          //totalfunds, totaldirect, totaltraded, premiums

        var total_funds=evalnum(thisform.totalfunds.value);
        var total_direct=evalnum(thisform.totaldirect.value);

				var pensTotal= total_funds+total_direct;
        var discount = 0;
        if (pensTotal>250000) { discount+=.35*(pensTotal-250000)*.006};
        if (pensTotal>150000) { discount+=.25*(Math.min(250000,pensTotal)-150000)*.006};
        if (pensTotal>50000) { discount+=.15*(Math.min(150000,pensTotal)-50000)*.006};
        //alert(discount);
        var total_traded=evalnum(thisform.totaltraded.value);
        var total_premiums=evalnum(thisform.premiums.value);

                           //"ppadminister","pppolicyfees","ppfundadmin","ppfundsecs","ppfundcommis","ppfundswitch"
        thisform.ppadminister.value=evalpound(.006*pensTotal);
        thisform.pppolicyfees.value=evalpound(.002*pensTotal);
        thisform.ppfundadmin.value=evalpound(.015*total_funds);
        thisform.ppfundsecs.value=evalpound(.003*total_funds);
        thisform.ppfundcommis.value=evalpound(.05*total_premiums);
        thisform.ppfundswitch.value=evalpound(.03*total_traded);

        thisform.mnadminister.value=evalpound(.006*pensTotal-discount);
        thisform.mnpolicyfees.value=evalpound(48);
        thisform.mnfundadmin.value=evalpound(.007*total_funds);

        thisform.ppadminister.disabled=true;
        thisform.pppolicyfees.disabled=true;
        thisform.ppfundadmin.disabled=true;
        thisform.ppfundsecs.disabled=true;
        thisform.ppfundcommis.disabled=true;
        thisform.ppfundswitch.disabled=true;

        thisform.mnadminister.disabled=true;
        thisform.mnpolicyfees.disabled=true;
        thisform.mnfundadmin.disabled=true;
        thisform.mnfundsecs.disabled=true;
        thisform.mnfundcommis.disabled=true;
        thisform.mnfundswitch.disabled=true;
        thisform.mnallocation.disabled=true;


        thisform.ppTotal.disabled=true;
        thisform.mnTotal.disabled=true;

        if (thisform.C1.checked==true) {
          thisform.mnallocation.value=evalpound(.005*pensTotal);
          }
        else {
          thisform.mnallocation.value=evalpound(0);
          }

        thisform.mnfundsecs.value=evalpound(.003*total_funds);
        thisform.mnfundcommis.value=evalpound(.00*total_premiums);
        thisform.mnfundswitch.value=evalpound(.02*total_traded);

        thisform.ppTotal.value = evalpound(.006*pensTotal+ .002*pensTotal + .015*total_funds + .003*total_funds + .05*total_premiums + .03*total_traded);
        thisform.mnTotal.value= evalpound(.006*pensTotal-discount + (48) + .007*total_funds + .003*total_funds + .00*total_premiums + .03*total_traded);

        saved = (.006*pensTotal+ .002*pensTotal + .015*total_funds + .003*total_funds + .05*total_premiums + .03*total_traded);
        saved-= (.006*pensTotal + (48) + .007*total_funds + .003*total_funds + .00*total_premiums + .03*total_traded);

        if (thisform.C1.checked==true) {
          thisform.mnTotal.value=evalpound(evalnum(thisform.mnTotal.value) + .003*pensTotal+.002*pensTotal);
          }

}



// -------------------------------------------------------------------------
// strips out invalid charaters from a string to convert to a numeric value
function evalnum(numstr) {

	var i,c;
	var ret="";
	var p=false;

	for (i=numstr.length-1; i>=0; i--) {
		c=numstr.charAt(i);

		if (c == ".") {
			if (p == false) p=true;
			else c="";
		}

		if ((c < "0" || c > "9") && c != ".") c="";

		ret = c + ret;
	}

	if (ret=="") ret="0";
	return parseFloat(ret);

}


// ----------------------------------------------------------------------------
// shows a number as pounds
function evalpound(num) {

	var i,l,d;
	var nums;
	var ret;

  nums = String(Math.round(num*100));

	while (nums.length <3) nums = "0" + nums;

	l = nums.length-3;
  ret = "" //"." + nums.charAt(l+1) + nums.charAt(l+2);

	d=0;
	for (i=l; i>=0; i--) {
		ret = nums.charAt(i) + ret;
		d++;
		if (d==3 && i>0) {
			ret="," + ret;
			d=0;
		}
	}

	ret = "£" + ret;
	return ret;

}
















