﻿// JScript File

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 (tn == "input" || tn == "select") {
                    var val;
                    if (el.getAttribute("type") == "checkbox")
                        val = el.checked;
                    else
                        val = el.value;
                    qs += el.id + "=" + val + "&";
                }
                qs += GetChildNodeQS(el);
            }
        }
    }
    return qs;
}

function ToggleListSearching(show, doc) {
    var loading = doc.getElementById("product-list-searching");
    var ifr = frames["ProductListFrame"];
    
    if (ifr) {
        var noproducts = ifr.document.getElementById("product-list-noproducts");
        if (noproducts)
            noproducts.style.display = "none";
    }
        
    loading.style.display = (show ? "block" : "none");
    //ifr.className = (show ? "product-list-transparent" : "product-list-normal");
}


function UpdateProductList(p) {
    var pEl = document.getElementById("page");
    
    if (pEl) {
        if (p)
            pEl.value = p;
        else
            pEl.value = "1";
    }

    var f = document.getElementById("spec-form");
    var qs = "?" + GetChildNodeQS(f);  
    var ifr = document.getElementById("ProductListFrame");
    if (ifr) {
        ifr.src = "/shop/ProductList.aspx" + qs + Math.random();
        ToggleListSearching(true, document);
    }
}

function SetProductPage(p) {
    document.getElementById("page").value = p;
    UpdateProductList(p);
    location.hash = "#";
}

function SetAmount(a, n) {
    document.getElementById("amount").value = a;
    document.getElementById("amount_" + n).checked = true;
//    document.getElementById("amount_" + n + "_bottom").checked = true;
    UpdateProductList();
    location.hash = "#";
}


var lastUpdatedProductRow;

function AddToCart(productID, amount) {
    var ids = productID.split("|");
    var doIt = true;
    for (var i = 0; i < ids.length; i++) {
        var defaultAmount = document.getElementById("DefaultAmount_" + ids[i]).value;
        if (defaultAmount < 4 && amount > defaultAmount) {
            var msg = "Det finns tyvärr endast " + defaultAmount + " st av denna dimension i lager.";
            alert(msg);
            doIt = false;
        } 
    }
    if (doIt) {

        new Ajax.Request("/inc/Events.aspx?func=Events/AddToCart", {
            method: "post",
            parameters: {
                productID: productID,
                amount: amount
            },
            onSuccess: function() { self.location.reload() },
            onFailure: function() { alert('Ett fel uppstod.') }
        });
    
//        var _post;
//        if (parent)
//            _post = parent.ob_post;
//        else
//            _post = ob_post;
//            
//        _post.ResetParams();
//        _post.AddParam("productID", productID);
//        _post.AddParam("amount", amount);
//        _post.post("/Events.aspx", "AddToCart", ClientUpdateCart);
    }
}

function AddDimensionToCart(productID, amount) {
    var stock = parseInt(document.getElementById("DefaultAmount_" + productID).value);
    var amountInCart = parseInt(document.getElementById("AmountInCart_" + productID).value);
    amount = parseInt(amount);

    if (((amount + amountInCart) <= stock) || stock >= 4) {
        document.getElementById("AmountInCart_" + productID).value = amount + amountInCart;
        AddToCart(productID, amount);
    }
    else
        alert("Du kan inte tyvärr beställa fler än det finns i lager av denna produkt.");
}

function GetIdFromList(listID)
{
    var list = document.getElementById(listID);
    var value = list.options[list.selectedIndex].value.split(";");
    return value[0];
}

function OpenImagePopup(url)
{
    var width = 650;
    var height = 650;
    var x = screen.width;
	var y = screen.height;
	window.open(url, "ProductImage", "top=" + parseInt(y/2-height/2-16) + ",left=" + parseInt(x/2-width/2-5) + ",width=" + width + ",height=" + height + ",scrollbars=1, status=0");
}


var __selectedProductType = null;
var __selectedManufacturerName = null;
var __selectedModel = null;
var __selectedDimension = null;
var __selectedPayment = 153;


function UpdateDimensionSum(el, nr, price) {
    if (!isNaN(el.value)) {
        document.getElementById("rowSum_" + nr).value = (price * parseInt(el.value)) + " SEK";
    }
}
