//--------------------------------------------------
//  STUDENT POSSESSION CALC
//--------------------------------------------------

function possession_calc() {
    var itemsArray = new Array("hifi_value","portable_value","tv_value","camera_value","luggage_value","computer_value","pda_value","games_value","pictures_value","specs_value","phone_value","musical_value","sports_value","clothes_value","domestics_value","other_electrical_value","course_equip_value","other_books_value","jewellery_value","makeup_value","perfume_value","other_value");
    var nItemsArray= new Array(["number_cds",13.00],["number_vinyls",11.00],["number_videos",12.00],["number_dvds",20.00]);

    var total = 0;

    // Items which have an overall value
    for (var i=0; i< itemsArray.length; i++) {
        var item = itemsArray[i];
        var value = document.forms.possession_calc[item].value;

        if ( value ) {
            value = parseFloat(Number(value.replace(/(,)/g, "")));
            if (! isNaN(value) ) {
                total += value;
            }
        }
    }

    // check the cd/vinyl/videos/dvds
    for (var i=0; i<nItemsArray.length;i ++) {
        var elemId     = nItemsArray[i][0];
        var itemValue  = nItemsArray[i][1];
        var quantity   = document.forms.possession_calc[elemId].value;

        if ( quantity ) {
            quantity = parseFloat(Number(quantity));
            if (! isNaN(quantity) ) {
                total += itemValue * quantity;
            }
        }
    }

    if (document.getElementById ) {
        document.getElementById('totalValue').innerHTML = '&pound;' + makeFloat(total);
    }
}

//--------------------------------------------------
//  END STUDENT POSSESSION CALC
//--------------------------------------------------

function makeFloat(theFloat){
    if (theFloat) {
        var optionalRateFix = theFloat
        optionalRateFix = Math.round(optionalRateFix*100).toString();
        return optionalRateFix.substring(0,optionalRateFix.length-2) +'.'+ optionalRateFix.substring(optionalRateFix.length-2,optionalRateFix.length);
    } else {
        return '0.00';
    }
}

//--------------------------------------------------
//  UNIVERSITY FEES CALC
//--------------------------------------------------

function uniFeesCalc() {
    if (document.forms.uni_fees_calc.fees.value =="" || document.forms.uni_fees_calc.living_cost.value == "" || document.forms.uni_fees_calc.entertainment_costs.value == "" || document.forms.uni_fees_calc.other_costs.value == "" || document.forms.uni_fees_calc.education_starts.value == "" || document.forms.uni_fees_calc.education_length.value == "" || document.forms.uni_fees_calc.savings.value == ""){
      alert('Please complete all fields.')

    } else {
      var fees = parseFloat(Number(document.forms.uni_fees_calc.fees.value.replace(/(,)/g, "")));
      var living_cost = parseFloat(Number(document.forms.uni_fees_calc.living_cost.value.replace(/(,)/g, "")));
      var entertainment_costs = parseFloat(Number(document.forms.uni_fees_calc.entertainment_costs.value.replace(/(,)/g, "")));
      var other_costs = parseFloat(Number(document.forms.uni_fees_calc.other_costs.value.replace(/(,)/g, "")));
      var savings = parseFloat(Number(document.forms.uni_fees_calc.savings.value.replace(/(,)/g, "")));

      var education_starts = parseFloat(Number(document.forms.uni_fees_calc.education_starts.value));
      var education_length = parseFloat(Number(document.forms.uni_fees_calc.education_length.value));

      var total_cost = (fees + living_cost + entertainment_costs + other_costs) * education_length;

      // assume inflation 2.5%
      for (i=1; i <= education_starts; i++) {
         total_cost += (total_cost * 0.025);
      }

      // assume APR 5%
      var APR = parseInt(5);
      var rate = APR / 1200;
      var months = education_starts * 12;

      var divider = Math.pow((1 + rate), months);
      monthly_payments = (rate + (rate / (divider -1))) * total_cost;

      if (document.getElementById){
        document.getElementById('monthlyValue').innerHTML = '&pound;' + monthly_payments.toFixed(2);
        document.getElementById('totalValue').innerHTML = '&pound;' + total_cost.toFixed(2);
      }
    }
}

//--------------------------------------------------
//  END UNIVERSITY FEES CALC
//--------------------------------------------------

//--------------------------------------------------
//  EDUCATION CALC
//--------------------------------------------------

function educationCalc() {
    if (document.forms.edu_calc.fees.value =="" || document.forms.edu_calc.education_starts.value == "" || document.forms.edu_calc.education_length.value == "" || document.forms.edu_calc.savings.value == ""){
      alert('Please complete all fields.')
    } else {
      var fees = parseFloat(Number(document.forms.edu_calc.fees.value.replace(/(,)/g, "")));
      var savings = parseFloat(Number(document.forms.edu_calc.savings.value.replace(/(,)/g, "")));

      var education_starts = parseFloat(Number(document.forms.edu_calc.education_starts.value));
      var education_length = parseFloat(Number(document.forms.edu_calc.education_length.value));
      
      var total_cost = fees * education_length;
      var APR = parseInt(Number(document.forms.edu_calc.interest.value));

      //alert("total pre inflation "  + total_cost);
      	
      // assume inflation 2.5%
      for (i=1; i <= education_starts; i++) {
         total_cost += (total_cost * 0.025);
      }

      // assume APR 5%
      var APR = parseInt(5);
      var rate = APR / 1200;
      var months = education_starts * 12;

      var divider = Math.pow((1 + rate), months);
      monthly_payments = (rate + (rate / (divider -1))) * total_cost;

      if (document.getElementById){
        document.getElementById('monthlyValue').innerHTML = '&pound;' + monthly_payments.toFixed(2);
        document.getElementById('totalValue').innerHTML = '&pound;' + total_cost.toFixed(2);
      }
    }
}

//--------------------------------------------------
//  END EDUCATION CALC
//--------------------------------------------------



//--------------------------------------------------
//  INVESTMENT CALC
//--------------------------------------------------

function investCalc() {
    if (document.forms.invest_calc.save_years.value =="" || document.forms.invest_calc.interest.value =="" || document.forms.invest_calc.lump_sum.value =="" || document.forms.invest_calc.month_contribution.value =="" || document.forms.invest_calc.goal.value ==""){
      alert('Please complete all fields.')
    } else {
      var years = parseInt(Number(document.forms.invest_calc.save_years.value));
      var APR = parseInt(Number(document.forms.invest_calc.interest.value));
      var savings = parseFloat(Number(document.forms.invest_calc.lump_sum.value.replace(/(,)/g, "")));
      var month_contribution = parseFloat(Number(document.forms.invest_calc.month_contribution.value.replace(/(,)/g, "")));
      var goal = parseFloat(Number(document.forms.invest_calc.goal.value.replace(/(,)/g, "")));
     
      var total = 0;
      var monthly_mult = 1 + ((APR/100)/12);
      var months = (years * 12) - 1;

      if (savings > 1) {
        total = (total + month_contribution + savings) * monthly_mult;        

        for (i = 1; i <= (months); i++) {
          total = (total + month_contribution) * monthly_mult;
        }
      } else {

        for (i = 1; i <= (years * 12); i++) {
          total = (total + month_contribution) * monthly_mult;
        }
      }     
      
      var shortfall = 0;
      var month_shortfall = 0;
      if (goal > 0) {
        if (goal > total) { 
          shortfall = goal - total;

          var rate = APR / 1200;
          var months = years * 12;

          var divider = Math.pow((1 + rate), months);
          month_shortfall = (rate + (rate / (divider -1))) * shortfall;

        }
      }

      if (document.getElementById){
        document.getElementById('lumpShortfall').innerHTML = '&pound;' + makeFloat(shortfall);
        document.getElementById('monthShortfall').innerHTML = '&pound;' + makeFloat(month_shortfall);
        document.getElementById('totalValue').innerHTML = '&pound;' + makeFloat(total);
      }
    }
}

//--------------------------------------------------
//  END INVESTMENT CALC
//--------------------------------------------------

//--------------------------------------------------
//  PENSION CALC
//--------------------------------------------------

function pensionCalc() {
    if (document.forms.pension_calc.month_contrib.value =="" || document.forms.pension_calc.lump_sum.value == "" || document.forms.pension_calc.years.value == ""){
      alert('Please complete all fields.')
    } else {
      var month_contrib = parseFloat(Number(document.forms.pension_calc.month_contrib.value.replace(/(,)/g, "")));
      var savings = parseFloat(Number(document.forms.pension_calc.lump_sum.value.replace(/(,)/g, "")));

      var years = parseFloat(Number(document.forms.pension_calc.years.value));
      
      var APR = 7;
      var total = 0;
      var inflation_total = 0;
      var monthly_mult = 1 + ((APR/100)/12);
      var months = (years * 12) - 1;

      if (savings > 1) {
        total = (total + month_contrib + savings) * monthly_mult;        
        inflation_total = ((inflation_total + month_contrib + savings) * monthly_mult) - (((inflation_total + month_contrib + savings) * monthly_mult) * 0.025);

        for (i = 1; i <= (months); i++) {
          total = (total + month_contrib) * monthly_mult;
	  inflation_total = ((inflation_total + month_contrib) * monthly_mult) - (((inflation_total + month_contrib + savings) * monthly_mult) * 0.025);
        }
      } else {
        for (i = 1; i <= (years * 12); i++) {
          total = (total + month_contrib) * monthly_mult;
	  inflation_total = ((inflation_total + month_contrib) * monthly_mult) - (((inflation_total + month_contrib + savings) * monthly_mult) * 0.025);
        }
      }
       
      if (document.getElementById){
        document.getElementById('inflationTotalValue').innerHTML = '&pound;' + inflation_total.toFixed(2);
        document.getElementById('totalValue').innerHTML = '&pound;' + total.toFixed(2);
      }
    }
}

//--------------------------------------------------
//  END PENSION CALC
//--------------------------------------------------

//--------------------------------------------------
//  RETIREMENT CALC
//--------------------------------------------------

function retireCalc() {
    if (document.forms.retire_calc.annual_income.value =="" || document.forms.retire_calc.lump_sum.value == "" || document.forms.retire_calc.years.value == ""){
      alert('Please complete all fields.')
    } else {
      var annual_income = parseFloat(Number(document.forms.retire_calc.annual_income.value.replace(/(,)/g, "")));
      var savings = parseFloat(Number(document.forms.retire_calc.lump_sum.value.replace(/(,)/g, "")));

      var years = parseFloat(Number(document.forms.retire_calc.years.value));
      
      // assume 15 years of pension payouts
      var total_income = annual_income * 15;
      //alert(total_income);

      var interest = 6;
      var rate = interest / 1200;
      var months = years * 12;

      var divider = Math.pow((1 + rate), months);
      var multipler = 1 + ((interest/100));

      if (savings > 1) {
        total_income = total_income - savings;
        monthly_payments = (rate + (rate / (divider -1))) * total_income;

        for (i = 1; i <= years; i++) {
          total_income = total_income / multipler;
        }
      } else {
        monthly_payments = (rate + (rate / (divider -1))) * total_income;

        for (i = 1; i <= years; i++) {
          total_income = total_income / multipler;
        }

      }	

      if (monthly_payments < 1) {
        return;
      } 
      if (total_income < 1) {
        return;
      }

      if (document.getElementById){
        document.getElementById('monthValue').innerHTML = '&pound;' + monthly_payments.toFixed(2);
        document.getElementById('totalValue').innerHTML = '&pound;' + total_income.toFixed(2);
      }
    }
}

//--------------------------------------------------
//  END RETIREMENT CALC
//--------------------------------------------------

//--------------------------------------------------
//  MORTGAGE REPAYMENT CALC
//--------------------------------------------------


function makeFloat(theFloat){

      var optionalRateFix = theFloat
      optionalRateFix = Math.round(optionalRateFix*100).toString();
      theNewNumber = optionalRateFix.substring(0,optionalRateFix.length-2) +'.'+ optionalRateFix.substring(optionalRateFix.length-2,optionalRateFix.length);
      return theNewNumber
}


function mortgageRepayCalc() {
    if (document.forms.repay_calc.mortgage_amount.value =="" || document.forms.repay_calc.interest.value == "" || document.forms.repay_calc.years.value == "" || document.forms.repay_calc.months.value == ""){
      alert('Please complete all fields.')
    } else {
      var mortgage_amount = parseFloat(Number(document.forms.repay_calc.mortgage_amount.value.replace(/(,)/g, "")));
      var interest = parseFloat(Number(document.forms.repay_calc.interest.value.replace(/(,)/g, "")));

      var years = parseFloat(Number(document.forms.repay_calc.years.value));
      var repay_months = parseFloat(Number(document.forms.repay_calc.months.value));
      

      var rate = interest / 1200;
      var months = years * 12;
      months = months + repay_months;

      var divider = Math.pow((1 + rate), months);

      monthly_payments = (rate + (rate / (divider -1))) * mortgage_amount;


      // inflation as interest
      interest = 2.5;

      var rate = interest / 1200;
      var months = years * 12;
      months = months + repay_months;

      var divider = Math.pow((1 + rate), months);

      monthly_payments_interest = (rate + (rate / (divider -1))) * mortgage_amount;

      if (monthly_payments < 1) {
        return;
      } 

      if (document.getElementById){
        document.getElementById('monthValue').innerHTML = '&pound;' + makeFloat(monthly_payments)//.toFixed(2);
        document.getElementById('monthInterestValue').innerHTML = '&pound;' + makeFloat(monthly_payments_interest)//.toFixed(2);
      }
    }
}



function quickCalc(omortgage_amount, ointerest, oyears, otype) {
    if (omortgage_amount =="" || ointerest == "" || oyears == ""){
      alert('Please complete all fields.')
    } else {
      
      var mortgage_amount = parseFloat(Number(omortgage_amount.replace(/(,)/g, "")));
      var interest = parseFloat(Number(ointerest.replace(/(,)/g, "")));
      
      var years = parseFloat(Number(oyears));
      var repay_months = 0//parseFloat(Number(document.forms.repay_calc.months.value));
       

      var rate = interest / 1200;
      var months = years * 12;
      months = months + repay_months;

      var divider = Math.pow((1 + rate), months);

      monthly_payments = (rate + (rate / (divider -1))) * mortgage_amount;

      // inflation as interest
      interest = 2.5;

      var rate = interest / 1200;
      var months = years * 12;
      months = months + repay_months;

      var divider = Math.pow((1 + rate), months);

      monthly_payments_interest = (rate + (rate / (divider -1))) * mortgage_amount;

      if (monthly_payments < 1) {
        return;
      }
      if (document.getElementById){
        if (otype=='cr'){
          document.getElementById('quickMonthValue').innerHTML = '&pound;' + makeFloat(monthly_payments)
        
        }else{
          document.getElementById('quickMonthValue').innerHTML = '&pound;' + makeFloat(monthly_payments_interest)
        }
        //document.getElementById('quickMonthValue').innerHTML = '&pound;' + makeFloat(monthly_payments)
       // document.getElementById('quickMonthInterestValue').innerHTML = '&pound;' + makeFloat(monthly_payments_interest)
      }
      
    }
}





//--------------------------------------------------
//  END MORTGAGE REPAYMENT CALC
//--------------------------------------------------
