<?
echo `
<script language=javascript>
function AllShow()
{
//alert(~AllShow started~);
//   document.getElementById(~bibdiv~).innerHTML = ~Updated text~;
   document.getElementById(~fildiv~).style.display =~none~;
   document.getElementById(~tabdiv~).style.display =~none~;
   document.getElementById(~issdiv~).style.display =~none~;
   document.getElementById(~bibdiv~).style.display =~none~;
   document.getElementById(~alldiv~).style.display =~block~;
}
function BibShow()
{
//alert(~BibShow started~);
//   document.getElementById(~bibdiv~).innerHTML = ~Updated text~;
   document.getElementById(~fildiv~).style.display =~none~;
   document.getElementById(~tabdiv~).style.display =~none~;
   document.getElementById(~issdiv~).style.display =~none~;
   document.getElementById(~bibdiv~).style.display =~block~;
   document.getElementById(~alldiv~).style.display =~none~;
}
function TabShow()
{
//alert(~TabShow started~);
//   document.getElementById(~bibdiv~).innerHTML = ~Updated text~;
   document.getElementById(~fildiv~).style.display =~none~;
   document.getElementById(~tabdiv~).style.display =~block~;
   document.getElementById(~issdiv~).style.display =~none~;
   document.getElementById(~bibdiv~).style.display =~none~;
   document.getElementById(~alldiv~).style.display =~none~;
}
function FilShow()
{
//alert(~FilShow started~);
//   document.getElementById(~bibdiv~).innerHTML = ~Updated text~;
   document.getElementById(~fildiv~).style.display =~block~;
   document.getElementById(~tabdiv~).style.display =~none~;
   document.getElementById(~issdiv~).style.display =~none~;
   document.getElementById(~bibdiv~).style.display =~none~;
   document.getElementById(~alldiv~).style.display =~none~;
}
function IssShow()
{
//alert(~IssShow started~);
//   document.getElementById(~bibdiv~).innerHTML = ~Updated text~;
   document.getElementById(~fildiv~).style.display =~none~;
   document.getElementById(~tabdiv~).style.display =~none~;
   document.getElementById(~issdiv~).style.display =~block~;
   document.getElementById(~bibdiv~).style.display =~none~;
   document.getElementById(~alldiv~).style.display =~none~;
}</script>
`;

$TABLEBGCOLOUR=~#FFBF00~;
$TABLEFGCOLOUR=~#333333~;
$CELLBGCOLOUR=~#fffff8~;
$CELLFGCOLOUR=~BLACK~;
$TABLEBORDER=1;

$CELLPADDING=5;
$CELLSPACING=1;

$_REQUEST[`nowiki`]=1;
$txtclass=`tdpinkgreenS`;

$clean=$_REQUEST[`clean`];

$ProjectId=$_SESSION[`ProjectId`];

$whit=$_SESSION[`PM_wHit`];

$str = $_REQUEST[~lsstr~];
$Sstr = $_SESSION[~Sstring~];

if (~$str~==``) 
  {
    echo `
<body onload=~javascript:GetSrchStr();~>
    `;
  }
else
  {
     if ($str==`null`) die();
     $_SESSION[~Sstring~]=$str;

     $ostr=$str;
     $strs=explode(` `,$str);
     $strx=str_replace(` `,`,`,$str);
  
     ExecuteSQL(~create temporary table tmpfils(Type varchar(20), Item varchar(200), Description mediumtext)~);

     $dir=~../upload/~.$_SESSION[`RootDir`];
     CheckDir($dir,$strs);

     $dir=~../upload/common~;
     CheckDir($dir,$strs);
  
//echo ~<br>Role=$userRole<br>~;
    $NOSORT=1;

    $lnkT=~concat(`<a x=$dq`,TabName,`$dq href=~.$dq.~javascript:PopTab(```,TabName,```,`,TabID,`,$whit);~.$dq.~>`,TabName,`</a>`)~;

    $lnkI=~concat(`<a x=$dq`,IssueID,`$dq href=~.$dq.~javascript:PopIss(`,IssueID,`,$whit);~.$dq.~>`,IssueID,`</a>`)~;

//echo ~<br>lnkI=$lnkI~;

    $lnkB=~concat(`<a x=$dq`,book,chapter,verse,`$dq href=~.$dq.~javascript:PopVrs(```,book,` `,chapter,`:`,verse,```);~.$dq.~>`,book,` `,chapter,`:`,verse,`</a>`)~;

    foreach($strs as $si=>$str)
	{

	    if($si==0)
	      {
	    	$instr1=~instr(Tabs.Description,`$str`)>0~;
	    	$instr2=~instr(Tabs.Title,`$str`)>0~;
	    	$instr3=~instr(Tabs.TabName,`$str`)>0~;
	    	
	    	$instri1=~instr(IssueTitle,`$str`)>0~; 
	    	$instri2=~instr(AssignedDisplayName,`$str`)>0~; 
	    	$instri3=~instr(OwnerDisplayName,`$str`)>0~;
	    	$instri4=~instr(CreatorDisplayName,`$str`)>0~;
		
		$instrb1= ~instr(Bible.text,`$str`)>0~;
	      }
	    else
	      {
	    	$instr1=$instr1.~ and instr(Description,`$str`)>0~;
	    	$instr2=$instr2.~ and instr(Tabs.Title,`$str`)>0~;
	    	$instr3=$instr3.~ and instr(Tabs.TabName,`$str`)>0~;
	    	
	    	$instri1=$instri1.~ and instr(IssueTitle,`$str`)>0~;
	    	$instri2=$instri2.~ and instr(AssignedDisplayName,`$str`)>0~;
	    	$instri3=$instri3.~ and instr(OwnerDisplayName,`$str`)>0~;
	    	$instri4=$instri4.~ and instr(CreatorDisplayName,`$str`)>0~;
	    	
		$instrb1=$instrb1.~ and  instr(Bible.text,`$str`)>0~;
	      }
	}
	
	$instr  =`((`.$instr1.`) or (`.$instr2.`) or (`.$instr3.`))`;
	$instri =`((`.$instri1.`) or (`.$instri2.`) or (`.$instri3.`) or (`.$instri4.`))`;
	$instrb =     $instrb1;
	
    ExecuteSQL(~insert into tmpfils select `Tab` as Type, $lnkT as Item, Title as Description from Tabs where ProjectId in (`$ProjectId`,0) and isDeleted=0 and (AuthorizedRoles=`` or instr(AuthorizedRoles,`$userRole;`)>0) and $instr~);
    ExecuteSQL(~insert into tmpfils select `Issue` as Type, $lnkI as Description, IssueTitle as Description from IssuesView where ProjectId in (`$ProjectId`,0) and $instri~);
    ExecuteSQL(~insert into tmpfils select `Bib` as Type, $lnkB as Item, Bible.text as Description from Bible where $instrb~);

    $cnt   =ShowQueryResultScalar(~select count(*) from tmpfils~);    
    $cntbib=ShowQueryResultScalar(~select count(*) from tmpfils where Type=`Bib`~);    
    $cnttab=ShowQueryResultScalar(~select count(*) from tmpfils where Type=`Tab`~);    
    $cntiss=ShowQueryResultScalar(~select count(*) from tmpfils where Type=`Issue`~);    
    $cntfil=ShowQueryResultScalar(~select count(*) from tmpfils where Type!=`Bib` and Type!=`Tab` and Type!=`Issue`~);    

echo `
<center><table border=0><tr><td align=center class=tdgreenV> 
Search Results for <b><i><font color=red>`,$ostr,`</font></i></b></font>   
<a href=~javascript:GetSrchStr();~ title=~New search~><img src=../images/new.gif height=15px></a>
<!hr> 
</td><td>
<table border=0 cellpadding=5 cellspacing=5><tr>
<td><a href=~javascript:AllShow();~><b class=~tdgoldV~> Total: ` ,$cnt,   `</b></a></td>`;
if ($cnttab>0) echo `
<td><a href=~javascript:TabShow();~><b class=~tdgoldV~> Tabs: `  ,$cnttab,`</b></a></td>`;
if ($cntbib>0) echo `
<td><a href=~javascript:BibShow();~><b class=~tdgoldV~> Verses: `,$cntbib,`</b></a></td>`;
if ($cntfil>0) echo `
<td><a href=~javascript:FilShow();~><b class=~tdgoldV~> Files: ` ,$cntfil,`</b></a></td>`;
if ($cntiss>0) echo `
<td><a href=~javascript:IssShow();~><b class=~tdgoldV~> Issues: `,$cntiss,`</b></a></td>`;
echo `</tr></table></td></tr></table></center><br>`;

echo `<div id=~alldiv~ style=~display:none;~>`;
    echo `<center><font size=4><b><i>All</i></b> Search Results for <i><font color=red>`,$ostr,`</font></i></font> <font size=0 color=darkgreen>(`,$cnt,`)</font>`,$crlf;
ShowQueryResult(~select * from tmpfils order by Type,Item~);
echo `</div>`;

echo `<div id=~bibdiv~ style=~display:none~>`;
    echo `<center><font size=4><b><i>Bible</i></b> Search Results for <i><font color=red>`,$ostr,`</font></i></font> <font size=0 color=darkgreen>(`,$cntbib,`)</font>`,$crlf;
ShowQueryResult(~select * from tmpfils where Type=`Bib` order by Type,Item~);
echo `</div>`;

echo `<div id=~fildiv~ style=~display:none~>`;
echo `<center><font size=4><b><i>File</i></b> Search Results for <i><font color=red>`,$ostr,`</font></i></font> <font size=0 color=darkgreen>(`,$cntfil,`)</font>`,$crlf;
    ShowQueryResult(~select * from tmpfils where Type!=`Bib` and Type!=`Tab` and Type!=`Issue` order by Type,Item~);
echo `</div>`;

echo `<div id=~tabdiv~ style=~display:none~>`;
echo `<center><font size=4><b><i>Tab</i></b> Search Results for <i><font color=red>`,$ostr,`</font></i></font> <font size=0 color=darkgreen>(`,$cnttab,`)</font>`,$crlf;
    ShowQueryResult(~select * from tmpfils where Type=`Tab` order by Type,Item~);
echo `</div>`;

echo `<div id=~issdiv~ style=~display:none~>`;
echo `<center><font size=4><b><i>Issue</i></b> Search Results for <i><font color=red>`,$ostr,`</font></i></font> <font size=0 color=darkgreen>(`,$cntiss,`)</font>`,$crlf;
    ShowQueryResult(~select * from tmpfils where Type=`Issue` order by Type,Item~);
echo `</div>
`;

echo `
<script language=javascript>
AllShow();
</script>
`;
  }

function CheckDir($dir,$strs)
{
     global $dq;
//echo ~<br>CheckDir started for dir=$dir~;

     $recurse=1;

     $files = directoryToArray($dir,$recurse);

     $cnt=0;

     for ($i = 0; $i <= count($files); $i++)
     {
   	$file=$files[$i];
	CleanText($file);

	$bn=basename ($file);
//echo ~<br>bn=$bn~;

	foreach($strs as $si=>$stri)
	  {
 		if (!stristr($bn, $stri)) continue 2;
	  } 
        
            $cnt=$cnt+1;

//echo `srch=`,$srch,` file=`,$files[$i],`<br>`;

	    $ext = pathinfo($file, PATHINFO_EXTENSION);

            $file2=$file;
            $file=str_replace(~ ~,~%20~,$file);

//echo ~<br>file=$file bn=$bn ext=$ext ~;

	    if (is_dir($file2))
               {
                  $ext=`dir`;
	 	  $lnk=~<a x=$dq$bn$dq~ . ` href=~../inc/DirList.php?dir=` . $file . `&showdirs=TRUE&breadcrumbstop=Share~ target=_BLANK>` . $bn . `</a>`;
               }
	    else
		  $lnk=~<a x=$dq$bn$dq href=$dq~ . $file . ~$dq target=_BLANK>~ . $bn . `</a>`;

//echo ~<br>lnk=$lnk~;

     	    $sql=~insert into tmpfils select `$ext`,`$lnk`,`$file2`~;

	    ExecuteSQL($sql);

//echo ~<br>sql=$sql~;

//if($cnt==1) break;
     } 
//ShowQueryResult(~select * from tmpfils~);
}

function directoryToArray($directory, $recursive)
{
	$array_items = array();
	if ($handle = opendir($directory)) {
		while (false !== ($file = readdir($handle))) {
			if ($file != ~.~ && $file != ~..~) {
				if (is_dir($directory. ~/~ . $file)) 
				   {
					if($recursive) 
					  {
						$array_items = array_merge($array_items, directoryToArray($directory. ~/~ . $file, $recursive));
					  }
					$file = $directory . ~/~ . $file;
					$array_items[] = $file;
				  } 
				else 
				   {
					$file = $directory . ~/~ . $file;
					$array_items[] = $file;
				   }
			}
		}
		closedir($handle);
	}
	return $array_items;
}

echo `
<script language=javascript>

function GetSrchStr()
{
  lsstr=prompt(~Enter the string to search~,~`,$_SESSION[~Sstring~],`~);
  if (lsstr==~~) {lsstr=~null~;}
  window.document.Help.action=~Help.php?iprm=`,$iPrm,`&iprm2=`,$iPrm2,`&clean=`,$clean,`&lsstr=~+lsstr+~&txtclass=$txtclass~ ;
  window.document.Help.submit(true);
}
function PopTab(tbnm,itm,whit,projectid)
{
//alert(~PopTab hit=~+whit);
  return popitup(~Help.php?projectid=~+projectid+~&tabname=~+tbnm+~&iprm=~+itm+~&winnam=SearchItem&helpwiki=1&exte=1~,50,100,whit,~800,resizable=1,scrollbars=yes,location=no,toolbar=no,status=no,menubar=yes~);
}
function PopIss(itm,whit)
{
  return popitup(~IssueEdit.php?winnam=SearchItem&id=~+itm,50,100,whit,~800,resizable=1,scrollbars=yes,location=no,toolbar=no,status=no,menubar=yes~);
}
function PopVrs(ref)
{
//alert(~PopVrs ref=~+ref);
  return popitup(~Help.php?tabname=BiblePassage&ref=~+ref+~&winnam=SearchBible&helpwiki=1&exte=1&clean=1~,75,300,400,~500,resizable=1,scrollbars=yes,location=no,toolbar=no,status=no,menubar=yes~);
}
</script>
`;

?>