﻿// RcsWeb Global JavaScript library
// Copyright (c) 2007 RightWeb Consulting Services

// ---------------------------------------------------------------------------------

/* DISPLAY PROCESSING MODAL DIALOG */

function DisplayProcessingModalDialog() {


	// Create the modal dialog window
	createModalDialog();
	
    var dialogMessage = processingMessage;
	displayModalDialog(490,280,dialogMessage); // parameters are for centering, not container width/height    
	
		// give the window focus
	self.focus();
	
	return true;
}

// ---------------------------------------------------------------------------------

/* DISPLAY PROCESSING MODAL DIALOG */

function PerformLogout() {
    DisplayProcessingModalDialog()
    window.location =   rootPath + 
                        '/System/Logout.aspx?OrigUrl=' +
                        urlPathAndQuery
}


// ---------------------------------------------------------------------------------

/* MODAL DIALOG */

var stMask = null;
var stContainer = null;
var stIsShown = false;
var stHideSelects = false;

function getScrollTop() {
	if (self.pageYOffset) {return self.pageYOffset} // non IE browsers
	else if (document.documentElement && document.documentElement.scrollTop) {return document.documentElement.scrollTop} // IE 6 Strict
	else if (document.body) {return document.body.scrollTop} // all other IEs
}

function addEvent(obj,evType,fn) {
	if (obj.addEventListener){obj.addEventListener(evType, fn, false); return true}
	else if (obj.attachEvent){var r = obj.attachEvent('on'+evType, fn); return r}
	else {return false}
}


// Pre-defined list of tags we want to disable/enable tabbing into
// If using Mozilla or Firefox, use Tab-key trap.
if (!document.all) {
	document.onkeypress = function(e) {
	if (stIsShown && e.keyCode == 9)  return false;
	}
}

function createModalDialog() {
	theBody = document.getElementsByTagName('BODY')[0];
	theMask = document.createElement('div');
	theMask.id = 'modalMask';
	theContent = document.createElement('div');
	theContent.id = 'modalContainer';
	theContent.innerHTML = '<div id="modalDialogBody"></div>';
	theBody.appendChild(theMask);
	theBody.appendChild(theContent);

	stMask = document.getElementById('modalMask');
	stContainer = document.getElementById('modalContainer');

	// check to see if this is IE version 6 or lower. hide select boxes if so
	var brsVersion = parseInt(window.navigator.appVersion.charAt(0), 10);
	if (brsVersion <= 6 && window.navigator.userAgent.indexOf('MSIE') > -1) {
		stHideSelects = true;
	}
}

function displayModalDialog(width,height,dialogMessage) {
	stIsShown = true;

	var modalDialogBody = document.getElementById('modalDialogBody');
	modalDialogBody.innerHTML = dialogMessage;

	stMask.style.display = 'block';
	stContainer.style.display = 'block';

	// place window on screen : coordinates
	centerModalDialog(width,height);
	stContainer.style.width = width + 'px';
	stContainer.style.height = height + 'px';
	setMaskSize();

	// hide all SELECT boxes : IE 6 z-index bug
	if (stHideSelects == true) {hideAllSelects()}
}

function centerModalDialog(width,height) {
	if (stIsShown == true) {
		if (width == null || isNaN(width)) {
			width = stContainer.offsetWidth;
		}
		if (height == null) {
			height = stContainer.offsetHeight;
		}

		var theBody = document.getElementsByTagName('BODY')[0];
		var scTop = parseInt(getScrollTop(),10);
		var scLeft = parseInt(theBody.scrollLeft,10);

		setMaskSize();

		var fullHeight = getViewportHeight();
		var fullWidth = getViewportWidth();


		stContainer.style.top = (scTop + ((fullHeight - height) / 2)) + 'px';
		stContainer.style.left = (scLeft + ((fullWidth - width) / 2)) + 'px';
	}
}

addEvent(window,'resize',centerModalDialog);
addEvent(window,'scroll',centerModalDialog);

// set modal dialog mask size
function setMaskSize() {
	var theBody = document.getElementsByTagName('BODY')[0];

	var fullHeight = getViewportHeight();
	var fullWidth = getViewportWidth();

	// determine what's bigger, scrollHeight or fullHeight / width
	if (fullHeight > theBody.scrollHeight) {popHeight = fullHeight}
	else {popHeight = theBody.scrollHeight}

	if (fullWidth > theBody.scrollWidth) {popWidth = fullWidth}
	else {popWidth = theBody.scrollWidth}

	stMask.style.height = popHeight + 'px';
	if (!window.attachEvent){stMask.style.height = (popHeight+16) + 'px';} // correct for scroll bars (FF)
	stMask.style.width = popWidth + 'px';
	if (!window.attachEvent){stMask.style.width = (popWidth-16) + 'px';} // correct for scroll bars (FF)
}

function hideModalDialog() {
	stIsShown = false;
	var theBody = document.getElementsByTagName('BODY')[0];
	theBody.style.overflow = '';
	if (stMask == null) {return}
	stMask.style.display = 'none';
	stContainer.style.display = 'none';

	// display all SELECT boxes : IE 6 z-index bug
	if (stHideSelects == true) {showAllSelects()}
}

/* hide all SELECT boxes : IE 6 z-index bug */
function hideAllSelects() {
	var targets = document.getElementsByTagName('SELECT');
	for(var i=0; i<targets.length; i++){
		targets[i].style.visibility='hidden';
	}
}

/* display all SELECT boxes : IE 6 z-index bug */
function showAllSelects() {
	var targets = document.getElementsByTagName('SELECT');
	for(var i=0; i<targets.length; i++){
		targets[i].style.visibility='visible';
	}
}

// ---------------------------------------------------------------------------------

// returns height of inner window

function getViewportHeight() {
	// standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	if (window.innerHeight!=window.undefined) return window.innerHeight;
	// IE6 in standards compliant mode
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
	// IE older versions
	if (document.body) return document.body.clientHeight;

	return window.undefined;
}

// ---------------------------------------------------------------------------------

// returns width of inner window

function getViewportWidth() {
	// standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	if (window.innerWidth!=window.undefined) return window.innerWidth;
	// IE6 in standards compliant mode
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth;
	// older versions of IE
	if (document.body) return document.body.clientWidth;

	return window.undefined;
}


function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}