<?php 
	require('list.func.php');
	require('../includes/listheader.php');

	$offset = 30;
	
	$field  = $_SERVER["argv"][1];
	$hidden = $_SERVER["argv"][2];
	$search = $_SERVER["argv"][3];
	
	if (isset($_SERVER["argv"][4]) && is_numeric($_SERVER["argv"][4]))
		$page = $_SERVER["argv"][4];
	else
		$page = 0;	
	$module = $_SERVER["argv"][5];
	
	$query = "select ".$_SESSION['selectParams']['codeField'].", ".$_SESSION['selectParams']['descField'].
			 " from ".$_SESSION['selectParams']['tableName'];
			 
	$condition = str_replace('%20', ' ', $_SESSION['selectParams']['condition']);
	if (($search != '') && ($condition != ''))
	{
		$query .= " where ".$_SESSION['selectParams']['descField']." like '%".$search."%' ".
		"and ".$condition;
	}
	else
	{	
		if ($condition != '')
			$query .= " where ".$condition;
		else
		{	
			if ($search != '') $query .= " where ".$_SESSION['selectParams']['descField']." like '%".$search."%'";
		}
	}
	
	$query .= " order by " . $_SESSION['selectParams']['descField'];
	$query .= " limit ".($page * $offset).", ".$offset;
	$result = executeQuery($query,3,'',true);
	
	//setup the navigational buttons
	$cnt = mysql_num_rows($result);
	if ($cnt < $offset)
	{
		$next = $page;
		$nextimg = "";
	}
	else
	{
		$next = $page + 1;
		$nextimg = "<img src='../images/next.png' class='link' onclick='fetchList(\"".$field."\", \"".$hidden."\", \"".$search."\", ".$next.", \"".$module."\")' />";
	}
	if ($page > 0)
	{
		$prev = $page - 1;
		$previmg = "<img src='../images/prior.png' class='link' onclick='fetchList(\"".$field."\", \"".$hidden."\", \"".$search."\", ".$prev.", \"".$module."\")' />";
	}
	else
	{
		$prev = 0;
		$previmg = "";
	}	
	
	$html = '<table style="width:100%; height:100%" border="0" cellpadding="0" cellspacing="0">';
	$html .= '<tr><td colspan="3" align="right"><input style="vertical-align:middle" type="text" 
	id="edtSearch" class="textbox" onkeypress="handleEnter(this, event)"/>
	<input type="image" src="../images/search.png" style="vertical-align:middle" 
	onclick="fetchList(\''.$field.'\', \''.$hidden.'\', document.getElementById(\'edtSearch\').value, 0, \''.$module.'\')" class="link" id="btnOk"/></td></tr>';
	
	$cnt = $page * $offset;
	
	while($row = mysql_fetch_assoc($result))
	{	
		$cnt++;
		$code = $row[$_SESSION["selectParams"]["codeField"]];
		$desc = $row[$_SESSION["selectParams"]["descField"]];
		$desc_val = str_replace("'", "\'", $desc);
		$html .= ' <tr>';
		$html .= '<td><a title="'.$desc.'" href="javascript:insertSelectionCode(\''.$code.'\',\''.$desc_val.'\',\''.$field.'\',\''.$hidden.'\', \''.$search.'\')" class="link_contrast">'.$cnt.'</a></td>';
		$html .= '<td width="100%"><a title="'.$desc.'" 
		href="javascript:insertSelectionCode(\''.$code.'\',\''.$desc_val.'\',\''.$field.'\',\''.$hidden.'\', \''.$search.'\')" class="link_contrast">'.$desc.'</a></td>';
		$html .= '</tr>';
	}
	if ($cnt == 0) $html .= '<tr><td colspan="2">No records were found or the end of the list has been reached.<a href="javascript:fetchList(\''.$field.'\', \''.$hidden.'\', document.getElementById(\'edtSearch\').value, 0, \''.$module.'\')">Click here to refresh.</a></td></tr>';
	$html .= '<tr><td colspan="2">'.$previmg.'</td><td align="right">'.$nextimg.'</td></tr>';
	$html .= '</table>';
	
?> 
<script src="../scripts/ajax2.js" ></script>
<div id="ajax2"><?php echo($html); ?></div>
<?php require('../includes/listfooter.php'); ?>