
// tested in ns 4.08, 6.2 & ie 5.5

// get cookie
function GetCookie(name) {
    var start = document.cookie.indexOf(name+"=");
    var len = start+name.length+1;
    if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
    if (start == -1) return null;
    var end = document.cookie.indexOf(";",len);
    if (end == -1) end = document.cookie.length;
    return unescape(document.cookie.substring(len,end));
}

// set cookie
function SetCookie(name,value,expires,path,domain,secure) {
    var expires = new Date("December 31, 2010");
    document.cookie = name + "=" +escape(value) +
    ( (expires) ? ";expires=" + expires.toGMTString() : "") +
    ( (path) ? ";path=" + path : "") + 
    ( (domain) ? ";domain=" + domain : "") +
    ( (secure) ? ";secure" : "");
    return;
}

// delete entire watchlist
function DeleteWatchlist (name) {
    var expireNow = new Date();
    document.cookie = name + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    return;
}

// add one pet to watchlist
function AddToWatchlist (_id,_name)  {
    var already_added = "";
    watchlist = GetCookie('watchlist');
    if (watchlist != null) {
        already_added = IsInList(watchlist,_id);
    }	
    if (already_added != "yes")  {
        watchlist = watchlist + "," + _id;	
        SetCookie('watchlist',watchlist);
    }    
    alert("    " +_name + " thanks you and is\nbeing added to your watchlist.");
    return false;
}

//  check pet list for already existing id
function IsInList(_list,_id)  {
    var already_in = "no";
    list_array = _list.split(",");
    for (var i=0;i < list_array.length; i++)  {
         if (list_array[i] == _id)  {
	    already_in = "yes";
	 }
    }
    return already_in;
}

// delete one from watchlist
function DeleteFromWatchlist (_id,_name)  {
    var new_watchlist = "";
    watchlist = GetCookie('watchlist');
    already_in = IsInList(watchlist,_id);	
    if ((watchlist != null) && (already_in == "yes")) {
        watchlist_array = watchlist.split(",");  	
        for (var i=0; i < watchlist_array.length; i++)  {  
            if (watchlist_array[i] != _id) {
	        new_watchlist += watchlist_array[i] + ",";
	    }  
        }
	new_watchlist_length = new_watchlist.length;
	new_watchlist = new_watchlist.substring(0,new_watchlist_length-1);
	SetCookie('watchlist',new_watchlist);
    }	
    alert(_name + " is being removed from your watchlist.");
    return false;
}
