/* Functions used in every page for text size/colour switching
 *
 * 16-Aug-2005 [Mike Ford] Extract into separate file 
 */

function leedsMetCookie(name, hours, path, domain) {

  this.$name = name;
  this.$expiry = hours ? new Date((new Date()).getTime() + hours*60*60*1000) : null;
  this.$path = path ? path : "/";
  this.$domain = domain ? domain : ".leedsmet.ac.uk";

  var cookies = document.cookie;
  if (cookies=="") {
	return;
  }

  var wantedCookie = cookies.match("(^|;\\s?)" + name + "=([^;]*)(;|$)");
  if (wantedCookie===null) {
    return;
  }

  // if the cookie exists, decode its sub-cookies

  var settings = wantedCookie[2].split("||");
  for (var i=0, n=settings.length; i<n; i++)
  {
	subCookie = settings[i].split("::");
    this[subCookie[0]] = unescape(subCookie[1]);
  }
}

leedsMetCookie.prototype.save = function() {
  var newCookie = "";
  for (var subCookie in this) {
	if (subCookie.charAt(0)!="$" && typeof this[subCookie]!="function") {
	  newCookie += (newCookie?"||":"") + subCookie + "::" + escape(this[subCookie]);
	}
  }

  var fullCookie = this.$name + "=";
  if (newCookie)
    fullCookie += newCookie + (this.$expiry ? "; expires=" + this.$expiry.toUTCString() : '');
  else
	fullCookie += "; expires=Fri, 02-Jan-1970 00:00:00 GMT";
  fullCookie += (this.$path ? "; path=" + this.$path : '') +
				(this.$domain ? "; domain=" + this.$domain : '');

  document.cookie = fullCookie;
}
