// file: cookie_lib.js
// set and get are routines in the public domain

// get a specific cookie value
function get_cookie(name) {
   var search4 = name + "=";
   var offset = 0;
   var end = 0;
   var value = null;
   
// if there are any cookies
   if (document.cookie.length > 0) {
// find out where the value of this one begins
// set index of beginning value
      offset = document.cookie.indexOf(search4);
      if (offset != -1) {
         offset += search4.length;
// set index of end of cookie value
         end = document.cookie.indexOf(";", offset);
         if (end == -1) {
            end = document.cookie.length;
         }
         value = document.cookie.substring(offset,end);
      }
   }
   return value;
}
 
// store a specific cookie value
function set_cookie(name, value, expires) {
   var expires_expr = "";
   if (expires != null) {
      expires_expr = "; expires=" + expires.toGMTString();
   }
   document.cookie = name + "=" + value + expires_expr + "; path=/";

   return true;
}

// =================================================
// these routines are specific to on-line meet entry
// =================================================

// get magicword cookie
function get_magicword(site) {
   var magicword;
   var mw_name = site + "_MW";

   pin = get_cookie(mw_name);
   return magicword;
}

// remove magicword cookie
function rm_magicword(site) {
   var mw_name = site + "_MW";

   set_cookie(mw_name,"",-1);
   return true;
}

// set magicword cookie
function set_magicword(site, magicword) {
// var today = new Date();
   var a_year_in_milliseconds = 365 * 24 * 60 * 60 * 1000;
   var expiration_date = new Date();
   var mw_name = site + "_MW";
   var msg = "This process will save your site name and your magicword on this computer.\ \n Press OK to confirm.";

// ask for permission
   if (window.confirm(msg)) {
// save site and magicword
      expiration_date.setTime(expiration_date.getTime() + 
         a_year_in_milliseconds);
      set_cookie(mw_name, magicword, expiration_date);
//    window.alert("Your site name (" + site + ") and magicword (" + magicword + ") have been saved.");
   } else {
      window.alert("Your site name and magicword were not saved.");
// otherwise, permission denied
   }
   return true;
}

// get PIN cookie
function get_pin(team) {
   var pin;
   var pin_name = team + "_PIN";

   pin = get_cookie(pin_name);
   return pin;
}

// remove PIN cookie
function rm_pin(team) {
   var pin_name = team + "_PIN";

   set_cookie(pin_name,"",-1);
   return true;
}

// set PIN cookie
function set_pin(team, pin) {
// var today = new Date();
   var a_year_in_milliseconds = 365 * 24 * 60 * 60 * 1000;
   var expiration_date = new Date();
   var pin_name = team + "_PIN";
   var msg = "This process will save your team name and your PIN on this computer.\ \n Press OK to confirm.";

// ask for permission
   if (window.confirm(msg)) {
// save team and PIN
      expiration_date.setTime(expiration_date.getTime() + 
         a_year_in_milliseconds);
      set_cookie(pin_name, pin, expiration_date);
//    window.alert("Your team name (" + team + ") and PIN (" + pin + ") have been saved.");
   } else {
      window.alert("Your team name and PIN were not saved.");
// otherwise, permission denied
   }
   return true;
}
// end of cookie library


