/*============================================================================ 
Cookies handling routines

Copyright (c) 2005 DragonLord Enterprises, Inc.  All rights reserved.
Put in the public domain for NASA 2009.
==============================================================================
*/

var MyCookie;

function dumpCookie()
{
  alert( "MyCookie count = "+MyCookie.count);
  for( var i=1; i<=MyCookie.count; i++)
	alert( "cookieVal " + i + ":" + MyCookie.names[i] + "=" + MyCookie.values[i] );
}


function Cookie_read_parse_cookie()
//Reads in the cookie so we can work with it.
{
//Gets the document.cookie, splits it apart into names,values lists.
//Cookie is format name1=val1; name2=val2; etc.  Skips/ignores if no = sign.
//Returns a new object with slots names[], values[], count.  Lists 0-based.
   	var cookie = document.cookie;	//Read the magic string.
   	if(cookie == "") { return null; }
   	var pairs = cookie.split("; "); 
 	var j = 0;
   	for(var i = 0; i < pairs.length; i++)
   	{	var pos = pairs[i].indexOf('='); 
     	if(pos == -1)	{ continue; } 
     	var cookie_name = pairs[i].substring(0,pos); 
     	var cookie_value = pairs[i].substring(pos+1); 
     	if( cookie_name == "expires" )
     	{
     		this.expires = cookie_value;
     	}
     	else
     	{
 			j++;  //array now 1-based.     	
     		this.names[j] = cookie_name; 
     		this.values[j] = cookie_value; 
     		this.lookup[cookie_name] = cookie_value;
 		}
  	} 
  	this.count = j;
}


function setPersistentCookieVarValue(VarName, Value, periodType, offset) 
{

  var expireDate = new Date();
  offset = offset / 1;
  
  var myPeriodType = periodType;
  switch (myPeriodType.toLowerCase()) 
  {
    case "years": 
     var year = expireDate.getYear();     
     // Note some browsers give only the years since 1900, and some since 0.
     if (year < 1000) year = year + 1900;     
     expireDate.setYear(year + offset);
     break;
    case "months":
      expireDate.setMonth(expireDate.getMonth() + offset);
      break;
    case "days":
      expireDate.setDate(expireDate.getDate() + offset);
      break;
    case "hours":
      expireDate.setHours(expireDate.getHours() + offset);
      break;
    case "minutes":
      expireDate.setMinutes(expireDate.getMinutes() + offset);
      break;
    default:
      alert ("Invalid periodType parameter for setPersistentCookie()");
      break;
  } 
  
  document.cookie = escape(VarName) + "=" + escape(Value) + "; expires=" + expireDate.toGMTString() + "; path=/";
}  


function getCookieVarValue (VarName) 
{
  var expr = new RegExp (escape(VarName) + "=([^;]+)");
  if ( expr.test(document.cookie + ";") ) 
  {
    expr.exec(document.cookie + ";");
    return unescape(RegExp.$1);
  }
  else return false;
}
function SessionCookiesWorkp () 
{
  document.cookie = "DoSessionCookiesWorkp=YesTheyDo";
  if (getCookieVarValue ("DoSessionCookiesWorkp")=="YesTheyDo")
    return true;
  else
    return false;
}
function PersistentCookiesWorkp () 
{
  writePersistentCookie("DoPersistentCookiesWorkp", "YesTheyDo", "minutes", 1);
  if (getCookieVarValue("DoPersistentCookiesWorkp")=="YesTheyDo")
    return true;
  else 
    return false;
}

function KillCookie (VarName) 
{
  if (getCookieVarValue(VarName)) 
    writePersistentCookie(VarName,"Being Killed","years", -1);  
  return true;     
}




function Cookie_Save()
//Writes out the cookie, saves it for next time.
{
    var date = new Date();
    date.setTime(date.getTime()+(120*24*60*60*1000));//set cookie to expire in 120 days
    var expiresPair = "expires="+date.toGMTString();
    var cookieout = "";
    for(var i = 1; i <= this.count; i++)
    {
    	cookieout = this.names[i] + "=" + this.values[i] + "; " ;
 
    //alert("Saving " + cookieout + expiresPair );
	document.cookie = cookieout + expiresPair + "; path=/";	//Write string out to magic variable, it gets recorded.
    }
}

function Cookie_Session_Save()
//Writes out the cookie, saves it for next time.
{
    var cookieout = "";
    Cookie_Wipe();  //Kill any permanent cookie with same data.  Use sledgehammer.
    for(var i = 1; i <= this.count; i++)
    {
    	cookieout = this.names[i] + "=" + this.values[i] + "; path=/"  +  "; "  ;
 
    //alert("Saving " + cookieout  );
	document.cookie = cookieout;	//Write string out to magic variable, it gets temporarily recorded.
    }
}


function Cookie_Wipe()
//Wipes out the cookie.  Lets it be a session cookie.
//Apparently not guaranteed to work well on all browsers...
{
    var date = new Date();
    date.setTime(date.getTime()-(120*24*60*60*1000));//set cookie to already expire 120 days ago
    var expiresPair = "expires="+date.toGMTString();
    var cookieout = "";
    for(var i = 1; i <= this.count; i++)
    {
    	cookieout = this.names[i] + "=" + this.values[i]  +  "; path=/"  +  "; " ;
 
    //alert("Saving " + cookieout + expiresPair );
	document.cookie = cookieout + expiresPair;	//Write string out to magic variable, it gets recorded.
	}
}

function Cookie_findindex(varname)
{
    for(var i = 1; i <= this.count; i++)
    {
    //alert( "Test: '" + this.names[i] + "' == '" + varname + "'? " + (this.names[i] == varname) );
    	if(this.names[i] == varname)
    	{
    		//alert("Found " + varname + " at " + i );
    		return(i);
    	}
    }
    //alert( "Didn't find " + varname + " returning 0 ");
    return(0);
}	
	
function Cookie_Add(varname, value)
{
	var j;
	if( !(j = this.findindex(varname)) )  //not already there?	
	{		//new, add
		this.count++;	
		j = this.count;
		this.names[j] = varname;
	}
	//alert( "Add[" + j + "] " + varname +"="+ value );
	this.values[ j ] = value;	
	this.lookup[ varname ] = value;	
}

function Cookie()
//Class for handling the cookie.
//Yes JS is case sensitive.
{
	//slots
	this.names 	= new Array;
	this.values     = new Array;
	this.lookup	= new Array;
	this.count	= 0;
	this.expires = 0;
	
	//methods
	this.read_parse_cookie = Cookie_read_parse_cookie;
	this.Save = Cookie_Save;	
	this.Session_Save = Cookie_Session_Save;	
	this.findindex = Cookie_findindex;
	this.Add = Cookie_Add;
	
	//inits
	this.read_parse_cookie();  //Go get it, fill it up.
}


//Create and read in a new Cookie.
MyCookie = new Cookie();  


