/*
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Prueba de popup</title>
<script type="text/javascript" src="/cgi-bin2/js/popup.js"></script>
<script type="text/javascript">
        var popup = new PopUp("popup1");
        popup.targetPage = "/index.shtml";
        popup.windowProperties = new WindowProperties(ancho, alto, izqda, arriba, redimensionable, barras scroll);
        popup.expirationTime = new ExpirationTime(dias, horas, minutos, segundos, milisegundos);
        popup.Show();
</script>
</head>
<body>
</body>
</html>


*/
function PopUp(name)
{
	var _popUpName = name;
	var expirationTime = new ExpirationTime(0, 0, 0, 0, 0);;
	var targetPage = "/" + this._popUpName + ".shtml";
	var windowProperties = new WindowProperties(640, 480, 20, 20, "yes", "yes");

	this.GetCookie = function(name) {  
		var arg = name + "=";  
		var alen = arg.length;  
		var clen = document.cookie.length;  
		var i = 0;  
		while (i < clen)
		{    
			var j = i + alen;    
			if (document.cookie.substring(i, j) == arg)
			{
				return this.getCookieVal (j);    
			}
			i = document.cookie.indexOf(" ", i) + 1;    
			if (i == 0)
			{
				break;
			}
		}  
		return null;
	}
	
	this.SetCookie = function(name, value)
	{  
		var argv = this.SetCookie.arguments;  
		var argc = this.SetCookie.arguments.length;  
		var expires = (argc > 2) ? argv[2] : null;  
		var path = (argc > 3) ? argv[3] : null;  
		var domain = (argc > 4) ? argv[4] : null;  
		var secure = (argc > 5) ? argv[5] : false;  
		document.cookie = name + "=" + escape (value) + 
			((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
			((path == null) ? "" : ("; path=" + path)) +  
			((domain == null) ? "" : ("; domain=" + domain)) +    
			((secure == true) ? "; secure" : "");
	}

	this.DeleteCookie = function(name)
	{  
		var exp = new Date();  
		exp.setTime(exp.getTime() - 1);  
		var cval = this.GetCookie(name);  
		document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
	}
	
	this.getCookieVal = function(offset)
	{
		var endstr = document.cookie.indexOf (";", offset);
		if (endstr == -1)
		{
			endstr = document.cookie.length;
		}
		return unescape(document.cookie.substring(offset, endstr));
	}

	this.Show = function()
	{
		var count = this.GetCookie(_popUpName);
		if (count == null)
		{
			count=1;
			this.SetCookie(_popUpName, count, this.expirationTime.Time());
			count = this.GetCookie(_popUpName);
			if (count==1)
			{
  				window.open(this.targetPage, _popUpName, this.windowProperties.ToString());
			}
		}
		else
		{
			count++;
			this.SetCookie(_popUpName, count, this.expirationTime.Time());
  		}
	}

}

function ExpirationTime(days, hours, minutes, seconds, mseconds)
{
	var _date = new Date();
	var _days = days;
	var _hours = hours;
	var _minutes = minutes;
	var _seconds = seconds;
	var _mseconds = mseconds;

	this.mSeconds = function()
	{
		return (_days * 24 * 3600000) + (_hours * 3600000) + (_minutes * 60000) + (_seconds * 1000) + _mseconds;
	}

	this.Time = function()
	{
		if ( this.mSeconds() == 0 )
		{
			return null;
		}

		_date.setTime(_date.getTime() + this.mSeconds());
		return _date;
	}
}

function WindowProperties(width, height, left, top, resizable, scrollbars)
{
	var _width = width;
	var _height = height;
	var _left = left;
	var _top = top;
	var _resizable = resizable;
	var _scrollbars = scrollbars;
	
	this.ToString = function()
	{
		return "width=" + _width + ", height=" + _height + ", left=" + _left + ",top=" + _top + ", resizable=" + _resizable + ", scrollbars=" + _scrollbars;
	}
}
