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));
}

//set upp til að eyða gömlu cookie - karfan var ekki virka hjá sumum notendum eftir uppfærslu í nóv 2005
function frontpageDeleteCookie()
{
	var oldCart = getCookie("rlcart");
	if (oldCart != null) {
		deleteCookie(oldCart);
	}
}

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,quant, comments) {
	var iTotalItems, iTotalPrice;
	var iItems, iPrice;
	var number = 1;
			
	var sComments = comments;
			
	if (quant && checkNumber(quant))
		number = quant;
	
	if (checkNumber(number)) {
		if (number > 0) {
			var CartItems = getCookie("rlcart");
			if (CartItems != null) {											
				CartItems = CartItems + "|" + id + "," + price + "," + number + "," + sComments;								
			} else {
				CartItems = id + "," + price + "," + number + "," + sComments;
			}
												
			setCookie("rlcart", 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()				
			}															
		}
	}	
}

function updateCart() {
	var newCart = "";
	var sSerial = "";
	var sUrl = "";
	var iPrice = "";
	var CartItems = getCookie("rlcart");
	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;
			
			//var sComment = arrItem[3];						
			//var sComment = document.Form1.sComment.value;
			
			comment = arrItem[3];
			sCom = "com"			
			var sComment = document.getElementById(comment+sCom+sSerial).value;
															
			if (!checkNumber(number))  {
				number = arrItem[2];
			}
			if (number < 1) {
				number = arrItem[2];
			}
			
			if (newCart != "") {
				newCart = newCart + "|" + sSerial + "," + iPrice + "," + number + "," + sComment;
			} else {
				newCart = sSerial + "," + iPrice + "," + number + "," + sComment;
			}
		}
		setCookie("rlcart", newCart);
		
		var sPath = document.forms["1"].path.value;
		document.location.href = "/karfa/default.asp?path=" + sPath;
	}
}

function overView() {
	var newCart = "";
	var sSerial = "";
	var sUrl = "";
	var iPrice = "";
	var CartItems = getCookie("rlcart");
					
	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;
			var sComment = arrItem[3];
			
			if (!checkNumber(number))  {
				number = arrItem[2];
			}
			if (number < 1) {
				number = arrItem[2];
			}
												
			if (newCart != "") {												
				newCart = newCart + "|" + sSerial + "," + iPrice + "," + number + "," + sComment;																	
			} else {
				newCart = sSerial + "," + iPrice + "," + number + "," + sComment;
			}
			
			arrTest[i] = sSerial;
		}
		setCookie("rlcart", 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("rlcart");
	alert(CartItems)
}

function ReadCarton() {
	var CartItems = getCookie("rlcart");
	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("rlcart");
	document.location.href = "/rl/karfa"
}

function ClearCart() {
	deleteCookie("rlcart");
	document.location.href = "/rl/karfa/skilabod/";
}

function EmptyCart() {
	deleteCookie("rlcart");
}

function DeleteItem(serial) {
	var CartItems = getCookie("rlcart");
	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] + "," + arrItem[3];
			} else {
				newCart = arrItem[0] + "," + arrItem[1] + "," + arrItem[2] + "," + arrItem[3];
			}
		}
				
	}
	
	setCookie("rlcart", newCart);
	var sPath = document.forms["Form1"].path.value;
	document.location.href = "/rl/karfa/default.asp?path=" + sPath;
	
}

function setCart() {
	var iTotalItems, iTotalPrice;
	var iItems, iPrice;
	var CartItems = getCookie("rlcart");
	//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){}

}



