// UML Cookie Object //
var UMLCookie = {
	UML : 'ultralive',
	DELIM : '.',
	PID_DELIM : '|',
	DEFAULT: ['0', '', '1', '0' ,'0'], // RaceID, Bibs, Nav Group, NA, NA
	get : function()
	{
		var cval = Cookie.get(this.UML);
		if (cval != null)
		{
			var a = cval.split(this.DELIM);
			if (a.length == 5) return a;
		}
		return this.DEFAULT;
	},

	setRaceID : function(id)
	{
		var a = this.get();
		if (a[0] == id) return;
		a[0] = id;
		this.set(a);	
		return;
	},

	getRaceID : function(id)
	{
		var a = this.get();
		return a[0];
	},

	setNavGroup : function(id)
	{
		var a = this.get();
		if (a[2] == id) return;
		a[2] = id;
		this.set(a);	
		return;
	},

	getNavGroup : function(id)
	{
		var a = this.get();
		return (a[2] < 1 ? 1 : a[2]);
	},
	
	setPID : function(aPID)
	{
		var a = this.get();
		a[1] = aPID.join(this.PID_DELIM);
		this.set(a);	
		return;
	},
	
	getPID : function()
	{
		var c = this.get();
		return c[1].split(this.PID_DELIM);
	},
	
	arrayToStr : function(a)
	{
		return a.join(this.DELIM);
	},
	// Set Cookie
	// a is array (size = 4)
	set : function(a)
	{
		if (a.length == 5)
		{
			Cookie.set(this.UML, this.arrayToStr(a), 30);
		}
		else
		{
			Cookie.set(this.UML, this.arrayToStr(this.DEFAULT), 30);
		}
		return;
	}
};

var Cookie = {
  set: function(name, value, daysToExpire) {
    var expire = '';
    if (daysToExpire != undefined) {
      var d = new Date();
      d.setTime(d.getTime() + (86400000 * parseFloat(daysToExpire)));
      expire = '; expires=' + d.toGMTString();
    }
    return (document.cookie = escape(name) + '=' + escape(value || '') + expire + '; path=/');
  },
  get: function(name) {
    var cookie = document.cookie.match(new RegExp('(^|;)\\s*' + escape(name) + '=([^;\\s]*)'));
    return (cookie ? unescape(cookie[2]) : null);
  },
  erase: function(name) {
    var cookie = Cookie.get(name) || true;
    Cookie.set(name, '', -1);
    return cookie;
  },
  accept: function() {
    if (typeof navigator.cookieEnabled == 'boolean') {
      return navigator.cookieEnabled;
    }
    Cookie.set('_test', '1');
    return (Cookie.erase('_test') === '1');
  }
};


