function getPopupBackgroundObject() {
	return document.getElementById("popupBackground");
}
function getPopupObject() {
	return document.getElementById("popup");
}

function isPopupOpen() {
	var popup = getPopupObject();
	var result = popup != null && popup.style.display != "none";
	return result;
}

function getPopup(popupContainerObjId) {
	var popupBackground = getPopupBackgroundObject();
	if(popupBackground == null) {
		popupBackground = document.createElement("DIV");
		popupBackground.setAttribute("id", "popupBackground");
		popupBackground.onclick = closePopup;
		popupBackground.innerHTML = "<!--[if lte IE 6.5]><div style=\"background-color:black\"> <a href=\"javascript:closePopup()\" style=\"filter:alpha(100);background-color:black; cursor:pointer\"><iframe></iframe></a></div><![endif]-->";
		popup = document.createElement("DIV");
		popup.setAttribute("id", "popup");
		
		var html = '<div id="popupBar"><a href="javascript:closePopup()" title="Close popup">Close</a></div>'
			     + '<div id="popupContent"></div>'
			     + '<div id="popupContentBuffer"></div>';
		
		popup.innerHTML = html;
		document.getElementById("wrap").appendChild(popupBackground);
		
		document.getElementById(popupContainerObjId).appendChild(popup);
	}
	return getPopupObject();
}

function showPopupBackground() {
	getPopupBackgroundObject().style.display="block";
}
function showPopup() {
	showPopupBackground();
	getPopupObject().style.display="block";
}
function hidePopupBackground() {
	hidePopup();
	getPopupBackgroundObject().style.display="none";
}
function hidePopup() {
	getPopupObject().style.display="none";
}

function openPopup(popupContainerObjId, form, width, height, usePopupContentBuffer, callback, callbackParameters) {
	var popup = getPopup(popupContainerObjId);
	var resultDivId;
	showPopupBackground();
	hidePopup();
	
	if(usePopupContentBuffer) {
		resultDivId = "popupContentBuffer";
	} else {
		resultDivId = "popupContent";
		
		if(width != null && height!=null) {
			popup.style.width = width+"px";
			popup.style.height = height+"px";
			center(popup);
			show(popup);
		}
	}
	ajaxFormSubmit(form, resultDivId, callback, callbackParameters);
}

function closePopup() {
	hidePopup();
	hidePopupBackground();
	document.getElementById("popupContent").innerHTML="";
 }

