<%@  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>SearchQuery</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" />

<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 displaySearching()
   {
      //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(); 
      
      //clear results window
      top.Results.document.open();
      top.Results.document.writeln('<html><head><title>SearchResults</title></head>');
      if (MS55)
         top.Results.document.writeln('<body bgcolor="#FFFFFF" style="border-style:solid; border-width:1; border-bottom-width:0; border-color:#BFBBB2;">');
      else
         top.Results.document.writeln('<body bgcolor="#FFFFFF">');
      top.Results.document.writeln('<div id=searchingDiv style="display:inline">');
      top.Results.document.writeln('<font face="Helvetica,Arial" size=-1 color=red><b><%= c.toDisplay(c.toScript(c.string("OS.Browser.Searching"))) %></b></font>');
      top.Results.document.writeln('</div></body></html>');
      top.Results.document.close();

      //make Searching... blink
      blinkSearching(-1);
   }

   var waitCnt = 20;
   var blinkVisibility = "none";
   function blinkSearching(cnt)
   {
      if (cnt==-1)
      {
         //initialize blinking
         top.resultsLoaded=false;
         waitCnt=20;
         cnt=0;
      }
      if (!top.resultsLoaded)
      {
         if (cnt<waitCnt)
         {
            //wait another .1 sec
            cnt++;
            setTimeout("blinkSearching("+cnt+")",100);
         }
         else
         {
            //waited long enough; change blink state
            blinkVisibility=((blinkVisibility=="none")?"inline":"none");
            waitCnt = ((blinkVisibility=="none")?5:20);
            setTimeout("blinkSearching(0)",100);   //wait .1 sec
            try
            {
               //change display
               top.Results.document.getElementById("searchingDiv").style.display=blinkVisibility;
            }
            catch (e)
            {
            }
         }
      }
   }

   function doGenericSearch()
   {
      displaySearching();

      //update global variables
      //var tmp = document.SearchQuery.SQNameFilter.value;
      //if ((tmp != null) && (tmp.length != 0))
      //{
      //   top.m_nameFilter = tmp;
      //}
      //top.m_container = getContext();
      //top.m_searchSubContainers = ((document.SearchQuery.SQSubContainer.checked)?"true":"false");

      updateGlobalVars();
      doSearch();
   }

   function doAdvancedSearch()
   {
      //alert("doing an advanced search...");
      displaySearching();

      //update global variables
      //top.m_container = getContext();
      //top.m_searchSubContainers = ((document.SearchQuery.SQSubContainer.checked)?"true":"false");

      updateGlobalVars();
      doSearch();
   }

   function doSearch()
   {
      //create action command
      var tmp = ((top.m_multiSelect=="true")?"MS":"SS");
      var strURL = "webacc?taskId=fw.ObjectSelector&merge=fw.OS.SearchResults"+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.SearchSubContainers" value="'+top.m_searchSubContainers+'">\n';
      strHTML += '<input type=hidden name="OS.ShowSubClasses" value="'     +top.m_showSubClasses+'">\n';
      strHTML += '<input type=hidden name="OS.SearchOnStartup" value="'    +top.m_searchOnStartup+'">\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.SearchQuery.action = strURL;
      document.getElementById("PostData").innerHTML = strHTML;
      document.SearchQuery.submit();
   }

   function getContext()
   {
      var strContext = document.SearchQuery.SQContext.value;
      if (strContext == null || strContext.length==0)
      {
         strContext = "<%= c.toScript(c.string("OS.Browser.RootName")) %>";
         document.SearchQuery.SQContext.value = strContext;
      }
      return strContext;
   }
   
   //called whenever context, scSearch or namefilter gui fields change
   function updateGlobalVars()
   {
      top.m_container = getContext();
      top.m_searchSubContainers = ((document.SearchQuery.SQSubContainer.checked)?"true":"false");
      top.m_nameFilter = document.SearchQuery.SQNameFilter.value;
      if (top.m_nameFilter==null || top.m_nameFilter.length==0)
      {
         top.m_nameFilter = "*";
         document.SearchQuery.SQNameFilter.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.SearchQuery.SQContext.value=o.container;
      
      //retrieve subcontainer search mode
      top.m_searchSubContainers = o.subcontainerSearch;
      document.SearchQuery.SQSubContainer.checked=(top.m_searchSubContainers=="true");
      
      //retrieve name filter
      top.m_nameFilter=xmlDecode(o.nameFilter);
      document.SearchQuery.SQNameFilter.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=top.m_searchSubContainers;
      p.nameFilter=top.m_nameFilter;
      p.typeFilter=top.getSelectedTypes(top.isAdvSelMode());
      var criteria = new Array();
      var cnt = 0;
      var critNames = "";
      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
            critNames += "<ctype>"+top.m_types[i][1]+"</ctype>";
            criteria[cnt]=top.m_types[i][2];
            cnt++;
         }
      }
      p.criteria=criteria;
      p.id="OS";
      //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.SearchQuery.SQContext.value=top.m_container;

            //initialized checked state of subcontainer search
            document.SearchQuery.SQSubContainer.checked=(top.m_searchSubContainers=="true");

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

            //do an initial search?
            if (top.m_searchOnStartup=="true")
            {
               top.doSearch();
               top.m_searchOnStartup="false";   //only once
            }

            // set focus to context entry well
            document.SearchQuery.SQContext.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" onLoad="top.queryLoaded=true; queryOnLoad(0)" onFocus="top.ensureOpenerValid();" onDragStart="iman.cancelEvent(event)"-->
<form name="SearchQuery" method=post action="webacc" target="Results" onSubmit="javascript:top.processDisplayCountChange();top.doSearch();return false">
<script>
   if (MS55) document.writeln('<br>');
</script>
<NOBR><FONT face="Helvetica,Arial" size=-1><b><%= c.toDisplay(c.string("OS.Browser.SearchFrom")) %></b></NOBR><BR>
<input style="width:100%" type=text name="SQContext" value="" onchange="updateGlobalVars();"><BR><NOBR>
</FONT>
<FONT face="Helvetica,Arial" size=-4>
<%= c.toDisplay(c.string("OS.Browser.SearchFromEx")) %></NOBR><BR>
</FONT>
<FONT face="Helvetica,Arial" size=-1>
<NOBR>
<input type=checkbox name="SQSubContainer" onchange="updateGlobalVars();"> <%= c.toDisplay(c.string("OS.Browser.SearchSubContainer")) %></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.SearchFor")) %></b></NOBR><BR>
<input style="width:100%" type=text name="SQNameFilter" value="" maxlength="128" onchange="updateGlobalVars();"><BR>
<NOBR></FONT>
<FONT face="Helvetica,Arial" size=-4>
<%= c.toDisplay(c.string("OS.Browser.SearchForEx")) %></NOBR>
</FONT>

<BR><BR>
<FONT face="Helvetica,Arial" size=-1><NOBR><b><%= c.toDisplay(c.string("OS.Browser.SearchObjectTypes")) %></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.SearchObjectTypes")) %></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)
   {
      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.SearchAdvanced")) %></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.SearchSimple")) %></a></span>');
      document.writeln('</FONT>');
      document.writeln('<BR>');
      top.setAdvSelMode(top.m_advSel);
   }
   else
   {
      document.getElementById("SimpleCriterion").style.display="block";
   }
</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.Search.alt")) %>" title="<%= c.toTag(c.string("Button.Search.alt")) %>" src="<%= c.getModulesUrl() %>/dev/images/<%= c.toTag(c.string("Button.Search.image")) %>" border=0>
      </td>
   </tr>
</table>
</center>
<script>
   if (top.m_numTypes>0)
      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>
