/**
 * Reads the content from a cookie
 * @param name The name of the cookie
 */
function readCookie(c_name) {
	var nameEQ = c_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 unescape(c.substring(nameEQ.length,c.length)); 
      }
	}
	return null;
}

/**
 * Sets a cookie
 * @param c_name The name of the cookie
 * @param value The value of the cookie
 * @param expiredays How long the cookie will live
 */
function setCookie(c_name,value,expiredays){
   var exdate=new Date();
   exdate.setDate(exdate.getDate()+expiredays);
   document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

/**
 * Adds a value to a cookie. Several values are comma separated
 * @param c_name The name of the cookie
 * @param value The value to add to the cookie
 */
function addValueToCookie(c_name, value){
   var cookie = readCookie(c_name);
   if(cookie==null){
      setCookie(c_name, value, 1000);
   } else {
      // check if the value already exists
      var hasValue = false;
      var ca = cookie.split(',');
      for(var i=0;i < ca.length;i++) {
         if(ca[i] == value)
            hasValue = true;
      }
      
      if(hasValue)
         return;
      else
         setCookie(c_name, cookie+","+value, 1000);
   }
}

/**
 * Removes a value from a cookie. Several values are comma separated
 * @param c_name The name of the cookie
 * @param value The value to be removed
 */
function removeValueFromCookie(c_name, value){
   var cookie = readCookie(c_name);
   if(cookie==null){
      return;
   } else {
      var newValue="";
      var ca = cookie.split(',');
      for(var i=0;i < ca.length;i++) {
         if(ca[i] == value)
            ca.splice(i, 1);
      }
      
      setCookie(c_name, ca.join(','), 1000);
   }
}

/**
 * Checks if cookie has value. Several values are comma separated
 * @param c_name The name of the cookie
 * @param value The value to be checked
 */
function cookieHasValue(c_name, value){
   var cookie = readCookie(c_name);
   if(cookie==null){
      return false;
   } else if(cookie==value){
      return true;
   } else {
      var ca = cookie.split(',');
      for(var i=0;i < ca.length;i++) {
         if(ca[i] == value)
            return true;
      }
      
      return false;
   }
}

/**
 * Toggles item from a shopping cart, where items are stored comma separated in a cookie
 * @param id The id of the item
 */
function toggleItem(id){
   var cookieName = "cart";
   if(cookieHasValue(cookieName, id)){
      removeValueFromCookie(cookieName, id);
      document.getElementById('cart'+id).innerHTML = "Legg til i huskeliste";
      document.getElementById('product'+id).style.display = 'none';
   } else {
      addValueToCookie(cookieName, id);
      document.getElementById('cart'+id).innerHTML = "Fjern fra huskeliste";
   }
   return false;
}