<%@  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" />
<jsp:include page='<%= c.getPath("dev/XmlScripts_inc.jsp") %>' flush="true" />

<script language="JavaScript">
   //------------- find OVTop ----------------
   var OVTop=top.Mainscreen.ObjectView;
   
   //------------ Global Variables ------------------
   
   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
      OVTop.ResultButtons.document.open();
      if (MS55)
         OVTop.ResultButtons.document.writeln('<HTML><HEAD></HEAD><BODY bgcolor="#EFEEE9" style="border-style:solid; border-width:1; border-color:#BFBBB2;"></BODY>');
      else
         OVTop.ResultButtons.document.writeln('<HTML><HEAD></HEAD><BODY bgcolor="#EFEEE9"></BODY>');
      OVTop.ResultButtons.document.close();
      
      //clear results window
//      if(!MS55){
       OVTop.Results.document.open();
       OVTop.Results.document.writeln('<html><head><title>SearchResults</title></head>');
       if (MS55)
         OVTop.Results.document.writeln('<body bgcolor="#FFFFFF" style="border-style:solid; border-width:1; border-bottom-width:0; border-color:#BFBBB2;">');
       else
         OVTop.Results.document.writeln('<body bgcolor="#FFFFFF">');
       OVTop.Results.document.writeln('<div id=searchingDiv style="display:inline">');
       OVTop.Results.document.writeln('<font face="Helvetica,Arial" size=-1 color=red><b><%= c.toScript(c.string("OV.Browser.Searching")) %></b></font>');
       OVTop.Results.document.writeln('</div></body></html>');
       OVTop.Results.document.close();
      
      //make Searching... blink
      blinkSearching(-1);
//    }
   }
   
   var waitCnt = 20;
   var blinkVisibility = "none";
   function blinkSearching(cnt)
   {
      if (cnt==-1)
      {
         //initialize blinking
         OVTop.resultsLoaded=false;
         waitCnt=20;
         cnt=0;
      }
      if (!OVTop.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
               OVTop.Results.document.getElementById("searchingDiv").style.display=blinkVisibility;
            }
            catch (e)
            {
            }
         }
      }
   }
      
   function doGenericSearch()
   {   
      displaySearching();
      updateGlobalVars();
      //update global variables
      //var tmp = document.SearchQuery.SQNameFilter.value;
      //if ((tmp != null) && (tmp.length != 0))
      //{
      //   OVTop.m_nameFilter = tmp;
      //}
      //OVTop.m_container = getContext();
      //OVTop.m_searchSubContainers = ((document.SearchQuery.SQSubContainer.checked)?"true":"false");
   
      doSearch();
   }
   
   function doAdvancedSearch()
   {
      //alert("doing an advanced search...");
      displaySearching();
      updateGlobalVars();
      
      //update global variables
      //OVTop.m_container = getContext();
      //OVTop.m_searchSubContainers = ((document.SearchQuery.SQSubContainer.checked)?"true":"false");
      
      doSearch();
   }
   
   function doSearch()
   {
      //create action command
      var tmp = ((OVTop.m_multiSelect=="true")?"MS":"SS");
      var strURL = "webacc?taskId=fw.ObjectView&merge=fw.OV.SearchResults"+tmp+"&error=dev.GenErr&SearchToken=SearchTokenPost";
      
      //create dynamic HTML input fields
      var strHTML="";
      strHTML  = '<input type=hidden name="User.context" value="'          +OVTop.m_userContext+'">\n';
      strHTML += '<input type=hidden name="Search.ContextId" value="'      +toTag(OVTop.m_searchContextId)+'">\n';
      strHTML += '<input type=hidden name="OV.AdvancedSelection" value="'  +OVTop.m_advSel+'">\n';
      strHTML += '<input type=hidden name="OV.Container" value="'          +toTag(OVTop.m_container)+'">\n';
      strHTML += '<input type=hidden name="OV.Mode" value="'               +OVTop.m_mode+'">\n';
      strHTML += '<input type=hidden name="OV.ResultsPerPage" value="'     +OVTop.m_resultsPerPage+'">\n';
      strHTML += '<input type=hidden name="OV.SearchSubContainers" value="'+OVTop.m_searchSubContainers+'">\n';
      strHTML += '<input type=hidden name="OV.SearchOnStartup" value="'    +OVTop.m_searchOnStartup+'">\n';
      
      var bAdvanced = OVTop.isAdvSelMode();
      strHTML += '<input type=hidden name="OV.TypeFilter" value="'         +toTag(OVTop.getSelectedTypes(bAdvanced))+ '">\n';
      if (bAdvanced)
      {
         strHTML += '<input type=hidden name="OV.NameFilter" value="">\n';
         var cnt=0;
         for (var i=0; i<OVTop.m_types.length; i++)
         {
            if ( OVTop.m_types[i][0] &&                                         //type is enabled 
                 OVTop.m_types[i][1]!=null && OVTop.m_types[i][1].length>0  &&    //we have a type name
                 OVTop.m_types[i][2]!=null && OVTop.m_types[i][2].length>0 )      //we have a criterion
            {
               //output advanced criterion
               strHTML += '<input type=hidden name="OV.TypeFilter.'+cnt+'.name" value="'         +toTag(OVTop.m_types[i][1])+ '">\n';
               strHTML += '<input type=hidden name="OV.TypeFilter.'+cnt+'.crit" value="'         +xmlEncode(OVTop.m_types[i][2])+ '">\n';
               cnt++;
            }
         }
         strHTML += '<input type=hidden name="OV.TypeFilter.cnt" value="'         +cnt+ '">\n';
      }
      else
      {
         strHTML += '<input type=hidden name="OV.NameFilter" value="'         +toTag(OVTop.m_nameFilter)+'">\n';
      }

      //alert("doGenericSearch: url = "+strURL);
      //alert("doGenericSearch: html = "+strHTML);
       var queryForm;
       var queryData;
       queryForm = OVTop.Results.document.getElementById("PostData");
       if(queryForm){
        iman.debugOV("queryForm found with OVTop.Results.document.getElementById(\"PostData\")");
        queryForm.innerHTML = document.SearchQuery.innerHTML;
        queryOnLoadSearch=false;
       }else{
       	if(OVTop.frames['Results'].document.getElementById("PostData")){
	     iman.debugOV("queryForm found with OVTop.resultsFrame.document.getElementById(\"PostData\")");
         queryForm = OVTop.frames['Results'].document.getElementById("PostData");
        }else{
         iman.debugOV("queryForm = document.SearchQuery");
         queryForm = document.SearchQuery;
        }
       }
        queryData = queryForm.ownerDocument.getElementById("PostData");
      
      queryForm.action=strURL;
      queryData.innerHTML=strHTML;
      queryForm.submit();
//      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.string("OV.Browser.RootName") %>";
         document.SearchQuery.SQContext.value = strContext;  
      }
      return strContext;
   }
   
   function updateGlobalVars()
   {
		OVTop.m_container = getContext();
      OVTop.m_searchSubContainers = ((document.SearchQuery.SQSubContainer.checked)?"true":"false");
      OVTop.m_nameFilter = document.SearchQuery.SQNameFilter.value;
      if (OVTop.m_nameFilter==null || OVTop.m_nameFilter.length==0)
      {
         OVTop.m_nameFilter = "*";
         document.SearchQuery.SQNameFilter.value = "*";
      }
		OVTop.m_selectedType = document.getElementById("typesel").value;
   }
   
   function isAllTypesMode()
   {
      return (OVTop.m_selectedType=="<%= c.toScript(c.string("OV.Browser.AllTypes")) %>");
   }
   
   //-------------------- load/save query -------------------------
   //called by doLoadQuery in QueryScripts.js
   function ovLoadQuery(o)
   {
      //debug dump of object o
      //dumpObject("ovLoadQuery",o);
      
      //retrieve context
      OVTop.m_container=o.container;
      document.SearchQuery.SQContext.value=o.container;
      
      //retrieve subcontainer search mode
      OVTop.m_searchSubContainers = o.subcontainerSearch;
      document.SearchQuery.SQSubContainer.checked=(OVTop.m_searchSubContainers=="true");
      
      //retrieve name filter
      OVTop.m_nameFilter=xmlDecode(o.nameFilter);
      document.SearchQuery.SQNameFilter.value=xmlDecode(o.nameFilter);
      
      OVTop.clearAllCriterion();          //clear all search criteria
      OVTop.setAdvSelMode('false');       //assume simple selection mode
      if (o.typeFilter!=null && o.typeFilter.length>0)
      {
         //enable selected type
         document.getElementById("typesel").value=o.typeFilter;
         OVTop.m_selectedType = o.typeFilter;
         OVTop.enableJustOneType(o.typeFilter);
      }
      else
      {
         OVTop.setEnableModeAllTypes(true);     //enable all types
      }
      
      //retrieve and set advanced type criteria
      //right now only using the first criterion and first type in criterion
      var criteria = o.criteria;
      if (criteria!=null && criteria.length>0)
      {
         //get type names from first xml criteria
         var typeNames = OVTop.getCriteriaTypes(criteria[0]);
         if (typeNames!=null && typeNames.length>0)
         {
            //select the type in the list
            document.getElementById("typesel").value=typeNames[0];
            OVTop.m_selectedType = typeNames[0];
            OVTop.enableJustOneType(typeNames[0]);
            
            //assign only first criteria 
            OVTop.saveCriterionArray(typeNames, criteria[0]);
         }
      }
      else
      {
         //refresh gui
         OVTop.configureTypeGUI();
         OVTop.configureNameGUI();
      }
   }
   
   //build a query object and return it to doSaveQuery in QueryScripts.js
   function ovSaveQuery()
   {
      //build query object
      var p = new Object();
      p.container=OVTop.m_container;
      p.subcontainerSearch=OVTop.m_searchSubContainers;
      p.nameFilter=OVTop.m_nameFilter;
      p.typeFilter=OVTop.getSelectedTypes(OVTop.isAdvSelMode());
      var criteria = new Array();
      var cnt = 0;
      for (var i=0; i<OVTop.m_types.length; i++)
      {
         if ( OVTop.m_types[i][0] &&                                         //type is enabled
              OVTop.m_types[i][1]!=null && OVTop.m_types[i][1].length>0  &&    //we have a type name
              OVTop.m_types[i][2]!=null && OVTop.m_types[i][2].length>0 )      //we have a criterion
         {
            //output advanced criterion
            criteria[cnt]=OVTop.m_types[i][2];
            cnt++;
         }
      }
      p.criteria=criteria;
      p.id="OV";
      //dumpObject("ovSaveQuery",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 (OVTop.resultsLoaded==false ||
             OVTop.titleLoaded==false)
         {
            cnt++;
            t = setTimeout("queryOnLoad("+cnt+")",100)  
         }
         else
         {
            if (t!=null)
            {
               clearTimeout(t);
            }
            
            //initialize container input
            if (OVTop.m_container=="") 
            {
               OVTop.m_container="<%= c.string("OV.Browser.RootName") %>";
            }
            document.SearchQuery.SQContext.value=OVTop.m_container;
            
            //initialized checked state of subcontainer search
            document.SearchQuery.SQSubContainer.checked=(OVTop.m_searchSubContainers=="true");
      
            //initialize name filter input
            if (OVTop.m_nameFilter=="")
            {
               OVTop.m_nameFilter="*";
            }
            document.SearchQuery.SQNameFilter.value=OVTop.m_nameFilter;
            
            // do an initial search?
            if (OVTop.m_searchOnStartup=="true")
            { 
               OVTop.doSearch();
               OVTop.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="'+OVTop.OV_BGCOLOR+'" onLoad="OVTop.queryLoaded=true; queryOnLoad(0);">');</script>
<form name="SearchQuery" method=post action="webacc" target="Results" onSubmit="javascript:OVTop.processDisplayCountChange();OVTop.doSearch();return false">
<span id=PostData>
   <!-- doSearch will use innerHTML to dynamically create the input fields here -->
</span>
<div style="width:100%;position:absolute;top:9;">
<table style="bottom-margin:0"border=0 cellspacing=0 cellpadding=0 width="100%">
	<tr>
      <td style="font-family:Trebuchet MS,Helvetica,Arial; font-size:12; font-weight:bold"><NOBR><%= c.string("OV.Browser.SearchFrom") %>&nbsp;&nbsp;</NOBR></td>
      <td style="width:100%"><input style="font-size:11px;padding-left:4;width:100%" type=text name="SQContext" value="" onChange="updateGlobalVars()"></td>
	</tr>
	<tr>
		<td style="height:5px"></td>
      <td style="font-family:Trebuchet MS,Helvetica,Arial; font-size:11; font-weight:bold; padding-left:5;"><input type=checkbox name="SQSubContainer" onChange="updateGlobalVars()"><%= c.string("OV.Browser.SearchSubContainer") %></td>
	</tr>
   
   <tr style="height:2px"><td></td></tr>
   
   <script>
      if (NN6)
      {
         document.writeln('<tr>');
         document.writeln('   <td id="SimpleCriterionLabel" style="font-family:Trebuchet MS,Helvetica,Arial;font-size:12;font-weight:bold"><NOBR><%= c.string("OV.Browser.BrowseFor") %>&nbsp;&nbsp;</NOBR></td>');
         document.writeln('   <td id="SimpleCriterion" style="width:100%;display:none"><input style="font-size:11px;padding-left:4;width:100%" type=text name="SQNameFilter" value="" maxlength="128" onChange="javascript:updateSearchCriterion()"></td>');
         document.writeln('</tr>');
      }
      else
      {
         document.writeln('<tr id="SimpleCriterion" style="display:none">');
         document.writeln('   <td style="font-family:Trebuchet MS,Helvetica,Arial;font-size:12;font-weight:bold"><NOBR><%= c.string("OV.Browser.BrowseFor") %>&nbsp;&nbsp;</NOBR></td>');
         document.writeln('   <td style="width:100%"><input style="font-size:11px;padding-left:4;width:100%" type=text name="SQNameFilter" value="" maxlength="128" onChange="updateGlobalVars()"></td>');
         document.writeln('</tr>');
      }
   </script>
   
   <tr style="height:2px"><td></td></tr>
   
   <tr style="width:100%">
      <td style="font-family:Trebuchet MS,Helvetica,Arial; font-size:12; font-weight:bold"><NOBR><%= c.string("OV.Browser.SearchObjectTypes") %></NOBR></td>
      <td>
         <nobr>
         <a id=minus style="display:none" href="#" onClick="javascript:OVTop.clearASCriterion()"><IMG src="<%= c.getModulesUrl() %>/fw/images/<%= c.string("Button.Minus.image") %>" alt="<%= c.string("OV.Browser.ClearAdvSelCriterion") %>" border=0></a>
         <a id=plus style="display:none" href="#" onClick="javascript:OVTop.enterASCriterion()"><IMG src="<%= c.getModulesUrl() %>/fw/images/<%= c.string("Button.Plus.image") %>" alt="<%= c.string("OV.Browser.EnterAdvSelCriterion") %>" border=0></a>
         <select id=typesel style="font-size:11px;padding-left:4;width:100%" onChange="javascript:OVTop.processTypeSelection()">
            <option style="font-size:9" value="<%= c.string("OV.Browser.AllTypes") %>"><%= c.string("OV.Browser.AllTypes") %>
            <script>
		      for(i=0; i<OVTop.m_types.length; i++)
		      {
		      	 if((OVTop.m_types[i][1] == "Directory") || (OVTop.m_types[i][1] == "File"))
		      	 {
		      	 	// Remove File and Directory from the list, cannot search for these types
		      	 	continue;
		      	 }
		      	 else
		      	 {
			         if (OVTop.m_selectedType==OVTop.m_types[i][1])
			         {
			            document.writeln('<option style="font-size:9" value="'+OVTop.m_types[i][1]+'" selected>'+OVTop.m_displayTypes[i]);
			         }
			         else
			         {
			      	   document.writeln('<option style="font-size:9" value="'+OVTop.m_types[i][1]+'">'+OVTop.m_displayTypes[i]);
			      	}
			  	}
		      }
		      OVTop.configureTypeGUI();
		      OVTop.configureNameGUI();
		      </script>
		   </select>
		</nobr>
       </td>
   </tr>
   
   <tr style="height:5px"><td></td></tr>
</table>
<center>
<table width="100%" cellpadding=0 cellspacing=0 border=0>
   <tr>
      <td align=right valign=middle>
         <FONT face="Helvetica,Arial" size=-4><a href="#" title="<%= c.toTag(c.string("OV.LoadQueryAltText")) %>" onclick="OVTop.doLoadQuery('Query.ovLoadQuery');"><%= c.toDisplay(c.string("OV.LoadQueryLink")) %></a></font>
      </td>
      <td width=120 align=center valign=middle>
         <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.string("Button.Search.image") %>" border=0>
      </td>
      <td align=left valign=middle>
         <FONT face="Helvetica,Arial" size=-4><a href="#" title="<%= c.toTag(c.string("OV.SaveQueryAltText")) %>" onclick="OVTop.doSaveQuery(ovSaveQuery());"><%= c.toDisplay(c.string("OV.SaveQueryLink")) %></a></font>
      </td>
   </tr>
</table>
</center>
</div>
</form>
</BODY><HTML>
