<?php include("../../include/dbconnect.php"); 
foreach($_POST as $q=>$r)
{
	$_POST[$q] = trim($r);
}

$bcode = $_POST['selbcode'];
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script language="javascript">
function funSubmit()
{
	document.formEntry.method = "post";
	document.formEntry.action = "<?=$_SERVER['PHP_SELF']?>?rec=Old"; 	
	document.formEntry.submit();
}

function funDirectToMain(act)
{
	//filename = formEntry.filename.value;
	filename = "parsys.php";
	if (act == "OK")
	{
		if (formEntry.selbcode.value != "-")
		{
			document.formEntry.method = "post";
			if (filename == "parsys.php" || filename == "edit_parsys.php")
			{
				document.formEntry.action = filename+"?rec=Old&isSrch=Yes"; 	
			}
			else
			{
				document.formEntry.action = filename; 	
			}
			document.formEntry.submit();		
		}
	}
	else if (act == "cancel")
	{
		document.formEntry.method = "post";
		document.formEntry.action = filename; 	
		document.formEntry.submit();		
	}
}

</script>
<title>Get Customer</title>
<style type="text/css">
body{
font-family:Verdana, Arial, Helvetica, sans-serif;
}
</style>
<link rel="stylesheet" href="../../include/style.css">
<body oncontextmenu = "javascript:return false;" ondragdrop= "javascript:return false;" onselectstart="javascript:return false;">
<form name = "formEntry">
<input type="hidden" value="<?= $filename ?>" name="filename">
<table width="780px" border=0 cellpadding=5 cellspacing=0 bordercolor="#9966CC" frame="box" rules="none" >
  <tr>
    <td colspan="4" class="tdhead">Search Customer</td>
  </tr>
  <tr class="tdindent">
    <td><b>Customer Name</b></td>
    <td>
	<select name="selbcode" onChange="javascript:funSubmit();">  
    <option value="-" selected>-</option>
    <?
		if($filename == "edit_parsys.php")
		{
			$qry = "select distinct(bcode) bcode, name from buy where active = 'Yes' and buyertype != 'Supplier'  ORDER BY name";
		}
		else
		{
			$qry = "select distinct(bcode) bcode, name from buy where active = 'Yes'  and buyertype != 'Supplier' ORDER BY name";		
		}
		
    	$result=mysqli_query($conn,$qry) or die(mysqli_error());
		$numRow = mysqli_num_rows($result);
		
		if ($numRow >0 )
		{
			while($row = mysqli_fetch_array($result))
			{
				if(isset($bcode) && preg_match("/" . preg_quote($bcode, '/') . "/", $row['bcode']))
				{ ?>
				<option selected value="<?= $row['bcode']; ?>"><?= $row['name']."-".$row['bcode']; ?></option>
				<?
				}
				else
				{
				?>	
				<option value="<?= $row['bcode']; ?>"><?= $row['name']."-".$row['bcode']; ?></option>
				<? 
				} 
			}
			
			
		}	
?>
    	</select>
    </td>
<?
	$query = "select buy.bcode as bcode,name,address1,address2,city,state,country,pin,telno,buyertype from buy where buy.bcode = '$bcode'";
	
	$result= mysqli_query($conn,$query) or
		  die (mysqli_error() );
	$row = mysqli_fetch_array($result);
		  
	if(is_array($row))
	{
	  $bcode = $row['bcode'];
	  $name = $row['name'];
	  $address1 = $row['address1'];
	  $address2 = $row['address2'];
	  $city = $row['city'];
	  $state = $row['state'];
	  $country = $row['country'];
	  $pin = $row['pin'];
	  $telno = $row['telno'];
	  $buyertype = $row['buyertype'];
	}
	else
	{
	  $bcode 	= "";
	  $name			= "";
	  $address1 	= "";
	  $address2 	= "";
	  $city 		= "";
	  $state 		= "";
	  $country 		= "";
	  $pin 			= "";
	  $telno 		= "";
	  $buyertype 	= "";	
	}
	
?>	
    <td><b>Description</b></td>
    <td><input type="text" size="35" name="buyertype" value="<? echo $buyertype; ?>" readonly>
    </td>
  </tr>
  <tr class="tdindent">
    <td ><b>Address</b></td>
    <td colspan="3"><input type="text" size="50" name="address1" value="<? echo $address1; ?>" readonly>
    </td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td colspan="3"><input type="text" size="50" name="address2" value="<? echo $address2; ?>" readonly>
    </td>
  </tr>
  <tr class="tdindent">
    <td><b>City</b></td>
    <td><input type="text" size="35" name="city" value="<? echo $city; ?>" readonly>
    </td>
    <td><b>State</b></td>
    <td><input type="text" size="35" name="state" value="<? echo $state; ?>" readonly>
    </td>
  </tr>
  <tr class="tdindent">
    <td><b>Pin Code</b></td>
    <td><input type="text" size="10" name="pin" value="<? echo $pin; ?>" readonly>
    </td>
    <td><b>Tel No(s)</b></td>
    <td><input type="text" size="35" name="telno" value="<? echo $telno; ?>" readonly>
    </td>
  </tr>
  <tr> 
	  <td colspan="4">
	  <table align="center" cellpading=0 cellspacing=10 >
		<tr>
		  <td> 
		  <input type ="button" name="btnok" value = "OK" onClick="javascript:funDirectToMain('OK');">
		  <input type ="button" name="btnCancel" value = "cancel" onClick="javascript:funDirectToMain('cancel');">
		  </td>
		</tr>
	  </table>
	  </td>
  </tr>  
</table>
</form>
</body>
</html>
