<?php
	include(__DIR__.'/../includes/sysinit.php');
	$sql = "select ".$resultIdField." as VAL, ".$resultDescField." as DESCR from ".$resultTableName;
	if(!empty($callIdField)) $sql .=" where ".$callIdField." = ".SableSystem::sableDecode($_POST['input_id']);
	
	//SableDebug::debug($sql);
	
	$rows = $sableDB->getRows($sql);
	$html = "";
	$cnt = 0;
	if ($_POST['default_value'] == -1)
	{
		if (isset($resultModule) && !empty($resultModule))
			$moduleName = $resultModule;
		else
		{
			$mods = explode(' ', trim($resultTableName)); //could contain inner joins, etc.
			$moduleName = SableGeneralFunctions::getFormattedModuleName($mods[0]);
		}
		$html .= '<option selected value="0">Select '.$moduleName.'</option>';
		unset($_POST['default_value']);
		$cnt = 1;
	}
	foreach($rows as $row)
	{
		if (isset($_POST['default_value']))
		{
			$sel = SableSystem::sableDecode($_POST['default_value'])===$row['VAL']?"selected='selected'":"";
		}
		else
		{
			$sel = $cnt===0?"selected='selected'":"";
		}
		$html .= '<option '.$sel.' value="'.SableSystem::sableEncode($row['VAL']).'">'.$row['DESCR'].'</option>';
		$cnt++;
	}
	$data['output_list'] = $html;
	//echo(json_encode($data));	
	SableSystem::ajaxFillField($html, $_POST['field_id']);
?>