<?php

function lookuptemplatevar(){
	$templateid=GETVAL('templateid');
	global $db;
	$key=SGET('varkey');
	$key=str_replace('%','',$key);
	
	$mode=SGET('mode');

	if ($mode!='embed'){
?>
<div class="section" style="position:relative;">

	<div class="listsearch">
	<div class="listsearch_">
		<input id="templatetypelookupkey" value="<?echo htmlspecialchars($key);?>" class="img-mg" onkeyup="_inline_lookuptemplatetypelookup(this,<?echo $templateid;?>);" autocomplete="off">
		<img src="imgs/inpback.gif" class="inpback" onclick="inpbackspace('templatetypelookupkey');_inline_lookuptemplatetypelookup(gid('templatetypelookupkey'),<?echo $templateid;?>)">
	</div>
	<input type="image" src="imgs/mg.gif" class="searchsubmit" value=".">
	</div>
	
	<div id="templatetypelookuplist">
<?	
	}
		
	$query="select templatetypeid from templates where templateid=?";
	$rs=sql_prep($query,$db,$templateid);
	$myrow=sql_fetch_assoc($rs);
	$templatetypeid=$myrow['templatetypeid']+0;

	$params=array($key);
	
	$keyorder='';
	if ($key!='') {
		$keyorder="templatevarname=? desc,";
	}
	
	$query="select * from templatevars where templatetypeid=$templatetypeid ";
	if ($key!=''){
		$query.=" and (templatevarname like ? or templatevardesc like ?) ";
		array_push($params,"%$key%","%$key%");	
	}
	$query.=" order by $keyorder templatevarid";
	$rs=sql_prep($query,$db,$params);
		
	while ($myrow=sql_fetch_assoc($rs)){

		$varname=$myrow['templatevarname'];
		$vardesc=$myrow['templatevardesc'];	
		$dbvar=noapos(htmlentities($varname));
?>
<div class="listitem" style="<?if ($varname==$key) echo 'font-weight:bold;background:#ffffcc;';?>"><a onclick="if (document.hotspot&&document.hotspot.onChange) document.hotspot.onChange.dispatch();if (document.hotspot&&document.hotspot.selection) {document.hotspot.selection.setContent('%%<?echo $dbvar;?>%%');document.hotspot.focus();}"><?echo $varname;?><br><em style="color:#666666;"><?echo htmlspecialchars($vardesc);?></em></a></div>
<?		
	}//while	
	
	if (!$embed){
?>
<?
	
	makehelp('templatevar','templatevar',1);
?>
	</div>
</div>
<?
	}
}