



/***************************************************/
/** ----------------- lock page ------------------ */
/** ------------------- config ------------------- */

// delay and interval (for ajax req.) in msec
var delay = 350;
var interval = 1000;


// ids of items, which will not lock page
// overrides onclick event of those items!
var skipIds = new Array("exportCoursesForm:exportcoursesButton");
	
	
// ids of items, which will lock page and unlock page on finished download (ajax)
// overrides onclick event of those items!	
var downloadIds = new Array(
	"angebotList:pdfOfflineExport",
	"angebotList:pdfOnlineExport",
	"projectList:pdfOfflineExport",
	"projectList:pdfOnlineExport",
	"matchform:pdfOnlineExport",
	"projectList:surchergebnisTable:\\d:pdfOnlineExport");


// ids of anchor-items, which will lock page
// overrides onclick event of those items!
var anchorIds = new Array("");

// form, which will be monitored
// can be index or id
//var formId = 2;


// url for download status
var ajaxUrl; // = "/cms/synchroweb/downloadStatus";


// message text
var headingText = "Hinweis"
var msgText = "Bitte haben Sie einen Moment Geduld.";
var downloadText = "Bitte haben Sie einen Moment Geduld,<br />der Download startet in wenigen Augenblicken.";


// close icon	
var closeIconImg = "/cms/export/system/modules/com.synchronity.gfaw.qualiservice.web/resources/img/icon_close.gif";
var closeIconText = "Schließen";
var closeIconStyleClass = "close"


// some style classes
var popUpContainerStyleClass = "contextHelp";
var contentContainerStyleClass = "loadingAction";
var iframeBugStyleClass = "ie6Bug";
var greyOutStyleClass = "greyout";


// background color for select-items in ie6
var selectItemHoverColor = "#E6E6E6";

/** ------------------ /config ------------------- */
/***************************************************/


var greyOut, statusContainer, selectItemCover, selectItems;
var skipHit = false;

function initStatusPopup() {
	// lock form on submit
	var formId = document.forms.length-1;
	var formular;
	if(formId != NaN)
		formular = document.forms[formId];
	else
		formular = document.getElementById(formId);	
	if(formular)
		formular.onsubmit = triggerPageLock;
	
	// lock form on link-click
	for(var i in anchorIds)
		anchorIds[i].onclick = triggerPageLock;
	
	// download items
	for(var i in downloadIds) {
		var downloadId = downloadIds[i];
		var item = document.getElementById(downloadId);
		if (item) {
			item.onclick = triggerDownloadPageLock;
		}
		else {
			// check input elements with regex
			var inputElements = document.getElementsByTagName("input");
			for (var i=0; i<inputElements.length; i++) {
				var inputElement = inputElements[i];
				if (inputElement.id.match(downloadId)) {
					inputElement.onclick = triggerDownloadPageLock;
				}
			};
		}
	}

	// skipping items
	for(var i in skipIds) {
		var item = document.getElementById(skipIds[i]);
		if(item) 
			item.onclick = skipPageLock;
	}
	
	// identify select elements for ie6
	if(ie < 7)
		idSelectElements();
}


function lockPage(key) {			
	// close helpPopUps
	for(var href in popUps) 
		popUps[href].close();
	
	greyOut = document.createElement("div");
	greyOut.className = greyOutStyleClass;
	document.body.appendChild(greyOut);	
	
	statusContainer = document.createElement("div");
	statusContainer.id = "statusContainer";
	statusContainer.className = popUpContainerStyleClass;
	statusContainer.style.visibility = "hidden";
	document.body.appendChild(statusContainer);	
	
	// ie6 fix: create & add selectItemCover	
	if(ie < 7) {
		selectItemCover = document.createElement("iframe");
		selectItemCover.className = iframeBugStyleClass;
		statusContainer.appendChild(selectItemCover);
	}

	// create & add helpContentContainer
	var loadingAction = document.createElement("div");
	loadingAction.className = contentContainerStyleClass;
	loadingAction.innerHTML = 
			"<h1>" +
			headingText +
			"</h1>" +
			"<p>" +
			(!key ? msgText : downloadText) +
			"</p>";
	statusContainer.appendChild(loadingAction);					

	// create & add closeButton
	var closeButton = document.createElement('a');
	closeButton.className = closeIconStyleClass;
	closeButton.setAttribute("href", "javascript:unlockPage();");
	closeButton.setAttribute("title", closeIconText);	
	var closeIcon = document.createElement("img");
	closeIcon.setAttribute("alt", closeIconText);
	closeIcon.setAttribute("src", closeIconImg);
	closeButton.appendChild(closeIcon);
	statusContainer.appendChild(closeButton);

	// move to the right position 	
	var pos = centerPos(statusContainer);
	statusContainer.style.left = pos.x + "px"
	statusContainer.style.top = pos.y + "px"	
	statusContainer.style.visibility = "";
	
	// ie6 fix
	if(ie < 7) {
		// mask select elements
		for(i = 0; i < selectItems.length; i++)
			selectItems[i].style.backgroundColor = selectItemHoverColor;
			
		selectItemCover.style.height = statusContainer.offsetHeight + "px";
			
		// make greyOut scrollable
		window.onscroll = function() {
			if(greyOut) {
				greyOut.style.top = document.documentElement.scrollTop;		
				greyOut.style.left = document.documentElement.scrollLeft;
			}
		};
	}	
}

function checkDownloadStatus() {
	if(!document.getElementById("statusContainer"))
		return;
		
	var httpRequest;
	if(window.XMLHttpRequest) { // !ie
		httpRequest = new XMLHttpRequest();
		if(httpRequest.overrideMimeType)
			httpRequest.overrideMimeType("text/plain");
	} else if(window.ActiveXObject) { // ie
		try {
			httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				//error(e);
			}
		}
	}

	if(httpRequest) {
		try {
			httpRequest.onreadystatechange = httpRequest.onreadystatechange = function() {
				//debug("readyState " + httpRequest.readyState);	
				if(httpRequest.readyState == 4) //	0 (uninitialized) 1 (loading) 2 (loaded) 3 (interactive) 4 (complete)
					if(httpRequest.status == 200) {	
						//debug(httpRequest.responseText);
						if(httpRequest.responseText == "true")
							setTimeout("checkDownloadStatus()", interval);
						else
							unlockPage();
							
						if(safari)
							clearTimeout(requestTimer);	
					}
			};
			
			var path = document.location.href;
			path = path.substr(0, path.indexOf("synchroweb"));
			ajaxUrl = path + "downloadStatus";
			
			httpRequest.open('GET', ajaxUrl + "?time=" + new Date().getTime(), true);	
			httpRequest.send(null);
			
			if(safari)
				requestTimer =  setTimeout(function() {
					httpRequest.abort();
					setTimeout("checkDownloadStatus()", interval);
				}, delay);
		} catch(e) {
			 //error(e);
		}
	} else {
		//error("no Request");
	}
}


function triggerPageLock() {
	this.blur();
	
	if(skipHit == false)
		setTimeout("lockPage()", delay);
		
	//return false;
}


function skipPageLock() {
	this.blur();		
	skipHit = true;
}


function triggerDownloadPageLock() {
	this.blur();		
	skipHit = true;

	setTimeout("lockPage('download')", delay);
	setTimeout("checkDownloadStatus()", delay);
}


function unlockPage() {
	try {
		// unmask select elements for ie6
		if(ie < 7)
			for(i = 0; i < selectItems.length; i++)
				selectItems[i].style.backgroundColor = "";
				
		document.body.removeChild(statusContainer);			
		document.body.removeChild(greyOut);	
	} catch(e) {};
	
	skipHit = false;
}


function centerPos(element) {
	var width;
	var height;
	if(ie) {
		width = document.documentElement.offsetWidth;
		height = document.documentElement.offsetHeight;
	} else {
		width = window.innerWidth;
		height = window.innerHeight;
	}

	var position = new Object();
		position.x = Math.round((width - parseInt(element.offsetWidth)) / 2) + document.documentElement.scrollLeft;
		position.y = Math.round((height - parseInt(element.offsetHeight)) / 2) + document.documentElement.scrollTop;
	return position;
}


function idSelectElements() {	
	selectItems = new Array();
	var select = document.getElementsByTagName("select");
	for(i = 0; i < select.length; i ++) {
		selectItems[i] = select[i];
	}
}
