// handles data and calculations for calculating loan repayments etc.

var current_loanamount = 25000;
var current_loanterm = 10;
var current_creditrating = CREDIT_EXCELLENT;
var current_repayment = 0;

var min_loan = 12500;
var max_loan = 100000;

var CREDIT_EXCELLENT = 3;
var CREDIT_GOOD = 2;
var CREDIT_AVERAGE = 1;
var CREDIT_POOR = 0;

var credit_text = new Array();
credit_text[CREDIT_EXCELLENT] = "9.9% APR";
credit_text[CREDIT_GOOD] = "12.9% APR";
credit_text[CREDIT_AVERAGE] = "16.9% APR";
credit_text[CREDIT_POOR] = "22.9% APR";

/* These are annual - (MIR * 12) */
var rate = new Array();
rate[CREDIT_EXCELLENT] = 9.48;
rate[CREDIT_GOOD] = 12.2;
rate[CREDIT_AVERAGE] = 15.7;
rate[CREDIT_POOR] = 20.8;

// allow adjusting of positions of credit ratings
function GetCreditRating($trackvalue) {
	//return Math.round(value / 10);
	if ($trackvalue < 8) {
		return CREDIT_POOR;
	} else if ($trackvalue < 16) {
		return CREDIT_AVERAGE;
	} else if ($trackvalue < 24) {
		return CREDIT_GOOD;
	} else {
		return CREDIT_EXCELLENT;
	}
}

// (PV * IR) / (1 - Math.pow(1 + IR, -NP))
function PMT($amount, $term, $credit_rating) {
	$rate = rate[$credit_rating] / 1200;
	$payment=($rate+($rate/(Math.pow((1+$rate),($term*12))-1)))*$amount;
	$payment = $payment*100;$payment=Math.round($payment);$payment=$payment/100;
	
	return $payment;
}

function UpdateCalculatorFields($amount, $term, $credit_rating, $amount_control, $term_control, $creditrating_control, $repayment_control, $totalrepayment_control, $effectiverate_control, $apr_control) {
	
	$amount = Math.round($amount / 2500) * 2500;
	$term = Math.round($term / 5) * 5;
	
	var repayment = PMT($amount, $term, $credit_rating);
	var total = Math.round(repayment * ($term * 12));
	
	if ($amount_control != null) {
		$amount_control.innerHTML = "&pound;" + FormatNumber($amount.toFixed(0));
	}
	if ($term_control != null) {
		$term_control.innerHTML = $term + " years";
	}
	if ($creditrating_control != null) {
		$creditrating_control.innerHTML = credit_text[$credit_rating];
	}
	if ($repayment_control != null) {
		$repayment_control.innerHTML = "<b>&pound;" + FormatNumber(repayment.toFixed(0)) + "</b>";
		current_repayment = repayment.toFixed(2)	
	}
	if ($totalrepayment_control != null) {
		$totalrepayment_control.innerHTML = "&pound;" + FormatNumber(total.toFixed(2));
	}
	if ($effectiverate_control != null) {
		$effectiverate_control.innerHTML = rate.toFixed(2);
	}
	if ($apr_control != null) {
		$apr_control.innerHTML = 0 + "%";
	}		
}

function UpdateFastCalculatorFields($amount, $term, $credit_rating, $amount_control, $term_control, $creditrating_control, $repayment_control, $totalrepayment_control, $effectiverate_control, $apr_control) {
	
	$amount = Math.round($amount / 2500) * 2500;
	$term = Math.round($term / 5) * 5;
	
	var repayment = PMT($amount, $term, $credit_rating);
	var total = Math.round(repayment * ($term * 12));

	if ($amount_control != null) {
		$amount_control.innerHTML = "&pound;" + FormatNumber($amount.toFixed(0));
	}
	if ($term_control != null) {
		$term_control.innerHTML = $term + " years";
	}
	if ($creditrating_control != null) {
		$creditrating_control.innerHTML = credit_text[$credit_rating];
	}
	if ($repayment_control != null) {
		$repayment_control.innerHTML = "<b>&pound;" + FormatNumber(repayment.toFixed(0)) + "</b>";
		current_repayment = repayment.toFixed(2)			
	}
	if ($totalrepayment_control != null) {
		$totalrepayment_control.innerHTML = "&pound;" + FormatNumber(total.toFixed(2));
	}	
}

function FormatNumber(num, decpoint, sep) {
  // check for missing parameters and use defaults if so
  if (arguments.length == 2) {
    sep = ",";
  }
  if (arguments.length == 1) {
    sep = "";
    decpoint = ".";
  }
  // need a string for operations
  num = num.toString();
  // separate the whole number and the fraction if possible
  a = num.split(decpoint);
  x = a[0]; // decimal
  y = a[1]; // fraction
  z = "";


  if (typeof(x) != "undefined") {
    // reverse the digits. regexp works from left to right.
    for (i=x.length-1;i>=0;i--)
      z += x.charAt(i);
    // add seperators. but undo the trailing one, if there
    z = z.replace(/(\d{3})/g, "$1" + sep);
    if (z.slice(-sep.length) == sep)
      z = z.slice(0, -sep.length);
    x = "";
    // reverse again to get back the number
    for (i=z.length-1;i>=0;i--)
      x += z.charAt(i);
    // add the fraction back in, if it was there
    if (typeof(y) != "undefined" && y.length > 0)
      x += decpoint + y;
  }
  return x;
}
