/*
----------------------------------------------
 Mongie
 Created by Mason Liu, Joe Liu, and Sean Liu    Last modified 8/31/2003
 Confidential data. All rights reserved
-----------------------------------------------
*/

function getCookies()  {
/*
PURPOSE: Collect all the cookies from this server and put them together into one super object. The super object will have one property for each cookie and each of those properties will be another object housing the individual keys and values.
USAGE: highestLevelObject = getCookies();
*/
//check for existing cookies. If there are none, spit out a message.
  var returnableObject = new Object();
  if (document.cookie != "")  {
    var allOfCookie = document.cookie;
    var listOfCookies = allOfCookie.split("; ");
//Creates an object for each cookie
    for (var i in listOfCookies)  {
      var cookieName = listOfCookies[i].split("=")[0];

//Checks for special cookie cases this function can't handle
      var checkResults = cookieName.search(/^consq/i);
      if(checkResults>0)  {
        continue;
      }
      var checkResults = cookieName.search(/^TABLE/);
      if(checkResults>0)  {
        continue;
      }

      var restOfCookie = listOfCookies[i].split("=")[1];
      returnableObject[cookieName] = new Object();
      var cookieArray = restOfCookie.split("|");
//Puts the keys and values into the lower level object
      for (var j in cookieArray)  {
        var cookieParts = cookieArray[j].split("^");
        var cookieKey = cookieParts[0];
        var cookieValue = cookieParts[1];
        returnableObject[cookieName][cookieKey] = cookieValue;
      }
    }
  }
  return returnableObject;
}


function writeCookie(objName, obj)  {

/* 
!!!!!!!!!!!!
DO NOT USE THESE NAMES FOR objName:
/^consq.(one or more)/
/^table.(one or more)/
!!!!!!!!!!!!

PURPOSE: Given an object with properties, this function will write them into a cookie
USAGE: writeCookie(nameOfCookie, Object)  Both fields can not contain following special characters
    "|", "^", ";", or "=" 
The data type of "nameOfCookie" is a string. the data type of "Object" is an object.
The cookie will be named nameOfCookie. The property names and values will be written into the cookie. Only enumerated properties will be included. In other words the properties must be user defined and not implied. Avoid including methods. I have not tested what occurs when methods are present, but it is possible that methods of the object might screw this function up.
RESULTING COOKIE: nameOfCookie=prop1^value1|prop2^value2|prop3^value3
*/
//Sets the expiration date of the cookie to 1 year later.
  var expireDate = new Date();
  //expireDate.setFullYear(expireDate.getFullYear()+1);
//Sets the expiration date of the cookie to 1 month later.
  expireDate.setMonth(expireDate.getMonth()+1);

//The following if conditions checks to make sure the arguments are the correct type
  if (typeof(obj) == "object")  {
    if (typeof(objName) == "string")  {
//cookieString will be the string inputed into the cookie. Everything will be added onto this.
      var cookieString = ""; 
//firstTime is to avoid a ^ in nameOfCookie=^...
      var firstTime = 1;
      cookieString += objName + "=";
      for (var j in obj)  {
        if (firstTime == 1)  {
          cookieString += j + "^" + obj[j];
          firstTime = 0;
        }
        else   {
          cookieString += "|" + j + "^" + obj[j];
        }
//--------DEBUG
//if (j =="prpList") {
//alert("WriteCookie Obj=["+objName+"]; ID name="+j+"; Value="+obj[j].replace(/\~/g," ")+"-END-");
//}
//-------- END DEBUG
      }
      document.cookie = cookieString + ";expires="+expireDate.toGMTString();

}
    else  {
//Remove comment to ensure the name of the cookie is a string. THIS WILL DISPLAY!
//alert("WriteCookie Error: The name of the cookie must be a string");
    }
  }
  else  {
//Remove comment to ensure an object for the cookie data was passed. THIS WILL DISPLAY!
//alert("WriteCookie Error: You did not pass an object to this function. You must pass an object");
  }
}

function cutCookie(objName)  {
/* 
!!!!!!!!!!!!
DO NOT USE THESE NAMES FOR objName:
/^consq.(one or more)/
/^table.(one or more)/
!!!!!!!!!!!!

PURPOSE: this function delete an object from cookie database
USAGE: cutCookie(nameOfCookie)
       Cookie name can not contain following special characters
       "|", "^", ";", or "=" 
The data type of "nameOfCookie" is a string.
*/
//Sets the expiration date of the cookie to 1 year later.
  var expireDate = new Date();
  expireDate.setFullYear(expireDate.getFullYear()-2); // let it expire rightaway
    if (typeof(objName) == "string")  {
//cookieString will be the string inputed into the cookie. Everything will be added onto this.
      var cookieString = ""; 
//firstTime is to avoid a ^ in nameOfCookie=^...
      var firstTime = 1;
      cookieString += objName + "=" + "whatsoever";
      document.cookie = cookieString + ";expires="+expireDate.toGMTString();

}
    else  {
//Remove comment to ensure the name of the cookie is a string. THIS WILL DISPLAY!
//alert("The name of the cookie must be a string");
    }
}

