﻿function Popup(id, title, width, height) {
	var body = document.getElementsByTagName('body')[0];

	this._id = id;
	this._width = width;
	this._height = height;

	var popup = document.getElementById(id);
	if (popup == null) { popup = document.createElement('div'); body.appendChild(popup); }
	this._popup = popup;
	popup._popup = this;

	var mask = document.getElementById(id + '_mask');
	if (mask == null) { mask = document.createElement('div'); body.appendChild(mask); }
	this._mask = mask;

	this._modality = true;

	popup.setAttribute('id', id);
	popup.style.position = 'absolute';
	popup.style.overflow = 'hidden';
	popup.style.width = width + 'px';
	popup.style.height = height + 'px';
	popup.style.backgroundColor = '#ffffff';

	mask.setAttribute('id', id + '_mask');
	mask.style.position = 'absolute';
	mask.style.left = '0px';
	mask.style.top = '0px';
	mask.style.width = '100%';
	mask.style.backgroundColor = '#000000';

	if (mask.style.opacity != null) {
		mask.style.opacity = 0.75;
	} else {
		mask.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(Opacity=50)';
	}

	for (var i = popup.childNodes.length - 1; i >= 0; i--) {
		popup.removeChild(popup.childNodes[i]);
	}

	var _this = this;

	var titleBar = document.createElement('div');
	titleBar.className = 'popupTitle';
	titleBar.setAttribute('class', 'popupTitle');
	popup.appendChild(titleBar);

	var titleText = document.createElement('div');
	titleText.style.whiteSpace = 'nowrap';
	titleText.style.overflow = 'visible';
	titleText.innerHTML = title;

	var close = document.createElement('div');
	close.className = 'popupClose';
	close.setAttribute('class', 'popupClose');
	close.style.cssFloat = 'right';
	close.onclick = function () { _this.hide(); }

	titleBar.appendChild(close);
	titleBar.appendChild(document.createTextNode(title));

	var content = document.createElement('div');
	content.style.clear = 'both';
	content.style.overflow = 'auto';
	popup.appendChild(content);

	this._title = titleBar;
	this._content = content;
}

Popup.prototype.show = function (url) {
	var body = document.getElementsByTagName('body')[0];
	var intHeight = (document.documentElement != null && document.documentElement.clientHeight != 0) ? document.documentElement.clientHeight : body.clientHeight;
	var intTopOffset = (document.documentElement.scrollTop != null) ? document.documentElement.scrollTop : body.scrollTop;

	this._popup.style.left = Math.floor((body.offsetWidth / 2) - (this._width / 2)) + 'px';
	this._popup.style.top = Math.floor((intHeight / 2) - (this._height / 2)) + intTopOffset + 'px';
	this._popup.style.visibility = '';
	this._popup.style.zIndex = 100002;

	this._mask.style.left = '0px';
	this._mask.style.top = '0px';
	this._mask.style.height = ((document.documentElement != null) ? document.documentElement.scrollHeight : body.scrollHeight) + 'px';
	this._mask.style.visibility = '';
	this._mask.style.zIndex = 100001;

	this._content.style.height = (this._popup.offsetHeight - this._title.offsetHeight) + 'px';

	if (url != null) {
		var fQueryStringPresent = false;
		for (var i = 0; i < url.length; i++) {
			if (url.charAt(i) == '?') { fQueryStringPresent = true; break; }
		}

		if (fQueryStringPresent && url.indexOf('popupID') == -1) {
			url += '&popupID=' + this._id;
		} else {
			url += '?popupID=' + this._id;
		}

		var content = this._content;
		content.innerHTML = '';

		var r = null;

		try {
			r = new XMLHttpRequest();
		} catch (ex) {
			r = new ActiveXObject('Microsoft.XMLHTTP');
		}

		r.onreadystatechange = function () {
			if (r.readyState != 4 && r.readyState != 'complete') { return; }
			content.innerHTML = r.responseText;
		}

		r.open('POST', url, true);
		r.send('');
	}
}

Popup.prototype.hide = function () {
	this._popup.style.visibility = 'hidden';
	this._mask.style.visibility = 'hidden';
}

Popup.prototype.setModality = function (modal) {
	this._modality = modal;
}

Popup.prototype.setOpacity = function (opacity) {
	if (this._mask.style.opacity != null) {
		this._mask.style.opacity = opacity;
	} else {
		this._mask.style.filters = 'progid:DXImageTransform.Microsoft.Alpha(Opacity=' + (opacity * 100) + ')';
	}
}

Popup.prototype.post = function (form) {
	var szFormData = '';

	for (var i = 0; i < form.elements.length; i++) {
		var e = form.elements[i];

		var key = escape((e.getAttribute('name') != null) ? e.getAttribute('name') : e.getAttribute('id'));
		var values = new Array();

		switch (e.tagName.toLowerCase()) {
			case 'input':
			case 'textarea':
				var type = e.getAttribute('type');
				if (type == 'checkbox' || type == 'radio') {
					if (e.checked) { values[values.length] = e.value; }
				} else if (type == 'submit') {
					// do nothing
				} else {
					values[values.length] = e.value;
				}

				break;

			case 'select':
				for (var x = 0; x < e.options.length; x++) {
					var o = e.options[x];
					if (o.selected) { values[values.length] = o.value; }
				}
		}

		for (var x = 0; x < values.length; x++) {
			szFormData += ((szFormData == '') ? '' : '&') + escape(key) + '=' + encodeURIComponent(values[x]);
		}
	}

	if (window.submitter != null) {
		szFormData += ((szFormData == '') ? '' : '&') + escape((window.submitter.getAttribute('name') != null) ? window.submitter.getAttribute('name') : window.submitter.getAttribute('id')) + '=' + window.submitter.getAttribute('value');
		window.submitter = null;
	}

	var r = null;
	try {
		r = new XMLHttpRequest();
	} catch (e) {
		r = new ActiveXObject('Microsoft.XMLHTTP');
	}

	if (r == null) {
		var e = new Error();
		e.message = 'Unable to create XmlHttpRequest.';
		e.description = 'The scripting engine was unable to create an instance of an XmlHttpRequest object or Microsoft.XMLHTTP object.';
		throw e;
		return;
	}

	var _this = this;
	r.onreadystatechange = function () {
		if (r.readyState == 4 || r.readyState == 'complete') {
			_this._content.innerHTML = r.responseText;
		}
	}

	r.open('POST', form.action, true);
	r.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	r.send(szFormData);
}

