<!-- cookie_listings js -->
<!-- Updated: 01/15/2010 ls -->

var fieldSeparator = "#"

function processListing(name, fields, days_to_live) {
	recentCookie = new cookieObject(name, days_to_live, "/");
	listing = new listingObject(fields);
	recentCookie.write(listing);
}

function listingObject(fields) {
	this.get = listingFieldGet
	this.put = listingFieldPut
	this.namepos = listingNamePos
	this.fieldnames = fieldnames
	this.fields = fields
	for (var i = 0; i < fieldnames.length; i++ ) {
		this.put(fieldnames[i], fields[i]);
	}
}

function cookieObject(name, expires, accessPath) {
this.name = name
this.listingSeparator = "XX"
this.maxListings = 25
this.found = false
this.expires = expires
this.accessPath = accessPath
this.rawValue = ""
this.read = cookieRead
this.write = cookieWrite
this.remove = cookieDelete
this.read()
}

function getListings(recentCookie) {
	var listings = new Array();
	if (recentCookie.found) {
//		alert("cookie found: " + recentCookie.rawValue);
		if (recentCookie.rawValue != null) { // unpack into listings
		  var raw_listings = recentCookie.rawValue.split(recentCookie.listingSeparator,recentCookie.maxListings);
		  for (var i = 0; i < raw_listings.length; i++) {
			  if (raw_listings[i].length > 0) {
				  listings.push(getListing(raw_listings[i]));
			  }
		  }
	  }
	}
	return listings;
}

function getListing(rawListing) {
  var fields = new Array();
  var raw_fields = rawListing.split(fieldSeparator);
  for (var j = 0; j < raw_fields.length; j++) {
	  fields.push(raw_fields[j]);
  }
  return new listingObject(fields);
}

function listingFieldGet(fieldname) {
var i = this.namepos(fieldname)
if (i >=0) {
  return this.fields[i]
} else {
  return "BadFieldName!"
}
}

function listingFieldPut (fieldname, fieldval) {
var i = this.namepos(fieldname)
if (i >=0) {
  this.fields[i] = fieldval
  return true
} else {
  return false
}
}

function listingNamePos(fieldname) {
var i
for (i = 0; i < this.fieldnames.length; i++) {
  if (fieldname == this.fieldnames[i]) {
    return i
  }
}
return -1
}


function cookieWrite(listing) {
  var cookietext = this.name + "=";

	if (listing != null) {
		//add the listing to the front of the list
		for (i= 0; i < listing.fields.length; i++) {
		  cookietext += listing.fields[i] + fieldSeparator;
		}
		cookietext += this.listingSeparator

		//remove listing from the current list if already there, using the first field as the primary key
		if (this.rawValue != null) { // unpack into listings

		  var sl = this.rawValue.length
		  var startidx = 0
		  var endidx = 0
		  var i = 0

		  do
		  {
		   endidx = this.rawValue.indexOf(this.listingSeparator, startidx)
		   if (endidx !=-1) {
	//		   alert("listing in cookie: " + this.rawValue.substring(startidx, endidx));
			   old_listing = getListing(this.rawValue.substring(startidx, endidx));

			   //compare primary key; if this listing already exists, don't include it again
			   if (listing.fields[0] != old_listing.fields[0]) {
				 i++
				 cookietext += this.rawValue.substring(startidx, endidx) + this.listingSeparator;
	//			 alert("cookie text: " + cookietext);
			   }
			 startidx = endidx + this.listingSeparator.length}
		  }
		  while (endidx !=-1 & startidx != this.rawValue.length & i < this.maxListings);
		} // end of unpack into listings if block
	}

    if (this.expires != null) {
      if (typeof(this.expires) == "number") { // Expiry period in days specified
        var today=new Date()
        var expiredate = new Date()
        expiredate.setTime(today.getTime() + 1000*60*60*24*this.expires)
        cookietext += "; expires=" + expiredate.toGMTString()
      } else { // assume it's a date object
        cookietext +=  "; expires=" + this.expires.toGMTString()
      } // end of typeof(this.expires) if
    } // end of this.expires != null if

// add path, if specified
   if (this.accessPath != null) {
   cookietext += "; PATH="+this.accessPath }

   	var domain = document.domain;
   	if (domain.indexOf("www.") == 0) {
   		domain = domain.substring(domain.indexOf(".") + 1);
   	}
   	cookietext += "; domain="+domain;

// write cookie
//   alert("writing "+cookietext)
   document.cookie = cookietext
}

function cookieRead() {
  var search = this.name + "="
  var CookieString = document.cookie
  this.rawValue = null
  this.found = false
  if (CookieString.length > 0) {
    offset = CookieString.indexOf(search)
    if (offset != -1) {
      offset += search.length
      end = CookieString.indexOf(";", offset)
      if (end == -1) {  // cookie is last item in the string, so no terminator
       end = CookieString.length }
      this.rawValue = CookieString.substring(offset, end)
      this.found = true
    }
  }
}

function cookieDelete() {
  this.expires = -10
  this.write()
  return this.read()
}


