var GVDEBUG       = false;
var GloVars       = new Array();
var GloVarsCookie = "GloVars";


function CookiesAreEnabled()
{
	var canbakecookies = false;

    if(document.cookie == '') 
     {
        document.cookie = 'CanBakeCookies=yes';
        if(document.cookie.indexOf('CanBakeCookies=yes') != -1) 
         {
            canbakecookies = true;
         }
     } 
    else 
     {
        canbakecookies = true;
     }
    if(canbakecookies == false)
     {
        alert('The browser you are using has disabled the use of "cookies". In order to purchase items the cookies feature must be enabled in order to track your shopping cart, and to keep your personal information secure. Please see our Privacy page for more details on how we use cookies.');
     } 
    return(canbakecookies);
}

function CreateGloVar(Name,Value)
{
    this.Name  = Name;
    this.Value = Value;
}

function DeleteGloVarsCookie()
{
    var i        = 0;
    var tempstr  = "";

    if(GloVars.length > 0)
     {
        for(i = 0; i < GloVars.length; i++)
         {
            PutGloVar(GloVars[i].Name,"");
         }
     }
}

function DisplayGloVars()
{
   var tempstr = "";
   var i       = 0;

   for(i = 0; i < GloVars.length; i++)
    {
       tempstr += GloVars[i].Name + "\n";
       tempstr += GloVars[i].Value + "\n\n";
    }
   alert(tempstr);
}

function GetGloVar(Name)
{
    var i     = 0;
    var found = false;
    var gvstr = "";

    for(i = 0; i < GloVars.length; i++)
     {
        if(GloVars[i].Name == Name)
         {
            gvstr = GloVars[i].Value;
            found = true;
         }
     }
    if(found == false)
     {
        gvstr = 'Error';
     }
    return(gvstr);
}

function GetGloVarCookie(CookieKey)
{
    var tempcookie = "";

    if(CookiesAreEnabled() == false)
     {
        return;
     }
    if(document.cookie && document.cookie.indexOf(CookieKey) != -1)
     {
        tempcookie = document.cookie;
        tempcookie = tempcookie.substring(tempcookie.indexOf(CookieKey),tempcookie.length);
        if(tempcookie.indexOf(';') != -1)
         {
            tempcookie = tempcookie.substring(0,tempcookie.indexOf(';'));
         }
        return tempcookie;
     }
}

function PutGloVar(Name,Value)
{
    var i     = 0;
    var found = false;


    for(i = 0; i < GloVars.length; i++)
     {
        if(GloVars[i].Name == Name)
         {
            found = true;
            GloVars[i].Value = Value;
         }
     }
    if(found == false)
     {
        GloVars[i] = new CreateGloVar(Name,Value);
     }
    SaveGloVars();
    return(Value);
}

function ReadGloVarCookie(CookieKey)
{
    var tempcookie = "";
    var tempvalue  = "";

    if(CookiesAreEnabled() == false)
     {
        return;
     }
    tempcookie = GetGloVarCookie(CookieKey);
    if(tempcookie != '' && tempcookie != null)
     {
        tempvalue = tempcookie.substring(tempcookie.indexOf(CookieKey) + CookieKey.length + 1,tempcookie.length);
        if(tempvalue.indexOf('|') != -1)
         {
            tempvalue = tempvalue.split('|');
         }
        return tempvalue;
     }
    else
     {
        return "";
     }
}

function ReadGloVars()
{
    var i        = 0;
    var contents = "";

    if(CookiesAreEnabled() == false)
     {
        return;
     }
    for(i = 0; i < 10000; i++)
     {
        contents = ReadGloVarCookie('GloVar_' + i);
        if(contents != '' && contents != null)
         {
            itemlist   = ReadGloVarCookie('GloVar_' + i);
            GloVars[i] = new CreateGloVar(itemlist[0],itemlist[1]);
         }
        else
         {
            break;
         }
     }
}

function SaveGloVarCookie(CookieKey,CookieValue,CookieExpire)
{
    var tempcookie = "";

    if(CookiesAreEnabled() == false)
     {
        return;
     }
    tempcookie = CookieKey + '=' + CookieValue;
    if(CookieExpire != "" && CookieExpire != null)
     {
        tempcookie += ';expires=' + CookieExpire.toGMTString();
     }
    document.cookie = tempcookie;
}

function SaveGloVars()
{
    var i        = 0;
    var tempstr  = "";

    if(GloVars.length > 0)
     {
        for(i = 0; i < GloVars.length; i++)
         {
            tempstr  = "";
            tempstr += GloVars[i].Name  + '|';
            tempstr += GloVars[i].Value + '|';
            SaveGloVarCookie('GloVar_' + i,tempstr,eval(SetCookieExpire(0,0,4,0,0,0,0)));
         }
     }
}

function SetCookieExpire(S,M,H,D,W,MM,YY)
{
    exp      = new Date();
    sec      = 1000;
    min      = 60  * sec;
    hr       = 60  * min;
    day      = 24  * hr;
    wk       = 7   * day;
    mo       = 30  * day;
    yr       = 365 * day;
    maxtime  = exp.getTime();
    maxtime += (S * sec) + (M * min) + (H * hr);
    maxtime += (D * day) + (W * wk) + (MM * mo);
    maxtime += (YY * yr);
    exp.setTime(maxtime);
    return exp;
}

