//	POPUP WINDOW CODE v1.1
//	Used for displaying DHTML only popups instead of using buggy modal windows.

//	By Seth Banks (webmaster at subimage dot com) http://www.subimage.com/
//	Contributions by Eric Angel (tab index code) and Scott (hiding/showing selects for IE users)
//	Up to date code can be found at http://www.subimage.com/dhtml/subModal
//	This code is free for you to use anywhere, just keep this comment block.

//	Updated to Warehousematch.com standard by Mike Henrichs (24-2-2006)

// Popup code
var objPopupMask = null;
var objPopupContainer = null;
var objMoveableContainer = null;
var objPopupFrame = null;

var blnPopupIsShown = false;
var blnHideSelects = false;
var blnMouseOver = false;
var blnAllowMove = true;

var intPopupWidth = null;
var intPopupHeight = null;
var intPopupTop = null;
var intPopupLeft = null;
var intCounter = 0;
var intPreviousX = null;
var intPreviousY = null;

var blnNotDocumentAll = false;

var arrTabIndexes = new Array();
// Pre-defined list of tags we want to disable/enable tabbing into
var arrTabbableTags = new Array('A','BUTTON','TEXTAREA','INPUT','IFRAME');	

// If using Mozilla or Firefox, use Tab-key trap.
if (!document.all) {
	document.onkeypress = keyDownHandler;
	// Following statements are used for moving the popup-window
	document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);
	blnNotDocumentAll = true;
}

function initPopUp() {
	objPopupMask = document.getElementById('popupMask');
	objPopupContainer = document.getElementById('popupContainer');
	objPopupFrame = document.getElementById('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 intVersion = parseInt(window.navigator.appVersion.charAt(0), 10);
	if (intVersion <= 6 && window.navigator.userAgent.indexOf('MSIE') > -1)
		blnHideSelects = true;
}
function showPopWin(varURL, varWidth, varHeight, varTitle, varTop, varLeft, varAllowMove) {
    var intTitleBarHeight = parseInt(document.getElementById('popupTitleBar').offsetHeight, 10);
	blnPopupIsShown = true;
	intPopupWidth = varWidth;
	intPopupHeight = varHeight;
	blnAllowMove = varAllowMove;
	
	if ((intPopupHeight + intTitleBarHeight) > getViewportHeight()) {
	    intPopupHeight = getViewportHeight() - (intTitleBarHeight + 40);
	    varTop = 0;
	}
	
	if (objPopupMask == null || objPopupContainer == null)
	    return 0;
	
	disableTabIndexes();
	objPopupMask.style.display = 'block';
	objPopupContainer.style.display = 'block';
	
	setPopupMask();
	centerPopWin();
	if ((varTop != null && varTop != '') || (varLeft != null && varLeft != ''))
		setPositionPopWin((getScrollTop() + varTop), varLeft);
	
	setHeightPopWin();
	objPopupContainer.style.width = varWidth + '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
	objPopupFrame.style.width = parseInt(document.getElementById('popupTitleBar').offsetWidth, 10) + 'px';
	
	// set the varURL
	objPopupFrame.src = varURL;
	
	if (blnHideSelects == true)
		hideSelectBoxes();
	
	if (varTitle != null && varTitle != '')
		setPopTitle(varTitle);
	else
		setTimeout('setDefaultPopTitle();', 20);
}
function increaseHeightPopWin(varIncrease) {
	intPopupHeight += varIncrease;
	setHeightPopWin();
}
function decreaseHeightPopWin(varDecrease) {
	intPopupHeight -= varDecrease;
	setHeightPopWin();
}
function setHeightPopWin() {
	var intTitleBarHeight = parseInt(document.getElementById('popupTitleBar').offsetHeight, 10);
	objPopupContainer.style.height = (intPopupHeight + intTitleBarHeight) + 'px';
	objPopupFrame.style.height = (intPopupHeight) + 'px';
}
function resizePopWin(varWidth, varHeight) {
    var intTitleBarHeight = parseInt(document.getElementById('popupTitleBar').offsetHeight, 10);
    if (varWidth == null) varWidth = intPopupWidth;
    if (varHeight == null) varHeight = intPopupHeight;
        
    intPopupWidth = varWidth;
    intPopupHeight = varHeight;
    
    objPopupContainer.style.height = (intPopupHeight + intTitleBarHeight) + 'px';
    objPopupContainer.style.width = (intPopupWidth) + 'px';	    	
	objPopupFrame.style.height = (intPopupHeight) + 'px';
	objPopupFrame.style.width = (intPopupWidth) + 'px';
}

function setPopupMask() {
	if (blnPopupIsShown == true) {
		var intScreenTop = getScrollTop();
		var intScreenLeft = getScrollLeft();
		var intFullHeight = getViewportHeight() + intScreenTop;
		var intFullWidth = getViewportWidth() + intScreenLeft;
		
		objPopupMask.style.height = (getViewportHeight() + 200) + 'px';
		objPopupMask.style.width = getViewportWidth() + 'px';
		objPopupMask.style.top = (intScreenTop - 100) + 'px';
		objPopupMask.style.left = intScreenLeft + 'px';
	}
}
function centerPopWin() {
	if (blnPopupIsShown == true) {
		var intTitleBarHeight = parseInt(document.getElementById('popupTitleBar').offsetHeight, 10);
		var intFullHeight = getViewportHeight();
		var intFullWidth = getViewportWidth();
		var intScreenTop = getScrollTop();
		var intScreenLeft = getScrollLeft();        
		
		intPopupTop = (intScreenTop + ((intFullHeight - (intPopupHeight + intTitleBarHeight)) / 2)) + 'px';
		intPopupLeft =  (intScreenLeft + ((intFullWidth - intPopupWidth) / 2)) + 'px';
		
		objPopupContainer.style.top = intPopupTop;
	    objPopupContainer.style.left = intPopupLeft;
	    objPopupMask.style.height = (getViewportHeight() + 200) + 'px';
		objPopupMask.style.width = getViewportWidth() + 'px';
	    objPopupMask.style.top = (intScreenTop - 100) + 'px';
		objPopupMask.style.left = intScreenLeft + 'px';
	}
}
function setPositionPopWin(varTop, varLeft) {
	if (varTop != null && varTop != '') intPopupTop = varTop;	
	if (varLeft != null && varLeft != '') intPopupLeft = varLeft;
	
	positionPopWin();
}
function positionPopWin() {
	objPopupContainer.style.top = intPopupTop;
	objPopupContainer.style.left = intPopupLeft;
}
function hidePopWin() {
	blnPopupIsShown = false;
	restoreTabIndexes();
	
	if (objPopupMask == null)
		return;

	objPopupMask.style.display = 'none';
	objPopupContainer.style.display = 'none';
	
	objPopupFrame.src = '/v2_includes/dialogs/submodal/loading.html';
	if (blnHideSelects == true)
		displaySelectBoxes();
}

//	Sets the popup title based on the title of the html document it contains.
//	Uses a timeout to keep checking until the title is valid.
function setPopTitle(varTitle) {
	document.getElementById('popupTitle').innerHTML = varTitle;
}
function setDefaultPopTitle() {
	if (window.frames['popupFrame'].document.title == null) {
		setTimeout('setDefaultPopTitle();', 5);
	} else {
		var strDefaultTitel = window.frames['popupFrame'].document.title;
		if (strDefaultTitel == null || strDefaultTitel == '') 
			strDefaultTitel = 'Warehousematch.com';
			
		setTimeout('setPopTitle("' + strDefaultTitel + '");', 200);
	}
}

// By pressing down on the mouse button the movement of the objMoveableContainer starts and the
// location of the fisrt click is recorded
function mouseDown(e) {
	if (blnMouseOver) {
		objMoveableContainer = document.getElementById('popupContainer');	
		if (blnNotDocumentAll) {
			intPreviousX = e.layerX;
			intPreviousY = e.layerY;
			return false;
		}
		else {
			objMoveableContainer = objMoveableContainer.style;
			intPreviousX = event.offsetX;
			intPreviousY = event.offsetY;
		} 
	}
}
// By moving the mouse (while holding down the mousebutton) the location of the popupContainer is 
// updated to match the mouse movement
function mouseMove(e) {
	if (objMoveableContainer && blnAllowMove) {
		if (blnNotDocumentAll) {
			objMoveableContainer.style.top = e.pageY - intPreviousY;
			objMoveableContainer.style.left = e.pageX - intPreviousX;
		}
		else {
			objMoveableContainer.pixelLeft = event.clientX - intPreviousX + document.body.scrollLeft;
			objMoveableContainer.pixelTop = event.clientY - intPreviousY + document.body.scrollTop;
			return false;
		}
	}
}
// By releasing the mouse button the objMoveableContainer is set to null and the movement of the container stops!
function mouseUp() {
	objMoveableContainer = null;
}
// Tab key trap. iff popup is shown and key was [TAB], suppress it.
// @argument e - event - keyboard event that caused this function to be called.
function keyDownHandler(e) {
	if (blnPopupIsShown) {
		if (e.keyCode == 9)	// TAB
			return false;
		else if (e.keyCode == 27)	// ESC
			hidePopWin();
	}
}
// For IE.  Go through predefined tags and disable tabbing into them.
function disableTabIndexes() {
	if (document.all) {
		var i = 0;
		for (var j = 0; j < arrTabbableTags.length; j++) {
			var tagElements = document.getElementsByTagName(arrTabbableTags[j]);
			for (var k = 0 ; k < tagElements.length; k++) {
				arrTabIndexes[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 < arrTabbableTags.length; j++) {
			var tagElements = document.getElementsByTagName(arrTabbableTags[j]);
			for (var k = 0 ; k < tagElements.length; k++) {
				tagElements[k].tabIndex = arrTabIndexes[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
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';
			}
		}
	}
}
//	X-browser event handler attachment and detachment
//	@argument objTarget - the object to attach event to
//	@argument varType - name of the event - DONT ADD 'on', pass only 'mouseover', etc
//	@argument varFunction - function to call
function addEvent(objTarget, varType, varFunction){
	var varResult = false;
	if (objTarget.addEventListener){
		objTarget.addEventListener(varType, varFunction, true);
		varResult = true;
	} 
	else if (objTarget.attachEvent){
		varResult = objTarget.attachEvent('on' + varType, varFunction);
	} 
	return varResult;
}
function removeEvent(objTarget, varType, varFunction, useCapture){
	var varResult = false
	if (objTarget.removeEventListener){
		objTarget.removeEventListener(varType, varFunction, useCapture);
		varResult = true;
	} 
	else if (objTarget.detachEvent) {
		varResult = objTarget.detachEvent('on' + varType, varFunction);
	} 
	return varResult;
}


//	Code below taken from - http://www.evolt.org/article/document_body_doctype_switching_and_more/17/30655/
//	Modified 4/22/04 to work with Opera/Moz (by webmaster at subimage dot com)
//	Gets the full width/height because it's different for most browsers.
//	Updated 24-2-2006 Mike Henrichs to work within Warehousematch.com
function getViewportHeight() {
	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; 
}
function getViewportWidth() {
	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; 
}
function getScrollTop() {
    if (document.all) return (document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
    else return window.pageYOffset;
}
function getScrollLeft() {
    if (document.all) return (document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft;
    else return window.pageXOffset;
}
