/*
* FoodPhoto Library, shopping basket.
* Code creted to manage 
*/

function getCart(name){
	name = name + "=";
	var start  = '';
	var offset = name.length;
	var dc = document.cookie;
	var dc_length = document.cookie.length;
	// make sure specified cookie exisits 
	var begin = dc.indexOf(name);
	/** uncomment to debug 
	 * alert("begin " + begin);
	 * alert("offset" + offset);
	 */
	if(begin == -1){
		return null;
	}
	else {
		/* 
	 	 * now check to get length from name to end of path 
	 	 * i.e cookname= (9 chars) start from 9 work until the 1st ;
	 	 */ 
		//var end	= document.cookie.indexOf (";", offset);
		var end	= document.cookie.indexOf (";", begin);
		// depending on how cookie is formed there might not be a ;
		if(end == -1){
		      	end = dc.length;
   		}
		start = begin+offset;
		return unescape(document.cookie.substring(start, end));
	}
}

/**
 * Set Cart 
 */
function setCart(name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape(value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

/**
 * Delete Cart
 */

function empty(name,path,domain) {
  if (getCart(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

/**
 * serialize array for cookie
 */

function serialize(array){
		var counter = array.length;
		var cookiestring ='';
		//alert(array);
		for(x in array){
			if(x < (counter -1)){
				cookiestring += array[x] + "|";
			}
			else {
				cookiestring += array[x];
			}
		}
		return cookiestring;	
}

/**
 * main worker to add and remove id's
 */
 
function modify_cart(id){
	var cookiename 	 	= "FoodPhotoLibrary";
	var cookiecontents 	= "";		
	var temp		= new Array();  // Holding array
	var dc 			= getCart(cookiename);
	//alert(dc);		// uncomment to debug 
	if(!dc){
		var photos = new Array(); 	// array to be used to manage ids 
	}
	else {
		// open cookie and split string 
		var photos = dc.split("|");	
	}
	var n = photos.length;	  		// get current counter 
	var marker = false; 		  	// marker for adding and removing 	


	var active              = document.Add_Form.addtobox;

	
	//alert(photos);
	// check to see if array is empty
	if(n > 0){
 	  for(x in photos){
		if(photos[x] != id){
			temp[temp.length] = photos[x];
			// image not in cookie array add to temp 
		} // end if
		else {
			marker = true;
		}
	} // end for 
		// now add item if marker not set 
		if(!marker){
			temp[temp.length] = id;
			// make sure marker is unset 
			marker = false; 
		}	// end if 
	}
	else {
		// add to array 
		temp[temp.length] = id;
	}
	// make sure cookie is now empty 
	if(temp.length > 0){
		cookiecontents = serialize(temp);
		//alert("Cookie Contents:" +cookiecontents); // uncomment to debug 
		setCart(cookiename,cookiecontents);
		document.getElementById('cookievalue').value = cookiecontents;
	
		active.className = 'lightboxon';
		active.disabled=false;		
	}
	else {
		active.className = 'lightboxoff';
		active.disabled=true;
		empty(cookiename);
	}		
}

// end 
