function emiTable(){
/*******************************************
 #- Object Properties
 *******************************************/
  this.currentRow = 0;
  this.Table = document.getElementById("emiTable");
  //---
  this.emi_month = 1;
  this.emi_year = 2007;
  this.emi_amount = 0;
  this.emi_inRate = 0;
  this.emi_period = 0;
  //---
  this.Year = 0;
  this.Month = 0;
  this.Payment = 0;
  this.bBalance = 0;
  this.eBalance = 0;
  this.Principal = 0;
  this.cPrincipal = 0;
  this.Intrest = 0;
  this.cIntrest = 0;
  //---
/*******************************************
 #- Object Methods
 *******************************************/
  this.toCurrency = function(N){
    var nm, pr = "", sign;
	nm = N;
	if(nm < 0){
	  N = N * (-1);
	}
	nm = nm.toString().replace(/\|\,/g,'');
	if(isNaN(N)){
	  pr = "0" + pr;
	}
	sign = (N == (N = Math.abs(N)));
	N = Math.floor(N * 100 + 0.50000000001);
	pr = N%100;
	N = Math.floor(N/100).toString();
	if(pr<10){
	  pr = "0" + pr;
	}
	for(i = 0; i < Math.floor((N.length-(1+i))/3); i++){
	  N = N.substring(0, N.length-(4*i+3))+','+N.substring(N.length-(4*i+3));
	}
	if(nm >=0){
	  return (((sign)?'':'-') + '' + N + '.' + pr);
	}else{
	  return (((sign)?'':'-') + '' + N + '.' + pr);
	}
  };
 
  // Start Calculation
  this.start = function(_year, _month, _amount, _intrest, _period){
    var obj = null, no = 0;
	window.status = 'Starting EMI Calculation..';
    this.emi_year = parseInt(_year);
    this.emi_month = parseInt(_month);
    this.emi_amount = parseInt(_amount);
    this.emi_inRate = parseFloat(_intrest);
    this.emi_period = parseInt(_period);
	no = this.emi_period;
	//---
    this.Year = this.emi_year;
    this.Month = this.emi_month;
	//--
    this.Payment = this.pmt(this.emi_inRate, this.emi_period, this.emi_amount);
    this.bBalance = this.emi_amount;
	//---
	document.getElementById("Monthly_payments").innerHTML = this.toCurrency(this.Payment);
	document.getElementById("Annual_payments").innerHTML = this.toCurrency(this.Payment * 12);
	//---
	while(this.bBalance > 0){
      this.Intrest = (this.bBalance * (this.emi_inRate / 12)) / 100;
      this.Principal = this.Payment - this.Intrest;
	  this.cPrincipal = (this.cPrincipal * 1) + this.Principal;
	  this.cIntrest = (this.cIntrest * 1) + this.Intrest;
      this.eBalance = this.bBalance - this.Principal;
	  if(no > 0){
		this.insertEntry();
		no--;
	  }else{
		window.status = 'Done';
		return;
	  }
	}
	window.status = 'Done';
  };
  // Reset the Table
  this.reset = function(){
    while(this.currentRow > 0){
	  this.Table.deleteRow(this.currentRow--);
	}
    this.currentRow = 0;
    //---
    this.emi_month = 1;
    this.emi_year = 2007;
    this.emi_amount = 0;
    this.emi_inRate = 0;
    this.emi_period = 0;
    //---
    this.Year = 0;
    this.Month = 0;
    this.Payment = 0;
    this.bBalance = 0;
    this.eBalance = 0;
    this.Principal = 0;
    this.cPrincipal = 0;
    this.Intrest = 0;
    this.cIntrest = 0;
  };
  
  //the pmt function
  this.pmt = function(AnnualInterestRate, YearsLength, Balance){
	var BAL = Balance;
	var INT = ((AnnualInterestRate/100) / 12);
	var MON = (YearsLength);
    var PMT = BAL * (INT / (1 - Math.pow(1 + INT, -MON)));
    return PMT;
  };
  // Add the Calculated detail[s] to the EMI Table
  this.insertEntry = function(){
    // Insert Row[s] / Column[s]
    var Row = this.Table.insertRow(++this.currentRow);
	var Col = new Array();
	Col[0] = Row.insertCell(0);
	Col[1] = Row.insertCell(1);
	Col[2] = Row.insertCell(2);
	Col[3] = Row.insertCell(3);
	Col[4] = Row.insertCell(4);
	Col[5] = Row.insertCell(5);
	Col[6] = Row.insertCell(6);
	Col[7] = Row.insertCell(7);
	Col[8] = Row.insertCell(8);
	Col[9] = Row.insertCell(9);
	// Set Value[s] for the same
	Col[0].innerHTML = this.currentRow + ". ";
	Col[2].innerHTML = MonthList[this.Month];
	Col[3].innerHTML = this.toCurrency(this.bBalance);
	Col[4].innerHTML = this.toCurrency(this.Payment);
	Col[5].innerHTML = this.toCurrency(this.Principal);
	Col[6].innerHTML = this.toCurrency(this.Intrest);
	Col[7].innerHTML = this.toCurrency(this.cPrincipal);
	Col[8].innerHTML = this.toCurrency(this.cIntrest);
	Col[9].innerHTML = this.toCurrency(this.eBalance);
	//---
	if(this.Month == 1){
	  Col[1].innerHTML = this.Year;	  
	}else{
	  Col[1].innerHTML = "&nbsp;";
	}
	if(this.Month == 12){
	  this.Month = 1;
	  this.Year++;
	}else{
	  this.Month++;
	}
	//---
	if(this.eBalance < 0){
	  this.eBalance = 0;
	}
	this.bBalance = this.eBalance;
  };
};
 
