
function GECalculator_GetMonthlyPayment(FinancedAmount, DownPaymentPeriod, MonthlyPaymentRef, TotalFinancingCostRef, EffectiveInterestRateRef, TotalFinancingCostAfterTaxRef) {
    GECalculator_CalculateByPeriod(FinancedAmount, DownPaymentPeriod, GECalculator_MonthlyInterestRate, GECalculator_MonthlyFee, GECalculator_PercentageDeductionAfterTax, MonthlyPaymentRef, TotalFinancingCostRef, EffectiveInterestRateRef, TotalFinancingCostAfterTaxRef);
}

function GECalculator_CalculateByPeriod(LoanAmount, NumberOfMonths, MonthlyRate, MonthlyFee, PercentageDeductionAfterTax, MonthlyPaymentRef, TotalFinancingCostRef, EffectiveInterestRateRef, TotalFinancingCostAfterTaxRef) {
    var MonthlyPayment               = GECalculator_CalculateMonthlyPayment(LoanAmount, NumberOfMonths, MonthlyRate, MonthlyFee);
    var PaymentPlanPayment           = GECalculator_CreatepaymentPlanPayment(MonthlyPayment, NumberOfMonths);
    var PaymentPlanPeriod            = GECalculator_CreatepaymentPlanPeriod(NumberOfMonths);
    var TotalPayment                 = GECalculator_CalculateTotalPayment(MonthlyPayment, NumberOfMonths, PaymentPlanPayment, PaymentPlanPeriod);
    var FinancingCost                = TotalPayment - LoanAmount;
    var InternalInterest             = GECalculator_CalculateInternalInterest(MonthlyPayment, NumberOfMonths, LoanAmount, PaymentPlanPayment, PaymentPlanPeriod);
    var FinancingCostAfterTax        = GECalculator_FinancingCostAfterTax(FinancingCost, PercentageDeductionAfterTax);

    MonthlyPaymentRef[0]             = MonthlyPayment;
    TotalFinancingCostRef[0]         = FinancingCost + LoanAmount;
    EffectiveInterestRateRef[0]      = (InternalInterest*100).toFixed(2);
    TotalFinancingCostAfterTaxRef[0] = FinancingCostAfterTax;
}

function GECalculator_GetMaxPeriod(FinancedAmount) {
    return GECalculator_CalculateMaximumMonths(FinancedAmount, GECalculator_MonthlyInterestRate, GECalculator_MonthlyFee, GECalculator_MinMonths, GECalculator_MaxMonths, GECalculator_PercentageCreditLimit, GECalculator_MinMonthlyPayment);
}

function GECalculator_CalculateMaximumMonths(LoanAmount, MonthlyRate, MonthlyFee, MinMonths, MaxMonths, PercentageCreditLimit, MinMonthlyPayment) {
    var MinPayment = GECalculator_CalculateMinimumPayment(LoanAmount, MonthlyRate, MonthlyFee, MinMonths, MaxMonths, PercentageCreditLimit, MinMonthlyPayment);
        
    var intMonths = MaxMonths;
    while (intMonths > MinMonths) {
        var MonthlyPayment = GECalculator_CalculateMonthlyPayment(LoanAmount, intMonths, MonthlyRate, MonthlyFee);
        if (!isNaN(MonthlyPayment)) {
            if (MonthlyPayment >= MinPayment) {
                return intMonths;
            }
        }
        intMonths -= 1;
    }
    return 0;
}

function GECalculator_CalculateMinimumPayment(LoanAmount, MonthlyRate, MonthlyFee, MinMonths, MaxMonths, PercentageCreditLimit, MinMonthlyPayment) {
    var CalculatedMin = GECalculator_CalculateMonthlyPayment(LoanAmount, MaxMonths, MonthlyRate, MonthlyFee);
    var PercentageMin = PercentageCreditLimit * LoanAmount;
    return Math.max(CalculatedMin, Math.max(MinMonthlyPayment, PercentageMin));
}

function GECalculator_CreatepaymentPlanPayment(MonthlyAmount, NumberOfMonths) {
    var PaymentPlanPayment = new Array();
    var I = 0;
    var x = 0;
    var periods = NumberOfMonths;
    
    while (periods > 0) {
        PaymentPlanPayment[I] = MonthlyAmount;
        I += 1;
        periods -= 1;
    }
    return PaymentPlanPayment;
}

function GECalculator_CreatepaymentPlanPeriod(NumberOfMonths) {
    var PaymentPlanPeriod = new Array();
    var I = 0;
    var x = 0;
    var periods = NumberOfMonths;
    
    while (periods > 0) {
        PaymentPlanPeriod[I] = periods;
        I += 1;
        periods -= 1;
    }
    return PaymentPlanPeriod;
}

function GECalculator_CalculateInternalInterest(MonthlyAmount, NumberOfMonths, LoanAmount, PaymentPlanPayment, PaymentPlanPeriod) {
    var dblRate = 0.5;
    var dblRateModifier = dblRate;
    var dblPresentValue = 0;

    while (Math.abs(dblPresentValue - LoanAmount) > 0.001) {
        while (dblPresentValue < LoanAmount) {
            dblRate = (dblRate + (dblRateModifier * -1));

            var dblSum = 0;
            for (x=0;x<NumberOfMonths;x++) {
                dblSum += (PaymentPlanPayment[x] / Math.pow((1 + dblRate), PaymentPlanPeriod[x]));
            }
            dblPresentValue = dblSum;
        }
        dblRateModifier = dblRateModifier / 2;
    
        while (dblPresentValue > LoanAmount) {
            dblRate = (dblRate + dblRateModifier);

            var dblSum = 0;
            for (x=0;x<NumberOfMonths;x++) {
                dblSum += (PaymentPlanPayment[x] / Math.pow((1 + dblRate), PaymentPlanPeriod[x]));
            }
            dblPresentValue = dblSum;
        }
        dblRateModifier = dblRateModifier / 2;
    }

    return GECalculator_ToAnual(dblRate);
}

function GECalculator_ToAnual(Rate) {
    return (Math.pow((1 + Rate), 12) - 1);
}

function GECalculator_CalculateTotalPayment(MonthlyAmount, NumberOfMonths, PaymentPlanPayment, PaymentPlanPeriod) {
    var dblSum = 0;
    for (x=0;x<NumberOfMonths;x++) {
        dblSum += PaymentPlanPayment[x];
    }

    return dblSum;
}

function GECalculator_CalculateMonthlyPayment(LoanAmount, NumberOfMonths, MonthlyRate, MonthlyFee) {
    var dblA = GECalculator_CalculateA(LoanAmount, MonthlyRate, MonthlyFee, NumberOfMonths);
    var intU = GECalculator_CalculateU(LoanAmount, MonthlyRate, MonthlyFee, NumberOfMonths, dblA);
    var dblTreshold = GECalculator_CalculateTreshold(MonthlyRate, NumberOfMonths, intU, MonthlyFee);

    if (LoanAmount <= dblTreshold) {
        return GECalculator_CalculateP(LoanAmount, MonthlyRate, MonthlyFee, intU, NumberOfMonths);
    } else {
        return GECalculator_CalculateP(LoanAmount, MonthlyRate, MonthlyFee, intU, -1);
    }
}

function GECalculator_CalculateA(LoanAmount, MonthlyInterestRate, MonthlyFee, NumberOfMonths) {
    var dblSmallerPeriodicInterestForMonths = GECalculator_CalculateSmallerPeriodicInterest(MonthlyInterestRate, NumberOfMonths);
    var dblLargerPeriodicInterestForMonths  = GECalculator_CalculateLargerPeriodicInterest(MonthlyInterestRate, NumberOfMonths);

    return LoanAmount - 1001 * dblSmallerPeriodicInterestForMonths + MonthlyFee * dblLargerPeriodicInterestForMonths / (MonthlyInterestRate * (1 + MonthlyInterestRate));
}

function GECalculator_CalculateSmallerPeriodicInterest(MonthlyInterestRate, NumberOfMonths) {
    return 1 - Math.pow(1 + MonthlyInterestRate, -NumberOfMonths);
}       

function GECalculator_CalculateLargerPeriodicInterest(MonthlyInterestRate, NumberOfMonths) {
    return 1 + Math.pow(1 + MonthlyInterestRate, -NumberOfMonths + 1);
}       

function GECalculator_CalculateU(LoanAmount, MonthlyInterestRate, MonthlyFee, NumberOfMonths, a) {
    var dblFirstNominator  = GECalculator_CalculateFirstNominatorPartOfU(MonthlyInterestRate, MonthlyFee);
    var dblSecondNominator = GECalculator_CalculateSecondNominatorPartOfU(LoanAmount, MonthlyInterestRate, a, MonthlyFee, NumberOfMonths);
    var dblDenominator     = GECalculator_CalculateDenominatorPartOfU(MonthlyInterestRate);
    var dblU = (dblFirstNominator - dblSecondNominator) / dblDenominator;
    return Math.floor(dblU);
}       

function GECalculator_CalculateFirstNominatorPartOfU(MonthlyInterestRate, MonthlyFee) {
    return GECalculator_log10(2.0 * MonthlyFee / MonthlyInterestRate);
}       

function GECalculator_CalculateSecondNominatorPartOfU(LoanAmount, MonthlyInterestRate, a, MonthlyFee, NumberOfMonths) {
    return GECalculator_log10(a - Math.sqrt(Math.pow(a, 2) - (4 * MonthlyFee / MonthlyInterestRate) * (LoanAmount + (MonthlyFee / MonthlyInterestRate)) * Math.pow(1 + MonthlyInterestRate, -NumberOfMonths - 1)));
}

function GECalculator_CalculateDenominatorPartOfU(MonthlyInterestRate) {
    return GECalculator_log10((1 + MonthlyInterestRate));
}

function GECalculator_CalculateTreshold(MonthlyInterestRate, NumberOfMonths, u, MonthlyFee) {
    var dblSmallerPeriodicInterestForMonths     = GECalculator_CalculateSmallerPeriodicInterest(MonthlyInterestRate, NumberOfMonths);
    var dblSmallerPeriodicInterestForMonthsAndU = GECalculator_CalculateSmallerPeriodicInterest(MonthlyInterestRate, (NumberOfMonths - u));
    var dblSmallerPeriodicInterestForU          = GECalculator_CalculateSmallerPeriodicInterest(MonthlyInterestRate, u);
    return 1001 * dblSmallerPeriodicInterestForMonths / dblSmallerPeriodicInterestForMonthsAndU - (MonthlyFee / MonthlyInterestRate) * dblSmallerPeriodicInterestForU;
}

function GECalculator_CalculateP(LoanAmount, MonthlyInterestRate, MonthlyFee, u, NumberOfMonths) {
    var dblSmallerPeriodicInterestForU = GECalculator_CalculateSmallerPeriodicInterest(MonthlyInterestRate, u);

    if (NumberOfMonths < 0) {
        return MonthlyInterestRate * (LoanAmount - 1001 * Math.pow(1 + MonthlyInterestRate, -u)) / dblSmallerPeriodicInterestForU + MonthlyFee;
    } else {
        var dblSmallerPeriodicInterestForMonths = GECalculator_CalculateSmallerPeriodicInterest(MonthlyInterestRate, NumberOfMonths);
        return (MonthlyInterestRate * LoanAmount + MonthlyFee * dblSmallerPeriodicInterestForU) / dblSmallerPeriodicInterestForMonths;
    }
}

function GECalculator_FinancingCostAfterTax(FinancingCost, PercentageDeductionAfterTax) {
    return (FinancingCost * (1 - PercentageDeductionAfterTax));
}

function GECalculator_log10(x) {
    return Math.log(x)/Math.log(10);
}

function GECalculator_FormatToCurrency(nStr) {
    nStr = Math.ceil(nStr) + '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + '.' + '$2');
    }
    return x1 + x2 + ",-";
}

function GECalculator_SetPaymentPeriods(FinancedAmount, extended) {
    var i;
    var selectedOptionValue = 0;
    var Added = 0;
    var PaymentOptions = new Array();
    
    PaymentOptions[PaymentOptions.length] = '3';
    PaymentOptions[PaymentOptions.length] = '6';
    PaymentOptions[PaymentOptions.length] = '9';
    PaymentOptions[PaymentOptions.length] = '12';
    PaymentOptions[PaymentOptions.length] = '24';
    PaymentOptions[PaymentOptions.length] = '36';
    PaymentOptions[PaymentOptions.length] = '48';
    PaymentOptions[PaymentOptions.length] = '60';

    var FinancingPaymentPeriodControl = document.getElementById("FinancingPayment");
    var NumberOfMonths                = GECalculator_GetMaxPeriod(FinancedAmount);
	NumberOfMonthsMax[0]              = NumberOfMonths;
    
    for (i = 0; i < PaymentOptions.length; i++) {
        if (NumberOfMonths > PaymentOptions[i]) {
            var option   = document.createElement("option");
			if (extended == true) {
                option.value = GECalculator_FormatToCurrency(GECalculator_CalculateMonthlyPayment(FinancedAmount, PaymentOptions[i], GECalculator_MonthlyInterestRate, GECalculator_MonthlyFee)) + ' pr mnd (' + PaymentOptions[i] + ' mnd)';
                option.text  = GECalculator_FormatToCurrency(GECalculator_CalculateMonthlyPayment(FinancedAmount, PaymentOptions[i], GECalculator_MonthlyInterestRate, GECalculator_MonthlyFee)) + ' pr mnd (' + PaymentOptions[i] + ' mnd)';
			} else {
                option.value = PaymentOptions[i];
                option.text  = PaymentOptions[i] + ' mnd';
			}

						if (PaymentOptions[i] == findBestMonth(FinancedAmount)) {
							  selectedOptionValue = i;
						}

            FinancingPaymentPeriodControl.options.add(option, i);
            Added +=1;
        }
    }

    var option2 = document.createElement("option");

    if (extended == true) {
        option2.value = GECalculator_FormatToCurrency(GECalculator_CalculateMonthlyPayment(FinancedAmount, NumberOfMonths, GECalculator_MonthlyInterestRate, GECalculator_MonthlyFee)) + ' pr mnd (' + NumberOfMonths + ' mnd)';
		option2.text  = GECalculator_FormatToCurrency(GECalculator_CalculateMonthlyPayment(FinancedAmount, NumberOfMonths, GECalculator_MonthlyInterestRate, GECalculator_MonthlyFee)) + ' pr mnd (' + NumberOfMonths + ' mnd)';
	} else {
        option2.value = NumberOfMonths;
		    option2.text  = NumberOfMonths + ' mnd';
	  }
  FinancingPaymentPeriodControl.options.add(option2, Added);
//  FinancingPaymentPeriodControl.options[FinancingPaymentPeriodControl.options.length - 1].setAttribute('selected', 'selected');
  FinancingPaymentPeriodControl.options[selectedOptionValue].setAttribute('selected', 'selected');
  FinancingPaymentPeriodControl.options[selectedOptionValue].selected = true;
}

function GECalculator_Calculate(FinancedAmount) {
    var FinancingPaymentPeriodControl = document.getElementById("FinancingPayment");
    var NumberOfMonths                = FinancingPaymentPeriodControl.value;
    var MonthlyPayment                = new Array(0);
    var TotalFinancingCost            = new Array(0);
    var EffectiveInterestRate         = new Array(0);
    var TotalFinancingCostAfterTax    = new Array(0);
    var DownPaymentPeriod             = new Array(0);


        NumberOfMonths = FinancingPaymentPeriodControl.value;
        GECalculator_GetMonthlyPayment(FinancedAmount, NumberOfMonths, MonthlyPayment, TotalFinancingCost, EffectiveInterestRate, TotalFinancingCostAfterTax);

        var OverrideMonthlyPayment         = GECalculator_CalculateAndDisplayMinimumPayment(FinancedAmount);

        GECalculator_viewBox('ge_finansing');
        if (showMoreOnStart == true) {
            GECalculator_viewBox('ge_finansing_more');
            document.getElementById('finansing_link').style.display = 'none';
        }

        var OverrideTotalCreditCost        = TotalFinancingCostRef[0];
        var FinancingOverrideForThisPeriod = NumberOfMonthsMax[0];

        if (FinancingOverrideForThisPeriod == NumberOfMonths) {
            if (OverrideMonthlyPayment != '') {
                MonthlyPayment[0]     = OverrideMonthlyPayment;
            }

            if (OverrideTotalCreditCost != '') {
                TotalFinancingCost[0] = OverrideTotalCreditCost;
           }
        }

        var OverrideMonths = findBestMonth(FinancedAmount);
        GECalculator_GetMonthlyPayment(FinancedAmount, OverrideMonths, MonthlyPaymentRef, TotalFinancingCostRef, EffectiveInterestRateRef, TotalFinancingCostAfterTaxRef);
        var OverrideMonthlyPayment        = GECalculator_CalculateAndDisplayMinimumPayment(FinancedAmount);

        document.getElementById("FinancingAmountDIV").childNodes[0].nodeValue                  = GECalculator_FormatToCurrency(MonthlyPayment[0]);
        document.getElementById("FinancingCostDIV").childNodes[0].nodeValue                    = GECalculator_FormatToCurrency(TotalFinancingCost[0]);
        document.getElementById("FinancingInternalInterestDIV").childNodes[0].nodeValue        = EffectiveInterestRate[0] + " %";
        document.getElementById("FinancingNominelInterestDIV").childNodes[0].nodeValue         = (100 * 12 * GECalculator_MonthlyInterestRate).toFixed(2) + " %";
        document.getElementById("FinancingMonthlyInterestRateDIV").childNodes[0].nodeValue     = (100 * GECalculator_MonthlyInterestRate).toFixed(2) + " %";
        document.getElementById("FinancingCostAfterTaxDIV").childNodes[0].nodeValue            = GECalculator_FormatToCurrency(TotalFinancingCostAfterTax[0]);
        document.getElementById("FinancingInternalPriceDIV").childNodes[0].nodeValue           = GECalculator_FormatToCurrency(FinancedAmount);
        //Udkommenteret da det "åbenbart" er medregnet i den samlet pris
        //document.getElementById("FinancingMonthlyFeeDIV").childNodes[0].nodeValue              = GECalculator_FormatToCurrency(GECalculator_MonthlyFee);

        document.getElementById("FinancingOverrideForThisPeriodDIV").childNodes[0].nodeValue   = OverrideMonths;
        document.getElementById("FinancingOverrideMonthlyPaymentDIV").childNodes[0].nodeValue  = GECalculator_FormatToCurrency(MonthlyPaymentRef[0]);
        document.getElementById("FinancingOverrideTotalCreditCostDIV").childNodes[0].nodeValue = GECalculator_FormatToCurrency(TotalFinancingCostRef[0]);

}

function GECalculator_CalculateAndDisplayMinimumPayment(LoanAmount) {
	if ((LoanAmount >= GECalculator_MinLoanAmount) && (LoanAmount <= GECalculator_MaxLoanAmount)) {
		var MonthlyPayment = GECalculator_PercentageCreditLimit * LoanAmount;
		if (MonthlyPayment < GECalculator_MinMonthlyPayment) {
			return GECalculator_MinMonthlyPayment;
		} else {
			return Math.ceil(MonthlyPayment);
		}
	}
	return 0;
}

function GECalculator_calcNewLoan(FinancedAmount) {
   if(FinancedAmount >= GECalculator_MinLoanAmount) {
       GECalculator_GetMonthlyPayment(FinancedAmount, GECalculator_GetMaxPeriod(FinancedAmount), MonthlyPaymentRef, TotalFinancingCostRef, EffectiveInterestRateRef, TotalFinancingCostAfterTaxRef);
       GECalculator_Calculate(FinancedAmount);
   }
}

function GECalculator_viewBox(whichLayer) {
    var elem;

    if( document.getElementById ) // this is the way the standards work
        elem = document.getElementById( whichLayer );
    else if( document.all ) // this is the way old msie versions work
        elem = document.all[whichLayer];
    else if( document.layers ) // this is the way nn4 works
        elem = document.layers[whichLayer];

    elem.style.display = 'block';
}

function GECalculator_draw_small_pricebox(priceId, urllink) {
    document.write('<div style="display: none;" id="' + priceId + '">');
    document.write('<strong>Finansiering kr <span id="' + priceId + '_FinancingOverrideMonthlyPaymentDIV">-</span> pr mnd (<span id="' + priceId + '_FinancingOverrideForThisPeriodDIV">-</span> mnd.)</strong><br />');
    document.write('Kredittkjøpspris kr <span id="' + priceId + '_FinancingOverrideTotalCreditCostDIV">-</span> <a href="' + urllink + '">Eff. rente</a>');
    document.write('  </div>');
}

function GECalculator_Calculate_Small(FinancedAmount, priceId) {
    if(FinancedAmount >= GECalculator_MinLoanAmount) {
        var NumberOfMonths         = findBestMonth(FinancedAmount);
        GECalculator_GetMonthlyPayment(FinancedAmount, NumberOfMonths, MonthlyPaymentRef, TotalFinancingCostRef, EffectiveInterestRateRef, TotalFinancingCostAfterTaxRef);
//        var OverrideMonthlyPayment = GECalculator_CalculateAndDisplayMinimumPayment(FinancedAmount);

        GECalculator_viewBox(priceId);
        document.getElementById(priceId + "_FinancingOverrideForThisPeriodDIV").childNodes[0].nodeValue   = NumberOfMonths;
//        document.getElementById(priceId + "_FinancingOverrideMonthlyPaymentDIV").childNodes[0].nodeValue  = GECalculator_FormatToCurrency(OverrideMonthlyPayment);
        document.getElementById(priceId + "_FinancingOverrideMonthlyPaymentDIV").childNodes[0].nodeValue  = GECalculator_FormatToCurrency(MonthlyPaymentRef[0]);
        document.getElementById(priceId + "_FinancingOverrideTotalCreditCostDIV").childNodes[0].nodeValue = GECalculator_FormatToCurrency(TotalFinancingCostRef[0]);
    }
}


function hideMultiElement(elementId, hideAction, block) {
    if (document.layers) {
        // Opera
        if (document.layers[elementId] != null) {
            document.layers[elementId].display               = (hideAction ? 'none' : (onlyBlock || block ? 'block' : 'table-row'));
        }
    } else if (document.all) {
        //Internet Exploder
        if (document.all[elementId] != null) {
            document.all[elementId].style.display            = (hideAction ? 'none' : (onlyBlock || block ? 'block' : 'table-row'));
        }
    } else if (document.getElementById) {
        //Firefix, Safari, Netscape
        if (document.getElementById(elementId) != null) {
            document.getElementById(elementId).style.display = (hideAction ? 'none' : (onlyBlock || block ? 'block' : 'table-row'));
        }
    }
}

function findBestMonth(price) {
	var months = 0;
	if (price < 3300) {
		months = 12;
	} else if( price < 4300) {
		months = 24;
	} else
		months = 36;
	return months;
}




   // GE Money Bank produkt -informationer
   var GECalculator_MonthlyInterestRate         = 0.0165;
   var GECalculator_MonthlyFee                  = 45;
   var GECalculator_MinLoanAmount               = 1990;
   var GECalculator_MaxLoanAmount               = 50000;
   var GECalculator_MinMonths                   = 1;
   var GECalculator_MaxMonths                   = 100;
   var GECalculator_MinMonthlyPayment           = 199;
   var GECalculator_MaxMonthlyPayment           = 55000;
   var GECalculator_PercentageCreditLimit       = 0.03;
   var GECalculator_PercentageDeductionAfterTax = 0.28;

   // Plads-holdere til informationer
   var MonthlyPaymentRef                        = new Array(0);
   var TotalFinancingCostRef                    = new Array(0);
   var EffectiveInterestRateRef                 = new Array(0);
   var TotalFinancingCostAfterTaxRef            = new Array(0);
   var NumberOfMonthsMax                        = new Array(0);

