﻿if (!window.XMLHttpRequest) { 
	window.XMLHttpRequest = function() { 
		var xmlHttp; 
		try	{ 
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP.4.0"); 
			return xmlHttp;
		} 
		catch (ex) {}
		try { 
			xmlHttp = new ActiveXObject("MSXML2.XMLHTTP"); 
			return xmlHttp;
		} 
		catch (ex) {}
		try { 
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
			return xmlHttp;
		} 
		catch (ex) {}
		return null;
	}
}

Viewport = new function()
{
	this.Height = function() {
		if (window.innerHeight!=window.undefined) return window.innerHeight;
		if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
		if (document.body) return document.body.clientHeight; 
		return window.undefined; 
	}
	this.Width = function() {
		if (window.innerWidth!=window.undefined) return window.innerWidth; 
		if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth; 
		if (document.body) return document.body.clientWidth; 
		return window.undefined; 
	}
	this.ShowSelects = function() {
		ShowHideSelects('visible');
	}

	this.HideSelects = function() {
		ShowHideSelects('hidden');
	}
	function ShowHideSelects(visibility) {	
		for(var i = 0; i < document.forms.length; i++) {
			for(var e = 0; e < document.forms[i].length; e++){
				if(document.forms[i].elements[e].tagName == "SELECT") {
					document.forms[i].elements[e].style.visibility = visibility;
				}
			}
		}
	}
}

ModalPopWin = new function() 
{
	var popup = document.getElementById("popDiv");
	var mask = document.getElementById("maskDiv");
		
	this.Show = function(filename) {
		FetchData('/' + filename, this.SetContent);
		popup.style.zIndex = 201;
		popup.style.display = "block";
		mask.style.display = "block";
		mask.style.zIndex = 200;
		var btn = document.getElementById("popWinOk");
		if(btn) btn.focus();
		addEvent(document,"keydown",ModalPopWin.CaptureKey);
		this.Center();
		Viewport.HideSelects();
	}
	this.Hide = function() {
		popup.style.display = "none";
		if(mask.style.display = "block")
		{
			popup.style.zIndex = 1;
			mask.style.display = "none";
			mask.style.zIndex = -1;
		}
		popup.innerHTML = "";
		document.onkeydown = null;
		Viewport.ShowSelects();
	}
	this.Center = function(width, height) {
		if (popup.style.display == "block") 
		{
			if (width == null || isNaN(width)) width = popup.offsetWidth;
			if (height == null)	height = popup.offsetHeight;
			
			var docElem = document.documentElement;
			var scTop = parseInt(docElem.scrollTop,10);
			var scLeft = parseInt(docElem.scrollLeft,10);
			
			mask.style.height = Viewport.Height() + "px";
			mask.style.width = Viewport.Width() + "px";
			mask.style.top = scTop + "px";
			mask.style.left = scLeft + "px";
			popup.style.top = (scTop + ((Viewport.Height() - height) / 2)) + "px";
			popup.style.left =  (scLeft + ((Viewport.Width() - width) / 2)) + "px";
		}
	}
	this.SetContent = function(data) {
		popup.innerHTML = data;
		
	}
	this.CaptureKey = function(event) {
		if(event.keyCode == 27) ModalPopWin.Hide();
		if(event.srcElement.tabIndex == 200 && event.keyCode == 9 && event.shiftKey)
			event.returnValue = false;
		if(event.srcElement.tabIndex == 204 && event.keyCode == 9 && !event.shiftKey)
			event.returnValue = false;
	}
}

function addEvent(obj, eventType, callback) {
	if(obj.addEventListener) {
		obj.addEventListener(eventType, callback, true);
	} else if(obj.attachEvent) {
		obj.attachEvent("on"+eventType, callback);
	}
}

function InitMe() {
		
	addEvent(window, "resize", ModalPopWin.Center);
	window.onscroll = ModalPopWin.Center;
}

function FetchData(obj, sucessFunction)
{
	var xmlReq = new XMLHttpRequest();
	if(obj.timeout) obj.timer = setTimeout(TimedOut, obj.timeout);
	xmlReq.onreadystatechange = doCallback; 
	xmlReq.open("GET",obj,true);
	xmlReq.send("");
	function doCallback() { 
		if (4 == xmlReq.readyState) {
			if(obj.timer) clearTimeout(obj.timer);
			if(xmlReq.status == 200) sucessFunction(xmlReq.responseText);
			xmlReq = null;
		}
	}
	function TimedOut() {
		xmlReq.abort();
		xmlReq.onreadystatechanged = function(){};
		xmlReq = null;
	}
}