// JavaScript Document

var BankChargesCalculator               =
{

    rows                                :     [],
    
    totals                              :     {},
    
    chargearray                         :     [],
    
    interestowedarray                   :     [],
    
    totaltoclaimarray                   :     [],
    
    rowsprint                           :     [],
    
    date                                :     [],

    UKdate                                :     [], 

	  charge                              :     0,    
	  
	  interestowed                        :     0,
    
    calculateValues                     :     function()
    {

      var interestowedHTMLElement         =     document.getElementById('interestowed');
      
      var totaltoclaimHTMLElement         =     document.getElementById('totaltoclaim');


      var yearSelectHTMLElement           =     document.getElementById('year');
      
      
      var year                                =     yearSelectHTMLElement.options[yearSelectHTMLElement.selectedIndex].value;
	
      this.date                           =     document.getElementById('month').value+ "/" +document.getElementById('day').value + "/" +year;
      this.UKdate                           =     document.getElementById('day').value+ "/" +document.getElementById('month').value + "/" +year; 
	
	    var datDate1                        =     Date.parse(this.date);
	
      var now                             =     new Date();
      
	    var datDate2                        =     Date.parse(now);
	    
	    this.charge                         =     document.getElementById('charge').value*1;
	    
	    var noofdays                        =     ((datDate2-datDate1)/(24*60*60*1000));
	    
	    this.interestowed                   =     this.charge*0.000219178*noofdays;
	
      this.totaltoclaim                   =     this.charge+this.interestowed;
      
	    interestowedHTMLElement.value       =     Math.round(this.interestowed*100)/100;
	
      totaltoclaimHTMLElement.value       =     Math.round(this.totaltoclaim*100)/100;
      

    },
    
    printVersion                        :     function(){
    

      
      var HTMLOutput                      =     "<html><head><title>Bank Charges Interest Calculations</title>";
      
      HTMLOutput                          +=    "<link rel=\"stylesheet\" href=\"style.css\">";

      HTMLOutput                          +=    "</head><body><h1>Bank Charges Interest Calculations</h1>";
      
      HTMLOutput                          +=    this.generateTable("print");
    
      HTMLOutput                          +=    "<script>window.print();</script></body></html>";
    
      var generator = window.open('','name','height=400,width=500');
      
      generator.document.write(HTMLOutput);
      
     generator.document.close();

    },
    
    clearlist                           :     function()
    {
    
      var calculator                   =     document.getElementById('calculator');
    	
      this.count                       =     1;

      this.rows                        =     [];
        
      this.chargearray                 =     [];
	
      this.interestowedarray           =     [];
	
      this.totaltoclaimarray           =     [];
      
	    calculator.innerHTML             =     "";
    
    },                                     
    
    remove                              :     function(value)
    {

        var calculator                      =     document.getElementById('calculator');

        this.rows.splice(value,1);
	
        this.chargearray.splice(value,1);
	
        this.interestowedarray.splice(value,1);
	
        this.totaltoclaimarray.splice(value,1);

        calculator.innerHTML                 =     this.generateTable();

    },

    addToRHS                             :      function()
    {
        var calculator                   =     document.getElementById('calculator');
        
        this.calculateValues();

        this.rows[this.rows.length]                          =      {};

        this.rows[(this.rows.length-1)].charge                   =      Math.round( this.charge * 100 ) /  100;
        
        this.rows[(this.rows.length-1)].date                     =      this.UKdate;

        this.rows[(this.rows.length-1)].interestowed             =      Math.round( this.interestowed * 100 ) / 100;
        
        this.rows[(this.rows.length-1)].totaltoclaim             =      Math.round( this.totaltoclaim  * 100  ) /  100;
        
        this.interestowedarray[this.interestowedarray.length]             =      this.rows[(this.rows.length-1)].interestowed;
        
        this.chargearray[this.chargearray.length]                   =      this.rows[(this.rows.length-1)].charge;        
        
        this.totaltoclaimarray[this.totaltoclaimarray.length]             =      this.rows[(this.rows.length-1)].totaltoclaim;        

        this.totalinterestowed                =     0;

        this.totalcharge                      =     0;

        this.fulltotaltoclaim                 =     0;

        this.count                            =     this.count+1;
        
        calculator.innerHTML                 =     this.generateTable();

    },

    generateTable                           :       function(media)
    {
  
        var totalcharge              =     0;
        
        var totalinterestowed          =     0;
        
        var fulltotaltoclaim           =     0;
    
        var tableHTML                  =      "";
        
        tableHTML                     +=      "<table width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" style=\"text-align:center;font-size:10pt;\">";
        
        tableHTML                      +=      "<tr>";        

        tableHTML                     +=      "<th> &nbsp;</th>";

        tableHTML                      +=      "<th>Charge Value </th>";
        
        tableHTML                      +=      "<th>Date</th>";        
        
        tableHTML                      +=      "<th>Interest</th>"; 
        
        tableHTML                      +=      "<th>Total</th>";
        
        tableHTML                      +=      (typeof(media)=="undefined")   ?   "<th> &nbsp;</th>":"";

        tableHTML                      +=      "</tr>";  

        for ( var x=0;  x<this.rows.length; x++ )
        {

             
              tableHTML                      +=      "<tr>";        
      
              tableHTML                      +=      "<td> &nbsp;</td>";
      
              tableHTML                      +=      "<td>"+this.rows[x].charge+"</td>";
              
              tableHTML                      +=      "<td>"+this.rows[x].date+"</td>"; 

              tableHTML                      +=      "<td>"+this.rows[x].interestowed+"</td>"; 
              
              tableHTML                      +=      "<td>"+this.rows[x].totaltoclaim+"</td>";
              
              tableHTML                      +=      (typeof(media)=="undefined")   ?   "<td><input type=\"button\"value=\"X\" onclick=\"BankChargesCalculator.remove("+ x +")\"></td>":"";
      
              tableHTML                      +=      "</tr>";

	         
	      }
	      for( var i=0; i<this.chargearray.length; i++ )
	      {
       
          totalcharge                    +=      parseFloat(this.chargearray[i], 10);

          totalinterestowed              +=      parseFloat(this.interestowedarray[i],10);
          
          fulltotaltoclaim               +=      parseFloat(this.totaltoclaimarray[i],10);

        
        }
	      
	      
        tableHTML                      +=	    "<tr><td><b>Total</b></td>";
        
        tableHTML                      +=	    "<td><b>£"+Math.round(totalcharge*100)/100+"</b></td>";
        
        tableHTML                      +=	    "<td>&nbsp;</td>";
        
        tableHTML                      +=	    "<td><b>£"+Math.round(totalinterestowed*100)/100+"</b></td>";
        
        tableHTML                      +=	    "<td><b>£"+Math.round(fulltotaltoclaim*100)/100+"</b></td>";
        
        tableHTML                      +=	    (typeof(media)=="undefined")    ?   "<td>&nbsp;</td></tr>":"</tr>";     
        
        tableHTML                      +=      "</table>";
        
        return tableHTML;

    }


}



