// shoplist.js

<!--
	/*var cookieShopID 	= 'ra_shoplist';	
    */
	var gLastList;

/*	SetLastList();	// Execute when loading page
*/

	function Item(i,e) {
		//var re=/\/images\/btn_add_shopping_list.png/;
		var obj = document.getElementById(e).firstChild;		
		if(obj.src.indexOf('btn_add_shopping_list.png') > 0) { 
			obj.src='/www.riteaid.com/w-content/images/btn_remove_shopping_list.png';
			document.getElementById(e).setAttribute("title","Remove from shopping list");
			document.getElementById(e).setAttribute("alt","Remove from shopping list");
			obj.setAttribute("title","Remove from shopping list"); 
			addItem(i); 
			return true; 
		} else { 
			obj.src='/www.riteaid.com/w-content/images/btn_add_shopping_list.png'; 
			document.getElementById(e).setAttribute("title","Add to shopping list");
			document.getElementById(e).setAttribute("alt","Add to shopping list");
			obj.setAttribute("title","Add to shopping list");	
			removeItem(i);		
			return true; 
		} 
	} 
	
	
	function addItem(new_item) {
		var item_string = "";
		var baction = '';
		var gLastList = '';
		var arrShopList = '';
		var newShopList = '';
		var found_prop = false;
		var expDate = new Date (); 

		expDate.setTime( expDate.getTime() + (1 * 24 * 60 * 60 * 1000) ); 

		var cookieShopID = document.getElementById("circId").value + 'ra_shoplist';
		
		item_string = item_string+new_item;
						
		if (item_string != null) {
		
		var thevalue = item_string;
		var thearr = thevalue.split("~");
		var newitem = thearr[0];
		var newvalue = thearr[1];
		//alert ('go to getCookie function');
			if (GetCookie (cookieShopID) != null)				
				{
					gLastList = GetCookie (cookieShopID);
					//alert('We got a cookie ! ('+gLastList+')');
					//var broken_cookie = gLastList.split("=");
			//alert('no local cookie of correctness found('+gLastList.toString+')');
					//var the_values = broken_cookie[1];
					var the_values = gLastList;
			// break each name:value pair into an array

					var separated_values = the_values.split("/");

			// loop through the list of name:values and load
			// up the associate array

					var property_value = "";
			
					
					for (var loop = 0; loop < separated_values.length; loop++)

					{
						property_value = separated_values[loop];
						var broken_info = property_value.split(":");
						var the_property = broken_info[0];
						if (the_property == newitem)
						{
							//alert('Me thinks I already have this value
							var the_value = newvalue;
							found_prop = true;
						}
						else
							var the_value = broken_info[1];
						arrShopList[the_property] = the_value;
						if (newShopList.length > 0) /* Never want a slash as the first char */
						{
							//alert('Creating Cookie from existing cookie...current is:'+newShopList);
							newShopList = newShopList + '/' + the_property + ':' + the_value;
						}
						else
						{
							//alert('Creating Cookie from existing cookie (FIRST OCCURENCE)...current is:'+newShopList);
							newShopList = the_property + ':' + the_value;
						}
					}
				}
				//alert('Foundprop is:'+found_prop);
				if (!found_prop)
				{
					//alert('Adding something to the list!');
					if (gLastList.length > 0) /* Never want a slash as the first char */
					{
						newShopList = gLastList + '/' + newitem + ':' + newvalue;
						//alert('Cookie length:'+newShopList.length+' And cookie is: '+newShopList);
					}
					else
					{
						newShopList = newitem + ':' + newvalue;
						//alert('(NEW COOKIE) Cookie length:'+newShopList.length+' And cookie is: '+newShopList);
					}
				}

		/*
		else
			{
				//alert('item unchecked!');
				var thevalue = item_string;
				var thearr = thevalue.split("~");
				var newitem = thearr[0];
				var newvalue = thearr[1];
				if (GetCookie (cookieShopID) != null)
				{
					gLastList = GetCookie (cookieShopID);
					//alert('We got a cookie for UnCheckboxes! ('+gLastList+')');
					//var broken_cookie = gLastList.split("=");
					//alert('no local cookie of correctness found('+gLastList.toString+')');
					//var the_values = broken_cookie[1];
					var the_values = gLastList;
			// break each name:value pair into an array

					var separated_values = the_values.split("/");

			// loop through the list of name:values and load
			// up the associate array

					var property_value = "";
			
					
					for (var loop = 0; loop < separated_values.length; loop++)

					{
						property_value = separated_values[loop];
						var broken_info = property_value.split(":");
						var the_property = broken_info[0];
						if (the_property != newitem) 
						{
							//alert('Property not equal, setting cookie!');
							if (newShopList.length > 0) // Never want a slash as the first char
								newShopList = newShopList + '/' + the_property + ':' + broken_info[1];
							else
								newShopList = the_property + ':' + broken_info[1];
							//alert('Cookie now looks like: ('+newShopList+')');
						}
					}
				}
				if (found_prop)
					{
					//alert('Something was deleted from the list!');
					}
			}
			*/
		}//end if new_item != null
		//if ( newShopList.length > 0)
		//{
			//alert('Cookie length:'+newShopList.length+' And cookie is: '+newShopList);
			SetCookie( cookieShopID, newShopList, expDate, "/");
		//}
		//else
			//DeleteCookie(cookieShopID);		
	}
	
	function removeItem(new_item) {
		//alert('item unchecked!');
		var item_string = "";
		var baction = '';
		var gLastList = '';
		var arrShopList = '';
		var newShopList = '';
		var expDate = new Date (); 
		//var found_prop = false;
		//var expDate = new Date (); 

		expDate.setTime( expDate.getTime() + (1 * 24 * 60 * 60 * 1000) ); 

		var cookieShopID = document.getElementById("circId").value + 'ra_shoplist';
		
		item_string = item_string+new_item;
		//alert('item_string = '+item_string);
				
		if (item_string != null) {
		
			var thevalue = item_string;
			var thearr = thevalue.split("~");
			var newitem = thearr[0];
			var newvalue = thearr[1];
			//alert('newitem ='+newitem);
				if (GetCookie (cookieShopID) != null)
				{
					gLastList = GetCookie (cookieShopID);
					//alert('We got a cookie for UnCheckboxes! ('+gLastList+')');
					//var broken_cookie = gLastList.split("=");
					//alert('no local cookie of correctness found('+gLastList.toString+')');
					//var the_values = broken_cookie[1];
					var the_values = gLastList;
			// break each name:value pair into an array

					var separated_values = the_values.split("/");

			// loop through the list of name:values and load
			// up the associate array

					var property_value = "";
			
					
					for (var loop = 0; loop < separated_values.length; loop++)

					{
						//alert('inside for loop');
						property_value = separated_values[loop];
						var broken_info = property_value.split(":");
						var the_property = broken_info[0];
						//alert('the_property = '+the_property);
						if (the_property != newitem) 
						{
							//alert('Property not equal, setting cookie!');
							if (newShopList.length > 0) // Never want a slash as the first char
								newShopList = newShopList + '/' + the_property + ':' + broken_info[1];
							else
								newShopList = the_property + ':' + broken_info[1];
							//alert('Cookie now looks like: ('+newShopList+')');
						}
					}
				}
			//alert('Cookie now looks like: ('+newShopList+')');
			if ( newShopList.length > 0) {
				//alert('setting cookie to new value');
				SetCookie( cookieShopID, newShopList, expDate, "/");
			} else {
				DeleteCookie(cookieShopID, "/");	
			}
		}
	}

    // ---------------------------------------------------------------
    //  Cookie Functions - Second Helping  (21-Jan-96)
    //  Written by:  Bill Dortch, hIdaho Design <BDORTCH@NETW.COM>
    //  The following functions are released to the public domain.
    //
    //  The Second Helping version of the cookie functions dispenses with
    //  my encode and decode functions, in favor of JavaScript's new built-in
    //  escape and unescape functions, which do more complete encoding, and
    //  which are probably much faster.
    //
    //  The new version also extends the SetCookie function, though in
    //  a backward-compatible manner, so if you used the First Helping of
    //  cookie functions as they were written, you will not need to change any
    //  code, unless you want to take advantage of the new capabilities.
    //
    //  The following changes were made to SetCookie:
    //
    //  1.  The expires parameter is now optional - that is, you can omit
    //      it instead of passing it null to expire the cookie at the end
    //      of the current session.
    //
    //  2.  An optional path parameter has been added.
    //
    //  3.  An optional domain parameter has been added.
    //
    //  4.  An optional secure parameter has been added.
    //
    //  For information on the significance of these parameters, and
    //  and on cookies in general, please refer to the official cookie
    //  spec, at:
    //
    //      http://www.netscape.com/newsref/std/cookie_spec.html    
    //
    //
    // "Internal" function to return the decoded value of a cookie
    //
    function getCookieVal (offset) {
      var endstr = document.cookie.indexOf (";", offset);
      if (endstr == -1)
        endstr = document.cookie.length;
      return unescape(document.cookie.substring(offset, endstr));
    }

    //
    //  Function to return the value of the cookie specified by "name".
    //    name - String object containing the cookie name.
    //    returns - String object containing the cookie value, or null if
    //      the cookie does not exist.
    //
    function GetCookie (name) {
      var arg = name + "=";
      var alen = arg.length;
      var clen = document.cookie.length;
      var i = 0;
      while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg)
          return getCookieVal (j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break; 
      }
      return null;
    }

    //
    //  Function to create or update a cookie.
    //    name - String object object containing the cookie name.
    //    value - String object containing the cookie value.  May contain
    //      any valid string characters.
    //    [expires] - Date object containing the expiration data of the cookie.  If
    //      omitted or null, expires the cookie at the end of the current session.
    //    [path] - String object indicating the path for which the cookie is valid.
    //      If omitted or null, uses the path of the calling document.
    //    [domain] - String object indicating the domain for which the cookie is
    //      valid.  If omitted or null, uses the domain of the calling document.
    //    [secure] - Boolean (true/false) value indicating whether cookie transmission
    //      requires a secure channel (HTTPS).  
    //
    //  The first two parameters are required.  The others, if supplied, must
    //  be passed in the order listed above.  To omit an unused optional field,
    //  use null as a place holder.  For example, to call SetCookie using name,
    //  value and path, you would code:
    //
    //      SetCookie ("myCookieName", "myCookieValue", null, "/");
    //
    //  Note that trailing omitted parameters do not require a placeholder.
    //
    //  To set a secure cookie for path "/myPath", that expires after the
    //  current session, you might code:
    //
    //      SetCookie (myCookieVar, cookieValueVar, null, "/myPath", null, true);
    //
    function SetCookie (name, value) {
      var argv = SetCookie.arguments;
      var argc = SetCookie.arguments.length;
      var expires = (argc > 2) ? argv[2] : null;
      var path = (argc > 3) ? argv[3] : null;
      var domain = (argc > 4) ? argv[4] : null;
      var secure = (argc > 5) ? argv[5] : false;
      document.cookie = name + "=" + escape (value) +
        ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
        ((path == null) ? "" : ("; path=" + path)) +
        ((domain == null) ? "" : ("; domain=" + domain)) +
        ((secure == true) ? "; secure" : "");
    }

    //  Function to delete a cookie. (Sets expiration date to current date/time)
    //    name - String object containing the cookie name
    //
    function DeleteCookie (name) {
      var argv = DeleteCookie.arguments;
      var argc = DeleteCookie.arguments.length;
      var exp = new Date();
      exp.setTime (exp.getTime() - 1);  // This cookie is history
      var cval = GetCookie (name);
      var path = (argc > 1) ? argv[1] : null;
      document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString() + "; path=" + path;
    }

// -->