﻿var vatcalc = 14/114;
var deliveryVAT = deliveryCharge * vatcalc;
function loadCart()
{
    $("button, div.rsButton").css("opacity",0.7);
    //show - hide detail updated
    var curState = "summary";
    $("div.summary, div.systemGuide").click(function(){
        $("div.detail").fadeOut("fast", function(){
            $(this).removeClass("detail").addClass(curState);
            $(this).show();//slideDown("normal");
            $(this).attr("title","Click to Show Detail View");
            $("div.prodImg img").attr("title","Click to Show Detail View");
        });
        if($(this)[0].className.indexOf("detail") < 0){
            if($(this).parent()[0].id == "cart")return;
            curState = $(this).attr("class").replace("product","");
            $(this).fadeOut("fast", function(){
                $(this).removeClass(curState).addClass("detail");            
                $(this).prependTo($(this).parent());
                $(this).show();//slideDown("normal");
                $(this).attr("title","Click to Hide Detail View");
                $(this).find("div.prodImg img").attr("title","Click to Hide Detail View");
            }); 
        }       
    }).attr("title","Click to Show Detail View").find("div.prodImg img").attr("title","Click to Show Detail View");

    //add to cart
    $("div.addCart").click(function(){
        var p = $(this).parent().parent().clone();
        p.removeAttr("title");
        p.find("div.prodImg img").removeAttr("title");
        p.removeClass("detail").removeClass("summary").removeClass("systemGuide").addClass("cart");
        var price = parseFloat(p.find("div.price span").text());
        var ppp = parseFloat(p.find("span.pppvalue").text());
        var total = parseFloat($("span.mod_cart_total_value").text()) + (price);
        var totalppp = parseFloat($("span.mod_cart_ppp_value").text()) + ppp;
        alertPPP(totalppp);
        var incart = $("#cart").find("#" + p[0].id);
        if(incart.length > 0){
            var qty = incart.find("span.value");
            $("div.mod_cart div.mod_body:hidden").show();
            qty.html((parseInt(qty.html(),10) + 1) + "");
            $("span.mod_cart_total_value").html(total.toFixed(2));
            $("span.mod_cart_vat_value").html(((total* vatcalc) + deliveryVAT).toFixed(2));
            if(isNaN(totalppp))totalppp = 0;
            $("span.mod_cart_ppp_value").html(totalppp);
            var msg = p.find("div.prodName").text() + " was incremented"     
            persistCart(msg); 
            return false;
        }
        p.slideUp("normal", function(){           
            p.prependTo($("#cart"));
            $("div.mod_cart div.mod_header_icon_down").removeClass("mod_header_icon_down").addClass("mod_header_icon_up");
            $("div.mod_cart div.mod_body:hidden").show();
            p.slideDown("normal", function(){  
                window.scrollTo(0,0);           
                $("span.mod_cart_total_value").html(total.toFixed(2));
                $("span.mod_cart_vat_value").html(((total* vatcalc) + deliveryVAT).toFixed(2));
                if(isNaN(totalppp))totalppp = 0;
                $("span.mod_cart_ppp_value").html(totalppp);
                var msg = p.find("div.prodName").text() + " was added to the basket"
                persistCart(msg);
            });
        });
        //remove from cart
        p.find("div.delete").attr("title","Click to Remove from basket").click(function(){
            var p = $(this).parent().parent();
            p.attr("title","");
            var price = parseFloat(p.find("div.price span").text());
            var ppp = parseInt(p.find("span.pppvalue").text(),10);
            var qty = parseInt(p.find("div.qty span.value").text(),10);
            var total = parseFloat($("div.mod_cart_total span.mod_cart_total_value").text()) - (price * qty);
            var totalppp =  parseInt($("span.mod_cart_ppp_value").text(),10) - (ppp * qty);
            alertPPP(totalppp);
            p.find("div.qty span.value").text("1"); 
            var prodName = p.find("div.prodName").text();
            p.slideUp("normal", function(){
                p.remove();
                if(total == 0)
                {
                    deliveryVAT = 0;
                }
                $("div.mod_cart_total span.mod_cart_total_value").html(total.toFixed(2));
                if(isNaN(totalppp))totalppp = 0;
                $("span.mod_cart_ppp_value").html(totalppp.toString());
                $("span.mod_cart_vat_value").html(((total* vatcalc) + deliveryVAT).toFixed(2));                
                var msg = prodName + " was removed from the basket"
                persistCart(msg); 
            });                 
            return false;
        });        
        
        //increment quantity
        p.find("div.plus").attr("title","Click to increment this product").click(function(){
            var p = $(this).parent().parent().parent();
            var price = parseFloat(p.find("div.price span").text());
            var ppp = parseInt(p.find("span.pppvalue").text(),10) 
            var qty = p.find("span.value"); 
            qty.html((parseInt(qty.html(),10) + 1) + "");
            var total = parseFloat($("span.mod_cart_total_value").html()) + price;
            var totalppp = parseInt($("span.mod_cart_ppp_value").html(),10) + ppp;
            alertPPP(totalppp);           
            $("span.mod_cart_total_value").html(total.toFixed(2));
            $("span.mod_cart_vat_value").html(((total* vatcalc) + deliveryVAT).toFixed(2));
            if(isNaN(totalppp))totalppp = 0;
            $("span.mod_cart_ppp_value").html(totalppp);
            var msg = p.find("div.prodName").text() + " was incremented"
            persistCart(msg); 
        });
        
        //decrement quantity
        p.find("div.minus").attr("title","Click to decrement this product").click(function(){
            var p = $(this).parent().parent().parent();
            var price = parseFloat(p.find("div.price span").text());
            var ppp =  parseInt(p.find("span.pppvalue").text(),10);
            var qty = p.find("span.value");
            qty.html((parseInt(qty.html(),10) - 1) + "");       
            if(parseInt(qty.html(),10) == 0)
            {
                qty.html("1"); 
                p.find("div.delete").trigger("click");  
            }
            else
            {
                var total = parseFloat($("span.mod_cart_total_value").html()) - price ;
                var totalppp = parseInt($("span.mod_cart_ppp_value").html(),10) - ppp;
                alertPPP(totalppp);
                $("span.mod_cart_total_value").html(total.toFixed(2));
                $("span.mod_cart_vat_value").html(((total* vatcalc) + deliveryVAT).toFixed(2));
                if(isNaN(totalppp))totalppp = 0;
                $("span.mod_cart_ppp_value").html(totalppp);        
            }
            var msg = p.find("div.prodName").text() + " was decremented"
            persistCart(msg);
        });   
        return false;
    }).attr("title","Click to Add to basket");
    $("body")[0].style.height = "auto";
    $("button, div.rsButton").hover(function(){
       $(this).css({opacity:1.0});
     },function(){
       $(this).css({opacity:0.7});
     });
}

function alertPPP(ppp)
{
    if(ppp >= 100)
    {
        if($("div#alert").is(":hidden"))
        {
            $("div#alert").click(function(){
                linkTo("/login");    
            }).css("cursor","pointer").slideDown("slow");
        }
    } 
    else
    {
        $("div#alert").hide();
    }
}

var freeDeliveryAmount = 600;
var freeDeliveryActive = false;

function checkDelivery()
{
    var products;
    var delivery = 0;
    if(freeDeliveryActive)
    {
        if(parseFloat($("span.mod_cart_total_value").html()) >= freeDeliveryAmount)
        {
            deliveryCharge = 0;
        }
        else
        {
            deliveryCharge = deliveryCharge0;
        }
    }
    var webincart = $("div#cart").find("span.prdCode").contains("WEB");
    if(webincart.length > 0)
    {
        products = $("div#cart").find("div.product");
        if(products.length > 1)
        {
            $("span.mod_cart_del_value").html(deliveryCharge + ".00");
            delivery = deliveryCharge;
            deliveryVAT = deliveryCharge * vatcalc; 
        }
        else
        {
            $("span.mod_cart_del_value").html("0.00");
            delivery = 0;
            deliveryVAT = 0;
        }
    }
    else
    {
        products = $("#cart").find("span.ProductType_1");
        //debugger;
        if(products.length > 0)
        {
            $("span.mod_cart_del_value").html(deliveryCharge + ".00"); 
            delivery = deliveryCharge;
            deliveryVAT = deliveryCharge * vatcalc; 
        }
        else
        {
            $("span.mod_cart_del_value").html("0.00");
            delivery = 0;
            deliveryVAT = 0;
        }
    }
    var totalPlusDelivery = (parseFloat($("span.mod_cart_total_value").html())+ delivery).toFixed(2);
    $("span.mod_cart_total_incdel").html(totalPlusDelivery);
}

function getCartProducts()
{
    var aProd = [];
    $("div#cart span.prdCode").each(function(){
        aProd.push($(this).text());        
    });
    var parms = {ProductCodes: aProd.join(";"), cache:newid()};
    $.getJSON("includes/sportron/getProductPricing.aspx",parms,function(products){
        return products;
    });
    return null;       
}

function recalc()
{
    if(typeof ppp != "undefined")
    {
        if(ppp == "")ppp="0";
        if(parseInt(ppp,10) < 100)
        {
            alert("You need a minimum PPP value of 100 in your Shopping Basket to sign-up as a Sportron Marketing Associate.\n\nPlease add a few more products to your Shopping Basket and try again!");
            return false;
        }
    }
    var total = 0; 
    $("div.product").each(function(){
        $(this).find("div.price span").text($(this).find("div.maprice span").text());
        total += (parseFloat($(this).find("div.price span").text()) * parseInt($(this).find("div.qty span.value").text(),10));
    });
    //alert("We have recalulated your shopping basket to give you the Marketing Associate discounted prices...");
    $("span.mod_cart_total_value").text(total.toFixed(2));
    $("span.mod_cart_total_incdel").text((total + deliveryCharge).toFixed(2));
    $("span.mod_cart_vat_value").html(((total + deliveryCharge)* vatcalc).toFixed(2));
    
}

function persistCart(sMsg)
{   
    try
    {
        $("div.mod_cart_desc").html("<img alt=\"Processing please wait ...\" style=\"margin-top:6px; width:190px;\" src=\"/images/loadingAnimation.gif\" />"); 
        $.blockUI(" ");       
        var sPPP = $("span.mod_cart_ppp_value").text();
        var cartHTML = $("div#cart_all").html();
        checkDelivery();
        $("div.mod_cart_desc").load("/includes/sportron/saveAndLoad.aspx", {cart: cartHTML, msg:sMsg, ppp:sPPP, cache:newid() }, function(){
            $.unblockUI();
            if($("div.wizTitle").text().trim() == "Checkout Wizard")
            {
                setupOrder(_customer.MANum == undefined ? "0" : _customer.MANum, _customer.MAType == undefined ? "D" : _customer.MAType );
                $("body")[0].style.height = "auto";
            }
        });
    }
    catch(e)
    {
        $.unblockUI();
    }     
}

function reLoadCart()
{
    //remove from cart
    $("div#cart div.delete").attr("title","Click to Remove from basket").click(function(){
        var p = $(this).parent().parent();
        p.attr("title","");
        var price = parseFloat(p.find("div.price span").text());
        var ppp = parseInt(p.find("span.pppvalue").text(),10);
        var qty = parseInt(p.find("div.qty span.value").text());
        var total = parseFloat($("div.mod_cart_total span.mod_cart_total_value").text()) - (price * qty); 
        var totalppp = parseInt($("span.mod_cart_ppp_value").text(),10) - (ppp * qty);
        alertPPP(totalppp);
        p.find("div.qty span.value").text("1"); 
        var prodName = p.find("div.prodName").text();
        p.slideUp("normal", function(){
            p.remove();
            if(total == 0)
            {
                deliveryVAT = 0;
            }
            $("div.mod_cart_total span.mod_cart_total_value").html(total.toFixed(2));
            $("span.mod_cart_vat_value").html(((total* vatcalc) + deliveryVAT).toFixed(2));
            $("span.mod_cart_ppp_value").html(totalppp.toString());            
            var msg = prodName + " was removed from the Cart"
            persistCart(msg); 
        });                            
    });               
    //increment quantity
    $("div#cart div.plus").attr("title","Click to increment this product").click(function(){
        var p = $(this).parent().parent().parent();
        var price = parseFloat(p.find("div.price span").html());
        var ppp = parseInt(p.find("span.pppvalue").text(),10);  
        var qty = p.find("span.value"); 
        qty.html((parseInt(qty.html(),10) + 1) + "");
        var total = parseFloat($("span.mod_cart_total_value").html()) + price;
        var totalppp = parseInt($("span.mod_cart_ppp_value").html(),10) + ppp;
        alertPPP(totalppp);        
        $("span.mod_cart_total_value").html(total.toFixed(2));
        $("span.mod_cart_vat_value").html(((total* vatcalc) + deliveryVAT).toFixed(2));
        $("span.mod_cart_ppp_value").html(totalppp);
        var msg = p.find("div.prodName").text() + " was incremented"
        persistCart(msg); 
    });
        
    //decrement quantity
    $("div#cart div.minus").attr("title","Click to decrement this product").click(function(){
        var p = $(this).parent().parent().parent();
        var price = parseFloat(p.find("div.price span").html()); 
        var ppp = parseInt(p.find("span.pppvalue").text(),10);
        var qty = p.find("span.value");
        qty.html((parseInt(qty.html(),10) - 1) + "");       
        if(parseInt(qty.html(),10) == 0)
        {
            qty.html("1"); 
            p.find("div.delete").trigger("click");  
        }
        else
        {
            var total = parseFloat($("span.mod_cart_total_value").text()) - price;
            var totalppp = parseInt($("span.mod_cart_ppp_value").text(),10) - ppp;
            alertPPP(totalppp);
            $("span.mod_cart_total_value").html(total.toFixed(2)); 
            $("span.mod_cart_vat_value").html(((total* vatcalc) + deliveryVAT).toFixed(2));
            $("span.mod_cart_ppp_value").html(totalppp);          
        }
        var msg = p.find("div.prodName").text() + " was decremented"
        persistCart(msg);
    }); 
        
    //count cart items
    var productCount = 0;
    
    $("div#cart div.qty span.value").each(function()
    {
        productCount += parseInt($(this).text(),10);
    }
    ); 
    if(productCount > 0)
        $("div.mod_cart_desc").text("You have " + productCount + " items in your basket");    
    else
        $("div.mod_cart_desc").text("Your shopping Basket");
        
    $("button, div.rsButton").hover(function(){
       $(this).css({opacity:1.0});
     },function(){
       $(this).css({opacity:0.7});
     });        
}

$(document).ready(function(){
    reLoadCart();
    checkDelivery();
});
