/////////////////////////////////////////////////////////////////////////////
// Checks if object is defined

function isdef(obj) { return (String(obj) != "undefined") && (String(obj) != "null") && (String(obj) != ""); };
function trim(s) { return s.replace(/^\s*/,'').replace(/\s*$/,'') };
String.prototype.trim = function () { return this.replace(/^\s*/,'').replace(/\s*$/,'') };
////////////////////////////////////////////////////////////////////////////////
// Cookie class

function TCookie() {
	this.domain = ".medborgarskolan.com";
	this.host = location.hostname.toLowerCase();
	if (this.host.indexOf(".medborgarskolan") > 0)
		this.domain = this.host.substring(this.host.indexOf(".medborgarskolan"));

	//  Corrects 2.x Mac date bug
	this._FixDate = function(date) {
		var base = new Date(0);
		var skew = base.getTime();
		if (skew > 0)
			date.setTime(date.getTime() - skew);
	};

	// "Internal" function to return the decoded value of a cookie
	this._Get = function(offset) {
		var endstr = top.document.cookie.indexOf(";", offset);
		if (endstr == -1)
				endstr = top.document.cookie.length;
		return unescape(top.document.cookie.substring(offset, endstr));
	};

	//  Returns the value of the cookie
	this.Get = function(name) {
		var result = "";
		var arg = name + "=";
		var aLen = arg.length;
		var cLen = top.document.cookie.length;
		var i = 0;
		while (i < cLen) {
			var j = i + aLen;
			if (top.document.cookie.substring(i,j) == arg) {
				result = this._Get(j);
				break;
			};
			i = top.document.cookie.indexOf(" ",i) + 1;
			if (i == 0)
				break;
		};
		return result;
	};

	//  Creates/updates a cookie.
	this.Set = function() {
		var name = arguments[0];
		var value = arguments[1];
		var path = arguments[2];
		var domain = arguments[3];
		var expires = arguments[4];
		var secure = arguments[5];
		var result = "";
		if (name)   result += name + "=";
		if (value)  result += value;
		if (domain) result += "; domain=" + domain;
		if (path)   result += "; path=" + path;
		if (expires) {
			this._FixDate(expires);
			result += "; expires=" + expires.toGMTString();
		};
		if (secure)
			result += "; secure=" + secure;
		top.document.cookie = result;
	};

	//  Deletes a cookie
	this.Delete = function() {
		var name = arguments[0];
		var domain = arguments[1] ? arguments[1] : "";
		var path = arguments[2] ? arguments[2] : "";
		var expires = new Date(0);
		if (name)
			if (this.Get(name))
				this.Set(name, "", expires, domain, path, false);
	};
};

var Cookie = new TCookie();



