﻿   // Validates the values on the console.
   function ValidateConsole(controlPrefix, id) 
   {   
      // Get elements from form
	  var inputTotal = document.getElementById(controlPrefix + '_txtTot' + id);
      var inputRepay = document.getElementById(controlPrefix + '_txtRep' + id);
	
	   // Set background color
	   inputTotal.style.backgroundColor = '#dddddd';
	   inputRepay.style.backgroundColor = '#dddddd';
	
	   // blank-out non-numeric illegal entries
	   if ((inputTotal.value * 1) > 0) 
	   {
	      inputTotal.style.backgroundColor = '#ffffff';
	   }
	   else 
	   {
		   inputTotal.value = "";
	   }
		
	   if ((inputRepay.value * 1) > 0) 
	   {
	      inputRepay.style.backgroundColor = '#ffffff';
	   }
	   else 
	   {
		   inputRepay.value = "";
	   }
		
	  if ((inputTotal.value + inputRepay.value) == 0) 
	  {
        inputTotal.value                 = '';
	    inputRepay.value                 = '';
	    inputTotal.style.backgroundColor = '#ffffff';
	    inputRepay.style.backgroundColor = '#ffffff';
	  }
	
	   // Perform calculation
	   PerformCalculation(controlPrefix);   
	   ShowCalculationSummary(controlPrefix);  
   }
   
   
   // Reset The Calculations when fields change
   function ResetCalculations(controlPrefix) 
   {
	   //Perform calculation
       PerformCalculation(controlPrefix);
	   ShowCalculationSummary(controlPrefix);  
   }
   
   
   // This prints some calculation summary text to the screen
   function ShowCalculationSummary(controlPrefix)
   {
       var Rate = 0.007166667;
       var APRRate = "8.6%"; //"8.60%";
       
       var RepaymentMonths      = 36;
       var TotalOwed            = document.getElementById(controlPrefix + '_txtTotalDebt').value;
       var TotalRepayment       = document.getElementById(controlPrefix + '_txtTotalRepayments').value;       
       
       if (document.getElementById(controlPrefix + '_rblCreditProfile_0').checked)
       {
            Rate = 0.007166667;
            APRRate = "8.6%"; //"8.60%";
       }
       
       if (document.getElementById(controlPrefix + '_rblCreditProfile_1').checked)
       {
            Rate = 0.00875;
            APRRate = "10.5%"; //"10.50%";
       }
       
       if (document.getElementById(controlPrefix + '_rblCreditProfile_2').checked)
       {
            Rate = 0.008916667;
            APRRate = "10.7%"; //"10.70%";
       }
       
       if(document.getElementById(controlPrefix + '_ddlLoanPeriod').value > 0 )
       {
            RepaymentMonths     = document.getElementById(controlPrefix + '_ddlLoanPeriod').value;
       }
       

       // Calculate new repayment using rate
       NewTotalRepayment = CurrencyFormatted((TotalOwed * Math.pow(eval(1 + Rate), RepaymentMonths/12))/(RepaymentMonths)); 
      
       // Set label text ONLY if enough info. has been provided 
       if(TotalOwed !='' && TotalRepayment !='' && TotalOwed !='0' && TotalRepayment !='0')
       {
           document.getElementById("couldBePaying").style.display   = "block";
           document.getElementById("savingYou").style.display       = "block";
           
           document.getElementById("couldBePaying").innerHTML   = (TotalRepayment > 0)                     ? ("Your new monthly payments could be &pound;" + NewTotalRepayment                                     + "</b> per month"):"";
           document.getElementById("savingYou").innerHTML       = (TotalRepayment - NewTotalRepayment > 0) ? ("A reduction of <strong>&pound;"                   + CurrencyFormatted(TotalRepayment - NewTotalRepayment) + "</strong> a month, this calculation is based on an APR rate of <strong>" + APRRate + "</strong>"):"Increase the repayment period for lower monthly payments" ;  
          
           document.getElementById("divResult").style.display = "block";
       }
       else
       {
           document.getElementById("couldBePaying").style.display   = "none";
           document.getElementById("savingYou").style.display       = "none";
           
           document.getElementById("couldBePaying").innerHTML   = "";
           document.getElementById("savingYou").innerHTML       = "";
           
           document.getElementById("divResult").style.display = "none";
           
       }
   }
          
    // Currency format function used in debt console calculator
    function CurrencyFormatted(amount)
    {
	    var i = parseFloat(amount);
	    if(isNaN(i)) { i = 0.00; }
	    var minus = '';
	    if(i < 0) { minus = '-'; }
	    i = Math.abs(i);
	    i = parseInt((i + .005) * 100);
	    i = i / 100;
	    s = new String(i);
	    if(s.indexOf('.') < 0) { s += '.00'; }
	    if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	    s = minus + s;
	    return s;
    }

   
   // This performs the total repayment and total debt calculation.
   function PerformCalculation(controlPrefix) 
   {	
	   var totalDebt = 0;
	   var totalRepayment = 0;
	
	   for (i = 1; i <= 4; i++) 
	   {
	      if (document.getElementById(controlPrefix + '_txtTot' + i).value != '')
	      {
	        totalDebt += + eval(document.getElementById(controlPrefix + '_txtTot' + i).value);
	      }
	      if (document.getElementById(controlPrefix + '_txtRep' + i).value != '')
	      {
	        totalRepayment += + eval(document.getElementById(controlPrefix + '_txtRep' + i).value);
	      }
		}
   
      // Set values of totals
      document.getElementById(controlPrefix + '_txtTotalDebt').value = totalDebt;
      document.getElementById(controlPrefix + '_txtTotalRepayments').value = totalRepayment;
   }
