// Popup methods and variables //

var m_bPopupDebug = false;

var m_bPopupForceClose = false;
var m_bClosePopupsOnUnload = true;

function popupExecuteOnLoad(strFormName, parentFormName) {
   var form = formGetForm(strFormName);
   if (form == null) {
      _popupFatal("Cannot find a valid form ("+strFormName+") in the popup. Probably because some fields are missing (popupExecuteOnLoad)");
      return;
   }
   if (form._CloseOnLoad == null) {
      _popupFatal("A hidden field named '_CloseOnLoad' must exist in the popup window (popupExecuteOnLoad)");
      return;
   }
   if (form._ReloadParentOnLoad == null) {
      _popupFatal("A hidden field named '_ReloadParentOnLoad' must exist in the popup window (popupExecuteOnLoad)");
      return;
   }

   window.opener._formUnlockForSubmit();

   if (form._CloseOnLoad.value == "true")
      window.opener._popupParentDereferencePopup(parentFormName, this, this.name);

   if (form._ReloadParentOnLoad.value == "true")
      window.opener.popupParentReloadAndKeepPopups(parentFormName);
   if (form._CloseOnLoad.value == "true") {
      m_bPopupForceClose = true;
      if (!window.closed)
         window.close();
   }
}

function popupExecuteOnUnload(strFormName, parentFormName) {
   var form = formGetForm(strFormName);
   var popup;

   if (form == null) {
      _popupFatal("cannot find a valid form ("+strFormName+") in the popup. Probably because some fields are missing (popupExecuteOnUnload)");
      return;
   }

   if (!m_bPopupForceClose) {

      if (form._CloseOnUnload == null) {
         _popupFatal("An hidden field named '_CloseOnUnload' must exist in the popup window (popupExecuteOnUnload)");
         return;
      }
      if (form._CloseOnUnload.value == "true" || form._CloseOnUnload.value == "") {
         try {
            popup = window.opener._popupParentDereferencePopup(parentFormName, this, this.name);
            if (popup == null) {
               _popupAddCookie("Closed", this.name);
            }
         } catch (e) {
            _popupAddCookie("Closed", this.name);
         }
      }

      if (form._ReloadParentOnUnload == null) {
         _popupFatal("An hidden field named '_ReloadParentOnUnload' must exist in the popup window (popupExecuteOnUnload)");
         return;
      }


      if (form._ReloadParentOnUnload.value == "true")
         window.opener.popupParentReloadAndKeepPopups(parentFormName);

      if (form._CloseOnUnload.value == "true" || form._CloseOnUnload.value == "") {
         if (!window.closed)
            window.close();
      }
   }
}

function popupRefresh(strFormName) {
   var form = formGetForm(strFormName);
   if (form == null) {
      _popupFatal("cannot find a valid form ("+strFormName+") in the popup. Probably because some fields are missing (popupRefresh)");
      return;
   }

   return popupSubmitUrl(strFormName, form.Origin.value, "_self");
}

function popupSubmitUrl(strFormName, strUrl, strTarget) {
   var form = formGetForm(strFormName);
   if (form == null) {
      _popupFatal("cannot find a valid form ("+strFormName+") in the popup. Probably because some fields are missing (popupSubmitUrl)");
      return;
   }

   form._CloseOnLoad.value="false";
   form._ReloadParentOnLoad.value="false";
   form._CloseOnUnload.value="false";
   form._ReloadParentOnUnload.value="false";
   m_bClosePopupsOnUnload = false;
   formSubmitUrl(strFormName, strUrl, strTarget)

   return false; // must return false
}

function popupSubmitExecuteCommand(strFormName, strCommand, strRedirect, strTarget) {
   return _popupSubmitExecuteCommand(strFormName, strCommand, strRedirect, strTarget, true);
}

//This function is to called only if you're sure the executed strCommand won't refresh
//the current popup. If it does, the popup, will be closed on unload
function popupSubmitExecuteCommandAndNoReload(strFormName, strCommand, strRedirect, strTarget) {
   _popupSubmitExecuteCommand(strFormName, strCommand, strRedirect, strTarget, false);
   _formUnlockForSubmit();
   return false;
}

function _popupSubmitExecuteCommand(strFormName, strCommand, strRedirect, strTarget, bReloadWin) {
   var form = formGetForm(strFormName);
   if (form == null) {
      _popupFatal("cannot find a valid form ("+strFormName+") in the popup. Probably because some fields are missing (_popupSubmitExecuteCommand)");
      return;
   }

   form._CloseOnLoad.value="false";
   form._ReloadParentOnLoad.value="false";
   if (bReloadWin)
      form._CloseOnUnload.value="false";
   else
      form._CloseOnUnload.value="true";
   form._ReloadParentOnUnload.value="false";
   m_bClosePopupsOnUnload = false;
   formSubmitExecuteCommand(strFormName, strCommand, strRedirect, strTarget);
   return false; // must return false
}

function popupClose(strFormName) {
   var form = formGetForm(strFormName);
   if (form == null) {
      _popupFatal("cannot find a valid form ("+strFormName+") in the popup. Probably because some fields are missing (popupClose)");
      return;
   }

   if (!window.closed)
      window.close();
}

function popupCloseAndParentRefresh(strFormName, parentFormName) {
   if (!window.closed) {
      window.opener._popupParentDereferencePopup(parentFormName, this, this.name);
      window.opener.formRefresh(parentFormName);
      popupClose(strFormName, parentFormName);
   }
}

function popupCloseAndForceParentRefresh(strFormName, strParentFormName) {
   window.opener._formUnlockForSubmit(strParentFormName);
   popupCloseAndParentRefresh(strFormName, strParentFormName)
}

function popupCloseAndParentSubmitUrl(strFormName, parentFormName, strUrl) {
   if (!window.closed) {
      var form = formGetForm(strFormName);
      if (form == null) {
         _popupFatal("cannot find a valid form ("+strFormName+") in the popup. Probably because some fields are missing (popupCloseAndParentSubmitUrl)");
         return;
      }

      form._CloseOnUnload.value = "false";
      window.opener._popupParentDereferencePopup(parentFormName, this, this.name);
      window.opener.formSubmitUrl(parentFormName, strUrl, "_self");
      popupClose(strFormName, parentFormName);
   }
}

function popupCloseAndParentExecuteCommand(strFormName, parentFormName, strCommand, strRedirect) {
   if (!window.closed) {
      var form = formGetForm(strFormName);
      if (form == null) {
         _popupFatal("cannot find a valid form ("+strFormName+") in the popup. Probably because some fields are missing (popupCloseAndParentSubmitUrl)");
         return;
      }

      form._CloseOnUnload.value = "false";
      window.opener._popupParentDereferencePopup(parentFormName, this, this.name);
      window.opener.formSubmitExecuteCommand(parentFormName, strCommand, strRedirect, "_self");
      popupClose(strFormName, parentFormName);
   }
}

function _popupGetTopParent(obj) {
   if (obj.opener == null) {
      return obj;
   } else {
      return _popupGetTopParent(obj.opener);
   }
}

function popupTopParentChangeUrl(strUrl) {
   ascendant = _popupGetTopParent(this);
   ascendant.location = strUrl;
}

function popupRefreshAndClose(strFormName) {
   var form = formGetForm(strFormName);
   if (form == null) {
      _popupFatal("cannot find a valid form ("+strFormName+") in the popup. Probably because some fields are missing (popupRefreshAndClose)");
      return;
   }

   return popupSubmitUrlAndClose(strFormName, form.Origin.value, "_self");
}

function popupSubmitUrlAndClose(strFormName, strUrl, strTarget) {
   var form = formGetForm(strFormName);
   if (form == null) {
      _popupFatal("cannot find a valid form ("+strFormName+") in the popup. Probably because some fields are missing (popupSubmitUrlAndClose)");
      return;
   }

   form._CloseOnLoad.value="true";
   form._ReloadParentOnLoad.value="false";
   form._CloseOnUnload.value="false";
   form._ReloadParentOnUnload.value="false";
   m_bClosePopupsOnUnload = false;
   formSubmitUrl(strFormName, strUrl, strTarget);
   return false; // must return false
}

function popupSubmitExecuteCommandAndClose(strFormName, strCommand, strRedirect, strTarget) {
   var form = formGetForm(strFormName);
   if (form == null) {
      _popupFatal("cannot find a valid form ("+strFormName+") in the popup. Probably because some fields are missing (popupSubmitExecuteCommandAndClose)");
      return;
   }

   form._CloseOnLoad.value="true";
   form._ReloadParentOnLoad.value="false";
   form._CloseOnUnload.value="false";
   form._ReloadParentOnUnload.value="false";
   m_bClosePopupsOnUnload = false;
   formSubmitExecuteCommand(strFormName, strCommand, strRedirect, strTarget)
   return false; // must return false
}

function popupRefreshAndCloseAndParentRefresh(strFormName) {
   var form = formGetForm(strFormName);
   if (form == null) {
      _popupFatal("cannot find a valid form ("+strFormName+") in the popup. Probably because some fields are missing (popupRefreshAndCloseAndParentRefresh)");
      return;
   }

   return popupSubmitUrlAndCloseAndParentRefresh(strFormName, form.Origin.value, "_self");
}

function popupSubmitUrlAndCloseAndParentRefresh(strFormName, strUrl, strTarget) {
   var form = formGetForm(strFormName);
   if (form == null) {
      _popupFatal("cannot find a valid form ("+strFormName+") in the popup. Probably because some fields are missing (popupSubmitUrlAndCloseAndParentRefresh)");
      return;
   }

   form._CloseOnLoad.value="true";
   form._ReloadParentOnLoad.value="true";
   form._CloseOnUnload.value="false";
   form._ReloadParentOnUnload.value="false";
   m_bClosePopupsOnUnload = false;
   formSubmitUrl(strFormName, strUrl, strTarget);
   return false; // must return false
}

function popupSubmitExecuteCommandAndCloseAndParentRefresh(strFormName, strCommand, strRedirect, strTarget) {
   var form = formGetForm(strFormName);
   if (form == null) {
      _popupFatal("cannot find a valid form ("+strFormName+") in the popup. Probably because some fields are missing (popupSubmitExecuteCommandAndCloseAndParentRefresh)");
      return;
   }

   form._CloseOnLoad.value="true";
   form._ReloadParentOnLoad.value="true";
   form._CloseOnUnload.value="false";
   form._ReloadParentOnUnload.value="false";
   m_bClosePopupsOnUnload = false;
   formSubmitExecuteCommand(strFormName, strCommand, strRedirect, strTarget);
   return false; // must return false
}

function popupRefreshAndParentRefresh(strFormName) {
   var form = formGetForm(strFormName);
   if (form == null) {
      _popupFatal("cannot find a valid form ("+strFormName+") in the popup. Probably because some fields are missing (popupRefreshAndParentRefresh)");
      return;
   }

   return popupSubmitUrlAndParentRefresh(strFormName, form.Origin.value, "_self");
}

function popupSubmitUrlAndParentRefresh(strFormName, strUrl, strTarget) {
   var form = formGetForm(strFormName);
   if (form == null) {
      _popupFatal("cannot find a valid form ("+strFormName+") in the popup. Probably because some fields are missing (popupSubmitUrlAndParentRefresh)");
      return;
   }

   form._CloseOnLoad.value="false";
   form._ReloadParentOnLoad.value="true";
   form._CloseOnUnload.value="false";
   form._ReloadParentOnUnload.value="false";
   m_bClosePopupsOnUnload = false;
   formSubmitUrl(strFormName, strUrl, strTarget);
   return false; // must return false
}

function popupSubmitExecuteCommandAndParentRefresh(strFormName, strCommand, strRedirect, strTarget) {
   var form = formGetForm(strFormName);
   if (form == null) {
      _popupFatal("cannot find a valid form ("+strFormName+") in the popup. Probably because some fields are missing (popupSubmitExecuteCommandAndParentRefresh)");
      return;
   }

   form._CloseOnLoad.value="false";
   form._ReloadParentOnLoad.value="true";
   form._CloseOnUnload.value="false";
   form._ReloadParentOnUnload.value="false";
   m_bClosePopupsOnUnload = false;
   formSubmitExecuteCommand(strFormName, strCommand, strRedirect, strTarget);
   return false; // must return false
}

function _popupForceClose(strFormName) {
   m_bPopupForceClose = true;
   this.close();
}


// Parent methods and variables //

var m_popupParentOldAction;
var m_popupParentOldCommand;
var m_popupParentOldRedirect;
var m_popups = new Array();
var m_popupParentLoaded = false;

function popupParentExecuteOnLoad(strFormName) {
   var form = formGetForm(strFormName);
   var strPopups;
   var strPopupName;
   var nStart, nEnd, nCounter;
   var iPopup;

   if (form == null) {
      _popupFatal("cannot find a valid form ("+strFormName+") in the parent. Probably because some fields are missing (popupParentExecuteOnLoad)");
      return;
   }

   if (form._Popups == null) {
      _popupFatal("A hidden field named '_Popups' must exist in the parent window");
      return;
   }
   strPopups = form._Popups.value;
   nStart=1; // a comma precede the name of the window
   nCounter = 0;

   strPopups = popupCookieRemoveClosedPopups("Closed", strPopups);

   while((nEnd = strPopups.indexOf(",", nStart)) != -1) {
      strPopupName = strPopups.substring(nStart, nEnd);
      iPopup = new _IPopup(window.open("", strPopupName, ""), strPopupName);
      m_popups[nCounter]=iPopup;
      nStart = nEnd + 2;
      nCounter++;
   }

   m_popupParentLoaded = true;
}

function popupParentExecuteOnUnload(strFormName) {
   popupParentCloseAllPopupsOnUnload(strFormName);
}

function popupParentHasPopup(strFormName) {
   var form = formGetForm(strFormName);
   if (m_popups != null && m_popups.length>0)
      return true;
   return false;
}

function popupParentOpenPopup(strFormName, name, strCommand, strUrl, width, height) {
   return popupParentOpenCustomPopup(strFormName, name, strCommand, strUrl, width, height, false, true);
}

function popupParentOpenCustomPopup(strFormName, name, strCommand, strUrl, width, height, bCentered, bHasScrollbars) {
   var form = formGetForm(strFormName);
   var newPopup;
   var i;
   var strPopupUrl;
   var strFeatures;
   var temp_Popups;
   var tempParentFormName;
   var strBlankUrl;
   var bSubmitForm;
   var currentDate;
   var diff;

   strBlankUrl = WEBAPP_URL + "blank.htm";

   if (form == null) {
      _popupFatal("cannot find a valid form ("+strFormName+") in the parent. Probably because some fields are missing (popupParentOpenPopup)");
      return;
   }

   bSubmitForm = true;
   strPopupUrl = strBlankUrl;
   newPopup = null;

   if ((newPopup = _popupIsOpened(name)) != null) {
      // popup already opened
      newPopup.popup.focus();
   } else {
      _popupParentPushContext(strFormName);

      if(strCommand==null || strCommand=="") {
         if (strUrl==null || strUrl=="") {
            bSubmitForm = false;
         } else if (strUrl.indexOf("?") != -1) {
            strPopupUrl = strUrl;
            bSubmitForm = false;
         } else {
            form.action = strUrl;
         }
      } else {
         form.action=INSTRASERVLET_URL;
         form.Command.value=strCommand;
      }

      if (bHasScrollbars)
         strFeatures = "scrollbars=1,";
      else
         strFeatures = "scrollbars=0,";
      if (bCentered) {
         if (isMSIE4)
            strFeatures += "left=" + (screen.width/2 - width/2) + ",top=" + (screen.height/2 - height/2) + ",";
         else if (isNav4)
            strFeatures += "screenX=" + (screen.width/2 - width/2) + ",screenY=" + (screen.height/2 - height/2) + ",";
      }
      strFeatures += "toolbar=0,location=0,directories=0,status=0,resizable=1,copyhistory=0,menuBar=0,width="+width+",height="+height;

      newPopup = window.open(strPopupUrl,name,strFeatures);

      while(!newPopup.location);

      _popupParentReferenceNewPopup(strFormName, newPopup, name);

      if (bSubmitForm) {
         if (_formCheckSubmitableAndLock()) {
            _formPushContext(strFormName, name)

            form.target=name;
            while(newPopup == null) {}
            form.submit();

            _formPopContext(strFormName, name)
         }
      }

      _popupParentPopContext(strFormName);
   }

   return false;
}

function popupParentReloadAndClosePopups(strFormName) {
   var form = formGetForm(strFormName);
   m_bClosePopupsOnUnload = true;
   formRefresh(strFormName);
}

function popupParentReloadAndKeepPopups(strFormName) {
   var form = formGetForm(strFormName);
   m_bClosePopupsOnUnload = false;
   // if the parent is a popup, set correctly the popup's fields
   if (form._CloseOnLoad != null)
      form._CloseOnLoad.value="false";
   if (form._ReloadParentOnLoad != null)
      form._ReloadParentOnLoad.value="false";
   if (form._CloseOnUnload != null)
      form._CloseOnUnload.value="false";
   if (form._ReloadParentOnUnload != null)
      form._ReloadParentOnUnload.value="false";
      m_bClosePopupsOnUnload = false;

   formRefresh(strFormName);
}

function popupParentSubmitUrlAndClosePopups(strFormName, strUrl, strTarget) {
   var form = formGetForm(strFormName);
   if (form == null) {
      _popupFatal("cannot find a valid form ("+strFormName+") in the popup. Probably because some fields are missing (popupParentSubmitUrlAndClosePopups)");
      return;
   }

   form._CloseOnLoad.value="false";
   form._ReloadParentOnLoad.value="false";
   form._CloseOnUnload.value="false";
   form._ReloadParentOnUnload.value="false";
   m_bClosePopupsOnUnload = true;
   formSubmitUrl(strFormName, strUrl, strTarget);
   return false; // must return false
}

function popupParentSubmitUrlAndKeepPopups(strFormName, strUrl, strTarget) {
   var form = formGetForm(strFormName);
   if (form == null) {
      _popupFatal("cannot find a valid form ("+strFormName+") in the popup. Probably because some fields are missing (popupParentSubmitUrlAndKeepPopups)");
      return;
   }

   form._CloseOnLoad.value="false";
   form._ReloadParentOnLoad.value="false";
   form._CloseOnUnload.value="false";
   form._ReloadParentOnUnload.value="false";
   m_bClosePopupsOnUnload = false;
   formSubmitUrl(strFormName, strUrl, strTarget);
   return false; // must return false
}

function popupParentSubmitExecuteCommandAndKeepPopups(strFormName, strCommand, strRedirect, strTarget) {
   var form = formGetForm(strFormName);
   if (form == null) {
      _popupFatal("cannot find a valid form ("+strFormName+") in the popup. Probably because some fields are missing (popupParentSubmitUrlAndKeepPopups)");
      return;
   }

   form._CloseOnLoad.value="false";
   form._ReloadParentOnLoad.value="false";
   form._CloseOnUnload.value="false";
   form._ReloadParentOnUnload.value="false";
   m_bClosePopupsOnUnload = false;
   formSubmitExecuteCommand(strFormName, strCommand, strRedirect, strTarget)
   return false; // must return false
}

function _popupParentPushContext(strFormName) {
   var form = formGetForm(strFormName);
   m_popupParentOldAction = form.action;
   m_popupParentOldCommand = form.Command.value;
   m_popupParentOldRedirect = form.Redirect.value;
}

function _popupParentPopContext(strFormName) {
   var form = formGetForm(strFormName);
   form.action = m_popupParentOldAction;
   form.target = "_self";
   form.Command.value = m_popupParentOldCommand;
   form.Redirect.value = m_popupParentOldRedirect;
}

function _popupParentReferenceNewPopup(strFormName, newPopup, name) {
   var nPopupsReferenced = m_popups.length;
   m_popups[nPopupsReferenced] = new _IPopup(newPopup, name);
}

function _popupParentDereferencePopup(strFormName, popup, popupName) {
   var popusArrayCopy;
   var i, j;

   if (m_popupParentLoaded == false)
      return null;

   popupsArrayCopy = new Array();
   j=0;
   for (i=0; i<m_popups.length; i++) {
      if (m_popups[i].name != popupName) {
         popupsArrayCopy[j] = m_popups[i];
         j++;
      }
   }
   m_popups = popupsArrayCopy;
   // calling _popupPrepareFormForSubmit allow to update the _Popups field of the parent dynamically.
   // it allows to quit the parent (clicking on an other tab for example), without keeping old references to closed popups.
   _popupPrepareFormForSubmit(strFormName);
   return popup;
}

function popupParentCloseAllPopupsOnUnload(strFormName) {
   if (m_bClosePopupsOnUnload == true)
      _popupParentCloseAllPopups(strFormName);
}

function _popupParentCloseAllPopups(strFormName) {
   var i;
   var dereferencedPopup;
   while (m_popups != null && m_popups.length>0) {
      dereferencedPopup = _popupParentDereferencePopup(strFormName, m_popups[0].popup, m_popups[0].name);
      try {
         dereferencedPopup._popupForceClose(strFormName);
      } catch (e) {
         _popupFatal("Cannot access to the _popupForceClose() function of the popup, verify that the file popup.js is included in the popup (_popupParentCloseAllPopups) with parameters dereferencedPopup=" + dereferencedPopup + " and strFormName=" + strFormName);
         return;
      }
   }
}

function _popupFatal(strMessage) {
   if (m_bPopupDebug)
      alert("***DEBUG ERROR***\n" + strMessage);
}

function _IPopup(windowObject, windowName) {
   this.popup=windowObject;
   this.name=windowName;
}

function _popupPrepareFormForSubmit(strFormName) {
   var form = formGetForm(strFormName);
   var strPopups;
   var i;
   if (form._Popups == null) {
      return;
   }
   if (m_popups == null) {
      return;
   }

   strPopups = '';
   for (i=0; i<m_popups.length; i++) {
      strPopups += ',' + m_popups[i].name + ',';
   }
   form._Popups.value = strPopups;
}

function _popupIsOpened(name) {
   var i;
   var popup;

   for (i=0; i<m_popups.length; i++) {
      if (m_popups[i].name == name)
         return m_popups[i];
   }
   return null;
}




// TODO: put these functions in cookie.js, change the names, etc...
function _popupAddCookie(strCookieName, strValue) {
   var strCookies = document.cookie;
   var nStart = strCookies.indexOf(strCookieName);
   var nEnd;
   var strValues;

   if (nStart == -1) {
      document.cookie = strCookieName + '=' + strValue;
      return;
   }

   nEnd = strCookies.indexOf(';', nStart);

   if (nEnd == -1)
      strValues = strCookies.substring(nStart + strCookieName.length + 1);
   else
      strValues = strCookies.substring(nStart + strCookieName.length + 1, nEnd);

   if (strValues.indexOf(strValue) >= 0)
      return;

   if (nEnd == -1) {
      document.cookie = strCookies.substring(nStart) + ',' + strValue;
   } else {
      document.cookie = strCookies.substring(nStart, nEnd) + ',' + strValue;
   }
}

var killCookieTime = new Date("January 1, 1970");
var strKillCookieTime = killCookieTime.toGMTString();

function popupCookieRemoveClosedPopups(strCookieName, strPopups) {
   var strCookies = document.cookie;
   var nStart = strCookies.indexOf(strCookieName);
   var nEnd;
   var strValues;
   var names;
   var newNames;
   var popups;
   var newPopups;
   var iName;
   var iPopup;

   if ((nStart == -1) || (strPopups.length == 0)) {
      return strPopups;
   }

   nEnd = strCookies.indexOf(';', nStart);

   if (nEnd == -1)
      strValues = strCookies.substring(nStart + strCookieName.length + 1);
   else
      strValues = strCookies.substring(nStart + strCookieName.length + 1, nEnd);

   if (strValues.length == 0)
      return strPopups;

   names = strValues.split(',');
   newNames = new Array();
   popups = strPopups.split(',');
   newPopups = new Array();

   iName = 0;
   iPopup = 0;
   while (iName < names.length) {
      if (strPopups.indexOf(names[iName]) == -1) {
         newNames[newNames.length] = names[iName];
      }
      iName++;
   }

   while (iPopup < popups.length) {
      if (strValues.indexOf(popups[iPopup]) == -1) {
         newPopups[newPopups.length] = popups[iPopup];
      }
      iPopup++;
   }

   if (newNames.length == 0) {
      // Remove cookie
      document.cookie = strCookieName + "=x;expires=" + strKillCookieTime;
   } else {
      document.cookie = strCookieName + '=' + newNames.join(',');
   }

   if (newPopups.length == 0)
      return "";
   else
      return ',' + newPopups.join(',') + ',';
}

