/////////////////////////////////////////////////////////////////////
//  SJL 9/12/98                                                    //
//                                                                 //
//  These public domain cookie functions were swiped from the web. //
//  The latest versions can be found at:                           //
//      http://www.hidaho.com/cookies/cookie.txt                   //
/////////////////////////////////////////////////////////////////////


//  Cookie Functions -- "Night of the Living Cookie" Version (25-Jul-96)
//
//  Written by:  Bill Dortch, hIdaho Design <bdortch@hidaho.com>
//  The following functions are released to the public domain.
//
//  This version takes a more aggressive approach to deleting
//  cookies.  Previous versions set the expiration date to one
//  millisecond prior to the current time; however, this method
//  did not work in Netscape 2.02 (though it does in earlier and
//  later versions), resulting in "zombie" cookies that would not
//  die.  DeleteCookie now sets the expiration date to the earliest
//  usable date (one second into 1970), and sets the cookie's value
//  to null for good measure.
//
//  Also, this version adds optional path and domain parameters to
//  the DeleteCookie function.  If you specify a path and/or domain
//  when creating (setting) a cookie**, you must specify the same
//  path/domain when deleting it, or deletion will not occur.
//
//  The FixCookieDate function must now be called explicitly to
//  correct for the 2.x Mac date bug.  This function should be
//  called *once* after a Date object is created and before it
//  is passed (as an expiration date) to SetCookie.  Because the
//  Mac date bug affects all dates, not just those passed to
//  SetCookie, you might want to make it a habit to call
//  FixCookieDate any time you create a new Date object:
//
//    var theDate = new Date();
//    FixCookieDate (theDate);
//
//  Calling FixCookieDate has no effect on platforms other than
//  the Mac, so there is no need to determine the user's platform
//  prior to calling it.
//
//  This version also incorporates several minor coding improvements.
//
//  **Note that it is possible to set multiple cookies with the same
//  name but different (nested) paths.  For example:
//
//    SetCookie ("color","red",null,"/outer");
//    SetCookie ("color","blue",null,"/outer/inner");
//
//  However, GetCookie cannot distinguish between these and will return
//  the first cookie that matches a given name.  It is therefore
//  recommended that you *not* use the same name for cookies with
//  different paths.  (Bear in mind that there is *always* a path
//  associated with a cookie; if you don't explicitly specify one,
//  the path of the setting document is used.)
//  
//  Revision History:
//
//    "Toss Your Cookies" Version (22-Mar-96)
//      - Added FixCookieDate() function to correct for Mac date bug
//
//    "Second Helping" Version (21-Jan-96)
//      - Added path, domain and secure parameters to SetCookie
//      - Replaced home-rolled encode/decode functions with Netscape's
//        new (then) escape and unescape functions
//
//    "Free Cookies" Version (December 95)
//
//
//  For information on the significance of cookie parameters, and
//  and on cookies in general, please refer to the official cookie
//  spec, at:
//
//      http://www.netscape.com/newsref/std/cookie_spec.html    
//
//******************************************************************
//
// "Internal" function to return the decoded value of a cookie
//
function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}
//
//  Function to correct for 2.x Mac date bug.  Call this function to
//  fix a date object prior to passing it to SetCookie.
//  IMPORTANT:  This function should only be called *once* for
//  any given date object!  See example at the end of this document.
//
function FixCookieDate (date) {
  var base = new Date(0);
  var skew = base.getTime(); // dawn of (Unix) time - should be 0
  if (skew > 0)  // Except on the Mac - ahead of its time
    date.setTime (date.getTime() - skew);
}
//
//  Function to return the value of the cookie specified by "name".
//    name - String object containing the cookie name.
//    returns - String object containing the cookie value, or null if
//      the cookie does not exist.
//
function GetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; 
  }
  return null;
}
//
//  Function to create or update a cookie.
//    name - String object containing the cookie name.
//    value - String object containing the cookie value.  May contain
//      any valid string characters.
//    [expires] - Date object containing the expiration data of the cookie.  If
//      omitted or null, expires the cookie at the end of the current session.
//    [path] - String object indicating the path for which the cookie is valid.
//      If omitted or null, uses the path of the calling document.
//    [domain] - String object indicating the domain for which the cookie is
//      valid.  If omitted or null, uses the domain of the calling document.
//    [secure] - Boolean (true/false) value indicating whether cookie transmission
//      requires a secure channel (HTTPS).  
//
//  The first two parameters are required.  The others, if supplied, must
//  be passed in the order listed above.  To omit an unused optional field,
//  use null as a place holder.  For example, to call SetCookie using name,
//  value and path, you would code:
//
//      SetCookie ("myCookieName", "myCookieValue", null, "/");
//
//  Note that trailing omitted parameters do not require a placeholder.
//
//  To set a secure cookie for path "/myPath", that expires after the
//  current session, you might code:
//
//      SetCookie (myCookieVar, cookieValueVar, null, "/myPath", null, true);
//
function SetCookie (name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

//  Function to delete a cookie. (Sets expiration date to start of epoch)
//    name -   String object containing the cookie name
//    path -   String object containing the path of the cookie to delete.  This MUST
//             be the same as the path used to create the cookie, or null/omitted if
//             no path was specified when creating the cookie.
//    domain - String object containing the domain of the cookie to delete.  This MUST
//             be the same as the domain used to create the cookie, or null/omitted if
//             no domain was specified when creating the cookie.
//
function DeleteCookie (name,path,domain) {
  if (GetCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}




///////////////////////////////////////////////////////////////////////////
// The following are for storing and retrieving a loan period in cookies //
// Obviously these are ALSi specific (SJL 11/12/98)                      //
///////////////////////////////////////////////////////////////////////////

// Retrieves the loan period from the pair of cookies with names
// created using "LoanPeriodName"
//
// The result is an object with two properties: "Num" (a number,
// 0..5) and "Date" (a string, which can be empty if the loan
// period doesn't have a date associated with it).
//
function RetrieveLoanPeriod( LoanPeriodName )
{
	var LoanPeriodNumName = LoanPeriodName + "_num";
	var LoanPeriodNumValue = "0";
	if ( GetCookie(LoanPeriodNumName) )
		LoanPeriodNumValue = "" + GetCookie(LoanPeriodNumName);

	var LoanPeriodDateName = LoanPeriodName + "_date";
	var LoanPeriodDateValue = "";
	if ( GetCookie(LoanPeriodDateName) )
		LoanPeriodDateValue = GetCookie(LoanPeriodDateName);

	var LoanPeriodObj = new Object;
	LoanPeriodObj.Num = parseInt( LoanPeriodNumValue, 10 );
	LoanPeriodObj.Date = LoanPeriodDateValue;

	return ( LoanPeriodObj );
}

// Stores the loan period number and loan period date, given by
// "LoanPeriodNum" (a number, 0..5) and "LoanPeriodDate" (a string)
// respectively, in a pair of cookies (with names created using
// "LoanPeriodName") that live until the browser closes
//
// Pass an empty string for the date if the loan period doesn't
// have a date associated with it
//
function StoreLoanPeriod(LoanPeriodName, LoanPeriodNum, LoanPeriodDate)
{
	var LoanPeriodNumName = LoanPeriodName + "_num";
	SetCookie( LoanPeriodNumName, "" + LoanPeriodNum );

	var LoanPeriodDateName = LoanPeriodName + "_date";
	SetCookie( LoanPeriodDateName, LoanPeriodDate );

	return;
}

// Stores the status of the filters radio buttons, shown on the 
// BulkRenewParametersDialog pop-up. This is stored as
// "FiltersOnOffStatus", which can have one of the following
// string values: "Off" (filters disabled), "All" (filters will
// apply to all types of borrower) or "Pseudo" (filters will
// apply only to pseudo borrowers).
//
function StoreFiltersOnOffStatus( FiltersOnOffValue )
{
	SetCookie( "FiltersOnOffStatus", "" + FiltersOnOffValue );
	return;
}

// Retrieves the status of the filters radio buttons, shown on the 
// BulkRenewParametersDialog pop-up.
//
// The result is one of the following possible string values:
// "Off" (filters disabled), "All" (filters will apply to all types
// of borrower) or "Pseudo" (filters will apply only to pseudo
// borrowers). "Off" is the default.
//
function RetrieveFiltersOnOffStatus()
{
	var FiltersOnOffValue = "Off";

	if ( GetCookie( "FiltersOnOffStatus" ) )
		FiltersOnOffValue = "" + GetCookie( "FiltersOnOffStatus" );

	return ( FiltersOnOffValue );
}

// Stores bulk renewal filter settings entered by the user.
// These are stored as a series of string variables representing
// the different settings ( "1" and "0" are used for Booleans).
//
function StoreFilters( 
						IssueDate,
						IssueLocn,
						DueDate,
						OnDueDate,
						NotAccessioned,
						Reserved,
						NotRenewable,
						RenewalLimit,
						NotForLoan,
						RenewalFees,
						Fines,
						DebtTooHigh )
{
	SetCookie( "FiltersIssueDate",		"" + IssueDate		);
	SetCookie( "FiltersIssueLocn",		"" + IssueLocn		);
	SetCookie( "FiltersDueDate",		"" + DueDate		);
	SetCookie( "FiltersOnDueDate",		"" + OnDueDate		);
	SetCookie( "FiltersNotAccessioned",	"" + NotAccessioned	);
	SetCookie( "FiltersReserved",		"" + Reserved		);
	SetCookie( "FiltersNotRenewable",	"" + NotRenewable	);
	SetCookie( "FiltersRenewalLimit",	"" + RenewalLimit	);
	SetCookie( "FiltersNotForLoan",		"" + NotForLoan		);
	SetCookie( "FiltersRenewalFees",	"" + RenewalFees	);
	SetCookie( "FiltersFines",			"" + Fines			);
	SetCookie( "FiltersDebtTooHigh",	"" + DebtTooHigh	);
	return;
}

// Retrieves bulk renewal filter settings saved by the user.
//
// The result is an object with a series of properties representing
// the various settings ( "1" and "0" are used for Booleans).
// If no value has been stored, for any reason, sensible defaults
// apply (see below).
//
function RetrieveFilters()
{
	var IssueDate		= "";
	var IssueLocn		= "0";
	var DueDate			= "";
	var OnDueDate		= "0";
	var NotAccessioned	= "0";
	var Reserved		= "0";
	var NotRenewable	= "0";
	var RenewalLimit	= "0";
	var NotForLoan		= "0";
	var RenewalFees		= "0";
	var Fines			= "0";
	var DebtTooHigh		= "0";

	if ( GetCookie( "FiltersIssueDate" ) )
		IssueDate = GetCookie( "FiltersIssueDate" );

	if ( GetCookie( "FiltersIssueLocn" ) )
		IssueLocn = GetCookie( "FiltersIssueLocn" );

	if ( GetCookie( "FiltersDueDate" ) )
		DueDate = GetCookie( "FiltersDueDate" );

	if ( GetCookie( "FiltersOnDueDate" ) )
		OnDueDate = GetCookie( "FiltersOnDueDate" );

	if ( GetCookie( "FiltersNotAccessioned" ) )
		NotAccessioned = GetCookie( "FiltersNotAccessioned" );

	if ( GetCookie( "FiltersReserved" ) )
		Reserved = GetCookie( "FiltersReserved" );

	if ( GetCookie( "FiltersNotRenewable" ) )
		NotRenewable = GetCookie( "FiltersNotRenewable" );

	if ( GetCookie( "FiltersRenewalLimit" ) )
		RenewalLimit = GetCookie( "FiltersRenewalLimit" );

	if ( GetCookie( "FiltersNotForLoan" ) )
		NotForLoan = GetCookie( "FiltersNotForLoan" );

	if ( GetCookie( "FiltersRenewalFees" ) )
		RenewalFees = GetCookie( "FiltersRenewalFees" );

	if ( GetCookie( "FiltersFines" ) )
		Fines = GetCookie( "FiltersFines" );

	if ( GetCookie( "FiltersDebtTooHigh" ) )
		DebtTooHigh = GetCookie( "FiltersDebtTooHigh" );

	var FiltersObj = new Object;

	FiltersObj.IssueDate		= IssueDate;
	FiltersObj.IssueLocn		= IssueLocn;
	FiltersObj.DueDate			= DueDate;
	FiltersObj.OnDueDate		= OnDueDate;
	FiltersObj.NotAccessioned	= NotAccessioned;
	FiltersObj.Reserved			= Reserved;
	FiltersObj.NotRenewable		= NotRenewable;
	FiltersObj.RenewalLimit		= RenewalLimit;
	FiltersObj.NotForLoan		= NotForLoan;
	FiltersObj.RenewalFees		= RenewalFees;
	FiltersObj.Fines			= Fines;
	FiltersObj.DebtTooHigh		= DebtTooHigh;

	return ( FiltersObj );
}

// Stores the status of the invoice print filters radio buttons,
// shown on the BrowseSum_Account_PrintDialog pop-up. This is 
// stored as "IFOnOffStatus", which can have one of
// the following string values: "Off" (filters disabled) or "All"
// (filters will apply).
//
function StoreInvoiceFiltersOnOffStatus( FiltersOnOffValue )
{
	SetCookie( "IFOnOffStatus", "" + FiltersOnOffValue );
	return;
}

// Retrieves the status of the invoice print filters radio buttons,
// shown on the BrowseSum_Account_PrintDialog pop-up.
//
// The result is one of the following possible string values:
// "Off" (filters disabled) or "All" (filters will apply).
// "Off" is the default.
//
function RetrieveInvoiceFiltersOnOffStatus()
{
	var FiltersOnOffValue = "Off";

	if ( GetCookie( "IFOnOffStatus" ) )
		FiltersOnOffValue = "" + GetCookie( "IFOnOffStatus" );

	return ( FiltersOnOffValue );
}

// Stores invoice filter settings entered by the user (except library).

function StoreInvoiceFilters( GreaterThan,
							  AndOr,
							  OlderThan,
							  AdditionalText,
							  SelectAll,
							  Use2ndAddress,
							  AccountTypes )
{
	SetCookie( "IFGreaterThan",		"" + GreaterThan	);
	SetCookie( "IFAndOr",			"" + AndOr			);
	SetCookie( "IFOlderThan",		"" + OlderThan		);
	SetCookie( "IFAdditionalText",	"" + AdditionalText	);
	SetCookie( "IFSelectAll",		"" + SelectAll		);
	SetCookie( "IFUse2ndAddress",	"" + Use2ndAddress	);
	SetCookie( "IFAccountTypes",	"" + AccountTypes	);
}

function RetrieveInvoiceFilters()
{
	var GreaterThan		= "";
	var AndOr			= "0";
	var OlderThan		= "0";
	var AdditionalText	= "";
	var SelectAll		= "true";
	var Use2ndAddress	= "false";
	var AccountTypes	= "";

	if ( GetCookie( "IFGreaterThan" ) )
		GreaterThan = GetCookie( "IFGreaterThan" );

	if ( GetCookie( "IFAndOr" ) )
		AndOr = GetCookie( "IFAndOr" );

	if ( GetCookie( "IFOlderThan" ) )
		OlderThan = GetCookie( "IFOlderThan" );

	if ( GetCookie( "IFAdditionalText" ) )
		AdditionalText = GetCookie( "IFAdditionalText" );

	if ( GetCookie( "IFSelectAll" ) )
		SelectAll = GetCookie( "IFSelectAll" );

	if ( GetCookie( "IFUse2ndAddress" ) )
		Use2ndAddress = GetCookie( "IFUse2ndAddress" );

	if ( GetCookie( "IFAccountTypes" ) )
		AccountTypes = GetCookie( "IFAccountTypes" );

	var FiltersObj = new Object;

	FiltersObj.GreaterThan		= GreaterThan;
	FiltersObj.AndOr			= AndOr;
	FiltersObj.OlderThan		= OlderThan;
	FiltersObj.AdditionalText	= AdditionalText;
	FiltersObj.SelectAll		= SelectAll;
	FiltersObj.Use2ndAddress	= Use2ndAddress;
	FiltersObj.AccountTypes		= AccountTypes;

	return ( FiltersObj );
}

