// Popup div - requires Yahoo AJAX Connection
var popUpLayerId = "popUpLayer";
var divPopUpContainer = createEl('div',
    {'class': "popUpLayer", id: popUpLayerId, name: popUpLayerId},
    {},
    '');
var popUpLayerBackgroundId = "popUpLayerBg";
var divPopUpBackground = createEl('div',
    {'class': "popUpLayerBg", id: popUpLayerBackgroundId, name: popUpLayerBackgroundId},
    {},
    '');
var evtSender;
var layerCentered;
var showBackground;
var layerLeft;
var containerWidth;
var containerHeight;
        
YAHOO.util.Event.on(window, 'load', function () {     
    document.body.appendChild(divPopUpContainer);
});
YAHOO.util.Event.on(window, 'load', function () {     
    document.body.appendChild(divPopUpBackground);
});

YAHOO.util.Event.on(popUpLayerBackgroundId, 'click', function () {     
    closePopUpDiv();
});

var handleQuestionSuccess = function(o) {
	var d=document.getElementById(popUpLayerId);
	
	if(d==null){
		document.body.appendChild(divPopUpContainer);
	}
	else{
		document.body.replaceChild(divPopUpContainer,d);
	}
	if(document.getElementById(popUpLayerBackgroundId)==null){
		document.body.appendChild(divPopUpBackground);
	}

    if(o.responseText !== undefined) {

        divPopUpContainer.innerHTML = o.responseText;
        var region = YAHOO.util.Dom.getRegion(popUpLayerId);
        if (layerCentered) { 
            // modified 20080508           
            divPopUpContainer.style.left = parseInt((YAHOO.util.Dom.getViewportWidth() - (region.right - region.left))/2 + YAHOO.util.Dom.getDocumentScrollLeft())+'px';
            divPopUpContainer.style.top = parseInt((YAHOO.util.Dom.getViewportHeight() - (region.bottom - region.top))/2 + YAHOO.util.Dom.getDocumentScrollTop())+'px';
        } else if (layerLeft) {
	        
	        containerWidth = 0;
	        containerHeight = 0;
	        containerWidth = document.getElementById(popUpLayerId).offsetWidth;
	        containerHeight = document.getElementById(popUpLayerId).offsetHeight;

	        divPopUpContainer.style.left = (parseInt(YAHOO.util.Dom.getX(evtSender)) - containerWidth - 50) +'px';
            divPopUpContainer.style.top = (parseInt(YAHOO.util.Dom.getY(evtSender)) - (containerHeight /2)) +'px';
            
        } else {
            divPopUpContainer.style.left = (parseInt(YAHOO.util.Dom.getX(evtSender)) + 127) +'px';
            divPopUpContainer.style.top = YAHOO.util.Dom.getY(evtSender)+'px';
        }
        if (parseInt(divPopUpContainer.style.left) < 0)
            divPopUpContainer.style.left = '0px';
        if (parseInt(divPopUpContainer.style.top) < 0)
            divPopUpContainer.style.top = '0px';

        divPopUpContainer.style.visibility = "visible";
        
        if (showBackground) {
            divPopUpBackground.style.width = YAHOO.util.Dom.getDocumentWidth()+'px';
            divPopUpBackground.style.height = YAHOO.util.Dom.getDocumentHeight()+'px';
            divPopUpBackground.style.visibility = "visible";
        }
    }
}
var handleQuestionFailure = function(o) {
    if(o.responseText !== undefined)
        divPopUpContainer.innerHTML = "Error retrieving data.";
}
var questionCallback = {
  success:handleQuestionSuccess,
  failure:handleQuestionFailure,
  argument: {}
};
function openPopUpDiv(url, sender, centered, showBk) {
    evtSender = sender;
    layerCentered = centered;
    showBackground = showBk;
    layerLeft = false;
        
    var request = YAHOO.util.Connect.asyncRequest('GET', url, questionCallback);
    return false;
}

function openLeftPopUpDiv(url, sender, showBk) {
    evtSender = sender;
    layerCentered = false;
    showBackground = showBk;
    layerLeft = true;
        
    var request = YAHOO.util.Connect.asyncRequest('GET', url, questionCallback);
    return false;
}

function openPopUpDivWithForm(url, sender, centered, showBk, formId) {
    evtSender = sender;
    layerCentered = centered;
    showBackground = showBk;
    
    var request;
    if (formId != null) {
		var theForm = document.forms[formId];
		YAHOO.util.Connect.setForm(theForm);
		request = YAHOO.util.Connect.asyncRequest('POST', url, questionCallback);
	} else {
    	request = YAHOO.util.Connect.asyncRequest('GET', url, questionCallback);
    }
    return false;
}
function closePopUpDiv() {
    divPopUpContainer.innerHTML = "";
    divPopUpContainer.style.visibility = "hidden"; 
    divPopUpBackground.style.visibility = "hidden"; 
}