<?php

session_start();
if($HTTP_SESSION_VARS['dbName']==""){
header("location:../index.php");
}


include("config.inc.php");

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");
mysql_select_db($HTTP_SESSION_VARS['dbName'],$connection) or die("Could not find DB");



$querysetup="select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2 from setup";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
extract($rowsetup);
}

//User Access
$querylog=" select ifNull(login_belongstogrp,0) As FI_BelongsToGrp, ifNull(FA_ProjectDel,0) As FA_ProjectDel, ".
          "ifNull(FA_ProjectMod,0) As FA_ProjectMod from ".$HTTP_SESSION_VARS['dbLabel'].".login where user_account='".$HTTP_SESSION_VARS['useraccount']."' and Upper(Login_Schema) = Upper('".$HTTP_SESSION_VARS['dbName']."')  ";
$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}

//Access Mode On Insert/Edit/Delete
$DelMode = '';
$ModMode = '';
if ($FA_ProjectDel == 0)
	$DelMode = ' disabled ';
if ($FA_ProjectMod == 0)
	$ModMode = ' disabled ';

print <<<HERE
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="default.css" type="text/css" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Search projects</title>
<script type="text/javascript" language="javascript">

function Edit_Record(projectId){

opener.document.project.actionmode.value='edit';

opener.document.project.Project_Id.value=projectId;

opener.project.action="Form_Project.php?mode=edit";

opener.project.submit();
self.close();
}
function clearform()
{
  document.search.PRef.value='';
  document.search.action='Form_ProjectSearch.php';
  document.search.submit();

}

function LoadForm() 
{
	x=document.search;
	x.PRef.focus();
	

}

function SetNextPage(startrange,perpage){
	document.search.action='Form_ProjectSearch.php?PRef=&startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}

function SetPrvPage(startrange,perpage){
	document.search.action='Form_ProjectSearch.php?PRef=&startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}

function set_limit(){

	perpage=document.search.perpage.value;
	document.search.action='Form_ProjectSearch.php?perpage='+perpage;
	document.search.submit();
}



function confirm_delete(Code, projectId) {
	var strReplaceAll = Code;
	var intIndexOfMatch = strReplaceAll.indexOf( "%20" );

	while (intIndexOfMatch != -1){
		strReplaceAll = strReplaceAll.replace( "%20", " " )
		intIndexOfMatch = strReplaceAll.indexOf( "%20" );
	}
	Code= strReplaceAll;
				
	
	if (confirm ( "Are you sure you want to Delete the Project " + Code + "?"))
	{
		document.search.action="Form_Project_Actions.php?actionmode=delete&Project_Id="+projectId;
		document.search.submit();
	}
	
	
							
}					

</script>		
</head>
<body onLoad='LoadForm();'>
<form name='search' method='post' action=''>
HERE;

echo"<table border='0' style='table-layout:fixed'>
	  <col width=3%>
	  <col width=3%>
	  <col width=15%>
	  <col width=35%>
	  <col width=50%>
		     
	  <tr bgcolor='#DDDDDD'>
	    <td align='center' colspan='5'><span class='style16'>List of projects</span></td></tr>
	  <tr bgcolor='#DDDDDD' height=30px>
	    <td align='left' colspan='3'><span class='style11'>Search by Code</span></td>
		<td align='left'><span class='style11'><input type='text' name='PRef' id='PRef' value='$PRef' size='15' maxlength='30'/><input type='hidden' name='PMode' id='PMode' value='$PMode' size='15' maxlength='30'/></span>
		    <input type='submit' name='Submit' value='Find' style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/>
			<input type='button' name='btnCancel' value='Cancel' onclick='clearform();'style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/></td>
		<td align='left' valign='center'><span class='style25'>Result per page </span><span class='style11'><select name='perpage' id='perpage' value='$perpage' onchange=set_limit();>";
              


        if ($perpage==20)
			$s20 = 'selected';
		else if ($perpage==30)
			$s30 = 'selected';
		else if ($perpage==40)
			$s40 = 'selected';
		else if ($perpage==50)
			$s50 = 'selected';

			
				
        echo'<option '.$s20.' value="20">20</option>
			 <option '.$s30.' value="30">30</option>
			 <option '.$s40.' value="40">40</option>
			 <option '.$s50.' value="50">50</option>
			 </select></span></td>';	
echo" </tr>
	  <tr bgcolor='#DDDDDD'>
		<td height='15' align='center' colspan='5'><font size='1' color='#FF0000' face='Verdana'><b>".$message."</b></font></td>
	  </tr>	
	  <tr bgcolor='#CCD8E6'>
		<td  height='25' align='center'><span class='style25'>&nbsp;</span></td>
		<td  height='25' align='center'><span class='style25'>&nbsp;</span></td>
		<td  height='25' align='left'><span class='style25'>Code </span></td>
		<td  height='25' align='center' colspan=2><span class='style25'>Name </span></td>
		
	  </tr>";
		

	

	


$querysearch="select  Project_Id, project_code,project_name,date_format(project_date,'%d-%m-%Y') from project ";


$querysearch=$querysearch." where (project_code like"."('%".$PRef."%') or project_name like"."('%".$PRef."%')) ".
						  " And ifNull(Project_BelongsToGroup,0) >= ifNull($FI_BelongsToGrp,0) order by project_code ";
//echo$querysearch;
$resultsearch=mysql_query($querysearch,$connection) or die(mysql_error());
$totalrows = mysql_num_rows($resultsearch);
	
if ($startrange=='')
	$startrange = 0;
if ($perpage=='')
	$perpage = 20;
		
$querysearch = $querysearch." limit ".$startrange.",".$perpage;
$resultsearch=mysql_query($querysearch,$connection) or die(mysql_error());		


while($rows=mysql_fetch_array($resultsearch)){
		
	extract($rows);
	$ct+=1;

	if($ct%2==0)
		$Color="style='background-color:".$grid_color1."'";
	else			
		$Color="style='background-color:".$grid_color2."'";	
			

	echo'<tr '.$Color.'>';
	
	
print<<<HERE
	

HERE;
	
	
	$code=ereg_replace(" ","%20",$rows[1]);	
	$name=ereg_replace(" ","%20",$rows[2]);	
	$name=ereg_replace("'","%27",$name);
print<<<HERE
	
	
	
<td align=center width='3%'><img border='0' src='images/button_edit.png' width='12' height='13' title='Edit Detail' onmouseover="this.style.cursor='pointer'" onclick="Edit_Record('$rows[0]')" $ModMode></td>


<td align=center width='3%'><img border='0' src='images/c_delete.png' width='9' height='10' title='Delete Detail' onmouseover="this.style.cursor='pointer'" OnClick="confirm_delete('$code', '$rows[0]');" $DelMode></td>

<td><font face='Arial' size='2' color='#186194'>

HERE;
	

print<<<HERE
<span class="style15">$rows[1]&nbsp;</span>

HERE;


echo"</font></td><td colspan=2><font face='Arial' size='2' color='#186194'><span class='style15'>$rows[2]&nbsp;</span></font></td>";
echo"</tr>";


}
							
//<td height="20" align="left"><a href="#" onClick="SelectRef('.$Purchase_ID.');" class="style15"><span class="style15 style17" style="text-decoration:underline;">'.$Purchase_Reference.'</span></a></td>			
			
		
echo"   <tr><td colspan='5'>&nbsp;</td></tr>
		<tr><td colspan=5><table width='100%' border=0><tr><td width='260'>&nbsp;</td><td width='150' align='center'>";
		
		$diff= $startrange - $perpage;
		
		if (($diff) >= 0 ){
			$prvstartrange = $startrange - $perpage;
			echo"<img src='images/arrowleft.jpg' title='Previous' onMouseOver=\"style.cursor='hand'\"  onclick=SetPrvPage('".$prvstartrange."','".$perpage."')>";
		}
		else
			echo "&nbsp;";
		
		echo"</td><td width='150' align='center'>";
		
		
		if ($startrange <= $totalrows){
			$startrange = $startrange + $perpage;
			if  ($startrange <= $totalrows)
				echo"<img src='images/arrowright.jpg' title='Next' onMouseOver=\"style.cursor='hand'\" onclick=SetNextPage('".$startrange."','".$perpage."')>";
		} else
			echo "&nbsp;";
			
		echo"</td><td width='170'>&nbsp;</td></tr>";
		echo"<table>";

echo"</td></tr></table>";
	


          

		  

print <<<HERE
</form>
</body>

HERE;

?>							
							






							