//  cookies.js
//  Written:  2000-02-10 by James Alarie <jalarie@umich.edu>
//    http://spruce.flint.umich.edu/~jalarie/
//
//  from: http://developer.netscape.com/docs/manuals/communicator/jsguide4/cookies.htm#1002170
//
//  Subroutines:
//    SetCookie (name, value, expires, days)
//      name      
//      value     
//      expires   formatted as "Wdy, DD-Mon-YY HH:MM:SS GMT":
//          Wdy is a string representing the full name of the day of the week.
//          DD is an integer representing the day of the month.
//          Mon is a string representing the three-character abbreviation of the month.
//          YY is an integer representing the last two digits of the year.
//          HH, MM, and SS are 2-digit representations of hours, minutes, and seconds, respectively.
//        For example:  Wednesday, 09-Nov-99 23:12:40 GMT
//      days      number of days to keep the cookie.  This computes the "expires" for you.
//    GetCookie (name)
//      name      
//    GetCookieCrumb (instring, seeking)
//      instring
//      seeking

        function SetCookie(name, value, expires, days) {
          var cookies=document.cookie;              // cookies for site
          var ix1;                                  // index
          var cookie_name=name;                     // main cookie name
          var crumb_name='';                        // crumb name
          var result='';                            // value to return
          var p2=value;                             // new value
          var p3=expires;                           // param work area
          var p4=days;
          var mspd=1000 * 60 * 60 * 24;             // milliseconds per day
          if (!p3) {                                // no expiration
            if (!p4) { p4=1; }                      // no days: use 1
            p3=new Date();
            p3.setTime(p3.getTime() + mspd * days);
          }
          ix1=cookie_name.indexOf(';');             // cookie/crumb break
          if (ix1 != -1) {                          // found ';' means seeking crumb
            crumb_name=cookie_name.substring(ix1 + 1); // crumb name
            cookie_name=cookie_name.substring(0, ix1); // main cookie name
          }
          result=GetCookieCrumb(cookies, cookie_name);
          if (crumb_name) {
            if (result) { result=GetCookieCrumb(result, crumb_name); }
            else { rest=''; }
            if (rest) { p2=rest + ';' + crumb_name + '=' + p2; }
            else      { p2=             crumb_name + '=' + p2; }
          }
          p3=new Date(p3);
          document.cookie=cookie_name+'='+escape(p2)+'; expires='+p3.toGMTString();
          return true;
        } // SetCookie

        function GetCookie(name) {
          var cookies=document.cookie;              // cookies for site
          var ix1;                                  // loop index
          var cookie_name=name;                     // main cookie name
          var crumb_name;                           // crumb name
          var result;                               // value to return
          ix1=cookie_name.indexOf(';');             // cookie/crumb break
          if (ix1 != -1) {                          // found ';' means seeking crumb
            crumb_name=cookie_name.substring(ix1 + 1); // crumb name
            cookie_name=cookie_name.substring(0, ix1); // main cookie name
          }
          result=GetCookieCrumb(cookies, cookie_name);
          if (crumb_name && result) {               // seeking crumb and have a cookie
            result=GetCookieCrumb(result, crumb_name); // get the crumb value
          }
          cookie_rest=rest;                         // everything except current crumb
          return result;                            // send back the value
        } // GetCookie

        function GetCookieCrumb(instring, seeking) {
          var c1;                                   // cookies work area
          var c2;                                   // cookie work area
          var c3;                                   // cookie name
          var c4;                                   // cookie value
          var ix1; var ix2; var ix3; var ix4;       // indexes
          var result;                               // value to return
          rest='';                                  // everything except desired item
          c1=instring;
          c1=c1.replace(/\n/g, '');                 // drop new-line characters
          c1=c1.replace(/\r/g, '');                 // drop carriage-return characters
          c1=c1 + ';';                              // force it to find the final cookie
          ix1=0;                                    // from beginning
          if (seeking.substring(0, 1) == '#') {     // seeking an item by index
            if (seeking == '#0' && !instring) {     // want count and nothing exists
              return 0;
            }
            ix4=seeking.substring(1);               // which item wanted
            ix3=0;                                  // none so far
            while (c1.indexOf(';', ix1) != -1) {    // a ';' exists after column ix1
              ix2=c1.indexOf(';', ix1);             // ...column number of above
              cookie=c1.substring(ix1, ix2);
              ix3 += 1;                             // update the count of items
              if (ix3 == ix4) { return cookie; }    // entire name=value
              ix1=ix2 + 1;                          // next column in which to start
            }
            if (ix4 == 0) { return ix3; }           // count of items
            return '';                              // not found
          }
          while (c1.indexOf(';', ix1) != -1) {
            ix2=c1.indexOf(';', ix1);               // next cookie break
            if (ix2 == -1) { ix2=c1.length; }
            cookie=c1.substring(ix1, ix2);          // a single cookie
            c2=cookie;                              // cookie work area
            ix1=ix2 + 1;
            ix3=c2.indexOf('=', 0);                 // name=value break
            if (ix3 == -1) { ix3=c2.length; }
            c3=c2.substring(0, ix3);                // get the name
            c4=c2.substring(ix3 + 1);               // get the value
            c3=c3.replace(/ /g, '');                // drop all spaces
            if (c3 == seeking) {
              result=unescape(c4);
            } else {
              if (rest) { rest=rest + ';' + cookie; }
              else      { rest=cookie; }
            }
          }
          return result;
        } // GetCookieCrumb
