
// Parse HTML to get the product price
function getCurrentPrice(PriceContainerID,CurrencySymbol) {
    var price = 0;
    var divnlqprices = document.getElementById(PriceContainerID);
    if (divnlqprices != null) {
        var matches = divnlqprices.innerHTML.match("(\\d{1,3}(\\.\\d{3})*)(\\,\\d{2}) ");
        if (matches != null) {
            var strAmount = matches[0].replace('.', '').replace('.', '').replace(',', '.');
            price = parseFloat(strAmount);
        }
    }
    return price;
}

// Handle click event on buy button
function buyClicked(Insurance, InsuranceCheckBoxID) {

    var shopUrl = window.location.protocol + "//" + window.location.host
    var proceed = true;

    if (document.getElementById(InsuranceCheckBoxID).checked) {
        var amountText = document.getElementById("amount").value;
        if (isNaN(parseInt(amountText))) {
            proceed = false;
        }
        if (proceed) {
            var img = new Image();
            var amp = String.fromCharCode(38);
            var url = shopUrl + "/shop/basket.asp?Add=1" + amp + "Productid=" + Insurance.Number + amp + "amount=" + amountText
            img.src = url;
        }
    }            
    return proceed;
}

// Attach click event to buy button
function attachBuyEvent(Insurance, InsuranceCheckBoxID)
{
	var inputElems = document.getElementsByTagName("input");
	for (var i = 0; i < inputElems.length; i++) {
	    var input = inputElems.item(i);
	    if (input.attributes.getNamedItem("type").nodeValue == "image") {
	        input.onclick = function() { buyClicked(Insurance, InsuranceCheckBoxID) };
	    }
	}
}

// Returns an Insurance Product based on the specified product price
function getInsuranceProduct(price) {

  var Insurance = new InsuranceProduct();

  
    // Set insurance product based on price
    if (price >= 2500 & price <= 4999) {
        Insurance.Number = "forsikring1";
        Insurance.Price = "799"
    }
    else if (price >= 5000 & price <= 7499) {
        Insurance.Number = "forsikring2";
        Insurance.Price = "899"
    }
    else if (price >= 7500 & price <= 9999) {
        Insurance.Number = "forsikring3";
        Insurance.Price = "999"
    }
    else if (price >= 10000 & price <= 14999) {
        Insurance.Number = "forsikring4";
        Insurance.Price = "1499"
    }
    else if (price >= 15000 & price <= 19999) {
        Insurance.Number = "forsikring5";
        Insurance.Price = "1999"
    }
    else if (price >= 20000 & price <= 24999) {
        Insurance.Number = "forsikring6";
        Insurance.Price = "2499"
    }
    else if (price >= 25000 & price <= 29999) {
        Insurance.Number = "forsikring7";
        Insurance.Price = "2999"
    }
    else if (price >= 30000 & price <= 34999) {
        Insurance.Number = "forsikring8";
        Insurance.Price = "3499"
    }
    else if (price >= 35000 & price <= 39999) {
        Insurance.Number = "forsikring9";
        Insurance.Price = "3999"
    }
    else if (price >= 40000 & price <= 44999) {
        Insurance.Number = "forsikring9";
        Insurance.Price = "4499"
    }
    else if (price >= 45000 & price <= 49999) {
        Insurance.Number = "forsikring10";
        Insurance.Price = "4999"
    }
    else {
        return null
    }
    return Insurance 
}

// Object to hold info for the Insurance Product
function InsuranceProduct() {
    this.Number;
    this.Price;
}

//Disable Insurance for specific root catagories
function disableInsuranceOptionForRootCatagory() {

    var excludedCatagories = new Array("2518","2407","2120");
    var rootCategoryContainer = document.getElementById("RootCatagory")
    if (rootCategoryContainer != null) {
        for (var i = 0; i < excludedCatagories.length; i++) {

            if (rootCategoryContainer.value == excludedCatagories[i]) {
                return true
            }
        }
    }
    return false;
}

function displayInsuranceOption(PriceContainerID, CurrencySymbol, InsuranceContainerID, InsuranceCheckBoxID, InsurancePriceContainerID) {

    // Get the element to hold the Insurance Price
    var insurancePriceContainer = document.getElementById(InsurancePriceContainerID);
    var insuranceContainer = document.getElementById(InsuranceContainerID);
    
    if (insurancePriceContainer != null && insuranceContainer !=null) {

        // Get Insurance product matching the current product price
        var Insurance = getInsuranceProduct(getCurrentPrice(PriceContainerID, CurrencySymbol))

        if (Insurance != null && disableInsuranceOptionForRootCatagory() == false) {

            // Show insurance layer
            insuranceContainer.style.display = '';
            insurancePriceContainer.innerHTML = Insurance.Price;

            // Attach event to the buy button to allow the Insurance product to be added to basket with the main product
            attachBuyEvent(Insurance, InsuranceCheckBoxID);

        }
        else {
            insuranceContainer.style.display = 'none';
        }
    }
}
