//
// Picker.js - Pick a random message from a list 
//
// Version 1.0 by David Methvin, Windows Magazine
//
// Tested with JavaScript on Netscape 3.0+ and IE 3.02+
//
// For info/examples see http://www.winmag.com/web/howto/picker/
//
// Note: Cookies don't always work with pages fetched from your
// local hard disk (i.e., not from a web server), especially with
// Internet Explorer. This is not a bug in Picker.

function GetCookie(name) {
  var nmatch = name + "=";	// cookie name to match
  var cstart = 0;		// cookie name search start point
  while ( cstart < document.cookie.length ) {
    var cvalue = cstart + nmatch.length;
    if ( document.cookie.substring(cstart, cvalue) == nmatch ) {
      var cvend = document.cookie.indexOf(";", cvalue);
      if ( cvend < 0 ) cvend = document.cookie.length;
      //alert("[GetCookie] Found "+nmatch+document.cookie.substring(cvalue, cvend));
      return unescape(document.cookie.substring(cvalue, cvend));
    }
    cstart = document.cookie.indexOf(" ", cstart) + 1;
    if ( cstart == 0 ) break; 
  }
  //alert("[GetCookie] No cookie for "+name);
  return "";
}

function SetCookie (name, value) { // expires, path are optional
  var argv = SetCookie.arguments;
  var argc = SetCookie.arguments.length;
  var exp = (argc > 2)? argv[2] : "";
  var edate = new Date();
  if ( value.length == 0 ) {
    edate.setTime(edate.getTime()-24*60*60*1000);
    exp = edate.toGMTString();
    value = "X";
  }
  var cv = name + "=" + escape(value) + 
    (exp.length? ("; expires=" + exp) : "") +
    ((argc > 3 && argv[3] != "")? ("; path=" + argv[3]) : "");
  document.cookie = cv;
  //alert("[SetCookie] Set "+cv);
}

// Create a new item for a picker object
function PickerItem(item_text) {
  if ( !this.had_tokens )
    this.tokens += this.token_order.charAt(this.n_items);
  this[this.n_items++] = item_text;
  //alert("[PickerItem] Total tokens="+this.tokens.length+"; item="+item_text);
  if ( this.name != "" && !this.had_tokens )
    SetCookie(this.name, this.tokens, this.expdate, this.locn);
}

// Pick an item from the list, optionally using cookies as memory
function PickOne() {
  if ( this.n_items == 0 ) return "(NO ITEMS!)";
  this.rand = (this.rand % 4652353) * 492113 + 1;
  if ( !this.tokens.length )	// no tokens left, replenish them
    this.tokens = this.token_order.substring(0, this.n_items);
  var pick = Math.floor(this.rand % this.tokens.length);
  var token = this.tokens.charAt(pick);
  this.tokens = this.tokens.substring(0, pick) + this.tokens.substring(pick+1, this.tokens.length);
  //alert("[PickOne] Picked "+this.token_order.indexOf(token)+" ("+token+"); new tokens="+this.tokens);
  if ( this.name != "" )
    SetCookie(this.name, this.tokens, this.expdate, this.locn);
  return this[this.token_order.indexOf(token)];
}

// Create the content picker object
function Picker(pname) {
  this.name    = pname;		// name of the picker
  this.n_items = 0;		// no items at the moment
  this.item    = PickerItem;	// function to define new items
  this.pick    = PickOne;	// function to pick an item
  this.had_tokens = 0;		// started with no tokens
  var now = new Date();		// initialize random seed
  this.rand = Math.abs(now.getTime()/1000);
  this.token_order = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  // use cookies if picker is named
  if ( pname != "" ) {
    this.tokens = GetCookie(pname);
    //alert("[Picker] Starting with "+pname+"="+this.tokens);
    this.had_tokens = this.tokens.length? 1 : 0;
    var exptime = new Date();	// expire cookie two weeks from now
    exptime.setTime(exptime.getTime()+14*24*60*60*1000);
    this.expdate = exptime.toGMTString();
    this.locn = "";
    //if ( this.locn.substring(0,5) == "http:") this.locn = document.location.toString();
  }
}

