function popup(openCallback, closeCallback)
{
	this.counter = popup.counter++;
	popup['popup'+this.counter] = this;

	this.openWindow = null;
	this.openCallback = openCallback || null;
	this.closeCallback = closeCallback || null;

	this.blockerPollTimeout = 0;
	this.closePollTimeout = 0;
	this.doPollForWindowClose = false;

	this.chromeBlocked = false;
	this.type = 'normalpopup';
}

function normalWindow(openCallback, closeCallback, keepReferences)
{
	this.parentConstructor(openCallback, closeCallback);
	this.type = keepReferences ? 'fullreference' : 'normalwindow';
}

// Chain constructors. Javascript kent het fenomeen 'class' niet, maar
// heeft wel constructorfuncties met een prototype object. Dit prototype
// object kan worden gezien als de classdefinitie. Classes extenden doe
// je door constructors in serie te hangen en te zorgen dat het prototype
// van de subclass een instantie is van de hoofdclass.
normalWindow.prototype = new popup();
normalWindow.prototype.constructor = normalWindow;
normalWindow.prototype.parentConstructor = popup;
// De class 'normalWindow' is nu zo ongeveer een kopie van de class 'popup';


popup.counter = 1;
popup.width = 1000;
popup.height = 800;
popup.pos = 'center';
popup.loaderDoc = '/js/popup.html';
popup.isIE = navigator.userAgent.indexOf("MSIE") >= 0;
popup.isChrome = !popup.isIE && navigator.userAgent.indexOf("Chrome") >= 0;
popup.isSafari = !popup.isIE && !popup.isChrome && navigator.userAgent.indexOf("Safari") >= 0;

// LET OP: 'name' mag geen rare tekens en ook geen spaties bevatten. De meeste browsers
// accepteren wel spaties, maar IE niet.
popup.prototype.open = function popup_open(url, name, width, height, dialog, pos)
{
	this.doPollForWindowClose = false;

	if (this.blockerPollTimeout)
	{
		clearTimeout(this.blockerPollTimeout);
		this.blockerPollTimeout = 0;
	}
	if (this.closePollTimeout)
	{
		clearTimeout(this.closePollTimeout);
		this.closePollTimeout = 0;
	}

	url = url || '';
	name = name || '';
	width = width || popup.width;
	height = height || popup.height;
	this.type = dialog ? 'dialog' : this.type;

	var popupSettings = '';
	switch (this.type)
	{
		case 'normalwindow':
			break;

		case 'fullreference':
			url = popup.loaderDoc+'?var=popup'+this.counter+'&url='+encodeURIComponent(url);
			break;

		case 'normalpopup':
		case 'dialog':
			url = popup.loaderDoc+'?var=popup'+this.counter+'&url='+encodeURIComponent(url);

			pos = (typeof pos == 'string') ? pos : popup.pos;
			switch (pos)
			{
				case 'random':
					var x = Math.floor(Math.random() * (screen.width - width));
					var y = Math.floor(Math.random() * (screen.height - height));
					break;
				case 'center':
					var x = Math.floor((screen.width - width) / 2);
					var y = Math.floor((screen.height - height) / 2);
					break;
				default:
					var x = 20;
					var y = 20;
					break;
			}

			popupSettings = 'width='+width+',height='+height+',top='+y+',left='+x+',location=no,directories=no,status=no,menubar=no,toolbar=no';
			popupSettings += this.type == 'dialog' ? ',scrollbars=no,resizable=no' : ',scrollbars=yes,resizable=yes';
			break;
	}

	if (name && !name.match(/^[a-zA-Z_][a-zA-Z0-9_]*$/))
	{
		var oldName = name;
		if (typeof removeAccents !== 'undefined')
			name = removeAccents(name);
		name = name.replace(/[^_a-zA-Z0-9]/g, '_');
		if (!name.match(/^[a-zA-Z_]/))
			name = '_'+name;
		if (typeof window.onerror === 'function')
			window.onerror('Popup: window name opschonen ('+oldName+'): '+name, window.location.protocol+'\/\/'+window.location.hostname+'\/js\/popup.js', 113);
	}
	try { this.openWindow = window.open(url, name, popupSettings); }
	catch(err)
	{
		if (typeof window.onerror === 'function')
			window.onerror('Popup: '+(err && err.message ? err.message : err), window.location.protocol+'\/\/'+window.location.hostname+'\/js\/popup.js', 119);
		alert('Er is geprobeerd om een popupvenster te openen, maar dat is mislukt. De oorzaak is onduidelijk. Het meest waarschijnlijk is een popup blocker.');
		return false;
	}

	if (!popup.isChrome)
	{
		if (!this.checkForBlocker())
		{
			this.doPollForWindowClose = true;
			return false;
		}

		this.doPollForWindowClose = true;
		var popupObject = this;
		this.closePollTimeout = setTimeout(function() { return popupObject.checkForWindowClose(); }, 500);
		this.blockerPollTimeout = setTimeout(function() { return popupObject.checkForBlocker(); }, 10);
	}

	return true;
};

popup.prototype.checkForBlocker = function popup_checkForBlocker()
{
	var popupObject = this;

	if (!this.openWindow || typeof this.openWindow.closed == 'undefined' || this.openWindow.closed || this.chromeBlocked)
	{
		if (this.closePollTimeout && this.doPollForWindowClose)
			clearTimeout(this.closePollTimeout);

		if (this.closePollTimeout || !this.doPollForWindowClose)
		{
			if (typeof window.onerror === 'function')
				window.onerror('Popup geblockt', window.location.protocol+'\/\/'+window.location.hostname+'\/js\/popup.js', 153);
			alert('Er is een popup blocker actief. Het popupvenster kan niet geopend worden!');
		}

		// De meeste browsers geven een gele balk of zo waarmee een geblockte popup alsnog
		// geopend kan worden, maar Safari doet dat niet. Voor Safari hoeven we dat dus ook niet
		// te blijven controleren. Google Chrome heeft een totaal andere aanpak in het tegenhouden
		// van popups. Die hoeft ook niet gepolld te worden. Chrome meldt zichzelf zodra de popup
		// alsnog geopend wordt.
		if (!popup.isSafari && !popup.isChrome)
			this.blockerPollTimeout = setTimeout(function() { return popupObject.checkForBlocker(); }, 500);

		this.closePollTimeout = 0;
		return false;
	}

	if (!this.closePollTimeout && this.doPollForWindowClose)
	{
		// Dan is-ie alsnog geopend en kunnen we een soort van doorstart maken
		this.closePollTimeout = setTimeout(function() { return popupObject.checkForWindowClose(); }, 500);
	}

	if (this.doPollForWindowClose)
	{
		this.openWindow.focus();

		if (this.openCallback)
		{
			if (popup.isIE || typeof window.onerror !== 'function')
			{
				// Voor IE geen try/catch, omdat-ie dan geen regelnummers meer geeft in de foutmelding
				var func = function popup_openCallbackFunction1() { return popupObject.openCallback(popupObject.openWindow); };
			}
			else
			{
				func = function popup_openCallbackFunction2()
				{
					try { var ok = popupObject.openCallback(popupObject.openWindow); }
					catch(ex) { ok = false; window.onerror(ex); }
					return ok;
				};
			}

			setTimeout(func, 10);
		}
	}

	return true;
};

popup.prototype.checkForWindowClose = function popup_checkForWindowClose()
{
	var popupObject = this;

	if (this.openWindow && !this.openWindow.closed)
	{
		// Dan is de popup nog open
		this.closePollTimeout = setTimeout(function() { return popupObject.checkForWindowClose(); }, 500);
		return true;
	}

	if (this.closeCallback)
	{
		var popupObject = this;

		if (popup.isIE || typeof window.onerror !== 'function')
		{
			// Voor IE geen try/catch, omdat-ie dan geen regelnummers meer geeft in de foutmelding
			var func = function popup_closeCallbackFunction1() { return popupObject.closeCallback(); return true; };
		}
		else
		{
			func = function popupCloseCallbackFunction2()
			{
				try { var ok = popupObject.closeCallback(); }
				catch(ex) { ok = false; window.onerror(ex); }
				return ok;
			};
		}

		setTimeout(func, 10);
	}

	return true;
};

popup.prototype.setChromeBlocked = function popup_setChromeBlocked(blocked)
{
	this.chromeBlocked = blocked;
	var isOpen = this.checkForBlocker();
	this.doPollForWindowClose = true;
	return isOpen;
};



/**
 * Gejat van http://www.popuptest.com/ (http://www.popuptest.com/goodpopups.html)
 *
 * @param mypage string		URL van popup
 * @param myname string		Titel van popup
 * @param w integer		Width
 * @param h integer		Height
 * @param dialog bool		bool, dialog is zonder toolbar/status/etc
 * @param pos string		random of center, default links boven
 */
function NewWindow(mypage, myname, w, h, dialog, pos)
{
	if(pos == "random") {
		LeftPosition = (screen.width) ? Math.floor(Math.random() * (screen.width - w)) : 100;
		TopPosition = (screen.height) ? Math.floor(Math.random() * ((screen.height - h) - 75)) : 100;
	} else if(pos == "center") {
		LeftPosition = (screen.width) ? (screen.width - w) / 2 : 100;
		TopPosition = (screen.height) ? (screen.height - h) / 2 : 100;
	} else if((pos != "center" && pos != "random") || pos == null) {
		LeftPosition = 20;
		TopPosition = 20
	}

	settings = 'width=' + w + ',height=' + h + ',top=' + TopPosition + ',left=' + LeftPosition;
	if(dialog) {
		settings = settings + ',scrollbars=no,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
	} else {
		settings = settings + ',scrollbars=yes,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=yes';
	}

	var openWindow = window.open(mypage, myname, settings);
	if (typeof openWindow == 'undefined' || openWindow == null)
	{
		if (typeof toerEngineError !== 'undefined')
			toerEngineError('Popup '+myname+' geblockt'+(mypage ? ': '+mypage : ''));
		alert("Er is een popup blocker actief, Toerkoop kan het gevraagde scherm niet openen!");
	}
	return openWindow;
}

