// Sam's Cookie handling functions
// These Javascript Cookie Functions adapted from http://tech.irt.org/articles/js025/index.htm

var Cookie = new Object();
Cookie.get = function(name) {
    var start = document.cookie.indexOf(name+"=");
    var len = start+name.length+1;
    if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
    if (start == -1) return null;
    var end = document.cookie.indexOf(";",len);
    if (end == -1) end = document.cookie.length;
    return unescape(document.cookie.substring(len,end));
}

Cookie.set = function(name,value,expires,path,domain,secure) {
    document.cookie = name + "=" +escape(value) +
        ( (expires) ? ";expires=" + expires.toGMTString() : "") +
        ( (path) ? ";path=" + path : "") + 
        ( (domain) ? ";domain=" + domain : "") +
        ( (secure) ? ";secure" : "");
}
Cookie.del = function(name,path,domain) {
    if (this.get(name)) document.cookie = name + "=" +
        ( (path) ? ";path=" + path : "") +
        ( (domain) ? ";domain=" + domain : "") +
        ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

Cookie.setMaster = function(){
	if(!this.get('MasterCookie')) this.set('MasterCookie', 'MasterCookie');
}
Cookie.Intelligent = function(name, value){
	// Determine expiration
	var e = new Date(), t = new Date();
	e.setTime(t.getTime() + (8 * 7 * 86400000));

	if(this.get('MasterCookie')){
		var c = this.get(name);
		if((!c) || (c != value)){
			set(name, value, e);
			var c = this.get(name);
			if((!c) || (c != value)) this.del('MasterCookie');
		}
	}
}
