//MODAL POPUP
function openPopup(nWidth, nHeight, sURL, sTitle, bWindowed){
	var oWin = '';
	var aPopupCoord = '';
	var bBypass = false;
	
	if(typeof bWindowed != "undefined" && bWindowed){
		bBypass = true;
	}
	
	nWidth = nWidth * nPopupSizeOffset;
	nHeight = nHeight * nPopupSizeOffset;
	
	if(sPopupModal && !bBypass){
		if(!document.getElementById('DialogPopupPackage')){
			displayCover();
		}
		
		if(sTitle){
			document.getElementById('PopupTitle').innerHTML = sTitle;
		}else{
			document.getElementById('PopupTitle').innerHTML = '';
		}
		
		document.getElementById('PopupIFrame').src = sURL;
		document.getElementById('PopupModal').style.width = nWidth + 'px';
		document.getElementById('PopupSizing').style.height = nHeight + 'px';
		document.getElementById('PopupIFrame').style.top = '0px';
		document.getElementById('PopupIFrame').style.width = (nWidth - 23) + 'px'
		document.getElementById('PopupIFrame').style.height = (nHeight - 35) + 'px'
		document.getElementById('DialogPopupPackage').style.display = 'block';
	
		positionPopup();
		document.getElementById('DialogPopupPackage').style.top = document.body.scrollTop;
		
		window.onscroll = function(){ document.getElementById('DialogPopupPackage').style.top = document.body.scrollTop; };
		window.onresize = function(){positionPopup();};
	}else{
		aPopupCoord = centerPopup(nWidth, nHeight);
		oWin = window.open(sURL, 'ICPopup','width=' + nWidth + ',height=' + nHeight + ',left=' + aPopupCoord[0] + ',top=' + aPopupCoord[1] + ',toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,titlebar=0');
		oWin.focus();
	}
}

function displayCover(){
	oContainer = document.createElement('div');
	oContainer.id = 'DialogPopupPackage';
	oContainer.className = "ICDialogCoverContainer";
	oCover = document.createElement('div');
	oCover.className = "ICDialogCover";
	
	oContainer.appendChild(oCover);

	window.document.body.appendChild(oContainer);
	
	oContainer.appendChild(document.getElementById('PopupModal'));
	
	oCover.style.zIndex = 1999;
	document.getElementById('PopupModal').style.zIndex = 2000;
	document.getElementById('PopupModal').style.display = 'block';
	document.getElementById('PopupModal').style.position = 'absolute';
}

function positionPopup(){
	document.getElementById('PopupModal').style.top = (popupPageHeight() - document.getElementById('PopupModal').offsetHeight) / 2;
	document.getElementById('PopupModal').style.left = (popupPageWidth() - document.getElementById('PopupModal').offsetWidth) / 2;
}

function closeModalPopup(){
	if(typeof top.frames["PopupIFrame"].modalCloseEvent != "undefined"){
		top.frames["PopupIFrame"].modalCloseEvent();
	}
	document.getElementById('PopupIFrame').src = '';
	document.getElementById('DialogPopupPackage').style.display = 'none';

	window.onscroll = function() {};
	window.onresize = function() {};
}

function popupPageWidth(){
	return window.innerWidth != null ? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
} 

function popupPageHeight(){
	return  window.innerHeight != null ? window.innerHeight : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;
} 


//OLDER POPUP METHOD
function centerPopup(nWinWidth, nWinHeight){
	var nW = 480, nH = 340;
	var aPopupCoord = new Array(2);
	//centers in the middle of the user's screen
	nW = screen.availWidth;
	nH = screen.availHeight;

	aPopupCoord[0] = (nW-nWinWidth)/2;
	aPopupCoord[1] = (nH-nWinHeight)/2;
	
	return aPopupCoord;
}

function openEmbeddedPopup(oClickEvent, sContentDiv){			
	var nRight = document.body.clientWidth - oClickEvent.clientX;
	var nBottom = document.body.clientHeight - oClickEvent.clientY;
	var nClickOffset = 0;
	
	var oContentDiv = document.getElementById(sContentDiv);
	var oContainerDiv = document.getElementById('PopupContainer');
	
	var nWidth = parseInt(oContentDiv.style.width.substring(0, oContentDiv.style.width.length - 2));
	var nHeight = parseInt(oContentDiv.style.height.substring(0, oContentDiv.style.height.length - 2));
	
	var nWidthOffset = 5;
	var nHeightOffset = -(nHeight/2);
	
	if(oContentDiv.parentNode.nodeName != "BODY"){
		document.getElementsByTagName("body")[0].appendChild(oContentDiv);
	}
	
	if(oContainerDiv.parentNode.nodeName != "BODY"){
		document.getElementsByTagName("body")[0].appendChild(oContainerDiv);
	}
	
	if (nRight < oContentDiv.offsetWidth){
		oContentDiv.style.left = document.body.scrollLeft + oClickEvent.clientX - oContentDiv.offsetWidth + nClickOffset + nWidthOffset;
	}else{
		oContentDiv.style.left = document.body.scrollLeft + oClickEvent.clientX + nClickOffset + nWidthOffset;
	}
	
	if (nBottom < oContentDiv.offsetHeight){
		oContentDiv.style.top = document.body.scrollTop + oClickEvent.clientY - oContentDiv.offsetHeight + nClickOffset + nHeightOffset;
	}else{
		oContentDiv.style.top = document.body.scrollTop + oClickEvent.clientY + nClickOffset + nHeightOffset;
	}
	
	// move box to the left if out of screen bounds on right (currently hardcoded due to calendars width being hardcoded)
	if((document.body.clientWidth - nWidth) <  oContentDiv.style.left.substring(0, oContentDiv.style.left.length - 2)){
		oContentDiv.style.left =  (oContentDiv.style.left.substring(0, oContentDiv.style.left.length - 2) - nWidth);
	}
	
	// move box to the top if out of screen bounds on bottom (currently hardcoded due to calendars height being hardcoded)
	if((document.body.clientHeight - nHeight) <  oContentDiv.style.top.substring(0, oContentDiv.style.top.length - 2)){
		oContentDiv.style.top =  (oContentDiv.style.top.substring(0, oContentDiv.style.top.length - 2) - nHeight);
	}
	
	oContentDiv.style.visibility = "visible";
	//used by div shim
	oContentDiv.style.display = "block";
	oContainerDiv.style.width = oContentDiv.offsetWidth;
	oContainerDiv.style.height = oContentDiv.offsetHeight;
	oContainerDiv.style.top = oContentDiv.style.top;
	oContainerDiv.style.left = oContentDiv.style.left;
	oContainerDiv.style.zIndex = oContentDiv.style.zIndex - 1;
	oContainerDiv.style.display = "block";
	//end div shim
}

function closeEmbeddedPopup(sContentDiv){
	document.getElementById(sContentDiv).style.display = 'none';
	document.getElementById('PopupContainer').style.display = 'none';
}