﻿
var _cartVisible = false;
var _reloadPage = false;


function ToggleCartOpen(open) {
    _cartVisible = open;
}

function UpdateCartInfo(transport) {
    LoadCartInfo();
}

function UpdateCartAmounts() {
    var productString = "";
    var inputs = document.getElementById("cart-table").getElementsByTagName("input");
    for (var i = 0; i < inputs.length - 1; i++) {
        if (inputs[i].getAttribute("type") == "hidden") {
            if (inputs[i + 1].value != "0")
                productString += inputs[i].value + ",";
        }
        else if (inputs[i].getAttribute("type") == "text") {
            if (!isNaN(inputs[i].value)) {
                if (parseInt(inputs[i].value) > 0)
                    productString += inputs[i].value + "!";
            }
        }
    }
    
    // Schparen kundvagnus
    new Ajax.Request("/shop/Events.aspx", {
        method: "get",
        parameters: {
            func: "UpdateCart",
            productString: productString
        },
        onSuccess: UpdateCartInfo,
        onFailure: function(){ alert('Fel uppstod: UpdateCartAmounts') }
    });    
}

function RemoveProduct(productID) {
    new Ajax.Request("/shop/Events.aspx", {
        method: "get",
        parameters: {
            func: "RemoveProduct",
            productID: productID
        },
        onSuccess: UpdateCartInfo,
        onFailure: function(){ alert('Fel uppstod: RemoveProduct') }
    });  
}

function ToggleSumVisibility(show) {
    var sum = document.getElementById("cart-flyout-sum");
    sum.style.visibility = (show ? "visible" : "hidden");
}

function SetCartInfo(transport) {
    var flyout = document.getElementById("cart-flyout");    
    if (flyout)
        flyout.innerHTML = transport.responseText;
    
    var sum = document.getElementById("sum-value").innerHTML;
    var amount = document.getElementById("total-amount-value").value;
    SetLeftCart(amount, sum);
    //alert(amount + " = " + sum);
    ToggleCartOpen(true);
    
    if (location.href.indexOf("CheckOut") > -1) {
        location.hash = "top";
    }
}

function LoadCartInfo() {
    new Ajax.Request("/shop/Events.aspx", {
        method: "get",
        parameters: {
            func: "GetCartInfo"
        },
        onSuccess: SetCartInfo,
        onFailure: function(){ alert('Fel uppstod: LoadCartInfo') }
    });
}

function ShowCartMessage(show, productID) {
    document.getElementById("cart-update-" + productID).style.visibility = (show ? "visible" : "hidden");
    if (show)
        setTimeout("ShowCartMessage(false, '" + productID + "')", 5000);
}

function SetLeftCart(amount, sum) {
    document.getElementById("cart-amount").innerHTML = amount;
    document.getElementById("cart-sum-text").innerHTML = sum;
}

function UpdateCart(transport) {
    //alert(transport.responseText);
    ShowFloatingMessage("Produkten lades till i kundvagnen.");
    if (_reloadPage) {
        location.reload();
//        if (location.href.indexOf("#") > -1)
//            location.href = location.href.substring(0, location.href.indexOf("#")) + "#top";
//        else
//            location.href += "&#top";
    }
    else if (transport.responseText.length > 0) {
        var arr = GetCallbackArray(transport.responseText);
        SetLeftCart(arr[0], arr[1]);
        ShowCartMessage(true, arr[2]);
        LoadCartInfo();
    }
}

function AddToCart(productID, amount, reload) {
    _reloadPage = reload;
    document.getElementById("productAmount_" + productID).value = "1";
    amount = parseInt(amount);
    
//    alert(document.getElementById("amountInShoppingCart_" + productID).value);
    
    var productStock = parseInt(document.getElementById("productStock_" + productID).value);
    var amountInCart = parseInt(document.getElementById("amountInShoppingCart_" + productID).value);
    if ((amountInCart + amount) > productStock) {
        var msg = "Det finns tyvärr inte " + (amount + amountInCart) + " st av artikeln i lager.\n\n" +
          "Välj OK för att restnotera artikeln i beställningen eller Avbryt för att minska antalet till det tillgängliga.";

        if (!confirm(msg))
            amount = productStock - amountInCart; 
    }
    
    document.getElementById("amountInShoppingCart_" + productID).value =  amountInCart + amount;
    
    new Ajax.Request("/shop/Events.aspx", {
        method: "get",
        parameters: {
            func: "AddToCart",
            productID: productID,
            amount: amount,
            rnd: Math.random()
        },
        onSuccess: UpdateCart,
        onFailure: function(){ alert('Fel uppstod: AddToCart') }
    });
    return false;
}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=")
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1 
    c_end=document.cookie.indexOf(";",c_start)
    if (c_end==-1) c_end=document.cookie.length
    return unescape(document.cookie.substring(c_start,c_end))
    } 
  }
return ""
}

function OpenProductLink(list) {
    var url = list.options[list.selectedIndex].value;
    if (url.length > 0) {
        if (url.indexOf("http://") < 0)
            url = "http://" + url;

        if (url.indexOf(".wmv") > 0) {
            Shadowbox.open({
                content: url,
                width: 400,
                height: 300,
                player: "wmp"
            });
        } else if (url.indexOf(".mov") > 0) {
            Shadowbox.open({
                content: url,
                width: 400,
                height: 300,
                player: "qt"
            });
        } else if (url.indexOf(".flv") > 0) {
            Shadowbox.open({
                content: url,
                width: 400,
                height: 300,
                player: "flv"
            });
        } else if (url.indexOf(".jpg") > 0 || url.indexOf(".gif") > 0 || url.indexOf(".png") > 0) {
            Shadowbox.open({
                content: url
            });
        }
        else
            window.open(url);
    }
}

function ClickIfReturn(e, btn)
{
    var code, character;
    if (!e)
        var e = window.event;
	if (e.keyCode)
	    code = e.keyCode;
	else if (e.which)
	    code = e.which;
	
	if (code == 13)
	{
	    btn.click();
	    return false;
	}
}

function findPosX(obj)
{
var curleft = 0;
if(obj.offsetParent)
    while(1) 
    {
      curleft += obj.offsetLeft;
      if(!obj.offsetParent)
        break;
      obj = obj.offsetParent;
    }
else if(obj.x)
    curleft += obj.x;
return curleft;
}

function findPosY(obj)
{
var curtop = 0;
if(obj.offsetParent)
    while(1)
    {
      curtop += obj.offsetTop;
      if(!obj.offsetParent)
        break;
      obj = obj.offsetParent;
    }
else if(obj.y)
    curtop += obj.y;
return curtop;
}

function ToggleListView(showImages) {
    var url = location.href.replace("?display=1", "").replace("&display=1", "").replace("?display=2", "").replace("&display=2", "");
//    alert(location.href);
    if (!showImages) {
        if (location.href.indexOf("?") < 0 && location.href.indexOf("&") < 0)
            location.href = url + "?display=2";
        else if (url.indexOf("?") > 0)
            location.href = url + "&display=2";
        else
            location.href = url + "?display=2";
    } else {
        if (location.href.indexOf("?") < 0 && location.href.indexOf("&") < 0)
            location.href = url + "?display=1";
        else if (url.indexOf("?") > 0)
            location.href = url + "&display=1";
        else
            location.href = url + "?display=1";
    }
}

function ToggleListImage(icon, prodID, show) {
    var img = document.getElementById("listProductImage");
    if (show) {
        img.src = "/img/products/" + prodID + "/product-" + prodID + "-thumb.jpg";
        var posX = (findPosX(icon) - 130 + (120 - img.width));
        
//        if (navigator.userAgent.indexOf("MSIE 6") > -1)
//            posX -= 110;
////        else if (navigator.userAgent.indexOf("MSIE 7") > -1)
////            posX += 20;
//        else if (navigator.userAgent.indexOf("Safari") > -1)
//            posX += 30;
        
        img.style.left = posX + "px";
//        document.getElementById("debug1").value = navigator.userAgent;
        img.style.top = (findPosY(icon) - 70) + "px";
        img.style.display = "block";
    } else {
       img.style.display = "none";
       img.src = "/gfx/px.gif";
    }
}

function CompareProducts(containerID) {
    var c = document.getElementById(containerID);
    var boxes = c.getElementsByTagName("input");
    var url = "/shop/CompareProducts.aspx?pid=";
    var a = 0;
    
    for (var i = 0; i < boxes.length; i++) {
        if (boxes[i].getAttribute("type") == "checkbox") {
            if (boxes[i].checked) {
                url += boxes[i].id.replace("compare_", "") + ";";
                a++;
            }
        }
    }
    
    if (a > 0) {
	    var x = screen.width, y = screen.height;
	    var width = 750, height = 600;
	    window.open(url, "CompareProducts", "top=" + parseInt(y/2-height/2-16) + ",left=" + parseInt(x/2-width/2-5) + ",width=" + width + ",height=" + height + ",scrollbars=1, status=1, resizable=1");
    } else
        alert("Markera de produkter du vill jämföra.");
}