
var coupon;
var maturity;//when is the bond going to mature
var lastCoupon;
var nextCoupon;

function displayBondDetails()
{
	var bond = bondForms[document.bondForm.bondSelect.value];
	var bondDetails = new Array();
	if(bond != null) {
		var settlement = str2dt(document.bondForm.settlementDate.value);
		maturity = str2dt(bond.maturity);
		if (settlement > maturity) {
			coupon = null;
			calculateBond();
			return;
		}
		lastCoupon = new Date(maturity);
		lastCoupon.setYear(settlement.getFullYear()+1);
		while (settlement < lastCoupon) {
			lastCoupon.setMonth(lastCoupon.getMonth()-6);
		}
		nextCoupon = new Date(lastCoupon);		
		nextCoupon.setMonth(nextCoupon.getMonth()+6);
		coupon = bond.coupon;
		bondDetails["Name"] = bond.name;
		bondDetails["Issuer"] = bond.issuer;
		bondDetails["Coupon"] = coupon+' %';
		bondDetails[''] = '';
		bondDetails['Dates'] = '';
		bondDetails["Maturity"] = dt2dtstr(maturity);
		bondDetails["Last Coupon"] = dt2dtstr(lastCoupon);
		bondDetails["Next Coupon"] = dt2dtstr(nextCoupon);
	}
	calculateBond();
	var bondText = '';
	for (var i in bondDetails) 
	  bondText += '<span class="label">'+i+'</span>' + bondDetails[i] + '<br />';
	document.getElementById("bond_details").innerHTML = bondText;
}

function calculateBond()
{
  if (document.bondForm.norminal.value == "" ||
	    document.bondForm.yield.value == "" || 
			document.bondForm.bondSelect.value == "space" ||
			coupon == null ) {
			document.getElementById("calc_bond").innerHTML = "";
			return;
	}
	var F, E, J, G, X, Y, nominal, tempDate, settlement;	
	settlement = str2dt(document.bondForm.settlementDate.value);
	nominal = parseFloat(document.bondForm.norminal.value); 
	Y = parseFloat(document.bondForm.yield.value); 
	
	for (F = -1, tempDate = new Date(settlement); tempDate < maturity; F++) 
		addMonths(tempDate, 6);
	E = days_between(settlement, nextCoupon);
	J = days_between(lastCoupon, nextCoupon);
	
	X = 1 + Y/200;
	G = E/J + (F);
	tempDate = new Date(settlement);
	addMonths(tempDate, 1);
	if (tempDate < nextCoupon) F++;
	var price = nominal*((100 / Math.pow(X, G)) + (((coupon/2) / (Math.pow(X, G)*(Y / 200))) * (Math.pow(X, F)-1)))/100;
	document.getElementById("calc_bond").innerHTML = '<b>All in Price</b><br />' + number_format(price, 2, '.', ',')+" N$";	
}

function addMonths(date, value) {
	return date.setMonth(date.getMonth()+value);
}

function days_between(date1, date2) {	
    var ONE_DAY = 1000 * 60 * 60 * 24;
	
    var a = date1.getTime();
    var b = date2.getTime();
	
    var diff = Math.abs(b - a);
	  //alert(date1 + "\n"+ date2)
    return (Math.round(diff / ONE_DAY));

}

function number_format (number, decimals, dec_point, thousands_sep)
{
  var exponent = "";
  var numberstr = number.toString ();
  var eindex = numberstr.indexOf ("e");
  if (eindex > -1)
  {
    exponent = numberstr.substring (eindex);
    number = parseFloat (numberstr.substring (0, eindex));
  }
  
  if (decimals != null)
  {
    var temp = Math.pow (10, decimals);
    number = Math.round (number * temp) / temp;
  }
  var sign = number < 0 ? "-" : "";
  var integer = (number > 0 ? 
      Math.floor (number) : Math.abs (Math.ceil (number))).toString ();
  
  var fractional = number.toString ().substring (integer.length + sign.length);
  dec_point = dec_point != null ? dec_point : ".";
  fractional = decimals != null && decimals > 0 || fractional.length > 1 ? 
               (dec_point + fractional.substring (1)) : "";
  if (decimals != null && decimals > 0)
  {
    for (i = fractional.length - 1, z = decimals; i < z; ++i)
      fractional += "0";
  }
  
  thousands_sep = (thousands_sep != dec_point || fractional.length == 0) ? 
                  thousands_sep : null;
  if (thousands_sep != null && thousands_sep != "")
  {
	for (i = integer.length - 3; i > 0; i -= 3)
      integer = integer.substring (0 , i) + thousands_sep + integer.substring (i);
  }
  
  return sign + integer + fractional + exponent;
}

function initialize() {
	var newOption;
	var bondSelect = document.getElementById('bondSelect');
	for (var i in bondForms) {
		if (bondForms[i] == null) {
  		newOption = new Option('', 'space', true, true);
		} else {
  		newOption = new Option(bondForms[i].name, i);
		}
		bondSelect.options[bondSelect.length] = newOption;
	}
	bondSelect.options[2].selected = true;

  document.bondForm.settlementDate.value = dt2dtstr(new Date());
  displayBondDetails();
  calculateBond();
}
