<%@  page pageEncoding="utf-8" contentType="text/html;charset=utf-8" import="com.novell.webaccess.common.JSPConduit2, com.novell.emframe.dev.eMFrameUtils" isELIgnored="false"%>
<%   JSPConduit2 c = JSPConduit2.getJSPConduit(request); %>
<%   c.stringTable("DevResources"); %>
<%   c.stringTable("FwResources"); %>
<!--BrowseQuery.jsp-->
<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">
   //------------- find OVTop ----------------
   var OVTop=top.Mainscreen.ObjectView;
   
   //------------- Global XML vars -------------------
   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))
      //{
      //   OVTop.m_nameFilter = tmp;
      //}
      //OVTop.m_container = getContext();
   
      iman.debugOV("in BrowseQuery.doGenericSearch() calling updateGlobalVars()");
      updateGlobalVars();
      iman.debugOV("in BrowseQuery.doGenericSearch() calling doSearch()");
      doSearch();
   }
   
   function doAdvancedSearch()
   {
      //alert("doing an advanced search...");
      //update global variables
      //OVTop.m_container = getContext();
      
      updateGlobalVars();
      doSearch();
   }
   
   var blinking = false;
   var blinkVisibility = "";
   var waitCnt = 30;
   function clearResults(cnt)
   {
      iman.debugOV("in  BrowseQuery::clearResults() cnt="+cnt);
      if (cnt==-1)
      {
         //initialize clearResults
         OVTop.resultsLoaded =   false; 
         blinking =              false;
         waitCnt =               30;      // initial 3 second wait
         cnt =                   0;
      }
      
      //do we have new results?
      if (OVTop.resultsLoaded)
      {
         iman.debugOV("in BrowseQuery::clearResults() results have been loaded!  Make sure results are visible");
         OVTop.Results.document.getElementById("resultsDiv").style.display="inline";
         OVTop.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
               iman.debugOV("making browsing message visible");
               OVTop.Results.document.getElementById("resultsDiv").style.display="none";
               OVTop.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
               OVTop.Results.document.getElementById("browsingDiv").style.display=blinkVisibility;
            }
            catch (e)
            {
            }
         }
         
         iman.debugOV("restart the timer check again in .1 sec");
         setTimeout("clearResults(0)",100);   //check again in .1 sec
      }
   }
   
   function clearResultButtons()
   {
      //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();
   }
   
   function doSearch()
   {
      iman.debugOV("in BrowseQuery.doSearch() calling clearResultButtons()");
      clearResultButtons();
      iman.debugOV("in BrowseQuery.doSearch() calling clearResults(-1)");
      clearResults(-1);  //clear results after 3 seconds
      
      //create action command
      var tmp = ((OVTop.m_multiSelect=="true")?"MS":"SS");
      var strURL = "webacc?taskId=fw.ObjectView&merge=fw.OV.BrowseResults"+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="false">\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';
      }

      var queryForm;
      var queryData;
       queryForm = OVTop.Results.document.getElementById("PostData");
       if(queryForm){
       queryForm.innerHTML = document.BrowseQuery.innerHTML;
       queryOnLoadSearch=false;
       }else{
        queryForm = document.BrowseQuery;
       }
        queryData = queryForm.ownerDocument.getElementById("PostData");
      
       queryForm.action=strURL;
       queryData.innerHTML=strHTML;
		queryForm.submit();
//      iman.debugOV("BrowseQuery::doGenericSearch: url = "+strURL);
//      iman.debugOV("BrowseQuery::doSearch: calling document.BrowseQuery.submit()\n action="+document.BrowseQuery.action+",\n innerHTML= "+strHTML+",\n target="+document.BrowseQuery.target);
//      document.getElementById("PostData").innerHTML = strHTML;
//      document.BrowseQuery.action = strURL;
//  	  document.BrowseQuery.submit();
   }
   
   function getContext()
   {
      var strContext = document.BrowseQuery.BQContext.value;
      if (strContext == null || strContext.length==0)
      {
         strContext = "<%= c.string("OV.Browser.RootName") %>";
         document.BrowseQuery.BQContext.value = strContext;  
      }
      return strContext;
   }
   
   function updateGlobalVars()
   {
   		iman.debugOV("in BrowseQuery::updateGlobalVars()");
		OVTop.m_container = getContext();
      OVTop.m_nameFilter = document.BrowseQuery.BQNameFilter.value;
      if (OVTop.m_nameFilter==null || OVTop.m_nameFilter.length==0)
      {
         OVTop.m_nameFilter = "*";
         document.BrowseQuery.BQNameFilter.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.BrowseQuery.BQContext.value=o.container;
      
      //retrieve name filter
      OVTop.m_nameFilter=xmlDecode(o.nameFilter);
      document.BrowseQuery.BQNameFilter.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();
         OVTop.configureLoadSave();
      }
   }
   
   //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="false";
      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;
   var queryOnLoadSearch = false;
   function queryOnLoad(cnt)
   {
      //wait up to 10 seconds for the Results frame to load
      iman.debugOV("in BrowseQuery:queryOnLoad() cnt="+cnt); 
      if (cnt<100)
      {
         if (OVTop.resultsLoaded==false ||
             OVTop.titleLoaded==false)
         {
            cnt++;
            t = setTimeout("queryOnLoad("+cnt+")",100)  
         }
         else
         {
            iman.debugOV("in BrowseQuery:queryOnLoad() results and title loaded."); 
            
            if (t!=null)
            {
	            iman.debugOV("in BrowseQuery:queryOnLoad() clearTimeout("+t+")"); 
               clearTimeout(t);
            }
            
            //initialize container input
            if (OVTop.m_container=="") 
            {
               OVTop.m_container="<%= c.string("OV.Browser.RootName") %>";
               iman.debugOV("in BrowseQuery:queryOnLoad() OVTop.m_container is empty setting to:"+OVTop.m_container); 
               
            }
            document.BrowseQuery.BQContext.value=OVTop.m_container;
      
            //initialize name filter input
            if (OVTop.m_nameFilter=="")
            {
               OVTop.m_nameFilter="*";
               iman.debugOV("in BrowseQuery:queryOnLoad() OVTop.m_nameFilter is empty setting to:"+OVTop.m_nameFilter); 
               
            }
            document.BrowseQuery.BQNameFilter.value=OVTop.m_nameFilter;
            
            // do an initial search
            iman.debugOV("in BrowseQuery:queryOnLoad() calling OVTop.doSearch()"); 
            queryOnLoadSearch = true;
            OVTop.doSearch();
   
            // set focus to context entry well
            document.BrowseQuery.BQContext.focus();
         }
      }else{
	      iman.debugOV("in BrowseQuery:queryOnLoad() timeout has been reached."); 
      }
   }
</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>
<!--<BODY TEXT="#000000" LINK="#000000" VLINK="#000000" ALINK="#000000" bgcolor="${(empty(OV_BGColor))?'#EDEEEC':OV_BGColor}" onLoad="javascript:OVTop.queryLoaded=true;queryOnLoad(0)">-->
<form name="BrowseQuery" id="BrowseQuery" 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.BrowseFrom") %>&nbsp;&nbsp;</NOBR></td>
      <td style="width:96%"><input style="font-size:11px;padding-left:4;width:96%" type=text name="BQContext" value="" onChange="updateGlobalVars();"></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:96%;display:none"><input style="font-size:11px;padding-left:4;width:96%" type=text name="BQNameFilter" value="" maxlength="128" onChange="updateGlobalVars()"></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:96%"><input style="font-size:11px;padding-left:4;width:96%" type=text name="BQNameFilter" value="" maxlength="128" onChange="updateGlobalVars()"></td>');
         document.writeln('</tr>');
      }
   </script>
   <tr style="height:2px"><td></td></tr>
   <tr>
      <td style="font-family:Trebuchet MS,Helvetica,Arial; font-size:12; font-weight:bold"><NOBR><%= c.string("OV.Browser.BrowseObjectTypes") %></NOBR></td>
      <td style="vertical-align:middle">
      <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:11;padding-left:4;width:96%" onChange="OVTop.processTypeSelection()">
            <option style="font-size:9" value="*"><%= c.string("OV.Browser.AllTypes") %>
            <script>
		      for(i=0; i<OVTop.m_types.length; i++)
		      {
		         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 width="25%" align=right valign=middle>
         <FONT face="Helvetica,Arial" size=-4><a  id=load style="display:none" href="#" title="<%= c.toTag(c.string("OV.LoadQueryAltText")) %>" onclick="OVTop.doLoadQuery('Query.ovLoadQuery');"><%= c.toDisplay(c.string("OV.LoadQueryLink")) %></a></font>
      </td>
      <td width="50%" align=center valign=middle>
         <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.string("Button.Apply.image") %>" border=0>
      </td>
      <td width="25%" align=left valign=middle>
         <FONT face="Helvetica,Arial" size=-4><a id=save style="display:none" href="#" title="<%= c.toTag(c.string("OV.SaveQueryAltText")) %>" onclick="OVTop.doSaveQuery(ovSaveQuery());"><%= c.toDisplay(c.string("OV.SaveQueryLink")) %></a></font>
      </td>
      <script>
      		OVTop.configureLoadSave();
      </script>
   </tr>
</table>
</center>
</div>
</form>
</BODY><HTML>
