/* ======================================================================= 
PURPOSE: Cookie functions for Troux.com website    
======================================================================= */

function fncSetCookie (name, value, exp, path, domain, secure) {
 /*
    INPUT
      name (string) - name of the cookie
      value (string) - value of the cookie
      exp (string) - default is "never"
                     if "never" it sets exp to "Thu, 7 Dec 2113 01:00:00 UTC"
                     if "exp" it sets exp to "Fri, 13 Apr 1970 01:00:00 UTC"
                     if int (ie "5", "20", "60000") it translates to int hours from now
                     if valid GMT date then it uses that as exp
                     else it defaults to 840 hours (5 weeks)
      path (string)
      domain (string)
      secure (binary) - true = secure, false = non-secure
 */
  if(exp == "") {
    exp = "never";
  }
  if (typeof(exp) == 'string') {
    if (exp == 'never') { 
      var strExp = "Thu, 7 Dec 2113 01:00:00 UTC";
    }
    else if (exp == 'exp') { 
      var strExp = "Fri, 13 Apr 1970 01:00:00 UTC";
    }
    else {
      if (Date.parse(exp)) {
       var strExp = exp;
      }
      else {
        exp = exp*1;
        if (isNaN(exp)) {
         var strExp = (new Date((new Date()).getTime() + 840*3600000)).toGMTString();
        }
        else {
         var strExp = (new Date((new Date()).getTime() + exp*3600000)).toGMTString();
        }
      }
    }
  }
  document.cookie = name + '=' + escape(value) + ((strExp)?(';expires=' + strExp):'') + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:'') + ((secure && (secure == true))?'; secure':'');
}

function fncReadCookie(name) {
  var firstChar, lastChar;
  var theBigCookie = document.cookie;
  firstChar = theBigCookie.indexOf(name);
  if(firstChar != -1) {
    firstChar += name.length + 1;
    lastChar = theBigCookie.indexOf(';', firstChar);
    if(lastChar == -1) lastChar = theBigCookie.length;
    return unescape(theBigCookie.substring(firstChar, lastChar));
  }
  else {
    return false;
  }
}

function fncKillCookie(name, path, domain) {
 /*
    REQUIRED MODULES
      "fncReadCookie" function
      "fncSetCookie" function
 */
  var value = fncReadCookie(name);
  if(value) {
    fncSetCookie(name, value, 'exp')    
  }
}

