//-----------------------------------------------------------------------------
// globals
//-----------------------------------------------------------------------------
var debugCookies        = false;
var expirationOneMonth  = new Date();           // date value for one month expiration
var expirationLong      = new Date();           // date value for long expiration
//-----------------------------------------------------------------------------
// initialize expiration values
//-----------------------------------------------------------------------------
expirationOneMonth.setMonth(expirationOneMonth.getMonth() + 1);
expirationLong.setYear(expirationLong.getYear() + 10);
//-----------------------------------------------------------------------------
// standard cookie set function.
//-----------------------------------------------------------------------------
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'                           : '');
}
//-----------------------------------------------------------------------------
// standard cookie get function.
//-----------------------------------------------------------------------------
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;
}
//-----------------------------------------------------------------------------
// parse a value from the cookie (used internally)
//-----------------------------------------------------------------------------
function getCookieVal(offset)
{
    var endstr = document.cookie.indexOf(';', offset);
    if(endstr == -1)
    {
        endstr = document.cookie.length;
    }
    return unescape(document.cookie.substring(offset, endstr));
}
//-----------------------------------------------------------------------------
// returns true if user has access cookie set to GOLD or ADMIN
//-----------------------------------------------------------------------------
function checkCookieForGoldAccess()
{
    var product = 'default';
    var cookiePos = document.cookie.indexOf('LiaHua=');

    if (cookiePos != -1)
    {
        var endOfCookiePos = document.cookie.length;
        if (document.cookie.indexOf(";", cookiePos) != -1 && document.cookie.indexOf(";", cookiePos) <= endOfCookiePos)
            endOfCookiePos = document.cookie.indexOf(";", cookiePos);
        var product = document.cookie.substring(cookiePos + 7, endOfCookiePos);
    }
    return (product.toUpperCase()=='GOLD' || product.toUpperCase()=='ADMIN');
}
//-----------------------------------------------------------------------------
// if user has access to Gold product, display Gold button link.
//-----------------------------------------------------------------------------
function toggleGoldIndexButton(id)
{
    if (checkCookieForGoldAccess())
        if( document.getElementById(id) )
            document.getElementById(id).style.display='block';
}
// ----------------------------------------------------------------------------
// CookieCrumbs class that handles multiple crumb cookies
// ----------------------------------------------------------------------------
// CookieCrumbs class constructor
//   document Document for cookie         (required)
//   name     name for the cookie         (required)
//   expires  when cookie should expire   (optional, default = session)
//   path     cookie path                 (optional, default = ?)
//   domain   cookie domain               (optional, default = current domain)
//   secure   boolean for secure cookie   (optional, default = true)
function CookieCrumbs(document, name, expires, path, domain, secure)
{
    // All the predefined properties of this object begin with '$'
    // to distinguish them from other properties which are the values to
    // be stored in the cookie.
    this.$document  = document;
    this.$name      = name;
    if (expires)  this.$expires = expires;  else  this.$expires = null;
    if (path)     this.$path    = path;     else  this.$path    = null;
    if (domain)   this.$domain  = domain;   else  this.$domain  = null;
    if (secure)   this.$secure  = true;     else  this.$secure  = false;
    // the methods
    this.SaveCookie   = SaveCookie;
    this.ReadCookie   = ReadCookie;
    this.RemoveCookie = RemoveCookie;
    // ------------------------------------------------------------------------
    // This function is the Save() method of the CookieCrumbs object
    // ------------------------------------------------------------------------
    function SaveCookie()
    {
        try
        {
            // First, loop through the properties of the CookieCrumbs object and
            // put together the value of the cookie.  Since cookies use the
            // equals sign and semicolons as separators, we'll use colons
            // and ampersands for the individual state variables we store
            // within a single cookie value.  Note that we escape the value
            // of each state variable, in case it contains punctuation or other
            // illegal characters.
            var cookieval = "";
            for(var prop in this)
            {
                // ignore properties with names that begin with '$' and also methods
                if ((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function'))
                    continue;
                if (cookieval != "")
                    cookieval += '&';
                cookieval += prop + ':' + escape(this[prop]);
            }
            // Now that we have the value of the cookie, put together the
            // complete cookie string, which includes the name, and the various
            // attributes specified when the CookieCrumbs object was created.
            var cookie = this.$name + '=' + cookieval;
            if (this.$expires)
                cookie += '; expires=' + this.$expires;
            if (this.$path)
                cookie += '; path='    + this.$path;
            if (this.$domain)
                cookie += '; domain='  + this.$domain;
            if (this.$secure)
                cookie += '; secure';
            // Now store the cookie by setting the magic Document.cookie property
            this.$document.cookie = cookie;
        }
        catch (exception)
        {
            Debug("CookieCrumbs.SaveCookie(): "+exception.message);
        }
    }
    // ------------------------------------------------------------------------
    // This function is the Read() method of the CookieCrumbs object
    // ------------------------------------------------------------------------
    function ReadCookie()
    {
        try
        {
            // First, get a list of all cookies that pertain to this document.
            // We do this by reading the magic Document.cookie property
            var allcookies = this.$document.cookie;
            if (allcookies == "")
                return false;

            // Now extract just the named cookie from that list.
            var start = allcookies.indexOf(this.$name + '=');
            if (start == -1)
                return false;   // cookie not defined for this page.
            start += this.$name.length + 1;  // skip name and equals sign.
            var end = allcookies.indexOf(';', start);
            if (end == -1)
                end = allcookies.length;
            var cookieval = allcookies.substring(start, end);

            // Now that we've extracted the value of the named cookie, we've
            // got to break that value down into individual state variable
            // names and values.  The name/value pairs are separated from each
            // other with ampersands, and the individual names and values are
            // separated from each other with colons.  We use the split method
            // to parse everything.
            var a = cookieval.split('&');  // break it into array of name/value pairs
            for(var i=0; i < a.length; i++)  // break each pair into an array
            {
                a[i] = a[i].split(':');
            }
            // Now that we've parsed the cookie value, set all the names and values
            // of the state variables in this CookieCrumbs object.  Note that we unescape()
            // the property value, because we called escape() when we stored it.
            for(var i = 0; i < a.length; i++)
            {
                this[a[i][0]] = unescape(a[i][1]);
            }
            // We're done, so return the success code
            return true;
        }
        catch (exception)
        {
            Debug("CookieCrumbs.ReadCookie(): "+exception.message);
        }
    }

    // ------------------------------------------------------------------------
    // This function is the remove() method of the CookieCrumbs object.
    // ------------------------------------------------------------------------
    function RemoveCookie()
    {
        try
        {
            var cookie;
            cookie = this.$name + '=';
            if (this.$path)
                cookie += '; path=' + this.$path;
            if (this.$domain)
                cookie += '; domain=' + this.$domain;
            cookie += '; expires=Fri, 02-Jan-1970 00:00:00 GMT';
            this.$document.cookie = cookie;
        }
        catch (exception)
        {
            Debug("CookieCrumbs.RemoveCookie(): "+exception.message);
        }
    }
}
// ----------------------------------------------------------------------------
// write the hash table values into the cookie
// ----------------------------------------------------------------------------
function WriteHashTableIntoCookie(hashTable, cookieName, remove)
{
    try
    {
        // create the cookie object
        var cookie = new CookieCrumbs(document, cookieName, expirationOneMonth, "/");
        // remove existing cookie
        if (remove && remove.toString().toLowerCase() == "true")
        {
            cookie.RemoveCookie();
        }
        // all crumbs in hash table
        for (var hashKey in hashTable)
        {
            // get the hash crumb
            var hashCrumb = hashTable[hashKey];
            // get the crumb key (there will only be one)
            for (var crumbKey in hashCrumb)
            {
                // get the crumb value
                var crumbValue = hashCrumb[crumbKey];
                // fix the value
                crumbValue = FixOneZero(crumbValue);
                // save the value in the cookie object
                cookie[crumbKey] = crumbValue;
                // debug
                if (debugCookies) Debug("HC "+crumbValue+" = "+crumbKey);
            }
        }
        // write the cookie object to the document
        cookie.SaveCookie();
        if (debugCookies) Debug("WriteHashTableIntoCookie("+cookieName+")");
    }
    catch (exception)
    {
        Debug("WriteHashTableIntoCookie("+hashTable+", "+cookieName+", "+remove+"): "+exception.message);
    }
}
// ----------------------------------------------------------------------------
// read the cookie values into the hash table
// ----------------------------------------------------------------------------
function ReadCookieIntoHashTable(cookieName, hashTable)
{
    try
    {
        // create the cookie object
        var cookie = new CookieCrumbs(document, cookieName, expirationOneMonth, "/");
        // read the cookie
        cookie.ReadCookie();
        // all crumbs in hash table
        for (var hashKey in hashTable)
        {
            // get the hash crumb
            var hashCrumb = hashTable[hashKey];
            // get the crumb key (there will only be one)
            for (var crumbKey in hashCrumb)
            {
                // get the crumb value
                var crumbValue = cookie[crumbKey];
                // fix the crumb value
                crumbValue = FixOneZero(crumbValue);
                // save the value in the hash table
                hashCrumb[crumbKey] = crumbValue;
                // debug
                if (debugCookies) Debug("CH "+crumbValue+" = "+crumbKey);
            }
        }
        if (debugCookies) Debug("ReadCookieIntoHashTable("+cookieName+")");
    }
    catch (exception)
    {
        Debug("ReadCookieIntoHashTable(): "+exception.message);
    }
}
// ----------------------------------------------------------------------------
// get a bool value from the hash table
// ----------------------------------------------------------------------------
function GetHashValueAsBool(hashTable, hashKey)
{
    try
    {
        var boolValue = false;
        // get the hash crumb
        var hashCrumb = hashTable[hashKey];
        // get the crumb key (there will only be one)
        for (var crumbKey in hashCrumb)
        {
            // get the crumb value
            var crumbValue = hashCrumb[crumbKey];
            // convert to bool
            boolValue = OneZeroToBool(crumbValue);
        }
        if (debugCookies) Debug("GetHashValueAsBool("+hashKey+") = "+boolValue);
        return boolValue;
    }
    catch (exception)
    {
        Debug("GetHashValueAsBool("+hashKey+"): "+exception.message);
    }
}
// ----------------------------------------------------------------------------
// set a bool value in the hash table
// ----------------------------------------------------------------------------
function SetHashValueFromBool(hashTable, hashKey, boolValue)
{
    try
    {
        if (debugCookies) Debug("SetHashValueFromBool("+hashTable+", "+hashKey+", "+boolValue+")");
        // convert the bool to 0/1
        var crumbValue = BoolToOneZero(boolValue);
        // get the hash crumb
        var hashCrumb = hashTable[hashKey];
        // get the crumb key (there will only be one)
        for (var crumbKey in hashCrumb)
        {
            // set the crumb value
            hashCrumb[crumbKey] = crumbValue;
        }
    }
    catch (exception)
    {
        Debug("SetHashValueFromBool("+hashTable+", "+hashKey+", "+boolValue+"): "+exception.message);
    }
}
// ----------------------------------------------------------------------------
// convert a 1/0 value to a bool
// ----------------------------------------------------------------------------
function OneZeroToBool(oneZeroValue)
{
    try
    {
        var boolValue = false;
        if (FixOneZero(oneZeroValue))
        {
             boolValue = true;
        }
        return boolValue;
    }
    catch (exception)
    {
        Debug("OneZeroToBool("+oneZeroValue+"): "+exception.message);
    }
}
// ----------------------------------------------------------------------------
// convert a bool to a 1/0 value
// ----------------------------------------------------------------------------
function BoolToOneZero(boolValue)
{
    try
    {
        var oneZeroValue = 0;
        if (FixBool(boolValue))
        {
             oneZeroValue = 1;
        }
        return oneZeroValue;
    }
    catch (exception)
    {
        Debug("BoolToOneZero("+boolValue+"): "+exception.message);
    }
}
// ----------------------------------------------------------------------------
// fix a bool
// ----------------------------------------------------------------------------
function FixBool(boolValue)
{
    try
    {
        if (boolValue && boolValue.toString().toLowerCase() == "true")
        {
             boolValue = true;
        }
        else
        {
             boolValue = false;
        }
        return boolValue;
    }
    catch (exception)
    {
        Debug("FixBool("+boolValue+"): "+exception.message);
    }
}
// ----------------------------------------------------------------------------
// fix a 1/0 value
// ----------------------------------------------------------------------------
function FixOneZero(oneZeroValue)
{
    try
    {
        if (oneZeroValue && oneZeroValue == 1)
        {
             oneZeroValue = 1;
        }
        else
        {
             oneZeroValue = 0;
        }
        return oneZeroValue;
    }
    catch (exception)
    {
        Debug("FixOneZero("+boolValue+"): "+exception.message);
    }
}
// ----------------------------------------------------------------------------
// for testing only
// ----------------------------------------------------------------------------
function DebugHashTable(hashTable)
{
    try
    {
        // all crumbs in hash table
        for (var hashKey in hashTable)
        {
            // get the hash crumb
            var hashCrumb = hashTable[hashKey];
            // get the crumb key (there will only be one)
            for (var crumbKey in hashCrumb)
            {
                // get the crumb value
                var crumbValue = hashCrumb[crumbKey];
                // debug out
                Debug("H  "+crumbValue+" = "+crumbKey);
            }
        }
        Debug("DebugHashTable("+hashTable+"): ");
    }
    catch (exception)
    {
        Debug("DebugHashTable("+hashTable+"): "+exception.message);
    }
}
// ----------------------------------------------------------------------------
// for testing only
// ----------------------------------------------------------------------------
function DebugCookie(cookieName)
{
    try
    {
        // create the cookie object
        var cookie = new CookieCrumbs(document, cookieName, expirationOneMonth, "/");
        // read the cookie
        cookie.ReadCookie();
        // all crumbs in cookie
        for (var crumbKey in cookie)
        {
            // skip members of class
            if (crumbKey.charAt(0) != "$".charAt(0) && crumbKey != "ReadCookie" && crumbKey != "SaveCookie" && crumbKey != "RemoveCookie")
            {
                // get the crumb value
                var crumbValue = cookie[crumbKey];
                // debug out
                Debug("C  "+crumbValue+" = "+crumbKey);
            }
        }
        Debug("DebugCookie("+cookieName+"): ");
    }
    catch (exception)
    {
        Debug("DebugInPlayCookie("+cookieName+"): "+exception.message);
    }
}
// ----------------------------------------------------------------------------
