<?php 
ini_set("display_errors","1");
ini_set("display_startup_errors","1");
set_magic_quotes_runtime(0);

include("oldinclude/dbcommon.php");
include("oldinclude/##@TABLE.strShortTableName##_variables.php");
##if @BUILDER.arrLanguages.len>1##
include("oldinclude/languages.php");
##endif##


##if @BUILDER.bCreateLoginPage##
//	check if logged in
if(!@$_SESSION["UserID"] || !CheckSecurity(@$_SESSION["_".$strTableName."_OwnerID"],"Edit"))
{ 
	$_SESSION["MyURL"]=$_SERVER["SCRIPT_NAME"]."?".$_SERVER["QUERY_STRING"];
	header("Location: login.php?message=expired"); 
	return;
}
##endif##

$filename="";
$status="";
$message="";
$usermessage="";
$error_happened=false;
$readevalues=false;


$showKeys = array();
$showValues = array();
$showRawValues = array();
$showFields = array();
$showDetailKeys = array();
$IsSaved = false;
$HaveData = true;
$inlineedit = (@$_REQUEST["editType"]=="inline") ? true : false;
##if @TABLE.bInlineEdit##
$templatefile = ( $inlineedit ) ? "##@TABLE.strShortTableName s##_inline_edit.htm" : "##@TABLE.strShortTableName##_edit.htm";
##else##
$templatefile = "##@TABLE.strShortTableName##_edit.htm";
##endif##

//connect database
$conn = db_connect();

//	Before Process event
if(function_exists("BeforeProcessEdit"))
	BeforeProcessEdit($conn);

$keys=array();
##foreach @TABLE.arrKeyFields as @k##
$keys["##@k s##"]=postvalue("editid##@index##");
##endfor##

//	prepare data for saving
if(@$_POST["a"]=="edited")
{
##if @TABLE.arrEventHandlers[strEventID=="EVENT_BEFOREEDIT" || strEventID=="EVENT_AFTEREDIT"].len##
//	read old values
//	$strSQL = $gstrSQL;
	$strWhereClause=KeyWhere($keys);
	##if @TABLE.nSecOptions!=ADVSECURITY_ALL && @BUILDER.nLoginMethod==SECURITY_TABLE && @BUILDER.bCreateLoginPage##
	//	select only owned records
	$strWhereClause=whereAdd($strWhereClause,SecuritySQL("Edit"));
	##endif##
	$strSQL=gSQLWhere($strWhereClause);
	$rsold=db_query($strSQL,$conn);
	$dataold=db_fetch_array($rsold);
##endif##
	$strWhereClause=KeyWhere($keys);
	$strSQL = "update ".AddTableWrappers($strOriginalTableName)." set ";
	$evalues=array();
	$efilename_values=array();
	$files_delete=array();
	$files_move=array();
##if @BUILDER.nDatabaseType==DATABASE_Oracle##
	$blobs=array();
##endif##
##foreach Fields as @f filter IsUpdatable(@f) && (@f.bInlineEdit || @f.bEditPage)##
//	processing ##@f.strName## - start
##if !@f.bEditPage##
	if($inlineedit)
	{
##elseif !@f.bInlineEdit##
	if(!$inlineedit)
	{
##endif##
	$value = postvalue("value_##@f.strName g##");
	$type=postvalue("type_##@f.strName g##");
	if (in_assoc_array("type_##@f.strName g##",$_POST) || in_assoc_array("value_##@f.strName g##",$_POST) || in_assoc_array("value_##@f.strName g##",$_FILES))	
	{
##if @f.strEditFormat==EDIT_FORMAT_DATABASE_FILE && @f.ListFormatObj.strFilename || @f.strEditFormat==EDIT_FORMAT_FILE##
		$value=prepare_for_db("##@f.strName s##",$value,$type,postvalue("filename_##@f.strName g##"));
##else##
		$value=prepare_for_db("##@f.strName s##",$value,$type);
##endif##
	}
	else
		$value=false;
	if(!($value===false))
	{	
##if @f.bCreateThumbnail && (@f.strEditFormat==EDIT_FORMAT_DATABASE_IMAGE || @f.strEditFormat==EDIT_FORMAT_FILE)##
		if($value)
		{
	##if @f.strEditFormat==EDIT_FORMAT_DATABASE_IMAGE##
			$ext = CheckImageExtension($_FILES["value_##@f.strName g##"]["name"]);
			$thumb = CreateThumbnail($value,##@f.nThumbnailSize##,$ext);
		##if @BUILDER.nDatabaseType==DATABASE_Oracle##
			$blobs["##@f.strThumbnail s##"]=$thumb;
			$evalues["##@f.strThumbnail s##"]="EMPTY_BLOB()";
		##else##
			$evalues["##@f.strThumbnail s##"]=$thumb;
		##endif##
	##else##
			$ext = CheckImageExtension($_FILES["file_##@f.strName g##"]["name"]);
			$contents = myfile_get_contents($_FILES["file_##@f.strName g##"]['tmp_name']);
			$thumb = CreateThumbnail($contents,##@f.nThumbnailSize##,$ext);
			$file = GetUploadFolder("##@f.strName s##")."##@f.strThumbnail s##".$value;
			if(file_exists($file))
					@unlink($file);
			$th = fopen($file,"w");
			fwrite($th,$thumb);
			fclose($th);
	##endif##
		}
##endif##

##if @f.bResizeOnUpload && (@f.strEditFormat==EDIT_FORMAT_DATABASE_IMAGE || @f.strEditFormat==EDIT_FORMAT_FILE)##
	// resize image on upload
		if($value)
		{
	##if @f.strEditFormat==EDIT_FORMAT_DATABASE_IMAGE##
			$ext = CheckImageExtension($_FILES["value_##@f.strName g##"]["name"]);
			$thumb = CreateThumbnail($value,##@f.nNewSize##,$ext);
		##if @BUILDER.nDatabaseType==DATABASE_Oracle##
			$blobs["##@f.strName s##"]=$thumb;
			$value="EMPTY_BLOB()";
		##else##
			$value=$thumb;
		##endif##
	##else##
			$ext = CheckImageExtension($_FILES["file_##@f.strName g##"]["name"]);
			$contents = myfile_get_contents($_FILES["file_##@f.strName g##"]['tmp_name']);
			$thumb = CreateThumbnail($contents,##@f.nNewSize##,$ext);
			$file = GetUploadFolder("##@f.strName s##").$value;
			if(file_exists($file))
					@unlink($file);
			$th = fopen($file,"w");
			fwrite($th,$thumb);
			fclose($th);
			$n = array_search(GetUploadFolder("##@f.strName s##").$value, $files_move);
			array_splice($files_move, $n, 1);
	##endif##
		}
##endif##

##if @BUILDER.nDatabaseType==DATABASE_Oracle && !@f.bResizeOnUpload && (@f.strEditFormat==EDIT_FORMAT_DATABASE_FILE || @f.strEditFormat==EDIT_FORMAT_DATABASE_IMAGE)##
		$blobs["##@f.strName s##"]=$value;
		$evalues["##@f.strName s##"]="EMPTY_BLOB()";
##else##
		$evalues["##@f.strName s##"]=$value;
##endif##
##if @f.strEditFormat==EDIT_FORMAT_DATABASE_FILE && @f.ListFormatObj.strFilename##
		$efilename_values["##@f.ListFormatObj.strFilename s##"]=$filename;
##endif##			
	}
##if @f.strEditFormat==EDIT_FORMAT_DATABASE_FILE && @f.ListFormatObj.strFilename##
	if($filename)
		$efilename_values["##@f.ListFormatObj.strFilename s##"]=$filename;
##endif##			

##foreach @TABLE.arrKeyFields as @k##
##if @k==@f.strName##
//	update key value
	$keys["##@k s##"]=$value;
##endif##
##endfor##

//	processibng ##@f.strName## - end
##if !@f.bInlineEdit || !@f.bEditPage##
	}
##endif##
##endfor##

	foreach($efilename_values as $ekey=>$value)
		$evalues[$ekey]=$value;
//	do event
	$retval=true;
	if(function_exists("BeforeEdit"))
		$retval=BeforeEdit($evalues,$strWhereClause,$dataold,$keys,$usermessage,$inlineedit);
	if($retval)
	{		
//	construct SQL string
		foreach($evalues as $ekey=>$value)
		{
			$strSQL.=AddFieldWrappers($ekey)."=".add_db_quotes($ekey,$value).", ";
		}
		if(substr($strSQL,-2)==", ")
			$strSQL=substr($strSQL,0,strlen($strSQL)-2);
		$strSQL.=" where ".$strWhereClause;
##if @TABLE.nSecOptions!=ADVSECURITY_ALL && @BUILDER.nLoginMethod==SECURITY_TABLE && @BUILDER.bCreateLoginPage##
		if(SecuritySQL("Edit"))
			$strSQL .= " and (".SecuritySQL("Edit").")";
##endif##
##if @BUILDER.nDatabaseType==DATABASE_Oracle##
		$locs=array();
		$idx=1;
		if(count($blobs))
		{
			$strSQL.=" returning ";
			$blobfields="";
			$blobvars="";
			foreach($blobs as $ekey=>$value)
			{
				if(count($locs))
				{
					$blobfields.=",";
					$blobvars.=",";
				}
				$blobfields.=AddFieldWrappers($ekey);
				$blobvars.=":bnd".$idx;
				$locs[$ekey]=OCINewDescriptor($conn, OCI_D_LOB);
				$idx++;
			}
			$strSQL.=$blobfields." into ".$blobvars;
		}
		$stmt=OCIParse($conn,$strSQL);
		$idx=1;
		foreach($locs as $ekey=>$value)
		{
			OCIBindByName($stmt,":bnd".$idx,$locs[$ekey],-1,OCI_B_BLOB);
			$idx++;
		}
		LogInfo($strSQL);
		set_error_handler("edit_error_handler");
		OCIExecute($stmt, OCI_DEFAULT);
		foreach($locs as $ekey=>$value)
		{
			$locs[$ekey]->save($blobs[$ekey]);
			$locs[$ekey]->free();
		}
		OCICommit($conn);
		OCIFreeStatement($stmt);
##else##
		set_error_handler("edit_error_handler");
		db_exec($strSQL,$conn);
##endif##
		set_error_handler("error_handler");
		if(!$error_happened)
		{
//	delete & move files
			foreach ($files_delete as $file)
			{
				if(file_exists($file))
					@unlink($file);
			}
			foreach ($files_move as $file)
			{
				move_uploaded_file($file[0],$file[1]);
				if(strtoupper(substr(PHP_OS,0,3))!="WIN")
					@chmod($file[1],0777);
			}
##if @TABLE.bInlineEdit##
			if ( $inlineedit ) 
			{
				$status="UPDATED";
				$message="".##message RECORD_UPDATED##."";
				$IsSaved = true;
			} 
			else 
				$message="<div class=message><<< ".##message RECORD_UPDATED##." >>></div>";
##else##				
			$message="<div class=message><<< ".##message RECORD_UPDATED##." >>></div>";
##endif##			
			if($usermessage!="")
				$message = $usermessage;
//	after edit event
			if(function_exists("AfterEdit"))
			{
				foreach($dataold as $idx=>$val)
				{
					if(!array_key_exists($idx,$evalues))
						$evalues[$idx]=$val;
				}
				AfterEdit($evalues,KeyWhere($keys),$dataold,$keys,$inlineedit);
			}
		}
	}
	else
	{
		$readevalues=true;
		$message = $usermessage;
		$status="DECLINED";
	}
}

//	get current values and show edit controls

//$strSQL = $gstrSQL;

$strWhereClause=KeyWhere($keys);
##if @TABLE.nSecOptions!=ADVSECURITY_ALL && @BUILDER.nLoginMethod==SECURITY_TABLE && @BUILDER.bCreateLoginPage##
//	select only owned records
$strWhereClause=whereAdd($strWhereClause,SecuritySQL("Edit"));
##endif##

$strSQL=gSQLWhere($strWhereClause);

$strSQLbak = $strSQL;
//	Before Query event
if(function_exists("BeforeQueryEdit"))
	BeforeQueryEdit($strSQL,$strWhereClause);

if($strSQLbak == $strSQL)
	$strSQL=gSQLWhere($strWhereClause);
LogInfo($strSQL);
$rs=db_query($strSQL,$conn);
$data=db_fetch_array($rs);

if($readevalues)
{
##foreach Fields as @f filter  (@f.bEditPage || @f.bInlineEdit) && @f.strEditFormat!=EDIT_FORMAT_DATABASE_FILE && @f.strEditFormat!=EDIT_FORMAT_DATABASE_IMAGE && @f.strEditFormat!=EDIT_FORMAT_FILE && @f.strEditFormat!=EDIT_FORMAT_READONLY##
	$data["##@f.strName s##"]=$evalues["##@f.strName s##"];
##endfor##
}

include('oldlibs/Smarty.class.php');
$smarty = new Smarty();

##if @TABLE.bInlineEdit##
if ( !$inlineedit ) {
##endif##
	//	include files
	$includes="";

	//	validation stuff
	$bodyonload="";
	$onsubmit="";
	##foreach Fields as @f filter @f.bEditPage && NeedValidationOnEdit(@f)##
	##if @first##
	$includes.="<script language=\"JavaScript\" src=\"oldinclude/validate.js\"></script>\r\n";
	$includes.="<script language=\"JavaScript\">\r\n";
	$includes.="var TEXT_FIELDS_REQUIRED='".addslashes(##message FIELDS_REQUIRED##)."';\r\n";
	$includes.="var TEXT_FIELDS_ZIPCODES='".addslashes(##message FIELDS_ZIPCODES##)."';\r\n";
	$includes.="var TEXT_FIELDS_EMAILS='".addslashes(##message FIELDS_EMAILS##)."';\r\n";
	$includes.="var TEXT_FIELDS_NUMBERS='".addslashes(##message FIELDS_NUMBERS##)."';\r\n";
	$includes.="var TEXT_FIELDS_CURRENCY='".addslashes(##message FIELDS_CURRENCY##)."';\r\n";
	$includes.="var TEXT_FIELDS_PHONE='".addslashes(##message FIELDS_PHONE##)."';\r\n";
	$includes.="var TEXT_FIELDS_PASSWORD1='".addslashes(##message FIELDS_PASSWORD1##)."';\r\n";
	$includes.="var TEXT_FIELDS_PASSWORD2='".addslashes(##message FIELDS_PASSWORD2##)."';\r\n";
	$includes.="var TEXT_FIELDS_PASSWORD3='".addslashes(##message FIELDS_PASSWORD3##)."';\r\n";
	$includes.="var TEXT_FIELDS_STATE='".addslashes(##message FIELDS_STATE##)."';\r\n";
	$includes.="var TEXT_FIELDS_SSN='".addslashes(##message FIELDS_SSN##)."';\r\n";
	$includes.="var TEXT_FIELDS_DATE='".addslashes(##message FIELDS_DATE##)."';\r\n";
	$includes.="var TEXT_FIELDS_TIME='".addslashes(##message FIELDS_TIME##)."';\r\n";
	$includes.="var TEXT_FIELDS_CC='".addslashes(##message FIELDS_CC##)."';\r\n";
	$includes.="var TEXT_FIELDS_SSN='".addslashes(##message FIELDS_SSN##)."';\r\n";
	$includes.="</script>\r\n";
	##endif##
	##if @f.strEditFormat!=EDIT_FORMAT_TEXT_FIELD && @f.strEditFormat!=EDIT_FORMAT_TIME##
		$validatetype="";
	##else##
	  ##if @f.EditFormatObj.strValidateAs==VALIDATE_AS_NUMBER##
		$validatetype="IsNumeric";
	  ##elseif @f.EditFormatObj.strValidateAs==VALIDATE_AS_PASSWORD##
		$validatetype="IsPassword";
	  ##elseif @f.EditFormatObj.strValidateAs==VALIDATE_AS_EMAIL##
		$validatetype="IsEmail";
	  ##elseif @f.EditFormatObj.strValidateAs==VALIDATE_AS_CURRENCY##
		$validatetype="IsMoney";
	  ##elseif @f.EditFormatObj.strValidateAs==VALIDATE_AS_ZIPCODE##
		$validatetype="IsZipCode";
	  ##elseif @f.EditFormatObj.strValidateAs==VALIDATE_AS_PHONENUMBER##
		$validatetype="IsPhoneNumber";
	  ##elseif @f.EditFormatObj.strValidateAs==VALIDATE_AS_STATE##
		$validatetype="IsState";
	  ##elseif @f.EditFormatObj.strValidateAs==VALIDATE_AS_SSN##
		$validatetype="IsSSN";
	  ##elseif @f.EditFormatObj.strValidateAs==VALIDATE_AS_CC##
		$validatetype="IsCC";
	  ##elseif @f.EditFormatObj.strValidateAs==VALIDATE_AS_TIME##
		$validatetype="IsTime";
	  ##else##
		$validatetype="";
	  ##endif##
	##endif##
	##if @f.EditFormatObj.bRequired && @f.strEditFormat!=EDIT_FORMAT_CHECKBOX && @f.strEditFormat!=EDIT_FORMAT_READONLY##
		$validatetype.="IsRequired";
	##endif##
		if($validatetype)
			$bodyonload.="define('value_##@f.strName g##','".$validatetype."','##@f.strLabel js##');";
	##endfor##

	if($bodyonload)
	{
		$onsubmit="return validate();";
		$bodyonload="onload=\"".$bodyonload."\"";
	}

	$includes.="<script language=\"JavaScript\" src=\"oldinclude/jquery.js\"></script>\r\n";
	$includes.="<script language=\"JavaScript\" src=\"oldinclude/onthefly.js\"></script>\r\n";
	if ($useAJAX) {
	$includes.="<script language=\"JavaScript\" src=\"oldinclude/ajaxsuggest.js\"></script>\r\n";
	}
	$includes.="<script language=\"JavaScript\" src=\"oldinclude/jsfunctions.js\"></script>\r\n";
	$includes.="<script language=\"JavaScript\">\r\n";
	$includes .= "var locale_dateformat = ".$locale_info["LOCALE_IDATE"].";\r\n".
	"var locale_datedelimiter = \"".$locale_info["LOCALE_SDATE"]."\";\r\n".
	"var bLoading=false;\r\n".
	"var TEXT_PLEASE_SELECT='".addslashes(##message PLEASE_SELECT##)."';\r\n";
	if ($useAJAX) {
	$includes.="var SUGGEST_TABLE='##@TABLE.strShortTableName##_searchsuggest.php';\r\n";
	}
	$includes.="</script>\r\n";

	if ($useAJAX)
		$includes.="<div id=\"search_suggest\"></div>\r\n";

	##foreach Fields as @f filter @f.bEditPage && UseCalendar(@f)##
	##if @first##
	//	include datepicker files
	$includes.="<script language=\"JavaScript\" src=\"oldinclude/calendar.js\"></script>\r\n";
	##endif##
	##endfor##

	##foreach Fields as @f filter @f.bEditPage && UseRTEBASIC(@f)##
	##if @first##
	//	include Rich Text Editor files
	$includes.="<script language=\"JavaScript\" type=\"text/javascript\" src=\"oldinclude/richtext.js\"></script>\r\n";
	$includes.="<script language=\"JavaScript\">\r\n";
	$includes.="var TEXT_VIEW_SOURCE='".addslashes(##message VIEW_SOURCE##)."';\r\n";
	$includes.="initRTE('oldinclude/images/', 'oldinclude/', '');\r\n";
	$includes.="</script>\r\n";
	$onsubmit="updateRTEs();".$onsubmit;
	##endif##
	##endfor##

	##foreach Fields as @f filter @f.bEditPage && UseRTEFCK(@f)##
	##if @first##
	include("plugins/fckeditor/fckeditor.php");
	##endif##
	##endfor##

	##foreach Fields as @f filter @f.bEditPage && UseRTEINNOVA(@f)##
	##if @first##
	$includes .= "<script language=\"Javascript\" src=\"plugins/innovaeditor/scripts/innovaeditor.js\"></script>";
	##endif##
	##endfor##

	$smarty->assign("includes",$includes);
	$smarty->assign("bodyonload",$bodyonload);
	if(strlen($onsubmit))
		$onsubmit="onSubmit=\"".$onsubmit."\"";
	$smarty->assign("onsubmit",$onsubmit);
##if @TABLE.bInlineEdit##
}
##endif##

##foreach @TABLE.arrKeyFields as @k##
$smarty->assign("key##@index##",htmlspecialchars($keys["##@k s##"]));
$showKeys[] = rawurlencode($keys["##@k s##"]);
	##if Field(@k).strViewFormat==FORMAT_HTML || Field(@k).strViewFormat==FORMAT_FILE_IMAGE || Field(@k).strViewFormat==FORMAT_FILE || Field(@k).strViewFormat==FORMAT_HYPERLINK || Field(@k).strViewFormat==FORMAT_EMAILHYPERLINK || Field(@k).strViewFormat==FORMAT_CHECKBOX##
$smarty->assign("show_key##@index##", htmlspecialchars($data["##@k s##"]));
	##else##
$smarty->assign("show_key##@index##", htmlspecialchars(GetData($data,"##@k s##", "##Field(@k).strViewFormat##")));
	##endif##
##endfor##

$smarty->assign("message",$message);

$readonlyfields=array();

##foreach Fields as @f filter (@f.bEditPage || @f.bInlineEdit)##
##if @f.bAutoUpdate && @f.strDefaultValue.len##
$data["##@f.strName s##"] = ##@f.strDefaultValue##;
##endif##
$smarty->assign("value_##@f.strName g##",@$data["##@f.strName s##"]);
##if @f.strEditFormat==EDIT_FORMAT_READONLY##
  ##if @f.strViewFormat==FORMAT_HTML || @f.strViewFormat==FORMAT_FILE_IMAGE || @f.strViewFormat==FORMAT_FILE || @f.strViewFormat==FORMAT_HYPERLINK || @f.strViewFormat==FORMAT_EMAILHYPERLINK || @f.strViewFormat==FORMAT_CHECKBOX##
	$readonlyfields["##@f.strName s##"] = GetData($data,"##@f.strName s##", "##@f.strViewFormat s##");
  ##else##
	$readonlyfields["##@f.strName s##"] = htmlspecialchars(GetData($data,"##@f.strName s##", "##@f.strViewFormat s##"));
  ##endif##
  ##if @f.bAutoUpdate && @f.strDefaultValue.len##
	$readonlyfields["##@f.strName s##"] = ##@f.strDefaultValue##;
  ##endif##

##endif##
##endfor##


$linkdata="";

if ($useAJAX) 
{
	$record_id= postvalue("recordID");
##foreach @TABLE.arrFieldObj as @f filter (@f.bEditPage || @f.bInlineEdit) && @f.strEditFormat==EDIT_FORMAT_LOOKUP_WIZARD && @f.bUseCategory##
##if !@f.bInlineEdit##
	if(!$inlineedit)
	{
##elseif !@f.bEditPage##
	if($inlineedit)
	{
##endif##
		$output = loadSelectContent("##@f.strName s##",$data["##@f.strCategoryControlField s##"],$data["##@f.strName s##"]);
		$txt = ""; 
		foreach( $output as $value ) {
			$txt .= jsreplace($value)."\\n";
		}
	##if @f.nSelectSize==1##
		$linkdata .= "preloadSelectContent('".$txt."', 'value_##@f.strName g##', '".jsreplace($data["##@f.strName s##"])."','".$record_id."');\r\n";
	##else##
		$arr = splitvalues($data["##@f.strName s##"]);
		$val="";
		foreach($arr as $value)
			$val.= jsreplace($value)."\\n";
		$linkdata .= "preloadMultiSelectContent('".$txt."', 'value_##@f.strName g##', '".$val."','".$record_id."');\r\n";
	##endif##
##if !@f.bInlineEdit || !@f.bEditPage##
	}
##endif##
##endfor##

##if @TABLE.bInlineEdit##
	if ( $inlineedit ) 
	{
		if(@$_REQUEST["browser"]=="ie")
			$smarty->assign("browserie",true);
		$smarty->assign("id",$record_id);

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

	##foreach Fields as @f filter @f.bInlineEdit && UseRTEFCK(@f)##
	##if @first##
	include("plugins/fckeditor/fckeditor.php");
	##endif##
	##endfor##

	} 
	else
	{
##endif##	
		$linkdata = "<script type=\"text/javascript\">\r\n".
		"$(document).ready(function(){ \r\n".
		$linkdata.
		"});</script>";
##if @TABLE.bInlineEdit##
	}
##endif##	
	
} else {
	##foreach Fields as @f filter @f.strEditFormat==EDIT_FORMAT_LOOKUP_WIZARD && @f.bUseCategory && @f.bEditPage##
	//	##@f.strName## - ##@f.strCategoryControlField##
	$linkdata.="<script language=\"Javascript\">";
	$linkdata.="bLoading = true;";
	$linkdata.="SetSelection('value_##@f.strCategoryControlField g##', 'value_##@f.strName g##', '".jsreplace(@$data["##@f.strCategoryControlField s##"])."', '".jsreplace($data["##@f.strName s##"])."', arr_##@f.strName g##);";
	$linkdata.="bLoading = false;";
	$linkdata.="</script>\r\n";
	##endfor##
}

$smarty->assign("linkdata",$linkdata);

##if @TABLE.bInlineEdit##
if ($_REQUEST["a"]=="edited" && $inlineedit ) 
{
	if(!$data)
	{
		$data=$evalues;
		$HaveData=false;
	}
	//Preparation   view values

//	detail tables
##foreach @BUILDER.Tables as @t filter @t.arrMasterTables[strMasterTable==@TABLE.strDataSourceTable].len##
	$masterquery="mastertable=##@TABLE.strDataSourceTable us##";
	##foreach @t.arrMasterTables[strMasterTable==@TABLE.strDataSourceTable].arrMasterKeys as @dk##
	$masterquery.="&masterkey##@index##=".rawurlencode($data["##@dk s##"]);
	##endfor##
	$showDetailKeys["##@t.strShortTableName##"]=$masterquery;
##endfor##

	$keylink="";
	##foreach @TABLE.arrKeyFields as @k##
	$keylink.="&key##@index##=".htmlspecialchars(rawurlencode(@$data["##@k s##"]));
	##endfor##

	##foreach Fields as @f filter @f.bListPage order @f.nListPageOrder##
	////////////////////////////////////////////
	//	##@f.strName## - ##@f.strViewFormat##
		$value="";
		##if @f.strViewFormat==FORMAT_DATABASE_IMAGE##
			##if @f.bShowThumbnail && !@f.bOriginalView##
			$value.="<a";
			##if @f.bUseiBox##
				$value .= " rel='ibox'";
			##else##
				$value .= " target=_blank";
			##endif##
			$value.=" href=\"##@TABLE.strShortTableName s##_imager.php?field=##@f.strName uhs##".$keylink."\">";
			$value.= "<img id=\"img_##@f.strName##_".$record_id."\" border=0";
			$value.=" src=\"##@TABLE.strShortTableName s##_imager.php?field=##@f.strThumbnail uhs##&alt=##@f.strName uhs##".$keylink."\">";
			$value.= "</a>";
			##else##
			$value = "<img";
			##if @f.ListFormatObj.nImageWidth && !@f.bOriginalView##
			$value.=" width=##@f.ListFormatObj.nImageWidth##";
			##endif##
			##if @f.ListFormatObj.nImageHeight && !@f.bOriginalView##
			$value.=" height=##@f.ListFormatObj.nImageHeight##";
			##endif##
			$value.=" id=\"img_##@f.strName##_".$record_id."\" border=0";
			$value.=" src=\"##@TABLE.strShortTableName s##_imager.php?field=##@f.strName uhs##".$keylink."\">";
		##endif##
		##elseif @f.strViewFormat==FORMAT_FILE_IMAGE##
		if(CheckImageExtension($data["##@f.strName s##"])) 
		{
		##if @f.bShowThumbnail && !@f.bOriginalView##
				 	// show thumbnail
			$thumbname="##@f.strThumbnail s##".$data["##@f.strName s##"];
			if(substr("##@f.strhlPrefix s##",0,7)!="http://" && !file_exists(GetUploadFolder("##@f.strName s##").$thumbname))
				$thumbname=$data["##@f.strName s##"];
			$value="<a";
			##if @f.bUseiBox##
				$value .= " rel='ibox'";
			##else##
				$value .= " target=_blank";
			##endif##
			$value.=" href=\"".htmlspecialchars(AddLinkPrefix("##@f.strName s##",$data["##@f.strName s##"]))."\">";
			$value.="<img";
			if($thumbname==$data["##@f.strName s##"])
			{
				##if @f.ListFormatObj.nImageWidth##
				$value.=" width=##@f.ListFormatObj.nImageWidth##";
				##endif##
				##if @f.ListFormatObj.nImageHeight##
				$value.=" height=##@f.ListFormatObj.nImageHeight##";
				##endif##
			}
			$value.=" id=\"img_##@f.strName##_".$record_id."\" border=0";
			$value.=" src=\"".htmlspecialchars(AddLinkPrefix("##@f.strName s##",$thumbname))."\"></a>";
		##else##
			$value="<img";
			##if @f.ListFormatObj.nImageWidth && !@f.bOriginalView##
			$value.=" width=##@f.ListFormatObj.nImageWidth##";
			##endif##
			##if @f.ListFormatObj.nImageHeight && !@f.bOriginalView##
			$value.=" height=##@f.ListFormatObj.nImageHeight##";
			##endif##
			$value.=" id=\"img_##@f.strName##_".$record_id."\" border=0";
			$value.=" src=\"".htmlspecialchars(AddLinkPrefix("##@f.strName s##",$data["##@f.strName s##"]))."\">";
		##endif##
		}
		##elseif @f.strViewFormat==FORMAT_DATABASE_FILE##
			##if @f.ListFormatObj.strFilename##
		$filename=$data["##@f.ListFormatObj.strFilename s##"];
		if(!$filename)
			$filename="file.bin";
			##else##
		$filename="file.bin";
			##endif##
		if(strlen($data["##@f.strName s##"]))
		{
			$value="<a href=\"##@TABLE.strShortTableName##_getfile.php?filename=".rawurlencode($filename)."&field=##@f.strName uhs##".$keylink."\">";
			$value.=htmlspecialchars($filename);
			$value.="</a>";
		}
		##elseif (@f.strEditFormat==EDIT_FORMAT_LOOKUP_WIZARD || @f.strEditFormat==EDIT_FORMAT_RADIO) && pLookupObj.nLookupType == LT_LOOKUPTABLE##
/*
		if(strlen($data["##@f.strName s##"]))
		{
			$strdata = make_db_value("##@f.strName s##",$data["##@f.strName s##"]);
			$LookupSQL="SELECT ";
				##if @f.EditFormatObj.pLookupObj.bCustomDisplay##
			$LookupSQL.="##@f.strDisplayField s##";
				##else## 
			$LookupSQL.="##@f.strDisplayField ws##";
				##endif##
			$LookupSQL.=" FROM ##@f.EditFormatObj.pLookupObj.strTable ts## WHERE ##@f.strLinkField ws## = " . $strdata;
				##if EditFormatObj.pLookupObj.strWhere##
			$LookupSQL.=" and (".##EditFormatObj.pLookupObj.strWhere##.")"; 
				##endif##
			LogInfo($LookupSQL);
			$rsLookup = db_query($LookupSQL,$conn);
			$lookupvalue=$data["##@f.strName s##"];
			if($lookuprow=db_fetch_numarray($rsLookup))
				$lookupvalue=$lookuprow[0];
			##if NeedEncode(@f)##
			$value=ProcessLargeText(GetDataInt($lookupvalue,$data,"##@f.strName s##", "##@f.strViewFormat s##"),"field=##@f.strName uhs##".$keylink,"",MODE_LIST);
			##else##
			$value=GetDataInt($lookupvalue,$data,"##@f.strName s##", "##@f.strViewFormat s##");
			##endif##
		}
		else
			$value="";
*/			
		$value=DisplayLookupWizard("##@f.strName s##",$data["##@f.strName s##"],$data,$keylink,MODE_LIST);
		##elseif NeedEncode(@f)##
		$value = ProcessLargeText(GetData($data,"##@f.strName s##", "##@f.strViewFormat##"),"","",MODE_LIST);
		##else##
		$value = GetData($data,"##@f.strName s##", "##@f.strViewFormat##");
		##endif##
		$smarty->assign("show_##@f.strName g##",$value);
		$showValues[] = $value;
		$showFields[] = "##@f.strName g##";
		##if @f.strtotalsType##
		$showRawValues[] = $data["##@f.strName s##"];
		##else##
		$showRawValues[] = "";
		##endif##
	##endfor##
}
##endif##

##if @TABLE.bInlineEdit##
if ( $_REQUEST["a"]=="edited" && $inlineedit ) 
{
	echo "<textarea id=\"data\">";
	if($IsSaved)
	{
		if($HaveData)
			echo "saved";
		else
			echo "savnd";
		print_inline_array($showKeys);
		echo "\n";
		print_inline_array($showValues);
		echo "\n";
		print_inline_array($showFields);
		echo "\n";
		print_inline_array($showRawValues);
		echo "\n";
		print_inline_array($showDetailKeys,true);
		echo "\n";
		print_inline_array($showDetailKeys);
		echo "\n";
		echo str_replace(array("&","<","\\","\r","\n"),array("&amp;","&lt;","\\\\","\\r","\\n"),$usermessage);
	}
	else
	{
		if($status=="DECLINED")
			echo "decli";
		else
			echo "error";
		echo str_replace(array("&","<","\\","\r","\n"),array("&amp;","&lt;","\\\\","\\r","\\n"),$message);
	}
	echo "</textarea>";
} 
else 
{
##endif##
	if(function_exists("BeforeShowEdit"))
		BeforeShowEdit($smarty,$templatefile);
	$smarty->display($templatefile);
##if @TABLE.bInlineEdit##
}
##endif##

function edit_error_handler($errno, $errstr, $errfile, $errline)
{
	global $readevalues, $message, $status, $inlineedit, $error_happened;
##if @TABLE.bInlineEdit##
	if ( $inlineedit ) 
		$message="".##message RECORD_NOT_UPDATED##.". ".$errstr;
	else  
##endif##
		$message="<div class=message><<< ".##message RECORD_NOT_UPDATED##." >>><br><br>".$errstr."</div>";
	$readevalues=true;
	$error_happened=true;
}

?>