//
// $Id: offerToOpenPopup.js,v 1.1 2008-01-03 04:45:45 aus10946 Exp $
//
// Provides the ability to offer the user to open a pop-up window should auto opening of pop-up windows fail.
//


// openOrOfferPopup
//
// Description:
//   Tries to open a pop-up window.  If this fails (e.g. pop-ups are blocked)
//   it offers the user the ability to open the pop-up or ignore it.
//
// Requirements:
//   Requires a suitably configured element with id=offerToOpenPopup to contain the offer HTML.
//   (See offerToOpenPopup.jsp for an example.)
//
// Parameters:
//   popupUrl                 URL to open in pop-up window (passed to window.open())
//   windowName               Name of pop-up window (passed to window.open())
//   windowFeatures           Features of pop-up window (passed to window.open())
//   purpose                  Text describing the purpose of the pop-up window
//                            in order to entice the user to open it.
//                            Will be displayed verbatim as part of the offer to open the pop-up. 
//   ignoreButtonJavascript   Javascript to execute when user clicks the 'ignore pop-up' button.
//                            Is optional: setting to null or "" will cause the offer to be removed.
//
function openOrOfferPopup(popupUrl, windowName, windowFeatures, purpose, ignoreButtonJavascript)
{
  popupWin = window.open(popupUrl, windowName, windowFeatures);
  if (popupWin != null) {
    popupWin.focus();
  }
  else
  {
    // unable to open pop-up
    // offer the user the ability to open the pop-up themselves manually
    var  errorCreatingOffer = "";
    var  offerToOpenPopupDiv = document.getElementById("offerToOpenPopup");
    if (offerToOpenPopupDiv == null) {
      errorCreatingOffer = "unable to find offerToOpenPopup element";
    } else {
      // customise content of offer
      var  popupPurposeElement = document.getElementById("popupPurpose");
      if (popupPurposeElement == null) {
        errorCreatingOffer = "unable to find popupPurpose element";
      } else {
        replaceChildrenWithText(popupPurposeElement, purpose);
      }
      // link to show pop-up
      var  showPopupLinkElement = document.getElementById("showPopupLink");
      if (showPopupLinkElement == null) {
        errorCreatingOffer = "unable to find showPopupLink element";
      } else {
        // button first hides the offer
        var  buttonActions = "hideOfferToOpenPopup();";
        // then opens the pop-up window
        buttonActions += "openPopup('" + popupUrl + "','" + windowName + "','" + windowFeatures + "');";
        showPopupLinkElement.setAttribute('href', "javascript:" + buttonActions );
        showPopupLinkElement.setAttribute('onclick', buttonActions + ";return false;");
      }
      // link to ignore pop-up
      var ignorePopupLinkElement = document.getElementById("ignorePopupLink");
      if (ignorePopupLinkElement == null) {
        errorCreatingOffer = "unable to find ignorePopupLink element";
      } else {
        // button first hides the offer
        var  buttonActions = "hideOfferToOpenPopup();";
        // and then any other actions requested
        if (ignoreButtonJavascript != null && ignoreButtonJavascript != "") {
          buttonActions += ignoreButtonJavascript;
        }
        ignorePopupLinkElement.setAttribute('href', "javascript:" + buttonActions );
        ignorePopupLinkElement.setAttribute('onclick', buttonActions + ";return false;");
      }
    }
    
    if (errorCreatingOffer == "") {
      showOfferToOpenPopup();
    } else {
      // TODO: write this error to a hidden element
      //alert("Error creating offer to open pop-up.\nERROR: " + errorCreatingOffer);
      alert("You have a pop-up blocker enabled.\nTo use the full functionality you must enable pop-ups for this website.");
    }
  }
}

// openPopup
//
// Description:
//   Tries to open a pop-up window.
//   If this fails (e.g. pop-ups are blocked) alerts the user.
//
// Requirements:
//   None.
//
// Parameters:
//   popupUrl                 URL to open in pop-up window (passed to window.open())
//   windowName               Name of pop-up window (passed to window.open())
//   windowFeatures           Features of pop-up window (passed to window.open())
//
function openPopup(url, title, windowFeatures) {
  popupWin = window.open(url, title, windowFeatures);
  if (popupWin != null) {
    popupWin.focus();
  } else {
    alert("You have a pop-up blocker enabled.\nTo use the full functionality you must enable pop-ups for this website.");
  }
}

//
// HELPER FUNCTIONS
//

// Replace all child nodes of the given element with a text node with the given text.
//
function replaceChildrenWithText(element, text) {
  // remove existing child nodes
  while (element.childNodes[0]) {
    element.removeChild(elem.childNodes[0]);
  }
  // add new text node
  var newText = document.createTextNode(text);
  element.appendChild(newText);
}

// Makes visible the offer to open a pop-up window
//
function showOfferToOpenPopup() {
  var offerToOpenPopupDiv = document.getElementById("offerToOpenPopup");
  if (offerToOpenPopupDiv) {
    offerToOpenPopupDiv.style.display="block";
  }
}

// Makes invisible the offer to open a pop-up window
//
function hideOfferToOpenPopup() {
  var offerToOpenPopupDiv = document.getElementById("offerToOpenPopup");
  if (offerToOpenPopupDiv) {
    offerToOpenPopupDiv.style.display="none";
  }
}
