<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ include file="/portal/include/include_java.jsp" %>

<%@page import="com.appiancorp.services.WebServiceContextFactory"%>
<%@page import="com.appiancorp.services.ServiceContext"%>
<%@page import="com.appiancorp.suiteapi.common.ServiceLocator"%>
<%@page import="com.appiancorp.suiteapi.portal.NavigationService"%>
<%@page import="com.appiancorp.suiteapi.portal.NavigationMenu"%>
<%@page import="org.apache.commons.lang.StringUtils"%>
<%@page import="com.appiancorp.asi.components.common.BackgroundAction"%>

<%-- The next two CDATA variables are a workaround for weblogic, which translates to an XML-style
     JSP before translating to a servlet. Having this non-standard-XML stuff embedded in the JSP
     prevents weblogic from compiling it.
--%>
<c:set var="openCDATA" value="< ! [CDATA[" />
<c:set var="closeCDATA" value="]]>" />
<%-- End weblogic workaround --%>
<c:choose><c:when test="${empty $e && empty param.$e}">
  <c:set var="$e" value="environmentContent"/>
</c:when><c:when test="${empty $e}">
  <c:set var="$e" value="${param.$e}"/>
</c:when></c:choose>
<c:if test="${empty $replace && !empty param.$replace}">
  <c:set var="$replace" value="${param.$replace}"/>
</c:if>
<c:if test="${empty $replace}">
  <c:set var="$replace" value="true"/>
</c:if>
<c:set var="queryString">
  <%=request.getQueryString()==null?"":"&"+request.getQueryString()%>
</c:set>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="<c:out value='${LOCALE_RENDERING_DIRECTION}'/>">
<head>
<title><decorator:title /></title>
<meta http-equiv="content-type" content="text/html" />
<script type="text/javascript">
  if (typeof Document != 'undefined') {
    Document.prototype.loadXML = function (s) {     
       var doc = (new DOMParser()).parseFromString(s, "text/xml");
       while (this.hasChildNodes()) { this.removeChild(this.lastChild); }
       for (var i = 0; i < doc.childNodes.length; i++) {
          this.appendChild(this.importNode(doc.childNodes[i], true));
       }
       return true;
    };
    Document.prototype.__defineGetter__("xml", function () {
       return (new XMLSerializer()).serializeToString(this);
    });
  }
</script>
<script type="text/javascript">
function refresh() {
  var xmlDoc, contentTxt = document.getElementById('content').value.replace(/^\s+|\s+$/g, '');
  if (!contentTxt) { return; }
  if (typeof ActiveXObject != 'undefined') { xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); }
  else { xmlDoc=document.implementation.createDocument("","",null); }
  xmlDoc.async="false";
  contentTxt = contentTxt.replace(/<\/~textarea>/g, '</textarea>');
  contentTxt = contentTxt.replace(/__greater_than__/g, '&gt;');
  contentTxt = contentTxt.replace(/__less_than__/g, '&lt;');
  contentTxt = contentTxt.replace(/< ! \[/g, '<![');
  if (xmlDoc.loadXML(contentTxt)) {   
    window.parent.MARS = window.parent.MARS || new window.parent.Mars('backgroundAction');
    var contentObj = parent.MARS.parseXml(xmlDoc);
    var loadConfig = {scripts:contentObj.scripts};
    window.parent.executeBackgroundLogic(contentObj, loadConfig);
  } else { throw new Error('Unable to load response as XML'); }  
}
</script>
<style type="text/css" >
body{padding:0; margin:0;}
#content{display: block; width: 98%; height: 350px; border: 1px solid gray; margin: 0 auto;}
</style>
</head>
<body onload="refresh();">
  <textarea id="content">
    <?xml version="1.0"?>
    <root>
      <content replace="<c:out value="${$replace}"/>" targetId="<c:out value="${$e}" />"> 
        <c:set var="titleContents"><decorator:title /></c:set>
        <%
              String titleContents = (String)pageContext.getAttribute("titleContents");
              titleContents=titleContents.replaceAll("&lt;","__less_than__");
              titleContents=titleContents.replaceAll("&gt;","__greater_than__");
              titleContents=BackgroundAction.replaceClosingCdata(titleContents);
              pageContext.setAttribute("titleContents",titleContents);
        %>
        <title><c:out value="${openCDATA}" /><c:out value="${titleContents}"/><c:out value="${closeCDATA}" /></title>
        <path><c:out value="${openCDATA}" />
          <%=request.getServletPath()%>?$e=<c:out value="${$e}"/><c:out value="${queryString}" />
          <c:out value="${closeCDATA}" />
        </path>
        <decorator:usePage id="thePage" />
        <%
          String navMenuPath = "";
          String metaNavMenu = thePage.getProperty("meta.navMenu");
          if (!StringUtils.isBlank(metaNavMenu)) {
            ServiceContext sc = WebServiceContextFactory.getServiceContext(request);
            NavigationService ns = ServiceLocator.getNavigationService(sc);
            NavigationMenu navMenu = ns.getNavigationMenu(metaNavMenu);
            if (navMenu != null) {
              navMenuPath = navMenu.getPath();
            }
          }
          pageContext.setAttribute("navMenuPath", navMenuPath);
        %>
        <navMenu><c:out value="${openCDATA}" /><asi:out encoding="html" value="${navMenuPath}" /><c:out value="${closeCDATA}" /></navMenu>
        <navButton><c:out value="${openCDATA}" /><decorator:getProperty property="meta.navButton" /><c:out value="${closeCDATA}" /></navButton>
        <body><c:out value="${openCDATA}" />
        <c:set var="bodyContents"><s:replace replace="</textarea>" with="</~textarea>"><decorator:body /></s:replace></c:set>
            <%
              String bodyContents = (String)pageContext.getAttribute("bodyContents");
              bodyContents=bodyContents.replaceAll("&lt;","__less_than__");
              bodyContents=bodyContents.replaceAll("&gt;","__greater_than__");
              bodyContents=BackgroundAction.replaceInvalidCharactersInXML(bodyContents);
              bodyContents=BackgroundAction.replaceClosingCdata(bodyContents);
              pageContext.setAttribute("bodyContents",bodyContents);
            %>
            <s:replace replace="<![" with="< ! [">
              <c:out value="${bodyContents}"/>
            </s:replace>
          <c:out value="${closeCDATA}" />
        </body>
      </content>
      <scripts><c:out value="${openCDATA}" />
        <c:if test="${$e=='environmentContent'}">
          <c:import url="/portal/include/background_scripts.jsp" charEncoding="UTF-8" />
        </c:if>
        <c:import url="/portal/decorators/errors.jsp" charEncoding="UTF-8"/>
        <c:out value="${closeCDATA}" />
      </scripts>
    </root>
  </textarea>
</body>
</html>