function CookieJar(pPath) {
	this.path = (typeof(pPath) != "undefined") ? "path=" + pPath + "; " : "path=/; ";
	return this;
}

CookieJar.prototype.addCookie = function(pName, pValue, pDaysActive) {
	var nameValue = pName + "=" + pValue + "; ";

	if (typeof(pDaysActive) != "undefined") {
		var date = new Date();
		var msPerDay = 1000 * 60 * 60 * 24;
		date.setTime(date.getTime() + (pDaysActive * msPerDay));
		var expires = "expires=" + date.toGMTString() + "; ";
	} else {
		expires = "";
	}
	
	var cookieContent = nameValue;
	cookieContent += expires;
	cookieContent += this.path;
	
	window.document.cookie = cookieContent;
}

CookieJar.prototype.getCookie = function(pName) {
	var name = pName + "=";
	var cookieArr = window.document.cookie.split(';');
	for (var i = 0; i < cookieArr.length; i++) {
		var c = cookieArr[i];
		while (c.charAt(0)==' ') {
			c = c.substring(1,c.length);
		}
		if (c.indexOf(name) == 0) {
			return c.substring(name.length,c.length);
		}
	}
	return null;
}

CookieJar.prototype.removeCookie = function(pName) {
		this.addCookie(pName,"",-1);
}


function Popup(pSrc, pName, pWidth, pHeight, pLeft, pTop, pChrome) {
	if (typeof(pSrc) != "undefined") {
		this.src = pSrc;
		this.name = (typeof(pName) != "undefined")? pName : "Untitled";
		this.width = (typeof(pWidth) != "undefined")? pWidth : 100;
		this.height = (typeof(pHeight) != "undefined")? pHeight : 100;
		this.left = (typeof(pLeft) != "undefined")? pLeft : 20;
		this.top = (typeof(pTop) != "undefined")? pTop : 20;
		if (typeof(pChrome) != "undefined") {
			this.chrome = (pChrome != false)? "yes" : "no";
		} else {
			this.chrome = "no";
		}
		return this;
	}
	return null;	
}

Popup.prototype.getPopupDetails = function() {
		var popupDetails = "width=" + this.width + ",";
		popupDetails += "height=" + this.height + ",";
		popupDetails += "directories=" + this.chrome + ",";
		popupDetails += "location=" + this.chrome + ",";
		popupDetails += "menubar=" + this.chrome + ",";
		popupDetails += "scrollbars=" + this.chrome + ",";
		popupDetails += "status=" + this.chrome + ",";
		popupDetails += "toolbar=" + this.chrome + ",";
		popupDetails += "resizable=true,";
		popupDetails += "left=" + this.left + ",";
		popupDetails += "top=" + this.top + ",";
		popupDetails += "screenX=" + this.left + ",";
		popupDetails += "screenY=" + this.top;
		
		return popupDetails;
}


function MeteredPopup(pName, pDaysActive, pMaxPopups, pPopup) {
	if (typeof(pName) != "undefined") {
		var mpck;
		mpck = new CookieJar();
		
		this.cookie = mpck;
		this.popup = (typeof(pPopup) != "undefined")? pPopup : null;
		this.name = pName;
		this.daysActive = pDaysActive;
		this.suid = this.cookie.getCookie("SUID");
		this.maxPopups = pMaxPopups;
		this.popupCount = this.setPopupCount();
			
		if ((this.suid != null) && (this.popup != null)) {
			this.pop();
			return this;
		}
	}
	return null;
}

MeteredPopup.prototype.getValue = function(pName) {
	var name = pName + "=";
	var valueArr = unescape(this.cookie.getCookie(this.name)).split(';');
	for (var i = 0; i < valueArr.length; i++) {
		var v = valueArr[i];
		while (v.charAt(0)==' ') {
			v = v.substring(1,v.length);
		}
		if (v.indexOf(name) == 0) {
			return v.substring(name.length,v.length);
		}
	}
	return null;
}

MeteredPopup.prototype.setPopupCount = function() {
	return (this.getValue("popupCount") != null)? this.getValue("popupCount") : 0;
}

MeteredPopup.prototype.pop = function() {
	if ((this.popupCount < this.maxPopups) && (this.suid != this.getValue("lastSUID"))) {
		if (typeof(mpWindow) != "undefined" && !mpWindow.closed) {
			mpWindow.close();
		}
		
		mpWindow = window.open(this.popup.src,this.popup.name,this.popup.getPopupDetails())
	
		this.popupCount++;
		this.writeCookie();
	}
}

MeteredPopup.prototype.toCookieValue = function() {
	var cookieValue = "lastSUID=" + this.suid + ";";
	cookieValue += "maxPopups=" + this.maxPopups + ";";
	cookieValue += "popupCount=" + this.popupCount + ";";
	return escape(cookieValue);
}

MeteredPopup.prototype.writeCookie = function() {
	this.cookie.addCookie(this.name,this.toCookieValue(),this.daysActive);
}

MeteredPopup.prototype.end = function() {
	this.popupCount = this.maxPopups;
	this.writeCookie();
}

MeteredPopup.prototype.toString = function() {
	var content = "Name: " + this.name + "\n";
	content += "Duration: " + this.daysActive + " days\n";
	content += "Max popups: " + this.maxPopups + "\n";
	content += "Popup count: " + this.popupCount + "\n";
	content += "SUID: " + this.suid + "\n";
	
	if ((this.name == null) || (this.suid == null)) {
		content += "\nCOOKIE NOT WRITTEN!\n\nOne of the required attributes is null \nthe popup will NOT be displayed!";
	} 
	return content;
}

