/* Start Header ------------------------------------------------------------

    Kodak Graphic Communications Canada Company
    Burnaby B.C. Canada
    V5G 4M1

    Copyright (C) 2008 Kodak Graphic Communications Canada Company
    
    Reproduction or disclosure of this file or its contents without the
    prior written consent of Kodak Graphic Communications Canada Company
    is prohibited.

 * End Header --------------------------------------------------------------
 */
 
var gHideSelects = false;
var gTabIndexes = new Array();
// Pre-defined list of tags we want to disable/enable tabbing into
var gTabbableTags = new Array("A","BUTTON","TEXTAREA","INPUT","IFRAME");	

function POPF_Popup( id )
{
    this.m_PopupId = id;
    this.m_PopupMask = null;
    this.m_PopupContainer = null;
    this.m_PopFrame = null;
    this.m_ReturnFunc;
    this.m_PopupIsShown = false;
    
    this.m_LoadingPage = '/' + kSiteName + '/Blank.html';

    this.m_HideSelects = false;
    this.m_PopupFastHack = false;
}


POPF_Popup.prototype.initPopUp = function() 
{
	// Add the HTML to the body
	body = document.getElementsByTagName('body')[0];
	popmask = document.createElement('div');
	popmask.id = 'popupMask_' + this.m_PopupId;
	popcont = document.createElement('div');
	popcont.id = 'popupContainer_' + this.m_PopupId;
	popcont.innerHTML = '' +
		'<div class="popupInner" id="popupInner_' + this.m_PopupId + '">' +
			'<iframe src="' + this.m_LoadingPage + '" style="width:100%;height:100%;background-color:transparent;" scrolling="auto" frameborder="0" allowtransparency="true" id="popupFrame_' + this.m_PopupId + '" class="popupFrame" name="popupFrame_' + this.m_PopupId + '" width="100%" height="100%"></iframe>' +
		'</div>';
	body.appendChild(popmask);
	body.appendChild(popcont);
	
	this.m_PopupMask = document.getElementById("popupMask_" + this.m_PopupId);
	this.m_PopupContainer = document.getElementById("popupContainer_" + this.m_PopupId);
	this.m_PopFrame = document.getElementById("popupFrame_" + this.m_PopupId);	
	
	
	this.m_PopupMask.className = "popupMask";
	this.m_PopupContainer.className = "popupContainer"
	this.m_PopFrame.className = "popupFrame"
	
	// check to see if this is IE version 6 or lower. hide select boxes if so
	// maybe they'll fix this in version 7?
	var brsVersion = parseInt(window.navigator.appVersion.charAt(0), 10);
	if (brsVersion <= 6 && window.navigator.userAgent.indexOf("MSIE") > -1) {
		gHideSelects = true;
	}	
}

POPF_Popup.prototype.showPopWin = function( url, width, height, returnFunc )
{
    this.showPopWin( url, width, height, returnFunc, null )
}
    
POPF_Popup.prototype.showPopWin = function( url, width, height, returnFunc, loadingPage )
{
    if ( loadingPage != null ) 
    {
        this.m_LoadingPage = loadingPage;
    }
    
	if (!this.m_PopupMask )
	{
	    this.initPopUp();
	}

	this.m_PopupIsShown = true;
	
	if (!this.m_PopupFastHack ) {    
   	    disableTabIndexes();
   	}

	this.m_PopupMask.style.display = "block";
	this.m_PopupContainer.style.display = "block";
	
	// calculate where to place the window on screen
	this.centerPopWin(width, height);
	
    var titleBarHeight = 0;
	
	this.m_PopupContainer.style.width = width + "px";
	this.m_PopupContainer.style.height = (height+titleBarHeight) + "px";
	// need to set the width of the iframe to the title bar width because of the dropshadow
	// some oddness was occuring and causing the frame to poke outside the border in IE6
    this.m_PopFrame.style.width = width + "px";
	this.m_PopFrame.style.height = height + "px";
	
	// set the url
	this.m_PopFrame.src = url;
	
	this.m_ReturnFunc = returnFunc;
	// for IE
	if (gHideSelects == true && !this.m_PopupFastHack) {
		hideSelectBoxes();
	}
	this.m_PopFrame.focus();
}

var gi = 0;
POPF_Popup.prototype.centerPopWin = function( width, height )
{
	if (this.m_PopupIsShown == true) {
		if (width == null || isNaN(width)) {
			width = this.m_PopupContainer.offsetWidth;
		}
		if (height == null) {
			height = this.m_PopupContainer.offsetHeight;
		}
		
		var fullHeight = getViewportHeight();
		var fullWidth = getViewportWidth();
		
		var theBody = document.documentElement;
		
		var scTop = parseInt(theBody.scrollTop,10);
		var scLeft = parseInt(theBody.scrollLeft,10);
		
		this.m_PopupMask.style.height = fullHeight + "px";
		this.m_PopupMask.style.width = fullWidth + "px";
		this.m_PopupMask.style.top = scTop + "px";
		this.m_PopupMask.style.left = scLeft + "px";
		
		window.status = this.m_PopupMask.style.top + " " + this.m_PopupMask.style.left + " " + gi++;
		
		titleBarHeight = 0;
		
		this.m_PopupContainer.style.top = (scTop + ((fullHeight - (height+titleBarHeight)) / 2)) + "px";
	    this.m_PopupContainer.style.left =  (scLeft + ((fullWidth - width) / 2)) + "px";
	}
}
/**
 * @argument callReturnFunc - bool - determines if we call the return function specified
 * @argument returnVal - anything - return value 
 */
POPF_Popup.prototype.hidePopWin = function(callReturnFunc) {
	this.m_PopupIsShown = false;
	
	if (!this.m_PopupFastHack) {
	    restoreTabIndexes();
	}
	    
	if (this.m_PopupMask == null) {
		return;
	}
	this.m_PopupMask.style.display = "none";
	this.m_PopupContainer.style.display = "none";
	if (callReturnFunc == true && gReturnFunc != null) {
		this.m_ReturnFuncc( window.frames["popupFrame_" + this.m_PopupId ].returnVal );
	}
	this.m_PopFrame.src = '/' + kSiteName + '/Blank.html';
	// display all select boxes
	if (gHideSelects == true && !this.m_PopupFastHack) {
		displaySelectBoxes();
	}
}

// For IE.  Go through predefined tags and disable tabbing into them.
function disableTabIndexes() {
	if (document.all) {
		var i = 0;
		for (var j = 0; j < gTabbableTags.length; j++) {
			var tagElements = document.getElementsByTagName(gTabbableTags[j]);
			for (var k = 0 ; k < tagElements.length; k++) {
				gTabIndexes[i] = tagElements[k].tabIndex;
				tagElements[k].tabIndex="-1";
				i++;
			}
		}
	}
}

// For IE. Restore tab-indexes.
function restoreTabIndexes() {
	if (document.all) {
		var i = 0;
		for (var j = 0; j < gTabbableTags.length; j++) {
			var tagElements = document.getElementsByTagName(gTabbableTags[j]);
			for (var k = 0 ; k < tagElements.length; k++) {
				tagElements[k].tabIndex = gTabIndexes[i];
				tagElements[k].tabEnabled = true;
				i++;
			}
		}
	}
}


/**
* Hides all drop down form select boxes on the screen so they do not appear above the mask layer.
* IE has a problem with wanted select form tags to always be the topmost z-index or layer
*
* Thanks for the code Scott!
*/
function hideSelectBoxes() {
	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="hidden";
			}
		}
	}
}

/**
* Makes all drop down form select boxes on the screen visible so they do not reappear after the dialog is closed.
* IE has a problem with wanted select form tags to always be the topmost z-index or layer
*/
function displaySelectBoxes() {
	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="visible";
			}
		}
	}
}