<?php 


////////////////////////////////////////////////////////////////////////////////
// table and field info functions
////////////////////////////////////////////////////////////////////////////////

function GetTableData($table,$key,$default)
{
	global $strTableName,$tables_data;
	if(!$table) 
		$table = $strTableName;
	if(!array_key_exists($table,$tables_data))
		return $default;
	if(!array_key_exists($key,$tables_data[$table]))
		return $default;
	return $tables_data[$table][$key];
}

function GetFieldData($table,$field,$key,$default)
{
	global $strTableName,$tables_data;
	if(!$table) 
		$table = $strTableName;
	if(!array_key_exists($table,$tables_data))
		return $default;
	if(!array_key_exists($field,$tables_data[$table]))
		return $default;
	if(!array_key_exists($key,$tables_data[$table][$field]))
		return $default;
	return $tables_data[$table][$field][$key];
}

function GetFieldByIndex($index, $table="")
{
	global $strTableName,$tables_data;
	if(!$table) 
		$table = $strTableName;
	if(!array_key_exists($table,$tables_data))
		return null;
	foreach($tables_data[$table] as $key=>$value)
	{
		if(!is_array($value) || !array_key_exists("Index",$value))
			continue;
		if($value["Index"]==$index and GetFieldIndex($key))
			return $key;
	}
	return null;
}

// return field label
function Label($field,$table="")
{
	return GetFieldData($table,$field,"Label",$field);
}

// return filename field if any
function GetFilenameField($field,$table="")
{
	return GetFieldData($table,$field,"Filename","");
}

//	return hyperlink prefix
function GetLinkPrefix($field,$table="")
{
	return GetFieldData($table,$field,"LinkPrefix","");
}

//	return database field type
//	using ADO DataTypeEnum constants
//	the full list available at:
//	http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ado270/htm/mdcstdatatypeenum.asp
function GetFieldType($field,$table="")
{
	return GetFieldData($table,$field,"FieldType","");
}

function IsAutoincField($field,$table="")
{
	return GetFieldData($table,$field,"AutoInc",false);
}


//	return Edit format
function GetEditFormat($field,$table="")
{
	return GetFieldData($table,$field,"EditFormat","");
}

//	return View format
function ViewFormat($field,$table="")
{
	return GetFieldData($table,$field,"ViewFormat","");
}

//	show time in datepicker or not
function DateEditShowTime($field,$table="")
{
	return GetFieldData($table,$field,"ShowTime",false);
}

//	use FastType Lookup wizard or not
function FastType($field,$table="")
{
	return GetFieldData($table,$field,"FastType",false);
}

function LookupControlType($field,$table="")
{
	return GetFieldData($table,$field,"LCType",LCT_DROPDOWN);
}


//	is Lookup wizard dependent or not
function UseCategory($field,$table="")
{
	return GetFieldData($table,$field,"UseCategory",false);
}

//	is Lookup wizard with multiple selection
function Multiselect($field,$table="")
{
	return GetFieldData($table,$field,"Multiselect",false);
}

function ShowThumbnail($field,$table="")
{
	return GetFieldData($table,$field,"ShowThumbnail",false);
}

function GetImageWidth($field,$table="")
{
	return GetFieldData($table,$field,"ImageWidth",0);
}

function GetImageHeight($field,$table="")
{
	return GetFieldData($table,$field,"ImageHeight",0);
}

//	return Lookup Wizard Where expression
function GetLWWhere($field,$table="")
{
	global $strTableName;
	if(!$table) 
		$table = $strTableName;
##foreach @BUILDER.Tables as @t##
##foreach @t.arrFieldObj as @f filter (@f.strEditFormat==EDIT_FORMAT_LOOKUP_WIZARD || @f.strEditFormat==EDIT_FORMAT_RADIO) && @f.strWhere##
	if($table=="##@t.strDataSourceTable s##" && $field=="##@f.strName s##")
		return ##@f.strWhere##;
##endfor##
##endfor##
	return "";
}

function GetLookupType($field,$table="")
{
	return GetFieldData($table,$field,"LookupType",0);
}

function GetLookupTable($field,$table="")
{
	return GetFieldData($table,$field,"LookupTable","");
}

function GetLWLinkField($field,$table="")
{
	return GetFieldData($table,$field,"LinkField","");
}

function GetLWLinkFieldType($field,$table="")
{
	return GetFieldData($table,$field,"LinkFieldType",0);
}

function GetLWDisplayField($field,$table="")
{
	return GetFieldData($table,$field,"DisplayField","");
}

function NeedEncode($field,$table="")
{
	return GetFieldData($table,$field,"NeedEncode",false);
}

function AppearOnListPage($field,$table="")
{
	return GetFieldData($table,$field,"ListPage",false);
}

function GetTablesList()
{
	$arr=array();
##foreach @BUILDER.tables as @t##
##if GroupSecurity##
	$strPerm = GetUserPermissions("##@t.strDataSourceTable s##");
	if(strpos($strPerm, "P")!==false)
	{
##endif##
		$arr[]="##@t.strDataSourceTable s##";
##if GroupSecurity##
	}
##endif##
##endfor##
	return $arr;
}

function GetTablesListReport()
{
	$arr=array();
##foreach @BUILDER.tables as @t filter @t.nType!=titCHART && @t.nType!=titREPORT##
##if GroupSecurity##
	$strPerm = GetUserPermissions("##@t.strDataSourceTable s##");
	if(strpos($strPerm, "P")!==false || strpos($strPerm, "S")!==false)
	{
##endif##
		$arr[]="##@t.strDataSourceTable s##";
##if GroupSecurity##
	}
##endif##
##endfor##
	return $arr;
}


function GetFieldsList($table="")
{
	global $strTableName,$tables_data;
	if(!$table)
		$table = $strTableName;
	if(!array_key_exists($table,$tables_data))
		return array();
	$t = array_keys($tables_data[$table]);
	$arr=array();
	foreach($t as $f)
		if(substr($f,0,1)!=".")
			$arr[]=$f;
	return $arr;
}

function GetNBFieldsList($table="")
{
	$t = GetFieldsList($table);
	$arr=array();
	foreach($t as $f)
		if(!IsBinaryType(GetFieldType($f,$table)))
			$arr[]=$f;
	return $arr;
}

//	Category Control field for dependent dropdowns
function CategoryControl($field,$table="")
{
	return GetFieldData($table,$field,"CategoryControl","");
}

//	create Thumbnail or not
function GetCreateThumbnail($field,$table="")
{
	return GetFieldData($table,$field,"CreateThumbnail",false);
}

//	return Thumbnail prefix
function GetThumbnailPrefix($field,$table="")
{
	return GetFieldData($table,$field,"ThumbnailPrefix","");
}

//	resize on upload
function ResizeOnUpload($field,$table="")
{
	return GetFieldData($table,$field,"ResizeImage",false);
}

//	get size to reduce image after upload
function GetNewImageSize($field,$table="")
{
	return GetFieldData($table,$field,"NewSize",0);
}

//	return field name
function GetFieldByGoodFieldName($field,$table="")
{
	global $strTableName,$tables_data;
	if(!$table)
		$table=$strTableName;
	if(!array_key_exists($table,$tables_data))
		return "";

	foreach($tables_data[$table] as $key=>$value)
	{
		if(count($value)>1)
		{
			if($value["GoodName"]==$field)
				return $key;
		}
	}
	return "";
}

//	return the full database field original name
function GetFullFieldName($field,$table="")
{
	$fname=AddTableWrappers(GetOriginalTableName($table)).".".AddFieldWrappers($field);
	return GetFieldData($table,$field,"FullName",$fname);
}

//     return height of text area
function GetNRows($field,$table="")
{
	return GetFieldData($table,$field,"nRows",$field);
}

//     return width of text area
function GetNCols($field,$table="")
{
	return GetFieldData($table,$field,"nCols",$field);
}

//	return original table name
function GetOriginalTableName($table="")
{
	global $strTableName;
	if(!$table)
		$table=$strTableName;
	return GetTableData($table,".OriginalTable",$table);
}

//	return list of key fields
function GetTableKeys($table="")
{
	return GetTableData($table,".Keys",array());
}


//	return number of chars to show before More... link
function GetNumberOfChars($table="")
{
	return GetTableData($table,".NumberOfChars",0);
}

//	return table short name
function GetTableURL($table="")
{
	global $strTableName;
	if(!$table)
		$table=$strTableName;
##foreach @BUILDER.Tables as @t##
	if("##@t.strDataSourceTable s##"==$table) 
		return "##@t.strShortTableName##";
##endfor##
}

//	return table Owner ID field
function GetTableOwnerID($table="")
{
	return GetTableData($table,".OwnerID",0);
}

//	is field marked as required
function IsRequired($field,$table="")
{
	return GetFieldData($table,$field,"IsRequired",false);
}

//	use Rich Text Editor or not
function UseRTE($field,$table="")
{
	return GetFieldData($table,$field,"UseRTE",false);
}

//	add timestamp to filename when uploading files or not
function UseTimestamp($field,$table="")
{
	return GetFieldData($table,$field,"UseTimestamp",false);
}

function GetUploadFolder($field, $table="")
{
	$path = GetFieldData($table,$field,"UploadFolder","");
	if(strlen($path) && substr($path,strlen($path)-1) != "/")
		$path.="/";
	return $path;
}

function GetFieldIndex($field, $table="")
{
	return GetFieldData($table,$field,"Index",0);
}

//	return Date field edit type
function DateEditType($field,$table="")
{
	return GetFieldData($table,$field,"DateEditType",0);
}

// returns text edit parameters
function GetEditParams($field, $table="")
{
	return GetFieldData($table,$field,"EditParams","");
}

// returns Chart type
function GetChartType($shorttable)
{
##foreach @BUILDER.tables as @t filter @t.nType==titCHART##
	if($shorttable=="##@t.strShortTableName##")
		return "##@t.strChartType c##";
##endfor##
	return "";
}

////////////////////////////////////////////////////////////////////////////////
// data output functions
////////////////////////////////////////////////////////////////////////////////

//	format field value for output
function GetData($data, $field, $format)
{
	return GetDataInt($data[$field], $data, $field, $format);
}

function GetDataValue($value, $field, $format)
{
	return GetDataInt($value, null, $field, $format);
}

//	GetData Internal
function GetDataInt($value, $data, $field, $format)
{
	global $strTableName;
	$ret="";
// long binary data?
	if(IsBinaryType(GetFieldType($field)))
	{
		$ret=##message LONG_BINARY##;
	} else
		$ret = $value;
	if($ret===false)
		return "";
	
	if($format == FORMAT_DATE_SHORT) 
		$ret = format_shortdate(db2time($value));
	else if($format == FORMAT_DATE_LONG) 
		$ret = format_longdate(db2time($value));
	else if($format == FORMAT_DATE_TIME) 
		$ret = str_format_datetime(db2time($value));
	else if($format == FORMAT_TIME) 
	{
		if(IsDateFieldType(GetFieldType($field)))
			$ret = str_format_time(db2time($value));
		else
		{
			$numbers=parsenumbers($value);
			if(!count($numbers))
				return "";
			while(count($numbers)<3)
				$numbers[]=0;
			$ret = str_format_time(array(0,0,0,$numbers[0],$numbers[1],$numbers[2]));
		}
	}
	else if($format == FORMAT_NUMBER) 
		$ret = str_format_number($value);
	else if($format == FORMAT_CURRENCY) 
		$ret = str_format_currency($value);
	else if($format == FORMAT_CHECKBOX) 
	{
		$ret="<img src=\"images/check_";
		if($value && $value!=0)
			$ret.="yes";
		else
			$ret.="no";
		$ret.=".gif\" border=0>";
	}
	else if($format == FORMAT_PERCENT) 
	{
		if($value!="")
			$ret = ($value*100)."%";
	}
	else if($format == FORMAT_PHONE_NUMBER)
	{
		if(strlen($ret)==7)
			$ret=substr($ret,0,3)."-".substr($ret,3);
		else if(strlen($ret)==10)
			$ret="(".substr($ret,0,3).") ".substr($ret,3,3)."-".substr($ret,6);
	}
	else if($format == FORMAT_FILE_IMAGE)
	{
		if(!CheckImageExtension($ret))
			return "";
			
		$thumbnailed=false;
		$thumbprefix="";
##foreach @BUILDER.Tables as @t filter @t.arrFieldObj[strViewFormat==FORMAT_FILE_IMAGE && bShowThumbnail].len##
	##foreach @t.arrFieldObj as @f filter @f.strViewFormat==FORMAT_FILE_IMAGE && @f.bShowThumbnail##
		if($strTableName=="##@t.strDataSourceTable s##" && $field=="##@f.strName s##")
		{
			$thumbnailed=true;
			$thumbprefix="##@f.strThumbnail s##";
		}
	##endfor##
##endfor##
		if($thumbnailed)
		{
		 	// show thumbnail
			$thumbname=$thumbprefix.$ret;
			if(substr(GetLinkPrefix($field),0,7)!="http://" && !myfile_exists(GetUploadFolder($field).$thumbname))
				$thumbname=$ret;
			$ret="<a target=_blank href=\"".htmlspecialchars(AddLinkPrefix($field,$ret))."\">";
			$ret.="<img";
			$ret.=" border=0";
			$ret.=" src=\"".htmlspecialchars(AddLinkPrefix($field,$thumbname))."\"></a>";
		}
		else
			$ret='<img src="'.AddLinkPrefix($field,$ret).'" border=0>';
	}
	else if($format == FORMAT_HYPERLINK)
	{
		if($data)
			$ret=GetHyperlink($ret,$field,$data);
	}
	else if($format==FORMAT_EMAILHYPERLINK)
	{
		$link=$ret;
		$title=$ret;
		if(substr($ret,0,7)=="mailto:")
			$title=substr($ret,8);
		else
			$link="mailto:".$link;
		$ret='<a href="'.$link.'">'.$title.'</a>';
	}
	else if($format==FORMAT_FILE)
	{
		$iquery="field=".rawurlencode($field);
	##foreach @BUILDER.Tables as @t##
		if($strTableName=="##@t.strDataSourceTable s##")
		{
	##foreach @t.arrKeyFields as @k##
			if($data)
				$iquery.="&key##@index##=".rawurlencode($data["##@k s##"]);
	##endfor##
		}
	##endfor##
		return 	'<a href="'.GetTableURL($strTableName).'_download.##@ext##?'.$iquery.'".>'.htmlspecialchars($ret).'</a>';
	}
	else if(GetEditFormat($field)==EDIT_FORMAT_CHECKBOX && $format==FORMAT_NONE)
	{
		if($ret && $ret!=0)
			$ret=##message YES##;
		else
			$ret=##message NO##;
	}
	else if($format == FORMAT_CUSTOM)
	{
		if($data)
			$ret = CustomExpression($value,$data,$field,"");
	}
	return $ret;
}


function ProcessLargeText($strValue,$iquery="",$table="", $mode=MODE_LIST)
{
	global $strTableName;

	$cNumberOfChars = GetNumberOfChars($table);
	if(substr($strValue,0,8)=="<a href=")
		return $strValue;
	if(substr($strValue,0,23)=="<img src=\"images/check_")
		return $strValue;
	if($cNumberOfChars>0 && strlen($strValue)>$cNumberOfChars && (strlen($strValue)<200 || !strlen($iquery)) && $mode==MODE_LIST)
	{
		$ret = substr($strValue,0,$cNumberOfChars );
		$ret=htmlspecialchars($ret);
		$ret.=" <a href=\"#\" onClick=\"javascript: pwin = window.open('',null,'height=300,width=400,status=yes,resizable=yes,toolbar=no,menubar=no,location=no,left=150,top=200,scrollbars=yes'); ";
		$ind = 1;
		$ret.="pwin.document.write('" . htmlspecialchars(jsreplace(nl2br(substr($strValue,0, 801)))) ."');";
//		$ret.="pwin.document.write('" . db_addslashes(str_replace("\r\n","<br>",htmlspecialchars(substr($strValue,0, 801)))) ."');";
		$ret.="pwin.document.write('<br><hr size=1 noshade><a href=# onClick=\\'window.close();return false;\\'>".##message CLOSE_WINDOW##."</a>');";
		$ret.="return false;\">".##message MORE##." ...</a>";
	}
	else if($cNumberOfChars>0 && strlen($strValue)>$cNumberOfChars && $mode==MODE_LIST)
	{
		$table = GetTableURL($table);
		$ret = substr($strValue,0,$cNumberOfChars );
		$ret=htmlspecialchars($ret);
		$ret.=" <a href=#  onClick=\"javascript: pwin = window.open('',null,'height=300,width=400,status=yes,resizable=yes,toolbar=no,menubar=no,location=no,left=150,top=200,scrollbars=yes');";
		$ret.=" pwin.location='".$table."_fulltext.##@ext##?".$iquery."'; return false;\">".##message MORE##." ...</a>";
	}
	else if($cNumberOfChars>0 && strlen($strValue)>$cNumberOfChars && $mode==MODE_PRINT)
	{
		$ret = substr($strValue,0,$cNumberOfChars );
		$ret=htmlspecialchars($ret);
		if(strlen($strValue)>$cNumberOfChars)
			$ret.=" ...";
	}
	else
		$ret= htmlspecialchars($strValue);

/*
//	highlight search results
	if ($mode==MODE_LIST && $_SESSION[$strTableName."_search"]==1)
	{
		$ind = 0;
		$searchopt=$_SESSION[$strTableName."_searchoption"];
		$searchfor=$_SESSION[$strTableName."_searchfor"];
//		highlight Contains search
		if($searchopt=="Contains")
		{
			while ( ($ind = my_stripos($ret, $searchfor, $ind)) !== false )
			{
				$ret = substr($ret, 0, $ind) . "<span class=highlight>". substr($ret, $ind, strlen($searchfor)) ."</span>" . substr($ret, $ind + strlen($searchfor));
				$ind+= strlen("<span class=highlight>") + strlen($searchfor) + strlen("</span>");
			}
		}
//		highlight Starts with search
		elseif($searchopt=="Starts with ...")
		{
			if( !strncasecmp($ret, $searchfor,strlen($searchfor)) )
				$ret = "<span class=highlight>". substr($ret, 0, strlen($searchfor)) ."</span>" . substr($ret, strlen($searchfor));
		}
		elseif($searchopt=="Equals")
		{
			if( !strcasecmp($ret, $searchfor) )
				$ret = "<span class=highlight>". $ret ."</span>";
		}
		elseif($searchopt=="More than ...")
		{
			if( strtoupper($ret)>strtoupper($searchfor) )
				$ret = "<span class=highlight>". $ret ."</span>";
		}
		elseif($searchopt=="Less than ...")
		{
			if( strtoupper($ret)<strtoupper($searchfor) )
				$ret = "<span class=highlight>". $ret ."</span>";
		}
		elseif($searchopt=="Equal or more than ...")
		{
			if( strtoupper($ret)>=strtoupper($searchfor) )
				$ret = "<span class=highlight>". $ret ."</span>";
		}
		elseif($searchopt=="Equal or less than ...")
		{
			if( strtoupper($ret)<=strtoupper($searchfor) )
				$ret = "<span class=highlight>". $ret ."</span>";
		}
	}
*/
	return nl2br($ret);
}

//	construct hyperlink
function GetHyperlink($str, $field,$data,$table="")
{
	global $strTableName;
	if(!strlen($table))
		$table=$strTableName;
	if(!strlen($str))
		return "";
	$ret=$str;
	$title=$ret;
	$link=$ret;
	if(substr($ret,strlen($ret)-1)=='#')
	{
		$i=strpos($ret,'#');
		$title=substr($ret,0,$i);
		$link=substr($ret,$i+1,strlen($ret)-$i-2);
		if(!$title)
			$title=$link;
	}
	$target="";
	
##foreach @BUILDER.Tables as @t filter @t.arrFieldObj[strViewFormat==FORMAT_HYPERLINK].len##
	if("##@t.strDataSourceTable s##"==$table) {
	##foreach @t.arrFieldObj as @f filter @f.strViewFormat==FORMAT_HYPERLINK##
		if($field=="##@f.strName##")
		{
		##if @f.bhlNewWindow##
			$target = " target=_blank";
		##else##
			$target = "";
		##endif##
			$type = ##@f.nhlDisplay##;
			if($type==1) $title = "##@f.strhlParam s##";
			if($type==2) $title = $data["##@f.strhlParam s##"];
			$prefix="##@f.strhlPrefix s##";
		}
	##endfor##
	} 
##endfor##
	if(strpos($link,"://")===false && substr($link,0,7)!="mailto:")
		$link=$prefix.$link;
	$ret='<a href="'.$link.'"'.$target.'>'.$title.'</a>';
	return $ret;
}

//	add prefix to the URL
function AddLinkPrefix($field,$link,$table="")
{
	if(strpos($link,"://")===false && substr($link,0,7)!="mailto:")
		return GetLinkPrefix($field,$table).$link;
	return $link;
}

function GetTotalsForTime($value,$arr)
{
	$nsec=0;
	$nmin=0;
	if($value!='')
	{
		$time=parsenumbers($value);
		if(!empty($time))
		{
			if(count($time)==3 && is_numeric($time[2]))
			{
				$nsec=$arr[2]+$time[2];
				if($nsec>59)
				{	
					$arr[2]=$nsec-60;
					$time[1]+=1;
				}
				else $arr[2]+=$time[2];
			}
			if(count($time)>1 && is_numeric($time[1]))
			{
				$nmin=$arr[1]+$time[1];  
				if($nmin>59)
				{
					$arr[1]=$nmin-60;
					$time[0]+=1;	
				}
				else $arr[1]+=$time[1];
			}
			if(is_numeric($time[0]))
				$arr[0]+=$time[0];
		}
	}
	return $arr;
}


//	return Totals string
function GetTotals($field,$value, $stype, $iNumberOfRows,$sFormat)
{
	$days=0;
	if($stype=="AVERAGE")
	{
		if($iNumberOfRows)
		{	
			if($sFormat == FORMAT_TIME)
			{
				if($value!='')
				{
					$pr=parsenumbers($value);
					if(!empty($pr) && count($pr)==3)
					{
						$avhor=round($pr[0]/$iNumberOfRows,0);
						if($avhor>23)
						{
							$days=floor($avhor/24);
							$avhor=$avhor-$days*24;
						}
						$avmin=round($pr[1]/$iNumberOfRows,0);
						$avsec=round($pr[2]/$iNumberOfRows,0);
						$value=($days!=0 ? $days.'d ' : '').$avhor.':'.($avmin>9 ? $avmin : ($avmin==0 ? '00' : '0'.$avmin)).':'.($avsec>9 ? $avsec : ($avsec==0 ? '00' : '0'.$avsec));
					}
				}
			}
			else $value=round($value/$iNumberOfRows,2);	
		}
		else
			return "";
	}
	if($stype=="TOTAL")
	{
		if($sFormat == FORMAT_TIME)
		{
			if($value!='')
			{
				$pr=parsenumbers($value);
				if(!empty($pr)&& count($pr)==3)
				{
					if($pr[0]>23)
					{	
						$days=floor($pr[0]/24);
						$pr[0]=$pr[0]-$days*24;
					}
					$value=($days!=0 ? $days.'d ' :'').($pr[0]==0 ? '00' : $pr[0]).':'.($pr[1]==0 ? '00' : ($pr[1]>9 ? $pr[1] : '0'.$pr[1])).':'.($pr[2]==0 ? '00' : ($pr[2]>9 ? $pr[2] : '0'.$pr[2]));
				}
			}
		}
	}
	$sValue="";
	$data=array($field=>$value);
	if($sFormat == FORMAT_CURRENCY)
	 	$sValue = str_format_currency($value);
	else if($sFormat == FORMAT_PERCENT)
		$sValue = str_format_number($value*100)."%"; 
	else if($sFormat == FORMAT_NUMBER)
 		$sValue = str_format_number($value);
	else if($sFormat == FORMAT_CUSTOM && $stype!="COUNT")
 		$sValue = GetData($data,$field,$sFormat);
	else 
 		$sValue = $value;

	if($stype=="COUNT") 
		return $value;
	if($stype=="TOTAL") 
		return $sValue;
	if($stype=="AVERAGE") 
		return $sValue;
	return "";
}


//	display Lookup Wizard value in List/View mode
function DisplayLookupWizard($field, $value, $data, $keylink, $mode)
{
	global $conn;
	if(!strlen($value))
		return "";
	$LookupSQL="SELECT ";
	$LookupSQL.=GetLWDisplayField($field);
	$LookupSQL.=" FROM ".AddTableWrappers(GetLookupTable($field))." WHERE ";
	$where="";
	$lookupvalue=$value;
	if(Multiselect($field))
	{
		$arr = splitvalues($value);
		$numeric=true;
		$type = GetLWLinkFieldType($field);
		if(!$type)
		{
			foreach($arr as $val)
				if(strlen($val) && !is_numeric($val))
				{
					$numeric=false;
					break;
				}
		}
		else
			$numeric = !NeedQuotes($type);
		$in="";
		foreach($arr as $val)
		{
			if($numeric && !strlen($val))
				continue;
			if(strlen($in))
				$in.=",";
			if($numeric)
				$in.=($val+0);
			else
				$in.="'".db_addslashes($val)."'";
		}
		if(strlen($in))
		{
			$LookupSQL.= GetLWLinkField($field)." in (".$in.")";
			$where = GetLWWhere($field);
			if(strlen($where))
				$LookupSQL.=" and (".$where.")";
			LogInfo($LookupSQL);
			$rsLookup = db_query($LookupSQL,$conn);
			$found=false;
			$out="";
			while($lookuprow=db_fetch_numarray($rsLookup))
			{
				$lookupvalue=$lookuprow[0];
				if($found)
					$out.=",";
				$found = true;
				$out.=GetDataInt($lookupvalue,$data,$field, ViewFormat($field));
			}
			if($found)
			{
				if(NeedEncode($field) && $mode!=MODE_EXPORT)
					return ProcessLargeText($out,"field=".htmlspecialchars(rawurlencode($field)).$keylink,"",$mode);
				else
					return $out;
			}
		}
	}
	else
	{
		$strdata = make_db_value($field,$value);
		$LookupSQL.=GetLWLinkField($field)." = " . $strdata;
		$where = GetLWWhere($field);
		if(strlen($where))
			$LookupSQL.=" and (".$where.")";
		LogInfo($LookupSQL);
		$rsLookup = db_query($LookupSQL,$conn);
		if($lookuprow=db_fetch_numarray($rsLookup))
			$lookupvalue=$lookuprow[0];
	}
	if(NeedEncode($field) && $mode!=MODE_EXPORT)
		$value=ProcessLargeText(GetDataInt($lookupvalue,$data,$field, ViewFormat($field)),"field=".htmlspecialchars(rawurlencode($field)).$keylink,"",$mode);
	else
		$value=GetDataInt($lookupvalue,$data,$field, ViewFormat($field));
	return $value;
}

function DisplayNoImage()
{
	$path = GetAbsoluteFileName("images/no_image.gif");
	$img=myfile_get_contents($path,"r");
	header("Content-Type: image/gif");
	echo $img;
}

function DisplayFile()
{
	$path = GetAbsoluteFileName("images/file.gif");
	$img=myfile_get_contents($path,"r");
	header("Content-Type: image/gif");
	echo $img;
}

function echobig($string, $bufferSize = 8192)
{
	for ($chars=strlen($string)-1,$start=0;$start <= $chars;$start += $bufferSize) 
		echo substr($string,$start,$bufferSize);
}

////////////////////////////////////////////////////////////////////////////////
// miscellaneous functions
////////////////////////////////////////////////////////////////////////////////



//	analog of strrpos function
function my_strrpos($haystack, $needle) {
   $index = strpos(strrev($haystack), strrev($needle));
   if($index === false) {
       return false;
   }
   $index = strlen($haystack) - strlen($needle) - $index;
   return $index;
}

//	utf-8 analog of strlen function
function strlen_utf8($str)
{
	$len=0;
	$i=0;
	$olen=strlen($str);
	while($i<$olen)
	{
		$c=ord(substr($str,$i,1));
		if($c<128)
			$i++;
		else if($i<$olen-1 && $c>=192 && $c<=223)
			$i+=2;
		else if($i<$olen-2 && $c>=224 && $c<=239)
			$i+=3;
		else if($i<$olen-3 && $c>=240)
			$i+=4;
		else
			break;
		$len++;
	}
	return $len;
}

//	utf-8 analog of substr function
function substr_utf8($str,$index,$strlen)
{
	if($strlen<=0)
		return "";
	$len=0;
	$i=0;
	$olen=strlen($str);
	$oindex=-1;
	while($i<$olen)
	{
		if($len==$index)
			$oindex=$i;
		
		$c=ord(substr($str,$i,1));
		if($c<128)
			$i++;
		else if($i<$olen-1 && $c>=192 && $c<=223)
			$i+=2;
		else if($i<$olen-2 && $c>=224 && $c<=239)
			$i+=3;
		else if($i<$olen-3 && $c>=240)
			$i+=4;
		else
			break;
		$len++;
		if($oindex>=0 && $len==$index+$strlen)
			return substr($str,$oindex,$i-$oindex);
	}
	if($oindex>0)
		return substr($str,$oindex,$olen-$oindex);
	return "";
}

//	construct "good" field name
function GoodFieldName($field)
{
	$field=(string)$field;	
	$out="";
	for($i=0;$i<strlen($field);$i++)
	{
		$t=substr($field,$i,1);
		if((ord($t)<ord('a') || ord($t)>ord('z')) && (ord($t)<ord('A') || ord($t)>ord('Z')) && (ord($t)<ord('0') || ord($t)>ord('9')))
			$out.='_';
		else
			$out.=$t;
	}
	return $out;
}

//	prepare string for JavaScript. Replace ' with \' and linebreaks with \r\n
function jsreplace($str)
{
	$ret= str_replace(array("\\","'","\r","\n"),array("\\\\","\\'","\\r","\\n"),$str);
	return my_str_ireplace("</script>","</scr'+'ipt>",$ret);
}


function LogInfo($SQL)
{
	global $dSQL,$dDebug;
	$dSQL=$SQL;
	if($dDebug)
	{
		echo $dSQL;
		echo "<br>";
	}
}


//	check if file extension is image extension
function CheckImageExtension($filename)
{
	if(strlen($filename)<4)
		return false;
	$ext=strtoupper(substr($filename,strlen($filename)-4));
	if($ext==".GIF" || $ext==".JPG" || $ext=="JPEG" || $ext==".PNG" || $ext==".BMP")
		return $ext;
	return false;
} 























































































function RTESafe($strText)
{
//	returns safe code for preloading in the RTE
	$tmpString="";
	
	$tmpString = trim($strText);
	if(!$tmpString) return "";
	
//	convert all types of single quotes
	$tmpString = str_replace( chr(145), chr(39),$tmpString);
	$tmpString = str_replace( chr(146), chr(39),$tmpString);
	$tmpString = str_replace("'", "&#39;",$tmpString);
	
//	convert all types of double quotes
	$tmpString = str_replace(chr(147), chr(34),$tmpString);
	$tmpString = str_replace(chr(148), chr(34),$tmpString);
	
//	replace carriage returns & line feeds
	$tmpString = str_replace(chr(10), " ",$tmpString);
	$tmpString = str_replace(chr(13), " ",$tmpString);
	
	return $tmpString;
}



function html_special_decode($str)
{
	$ret=$str;
	$ret=str_replace("&gt;",">",$ret);
	$ret=str_replace("&lt;","<",$ret);
	$ret=str_replace("&quot;","\"",$ret);
	$ret=str_replace("&#039;","'",$ret);
	$ret=str_replace("&#39;","'",$ret);
	$ret=str_replace("&amp;","&",$ret);
	return $ret;
}

////////////////////////////////////////////////////////////////////////////////
// database and SQL related functions
////////////////////////////////////////////////////////////////////////////////

function CalcSearchParameters()
{
	global $strTableName, $strSQL;
	$sWhere="";
	if(@$_SESSION[$strTableName."_search"]==2)
//	 advanced search
	{
		foreach(@$_SESSION[$strTableName."_asearchfor"] as $f => $sfor)
		{
			$strSearchFor=trim($sfor);
			$strSearchFor2="";
			$type=@$_SESSION[$strTableName."_asearchfortype"][$f];
			if(array_key_exists($f,@$_SESSION[$strTableName."_asearchfor2"]))
				$strSearchFor2=trim(@$_SESSION[$strTableName."_asearchfor2"][$f]);
			if($strSearchFor!="" || true)
			{
				if (!$sWhere) 
				{
					if($_SESSION[$strTableName."_asearchtype"]=="and")
						$sWhere="1=1";
					else
						$sWhere="1=0";
				}
				$strSearchOption=trim($_SESSION[$strTableName."_asearchopt"][$f]);
				if($where=StrWhereAdv($f, $strSearchFor, $strSearchOption, $strSearchFor2,$type))
				{
					if($_SESSION[$strTableName."_asearchnot"][$f])
						$where="not (".$where.")";
					if($_SESSION[$strTableName."_asearchtype"]=="and")
	   					$sWhere .= " and ".$where;
					else
	   					$sWhere .= " or ".$where;
				}
			}
		}
	}
	return $sWhere;
}

//	add WHERE condition to gstrSQL
function gSQLWhere($where)
{
	global $gsqlHead,$gsqlFrom,$gsqlWhereExpr,$gsqlTail;
	return gSQLWhere_int($gsqlHead,$gsqlFrom,$gsqlWhereExpr,$gsqlTail,$where);
}

function gSQLWhere_int($sqlHead,$sqlFrom,$sqlWhere,$sqlTail,$where)
{
	$strWhere=whereAdd($sqlWhere,$where);
	if(strlen($strWhere))
		$strWhere=" where ".$strWhere." ";
	
	return $sqlHead.$sqlFrom.$strWhere.$sqlTail;
}

//	add clause to WHERE expression
function whereAdd($where,$clause)
{
	if(!strlen($clause))
		return $where;
	if(!strlen($where))
		return $clause;
	return "(".$where.") and (".$clause.")";
}

//	add WHERE clause to SQL string
function AddWhere($sql,$where)
{
	if(!strlen($where))
		return $sql;
	$sql=str_replace(array("\r\n","\n","\t")," ",$sql);
	$tsql = strtolower($sql);
	$n = my_strrpos($tsql," where ");
	$n1 = my_strrpos($tsql," group by ");
	$n2 = my_strrpos($tsql," order by ");
	if($n1===false)
		$n1=strlen($tsql);
	if($n2===false)
		$n2=strlen($tsql);
	if ($n1>$n2)
		$n1=$n2;
	if($n===false)
		return substr($sql,0,$n1)." where ".$where.substr($sql,$n1);
	else
		return substr($sql,0,$n+strlen(" where "))."(".substr($sql,$n+strlen(" where "),$n1-$n-strlen(" where ")).") and (".$where.")".substr($sql,$n1);
}

//	construct WHERE clause with key values
function KeyWhere(&$keys, $table="")
{
	global $strTableName;
	if(!$table)
		$table=$strTableName;
	$strWhere="";
##foreach @BUILDER.Tables as @t##

//	##@t.strDataSourceTable##
	if($table=="##@t.strDataSourceTable s##")
	{
	##foreach @t.arrKeyFields as @k##
	##if !@first##
		$strWhere.=" and ";
	##endif##
		$value=make_db_value("##@k s##",$keys["##@k s##"]);
		##if @BUILDER.nDatabaseType==nDATABASE_Oracle##
		$valueisnull = ($value==="null" || $value=="''");
		##else##
		$valueisnull = ($value==="null");
		##endif##
		if($valueisnull)
			$strWhere.=GetFullFieldName("##@k s##")." is null";
		else
			$strWhere.=GetFullFieldName("##@k s##")."=".make_db_value("##@k s##",$keys["##@k s##"]);
	##endfor##		
	}
##endfor##		
	return $strWhere;
}

//	consctruct SQL WHERE clause for simple search
function StrWhereExpression($strField, $SearchFor, $strSearchOption, $SearchFor2)
{
	global $strTableName;
	$type=GetFieldType($strField);
	
##if @BUILDER.nDatabaseType!=nDATABASE_MSSQLServer##
	$ismssql=false;
##else##
	$ismssql=true;
##endif##		
	
	$btexttype=IsTextType($type);
##if @BUILDER.nDatabaseType==nDATABASE_MySQL##
	$btexttype=false;
##endif##

	if($strSearchOption=='Empty')
	{
		if(IsCharType($type) && (!$ismssql || !$btexttype))
			return "(".GetFullFieldName($strField)." is null or ".GetFullFieldName($strField)."='')";			
		elseif ($ismssql && $btexttype)	
			return "(".GetFullFieldName($strField)." is null or ".GetFullFieldName($strField)." LIKE '')";
		else
			return GetFullFieldName($strField)." is null";
	}
	$strQuote="";
	if(NeedQuotes($type))
		$strQuote = "'";
//	return none if trying to compare numeric field and string value
	$sSearchFor=$SearchFor;
	$sSearchFor2=$SearchFor2;
	if(IsBinaryType($type))
		return "";
	

##if @BUILDER.nDatabaseType==nDATABASE_MSSQLServer##
	if($btexttype && $strSearchOption!="Contains" && $strSearchOption!="Starts with ..." )
		return "";
##endif##
	
	if(IsDateFieldType($type) && $strSearchOption!="Contains" && $strSearchOption!="Starts with ..." )
	{
		$time=localdatetime2db($SearchFor);
		if($time=="null")
			return "";
		$sSearchFor=db_datequotes($time);
		if($strSearchOption=="Between")
		{
			$time=localdatetime2db($SearchFor2);
			if($time=="null")
				$sSearchFor2="";
			else
				$sSearchFor2=db_datequotes($time);
		}
	}
	
	if(!$strQuote && !is_numeric($sSearchFor) && !is_numeric($sSearchFor))
		return "";
	else if(!$strQuote && $strSearchOption!="Contains" && $strSearchOption!="Starts with ...")
	{
		$sSearchFor = 0+$sSearchFor;
		$sSearchFor2 = 0+$sSearchFor2;
	}
	else if(!IsDateFieldType($type) && $strSearchOption!="Contains" && $strSearchOption!="Starts with ...")
	{
		if(!$btexttype)
		{
			$sSearchFor=$strQuote.db_addslashes($sSearchFor).$strQuote;
			if($strSearchOption=="Between" && $sSearchFor2)
				$sSearchFor2=$strQuote.db_addslashes($sSearchFor2).$strQuote;
		}
		else
		{
			$sSearchFor=db_upper($strQuote.db_addslashes($sSearchFor).$strQuote);
			if($strSearchOption=="Between" && $sSearchFor2)
				$sSearchFor2=db_upper($strQuote.db_addslashes($sSearchFor2).$strQuote);
		}
	}
	else if(!IsDateFieldType($type) || $strSearchOption=="Contains" || $strSearchOption=="Starts with ..." )
		$sSearchFor=db_addslashes($sSearchFor);
		

	if(IsCharType($type) && !$btexttype)
		$strField=db_upper(GetFullFieldName($strField));
	elseif ($ismssql && !$btexttype && ($strSearchOption=="Contains" || $strSearchOption=="Starts with ..."))
		$strField="convert(varchar(50),".GetFullFieldName($strField).")";
	else 
		$strField=GetFullFieldName($strField);
	$ret="";
	##if @BUILDER.nDatabaseType==nDATABASE_PostgreSQL##
	$like="ilike";
	##else##
	$like="like";
	##endif##
	if($strSearchOption=="Contains")
	{
		if(IsCharType($type) && !$btexttype)
			return $strField." ".$like." ".db_upper("'%".$sSearchFor."%'");
		else
			return $strField." ".$like." '%".$sSearchFor."%'";
	}
	else if($strSearchOption=="Equals") return $strField."=".$sSearchFor;
	else if($strSearchOption=="Starts with ...")
	{
		if(IsCharType($type) && !$btexttype)
			return $strField." ".$like." ".db_upper("'".$sSearchFor."%'");
		else
			return $strField." ".$like." '".$sSearchFor."%'";
	}
	else if($strSearchOption=="More than ...") return $strField.">".$sSearchFor;
	else if($strSearchOption=="Less than ...") return $strField."<".$sSearchFor;
	else if($strSearchOption=="Equal or more than ...") return $strField.">=".$sSearchFor;
	else if($strSearchOption=="Equal or less than ...") return $strField."<=".$sSearchFor;
	else if($strSearchOption=="Between")
	{
		$ret=$strField.">=".$sSearchFor;
		if($sSearchFor2) $ret.=" and ".$strField."<=".$sSearchFor2;
			return $ret;
	}
	return "";
}

//	construct SQL WHERE clause for Advanced search
function StrWhereAdv($strField, $SearchFor, $strSearchOption, $SearchFor2, $etype)
{
	global $strTableName;
	$type=GetFieldType($strField);

##if @BUILDER.nDatabaseType!=nDATABASE_MSSQLServer##
	$ismssql=false;
##else##
	$ismssql=true;
##endif##		
	
	$btexttype=IsTextType($type);
##if @BUILDER.nDatabaseType==nDATABASE_MySQL##
	$btexttype=false;
##endif##

	if(IsBinaryType($type))
		return "";
##if @BUILDER.nDatabaseType==nDATABASE_MSSQLServer##
	if($btexttype && $strSearchOption!="Contains" && $strSearchOption!="Starts with ..." )
		return "";
##endif##
	if($strSearchOption=='Empty')
	{
		if(IsCharType($type) && (!$ismssql || !$btexttype))
			return "(".GetFullFieldName($strField)." is null or ".GetFullFieldName($strField)."='')";			
		elseif ($ismssql && $btexttype)	
			return "(".GetFullFieldName($strField)." is null or ".GetFullFieldName($strField)." LIKE '')";
		else
			return GetFullFieldName($strField)." is null";
	}
	##if @BUILDER.nDatabaseType==nDATABASE_PostgreSQL##
	$like="ilike";
	##else##
	$like="like";
	##endif##
	if(GetEditFormat($strField)==EDIT_FORMAT_LOOKUP_WIZARD)
	{
		
		if(Multiselect($strField))
			$SearchFor=splitvalues($SearchFor);
		else
			$SearchFor=array($SearchFor);
		$ret="";
		foreach($SearchFor as $value)
		{
			if(!($value=="null" || $value=="Null" || $value==""))
			{
				if(strlen($ret))
					$ret.=" or ";
				if($strSearchOption=="Equals")
				{
					$value=make_db_value($strField,$value);
					if(!($value=="null" || $value=="Null"))
						$ret.=GetFullFieldName($strField).'='.$value;
				}
				else
				{
					if(strpos($value,",")!==false || strpos($value,'"')!==false)
						$value = '"'.str_replace('"','""',$value).'"';
					$value=db_addslashes($value);
					$ret.=GetFullFieldName($strField)." = '".$value."'";
					$ret.=" or ".GetFullFieldName($strField)." ".$like." '%,".$value.",%'";
					$ret.=" or ".GetFullFieldName($strField)." ".$like." '%,".$value."'";
					$ret.=" or ".GetFullFieldName($strField)." ".$like." '".$value.",%'";
				}
			}
		}
		if(strlen($ret))
			$ret="(".$ret.")";
		return $ret;
	}
	if(GetEditFormat($strField)==EDIT_FORMAT_CHECKBOX)
	{
		if($SearchFor=="none")
			return "";
		if(NeedQuotes($type))
		{
			if($SearchFor=="on")
				return "(".GetFullFieldName($strField)."<>'0' and ".GetFullFieldName($strField)."<>'' and ".GetFullFieldName($strField)." is not null)";
			else
				return "(".GetFullFieldName($strField)."='0' or ".GetFullFieldName($strField)."='' or ".GetFullFieldName($strField)." is null)";
		}
		else
		{
			if($SearchFor=="on")
				return "(".GetFullFieldName($strField)."<>0 and ".GetFullFieldName($strField)." is not null)";
			else
				return "(".GetFullFieldName($strField)."=0 or ".GetFullFieldName($strField)." is null)";
		}
	}
	$value1=make_db_value($strField,$SearchFor,$etype);
	$value2=false;
	if($strSearchOption=="Between")
		$value2=make_db_value($strField,$SearchFor2,$etype);
	if($strSearchOption!="Contains" && $strSearchOption!="Starts with ..." && ($value1==="null" || $value2==="null" ))
		return "";

	if(IsCharType($type) && !$btexttype)
	{
		$value1=db_upper($value1);
		$value2=db_upper($value2);
		$strField=db_upper(GetFullFieldName($strField));
	}
	elseif ($ismssql && !$btexttype && ($strSearchOption=="Contains" || $strSearchOption=="Starts with ..."))
		$strField="convert(varchar,".GetFullFieldName($strField).")";
	else 
		$strField=GetFullFieldName($strField);
	$ret="";
	if($strSearchOption=="Contains")
	{
		if(IsCharType($type) && !$btexttype)
			return $strField." ".$like." ".db_upper("'%".db_addslashes($SearchFor)."%'");
		else
			return $strField." ".$like." '%".db_addslashes($SearchFor)."%'";
	}
	else if($strSearchOption=="Equals") return $strField."=".$value1;
	else if($strSearchOption=="Starts with ...")
	{
		if(IsCharType($type) && !$btexttype)
			return $strField." ".$like." ".db_upper("'".db_addslashes($SearchFor)."%'");
		else
			return $strField." ".$like." '".db_addslashes($SearchFor)."%'";
	}
	else if($strSearchOption=="More than ...") return $strField.">".$value1;
	else if($strSearchOption=="Less than ...") return $strField."<".$value1;
	else if($strSearchOption=="Equal or more than ...") return $strField.">=".$value1;
	else if($strSearchOption=="Equal or less than ...") return $strField."<=".$value1;
	else if($strSearchOption=="Between")
	{
		$ret=$strField.">=".$value1;
		$ret.=" and ".$strField."<=".$value2;
		return $ret;
	}
	return "";
}

//	get count of rows from the query
function gSQLRowCount($where,$groupby=false)
{
	global $gsqlHead,$gsqlFrom,$gsqlWhereExpr,$gsqlTail;
	return gSQLRowCount_int($gsqlHead,$gsqlFrom,$gsqlWhereExpr,$gsqlTail,$where,$groupby);
}

function gSQLRowCount_int($sqlHead,$sqlFrom,$sqlWhere,$sqlTail,$where,$groupby=false)
{
	global $conn;
	global $bSubqueriesSupported;
	
	$strWhere=whereAdd($sqlWhere,$where);
	if(strlen($strWhere))
		$strWhere=" where ".$strWhere." ";
	
	if($groupby)
	{
	##if @BUILDER.nDatabaseType!=nDATABASE_MySQL##
		$countstr = "select count(*) from (".gSQLWhere_int($sqlHead,$sqlFrom,$sqlWhere,$sqlTail,$where).") a";
	##else##
		if($bSubqueriesSupported)
			$countstr = "select count(*) from (".gSQLWhere_int($sqlHead,$sqlFrom,$sqlWhere,$sqlTail,$where).") a";
		else
		{
			$countstr = gSQLWhere_int($sqlHead,$sqlFrom,$sqlWhere,$sqlTail,$where);
			return GetMySQL4RowCount($countstr);
		}
	##endif##
	}
	else
		$countstr = "select count(*) ".$sqlFrom.$strWhere.$sqlTail;
		
	$countrs = db_query($countstr,$conn);
	$countdata = db_fetch_numarray($countrs);
	return $countdata[0];
}

//	get count of rows from the query
function GetRowCount($strSQL)
{
	global $conn;
	$strSQL=str_replace(array("\r\n","\n","\t")," ",$strSQL);
	$tstr = strtoupper($strSQL);
	$ind1 = strpos($tstr,"SELECT ");
	$ind2 = my_strrpos($tstr," FROM ");
	$ind3 = my_strrpos($tstr," GROUP BY ");
	if($ind3===false)
	{
		$ind3 = strpos($tstr," ORDER BY ");
		if($ind3===false)
			$ind3=strlen($strSQL);
	}
	$countstr=substr($strSQL,0,$ind1+6)." count(*) ".substr($strSQL,$ind2+1,$ind3-$ind2);
	$countrs = db_query($countstr,$conn);
	$countdata = db_fetch_numarray($countrs);
	return $countdata[0];
}

//	add MSSQL Server TOP clause
function AddTop($strSQL, $n)
{
	$tstr = strtoupper($strSQL);
	$ind1 = strpos($tstr,"SELECT");
	return substr($strSQL,0,$ind1+6)." top ".$n." ".substr($strSQL,$ind1+6);
}

//	add Oracle ROWNUMBER checking
function AddRowNumber($strSQL, $n)
{
	return "select * from (".$strSQL.") where rownum<".($n+1);
}

// test database type if values need to be quoted
function NeedQuotesNumeric($type)
{
    if($type == 203 || $type == 8 || $type == 129 || $type == 130 || 
		$type == 7 || $type == 133 || $type == 134 || $type == 135 ||
		$type == 201 || $type == 205 || $type == 200 || $type == 202 || $type==72 || $type==13)
		return true;
	else
		return false;
}

//	using ADO DataTypeEnum constants
//	the full list available at:
//	http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ado270/htm/mdcstdatatypeenum.asp

function IsNumberType($type)
{
	if($type==20 || $type==6 || $type==14 || $type==5 || $type==10 
	|| $type==3 || $type==131 || $type==4 || $type==2 || $type==16
	|| $type==21 || $type==19 || $type==18 || $type==17 || $type==139
	|| $type==11)
		return true;
	return false;
}

function IsFloatType($type)
{
	if($type==14 || $type==5 || $type==131 || $type==6)
		return true;
	return false;
}


function NeedQuotes($type)
{
	return !IsNumberType($type);
}

function IsBinaryType($type)
{
	if($type==128 || $type==205 || $type==204)
		return true;
	return false;
}

function IsDateFieldType($type)
{
	if($type==7 || $type==133 || $type==135)
		return true;
	return false;
}

function IsTimeType($type)
{
	if($type==134)
		return true;
	return false;
}

function IsCharType($type)	
{
	if(IsTextType($type) || $type==8 || $type==129 || $type==200 || $type==202 || $type==130)
		return true;
	return false;
}

function IsTextType($type)
{
	if($type==201 || $type==203)
		return true;
	return false;
}

////////////////////////////////////////////////////////////////////////////////
// security functions
////////////////////////////////////////////////////////////////////////////////

##if @BUILDER.bCreateLoginPage##

//	return user permissions on the table
//	A - Add
//	D - Delete
//	E - Edit
//	S - List/View/Search
//	P - Print/Export

##if @BUILDER.bDynamicPermissions##
function ReadUserPermissions($userID="")
{
	global $conn,$gPermissionsRead, $gPermissionsRefreshTime;
	
	if(!isset($_SESSION["UserRights"]) || (!$gPermissionsRead && time()-@$_SESSION["LastReadRights"]>$gPermissionsRefreshTime))
	{
		if (!strlen($userID))
			$userID=$_SESSION["UserID"];
		
		$groups=array();
		$bIsAdmin=false;
		if($userID!="Guest")
		{
			$sql = "select GroupID from ##@BUILDER.strUGMembers ts## where UserName='".db_addslashes($userID)."'";
			$rs = db_query($sql,$conn);
			while($data=db_fetch_numarray($rs))
				$groups[]=$data[0];
			if(!count($groups))
				$groups[]=-2;
		}
		else
			$groups[]=-3;
		$groupstr="";
		foreach($groups as $g)
		{
			if($groupstr!="")
				$groupstr.=",";
			$groupstr.=$g;
			if($g==-1)
				$bIsAdmin=true;
		}
		$rights=array();
		$sql="select TableName,AccessMask from ##@BUILDER.strUGRights ts## where GroupID in (".$groupstr.")";
		$rs = db_query($sql,$conn);
		while($data=db_fetch_numarray($rs))
		{
			if(!array_key_exists($data[0],$rights))
			{
				$rights[$data[0]]=$data[1];
				continue;
			}
			for($i=0;$i<strlen($data[1]);$i++)
			{
				if(strpos($rights[$data[0]],substr($data[1],$i,1))===false)
					$rights[$data[0]].=substr($data[1],$i,1);
			}
		}
		$_SESSION["UserRights"]=$rights;
		$_SESSION["IsAdmin"]=$bIsAdmin;
		$_SESSION["LastReadRights"]=time();
		
		$gPermissionsRead=true;
	}
}

function getGuestPermissions() 
{	
	ReadUserPermissions("Guest");
	return $_SESSION["UserRights"];
}


function GetUserPermissionsDynamic($table="")
{
	global $strTableName,$gPermissionsRefreshTime,$gPermissionsRead;
	if(!$table)
		$table=$strTableName;
	//if(!isset($_SESSION["UserRights"]) || (!$gPermissionsRead && time()-@$_SESSION["LastReadRights"]>$gPermissionsRefreshTime))
		ReadUserPermissions();
	if(IsAdmin())
	{
##foreach @BUILDER.tables as @t filter IsAdminTable(@t)##
	if($table=="##@t.strDataSourceTable s##")
		return "ADESPIM";
##endfor##
	}
		
	return @$_SESSION["UserRights"][$table];
}

function IsAdmin()
{
	global $gPermissionsRefreshTime,$gPermissionsRead;
	//if(!isset($_SESSION["UserRights"]) || (!$gPermissionsRead && time()-@$_SESSION["LastReadRights"]>$gPermissionsRefreshTime))
		ReadUserPermissions();
	return @$_SESSION["IsAdmin"];
}

##else##


function GetUserPermissionsStatic($table="")
{
	global $strTableName;
	if(!$table)
		$table=$strTableName;
##if @BUILDER.strAdminUserID##
	if($_SESSION["AccessLevel"] == ACCESS_LEVEL_ADMIN)
			return "ADESPIM";
##endif##

	$sUserGroup=@$_SESSION["GroupID"];
##foreach @BUILDER.Tables as @t##
##foreach @BUILDER.UserGroups as @u filter @u.strUserName!="<Default>"##
	if($table=="##@t.strDataSourceTable s##" && $sUserGroup=="##@u.strUserName s##")
	{
	##if @u.strUserName=="<Guest>" && @t.nSecOptions==ADVSECURITY_VIEW_OWN##
			return "";			//	guest sees nothing
	##else##
			return "##@u.TablePermissions[strTableName=@t.strDataSourceTable].strPermissionMask##";
	##endif##
	}
##endfor##
//	default permissions	
	if($table=="##@t.strDataSourceTable s##")
	{
##if @BUILDER.UserGroups[strUserName=="<Default>"].len##
		return "##@BUILDER.UserGroups[strUserName=="<Default>"].TablePermissions[strTableName=@t.strDataSourceTable].strPermissionMask##";
##elseif @BUILDER.UserGroups.len==0 || @BUILDER.UserGroups.len==1 && @BUILDER.UserGroups[strUserName=="<Guest>"].len==1##
		return "ADESPI";	// grant all by default
##else##
		return "";		// grant nothing by default
##endif##
	}
##endfor##
}

##endif##

##endif##
function GetUserPermissions($table="")
{
##if @BUILDER.bCreateLoginPage##
##if @BUILDER.bDynamicPermissions##
	return GetUserPermissionsDynamic($table);
##else##
	return GetUserPermissionsStatic($table);
##endif##
##else##
	return "ADESPIM";
##endif##
}


//	check whether field is viewable
function CheckFieldPermissions($field, $table="")
{
	return GetFieldData($table,$field,"FieldPermissions",false);
}

// 
function CheckSecurity($strValue, $strAction)
{
global $cAdvSecurityMethod, $strTableName;
##if @BUILDER.bCreateLoginPage##
	if($_SESSION["AccessLevel"]==ACCESS_LEVEL_ADMIN)
		return true;

	$strPerm = GetUserPermissions();
	if(@$_SESSION["AccessLevel"]!=ACCESS_LEVEL_ADMINGROUP && strpos($strPerm, "M")===false)
	{
##foreach @BUILDER.Tables as @t filter @t.nSecOptions==ADVSECURITY_VIEW_OWN || @t.nSecOptions==ADVSECURITY_EDIT_OWN##
		if($strTableName=="##@t.strDataSourceTable s##")
		{
	##if @t.nSecOptions==ADVSECURITY_VIEW_OWN##
			if(!((string)$_SESSION["_".$strTableName."_OwnerID"]===(string)$strValue))
				return false;
	##elseif @t.nSecOptions==ADVSECURITY_EDIT_OWN##
			if(( $strAction=="Edit" || $strAction=="Delete") && !((string)$_SESSION["_".$strTableName."_OwnerID"]===(string)$strValue))
				return false;
	##endif##
		}
##endfor##
	}
	##if GroupSecurity##
//	 check user group permissions
	if($strAction=="Add" && !(strpos($strPerm, "A")===false) ||
	   $strAction=="Edit" && !(strpos($strPerm, "E")===false) ||
	   $strAction=="Delete" && !(strpos($strPerm, "D")===false) ||
	   $strAction=="Search" && !(strpos($strPerm, "S")===false) ||
	   $strAction=="Import" && !(strpos($strPerm, "I")===false) ||
	   $strAction=="Export" && !(strpos($strPerm, "P")===false) )
		return true;
	else
		return false;
	##endif##
##endif##
	return true;
}


//	add security WHERE clause to SELECT SQL command
function SecuritySQL($strAction, $table="")
{
global $cAdvSecurityMethod,$strTableName;
	
	if (!strlen($table))	
		$table = $strTableName;
		
##if @BUILDER.bCreateLoginPage##
   	$ownerid=@$_SESSION["_".$table."_OwnerID"];
	$ret="";
	if(@$_SESSION["AccessLevel"]==ACCESS_LEVEL_ADMIN)
		return "";
	$ret="";

	$strPerm = GetUserPermissions($table);

##if !@BUILDER.bDynamicPermissions##	
	if(@$_SESSION["AccessLevel"]!=ACCESS_LEVEL_ADMINGROUP)
	{

##foreach @BUILDER.Tables as @t filter @t.nSecOptions==ADVSECURITY_VIEW_OWN || @t.nSecOptions==ADVSECURITY_EDIT_OWN##
		if($table=="##@t.strDataSourceTable s##")
		{
	##if @t.nSecOptions==ADVSECURITY_VIEW_OWN##
			$ret=GetFullFieldName(GetTableOwnerID($table), $table)."=".make_db_value(GetTableOwnerID($table),$ownerid);
	##elseif @t.nSecOptions==ADVSECURITY_EDIT_OWN##
			if($strAction=="Edit" || $strAction=="Delete")
				$ret=GetFullFieldName(GetTableOwnerID($table), $table)."=".make_db_value(GetTableOwnerID($table),$ownerid);
	##endif##
		}
##endfor##
	}
##else##
	if(strpos($strPerm,"M")===false)
	{
##foreach @BUILDER.Tables as @t filter @t.nSecOptions==ADVSECURITY_VIEW_OWN || @t.nSecOptions==ADVSECURITY_EDIT_OWN##
		if($strTableName=="##@t.strDataSourceTable s##")
		{
	##if @t.nSecOptions==ADVSECURITY_VIEW_OWN##
			$ret=GetFullFieldName(GetTableOwnerID($table), $table)."=".make_db_value(GetTableOwnerID($table),$ownerid);
	##elseif @t.nSecOptions==ADVSECURITY_EDIT_OWN##
			if($strAction=="Edit" || $strAction=="Delete")
				$ret=GetFullFieldName(GetTableOwnerID($table), $table)."=".make_db_value(GetTableOwnerID($table),$ownerid);
	##endif##
		}
##endfor##
	}
##endif##
	
	if($strAction=="Edit" && !(strpos($strPerm, "E")===false) ||
	   $strAction=="Delete" && !(strpos($strPerm, "D")===false) ||
	   $strAction=="Search" && !(strpos($strPerm, "S")===false) ||
	   $strAction=="Export" && !(strpos($strPerm, "P")===false) )
		return $ret;
	else
		return "1=0";
##endif##
	return "";
}

////////////////////////////////////////////////////////////////////////////////
// editing functions
////////////////////////////////////////////////////////////////////////////////

function make_db_value($field,$value,$controltype="",$postfilename="",$table="")
{
	$ret=prepare_for_db($field,$value,$controltype,$postfilename,$table);
	if($ret===false)
		return $ret;
	return add_db_quotes($field,$ret,$table);
}

function add_db_quotes($field,$value,$table="")
{
	global $strTableName;
	$type=GetFieldType($field,$table);
	if(IsBinaryType($type))
		return db_addslashesbinary($value);
	if(($value==="" || $value===FALSE) && !IsCharType($type))
		return "null";
	if(NeedQuotes($type))
	{
		if(!IsDateFieldType($type))
			$value="'".db_addslashes($value)."'";
		else
			$value=db_datequotes($value);
	}
	else
	{
		$strvalue = (string)$value;
		$strvalue = str_replace(",",".",$strvalue);
		if(is_numeric($strvalue))
			$value=$strvalue;
		else
			$value=0;
//		$value=0+$strvalue;
	}
	return $value;
}


function prepare_for_db($field,$value,$controltype="",$postfilename="",$table="")
{
	global $strTableName;
	$filename="";
	$type=GetFieldType($field,$table);
	if(!$controltype || $controltype=="multiselect")
	{
		if(is_array($value))
			$value=combinevalues($value);
		if(($value==="" || $value===FALSE) && !IsCharType($type))
			return "";
		return $value;
	}
	else if(substr($controltype,0,4)=="file")
	{
		return prepare_file($value,$field,$controltype,$postfilename);
	}
	else if(substr($controltype,0,6)=="upload")
	{
		return prepare_upload($field,$controltype,$postfilename,$value,$table);
	}
	else if($controltype=="time")
	{
		if(!strlen($value))
			return "";
		$time=localtime2db($value);
		if(IsDateFieldType(GetFieldType($field,$table)))
		{
			$time="2000-01-01 ".$time;
		}
		return $time;
	}
	else if(substr($controltype,0,4)=="date")
	{
		$dformat=substr($controltype,4);
		if($dformat==EDIT_DATE_SIMPLE || $dformat==EDIT_DATE_SIMPLE_DP)
		{
			$time=localdatetime2db($value);
			if($time=="null")
				return "";
			return $time;
		}
		else if($dformat==EDIT_DATE_DD || $dformat==EDIT_DATE_DD_DP)
		{
			$a=explode("-",$value);
			if(count($a)<3)
				return "";
			else
			{
				$y=$a[0];
				$m=$a[1];
				$d=$a[2];
			}
			if($y<100)
			{
				if($y<70)
					$y+=2000;
				else
					$y+=1900;
			}
			return mysprintf("%04d-%02d-%02d",array($y,$m,$d));
		}
		else
			return "";
	}
	else if(substr($controltype,0,8)=="checkbox")
	{
		if($value=="on")
			$ret=1;
		else if($value=="none")
			return "";
		else 
			$ret=0;
		return $ret;
	}
	else
		return false;
}

//	delete uploaded files when deleting the record
function DeleteUploadedFiles($where,$table="")
{
	global $conn,$gstrSQL;
	$sql = gSQLWhere($where);
	$rs = db_query($sql,$conn);
	if(!($data=db_fetch_array($rs)))
		return;
	foreach($data as $field=>$value)
	{
		if(strlen($value) && GetEditFormat($field)==EDIT_FORMAT_FILE)
		{
			if(myfile_exists(GetUploadFolder($field).$value))
				myunlink(GetUploadFolder($field).$value);
			if(GetCreateThumbnail($field) && myfile_exists(GetUploadFolder($field).GetThumbnailPrefix($field).$value))
				myunlink(GetUploadFolder($field).GetThumbnailPrefix($field).$value);
		}
	}
}

//	combine checked values from multi-select list box
function combinevalues($arr)
{
	$ret="";
	foreach($arr as $val)
	{
		if(strlen($ret))
			$ret.=",";
		if(strpos($val,",")===false && strpos($val,'"')===false)
			$ret.=$val;
		else
		{
			$val=str_replace('"','""',$val);
			$ret.='"'.$val.'"';
		}
	}
	return $ret;
}

//	split values for multi-select list box
function splitvalues($str)
{
	$arr=array();
	$start=0;
	$i=0;
	$inquot=false;
	while($i<=strlen($str))
	{
		if($i<strlen($str) && substr($str,$i,1)=='"')
			$inquot=!$inquot;
		else if($i==strlen($str) || !$inquot && substr($str,$i,1)==',')
		{
			$val=substr($str,$start,$i-$start);
			$start=$i+1;
			if(strlen($val) && substr($val,0,1)=='"')
			{
				$val=substr($val,1,strlen($val)-2);
				$val=str_replace('""','"',$val);
			}
			$arr[]=$val;
		}
		$i++;
	}
	return $arr;
}


////////////////////////////////////////////////////////////////////////////////
// edit controls creation functions
////////////////////////////////////////////////////////////////////////////////


//	write days dropdown
function WriteDays($d)
{
	$ret='<option value=""> </option>';
	for($i=1;$i<=31;$i++)
		$ret.='<option value="'.$i.'" '.($i==$d?"selected":"").'>'.$i."</option>\r\n";
	return $ret;
}

//	write months dropdown
function WriteMonths($m)
{
	$monthnames=array();
	$monthnames[1]=##message MONTH_JAN##;
	$monthnames[2]=##message MONTH_FEB##;
	$monthnames[3]=##message MONTH_MAR##;
	$monthnames[4]=##message MONTH_APR##;
	$monthnames[5]=##message MONTH_MAY##;
	$monthnames[6]=##message MONTH_JUN##;
	$monthnames[7]=##message MONTH_JUL##;
	$monthnames[8]=##message MONTH_AUG##;
	$monthnames[9]=##message MONTH_SEP##;
	$monthnames[10]=##message MONTH_OCT##;
	$monthnames[11]=##message MONTH_NOV##;
	$monthnames[12]=##message MONTH_DEC##;
	$ret='<option value=""></option>';
	for($i=1;$i<=12;$i++)
		$ret.='<option value="'.$i.'" '.($i==$m?"selected":"").'>'.$monthnames[$i]."</option>\r\n";
	return $ret;
}

//	write years dropdown
function WriteYears($y)
{
	$currectYear=GetCurrentYear();
	$firstyear = $currectYear-100;
	$lastyear=$currectYear+10;
	$ret='<option value=""> </option>';
	if($y && $firstyear>$y-5)
		$firstyear=$y-10;
	if($y && $lastyear<$y+5)
		$lastyear=$y+10;
	for($i=$firstyear;$i<=$lastyear;$i++)
		$ret.='<option value="'.$i.'" '.($i==$y?"selected":"").'>'.$i."</option>\r\n";
	return $ret;
}

//	returns HTML code that represents required Date edit control
function GetDateEdit($field, $value, $type, $secondfield=false,$search=MODE_EDIT,$record_id="")
{	
	global $cYearRadius, $locale_info, $jscode;
	$cfieldname=GoodFieldName($field);
	$cfield="value_".GoodFieldName($field);
	$ctype="type_".GoodFieldName($field);
	if($secondfield)
	{
		$cfield="value1_".GoodFieldName($field);
		$ctype="type1_".GoodFieldName($field);
	}
	$iname=$cfield;
	$tvalue=$value;
	if($search==MODE_SEARCH && ($type==EDIT_DATE_SIMPLE || $type==EDIT_DATE_SIMPLE_DP))
		$tvalue=localdatetime2db($value);
	$time=db2time($tvalue);
	if(!count($time))
		$time=array(0,0,0,0,0,0);
	$dp=0;
//	$edit_type = postvalue("editType");
	$inline=false;
	if($search==MODE_INLINE_ADD || $search==MODE_INLINE_EDIT)
		$inline=true;
	
	switch($type)
	{
		case EDIT_DATE_SIMPLE_DP:
			$ovalue=$value;
			if($locale_info["LOCALE_IDATE"]==1)
			{
				$fmt="dd".$locale_info["LOCALE_SDATE"]."MM".$locale_info["LOCALE_SDATE"]."yyyy";
				$sundayfirst="false";
			}
			else if($locale_info["LOCALE_IDATE"]==0)
			{
				$fmt="MM".$locale_info["LOCALE_SDATE"]."dd".$locale_info["LOCALE_SDATE"]."yyyy";
				$sundayfirst="true";
			}
			else
			{
				$fmt="yyyy".$locale_info["LOCALE_SDATE"]."MM".$locale_info["LOCALE_SDATE"]."dd";
				$sundayfirst="false";
			}
			if(DateEditShowTime($field) )
			{
				if($time[5])
					$fmt.=" HH:mm:ss";
				else if($time[3] || $time[4])
					$fmt.=" HH:mm";
			}
			if($time[0])
				$ovalue=format_datetime_custom($time,$fmt);
			$ovalue1=$time[2]."-".$time[1]."-".$time[0];
			$showtime="false";
			if(DateEditShowTime($field))
			{
				$showtime="true";
				$ovalue1.=" ".$time[3].":".$time[4].":".$time[5];
			}
			if ( $inline ) {
				$onblur="var dt=parse_datetime(this.value,".$locale_info["LOCALE_IDATE"]."); if(dt!=null) $('input#ts".$iname."_".$record_id."').get(0).value=print_datetime(dt,-1,".$showtime."); else $('input#ts".$iname."_".$record_id."').get(0).value='';";
			} else {
				$onblur="var dt=parse_datetime(this.value,".$locale_info["LOCALE_IDATE"]."); if(dt!=null) $('input[@name=ts".$iname."]').get(0).value=print_datetime(dt,-1,".$showtime."); else $('input[@name=ts".$iname."]').get(0).value='';";
			}
			$ret='<input type="Text" name="'.$iname.'" size = "20" value="'.$ovalue.'" onblur="'.$onblur.'">'; 
			$ret.='<input type="Hidden" name="ts'.$iname.'" value="'.$ovalue1.'">&nbsp;&nbsp;';
			if ( $inline ) {
				$ret.='<a href="#" onclick="javascript:var v=show_calendar(\'update\', \''.$iname.'\',\''.$record_id.'\', $(\'input#ts'.$iname.'_'.$record_id.'\').get(0).value,'.$showtime.','.$sundayfirst.'); return false;">'.
					'<img src="images/cal.gif" width=16 height=16 border=0 alt="'.##message PICKUP_DATE##.'"></a>';
				$ret.="<script language=\"javascript\">".
					"function update".$iname."_".$record_id."(newDate) ".
					"{ ";
				$ret.="	$('input#".$iname."_".$record_id."').get(0).value =  print_datetime(newDate,".$locale_info["LOCALE_IDATE"].",".$showtime.");";
				$ret.="	$('input#ts".$iname."_".$record_id."').get(0).value =  print_datetime(newDate,-1,".$showtime.");";
				$ret.="	}";
			} else {
				$ret.='<a href="#" onclick="javascript:var v=show_calendar(\'update'.$iname.'\', \'\',\'\', $(\'input[@name=ts'.$iname.']\').get(0).value,'.$showtime.','.$sundayfirst.'); return false;">'.
					'<img src="images/cal.gif" width=16 height=16 border=0 alt="'.##message PICKUP_DATE##.'"></a>';
				$ret.="<script language=\"javascript\">";
				$ret.="function update".$iname."(newDate)\r\n".
					"{ "."\r\n";
				$ret.="	$('input[@name=".$iname."]').get(0).value =  print_datetime(newDate,".$locale_info["LOCALE_IDATE"].",".$showtime.");\r\n";
				$ret.="	$('input[@name=ts".$iname."]').get(0).value =  print_datetime(newDate,-1,".$showtime.");\r\n";
				$ret.="	attr();}";
			}
			$ret.="</script>";
			echo $ret;
			return;
		case EDIT_DATE_DD_DP:
			$dp=1;
		case EDIT_DATE_DD:
			$ovalue=$value;
			if($time)
				$ovalue=format_datetime_custom($time,"yyyy-MM-dd");
			if ( $inline ) {
				$retday='<select class=selects name="day'.$iname.'" onchange="SetDate(\''.$iname.'\',\''.$record_id.'\'); return true;">'.WriteDays($time[2])."</select>";
				$retmonth='<select class=selectm name="month'.$iname.'" onchange="SetDate(\''.$iname.'\',\''.$record_id.'\'); return true;">'.WriteMonths($time[1])."</select>";
				$retyear='<select class=selects name="year'.$iname.'" onchange="SetDate(\''.$iname.'\',\''.$record_id.'\'); return true;">'.WriteYears($time[0])."</select>";
			} else {
				$retday='<select class=selects name="day'.$iname.'" onchange="javascript: SetDate'.$iname.'(); return true;">'.WriteDays($time[2])."</select>";
				$retmonth='<select class=selectm name="month'.$iname.'" onchange="javascript: SetDate'.$iname.'(); return true;">'.WriteMonths($time[1])."</select>";
				$retyear='<select class=selects name="year'.$iname.'" onchange="javascript: SetDate'.$iname.'(); return true;">'.WriteYears($time[0])."</select>";
			}
			
			$sundayfirst="false";
			if($locale_info["LOCALE_ILONGDATE"]==1)
				$ret=$retday."&nbsp;".$retmonth."&nbsp;".$retyear;
			else if($locale_info["LOCALE_ILONGDATE"]==0)
			{
				$ret=$retmonth."&nbsp;".$retday."&nbsp;".$retyear;
				$sundayfirst="true";
			}
			else
				$ret=$retyear."&nbsp;".$retmonth."&nbsp;".$retday;
				
			if($time[0] && $time[1] && $time[2])
				$ret.="<input type=hidden name=\"".$iname."\" value=\"".$time[0]."-".$time[1]."-".$time[2]."\">";
			else
				$ret.="<input type=hidden name=\"".$iname."\" value=\"\">";
			if($dp)
			{
				if($inline)
				{
					$ret.="&nbsp;".
						"<a href=\"#\" onclick=\"javascript:var v=show_calendar('update','".$iname."','".$record_id."', $('input#ts".$iname."_".$record_id."').get(0).value,false,".$sundayfirst."); return false;\">".
						"<img src=images/cal.gif width=16 height=16 border=0 alt=\"".##message PICKUP_DATE##."\"></a>".
						"<input type=hidden name=\"ts".$iname."\" value=\"".$time[2]."-".$time[1]."-".$time[0]."\">";
				}
				else{
						$ret.="&nbsp;".
						"<a href=\"#\" onclick=\"javascript:var v=show_calendar('update".$iname."','','', $('input[@name=ts".$iname."]').get(0).value,false,".$sundayfirst."); return false;\">".
						"<img src=images/cal.gif width=16 height=16 border=0 alt=\"".##message PICKUP_DATE##."\"></a>".
						"<input type=hidden name=\"ts".$iname."\" value=\"".$time[2]."-".$time[1]."-".$time[0]."\">";
					}
			}
			if($inline)
			{
				$ret.="<script language=\"javascript\">".
					"function SetDate".$iname."_".$record_id."()"."\r\n".
					"{ "."\r\n".
					"  if ( $('select#month".$iname."_".$record_id."').get(0).value!='' && $('select#day".$iname."_".$record_id."').get(0).value!='' && $('select#year".$iname."_".$record_id."').get(0).value!='') {"."\r\n".
					"	$('input#".$iname."_".$record_id."').get(0).value= ''+$('select#year".$iname."_".$record_id."').get(0).value + "."\r\n".
					" 	'-' + $('select#month".$iname."_".$record_id."').get(0).value + '-' + $('select#day".$iname."_".$record_id."').get(0).value; "."\r\n";
				if($dp)
					$ret.="   $('input#ts".$iname."_".$record_id."').get(0).value='' + $('select#day".$iname."_".$record_id."').get(0).value+'-'+$('select#month".$iname."_".$record_id."').get(0).value+'-'+$('select#year".$iname."_".$record_id."').get(0).value;"."\r\n";
				$ret.="  } else {"."\r\n";
				if($dp)
					$ret.="	$('input#ts".$iname."_".$record_id."').get(0).value= '".$time[2]."-".$time[1]."-".$time[0]."';"."\r\n";
				$ret.="	$('input#".$iname."_".$record_id."').get(0).value= '';"."\r\n".
					"   } "."\r\n".
					" } "."\r\n".
					"\r\n";
			} else {
				$ret.="<script language=\"javascript\">".
					"function SetDate".$iname."()"."\r\n".
					"{ "."\r\n".
					"  if ( $('select[@name=month".$iname."]').get(0).value!='' && $('select[@name=day".$iname."]').get(0).value!='' && $('select[@name=year".$iname."]').get(0).value!='') {"."\r\n".
					"	$('input[@name=".$iname."]').get(0).value= ''+$('select[@name=year".$iname."]').get(0).value + "."\r\n".
					" 	'-' + $('select[@name=month".$iname."]').get(0).value + '-' + $('select[@name=day".$iname."]').get(0).value; "."\r\n";
				if($dp)
					$ret.="   $('input[@name=ts".$iname."]').get(0).value='' + $('select[@name=day".$iname."]').get(0).value+'-'+$('select[@name=month".$iname."]').get(0).value+'-'+$('select[@name=year".$iname."]').get(0).value;"."\r\n";
				$ret.="  } else {"."\r\n";
				if($dp)
					$ret.="	$('input[@name=ts".$iname."]').get(0).value= '".$time[2]."-".$time[1]."-".$time[0]."';"."\r\n";
				$ret.="	$('input[@name=".$iname."]').get(0).value= '';"."\r\n".
					"   } "."\r\n".
					" } "."\r\n".
					" SetDate".$iname."(); "."\r\n".
					"\r\n";
			}
				
			if($dp) {
				if ( $inline ) {
					$ret.="	function update".$iname."_".$record_id."(newDate) "."\r\n".
					"{ "."\r\n".
					"	var dt_datetime; "."\r\n".
					" 	var curdate = new Date(); "."\r\n".
					"		dt_datetime = newDate;"."\r\n".
					"		$('input#".$iname."_".$record_id."').get(0).value =  dt_datetime.getFullYear() + '-' + (dt_datetime.getMonth()+1) + '-' + dt_datetime.getDate();"."\r\n".
					"		$('select#day".$iname."_".$record_id."').get(0).selectedIndex = dt_datetime.getDate();"."\r\n".
					"		$('select#month".$iname."_".$record_id."').get(0).selectedIndex = dt_datetime.getMonth()+1;"."\r\n".
					"		for(i=0; i<$('select#year".$iname."_".$record_id."').get(0).options.length;i++)".
					"			if($('select#year".$iname."_".$record_id."').get(0).options[i].value==dt_datetime.getFullYear())".
					"			{".
					"				$('select#year".$iname."_".$record_id."').get(0).selectedIndex=i;".
					"				break;".
					"			}".
					"  	$('input#ts".$iname."_".$record_id."').get(0).value = dt_datetime.getDate() + '-' + (dt_datetime.getMonth()+1) + '-' + dt_datetime.getFullYear();"."\r\n".
					"	}"."\r\n";
				} else {
					$ret.="	function update".$iname."(newDate) "."\r\n".
					"{ "."\r\n".
					"	var dt_datetime; "."\r\n".
					" 	var curdate = new Date(); "."\r\n".
					"		dt_datetime = newDate;"."\r\n".
					"		$('input[@name=".$iname."]').get(0).value =  dt_datetime.getFullYear() + '-' + (dt_datetime.getMonth()+1) + '-' + dt_datetime.getDate();"."\r\n".
					"		$('select[@name=day".$iname."]').get(0).selectedIndex = dt_datetime.getDate();"."\r\n".
					"		$('select[@name=month".$iname."]').get(0).selectedIndex = dt_datetime.getMonth()+1;"."\r\n".
					"		for(i=0; i<$('select[@name=year".$iname."]').get(0).options.length;i++)".
					"			if($('select[@name=year".$iname."]').get(0).options[i].value==dt_datetime.getFullYear())".
					"			{".
					"				$('select[@name=year".$iname."]').get(0).selectedIndex=i;".
					"				break;".
					"			}".
					"  	$('input[@name=ts".$iname."]').get(0).value = dt_datetime.getDate() + '-' + (dt_datetime.getMonth()+1) + '-' + dt_datetime.getFullYear();"."\r\n".
					"	attr();}"."\r\n";
				}
			}
			$ret.="</script>";
			echo $ret;
			return;
		case EDIT_DATE_SIMPLE:
		default:
			$ovalue=$value;
			if($time[0])
			{
				if($time[3] || $time[4] || $time[5])
					$ovalue=str_format_datetime($time);
				else
					$ovalue=format_shortdate($time);
			}
			echo '<input type=text name="'.$iname.'" size = "20" value="'.htmlspecialchars($ovalue).'">';
	}
}

//	create javascript array with values for dependent dropdowns
function BuildSecondDropdownArray( $arrName, $strSQL)
{
	global $conn;

	echo $arrName . "=new Array();\r\n";
	$i=0;
	$rs = db_query($strSQL,$conn);
	while($row=db_fetch_numarray($rs))
	{
		echo $arrName."[".($i*3)."]='".jsreplace($row[0]). "';\r\n";
		echo $arrName."[".($i*3 + 1)."]='".jsreplace($row[1]). "';\r\n";
		echo $arrName."[".($i*3 + 2)."]='".jsreplace($row[2]). "';\r\n";
		$i++;
	}
}

//	create Lookup wizard control
function BuildSelectControl($field, $value, $values="", $secondfield=false, $mode, $id="")
{
	global $conn,$LookupSQL,$strTableName;
	$LookupSQL ="";
	$strSize = 1;
	$cfieldname=GoodFieldName($field);
	$cfield="value_".GoodFieldName($field);
	$clookupfield="display_value_".GoodFieldName($field);
	$ctype="type_".GoodFieldName($field);
	if($secondfield)
	{
		$cfield="value1_".GoodFieldName($field);
		$ctype="type1_".GoodFieldName($field);
	}
	if($values)
		$arr=&$values;
	$addnewitem=false;
	$advancedadd=false;
	$add_page="";
	$strCategoryControl=CategoryControl($field);
	$lookuptype=LookupControlType($field);
	
	$checkBoxMode = false;
	##foreach @BUILDER.Tables as @t filter @t.arrFieldObj[strEditFormat==EDIT_FORMAT_LOOKUP_WIZARD].len##
	##foreach @t.arrFieldObj as @f filter @f.strEditFormat==EDIT_FORMAT_LOOKUP_WIZARD##
	##if @f.nControlType==LCT_CBLIST##
	if($strTableName=="##@t.strDataSourceTable s##" && $field=="##@f.strName s##") 
		$checkBoxMode = true;
	##endif##
	##endfor##
	##endfor##
	
	if($checkBoxMode)
		echo '<div align=\'left\'>';

	$script="";
//	build SQL strings for lookup wizards
	##foreach @BUILDER.Tables as @t filter @t.arrFieldObj[strEditFormat==EDIT_FORMAT_LOOKUP_WIZARD].len##
	##foreach @t.arrFieldObj as @f filter @f.strEditFormat==EDIT_FORMAT_LOOKUP_WIZARD##
		if($strTableName=="##@t.strDataSourceTable s##" && $field=="##@f.strName s##") 
		{
			$strPerm = GetUserPermissions("##@f.pLookupObj.strTable s##");
	##if @f.nControlType==LCT_LIST##
		$list_page="##@BUILDER.Tables[strDataSourceTable==@f.pLookupObj.strTable].strShortTableName##_list.##@ext##";
		if(strpos($strPerm,"S")===false)
			$lookuptype=LCT_DROPDOWN;
	##endif##
	##if @f.bAllowToAdd##
		##if GroupSecurity##	
			if(strpos($strPerm,"A")!==false)
			{
		##endif##
				$addnewitem=true;
			##if @f.bAdvancedAdd && @BUILDER.Tables[strDataSourceTable==@f.pLookupObj.strTable].len##
				$advancedadd = true;
				$add_page = "##@BUILDER.Tables[strDataSourceTable==@f.pLookupObj.strTable].strShortTableName##_add.##@ext##";
			##endif##
		##if GroupSecurity##
			}
		##endif##
	##endif##
			$LinkField="##@f.strLinkField s##";
			$DisplayField="##@f.strDisplayField s##";
			$LookupTable="##@f.pLookupObj.strTable s##";
			
			##if @f.nControlType!=LCT_CBLIST || @f.nSelectSize > 1##
			$strSize=##@f.nSelectSize##;
			##else##
			$strSize = 2; // simply > 1 for CBLIST
			##endif##

		##if @f.nLookupType==LT_LOOKUPTABLE##
			
			$LookupSQL = "select ";
			##if @f.bUnique##
			$LookupSQL .= "distinct ";
			##endif##
			$LookupSQL .= "##@f.pLookupObj.strTable ts##.##@f.strLinkField ws##";
			##if @f.bCustomDisplay##
			$LookupSQL .= ",##@f.strDisplayField s##";
			##else##
			
			$LookupSQL .= ",##@f.pLookupObj.strTable ts##.##@f.strDisplayField ws##";
			##endif##
			##if @f.bUseCategory##
			$LookupSQL .= ",##@f.strCategoryFilterField ws##";
			##endif##
			$LookupSQL .= " from ##@f.pLookupObj.strTable ts## ";
			
			##if @f.strWhere##
			$LookupSQL.="where ".##@f.strWhere##;
			##endif##
						
		##if @BUILDER.nDatabaseType!=nDATABASE_Access || !@f.bUnique## 
			
			##if @f.strOrderBy##
			$LookupSQL.=" ORDER BY ##@f.pLookupObj.strTable ts##.##@f.strOrderBy ws##";
			##if @f.pLookupObj.bDesc##
			$LookupSQL.=" DESC";
			##endif##
			##endif##
		##endif##
		##else##
			$arr=array();
			##foreach @f.arrListOfValues as @val##
			$arr[]="##@val s##";
			##endfor##
		##endif##
		}
	##endfor##
	##endfor##
	if($lookuptype==LCT_LIST)
		$addnewitem=false;
	
//	prepare multi-select attributes
	$multiple="";
	$postfix="";
	if($strSize>1)
	{
		$avalue=splitvalues($value);
		$multiple=" multiple";
		$postfix="[]";
	}
	else 
		$avalue=array((string)$value);
	
	$cfieldid=$cfield;
	if($mode==MODE_INLINE_EDIT || $mode==MODE_INLINE_ADD)
		$cfieldid.="_".$id;
	if($LookupSQL)
	{
//	ajax-lookup control
//		if ( FastType($field) )
		if ( $lookuptype==LCT_AJAX || $lookuptype==LCT_LIST )
		{
			if(UseCategory($field))
			{
//	dependent dropdown
				$clookupfieldid=$clookupfield;
				$categoryFieldId = GoodFieldName(CategoryControl($field));
				if($mode==MODE_INLINE_EDIT || $mode==MODE_INLINE_ADD)
				{
					$clookupfieldid.="_".$id;
					$categoryFieldId.="_".$id;
				}
				if(	$lookuptype==LCT_AJAX)
					echo '<input type="text" categoryId="'.$categoryFieldId.'" autocomplete="off" id="'.$clookupfieldid.'" name="'.$clookupfield.'" onkeydown="return listenEvent(event,this,\'lookup\');" onkeyup="lookupSuggest(\''.GetTableURL().'\',event,this,\''.htmlspecialchars(jsreplace($value)).'\',\''.$id.'\');" onblur="isSetFocus=false;showHideLookupError(this);" onfocus="isSetFocus=true;" >';
				elseif(	$lookuptype==LCT_LIST)
					echo '<input type="text" categoryId="'.$categoryFieldId.'" autocomplete="off" id="'.$clookupfieldid.'" name="'.$clookupfield.'"  readonly >';
				$onchange="";
##foreach @BUILDER.Tables as @t##
##foreach @t.arrFieldObj as @f filter @f.strEditFormat==EDIT_FORMAT_LOOKUP_WIZARD && @f.bUseCategory##
				if($strTableName=="##@t.strDataSourceTable s##" && $field == "##@f.strCategoryControlField s##") 
					$onchange .= "loadSelectContent('##@t.strShortTableName##','##@f.strCategoryControlField g##', '##@f.strName g##', '".$id."');";
##endfor##
##endfor##
		   		if($onchange)
					$onchange="onchange=\"".$onchange."\"";
				echo '<input type="hidden" id="'.$cfieldid.'" name="'.$cfield.'" '.$onchange.'>';
			//	add new item
				if($mode!=MODE_SEARCH && $addnewitem || $lookuptype==LCT_LIST)
				{
					if($mode!=MODE_INLINE_EDIT && $mode!=MODE_INLINE_ADD)
					{
						$celement="document.forms.editform.value_".GoodFieldName($strCategoryControl);
						$extra="";
					}
					else
					{
						$celement="document.getElementById('value_".GoodFieldName($strCategoryControl)."_".$id."')";
						$extra="&mode=".$mode."&id=value_".GoodFieldName($field)."_".$id;
					}
					$celementvalue = '$('.$celement.").val()";
					if($addnewitem)
					{
						if(!$advancedadd)
						{
							echo "<a href=# onclick=\"window.open('##@t.strShortTableName##_addnewitem.##@ext##?".
							"field=".htmlspecialchars(jsreplace(rawurlencode($field))).$extra.
							"&category='+escape(".$celementvalue.")".
							",\r\n".
							"'AddNewItem', 'width=250,height=100,status=no,resizable=yes,top=200,left=200');\">\r\n".
							##message ADD_NEW##."</a>";
						}
						else
						{
							echo "&nbsp;<a href=# onclick=\"return DisplayPage(event,'".$add_page."','".$cfieldid."','".GoodFieldName($field)."','".GetTableURL()."',".$celementvalue.");\">".##message ADD_NEW##."</a>";
						}
					}
					if($lookuptype==LCT_LIST)
					{
							echo "&nbsp;<a href=# onclick=\"return DisplayPage(event,'".$list_page."','".$cfieldid."','".GoodFieldName($field)."','".GetTableURL()."',".$celementvalue.");\">"."Select"."</a>";
					}
				}
				
				if($checkBoxMode)
					echo '</div>';
				return;
//	fasttype - dependent - end
			}
//	fasttype - regular - start
//	get the initial value
			$lookup_SQL = "";
			$lookup_value = "";
			
##foreach @BUILDER.Tables as @t filter @t.arrFieldObj[strEditFormat==EDIT_FORMAT_LOOKUP_WIZARD].len##
##foreach @t.arrFieldObj as @f filter @f.strEditFormat==EDIT_FORMAT_LOOKUP_WIZARD##
			if($strTableName=="##@t.strDataSourceTable s##" && $field=="##@f.strName s##") 
			{
				##if @f.nLookupType==LT_LOOKUPTABLE##
				$lookup_SQL = "SELECT ";
				##if @f.bUnique##
				$lookup_SQL .= "DISTINCT ";
				##endif##
				$lookup_SQL .= "##@f.pLookupObj.strTable ts##.##@f.strLinkField ws##";
				##if @f.bCustomDisplay##
				$lookup_SQL .= ",##@f.strDisplayField s##";
				##else##
				$lookup_SQL .= ",##@f.pLookupObj.strTable ts##.##@f.strDisplayField ws##";
				##endif##
				$lookup_SQL .= " FROM ##@f.pLookupObj.strTable ts## ";
				##if @f.strWhere##
				$lookup_SQL.="where (".##@f.strWhere##.")  AND ##@f.strLinkField ws##=".make_db_value($field,$value)."";
				##else##
				$lookup_SQL .= " WHERE ##@f.pLookupObj.strTable ts##.##@f.strLinkField ws##=".make_db_value($field,$value)."";
				##endif##
			##if @BUILDER.nDatabaseType!=nDATABASE_Access || !@f.bUnique##
				##if @f.strOrderBy##
					$lookup_SQL.= " ORDER BY ##@f.pLookupObj.strTable ts##.##@f.strOrderBy ws##";
					##if @f.pLookupObj.bDesc##
					$lookup_SQL.=" DESC";	
					##endif##
				##endif##
			##endif##	
				##endif##
			}
##endfor##
##endfor##
			
			$rs_lookup=db_query($lookup_SQL,$conn);
	
			if ( $data = db_fetch_numarray($rs_lookup) ) 
			{	
				$lookup_value = $data[1];
			} 
			else
			{
##foreach @BUILDER.Tables as @t filter @t.arrFieldObj[strEditFormat==EDIT_FORMAT_LOOKUP_WIZARD].len##
##foreach @t.arrFieldObj as @f filter @f.strEditFormat==EDIT_FORMAT_LOOKUP_WIZARD##
				if($strTableName=="##@t.strDataSourceTable s##" && $field=="##@f.strName s##") 
				{
					##if @f.nLookupType==LT_LOOKUPTABLE##
					$lookup_SQL = "SELECT ";
					##if @f.bUnique##
					$lookup_SQL .= "DISTINCT ";
					##endif##
					$lookup_SQL .= "##@f.pLookupObj.strTable ts##.##@f.strLinkField ws##";
					##if @f.bCustomDisplay##
					$lookup_SQL .= ",##@f.strDisplayField s##";
					##else##
					$lookup_SQL .= ",##@f.pLookupObj.strTable ts##.##@f.strDisplayField ws##";
					##endif##
					$lookup_SQL .= " FROM ##@f.pLookupObj.strTable ts## ";
					$lookup_SQL .= " WHERE ##@f.pLookupObj.strTable ts##.##@f.strLinkField ws##=".make_db_value($field,$value)."";
				// in access order by conflicts with unique
					##if @f.strOrderBy##
						$lookup_SQL.= " ORDER BY ##@f.pLookupObj.strTable ts##.##@f.strOrderBy ws##";
						##if @f.pLookupObj.bDesc##
						$lookup_SQL.=" DESC";	
						##endif##
					##endif##
					##endif##
				}
##endfor##
##endfor##
			
				$rs_lookup=db_query($lookup_SQL,$conn);			
				
				if($data = db_fetch_numarray($rs_lookup))
					$lookup_value = $data[1];
			}
//	build the control
			$clookupfieldid=$clookupfield;
			if($mode==MODE_INLINE_EDIT || $mode==MODE_INLINE_ADD)
				$clookupfieldid.="_".$id;
			
			
			if($lookuptype==LCT_AJAX)
				echo '<input type="text" autocomplete="off" id="'.$clookupfieldid.'" name="'.$clookupfield.'" value="'.htmlspecialchars($lookup_value).'" 	onkeydown="return listenEvent( event,this,\'lookup\');" onkeyup="lookupSuggest(\''.GetTableURL().'\',event,this,\''.htmlspecialchars(jsreplace($value)).'\',\''.$id.'\');" onblur="isSetFocus=false;showHideLookupError(this);" onfocus="isSetFocus=true;" >';
			elseif($lookuptype==LCT_LIST)
				echo '<input type="text" autocomplete="off" id="'.$clookupfieldid.'" name="'.$clookupfield.'" value="'.htmlspecialchars($lookup_value).'" 	readonly >';
			$onchange="";
##foreach @BUILDER.Tables as @t##
##foreach @t.arrFieldObj as @f filter @f.strEditFormat==EDIT_FORMAT_LOOKUP_WIZARD && @f.bUseCategory##
			if($strTableName=="##@t.strDataSourceTable s##" && $field == "##@f.strCategoryControlField s##") 
				$onchange .= "loadSelectContent('##@t.strShortTableName##','##@f.strCategoryControlField g##', '##@f.strName g##', '".$id."');";
##endfor##
##endfor##
	   		if($onchange)
				$onchange="onchange=\"".$onchange."\"";
			
			echo '<input type="hidden" id="'.$cfieldid.'" name="'.$cfield.'" value="'.htmlspecialchars($value).'" '.$onchange.'>';
			//	add new item
			if($addnewitem &&  $mode!=MODE_SEARCH)
			{
				if(!$advancedadd)
				{
					$extra="";
					if( $mode==MODE_INLINE_EDIT || $mode==MODE_INLINE_ADD )
						$extra="&mode=".$mode."&id=value_".GoodFieldName($field)."_".$id;
					echo "<a href=# onclick=\"window.open('".GetTableURL($strTableName)."_addnewitem.##@ext##?field=".htmlspecialchars(jsreplace(rawurlencode($field))).$extra."',\r\n".
					"'AddNewItem', 'width=250,height=100,status=no,resizable=yes,top=200,left=200');\">\r\n".
					##message ADD_NEW##."</a>";
				}
				else
				{
					if( $mode==MODE_INLINE_EDIT || $mode==MODE_INLINE_ADD )
						echo "&nbsp;<a href=# onclick=\"return DisplayPage(event,'".$add_page."','value_".GoodFieldName($field)."_".$id."','".GoodFieldName($field)."','".GetTableURL()."','');\">".##message ADD_NEW##."</a>";
					else
						echo "&nbsp;<a href=# onclick=\"return DisplayPage(event,'".$add_page."','value_".GoodFieldName($field)."','".GoodFieldName($field)."','".GetTableURL()."','');\">".##message ADD_NEW##."</a>";
				}
			}
			if($lookuptype==LCT_LIST)
			{
				if( $mode==MODE_INLINE_EDIT || $mode==MODE_INLINE_ADD )
					echo "&nbsp;<a href=# onclick=\"return DisplayPage(event,'".$list_page."','value_".GoodFieldName($field)."_".$id."','".GoodFieldName($field)."','".GetTableURL()."','');\">"."Select"."</a>";
				else
					echo "&nbsp;<a href=# onclick=\"return DisplayPage(event,'".$list_page."','value_".GoodFieldName($field)."','".GoodFieldName($field)."','".GetTableURL()."','');\">"."Select"."</a>";
			}
//	fasttype - regular - end
//	fasttype - end
		}
		else
		{
//	classic dropdown - start
			LogInfo($LookupSQL);
			$rs=db_query($LookupSQL,$conn);
			$onchange="";
##foreach @BUILDER.Tables as @t##
##foreach @t.arrFieldObj as @f filter @f.strEditFormat==EDIT_FORMAT_LOOKUP_WIZARD && @f.bUseCategory##
			if($strTableName=="##@t.strDataSourceTable s##" && $field == "##@f.strCategoryControlField s##") 
					$onchange .= "loadSelectContent('##@t.strShortTableName##','##@f.strCategoryControlField g##', '##@f.strName g##', '".$id."');";
##endfor##
##endfor##
	   		if($onchange)
				$onchange="onchange=\"".$onchange."\"";
##foreach @BUILDER.Tables as @t##
##foreach @t.arrFieldObj as @f filter @f.strEditFormat==EDIT_FORMAT_LOOKUP_WIZARD && @f.bUseCategory##
			if($strTableName=="##@t.strDataSourceTable s##" && $field == "##@f.strName s##") 
			{
				//	print Type control to allow selecting nothing
				if($multiple!="")
					echo "<input type=hidden name=\"".$ctype."\" value=\"multiselect\">";
				##if @f.nControlType!=LCT_CBLIST##
				echo '<select size = "'.$strSize.'" id="'.$cfield.'" name="'.$cfield.$postfix.'"'.$multiple.' '.$onchange.'>';
				echo '<option value="">'.##message PLEASE_SELECT##.'</option>';
				echo "</select>";
				##else##
				echo ##message PLEASE_SELECT##.'<br/>';
				##endif##
//	add new item
				if($addnewitem &&  $mode!=MODE_SEARCH)
				{
					if($mode!=MODE_INLINE_EDIT && $mode!=MODE_INLINE_ADD)
					{
						$celement="document.forms.editform.value_##@f.strCategoryControlField g##";
						$extra="";
					}
					else
					{
						$celement="document.getElementById('value_##@f.strCategoryControlField g##_".$id."')";
						$extra="&mode=".$mode."&id=value_##@f.strName g##_".$id;
					}
					$celementvalue = '$('.$celement.").val()";
					if(!$advancedadd)
					{
						echo "<a href=# onclick=\"window.open('##@t.strShortTableName##_addnewitem.##@ext##?".
						"field=".htmlspecialchars(jsreplace(rawurlencode($field))).$extra.
						"&category='+escape(".$celementvalue.")".
						",\r\n".
						"'AddNewItem', 'width=250,height=100,status=no,resizable=yes,top=200,left=200');\">\r\n".
						##message ADD_NEW##."</a>";
					}
					else
					{
						echo "&nbsp;<a href=# onclick=\"return DisplayPage(event,'".$add_page."','".htmlspecialchars(jsreplace($cfieldid))."','".GoodFieldName($field)."','".GetTableURL()."',".$celementvalue.");\">".##message ADD_NEW##."</a>";
					}
				}
				if($checkBoxMode)
					echo '</div>';
				return;
			}
##endfor##
##endfor##
				//	print Type control to allow selecting nothing
			if($multiple!="")
				echo "<input type=hidden name=\"".$ctype."\" value=\"multiselect\">";
				
			##foreach @BUILDER.Tables as @t filter @t.arrFieldObj[strEditFormat==EDIT_FORMAT_LOOKUP_WIZARD].len##
			##foreach @t.arrFieldObj as @f filter @f.strEditFormat==EDIT_FORMAT_LOOKUP_WIZARD##
			if($strTableName=="##@t.strDataSourceTable s##" && $field=="##@f.strName s##") 
			{
				##if @f.nControlType!=LCT_CBLIST##
	   	  		echo '<select size = "'.$strSize.'" id="'.$cfield.'" name="'.$cfield.$postfix.'"'.$multiple.' '.$onchange.'>';
				if($strSize<2)
					echo '<option value="">'.##message PLEASE_SELECT##.'</option>';
				else if($mode==MODE_SEARCH)
					echo '<option value=""> </option>';
				##else##
				if($strSize<2)
					echo ##message PLEASE_SELECT##.'<br/>';
				##endif##
			}
			##endfor##
			##endfor##

		      	$found=false;
			while($data=db_fetch_numarray($rs))
			{
				$res=array_search((string)$data[0],$avalue);
				if(!($res===NULL || $res===FALSE))
				{
					$found=true;
					if($checkBoxMode)
					{
						echo '<input type="checkbox" name="'.$cfield.$postfix.'" value="'.htmlspecialchars($data[0]).'" checked="checked"/>';
						echo '&nbsp;'.htmlspecialchars($data[1]).'<br/>';
					}
					else
						echo '<option value="'.htmlspecialchars($data[0]).'" selected>'.htmlspecialchars($data[1]).'</option>';
				}
				else
				{
					if($checkBoxMode)
					{
						echo '<input type="checkbox" name="'.$cfield.$postfix.'" value="'.htmlspecialchars($data[0]).'"/>';
					echo '&nbsp;'.htmlspecialchars($data[1]).'<br/>';
					}
					else
						echo '<option value="'.htmlspecialchars($data[0]).'">'.htmlspecialchars($data[1]).'</option>';
				}
			}
	##foreach @BUILDER.Tables as @t filter @t.arrFieldObj[strWhere && strEditFormat==EDIT_FORMAT_LOOKUP_WIZARD].len##
	##foreach @t.arrFieldObj as @f filter @f.strEditFormat==EDIT_FORMAT_LOOKUP_WIZARD && @f.strWhere##
			if(	$strTableName=="##@t.strDataSourceTable s##" && $field=="##@f.strName s##"
				&&$mode==MODE_EDIT && $mode==MODE_INLINE_EDIT && !$found && strlen($value)) 
			{
				$LookupSQL = "select ";
		##if @BUILDER.nDatabaseType==nDATABASE_MSSQLServer##
				$LookupSQL .= "top 1 ";
		##endif##
				$LookupSQL .= "##@f.pLookupObj.strTable ts##.##@f.strLinkField ws##";
				##if @f.bCustomDisplay##
				$LookupSQL .= ",##@f.strDisplayField s##";
				##else##
				$LookupSQL .= ",##@f.pLookupObj.strTable ts##.##@f.strDisplayField ws##";
				##endif##
				$LookupSQL .= " from ##@f.pLookupObj.strTable ts## ";
				
				$LookupSQL.=" where ##@f.pLookupObj.strTable ts##.##@f.strLinkField ws##=".make_db_value($field,$value);
	
		##if @BUILDER.nDatabaseType==nDATABASE_Oracle##
				$LookupSQL.=" and rownum<2";
		##endif##
		// in access order by conflicts with unique 
				##if @f.strOrderBy##
				$LookupSQL.=" ORDER BY ##@f.pLookupObj.strTable ts##.##@f.strOrderBy ws##";
				##endif##
				##if @f.pLookupObj.bDesc##
				$LookupSQL.=" DESC";
				##endif##
		##if @BUILDER.nDatabaseType==nDATABASE_MySQL##
				$LookupSQL.=" limit 0,1";
		##endif##
		##if @BUILDER.nDatabaseType==nDATABASE_PostgreSQL##
				$LookupSQL.=" limit 1";
		##endif##
	
				LogInfo($LookupSQL);
				$rs=db_query($LookupSQL,$conn);
				if($data=db_fetch_numarray($rs))
				{
				##if @f.nControlType!=LCT_CBLIST##
	      			echo '<option value="'.htmlspecialchars($data[0]).'" selected>'.htmlspecialchars($data[1]).'</option>';
				##else##
					echo '<input type="checkbox" name="'.$cfield.$postfix.'" value="'.htmlspecialchars($data[0]).'" checked="checked"/>';
					echo '&nbsp;'.htmlspecialchars($data[0]).'<br/>';
				##endif##
				}
			}
	##endfor##
	##endfor##
			if(!$checkBoxMode)
				echo "</select>";
				
//	add new item
			if($addnewitem &&  $mode!=MODE_SEARCH && $mode!=MODE_INLINE_EDIT && $mode!=MODE_INLINE_ADD)
			{
				if(!$advancedadd)
				{
					echo "<a href=# onclick=\"window.open('".GetTableURL($strTableName)."_addnewitem.##@ext##?field=".htmlspecialchars(jsreplace(rawurlencode($field)))."',\r\n".
					"'AddNewItem', 'width=250,height=100,status=no,resizable=yes,top=200,left=200');\">\r\n".
					##message ADD_NEW##."</a>";
				}
				else
				{
					echo "&nbsp;<a href=# onclick=\"return DisplayPage(event,'".$add_page."','".htmlspecialchars(jsreplace($cfield))."','".GoodFieldName($field)."','".GetTableURL()."','');\">".##message ADD_NEW##."</a>";
				}
			}
			if($addnewitem &&  $mode!=MODE_SEARCH &&  ($mode==MODE_INLINE_EDIT || $mode==MODE_INLINE_ADD))
			{
				if(!$advancedadd)
				{
					echo "<a href=# onclick=\"window.open('".GetTableURL($strTableName)."_addnewitem.##@ext##?field=".htmlspecialchars(jsreplace(rawurlencode($field)))."&mode=".$mode."&id=value_".GoodFieldName($field)."_".$id."',\r\n".
					"'AddNewItem', 'width=250,height=100,status=no,resizable=yes,top=200,left=200');\">\r\n".
					##message ADD_NEW##."</a>";
				}
				else
				{
					echo "&nbsp;<a href=# onclick=\"return DisplayPage(event,'".$add_page."','value_".GoodFieldName($field)."_".$id."','".GoodFieldName($field)."','".GetTableURL()."','');\">".##message ADD_NEW##."</a>";
				}
			}
		}
	}
	else
	{
		//	print Type control to allow selecting nothing
		if($multiple!="")
			echo "<input type=hidden name=\"".$ctype."\" value=\"multiselect\">";
		##foreach @BUILDER.Tables as @t filter @t.arrFieldObj[strEditFormat==EDIT_FORMAT_LOOKUP_WIZARD].len##
		##foreach @t.arrFieldObj as @f filter @f.strEditFormat==EDIT_FORMAT_LOOKUP_WIZARD##
		if($strTableName=="##@t.strDataSourceTable s##" && $field=="##@f.strName s##") 
		{
			##if @f.nControlType!=LCT_CBLIST##
			echo '<select size = "'.$strSize.'" name="'.$cfield.$postfix.'" '.$multiple.'>';
			if($strSize<2 )
				echo '<option value="">'.##message PLEASE_SELECT##.'</option>';
			else if($mode==MODE_SEARCH)
				echo '<option value=""> </option>';
				
			foreach($arr as $opt)
			{
				$res=array_search((string)$opt,$avalue);
				if(!($res===NULL || $res===FALSE))
		      			echo '<option value="'.htmlspecialchars($opt).'" selected>'.htmlspecialchars($opt).'</option>';
				else
		      			echo '<option value="'.htmlspecialchars($opt).'">'.htmlspecialchars($opt).'</option>';
			}
			echo "</select>";
			##else##
			if($strSize<2 )
				echo ##message PLEASE_SELECT##.'<br/>';
			foreach($arr as $opt)
			{
				echo '<input type="checkbox" name="'.$cfield.$postfix.'" value="'.htmlspecialchars($opt).'"';
				$res=array_search((string)$opt,$avalue);
				if(!($res===NULL || $res===FALSE))
					echo ' checked="checked" ';
				echo '/>';
				echo '&nbsp;'.htmlspecialchars($opt).'<br/>';
			}
			##endif##
		}
		##endfor##
		##endfor##
	}
	if($checkBoxMode)
		echo '</div>';
	return;
}

function BuildRadioControl($field, $value,$secondfield=false,$id="")
{
	global $conn,$LookupSQL,$strTableName;
	$cfieldname=GoodFieldName($field);
	$cfield="value_".GoodFieldName($field);
	$cfieldid="value_".GoodFieldName($field);
	$ctype="type_".GoodFieldName($field);
	if($id<>"")
	{
		$cfieldname.="_".$id;
		$cfieldid.="_".$id;
	}
	if($secondfield)
	{
		$cfield="value1_".GoodFieldName($field);
		$ctype="type1_".GoodFieldName($field);
	}
	$LookupSQL ="";
	$spacer = '<br/>';
##foreach @BUILDER.Tables as @t##
##foreach @t.arrFieldObj as @f filter @f.strEditFormat==EDIT_FORMAT_RADIO##
	if($strTableName=="##@t.strDataSourceTable s##" && "##@f.strName s##"==$field)
	{
		##if @f.nLookupType==LT_LOOKUPTABLE##
			$LookupSQL = "select ";
			##if @f.bUnique##
			$LookupSQL .= "distinct ";
			##endif##
			$LookupSQL .= "##@f.strLinkField ws##";
			##if @f.bCustomDisplay##
			$LookupSQL .= ",##@f.strDisplayField s##";
			##else##
			$LookupSQL .= ",##@f.strDisplayField ws##";
			##endif##
			$LookupSQL .= " from ##@f.pLookupObj.strTable ts##";

			##if @f.strWhere##
			$LookupSQL.=" where ".##@f.strWhere##;
			##endif##
			
		// in access order by conflicts with unique
		##if @BUILDER.nDatabaseType!=nDATABASE_Access || !@f.bUnique## 	
			##if @f.strOrderBy##
			$LookupSQL.=" ORDER BY ##@f.pLookupObj.strTable ts##.##@f.strOrderBy ws##";
			##endif##
		##endif##
			
		##else##
			$arr=array();
			##foreach @f.arrListOfValues as @val##
			$arr[]="##@val s##";
			##endfor##
		##endif##

		##if @f.horizontalLayout##
		$spacer = '&nbsp;';
		##endif##
	}
##endfor##
##endfor##

	if($LookupSQL)
	{
	    LogInfo($LookupSQL);
		$rs=db_query($LookupSQL,$conn);
		echo '<input type=hidden name="'.$cfield.'" value="'.htmlspecialchars($value).'" id="'.$cfieldid.'">';
	    while($data=db_fetch_numarray($rs))
		{
			$checked="";
			if($data[0]==$value)
				$checked=" checked";
			echo "<input type=\"Radio\" name=\"radio_".$cfieldname."\" onclick=\"javascript: $('#".$cfieldid."')[0].value='".htmlspecialchars($data[0])."'; return true;\" ".$checked.">".htmlspecialchars($data[1]).$spacer;
		}
	}
	else
	{
		echo '<input type=hidden name="'.$cfield.'" value="'.htmlspecialchars($value).'" id="'.$cfieldid.'">';
		foreach($arr as $opt)
		{
			$checked="";
			if($opt==$value)
				$checked=" checked";
			echo "<input type=\"Radio\" name=\"radio_".$cfieldname."\" onclick=\"javascript: $('#".$cfieldid."')[0].value='".htmlspecialchars($opt)."'; return true;\" ".$checked.">".htmlspecialchars($opt).$spacer;
//			echo '<input type="Radio" name="radio_'.$cfieldname.'" onclick="javascript: $("#'.$cfield."\")[0].value='".db_addslashes($opt).'\'; return true;" '.$checked.'>'.htmlspecialchars($opt)."<br>";
		}
	}
	return;

}

function BuildEditControl($field , $value, $format, $edit, $secondfield=false, $id="")
{
	global $rs,$data,$strTableName,$filenamelist,$keys,$locale_info,$jscode;
	$cfieldname=GoodFieldName($field);
	$cfield="value_".GoodFieldName($field);
	$ctype="type_".GoodFieldName($field);
	if($secondfield)
	{
		$cfield="value1_".GoodFieldName($field);
		$ctype="type1_".GoodFieldName($field);
	}
	$cfieldid=$cfield;
	if($edit==MODE_INLINE_EDIT || $edit==MODE_INLINE_ADD)
		$cfieldid.="_".$id;
	$type=GetFieldType($field);
	$arr="";

	$iquery="field=".rawurlencode($field);
	$keylink="";
	##foreach @BUILDER.Tables as @t##
	if($strTableName=="##@t.strDataSourceTable s##")
	{
	##foreach @t.arrKeyFields as @k##
		$keylink.="&key##@index##=".rawurlencode(@$keys["##@k s##"]);
	##endfor##
		$iquery.=$keylink;
	}
	##endfor##
	if($format==EDIT_FORMAT_FILE && $edit==MODE_SEARCH)
		$format="";
	if($format==EDIT_FORMAT_TEXT_FIELD)
	{
		if(IsDateFieldType($type))
			echo '<input type="hidden" name="'.$ctype.'" value="date'.EDIT_DATE_SIMPLE.'">'.GetDateEdit($field,$value,0,$secondfield,$edit,$id);
		else
	    {
			if($edit==MODE_SEARCH)
				echo '<input type="text" autocomplete="off" name="'.$cfield.'" '.GetEditParams($field).' value="'.htmlspecialchars($value).'">';
			else
				echo '<input type="text" name="'.$cfield.'" '.GetEditParams($field).' value="'.htmlspecialchars($value).'">';
		}
	}
	else if($format==EDIT_FORMAT_TIME)
	{
		echo '<input type="hidden" name="'.$ctype.'" value="time">';
##foreach @BUILDER.Tables as @t filter @t.arrFieldObj[strViewFormat==EDIT_FORMAT_TIME].len##
	##foreach @t.arrFieldObj as @f filter @f.strViewFormat==EDIT_FORMAT_TIME##
		if($strTableName=="##@t.strDataSourceTable s##" && $field=="##@f.strName s##")
		{
		##if @f.useTimePicker##
				$convention = ##@f.hours##;
				$locale_convention = $locale_info["LOCALE_ITIME"] ? 24 : 12;
				
				if($convention == $locale_convention)
				{
						$am = $locale_info["LOCALE_S1159"];
						$pm = $locale_info["LOCALE_S2359"];
						$locale = $locale_info["LOCALE_STIMEFORMAT"];
				}
				else
				{
						if($convention == 24)
						{
								$am = '';
								$pm = '';
								$locale = "H:mm:ss";
						}
						else
						{
								$am = 'am';
								$pm = 'pm';
								$locale = "h:mm:ss tt";
						}
				}
		##else##
				$locale = $locale_info["LOCALE_STIMEFORMAT"];
		##endif##
		
				$val = "";
				$dbtime = array();
				if(IsDateFieldType($type))
				{
					$dbtime = db2time($value);
					if(count($dbtime))
						$val = format_datetime_custom($dbtime, $locale);
				}
				else 
				{
					$arr = parsenumbers($value);
					if(count($arr))
					{
						while(count($arr)<3)
							$arr[] = 0;
						$dbtime = array(0, 0, 0, $arr[0], $arr[1], $arr[2]);
						$val = format_datetime_custom($dbtime, $locale);
					}
				}
		
				
		##if @f.useTimePicker##
				$ret = '$(function(){';
				$ret.= '$(\'#'.$cfieldid.'\').timepickr({';
				$ret.= 'handle: \'#trigger-test-'.$cfieldid.'\',';
				$ret.= 'convention: ' . $convention . ',';
				$ret.= 'seconds: ##@f.showSeconds##,';
				$range = array();
				if($convention==24)
				{
					for($h = 0; $h < $convention; $h ++)
							$range []= '\''.$h.'\'';
				}
				else
				{
					for($h = 1; $h <= $convention; $h ++)
							$range []= '\''.$h.'\'';
				}
				$ret.= 'range'.$convention.'h: ['.join(',', $range).'],';
				$minutes = array();
				for($m = 0; $m < 60; $m += ##@f.minutes##)
						$minutes []= '\''.$m.'\'';
				$ret.= 'rangeMin: ['.join(',', $minutes).'],';
				$ret.= 'apm: [\''.$am.'\',\''.$pm.'\'],';
				if(count($dbtime) > 0)
					$ret.= 'hover: ['.$dbtime[3].','.$dbtime[4].','.$dbtime[5].'],';
				$ret.= 'open: '.($val ? 'true' : 'false').',';
				$ret.= 'select: function(e, dropslide){';
				$ret.= 'var t = dropslide.getSelection();';
				$ret.= 'var newval = print_time(t[0], t[1], ';
				##if @f.showSeconds##
				$ret.='t[2]';
				##else##
				$ret.='0';
				##endif##
				##if @f.showSeconds##
				$tvar1='t[3]';
				##else##
				$tvar1='t[2]';
				##endif##
				$ret.=', ' .
				'\''.$locale . '\', ' .
				($convention == '12' ? '"' . $am . '"==' . $tvar1 . '?true:false'	: 'false') .
				',' . $convention . ',' .
				'\'' . $am . '\',' .
				'\'' . $pm . '\'' .
				');';
				$ret.= '$(this).val(newval);';
				$ret.= 'e.stopPropagation();';
				$ret.= '},';
				$ret.= "dropslide: {hideoutDelay: 2000, trigger: 'click'}";
				$ret.= '});';
				$ret.= '});';
				AddScript2Postload($ret,$id);
						
				echo '<input type="text" name="'.$cfield.'" id="'.$cfield.'" '.GetEditParams($field).' value="'.htmlspecialchars($val).'">';
				echo '&nbsp;';
				echo '<img src="include/img/clock.gif" alt="Time" border="0" style="margin:4px 0 0 6px;" id="trigger-test-'.$cfieldid.'" />';
				
		##else##
				echo '<input type="text" name="'.$cfield.'" '.GetEditParams($field).' value="'.htmlspecialchars($val).'">';
		##endif##
		}
	##endfor##
##endfor##
	}
	else if($format==EDIT_FORMAT_TEXT_AREA)
	{
	
		$nWidth = GetNCols($field);
		$nHeight = GetNRows($field);
		if(UseRTE($field))
		{
			$value = RTESafe($value);
			##if @BUILDER.m_strRTEType==RTE_BASIC##
			if($id!='')
				$cfield.="_".$id;
			$browser="";
			if(@$_REQUEST["browser"]=="ie")
				$browser="&browser=ie";
			echo "<iframe frameborder=\"0\" vspace=\"0\" hspace=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\" id=\"".$cfield."\" name=\"".$cfield."\" style='width: " . ($nWidth+1) . "px;height: " . ($nHeight+1) . "px;border:1px solid black;'";
			echo " src=\"".GetTableURL($strTableName)."_rte.##@ext##?".($edit==MODE_INLINE_EDIT || $edit==MODE_INLINE_ADD ? "id=".$id ."&": '').$iquery.$browser."\">";
			echo "</iframe>";
			##endif##
			##if @BUILDER.m_strRTEType==RTE_FCK##
			if($edit==MODE_INLINE_EDIT || $edit==MODE_INLINE_ADD)
				$cfield.="_".$id;
			$oFCKeditor = CreateFCKeditor($cfield, $value, $nWidth, $nHeight);
			##endif##
			##if @BUILDER.m_strRTEType==RTE_INNOVA##
			if($id!='')
				$cfield.="_".$id;
			$browser="";
			if(@$_REQUEST["browser"]=="ie" && $nWidth<565)
				$nWidth=565;
			if(@$_REQUEST["browser"]=="ie")
				$browser="&browser=ie";
			echo "<iframe frameborder=\"0\" vspace=\"0\" hspace=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\" id=\"".$cfield."\" name=\"".$cfield."\" style='width: " . ($nWidth+1) . "px;height: " . ($nHeight+1) . "px;'";
			echo " src=\"".GetTableURL($strTableName)."_innova.##@ext##?".($edit==MODE_INLINE_EDIT || $edit==MODE_INLINE_ADD ? "id=".$id."&" : '').$iquery.$browser."\">";
			echo "</iframe>";
			##endif##			
		}
		else
			echo '<textarea name="'.$cfield.'" style="width: ' . $nWidth . 'px;height: ' . $nHeight . 'px;">'.htmlspecialchars($value).'</textarea>';
	}
	else if($format==EDIT_FORMAT_PASSWORD)
		echo '<input type="Password" name="'.$cfield.'" '.GetEditParams($field).' value="'.htmlspecialchars($value).'">';
	else if($format==EDIT_FORMAT_DATE)
		echo '<input type="hidden" name="'.$ctype.'" value="date'.DateEditType($field).'">'.GetDateEdit($field,$value,DateEditType($field),$secondfield,$edit,$id);
	else if($format==EDIT_FORMAT_RADIO)
		BuildRadioControl($field,$value,$secondfield,$id);
	else if($format==EDIT_FORMAT_CHECKBOX)
	{
		if($edit==MODE_ADD || $edit==MODE_INLINE_ADD || $edit==MODE_EDIT || $edit==MODE_INLINE_EDIT)
		{
			$checked="";
			if($value && $value!=0)
				$checked=" checked";
			echo '<input type="hidden" name="'.$ctype.'" value="checkbox"><input type="Checkbox" name="'.$cfield.'" '.$checked.'>';
		}
		else
		{
			echo '<input type="hidden" name="'.$ctype.'" value="checkbox">';
			echo '<select name="'.$cfield.'">';
			$val=array("none","on","off");
			$show=array("","True","False");
			foreach($val as $i=>$v)
			{
				$sel="";
				if($value===$v)
					$sel=" selected";
				echo '<option value="'.$v.'"'.$sel.'>'.$show[$i].'</option>';
			}
			echo "</select>";
		}
	}
	else if($format==EDIT_FORMAT_DATABASE_IMAGE || $format==EDIT_FORMAT_DATABASE_FILE)
	{
		$disp="";
		$strfilename="";
		$onchangefile="";
		if($edit==MODE_EDIT || $edit==MODE_INLINE_EDIT)
		{
			if($id<>"")
				$ctype.="_".$id;
			$value=db_stripslashesbinary($value);
			$itype=SupposeImageType($value);
			$thumbnailed=false;
			$thumbfield="";
##foreach @BUILDER.Tables as @t filter @t.arrFieldObj[strViewFormat==FORMAT_DATABASE_IMAGE && bShowThumbnail].len##
	##foreach @t.arrFieldObj as @f filter @f.strViewFormat==FORMAT_DATABASE_IMAGE && @f.bShowThumbnail##
			if($strTableName=="##@t.strDataSourceTable s##" && $field=="##@f.strName s##")
			{
				$thumbnailed=true;
				$thumbfield="##@f.strThumbnail s##";
			}
	##endfor##
##endfor##

			if($itype)
			{
				if($thumbnailed)
				{
					$disp="<a target=_blank href=\"".GetTableURL($strTableName)."_imager.##@ext##?".$iquery."\">";
					$disp.= "<img name=\"".$cfield."\" border=0";
					$disp.=" src=\"".GetTableURL($strTableName)."_imager.##@ext##?field=".rawurlencode($thumbfield)."&alt=".rawurlencode($field).$keylink."\">";
					$disp.= "</a>";
				}
				else
					$disp='<img name="'.$cfield.'" border=0 src="'.GetTableURL($strTableName).'_imager.##@ext##?'.$iquery.'">';
			}
			else
			{
				if(strlen($value))
					$disp='<img name="'.$cfield.'" border=0 src="images/file.gif">';
				else
					$disp='<img name="'.$cfield.'" border=0 src="images/no_image.gif">';
			}
//	filename
			if($format==EDIT_FORMAT_DATABASE_FILE && !$itype && strlen($value))
			{
				if(!($filename=@$data[GetFilenameField($field)]))
					$filename="file.bin";
				$disp='<a href="'.GetTableURL($strTableName).'_getfile.##@ext##?filename='.htmlspecialchars($filename).'&'.$iquery.'".>'.$disp.'</a>';
			}
//	filename edit
			if($format==EDIT_FORMAT_DATABASE_FILE && GetFilenameField($field))
			{
				if(!($filename=@$data[GetFilenameField($field)]))
					$filename="";
				if($edit==MODE_INLINE_EDIT)
				{
					$strfilename='<br>'.##message FILENAME##.'&nbsp;&nbsp;<input id="filename_'.$cfieldname.'_'.$id.'" name="filename_'.$cfieldname.'" size="20" maxlength="50" value="'.htmlspecialchars($filename).'">';
					$onchangefile.="var path=$('#".$cfield."_".$id."').val(); var wpos=path.lastIndexOf('\\\\'); var upos=path.lastIndexOf('/'); var pos=wpos; if(upos>wpos) pos=upos; $('#filename_".$cfieldname."_".$id."').val(path.substr(pos+1));";
				}
				else
				{
					$strfilename='<br>'.##message FILENAME##.'&nbsp;&nbsp;<input name="filename_'.$cfieldname.'" size="20" maxlength="50" value="'.htmlspecialchars($filename).'">';
					$onchangefile.="var path=this.form.elements['".jsreplace($cfield)."'].value; var wpos=path.lastIndexOf('\\\\'); var upos=path.lastIndexOf('/'); var pos=wpos; if(upos>wpos) pos=upos; this.form.elements['filename_".jsreplace($cfieldname)."'].value=path.substr(pos+1);";
				}
			}
			$strtype='<br><input type="Radio" name="'.$ctype.'" value="file0" checked>'.##message KEEP##;
			if(strlen($value) && !IsRequired($field))
			{
				$strtype.='<input type="Radio" name="'.$ctype.'" value="file1">'.##message DELETE##;
				if ($edit==MODE_INLINE_EDIT ) {
					$onchangefile.='$(\'input[@type=radio][@value=file2][@name='.$ctype.']\').get(0).checked=true;';
				} else {				
					$onchangefile.='this.form.elements[\''.jsreplace($ctype).'\'][2].checked=true;';
				}
			}
			else {
				if ($edit==MODE_INLINE_EDIT) {
					$onchangefile.='$(\'input[@type=radio][@value=file2][@name='.$ctype.']\').get(0).checked=true;';
				} else {			
					$onchangefile.='this.form.elements[\''.jsreplace($ctype).'\'][1].checked=true;';
				}
			}
			
			$strtype.='<input type="Radio" name="'.$ctype.'" value="file2">'.##message UPDATE##;
		}
		else
		{
//	if Add mode
			$strtype='<input type="hidden" name="'.$ctype.'" value="file2">';
			if($format==EDIT_FORMAT_DATABASE_FILE && GetFilenameField($field))
			{
				$strfilename='<br>'.##message FILENAME##.'&nbsp;&nbsp;<input name="filename_'.$cfieldname.'" size="20" maxlength="50">';
				if($edit==MODE_INLINE_ADD)
					$onchangefile.="var path=$('#".$cfield."_".$id."').val(); var wpos=path.lastIndexOf('\\\\'); var upos=path.lastIndexOf('/'); var pos=wpos; if(upos>wpos) pos=upos; $('#filename_".$cfieldname."_".$id."').val(path.substr(pos+1));";
				else
					$onchangefile.="var path=this.form.elements['".jsreplace($cfield)."'].value; var wpos=path.lastIndexOf('\\\\'); var upos=path.lastIndexOf('/'); var pos=wpos; if(upos>wpos) pos=upos; this.form.elements['filename_".jsreplace($cfieldname)."'].value=path.substr(pos+1);";
			}
		}
		if($onchangefile)
			$onchangefile='onChange="'.$onchangefile.'"';
		if($edit==MODE_INLINE_EDIT && $format==EDIT_FORMAT_DATABASE_FILE)
			$disp="";
		echo $disp.$strtype.'<br><input type="File" id="'.$cfield."_".$id.'" name="'.$cfield.'" '.$onchangefile.'>'.$strfilename;
	}
	else if($format==EDIT_FORMAT_LOOKUP_WIZARD)
			BuildSelectControl($field, $value, $arr, $secondfield, $edit,$id);
	else if($format==EDIT_FORMAT_HIDDEN)
			echo '<input type="Hidden" name="'.$cfield.'" value="'.htmlspecialchars($value).'">';
	else if($format==EDIT_FORMAT_READONLY)
			echo '<input type="Hidden" name="'.$cfield.'" value="'.htmlspecialchars($value).'">';
	else if($format==EDIT_FORMAT_FILE)
	{
		$disp="";
		$strfilename="";
		$onchangefile="";
		$function="";
		if($edit==MODE_EDIT || $edit==MODE_INLINE_EDIT)
		{
//	show current file
			if($id<>"")
				$ctype.="_".$id;
			if(ViewFormat($field)==FORMAT_FILE || ViewFormat($field)==FORMAT_FILE_IMAGE)
			{
				$disp=GetData($data,$field,ViewFormat($field))."<br>";
			}
			$filename=$value;
			if($edit != MODE_INLINE_EDIT)
			{
				$function .=
				'<script language=\'javascript\'>'.
				'function controlfilename'.$cfieldname.'(enable)
				{
					if(enable)
					{
						document.forms.editform.'.$cfield.'.style.backgroundColor="white";
						document.forms.editform.'.$cfield.'.disabled=false;
					}
					else
					{
						document.forms.editform.'.$cfield.'.style.backgroundColor="gainsboro";
						document.forms.editform.'.$cfield.'.disabled=true;
					}
				}'.
				'</script>';
			}
//	filename edit
			$filename_size=30;
			if(UseTimestamp($field))
				$filename_size=50;
			$strfilename='<input type=hidden name="filename_'.$cfieldname.'" value="'.htmlspecialchars($filename).'"><br>'.##message FILENAME##.'&nbsp;&nbsp;<input style="background-color:gainsboro" disabled id="'.$cfield.'_'.$id.'" name="'.$cfield.'" size="'.$filename_size.'" maxlength="100" value="'.htmlspecialchars($filename).'">';
			if ( $edit==MODE_INLINE_EDIT ) {
				$onchangefile.="var path=$('[@id=file_".$cfieldname."_".$id."]').val(); var wpos=path.lastIndexOf('\\\\'); var upos=path.lastIndexOf('/'); var pos=wpos; if(upos>wpos) pos=upos; $('#".$cfield."_".$id."').css('backgroundColor','white');$('#".$cfield."_".$id."')[0].disabled=false;";
				if(UseTimestamp($field))
					$onchangefile.="$('[@id=".$cfield."_".$id."]').val(addTimestamp(path.substr(pos+1))); ";
				else
					$onchangefile.="$('[@id=".$cfield."_".$id."]').val(path.substr(pos+1)); ";
				$strtype='<br><input type="Radio" name="'.$ctype.'" value="upload0" checked onclick="$(\'[@id='.$cfield.'_'.$id.']\').css(\'backgroundColor\',\'gainsboro\');$(\'[@id='.$cfield.'_'.$id.']\')[0].disabled=true;">'.##message KEEP##;
			} else {
				$onchangefile.="var path=this.form.file_".$cfieldname.".value; var wpos=path.lastIndexOf('\\\\'); var upos=path.lastIndexOf('/'); var pos=wpos; if(upos>wpos) pos=upos; controlfilename".$cfieldname."(true);";
				if(UseTimestamp($field))
					$onchangefile.="this.form.".$cfield.".value=addTimestamp(path.substr(pos+1)); ";
				else
					$onchangefile.="this.form.".$cfield.".value=path.substr(pos+1); ";
				$strtype='<br><input type="Radio" name="'.$ctype.'" value="upload0" checked onclick="controlfilename'.$cfieldname.'(false)">'.##message KEEP##;
			}


			if(strlen($value) && !IsRequired($field))
			{
				if ($edit==MODE_INLINE_EDIT) {
					$strtype.='<input type="Radio" name="'.$ctype.'" value="upload1" onclick="$(\'[@id='.$cfield.'_'.$id.']\').css(\'backgroundColor\',\'gainsboro\');$(\'[@id='.$cfield.'_'.$id.']\')[0].disabled=true;">'.##message DELETE##;
					$onchangefile.='$(\'input[@type=radio][@value=upload2][@name='.$ctype.']\').get(0).checked=true;';
				} else {
					$strtype.='<input type="Radio" name="'.$ctype.'" value="upload1" onclick="controlfilename'.$cfieldname.'(false)">'.##message DELETE##;
					$onchangefile.='this.form.'.$ctype.'[2].checked=true;';
				}
			}
			else {
				if ($edit==MODE_INLINE_EDIT) {
					$onchangefile.='$(\'input[@type=radio][@value=upload2][@name='.$ctype.']\').get(0).checked=true;';
				} else {			
					$onchangefile.='this.form.'.$ctype.'[1].checked=true;';
				}
			}
			if ($edit==MODE_INLINE_EDIT) {
				$strtype.='<input type="Radio" name="'.$ctype.'" value="upload2" onclick="$(\'[@id='.$cfield.'_'.$id.']\').css(\'backgroundColor\',\'white\');$(\'[@id='.$cfield.'_'.$id.']\')[0].disabled=false;">'.##message UPDATE##;
			} else {
				$strtype.='<input type="Radio" name="'.$ctype.'" value="upload2" onclick="controlfilename'.$cfieldname.'(true)">'.##message UPDATE##;
			}
		}
		else
		{
//	if Adding record		
			$filename_size=30;
			if(UseTimestamp($field))
				$filename_size=50;
			$strtype='<input type="hidden" name="'.$ctype.'" value="upload2">';
			$strfilename='<br>'.##message FILENAME##.'&nbsp;&nbsp;<input name="'.$cfield.'" size="'.$filename_size.'" maxlength="100">';
			if($edit==MODE_INLINE_ADD)
			{
				$onchangefile.="var path=$('[@id=file_".$cfieldname."_".$id."]').val(); var wpos=path.lastIndexOf('\\\\'); var upos=path.lastIndexOf('/'); var pos=wpos; if(upos>wpos) pos=upos;";
				if(UseTimestamp($field))
					$onchangefile.=" $('[@id=".$cfield."_".$id."]').val(addTimestamp(path.substr(pos+1)));";
				else
					$onchangefile.=" $('[@id=".$cfield."_".$id."]').val(path.substr(pos+1));";
			}
			else
			{
				$onchangefile.="var path=this.form.file_".$cfieldname.".value; var wpos=path.lastIndexOf('\\\\'); var upos=path.lastIndexOf('/'); var pos=wpos; if(upos>wpos) pos=upos;";
				if(UseTimestamp($field))
					$onchangefile.=" this.form.".$cfield.".value=addTimestamp(path.substr(pos+1));";
				else
					$onchangefile.=" this.form.".$cfield.".value=path.substr(pos+1);";
			}
		}
		if($onchangefile)
			$onchangefile='onChange="'.$onchangefile.'"';
		echo $disp.$strtype.$function.'<br><input type="File" id="file_'.$cfieldname.'" name="file_'.$cfieldname.'" '.$onchangefile.'>'.$strfilename;
	}
}
function my_stripos($str,$needle, $offest)
{
    if (strlen($needle)==0 || strlen($str)==0)
		return false;
	return strpos(strtolower($str),strtolower($needle), $offest);
} 

function my_str_ireplace($search, $replace,$str)
{
    $pos=my_stripos($str,$search,0);
	if($pos===false)
		return $str;
	return substr($str,0,$pos).$replace.substr($str,$pos+strlen($search));
} 


function in_assoc_array($name, $arr)
{
foreach ($arr as $key => $value) 
	if ($key==$name)
		return true;

return false;
}

function loadSelectContent($field, $value,$fvalue="")
{
	global $conn,$LookupSQL,$strTableName;
	
	$Lookup = "";
	$response = array();
	$output = "";

##foreach @BUILDER.Tables as @t filter @t.arrFieldObj[strEditFormat==EDIT_FORMAT_LOOKUP_WIZARD].len##
##foreach @t.arrFieldObj as @f filter @f.strEditFormat==EDIT_FORMAT_LOOKUP_WIZARD && @f.bUseCategory##
	if($strTableName=="##@t.strDataSourceTable s##" && $field=="##@f.strName s##") 
	{
##if !@f.bFastType##
		$value = make_db_value("##@f.strCategoryControlField s##",$value);
	##if @f.nLookupType==LT_LOOKUPTABLE##
		$LookupSQL = "SELECT ";
	##if @f.bUnique##
		$LookupSQL .= "DISTINCT ";
	##endif##
		$LookupSQL .= "##@f.strLinkField ws##";
	##if @f.bCustomDisplay##
		$LookupSQL .= ",##@f.strDisplayField s##";
	##else##
		$LookupSQL .= ",##@f.strDisplayField ws##";
	##endif##
		$LookupSQL .= " FROM ##@f.pLookupObj.strTable ts## ";
	##if @f.strWhere##
		$LookupSQL.="where (".##@f.strWhere##.")  AND ##@f.strCategoryFilterField ws##=".$value;
	##else##
		$LookupSQL .= " WHERE ##@f.strCategoryFilterField ws##=".$value;
	##endif##
	##if @f.strOrderBy##
		$LookupSQL.= " ORDER BY ##@f.pLookupObj.strTable ts##.##@f.strOrderBy ws##";
		##if @f.pLookupObj.bDesc##
		$LookupSQL.=" DESC";
		##endif##
	##endif##
	##endif##
##else##
		$value = make_db_value("##@f.strCategoryControlField s##",$value);
		if(strlen($fvalue))
			$fvalue = make_db_value("##@f.strName s##",$fvalue);
	##if @f.nLookupType==LT_LOOKUPTABLE##
		$LookupSQL = "SELECT ";
	##if @f.bUnique##
		$LookupSQL .= "DISTINCT ";
	##endif##
		$LookupSQL .= "##@f.strLinkField ws##";
	##if @f.bCustomDisplay##
		$LookupSQL .= ",##@f.strDisplayField s##";
	##else##
		$LookupSQL .= ",##@f.strDisplayField ws##";
	##endif##
		$LookupSQL .= " FROM ##@f.pLookupObj.strTable ts## ";
	##if @f.strWhere##
		$LookupSQL.="where (".##@f.strWhere##.")  AND ##@f.strCategoryFilterField ws##=".$value;
	##else##
		$LookupSQL .= " WHERE ##@f.strCategoryFilterField ws##=".$value;
	##endif##
		if(strlen($fvalue))
			$LookupSQL.=" AND ##@f.strLinkField ws##=".$fvalue;
	##if @f.strOrderBy##
		$LookupSQL.= " ORDER BY ##@f.pLookupObj.strTable ts##.##@f.strOrderBy ws##";
		##if @f.pLookupObj.bDesc##
		$LookupSQL.=" DESC";	
		##endif##
	##endif##
	##endif##
##endif##
	}
##endfor##
##endfor##
	$rs=db_query($LookupSQL,$conn);

	if(!FastType($field))
	{
		while ($data = db_fetch_numarray($rs)) 
		{
			$response[] = $data[0];
			$response[] = $data[1];
		}
	}
	else
	{
		$data=db_fetch_numarray($rs);
//	one record only
		if($data && (strlen($fvalue) || !db_fetch_numarray($rs)))
		{
			$response[] = $data[0];
			$response[] = $data[1];
		}
	}
	return $response;
}

function xmlencode($str)
{

	$str = str_replace("&","&amp;",$str);
	$str = str_replace("<","&lt;",$str);
	$str = str_replace(">","&gt;",$str);

	$out="";
	$len=strlen($str);
	$ind=0;
	for($i=0;$i<$len;$i++)
	{
		if(ord(substr($str,$i,1))>=128)
		{
			if($ind<$i)
				$out.=substr($str,$ind,$i-$ind);
			$out.="&#".ord(substr($str,$i,1)).";";
			$ind=$i+1;
		}
	}
	if($ind<$len)
		$out.=substr($str,$ind);
	return str_replace("'","&apos;",$out);

}

function print_inline_array(&$arr,$printkey=false)
{
	if(!$printkey)
	{
		foreach ( $arr as $key=>$val )
			echo str_replace(array("&","<","\\","\r","\n"),array("&amp;","&lt;","\\\\","\\r","\\n"),str_replace(array("\\","\r","\n"),array("\\\\","\\r","\\n"),$val))."\\n";
	}
	else
	{
		foreach( $arr as $key=>$val )
			echo str_replace(array("&","<","\\","\r","\n"),array("&amp;","&lt;","\\\\","\\r","\\n"),str_replace(array("\\","\r","\n"),array("\\\\","\\r","\\n"),$key))."\\n";
	}
		
}

##if @BUILDER.bStrongPassword##
function checkpassword($pwd)
{
	$len=strlen($pwd);
##if @BUILDER.nPwdLen##
	if($len<##@BUILDER.nPwdLen##)
		return false;
##endif##
	$calpha=array();
	$clower=0;
	$cupper=0;
	$cdigit=0;
	for($i=0;$i<$len;$i++)
	{
		$c=substr($pwd,$i,1);
		if($c>='a' && $c<='z')
			$clower++;
		else if($c>='A' && $c<='Z')
			$cupper++;
		else
			$cdigit++;
		$calpha[$c]=1;
	}
	##if @BUILDER.nPwdUnique##
	if(count($calpha)<##@BUILDER.nPwdUnique##)
		return false;
	##endif##
	
	##if @BUILDER.nPwdDigits##
	if($cdigit<##@BUILDER.nPwdDigits##)
		return false;
	##endif##
	##if @BUILDER.bPwdUpperLower##
	if(!$clower || !$cupper)
		return false;
	##endif##
	return true;
}
##endif##

function GetChartXML($chartname)
{

##foreach @BUILDER.Tables as @t filter @t.nType==titCHART##
	if ("##@t.strShortTableName##"==$chartname)
	{
		return '<chart>
<attr value="tables">
	<attr value="0">##@t.strDatasourceTable xq##</attr>
</attr>
<attr value="chart_type">
	<attr value="type">##@t.strChartID##</attr>
</attr>

<attr value="parameters">
##foreach @t.arrChartDataSeries as @field filter @field.strDataField!=""##
<attr value="##eval @index-1##">
<attr value="name">##@field.strDataField xq##</attr>
</attr>
##if @last##
<attr value="##eval @index##">
<attr value="name">##@t.strChartLabelField xq##</attr>
</attr>
##endif##
##endfor##

</attr>

<attr value="appearance">
##foreach @t.arrChartDataSeries as @field##
	<attr value="color##@index##1">##@field.strColor##</attr>
	<attr value="color##@index##2">##@field.strColor##</attr>
##endfor##

<attr value="color51">##@t.strChartNamesColor##</attr>
<attr value="color52">##@t.strChartNamesColor##</attr>
<attr value="color61">##@t.strChartValuesColor##</attr>
<attr value="color62">##@t.strChartValuesColor##</attr>
<attr value="color71">##@t.strChartFirstColor##</attr>
<attr value="color72">##@t.strChartFirstColor##</attr>
<attr value="color81">##@t.strChartSecondColor##</attr>
<attr value="color82">##@t.strChartSecondColor##</attr>
<attr value="color91">##@t.strChartBorderColor##</attr>
<attr value="color92">##@t.strChartBorderColor##</attr>
<attr value="color101">##@t.strChartHeaderColor##</attr>
<attr value="color102">##@t.strChartHeaderColor##</attr>
<attr value="color111">##@t.strChartFooterColor##</attr>
<attr value="color112">##@t.strChartFooterColor##</attr>
<attr value="color121"></attr>
<attr value="color122"></attr>
<attr value="color131">##@t.strXAxisColor##</attr>
<attr value="color132">##@t.strXAxisColor##</attr>
<attr value="color141">##@t.strYAxisColor##</attr>
<attr value="color142">##@t.strYAxisColor##</attr>

<attr value="slegend">##if @t.bChartLegend##true##else##false##endif##</attr>
<attr value="sgrid">##if @t.bChartGrid##true##else##false##endif##</attr>
<attr value="sname">##if @t.bChartNames##true##else##false##endif##</attr>
<attr value="sval">##if @t.bChartValues##true##else##false##endif##</attr>
<attr value="sanim">##if @t.bAnimation##true##else##false##endif##</attr>
<attr value="scur">##if @t.bChartCurrency##true##else##false##endif##</attr>
<attr value="sstacked">##if @t.b100Stacked##true##else##false##endif##</attr>
<attr value="saxes">##if @t.bMultipleAxes##true##else##false##endif##</attr>
<attr value="slog">##if @t.bLogarithmic##true##else##false##endif##</attr>
<attr value="dec">##@t.nDigits##</attr>
<attr value="head">##@t.strChartHeader xq##</attr>
<attr value="foot">##@t.strChartFooter xq##</attr>
<attr value="aqua">##@t.strChartStyle xq##</attr>
<attr value="cview">##@t.strChartShape xq##</attr>
</attr>

<attr value="fields">
##foreach @t.arrFieldObj as @f##
	<attr value="##eval @index-1##">
		<attr value="name">##@f.strName xq##</attr>
		<attr value="label">##@f.strLabel xq##</attr>
		<attr value="search"></attr>
	</attr>
##endfor##
</attr>


<attr value="settings">
<attr value="name">##@t.strName xq##</attr>
<attr value="short_table_name">##@t.strShortTableName s##</attr>
</attr>

</chart>';
	}
##endfor##

}

//For add script code to function postloadstep, that performed after loading all files
function AddScript2Postload($code,$id="")
{
	echo "<script  language='javascript'>
			$(document).ready(function()
			{ 
				AddScript2Postload(function(){".$code."}, ".($id ? $id : "''").");
			});
		 </script>";
}

function AddCSSFile($file)
{
	global $includes_css;
	$includes_css[]=$file;
}

function AddJSFile($file,$req1="",$req2="",$req3="")
{
	global $includes_js,$includes_jsreq;
	$includes_js[]=$file;
	if($req1!="")
		$includes_jsreq[$file]=array($req1);
	if($req2!="")
		$includes_jsreq[$file][]=$req2;
	if($req3!="")
		$includes_jsreq[$file][]=$req3;
}

function LoadJS_CSS($id)
{
	global $includes_js,$includes_jsreq,$includes_css;
	$includes_js=array_unique($includes_js);
	$includes_css=array_unique($includes_css);
	$sl = "sl".$id;
	$out="var ".$sl." = new ScriptLoader('".$id."');\r\n";
	foreach($includes_css as $file)
		$out.=$sl.".loadCSS('".$file."');";
	foreach($includes_js as $file)
	{
		$out .= $sl.".addJS('".$file."'";
		if(array_key_exists($file,$includes_jsreq))
		{
			foreach($includes_jsreq[$file] as $req)
				$out.=",'".$req."'";
		}
		$out.=");\r\n";
	}
	$out.=$sl.".load();";
	return $out;
}

function PrepareJSCode(&$js,$id)
{
	$js="window.postloadstep".($id ? "_".$id : '')." = function(){".$js."};\r\n ";
	$js.=LoadJS_CSS($id);
}


function loadindicator()
{
	$path = GetAbsoluteFileName("templates/loadindicator.htm");
	return myfile_get_contents($path,"r");
}


class WhereClause
{
	var $_where = null;
	var $_security = '';
	
	function WhereClause()
	{
		$strSecuritySql = SecuritySQL("Search");
		if($strSecuritySql)
			$this->_security = $strSecuritySql;
	}

	function getWhere()
	{
		global $strTableName;
		$sWhere="";
		if($this->_where && isset($this->_where[$strTableName."_asearchfor"]))
		{
			foreach($this->_where[$strTableName."_asearchfor"] as $f => $sfor)
			{
				$strSearchFor = trim($sfor);
				$strSearchFor2 = "";
				$type=@$this->_where[$strTableName."_asearchfortype"][$f];
				if(array_key_exists($f,@$this->_where[$strTableName."_asearchfor2"]))
					$strSearchFor2=trim(@$this->_where[$strTableName."_asearchfor2"][$f]);
				if($strSearchFor!="" || true)
				{
					if (!$sWhere) 
					{
						if($this->_where[$strTableName."_asearchtype"]=="and")
							$sWhere="1=1";
						else
							$sWhere="1=0";
					}
					$strSearchOption=trim($this->_where[$strTableName."_asearchopt"][$f]);
					if($where=StrWhereAdv($f, $strSearchFor, $strSearchOption, $strSearchFor2,$type))
					{
						if($this->_where[$strTableName."_asearchnot"][$f])
							$where="not (".$where.")";
						if($this->_where[$strTableName."_asearchtype"]=="and")
		   					$sWhere .= " and ".$where;
						else
		   					$sWhere .= " or ".$where;
					}
				}
			}
		}
		
		if($this->_security)
			$sWhere = whereAdd($sWhere, $this->_security);
			
		return $sWhere;
	}
	
	function parseRequest()
	{
		global $strTableName;
		$this->_where = array();
		$this->_where[$strTableName."_asearchnot"]=array();
		$this->_where[$strTableName."_asearchopt"]=array();
		$this->_where[$strTableName."_asearchfor"]=array();
		$this->_where[$strTableName."_asearchfor2"]=array();
		$tosearch=0;
		$asearchfield = postvalue("asearchfield");
		$this->_where[$strTableName."_asearchtype"] = postvalue("type");
		if(!$this->_where[$strTableName."_asearchtype"])
			$this->_where[$strTableName."_asearchtype"]="and";
		if(isset($asearchfield) && is_array($asearchfield))
		{
			foreach($asearchfield as $field)
			{
				$gfield=GoodFieldName($field);
				$asopt=postvalue("asearchopt_".$gfield);
				$value1=postvalue("value_".$gfield);
				$type=postvalue("type_".$gfield);
				$value2=postvalue("value1_".$gfield);
				$not=postvalue("not_".$gfield);
				if($value1 || $asopt=='Empty')
				{
					$tosearch=1;
					$this->_where[$strTableName."_asearchopt"][$field]=$asopt;
					if(!is_array($value1))
						$this->_where[$strTableName."_asearchfor"][$field]=$value1;
					else
						$this->_where[$strTableName."_asearchfor"][$field]=combinevalues($value1);
					$this->_where[$strTableName."_asearchfortype"][$field]=$type;
					if($value2)
						$this->_where[$strTableName."_asearchfor2"][$field]=$value2;
					$this->_where[$strTableName."_asearchnot"][$field]=($not=="on");
				}
			}
		}
		if($tosearch)
			$this->_where[$strTableName."_search"]=2;
		else
			$this->_where[$strTableName."_search"]=0;
		$this->_where[$strTableName."_pagenumber"]=1;
	}
	
	function applyWhere(&$sql)
	{
		$toAdd = $this->getWhere();
		if($toAdd)
		{
			if($sql[2])
			{
				$sql[2] = '('.$sql[2].') AND ';
			}
			
			$sql[2] .= '('.$toAdd.') ';
		}
		
		return $sql;
	}
	
	function getTable()
	{
		return $this->_where;
	}
}

function GetSiteUrl()
{
	$url = "http://".$_SERVER["SERVER_NAME"];
	if($_SERVER["SERVER_PORT"]!=80)
	{
		if ($_SERVER["SERVER_PORT"]==443)
		   $url = "https://".$_SERVER["SERVER_NAME"];
		else
		   $url.=":".$_SERVER["SERVER_PORT"];
	}
	return $url;
}
?>