/* ======================================================================= 
FILE: cookies.js                                           
AUTHOR: John D Brown                                                    
CREATED: 17-Oct-2004 17:53                                              
UPDATED: 17-Oct-2004 17:54 JDB                                          
----------------------------------------------------------------------  
PURPOSE: Cookie functions    
======================================================================= */

function fncSetCookie (name, value, exp, path) {
 /*
    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)
 */
 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' || exp == 'now') { 
     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:'');
}



function fncReadCookie(name) {
 var strCookies = document.cookie;
 
 // find beginning of cookie value in document.cookie
 var prefix = name + "=";
 var begin = strCookies.indexOf("; " + prefix);
 if (begin == -1) {
   begin = strCookies.indexOf(prefix);
   if (begin != 0) return null;
 }
 else begin += 2;
 
 // find end of cookie value
 var end = document.cookie.indexOf(";", begin);
 if (end == -1) end = strCookies.length;
 
 // return cookie value
 return unescape(strCookies.substring(begin + prefix.length, end));
}



function fncKillCookie(name, path) {
 /*
    REQUIRED MODULES
      "fncReadCookie" function
      "fncSetCookie" function
 */
 fncSetCookie (name, null, 'now', path);
}



function fncKillAllCookies(path) {
 var strCookies = document.cookie;
 
 var astrCookies = document.cookie.split(";");
 
 for(var i=0;i<=astrCookies.length-1;i++) {
   var astrCookie = astrCookies[i].split("=");
   var strCookieName = astrCookie[0];
   if (i>0) {
     if (strCookieName.indexOf(" ") == 0) {
       // remove leading space
       strCookieName = strCookieName.substring(1,strCookieName.length)
     }
   }
   fncKillCookie(strCookieName, path);
 }
 alert('All cookies deleted for path ' + path);
}



function fncListCookies(path) {
 var strCookies = document.cookie;
 var astrCookies = document.cookie.split(";");
 var strOutput = "";
 if (strCookies != null && strCookies.length > 0) {
   for(var i=0;i<=astrCookies.length-1;i++) {
     var astrCookie = astrCookies[i].split("=");
     var strCookieName = astrCookie[0];
     var strCookieValue = astrCookie[1];
     if (i>0) {
       if (strCookieName.indexOf(" ") == 0) {
         // remove leading space
         strCookieName = strCookieName.substring(1,strCookieName.length)
       }
     }
     strOutput = strOutput + "\n   " + strCookieName + ": " + strCookieValue;
   }
 }
 if (strOutput.length < 5) {
   strOutput = " { no cookies for path " + path + " } ";
 }
 else {
   strOutput = "cookies for path " + path + strOutput;
 }
 alert(strOutput);
}

