﻿function UpdateShoppingCart(site) {
    var items = $.cookie(site + 'Cart');
    
    $('.shoppingCartList >option').remove();

    if (items != null) {

        var names = items.split("|");

        for (var i = 0; i < names.length - 1; i++) {
            var parts = names[i].split("_");
            $('.shoppingCartList').append('<option value=1>' + parts[0] + " " + parts[1] + '</option>');
        }
    }
    else {
        $('.shoppingCartList').append('<option value=-1>Varukorgen är tom</option>');
    }
}

function DeleteFromCart(id, site) {
    var items = $.cookie(site + 'Cart');
    
    
    if (items != null) {
        var names = items.split("|");

        for (var i = 0; i < names.length - 1; i++) {
    
            if (names[i].indexOf(id) > 0) {
                items = items.replace(names[i] + "|", "");
                break;
            }
        }        
        $.cookie(site + 'Cart', null);
        $.cookie(site + 'Cart', items, { path: '/' });
    }
}



function AddItemToCart(id, name, year, site) {
    if (name.length >= 17) {
        name = name.substring(0, 17) + "...";
    }
    name += ("_" + year + "_" + id + "_1");

    var items = $.cookie(site + 'Cart');

    if (items != null) {
        var names = items.split("|");

        for (var i = 0; i < names.length - 1; i++) {
            if (names[i] == name) {
                items = items.replace(name + "|", "");
                break;
            }
        }

        items = (name + "|") + items;
        $.cookie(site + 'Cart', null);
        $.cookie(site + 'Cart', items, { path: '/' });
    }
    else {
        items = (name + "|");
        $.cookie(site + 'Cart', null);
        $.cookie(site + 'Cart', items, { path: '/' });

    }

    $('#progressBar' + id).show();
    setTimeout("$('#progressBar" + id + "').hide()", 1000);

    //$('#progressBar' + id).hide()
    UpdateShoppingCart(site);
}


function UpdateItems(site) {
    var row = ".SRItemRow";

    if (site == "Wineagency") {
        row = ".SRItemRowShop";
    }

    $(row).each(

    // For each hottie, run this code. The "indIndex" is the
    // loop iteration index on the current element.
	function(intIndex) {
	    var id = $(this).find('input[type=hidden]').val();
	    var amount = $(this).find('input[type=text]').val();
	    
	    if (amount == 0) {
	        DeleteFromCart(id, site);
	    }
	    else {
	        var items = $.cookie(site + 'Cart');
	        
	        if (items != null) {
	            var names = items.split("|");

	            for (var i = 0; i < names.length - 1; i++) {
	                if (names[i].indexOf(id) > 0) {
	                    
	                    items = items.replace(names[i], names[i].substring(0, names[i].lastIndexOf("_")) + "_" + amount);
	                    break;
	                }
	            }


	            $.cookie(site + 'Cart', null);
	            $.cookie(site + 'Cart', items, { path: '/' });
	        }
	    }

	}

	);
}

function DeleteAll(site) {
    $.cookie(site + 'Cart', null);
    $.cookie(site + 'Cart', '', { path: '/' });
}