
	var cook = unescape(document.cookie);
//alert(cook);
// get rid of session thing...	
// just get list=....; part
	if (cook.indexOf("list=") > -1 ) {
		var start = cook.indexOf("list=");
		var stop = cook.indexOf(";", start);
		if(stop == -1) {
			cook = cook.substring(start)
		} else {
			cook = cook.substring(start, stop )
		}
	}


	var cookieExtras = ";domain=www.bowdoin.edu;expires=Wednesday, 15-Oct-08 23:12:40 GMT;path=/";


	function listEntriesPlain() {

		if(cook.length > 0) {
			displayCookiePlain();
		}

	}

	function listEntries() {

		if(cook.length > 0) {
			displayCookie();
		}

	}

	function badChars( nm, vl) {

		if( nm.indexOf("*") > -1 || nm.indexOf("^") > - 1 || vl.indexOf("*") > -1 || vl.indexOf("^") > -1 ) {
			alert("Link names and URLs may not contain * or ^ characters.");
			return true;
		}
		return false;
	}

	function addEntry(frm) {

		if( badChars( frm.newName.value, frm.newValue.value ) ) {
			return;
		}

		if( frm.newName.value.length > 0 || frm.newValue.value.length > 0) {

			var ck = cook;

			if(ck.indexOf("^") > -1) {
				ck = ck + "*";
			} else {
				ck = "list=";
			}
			ck = ck + frm.newName.value + "^" + frm.newValue.value + cookieExtras;
			document.cookie = ck;
//			alert("cooke:"+ck);
			window.location.reload();
		} else {
			alert("Please supply a name and URL for your new entry.");
			frm.newName.focus();
		}

	}

	function removeEntry(entryNum) {

		var cNV = parseCookie() //document.cookie);
		var ck = "list=";

//		alert(cNV[0].length);
		if(cNV[0].length > 1) {
			for(var i=0; i < cNV[0].length; i++) {
				if(i != entryNum) {
					ck = ck + cNV[0][i] + "^"+cNV[1][i] + "*";
				}
			}
		}

		if( ck.lastIndexOf("*") == ck.length - 1) {
			ck = ck.substring(0, ck.length - 1);
		}

		document.cookie = ck+cookieExtras;

		window.location.reload();
	}

       	function displayCookiePlain() {

		cNV = parseCookie();

		if( cNV != null ) {
			document.write("<p class='caption'>");
			for(var j=0; j < cNV[0].length; j++) {
				document.write("<a href='"+cNV[1][j] + "'>"+cNV[0][j]+"</a><br>");
			}
			document.write("</p>");
		} else {
			document.write("<p class='caption'><i>To add to your favorites, click the above link.</i></p>");
		}

	}



	function displayCookie() {

		cNV = parseCookie();

		if( cNV != null ) {
			for(var j=0; j < cNV[0].length; j++) {
				document.write("<nobr>"+(j+1)+". &nbsp;<a href='"+cNV[1][j] + "'>"+cNV[0][j]+"</a> [<a href='javascript:removeEntry("+j+");'>Remove</a>]</nobr><br>");
			}
		} else {
			document.write("<i>No entries</i>");
		}

	}


	function parseCookie() {
		var ck = cook; //unescape(document.cookie);
		var rtnArray = new Array(2);
		var stringToSplit = "";

		if(ck.lastIndexOf(";") > -1) {
			stringToSplit = ck.substring(ck.indexOf("=")+1, ck.lastIndexOf(";") );	
		} else {
			stringToSplit = ck.substring(ck.indexOf("=")+1 );
		}
//		alert("Listing:"+stringToSplit);
		if(stringToSplit == "") {
			return null;
		}

		var pairs = stringToSplit.split("*");

		var names = new Array(pairs.length);
		var values = new Array(pairs.length);

		for (var i=0; i < pairs.length; i++) {
			if(pairs[i].indexOf("^") < 0 ) {
//				alert(pairs[i]+":"+i);
				return null;
		}

			var nameValue = pairs[i].split("^");
			names[i] = nameValue[0];
			values[i] = nameValue[1];			
		}

//		alert("feg"+names);
		rtnArray[0] = names;
		rtnArray[1] = values;
		
//		alert("neh:"+rtnArray);
		return rtnArray;
				
	}

	function clearForm(frm) {
		frm.newName.value = "";
		frm.newValue.value = "";

	}
