window.addOnloadListener(checkForm);
window.addOnloadListener(updatePrice);


function toggleAmount(id, display) {
    document.getElementById(id + "Amount").parentNode.style.display = display;
    document.getElementById(id + "InfoAmount").parentNode.style.display = display;
}

function togglePrice(id, display) {
    document.getElementById(id + "Price").style.display = display;
}

function editAmount(obj) {
    var id = obj.id;
    var amount = obj.value;
    var productId = id.replace(/Info/,"").replace(/Amount/,"");
    document.getElementById(productId + "Amount").value = amount;
    document.getElementById(productId + "InfoAmount").value = amount;
    createCookie("blaTempOrder" + productId, productId + ":" + amount, 1);
    updatePrice();
}

function isChecked(productId, type) {
    var result = false;
    if (type == "list") {
        if (document.getElementById(productId + "InfoInclude").checked==true) result = true;
    } else {
        if (document.getElementById(productId + "Include").checked==true) result = true;
    }
    return result;
}

function check(productId) {
    if (document.getElementById(productId + "Include")) {
        document.getElementById(productId + "Include").checked=true;
        togglePrice(productId,"inline");
        if (document.getElementById(productId + "Amount")) {
            toggleAmount(productId, "block");
        }
    }
    if (document.getElementById(productId + "InfoInclude")) {
        document.getElementById(productId + "InfoInclude").checked=true;
    }
    createCookie("blaTempOrder" + productId, productId + ":" + 1, 1);
}

function unCheck(productId) {
    if (document.getElementById(productId + "Include")) {
        document.getElementById(productId + "Include").checked=false;
        togglePrice(productId,"none");
        if (document.getElementById(productId + "Amount")) {
            toggleAmount(productId, "none");
        }
    }
    if (document.getElementById(productId + "InfoInclude")) {
        document.getElementById(productId + "InfoInclude").checked=false;
    }
    eraseCookie("blaTempOrder" + productId);
}

function include(obj) {
    var id = obj.id;
    var type = "";
    if (id.match(".*InfoInclude")) {
        type = "list";
    }
    var productId = id.replace(/Info/,"").replace(/Include/,"");
    if (isChecked(productId, type)) {
        check(productId);
        if (productId == "blw" && isChecked("blwp", type)) {
            unCheck("blwp");
        }
        if (productId == "blwp" && isChecked("blw", type)) {
            unCheck("blw");
        }
        if (productId == "blwf2" && isChecked("blwfp", type)) {
            unCheck("blwfp");
        }
        if (productId == "blwfp" && isChecked("blwf2", type)) {
            unCheck("blwf2");
        }
        if (productId == "blw650" && isChecked("blwob", type)) {
            unCheck("blwob");
        }
        if (productId == "blwob" && isChecked("blw650", type)) {
            unCheck("blw650");
        }
    } else {
        unCheck(productId);
    }
    updatePrice();
}

function updatePrice() {
    var btp = document.getElementById("bla_total_price");
    var discountInfo = document.getElementById("discount");
    var discount = 0;
    var totalprice = 0;
    var text = "";
    var discounttext = "";
    var included = document.getElementsByName("infoInclude");
    for (var item = 0 ; item < included.length; item++){
        if (included[item].checked) {
            var productId = included[item].id.replace(/InfoInclude/,"");
            var amount = 1;
            if (document.getElementById(productId + "Amount")) {
                if (document.getElementById(productId + "Amount").value != 1) {
                    amount = parseInt(document.getElementById(productId + "Amount").value) ? document.getElementById(productId + "Amount").value : 1;
                }
            }
            totalprice = parseInt(totalprice) + (parseInt(included[item].value) * parseInt(amount));
        }
    }
    if (isChecked("blw") && isChecked("blwf2")) discount = 1000;
    if (isChecked("blw") && isChecked("blwfp")) discount = 1000;
    if (isChecked("blwp") && isChecked("blwf2")) discount = 1000;
    if (isChecked("blwp") && isChecked("blwfp")) discount = 1000;
    totalprice = totalprice - discount;
    // change price
    text = "Ditt pris: " + formatPrice(totalprice) + " kr";
    btp.innerHTML = text;
    // show discount if discount > 0
    if (discount > 0) {
        discounttext = "Paketrabatt: -" + formatPrice(discount) + " kr";
        discountInfo.innerHTML = discounttext;
        discountInfo.style.display = "block";
    } else {
        discountInfo.style.display = "none";
    }
}

function checkForm() {
    var ca = document.cookie.split(";");
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==" ") c = c.substring(1,c.length);
        if (c.indexOf("blaTempOrder") == 0) {
            c=c.substring(c.indexOf("=")+1,c.length);
            c=c.split(":");
            if (c[1].length > 0) {
                document.getElementById(c[0] + "Include").checked=true;
                if (document.getElementById(c[0] + "InfoInclude")) {
                    document.getElementById(c[0] + "InfoInclude").checked=true;
                }
                if (document.getElementById(c[0] + "Amount")) {
                    document.getElementById(c[0] + "Amount").value = c[1];
                    toggleAmount(c[0],"block");
                }
                togglePrice(c[0],"inline");
            }
        }
    }
}
