<%@  page pageEncoding="utf-8" contentType="text/html;charset=utf-8" import="com.novell.webaccess.common.JSPConduit2, com.novell.emframe.dev.eMFrameUtils" %>
<%   JSPConduit2 c = JSPConduit2.getJSPConduit(request);
     c.stringTable("DevResources");
     c.stringTable("FwResources"); %>

<HTML>
<HEAD><TITLE>BrowserQuery</TITLE>
<LINK href="<%= c.getModulesUrl() %>/fw/css/osStyles.css" rel="styleSheet" type="text/css">
<jsp:include page='<%= c.getPath("dev/eMFrameScripts_inc.jsp") %>' flush="true" />
<jsp:include page='<%= c.getPath("dev/XmlScripts_inc.jsp") %>' flush="true" />

<script language="JavaScript">

   var XML_ID                    = "<%= c.toScript(c.string("AS.XML.id")) %>";
   var XML_TYPES                 = "<%= c.toScript(c.string("AS.XML.types")) %>";

   //------------- doSearch functions ----------------
   function doGenericSearch()
   {
      //update global variables
      //var tmp = document.BrowseQuery.BQNameFilter.value;
      //if ((tmp != null) && (tmp.length != 0))
      //{
      //   top.m_nameFilter = tmp;
      //}
      //top.m_container = getContext();
      
      updateGlobalVars();
      doSearch();
   }

   function doAdvancedSearch()
   {
      //alert("doing an advanced search...");
      //update global variables
      //top.m_container = getContext();

      updateGlobalVars();
      doSearch();
   }


   var blinking = false;
   var blinkVisibility = "";
   var waitCnt = 30;
   function clearResults(cnt)
   {
      if (cnt==-1)
      {
         //initialize clearResults
         top.resultsLoaded =     false;
         blinking =              false;
         waitCnt =               30;      // initial 3 second wait
         cnt =                   0;
      }

      //do we have new results?
      if (top.resultsLoaded)
      {
         //results have been loaded!  Make sure results are visible
         top.Results.document.getElementById("resultsDiv").style.display="inline";
         top.Results.document.getElementById("browsingDiv").style.display="none";
      }
      //no new results yet!
      else if (cnt<waitCnt)
      {
         //we haven't waited long enough
         cnt++;
         setTimeout("clearResults("+cnt+")",100);  //check again in .1 sec
      }
      else
      {
         //waited long enough -- time to do something!
         if (!blinking)
         {
            //start blinking (clear results and buttons; make browingDiv visible)
            try
            {
               //make browing message visible
               top.Results.document.getElementById("resultsDiv").style.display="none";
               top.Results.document.getElementById("browsingDiv").style.display="inline";

               //start blinking
               blinking=true;
               blinkVisibility="inline";
               waitCnt = 20;     //wait up to 2 second more
            }
            catch (e)
            {
            }
         }
         //cycle blinking state
         else
         {
            blinkVisibility=((blinkVisibility=="none")?"inline":"none");
            waitCnt = ((blinkVisibility=="none")?5:20);    //wait up to .5 or 2 seconds more
            try
            {
               //set blink message visibility
               top.Results.document.getElementById("browsingDiv").style.display=blinkVisibility;
            }
            catch (e)
            {
            }
         }

         //restart the timer
         setTimeout("clearResults(0)",100);   //check again in .1 sec
      }
   }
   
   function clearResultButtons()
   {
      //clear result buttons
      top.ResultButtons.document.open();
      if (MS55)
         top.ResultButtons.document.writeln('<HTML><HEAD></HEAD><BODY bgcolor="#EFEEE9" style="border-style:solid; border-width:1; border-color:#BFBBB2;"></BODY>');
      else
         top.ResultButtons.document.writeln('<HTML><HEAD></HEAD><BODY bgcolor="#EFEEE9"></BODY>');
      top.ResultButtons.document.close();      
   }

   function doSearch()
   {
      clearResultButtons();
      clearResults(-1);  //clear results after 3 seconds

      //create action command
      var tmp = ((top.m_multiSelect=="true")?"MS":"SS");
      var strURL = "webacc?taskId=fw.ObjectSelector&merge=fw.OS.BrowseResults"+tmp+"&error=dev.GenErr&SearchToken=SearchTokenPost";

      //create dynamic HTML input fields
      var strHTML="";
      strHTML  = '<input type=hidden name="User.context" value="'          +top.m_userContext+'">\n';
      strHTML += '<input type=hidden name="Search.ContextId" value="'      +toTag(top.m_searchContextId)+'">\n';
      strHTML += '<input type=hidden name="OS.AdvancedSelection" value="'  +top.m_advSel+'">\n';
      strHTML += '<input type=hidden name="OS.Container" value="'          +toTag(top.m_container)+'">\n';
      strHTML += '<input type=hidden name="OS.Mode" value="'               +top.m_mode+'">\n';
      strHTML += '<input type=hidden name="OS.ResultsPerPage" value="'     +top.m_resultsPerPage+'">\n';
      strHTML += '<input type=hidden name="OS.AuthName" value="'           +top.m_authName+'">\n';
      strHTML += '<input type=hidden name="OS.ShowSubClasses" value="'     +top.m_showSubClasses+'">\n';
      strHTML += '<input type=hidden name="OS.SearchSubContainers" value="false">\n';

      var bAdvanced = top.isAdvSelMode();
      strHTML += '<input type=hidden name="OS.TypeFilter" value="'         +toTag(top.getSelectedTypes(bAdvanced))+'">\n';
      if (bAdvanced)
      {
         strHTML += '<input type=hidden name="OS.NameFilter" value="">\n';
         var cnt=0;
         for (var i=0; i<top.m_types.length; i++)
         {
            if ( top.m_types[i][0] &&                                         //type is enabled
                 top.m_types[i][1]!=null && top.m_types[i][1].length>0  &&    //we have a type name
                 top.m_types[i][2]!=null && top.m_types[i][2].length>0 )      //we have a criterion
            {
               //output advanced criterion
               strHTML += '<input type=hidden name="OS.TypeFilter.'+cnt+'.name" value="'         +toTag(top.m_types[i][1])+ '">\n';
               strHTML += '<input type=hidden name="OS.TypeFilter.'+cnt+'.crit" value="'         +xmlEncode(top.m_types[i][2])+ '">\n';
               cnt++;
            }
         }
         strHTML += '<input type=hidden name="OS.TypeFilter.cnt" value="'         +cnt+ '">\n';
      }
      else
      {
         strHTML += '<input type=hidden name="OS.NameFilter" value="'         +toTag(top.m_nameFilter)+'">\n';
      }

      //alert("doGenericSearch: url = "+strURL);
      //alert("doGenericSearch: html = "+strHTML);
      document.BrowseQuery.action = strURL;
      document.getElementById("PostData").innerHTML = strHTML;
      document.BrowseQuery.submit();
   }

   function getContext()
   {
      var strContext = document.BrowseQuery.BQContext.value;
      if (strContext == null || strContext.length==0)
      {
         strContext = "<%= c.toScript(c.string("OS.Browser.RootName")) %>";
         document.BrowseQuery.BQContext.value = strContext;
      }
      return strContext;
   }
   
   //called whenever context or namefilter gui fields change
   function updateGlobalVars()
   {
      top.m_container = getContext();
      top.m_nameFilter = document.BrowseQuery.BQNameFilter.value;
      if (top.m_nameFilter==null || top.m_nameFilter.length==0)
      {
         top.m_nameFilter = "*";
         document.BrowseQuery.BQNameFilter.value = "*";
      }
   }
   
   //-------------------- load/save query -------------------------
   //called by doLoadQuery in QueryScripts.js
   function osLoadQuery(o)
   {
      //debug dump of object o
      //dumpObject("osLoadQuery",o);
      
      //retrieve context
      top.m_container=o.container;
      document.BrowseQuery.BQContext.value=o.container;
      
      //retrieve name filter
      top.m_nameFilter=xmlDecode(o.nameFilter);
      document.BrowseQuery.BQNameFilter.value=xmlDecode(o.nameFilter);
      
      //clear all current search criteria
      top.clearAllCriterion();
      for (var i=0; i<top.m_types.length; i++)
      {
         top.m_types[i][0]=true;  //enable all types
      }
      
      //retrieve advanced type criteria
      var criteria = o.criteria;
      for (var i=0; i<criteria.length; i++)
      {
         var typeNames = top.getCriteriaTypes(criteria[i]);   //get type name from xml
         top.saveCriterionArray(typeNames, criteria[i]);
      }
       
      if (criteria==null || criteria.length==0)
      {
         top.setAdvSelMode('false');
      }
      else
      {
         top.setAdvSelMode('true');
      }
   }
   
   //build a query object and return it to doSaveQuery in QueryScripts.js
   function osSaveQuery()
   {
      //build query object
      var p = new Object();
      p.container=top.m_container;
      p.subcontainerSearch="false";
      p.nameFilter=top.m_nameFilter;
      p.typeFilter=top.getSelectedTypes(top.isAdvSelMode());
      var criteria = new Array();
      var cnt = 0;
      for (var i=0; i<top.m_types.length; i++)
      {
         if ( top.m_types[i][0] &&                                         //type is enabled
              top.m_types[i][1]!=null && top.m_types[i][1].length>0  &&    //we have a type name
              top.m_types[i][2]!=null && top.m_types[i][2].length>0 )      //we have a criterion
         {
            //output advanced criterion
            criteria[cnt]=top.m_types[i][2];
            cnt++;
         }
      }
      p.criteria=criteria;
      p.id = "OS";  //not used
      //dumpObject("osSaveQuery",p);
      return p;
   }
   
   function dumpObject(title,obj)
   {
      var s = "";
      s  = title+": query object = \n";
      s += "   id = "+obj.id+"\n";
      s += "   container = "+obj.container+"\n"; 
      s += "   subcontainerSearch = "+obj.subcontainerSearch+"\n"; 
      s += "   nameFilter = "+obj.nameFilter+"\n"; 
      s += "   typeFilter = "+obj.typeFilter+"\n"; 
      for (var i=0; i<obj.criteria.length; i++)
      {
         s += "   criteria["+i+"] = "+obj.criteria[i]+"\n";
      }
      alert(s);
   }
   
   
   //------------------- onLoad frame coordination ---------------------
   var t = null;
   function queryOnLoad(cnt)
   {
   	  //wait up to 10 seconds for the Results frame to load
      if (cnt<100)
      {
         if (top.resultsLoaded==false ||
             top.titleLoaded==false)
         {
            cnt++;
            t = setTimeout("queryOnLoad("+cnt+")",100)
         }
         else
         {
            if (t!=null)
            {
               clearTimeout(t);
            }

            //initialize container input
            if (top.m_container=="")
            {
               top.m_container="<%= c.toScript(c.string("OS.Browser.RootName")) %>";
            }            
            document.BrowseQuery.BQContext.value=top.m_container;

            //initialize name filter input
            if (top.m_nameFilter=="")
            {
               top.m_nameFilter="*";
            }
            document.BrowseQuery.BQNameFilter.value=top.m_nameFilter;

            // do an initial search
            top.doSearch();

            // set focus to context entry well
            document.BrowseQuery.BQContext.focus();
         }
      }
   }
</script>
</HEAD>

<script>document.writeln('<BODY TEXT="#000000" LINK="#000000" VLINK="#000000" ALINK="#000000" bgcolor="'+top.OS_BGCOLOR+'" onLoad="top.queryLoaded=true; queryOnLoad(0)" onFocus="top.ensureOpenerValid();" onDragStart="iman.cancelEvent(event)">');</script>
<!--BODY TEXT="#000000" LINK="#000000" VLINK="#000000" ALINK="#000000" bgcolor="#edeeec" onDragStart="iman.cancelEvent(event)" onLoad="top.queryLoaded=true; queryOnLoad(0)" onFocus="top.ensureOpenerValid();"-->
<form name="BrowseQuery" method=post action="webacc" target="Results" onSubmit="javascript:top.processDisplayCountChange();top.doSearch();return false">
<BR>
<NOBR>
<FONT face="Helvetica,Arial" size=-1>
<b><%= c.toDisplay(c.string("OS.Browser.BrowseFrom")) %></b></NOBR><BR>
<input style="width:100%" type=text name="BQContext" value="" onchange="updateGlobalVars();"><BR><NOBR>
</FONT>
<FONT face="Helvetica,Arial" size=-4>
<%= c.toDisplay(c.string("OS.Browser.BrowseFromEx")) %></NOBR><BR><BR>
</FONT>

<!--simple selection criterion -->
<div id=SimpleCriterion style="display:none">
<NOBR><FONT face="Helvetica,Arial" size=-1>
<b><%= c.toDisplay(c.string("OS.Browser.BrowseFor")) %></b></NOBR><BR>
<input style="width:100%" type=text name="BQNameFilter" value="" maxlength="128" onchange="updateGlobalVars();"><BR>
<NOBR></FONT>
<FONT face="Helvetica,Arial" size=-4>
<%= c.toDisplay(c.string("OS.Browser.BrowseForEx")) %></NOBR>
</FONT>

<BR><BR>
<FONT face="Helvetica,Arial" size=-1><NOBR><b><%= c.toDisplay(c.string("OS.Browser.BrowseObjectTypes")) %></b></NOBR></FONT><BR>
<script>
   top.showSimpleTypes();
</script>
</div>

<!--advanced selection criterion -->
<div id=AdvancedCriterion style="display:none">
<FONT face="Helvetica,Arial" size=-1><NOBR><b><%= c.toDisplay(c.string("OS.Browser.BrowseObjectTypes")) %></b></NOBR><BR></FONT>
<FONT face="Helvetica,Arial" size=-4><NOBR><%= c.toDisplay(c.string("OS.Browser.AdvancedTypeHint")) %></NOBR><BR></FONT>
<table width="100%" bgcolor="#EFEEE9" style="margin-top:5; border-style:solid; border-width:1; border-color:#BFBBB2;">
<tr><td>
   <table width="100%" cellpadding=0 cellspacing=0>
      <script>
         top.showAdvancedTypes();
      </script>
   </table>
</td></tr>
</table>
</div>

<script>
   if ((top.m_numTypes>0) && (top.m_simpleBrowsingOnly == "false"))
   {
      document.writeln('<BR>');
      document.writeln('<FONT face="Helvetica,Arial" size=-4>');
      document.writeln('<span id=AdvancedSearchLink style="display:inline"><a href="#" title="<%= c.toTag(c.string("OS.Browser.AdvancedSelectionTip")) %>" onClick="javascript:top.setAdvSelMode(\'true\')"><%= c.toDisplay(c.string("OS.Browser.BrowseAdvanced")) %></a></span>');
      document.writeln('<span id=SimpleSearchLink style="display:none"><a href="#" title="<%= c.toTag(c.string("OS.Browser.SimpleSelectionTip")) %>" onClick="javascript:top.setAdvSelMode(\'false\')"><%= c.toDisplay(c.string("OS.Browser.BrowseSimple")) %></a></span>');
      document.writeln('</FONT>');
	  document.writeln('<BR>');
	  top.setAdvSelMode(top.m_advSel);
   }
   else
   {
      document.getElementById("SimpleCriterion").style.display="block";
      document.writeln('<BR>');   
   }
</script>
<center>
<table width="100%" cellpadding=0 cellspacing=0 border=0>
	   <tr>
	      <td>
	         <span id="QueryLinks" style="display:none">
	         <FONT face="Helvetica,Arial" size=-4><a href="#" title="<%= c.toTag(c.string("OS.LoadQueryAltText")) %>" onclick="top.doLoadQuery('Query.osLoadQuery');"><%= c.toDisplay(c.string("OS.LoadQueryLink")) %></a></font>
	         <br>
	         <FONT face="Helvetica,Arial" size=-4><a href="#" title="<%= c.toTag(c.string("OS.SaveQueryAltText")) %>" onclick="top.doSaveQuery(osSaveQuery());"><%= c.toDisplay(c.string("OS.SaveQueryLink")) %></a></font>
	         </span>
	      </td>
	   </tr>
   <tr height=10px><td></td></tr>
   <tr>
      <td align=center>
         <INPUT type=image id="doSearch" alt="<%= c.toTag(c.string("Button.Apply.alt")) %>" title="<%= c.toTag(c.string("Button.Apply.alt")) %>" src="<%= c.getModulesUrl() %>/dev/images/<%= c.toTag(c.string("Button.Apply.image")) %>" border=0>
      </td>
   </tr>
</table>
</center>
<script>
   if(top.m_simpleBrowsingOnly == "true")
   {
      document.writeln('<BR>');
      document.writeln('<BR>');
      document.writeln('<FONT face="Helvetica,Arial" size=-4>');
      document.writeln('<%= c.toDisplay(c.string("OS.FileSystem.BrowseOnly")) %>');
      document.writeln('</FONT>');
	  document.writeln('<BR>');
   }
   
   if ((top.m_numTypes>0) && (top.m_simpleBrowsingOnly == "false"))
      document.getElementById("QueryLinks").style.display="inline";
</script>
<span id=PostData>
   <!-- doSearch will use innerHTML to dynamically create the input fields here -->
</span>
</form>
</BODY><HTML>
