<?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);
}

$query_login="select login_belongstogrp,ifNull(FI_SuppliersDel,0),ifNull(FI_SuppliersMod,0) from ".$HTTP_SESSION_VARS['dbLabel'].".login where user_account='".$HTTP_SESSION_VARS['useraccount']."' and Upper(Login_Schema) = Upper('".$HTTP_SESSION_VARS['dbName']."')  ";
$resultlogin=mysql_query($query_login,$connection) or die(mysql_error());

$row_login=mysql_fetch_row($resultlogin);
$belongstogroup=$row_login[0];
$FI_Supplier_Del=$row_login[1];
$FI_Supplier_Mod=$row_login[2];


//// Delete   Edit 
$DeleteMode=' ';
if($FI_Supplier_Del==0)
$DeleteMode ='disabled';

$EditMode=' ';
if($FI_Supplier_Mod==0)
$EditMode = ' 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 Suppliers</title>
<script type="text/javascript" language="javascript">


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

while (intIndexOfMatch != -1){
strReplaceAll = strReplaceAll.replace( "%20", " " )
intIndexOfMatch = strReplaceAll.indexOf( "%20" );
}
Code= strReplaceAll;
					window.event.returnValue = false;
				
					if (confirm ( "Are you sure you want to Delete this Record " + "?")) {
							window.event.returnValue = true;
					
					document.search.action='Form_Suppliers_Actions.php?actionmode=delete&ledger_number='+Code;
					document.search.submit();
					
					}
					
         		}
				
				

function clearform()
{
  document.search.PRef.value='';
  document.search.action='Form_SupplierSearch.php';
  document.search.submit();

}

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

}

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

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

function set_limit(){

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





function PrintPreview(PurId)
{
	var str = '';
	
	str = 'Form_PurchasePreview.php?PurchaseId='+PurId;
	window.open(str);

}

function Choosesupplier(supplierid){

opener.document.supplierdt.mainmode.value='edit';

opener.document.supplierdt.ledger_number.value=supplierid;

opener.supplierdt.action="Form_Suppliers.php?ledger_number="+supplierid;

opener.supplierdt.submit();
self.close();
}

function Viewsupplier(supplierid){

opener.document.supplierdt.actionmode.value='view';

opener.document.supplierdt.ledger_number.value=supplierid;

opener.supplierdt.action="Form_Suppliers.php?mode=view&ledger_number="+supplierid;

opener.supplierdt.submit();
self.close();
}


</script>

<style type="text/css">

body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
}
body {
	margin-top: 2px;
	margin-left: 1px;
}
a:link {
	text-decoration: none;
	color: #0000FF;
}
a:visited {
	text-decoration: none;
	color: #0000FF;
}
a:hover {
	text-decoration: none;
	color: #0000FF;
}
a:active {
	text-decoration: none;
	color: #0000FF;
}
.style11 {color: #999999; font-weight: bold; }
.style12 {
	font-size: 9px;
	color: #999999;
	font-weight: bold;
}
.style15 {color: #5151FF; }
.style16 {
	font-size: 16px;
	color: #FF0000;
	font-weight: bold;
	font-family: "Times New Roman", Times, serif;
}
.style17 {font-size: 9px}
.style25 {color: #999999; font-weight: bold; font-size: 10px; }
.style26 {color: #FF0000}
.style27 {color: #999999 ;font-size: 10px;}
.style28 {color: #000000 ;font-size: 10px;}

</style>

</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='6'><span class='style16'>List of Suppliers</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='6'><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 ledger_number,ledger_name from suppliers where ifNull(Supplier_BelongsToGroup,0) >= ".$belongstogroup;

$querysearch=$querysearch." and ( ledger_number like"."('%".$PRef."%') or ledger_name like"."('%".$PRef."%')) ";
//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[0]);	
	$name=ereg_replace(" ","%20",$rows[1]);	
		$name=ereg_replace("'","%27",$name);
print<<<HERE
	
<td align=center width='3%'><img border='0' src='img/button_edit.png' width='12' height='13' title='Edit Detail' onmouseover="this.style.cursor='pointer'" onclick="Choosesupplier('$rows[0]')"; $EditMode ></td>
<td align=center width='3%'><img border='0' src='img/c_delete.png' width='9' height='10' title='Delete Detail' onmouseover="this.style.cursor='pointer'" OnClick=confirm_delete('$code','$name');  $DeleteMode></td>

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

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

HERE;


echo"</font></td><td colspan=2><font face='Arial' size='2' color='#186194'><span class='style15'>$rows[1]&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='6'>&nbsp;</td></tr>
		<tr><td colspan=6><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='img/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='img/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;

?>