//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup 
function loadPopup(idToShow)
{  
	//loads popup only if it is disabled  
	if(popupStatus==0)
	{  
		jQuery("#response").html(""); 
		jQuery("#popUpFormBackground,#page").css({  
		"opacity": "0.3",
		"filter": "alpha(opacity=30)"
		});  		
		jQuery("#popUpFormBackground").fadeIn("slow");  
		jQuery(idToShow).fadeIn("slow");	
		
		popupStatus = 1;  
	}  
}  


//disabling popup 
function disablePopup(idToDisable)
{	
	//check for dynamic iframe and remove if exists
	jQuery("#dynamicIFrame").remove();
	jQuery("#popUpFormBackground").fadeOut("slow");  
	jQuery(idToDisable).fadeOut("slow");  
	jQuery("#page").css({"opacity": "1.0","filter": "alpha(opacity=100)"});
	popupStatus = 0;	  
}  


//center popup
function centerPopup(idToCenter)
{  
	var winSize = getWindowSize();
	var scrollAmt = getScrollXY();
		
	//request data for centering  
	var windowWidth = screen.width;  
	var windowHeight = screen.height;  
	var popupHeight = jQuery(idToCenter).height();  
	var popupWidth = jQuery(idToCenter).width();  
	//centering  
	jQuery(idToCenter).css({  
	"position": "absolute",  
	"top": (winSize[1]/2)+(scrollAmt[1]-(popupHeight/2)),  
	"left": (winSize[0]/2)+(scrollAmt[0]-(popupWidth/2))  
	});  
	//only need force for IE6  
	  
	jQuery("#popUpFormBackground").css({  
	"height": windowHeight  
	}); 		  
} 


function centerPopupRelative(idToCenter,idToCenterFrom,topOffset,leftOffset)
{
	var fromObjectPosition = jQuery(idToCenterFrom).offset();	
	var posLeft = fromObjectPosition.left;
	var posTop = fromObjectPosition.top;
			
	//make sure an integer passed in
	if (topOffset == parseFloat(topOffset))
	{
		posTop += topOffset;
	}
	
	if (leftOffset == parseFloat(leftOffset))
	{
		posLeft += leftOffset;
	}
	
	//centering  
	jQuery(idToCenter).css({  
	"position": "absolute",  
	"top": posTop,  
	"left": posLeft
	});
}


function centerPopupX(idToCenter,topOffset)
{  
	var winSize = getWindowSize();
	var scrollAmt = getScrollXY();
		
	//request data for centering  
	var windowWidth = screen.width;  
	var windowHeight = screen.height;  
	var popupHeight = jQuery(idToCenter).height();  
	var popupWidth = jQuery(idToCenter).width();
	
	var topCentered = (winSize[1]/2)+(scrollAmt[1]);
	
	//make sure an integer passed in
	if (topOffset == parseInt(topOffset))
	{
		if (topCentered - topOffset > 0)
			topCentered = topCentered - topOffset;
		else
			topCentered = 100;
	}	
		
	//centering  
	jQuery(idToCenter).css({  
	"position": "absolute",  
	"top": topCentered,  
	"left": (winSize[0]/2)+(scrollAmt[0]-(popupWidth/2))  
	});  
	//only need force for IE6  
	  
	jQuery("#popUpFormBackground").css({  
	"height": windowHeight  
	}); 		  
}

function centerPopupXY(idToCenter,topOffset,leftOffset)
{  
	var winSize = getWindowSize();
	var scrollAmt = getScrollXY();
		
	//request data for centering  
	var windowWidth = screen.width;  
	var windowHeight = screen.height;  
	var popupHeight = jQuery(idToCenter).height();  
	var popupWidth = jQuery(idToCenter).width();
	
	var topCentered = (winSize[1]/2)+(scrollAmt[1]);
	var leftCentered = (winSize[0]/2)+(scrollAmt[0]-(popupWidth/2));
	
	//make sure an integer passed in
	if (topOffset == parseInt(topOffset))
	{
		if (topCentered - topOffset > 0)
			topCentered = topCentered - topOffset;
		else
			topCentered = 100;
		
		if (topCentered < 100)
			topCentered = 100;
	}	
	
	if (leftOffset == parseInt(leftOffset))
	{
		if (leftCentered - leftOffset > 0)
			leftCentered = leftCentered - leftOffset;
		else
			leftCentered = 100;
		
		if (leftCentered < 100)
			leftCentered = 100;
	}
		
	//centering  
	jQuery(idToCenter).css({  
	"position": "absolute",  
	"top": topCentered,  
	"left": leftCentered  
	});  
	//only need force for IE6  
	  
	jQuery("#popUpFormBackground").css({  
	"height": windowHeight  
	}); 		  
}


//update div layer with an iframe and passed in html content
function createFrame(popUpId,contentContainerId,title,htmlContent,width,height,scroll)
{
	var htmlContent = Base64.decode(htmlContent);	
	var mainContainer = document.getElementById(popUpId);
	var mainTitle = document.getElementById("popUpGenericTitle");	
	var contentContainer = document.getElementById(contentContainerId);	
	
	if (mainContainer)
		mainContainer.style.width = width + "px";
	
	if (mainTitle)
		mainTitle.innerHTML = title;
		
	if (contentContainer)
	{ 		
		var myFrame = document.createElement("iframe");
		myFrame.id = "dynamicIFrame";
		myFrame.width = width;
		myFrame.height = height;
		myFrame.frameBorder = 0;
		myFrame.scrolling = scroll;
		
		contentContainer.style.width = width + "px";
		contentContainer.style.height = height + "px";
		contentContainer.appendChild(myFrame);
		
		var doc = myFrame.document;
		if(myFrame.contentDocument)
		    // Firefox, Opera
			doc = myFrame.contentDocument;
		else if(myFrame.contentWindow)
			// Internet Explorer
			doc = myFrame.contentWindow.document;			 
					
		doc.open();
		doc.write(htmlContent);
		doc.close();			
		
		hideSelect();
		centerPopup('#'+popUpId);
		loadPopup('#'+popUpId);			
	}
}
