// Document modified by Hens Breet, August 2004
// No expiration date is supplied when creating th cookie, making the cookie SESSION-ONLY

checkCookieEnable("/errors/cookiesoff.html")

//This function is used to retrieve a value from a name value pair
//stored in a cookie.  There are several assumptions being made
//each one is tested for and a false value is returned if the assumptions/requirements
//are not met
//1.  The cookie name value pairs are delimited by a semicolon ";"
//2.  Each name value pair is delimited by an equal sign "name=value"

//added 5/04 by JL
function setNameVal(cookieName, keyName, keyValue)
{

    var cookieString;
    var cookieArray;
    var cookieOutStr = "";
    var cookieKeyFound = false;
    var expdate = new Date();

    expdate.setFullYear(expdate.getFullYear() + 30);
    keyValue =  keyValue.replace("=","").replace(";","")

    cookieString = GetCookie(cookieName);

    if (cookieString != null)
    {
        cookieArray = cookieString.split(";");

        for (j = 0;  j < cookieArray.length; j++)
        {
             var nameVal = cookieArray[j].split("=");
             if(nameVal.length > 0 && nameVal[0] == keyName)
             {
                 cookieOutStr += keyName + "=" + keyValue + ";" ;
                 cookieKeyFound = true;
             }
             else
             {
                     if(cookieArray[j] != "") cookieOutStr += cookieArray[j] + ";";
             }
        }
     }

    if(!cookieKeyFound)
    {
     cookieOutStr += keyName + "=" + keyValue + ";" ;
    }
    
    //alert("setting cookie " + cookieName + "\nto " + cookieOutStr + "\nexpiring on " + expdate.toGMTString())
    SetCookie(cookieName,cookieOutStr)
}

function getNameVal(cookieName,valName) 
{
    
    var cookieString;
    var cookieArray;
    var nameValArray;
    var nameValStr;
    var nameValLength;
    var i;
    
    cookieString = GetCookie(cookieName);
    
    if (cookieString == null)
    {
        //alert("Debugg -- Cookie " + cookieName + " does not exist");
        return false;
    }
    
    cookieArray = getStringArr(cookieString,";");
    
    if (cookieArray.length == 0) 
    {
       // alert("Debugg -- Cookie does not contain values");
        return false;
    }
        
    nameValStr = setArrayStr(cookieArray,"=");
    nameValArray = getStringArr(nameValStr,"=");
    
    if (nameValArray == 0)
    {
        //alert("Debugg -- nameValArray == 0");
        return false;
    }
    
    nameValLength = nameValArray.length;
    
    for (i = 0; i < nameValLength;)
    {
        //alert(nameValArray[i]);
        if (nameValArray[i].toLowerCase() == valName.toLowerCase())
        {
            //alert(nameValArray[i+1]);
            //return stringReplace(nameValArray[i+1], "+", " ");
            return unescape(nameValArray[i+1]);
        }
        i=i+1; //increment every other array member to just get the key values
    }
    //alert("no entry found in the " + cookieName + " for " + valName);
}


//Purpose of this function is to create a "symbol" delimited
//string out of an array of values
function setArrayStr(strArray,sDelimiter)
{
    var arrLength;
    var sReturn = ""; 
    var i; 
    arrLength = strArray.length;
    if (arrLength == 0)
    {
        //alert("Nothing to concatenate");
        return false;
    }
    else
    {
        for (i=0;i<arrLength;i++)
        {
            sReturn = sReturn + strArray[i] + sDelimiter; 
            
        }
    }
    return sReturn
}   
    
    
    
//Purpose of this function is to create a string array
//out of a "symbol" delimited string
function getStringArr(strCookieVals,sDelimiter)
{
    var sReturn
    sReturn = (sDelimiter.length > 0)? strCookieVals.split(sDelimiter) : false;
    if (! sReturn)
    {
        alert("Delimiter not specified")
        return false
    }
    else
    {   
        return sReturn
    }
}
    
    
function checkCookieEnable(sRedirect)
{
    
    var sCheckCookieName = "check"
    var sCheckCookieVal = "000"
    var sCookieVal = ""
    var expdate = new Date ();
    expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000));
    SetCookie(sCheckCookieName,sCheckCookieVal);
    sCookieVal = GetCookie(sCheckCookieName)
    DeleteCookie(sCheckCookieName,"/")
    
    if (sCookieVal==null)
    {
        document.location.href = sRedirect
    }
    
    
}


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;
}
//

//
//  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, "/");

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";
  }
}

