
//  -------- erasing a cookie is pretty easy:


        function eraseCookie(name) {
                document.cookie = name + '=blah; expires=Thu, 17 Feb 2011  20:47:11 UTC; path=/;' ;
        }

	function deleteCookie(name) {
		document.cookie = name + '=blah; expires=Thu, 17 Feb 2011  20:47:11 UTC; path=/;' ;
        }



// ------------- Cookie scripts ----------------------------




	// writeCookie("myCookie", "my name", 24);
	// Stores the string "my name" in the cookie "myCookie" which expires after 24 hours.


	// Dreamweaver Cookie snippet
	// Example:
	// writeCookie("myCookie", "my name", 24);
	// Stores the string "my name" in the cookie "myCookie" which expires after 24 hours.


 	// Safari (and some others?) need a concrete order;
        //
        // - name/value
        // - expires
        // - path
        // - domain

        //   cookieName=cookieValue[;expires=dataAsString[;path=pathAsString[;domain=domainAsString[;secure]]]]





	function writeCookie(name, value)
	{

		document.cookie = name + "=" + escape(value) + ';  path=/;';
		   
	} 

              
	////
	//
	//
	//
	// Example:
	// alert( readCookie("myCookie") );

	function readCookie(name)
	{
  		var cookieValue = "";
  		var search = name + "=";
  		if(document.cookie.length > 0)
  		{                     
   	 		offset = document.cookie.indexOf(search);
    			if (offset != -1)
    			{                   
      				offset += search.length;
      				end = document.cookie.indexOf(";", offset);
      				if (end == -1) end = document.cookie.length;
      				cookieValue = unescape(document.cookie.substring(offset, end));
    			}
  		} 
        
  		return cookieValue;   
	}       


	function showPassedMessage(){



		var pm = readCookie('PassMessage');


		if (pm.length > 3) {


			if (pm.match(/[a-z]/i)) {
				pm = ' ' + pm;
			} else {		
				pm = Octal_to_Ascii(pm);
			}

			var bb = '';
					 
			bb += "\n	<img src='/ISMB/images/gene1.png' width=200 style='float:right;'>				";
			bb += "\n													";
			bb += "\n	<span style='font-size:140%; line-height:120%; '>"	+ 	pm 	+ "</span>	";
			bb += "\n													";
			bb += "\n	<p>&nbsp;<p>&nbsp;<p>										";
                        bb += "\n                                                                                                       ";


			GC_say(bb,"60");



			writeCookie('Passmessage','');	// Ensure that even if the browser didn't expire the cookie,
							// it would have a small number of bytes associated with it.

			// alert("            Debug:   passed message is: \n\n\n" + pm + "\n\n\n   resulting in: " + bb + "\n\n");

			eraseCookie('PassMessage');

		} 

		return;
	}


	function PassMessage(verbage) {

		var bb = readCookie('PassMessage');
		writeCookie('PassMessage',verbage + bb );

		return;
	}





	//  GC_history
	//
	// GC_GetHistoryPath(n)      	Get me history elements.  To match the history.go(-x), use negative numbers.
	// GC_SetHistoryPath;		Put the current window.location in here.
	// GC_ClearHistoryPath;		Wipe out all of those cookies that have 'GC_history' associated with it.
	//
	// GC_PopHistoryPath;		Just get rid of the current (last) history element.  Helps us traverse backward, rather than
	//										     causing an infinite loop between child/parent/child/parent
	// 
	//   Similar to readCookie and writeCookie.
	//    
	//    Use this to watch where you have been.   Call it    window.history.go(-x);
	//
	//    Put the most recent web page at the front of the list.
	//
	//   You might need another check to see if the cookies were written in increasing order, or decreasing...
	//

	function GC_GetHistoryPath(target) {

			var historyValue="";
				
			var cookieName = 'GC_history';
			var search 	= cookieName;
			var remainder 	= document.cookie;


                        var cookies = document.cookie.split(";");		// Hey -- does this add a ' ' at the start of each string???
			var histories = [];					// Get a history array ready to go.

			
			var histcount = -1;							// Starting point for histories


                        for (var i = 0; i < cookies.length; i++) {				// Look backwards to find most recent cookie
                                var cookie = cookies[i];
                                var eqPos = cookie.indexOf("=");
                                var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
		
				if (name.indexOf(cookieName) > -1) {				// Found a GC_history family member.
                                        
					histcount++;
					histories[histcount] = name;
				}

			}

			if (target > histcount) { target = histcount; }

			var which = target;

			if (target < 0 ) {
				which = histcount + target;
				if (which < 0) which = 0;					// Must be at top of tree.
			}

			
			var result = readCookie(histories[which]);


			if (result.length < 2) {
				result = './';		// Assume 'the root' if result was empty.
			}

		return result;

	}



        function GC_SetHistoryPath() {

                        var cookieName = 'GC_history';

                	var dd 	 = new Date();
			var uniq = dd.getTime();

			var checkme 	= GC_GetHistoryPath(999);

			var loc 	= window.location.href ;

			loc = loc.replace(/[\?].*/,'');
			loc = loc.replace(/%3F.*/,'');


			if ( loc == checkme ) {					// If the last page was exactly the current page,
						// GC_debug(' duplicate ');
			} else {
                                writeCookie(cookieName + uniq , loc);                // add a new cookie with a new timestamp associated with the key.
			
			}

			// debug_showAllCookies();
        }


	function GC_PopHistoryPath() {

			var historyValue="";

                        var cookieName 	= 'GC_history';
                        var search      = cookieName;
                        var remainder   = document.cookie;
                        var cookies 	= document.cookie.split(";");               	// Hey -- does this add a ' ' at the start of each string???
                        var histories 	= [];                                     	// Get a history array ready to go.


                        var histcount = -1;                                                     // Starting point for histories

                        for (var i = 0; i < cookies.length; i++) {                              // Look backwards to find most recent cookie
                                var cookie = cookies[i];
                                var eqPos = cookie.indexOf("=");
                                var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;

                        // alert('start of GC_PopHistoryPathBBBBBB ' + histcount);

                                if (name.indexOf(cookieName) > -1) {                            // Found a GC_history family member.

                                        histcount++;
                                        histories[histcount] = name;
                                }
                        }

			//alert('removing  histcount=' + histcount + ' entry from the history list - value=' + histories[histcount]);

                        eraseCookie(histories[histcount]);

                return;
	}




	function GC_ClearHistoryPath() {


			document.cookie='';

			var cookies = document.cookie.split(";");


			//alert('   about to clear Histories ');

			showPassedMessage();		// Make sure we say what was given to us before we start wiping out things.

                        for (var i = 0; i < cookies.length; i++) {
                                var cookie = cookies[i];
                                var eqPos = cookie.indexOf("=");
                                var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
			
				if  	(name.indexOf('istory') > -1) {

                                	eraseCookie(name);

				}

                        }
			//eraseCookie('GC_E');
			//eraseCookie('ADMIN');
			//eraseCookie('ADMIN_USER');


	}

	function deleteAllCookies() {
   		 	var cookies = document.cookie.split(";");

    			for (var i = 0; i < cookies.length; i++) {
        			var cookie = cookies[i];
        			var eqPos = cookie.indexOf("=");
        			var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
				eraseCookie(name);

				//alert('Debug:   deleting cookie: name="' + name + '"    ');
    			}
	}

	function debug_showAllCookies() {

			var cookies = document.cookie.split(";");

                        for (var i = 0; i < cookies.length; i++) {
                                var cookie = cookies[i];
                                var eqPos = cookie.indexOf("=");
                                var name = eqPos > -1 ? cookie.substr(0, eqPos ) : cookie;
				var val = readCookie(name);
				if (val) {
					GC_debug(' ......................... showAllCookies() ..   cookie ' + i + '   "' + name + '"  =   '  + unescape(val) );
				}
                        }
        }




	// Scary extra cookie work required for Safari.

	function getSafariExtraCookies() {

			var args = '';
		        loadXMLDoc('/htbin/RMS-SafariCookies' ,'POST',args);

	}

