/*

above are cookie implementation framework described in oreilly 
beyond this point, the function defined are written by samson

*/

//This function remove elements
function remove_from_list(cookie_name, element){
	var loadedElements = new Cookie(document, cookie_name, 8640, '/cameras');
	if (loadedElements.load() ){
		loaded_list = loadedElements.pid.split(',');

		for ( i=0; i<loaded_list.length; i++){
			if (loaded_list[i] == element){
				loaded_list.splice(i, 1);
			}
		}

		loadedElements.pid=loaded_list.join(",");
		loadedElements.store();
	}

}

//This function always adds to the front
function add_to_list(cookie_name, element, list_length_limit){
	if (list_length_limit == 0)
	{
		list_length_limit = 8;
	}
	var loadedElements = new Cookie(document, cookie_name, 8640, '/cameras');

	if (!loadedElements.load() ){
		// Cannot load, so we initialize it
		var loaded_list = new Array (element);
		loadedElements.pid=loaded_list.join(",");
		loadedElements.store();
	}else{
		// Cookie present, we load and add into cookie

		var loaded_list = loadedElements.pid.split(',');

		for ( i=0; i<loaded_list.length; i++){
			if (loaded_list[i] == element ){
				loaded_list.splice(i, 1);
			}
		}

		//Put in the front of the list
		loaded_list.unshift(element);

		loaded_list.splice(list_length_limit, 99);

		loadedElements.pid=loaded_list.join(",");

		loadedElements.store();
	}

}

function clear_list(cookie_name){
	var loadedElements = new Cookie(document, cookie_name, 8640, '/cameras');
	loadedElements.remove();
}



//Below are auxiluxy functions


function show(cookie_name){
	var loadedElements = new Cookie(document, cookie_name, 8640, '/cameras');
	if (loadedElements.load() ){
		document.getElementById("test_target").innerHTML = loadedElements.pid;
	}else{
		document.getElementById("test_target").innerHTML = 'cannot load';
	}
}

function testclick(cookie_name){
	var loadedElements = new Cookie(document, cookie_name, 8640, '/cameras');

	if (product.load() ){

		document.getElementById("test_target").innerHTML = '';

		var a = product.pid.split(',');

		for ( var i=0; i<a.length; i++){
			document.getElementById("test_target").innerHTML += a[i] + "<br>";
		}

	}

}