﻿
function PrintMonthPayment(price, amount, payment, elementID) {
    new Ajax.Request("/inc/Events.aspx?func=Events/GetMonthPayment", {
        method: "post",
        parameters: {
            price: price,
            amount: amount,
            payment: payment
        },
        onSuccess: function(transport) { $(elementID).innerHTML = transport.responseText },
        onFailure: function(){ alert('Ett fel uppstod.') }
    });
}

function GetChildNodeQS(topEl) {
    var qs = "";
    if (topEl.childNodes.length > 0) {
        for (var i = 0; i < topEl.childNodes.length; i++) {
            var el = topEl.childNodes[i];
            if (el.nodeType == 1) {
                var tn = el.tagName.toLowerCase();
                if (el.getAttribute("rel") == "filter" && (tn == "input" || tn == "select")) {
                    var val;
                    if (tn == "select")
                        val = el.options[el.selectedIndex].value;
                    else if (el.getAttribute("type") == "checkbox")
                        val = el.checked;
                    else
                        val = el.value;
                    qs += el.id + "=" + escape(val) + "&";
                }
                qs += GetChildNodeQS(el);
            }
        }
    }
    return qs;
}

function FilterProductList(p, a, s, sd) {
    if ($("productList"))
        $("productList").style.display = "none";
    if ($("noProducts"))
        $("noProducts").style.display = "none";        
    $("productListLoading").style.display = "block";
    var qs = GetChildNodeQS(document.forms[0]);
    self.location.href = "?p=1&a=" + a + "&s=" + s + "&sd=" + sd + "&" + qs;
}

function ShowCustomDimensionLists() {
    $("customDimensionLists").style.display = "block";
//    $("customLinkHolder").style.display = "none";
    
    if ($("extraDimensionLists"))
        $("extraDimensionLists").style.display = "block";
    
    if ($("cd").options[$("cd").options.length-1].value != "") {
        var opt = document.createElement("option");
        opt.value = "";
        opt.text = "Anpassad dimension";
        $("cd").options.add(opt);
    }
    $("cd").options.selectedIndex = $("cd").options.length - 1;
}

function SetCustomDimensions(list, p, a, s, sd) {
    if (list.options[list.selectedIndex].value.length > 0) {
        // Save wheelTypeSelection
        var wheelTypeSelection = $("wheelTypeSelection");
        if (wheelTypeSelection) {
            wheelTypeSelection.style.display = "none";
            document.forms[0].appendChild(wheelTypeSelection);
        }
        
        if ($("customDimensionLists"))
            $("customDimensionLists").parentNode.removeChild($("customDimensionLists"));
        if ($("extraDimensionLists"))
            $("extraDimensionLists").parentNode.removeChild($("extraDimensionLists"));
        FilterProductList(p, a, s, sd);
    }
    else
        $("customDimensionLists").style.display = "block";
}