function setCookie(name, value)
{
	var days = 1;
	var expdate = new Date(); 
		expdate.setTime(expdate.getTime() + days*24*60*60*1000);
		document.cookie = name + '=' + escape(value) + '; path=/; expires=' + expdate.toGMTString(); 
		
	return true;
}

function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name)
{
    if (getCookie(name))
    {
		var path= "/"
        document.cookie = name + "=" + 
            ((path) ? "; path=" + path : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

function InsertCart(id,price,update) {
//alert('Komst inn í falliđ');
	var iTotalItems, iTotalPrice;
	var iItems, iPrice;
	var number = 0;
	
	var objN = document.getElementById(id);
	//alert(objN);
	if (objN != null) {
		number = objN.value;
		if (checkNumber(number)) {
			if (number > 0) {
				var CartItems = getCookie("kokkacart");
				if (CartItems != null) {
					CartItems = CartItems + "|" + id + "," + price + "," + number;
				} else {
					CartItems = id + "," + price + "," + number;
				}
				
				setCookie("kokkacart", CartItems);
				
				var arrCart = CartItems.split("|");
					
				for (i=0; i<arrCart.length; i++) {
					var arrItem = arrCart[i].split(",");
					iPrice = arrItem[1];
					iItems = arrItem[2];
					
					if (isNaN(iTotalPrice)) { iTotalPrice = parseInt(iPrice) * parseInt(iItems); } else { iTotalPrice = iTotalPrice + (parseInt(iPrice) * parseInt(iItems)); }
					if (isNaN(iTotalItems)) { iTotalItems = parseInt(iItems); } else { iTotalItems = iTotalItems + parseInt(iItems); }
				}
				
				if (update == 0) {
					setCart()
				}
			}
		} else {
			objN.value = 1;
		} 
	}
}

function updateCart() {		
	var newCart = "";
	var sSerial = "";
	var sUrl = "";
	var iPrice = "";
	var CartItems = getCookie("kokkacart");
	if (CartItems != null) 
	{
		var arrCart = CartItems.split("|");
		for (i=0; i<arrCart.length; i++) 
		{
			var arrItem = arrCart[i].split(",");
			sSerial = arrItem[0];
			iPrice = arrItem[1];
			var objN = document.getElementById(sSerial);
			var number = objN.value;
			
			if (!checkNumber(number))  
			{
				number = arrItem[2];
			}
			if (number < 1) 
			{
				number = arrItem[2];
			}
			
			if (newCart != "") 
			{
				newCart = newCart + "|" + sSerial + "," + iPrice + "," + number;
			} 
			else 
			{
				newCart = sSerial + "," + iPrice + "," + number;
			}
		}
		setCookie("kokkacart", newCart);
		var sPath = document.getElementById("path").value;		
		document.location.href = "/kokka/aerogarden/karfa/default.asp?path=" + sPath;
	}
}

function overView() {
	var newCart = "";
	var sSerial = "";
	var sUrl = "";
	var iPrice = "";
	var CartItems = getCookie("kokkacart");
	if (CartItems != null) {
		var arrCart = CartItems.split("|");
		for (i=0; i<arrCart.length; i++) {
			var arrItem = arrCart[i].split(",");
			sSerial = arrItem[0];
			iPrice = arrItem[1];
			var objN = document.getElementById(sSerial);
			var number = objN.value;
			
			if (!checkNumber(number))  {
				number = arrItem[2];
			}
			if (number < 1) {
				number = arrItem[2];
			}
			
			if (newCart != "") {
				newCart = newCart + "|" + sSerial + "," + iPrice + "," + number;
			} else {
				newCart = sSerial + "," + iPrice + "," + number;
			}
		}
		setCookie("kokkacart", newCart);
		document.forms["karfa"].submit();
	}
}

function checkNumber(number) {
	var re = new RegExp("[^0-9]");	if (re.test(number)) {
		alert("Eingöngu má nota tölustafi í magni")
		return false;
	} else { 
		return true;
	}
}

function ReadCart() {
	var CartItems = getCookie("kokkacart");
	alert(CartItems)
}

function ReadCarton() {
	var CartItems = getCookie("kokkacart");
	if (CartItems !=  null )
	{
		var arrItems = CartItems.split('|');
		var counter = 0;
		for (i=0; i<arrItems.length; i++)
		{
			var arrItem = arrItems[i].split(',');
			for(j=0; j<arrItem.length; j=j+3)
			{
				counter++;
				if(counter % 2 == 1)
				{
					document.write('<div style="background: #fff;">' + arrItem[j] + ', ' + arrItem[j+2] + ' stk.</div>');	
				}
				else
				{
					document.write('<div>' + arrItem[j] + ', ' + arrItem[j+2] + ' stk.</div>');	
				}
				
			}
			//document.write(arrItems[i] + ' <br>');	
		}
	}

}

function DeleteCart() {
	deleteCookie("kokkacart");
	document.location.href = "/kokka/aerogarden/karfa/"
}

function ClearCart() {
	deleteCookie("kokkacart");
	document.location.href = "/kokka/karfa/skilabod/";
}

function EmptyCart() {
	deleteCookie("kokkacart");
}

function DeleteItem(serial) {
	var CartItems = getCookie("kokkacart");
	var newCart = ""
	var arrCart = CartItems.split("|");
	
	for (i=0; i<arrCart.length; i++) {
		var arrItem = arrCart[i].split(",");
		var iSerial = arrItem[0];
		
		if (iSerial == serial) {
			//alert("match")
		} else {
			if (newCart != "") {
				newCart = newCart + "|" + arrItem[0] + "," + arrItem[1] + "," + arrItem[2];
			} else {
				newCart = arrItem[0] + "," + arrItem[1] + "," + arrItem[2];
			}
		}
				
	}
	
	setCookie("kokkacart", newCart);
	var sPath = document.forms["1"].path.value;
	document.location.href = "/kokka/aerogarden/karfa/default.asp?path=" + sPath;
	
}

function setCart() {
	var iTotalItems, iTotalPrice;
	var iItems, iPrice;
	var CartItems = getCookie("kokkacart");
	//alert(CartItems)
	
	if (CartItems != null) {
		var arrCart = CartItems.split("|");
		for (i=0; i<arrCart.length; i++) {
			var arrItem = arrCart[i].split(",");
			iPrice = arrItem[1];
			iItems = arrItem[2];
			
			if (isNaN(iTotalPrice)) { iTotalPrice = parseInt(iPrice) * parseInt(iItems); } else { iTotalPrice = iTotalPrice + (parseInt(iPrice) * parseInt(iItems)); }
			if (isNaN(iTotalItems)) { iTotalItems = parseInt(iItems); } else { iTotalItems = iTotalItems + parseInt(iItems); }
		}
		if (! isNaN(iTotalPrice)) {
			var objP = document.getElementById("price");
			objP.innerHTML = iTotalPrice;
		} 
		if (! isNaN(iTotalItems)) {
			var objI = document.getElementById("items");
			objI.innerHTML = iTotalItems;
		} 
	} else {
			var objI = document.getElementById("items");
			objI.innerHTML = 0;
			var objP = document.getElementById("price");
			objP.innerHTML = 0;
	}
	try {
		var objG = document.getElementById("takki1");
		objG.style.display = (iTotalItems)?'block':'none';
	} catch(e){}

}




