
/*
 * This function opens a modal popup dialog with a Close button.
 * The contents in the popup should be in a DIV having class "popupcontents".
 * The ID of that DIV should be passed in to this function.
 */
function showInfoPopup(input_url) {
  showOverlay();
  sendHttpRequest(input_url, function(responseText) {
    document.getElementById('popdialogcontents').innerHTML = responseText;
    display(document.getElementById('popdialog'));
  });
}

function closeInfoPopup() {
  hide(document.getElementById('popdialog'));
  document.getElementById('popdialogcontents').innerHTML = '';
  scrollToPrevious();
  hideOverlay();
}

var popups = new Array();
var windowScrollX;
var windowScrollY;

/**
    This function is the main function for displaying modal popup dialogs.  The
    function expects at least ONE argument:  The URL to which to send the Ajax
    request.  After the URL, the arguments can be name/value pairs that should
    be added to the query string.  For example, this:
 
    showPopup("helloWorld.do", "firstName", "Bob");
 
    Sends the Ajax request to helloWorld.do, and appends "firstName=Bob" to 
    the query string.
 
    This function also takes care of showing the modal overlay.
*/
function showPopup() {
    showOverlay();

    var url = arguments[0];
    var ajaxRequest = createAjaxRequest(url);
    addPopupPostRequest(ajaxRequest);
    
    for (var i = 1; i < arguments.length - 1; i += 2) {
        var name = arguments[i];
        var value = arguments[i+1];
        ajaxRequest.addNameValuePair(name,value);
    }
    
    //ajaxRequest.setEchoDebugInfo();
    ajaxRequest.sendRequest();
}

/**
    Assumes everything on the query string has been added.
*/
function showPopupWithAjaxRequest(request) {
    showOverlay();
    addPopupPostRequest(request);
    request.sendRequest();
}

function addPopupPostRequest(request) {
    if(request.getPostRequest() != null) {
        //preserve the current postRequest while adding showPopupPostRequest
        var pr = request.getPostRequest();
        request.setPostRequest(function(req) { pr(); showPopupPostRequest(); } );
    }
    else {
        //simply set it
        request.setPostRequest(showPopupPostRequest);
    }
}

/**
    Handles the post request of an Ajax popup dialog.  Any existing popups 
     are hidden and the new one is displayed.
*/
function showPopupPostRequest() {
    //hide the current popup
    hide(getActivePopup());
    
    //reset the array of popups
    popups = findByClassName("popactionnew", "div", null);
    
    //display the active popup
    display(getActivePopup());

    //now that something is showing, hide the loading indicator
    hideLoadingIndicator();
}

/**
    Dismisses all of the existing popups on the page.
*/
function dismissAllPopups() {
    var documentBody = document.getElementsByTagName("body")[0];
    while(popups.length > 0) {
        documentBody.removeChild(popups.pop());
    }
    
    hideOverlay();
}

/**
    Dismisses the active popup.
*/
function dismissActivePopup() {
    var pop = popups.pop(); 
    hide(pop);
    document.getElementsByTagName("body")[0].removeChild(pop);
    
    //display the now-current popup (the one that was under the one we just dismissed, if one exists)
    display(getActivePopup());
    hideOverlay();
}

/**
    Dimisses the active and the previous popup.
*/
function dismissActiveAndPreviousPopup() {
    for(var q = 0; i < 2; q++) {
        dismissActivePopup();
    }
}

function display(element) {
    if(element != null) {
        element.style.display = "";
    }
}

function hide(element) {
    if(element != null) {
        element.style.display = "none";
    }
}

/**
    Returns a reference to the active popup, which is a <div class="popactionnew"> element.
*/
function getActivePopup() {
    var currentPopup = null;
    if(popups.length > 0) {
        currentPopup = popups[popups.length - 1];
    }
    return currentPopup;
}

function showOverlay() {
    scrollToTop();
    var overlay = document.getElementById("popoverlay");
    display(overlay);
    document.getElementsByTagName("body")[0].style.overflow = "hidden";
    document.getElementsByTagName("body")[0].style.position = "relative";
    hideSelects();
}

function hideOverlay() {

    //only hide the overlay if there are no open popups
    if(popups.length == 0) {
        hide(document.getElementById("popoverlay"));
        showSelects();
        
        //scroll to the previous coordinates
        scrollToPrevious();
        document.getElementsByTagName("body")[0].style.overflow = "visible";

        //reset the loading indicator so it's showing for next time
        showLoadingIndicator();
    }
}

function hideSelects() {
    var selects = document.getElementsByTagName("select");
    for(var i = 0; i < selects.length; i++) {
        selects[i].style.visibility = "hidden";
    }
}

function showSelects() {
    var selects = document.getElementsByTagName("select");
    for(var i = 0; i < selects.length; i++) {
        selects[i].style.visibility = "visible";
    }
}

function showLoadingIndicator() {
    display(document.getElementById("loadingIndicator"));
}

function hideLoadingIndicator() {
    hide(document.getElementById("loadingIndicator"));
}

function scrollToTop() {
  setCurrentScrollPosition();

  //scroll to the top
  scroll(0, 0);
}

function setCurrentScrollPosition() {
  if( document.documentElement.scrollLeft != 0 || document.documentElement.scrollTop != 0) {
      //remember the current scroll position
      windowScrollX = document.documentElement.scrollLeft;
      windowScrollY = document.documentElement.scrollTop;
  }
}

function scrollToPrevious() {
  if (windowScrollX || windowScrollY) {
    scroll(windowScrollX, windowScrollY);
  }
}
