function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


function writeShopcart() {
    // gets and writes the shoppingcart
    var shopcart = readCookie('ShoppingCart');
    var str = '';
    if(shopcart != null) {
        var exists = false;
        var rows = 0;
        //yes there is a shoppingcart
        var variables = shopcart.split('&');
        for (var i=0;i<variables.length;i++) {
            var name = variables[i].split('=')[0];
            var values = variables[i].split('=')[1];
            var singlevalue = values.split(',');
            rows = singlevalue.length;
        }
        if (rows > 0) {
            for (var k=0;k<rows;k++) {
                var productid = variables[0].split('=')[1].split(',')[k];
                var quantity = variables[4].split('=')[1].split(',')[k];
                var articleno = variables[8].split('=')[1].split(',')[k];
                if (articleno.length > 0) {
                    exists = true;
                    str += writeCartrow(productid,quantity,articleno);
                }
            }
            if (exists == false) {
                str = 'Din varukorg &#228;r tom';
            }
            return str;
        }
    } else {
        //no there is no shoppingcart
        return 'Din varukorg &#228;r tom';
    }
}

function writeCartrow(productid,quantity,articleno) {
    var str = '';
    str += '<div class="item">';
    str += '  <div class="articleno">'+articleno+'</div>';
    str += '  <div class="quantity">'+quantity+' st</div>';
    str += '</div>';
    return str;
}
