<?php 
include("include/dbcommon.php");

@ini_set("display_errors","1");
@ini_set("display_startup_errors","1");

add_nocache_headers();
include("include/employee_list_variables.php");
include('include/xtempl.php');
include('classes/runnerpage.php');

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

$filename = "";
$status = "";
$message = "";
$mesClass = "";
$usermessage = "";
$error_happened = false;
$readavalues = false;

$keys = array();
$showValues = array();
$showRawValues = array();
$showFields = array();
$showDetailKeys = array();
$IsSaved = false;
$HaveData = true;
$popUpSave = false;

$sessionPrefix = $strTableName;

$onFly = false;
if(postvalue("onFly"))
	$onFly = true;

if(@$_REQUEST["editType"]=="inline")
	$inlineadd = ADD_INLINE;
elseif(@$_REQUEST["editType"]==ADD_POPUP)
{
	$inlineadd = ADD_POPUP;
	if(@$_POST["a"]=="added" && postvalue("field")=="" && postvalue("category")=="")
		$popUpSave = true;	
}
elseif(@$_REQUEST["editType"]=="addmaster")
	$inlineadd = ADD_MASTER;
elseif($onFly)
{
	$inlineadd = ADD_ONTHEFLY;
	$sessionPrefix = $strTableName."_add";
}
else
	$inlineadd = ADD_SIMPLE;

if($inlineadd == ADD_INLINE)
	$templatefile = "employee_list_inline_add.htm";
else
	$templatefile = "employee_list_add.htm";

$id = postvalue("id");	
if(intval($id)==0)
	$id = 1;

//If undefined session value for mastet table, but exist post value master table, than take second
//It may be happen only when use dpInline mode on page add
if(!@$_SESSION[$sessionPrefix."_mastertable"] && postvalue("mastertable"))
	$_SESSION[$sessionPrefix."_mastertable"] = postvalue("mastertable");

$xt = new Xtempl();
	
// assign an id		
$xt->assign("id",$id);
	
$auditObj = GetAuditObject($strTableName);

//array of params for classes
$params = array("pageType" => PAGE_ADD,"id" => $id,"mode" => $inlineadd);

////////////////////// data picker
$params["calendar"] = true;

////////////////////// time picker

$params['tName'] = $strTableName;
$params['strOriginalTableName'] = $strOriginalTableName;
$params['menuTablesArr'] = $menuTablesArr;
$params['xt'] = &$xt;
$params['needSearchClauseObj'] = false;
$params['includes_js']=$includes_js;
$params['includes_jsreq']=$includes_jsreq;
$params['includes_css']=$includes_css;
$params['locale_info']=$locale_info;
$params['pageAddLikeInline'] = ($inlineadd==ADD_INLINE);
$params['useTabsOnAdd'] = useTabsOnAdd($strTableName);
if($params['useTabsOnAdd'])
	$params['arrAddTabs'] = GetAddTabs($strTableName);
$pageObject = new RunnerPage($params);

//Get detail table keys	
$detailKeys = $pageObject->detailKeysByM;

//Array of fields, which appear on add page
$addFields = $pageObject->getFieldsByPageType();

// add onload event
//$onLoadJsCode = GetTableData($pageObject->tName, ".jsOnloadAdd", '');
//$pageObject->addOnLoadJsEvent($onLoadJsCode);

if ($inlineadd==ADD_SIMPLE)
{
	// add button events if exist
	$pageObject->addButtonHandlers();
}

$url_page=substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1,12);

//For show detail tables on master page add
if($inlineadd==ADD_SIMPLE || $inlineadd==ADD_MASTER || $inlineadd==ADD_POPUP)
{
	$dpParams = array();
	if($pageObject->isShowDetailTables)
	{
		$ids = $id;
		$pageObject->jsSettings['tableSettings'][$strTableName]['dpParams'] = array('tableNames'=>$dpParams['strTableNames'], 'ids'=>$dpParams['ids']);
		if($inlineadd==ADD_SIMPLE)
			$pageObject->AddJSFile("include/detailspreview");	
	}
}

//	Before Process event
if($eventObj->exists("BeforeProcessAdd"))
	$eventObj->BeforeProcessAdd($conn);

// proccess captcha
if ($inlineadd==ADD_SIMPLE || $inlineadd==ADD_MASTER || $inlineadd==ADD_POPUP)
	if($pageObject->captchaExists())
		$pageObject->doCaptchaCode();	
	
// insert new record if we have to
if(@$_POST["a"]=="added")
{
	$afilename_values=array();
	$avalues=array();
	$blobfields=array();
//	processing Full Name - start
	$inlineAddOption = true;
	$inlineAddOption = $inlineadd!=ADD_INLINE;
	if($inlineAddOption)
	{
		$value = postvalue("value_Full_Name_".$id);
		$type=postvalue("type_Full_Name_".$id);
		if (FieldSubmitted("Full Name_".$id))
		{
				$value=prepare_for_db("Full Name",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "Full Name"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["Full Name"]=$value;
		}
		}
//	processibng Full Name - end
//	processing Father / Husband Name - start
	$inlineAddOption = true;
	$inlineAddOption = $inlineadd!=ADD_INLINE;
	if($inlineAddOption)
	{
		$value = postvalue("value_Father___Husband_Name_".$id);
		$type=postvalue("type_Father___Husband_Name_".$id);
		if (FieldSubmitted("Father / Husband Name_".$id))
		{
				$value=prepare_for_db("Father / Husband Name",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "Father / Husband Name"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["Father / Husband Name"]=$value;
		}
		}
//	processibng Father / Husband Name - end
//	processing Job Title - start
	$inlineAddOption = true;
	$inlineAddOption = $inlineadd!=ADD_INLINE;
	if($inlineAddOption)
	{
		$value = postvalue("value_Job_Title_".$id);
		$type=postvalue("type_Job_Title_".$id);
		if (FieldSubmitted("Job Title_".$id))
		{
				$value=prepare_for_db("Job Title",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "Job Title"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["Job Title"]=$value;
		}
		}
//	processibng Job Title - end
//	processing Address - start
	$inlineAddOption = true;
	$inlineAddOption = $inlineadd!=ADD_INLINE;
	if($inlineAddOption)
	{
		$value = postvalue("value_Address_".$id);
		$type=postvalue("type_Address_".$id);
		if (FieldSubmitted("Address_".$id))
		{
				$value=prepare_for_db("Address",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "Address"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["Address"]=$value;
		}
		}
//	processibng Address - end
//	processing City - start
	$inlineAddOption = true;
	$inlineAddOption = $inlineadd!=ADD_INLINE;
	if($inlineAddOption)
	{
		$value = postvalue("value_City_".$id);
		$type=postvalue("type_City_".$id);
		if (FieldSubmitted("City_".$id))
		{
				$value=prepare_for_db("City",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "City"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["City"]=$value;
		}
		}
//	processibng City - end
//	processing State - start
	$inlineAddOption = true;
	$inlineAddOption = $inlineadd!=ADD_INLINE;
	if($inlineAddOption)
	{
		$value = postvalue("value_State_".$id);
		$type=postvalue("type_State_".$id);
		if (FieldSubmitted("State_".$id))
		{
				$value=prepare_for_db("State",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "State"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["State"]=$value;
		}
		}
//	processibng State - end
//	processing Country - start
	$inlineAddOption = true;
	$inlineAddOption = $inlineadd!=ADD_INLINE;
	if($inlineAddOption)
	{
		$value = postvalue("value_Country_".$id);
		$type=postvalue("type_Country_".$id);
		if (FieldSubmitted("Country_".$id))
		{
				$value=prepare_for_db("Country",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "Country"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["Country"]=$value;
		}
		}
//	processibng Country - end
//	processing Postal Code - start
	$inlineAddOption = true;
	$inlineAddOption = $inlineadd!=ADD_INLINE;
	if($inlineAddOption)
	{
		$value = postvalue("value_Postal_Code_".$id);
		$type=postvalue("type_Postal_Code_".$id);
		if (FieldSubmitted("Postal Code_".$id))
		{
				$value=prepare_for_db("Postal Code",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "Postal Code"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["Postal Code"]=$value;
		}
		}
//	processibng Postal Code - end
//	processing Business Phone - start
	$inlineAddOption = true;
	$inlineAddOption = $inlineadd!=ADD_INLINE;
	if($inlineAddOption)
	{
		$value = postvalue("value_Business_Phone_".$id);
		$type=postvalue("type_Business_Phone_".$id);
		if (FieldSubmitted("Business Phone_".$id))
		{
				$value=prepare_for_db("Business Phone",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "Business Phone"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["Business Phone"]=$value;
		}
		}
//	processibng Business Phone - end
//	processing Home Phone - start
	$inlineAddOption = true;
	$inlineAddOption = $inlineadd!=ADD_INLINE;
	if($inlineAddOption)
	{
		$value = postvalue("value_Home_Phone_".$id);
		$type=postvalue("type_Home_Phone_".$id);
		if (FieldSubmitted("Home Phone_".$id))
		{
				$value=prepare_for_db("Home Phone",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "Home Phone"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["Home Phone"]=$value;
		}
		}
//	processibng Home Phone - end
//	processing Mobile Phone - start
	$inlineAddOption = true;
	$inlineAddOption = $inlineadd!=ADD_INLINE;
	if($inlineAddOption)
	{
		$value = postvalue("value_Mobile_Phone_".$id);
		$type=postvalue("type_Mobile_Phone_".$id);
		if (FieldSubmitted("Mobile Phone_".$id))
		{
				$value=prepare_for_db("Mobile Phone",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "Mobile Phone"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["Mobile Phone"]=$value;
		}
		}
//	processibng Mobile Phone - end
//	processing E-Mail Address - start
	$inlineAddOption = true;
	$inlineAddOption = $inlineadd!=ADD_INLINE;
	if($inlineAddOption)
	{
		$value = postvalue("value_E_Mail_Address_".$id);
		$type=postvalue("type_E_Mail_Address_".$id);
		if (FieldSubmitted("E-Mail Address_".$id))
		{
				$value=prepare_for_db("E-Mail Address",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "E-Mail Address"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["E-Mail Address"]=$value;
		}
		}
//	processibng E-Mail Address - end
//	processing Notes - start
	$inlineAddOption = true;
	$inlineAddOption = $inlineadd!=ADD_INLINE;
	if($inlineAddOption)
	{
		$value = postvalue("value_Notes_".$id);
		$type=postvalue("type_Notes_".$id);
		if (FieldSubmitted("Notes_".$id))
		{
				$value=prepare_for_db("Notes",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "Notes"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["Notes"]=$value;
		}
		}
//	processibng Notes - end
//	processing Department - start
	$inlineAddOption = true;
	$inlineAddOption = $inlineadd!=ADD_INLINE;
	if($inlineAddOption)
	{
		$value = postvalue("value_Department_".$id);
		$type=postvalue("type_Department_".$id);
		if (FieldSubmitted("Department_".$id))
		{
				$value=prepare_for_db("Department",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "Department"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["Department"]=$value;
		}
		}
//	processibng Department - end
//	processing Region - start
	$inlineAddOption = true;
	$inlineAddOption = $inlineadd!=ADD_INLINE;
	if($inlineAddOption)
	{
		$value = postvalue("value_Region_".$id);
		$type=postvalue("type_Region_".$id);
		if (FieldSubmitted("Region_".$id))
		{
				$value=prepare_for_db("Region",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "Region"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["Region"]=$value;
		}
		}
//	processibng Region - end
//	processing Birth Date - start
	$inlineAddOption = true;
	$inlineAddOption = $inlineadd!=ADD_INLINE;
	if($inlineAddOption)
	{
		$value = postvalue("value_Birth_Date_".$id);
		$type=postvalue("type_Birth_Date_".$id);
		if (FieldSubmitted("Birth Date_".$id))
		{
				$value=prepare_for_db("Birth Date",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "Birth Date"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["Birth Date"]=$value;
		}
		}
//	processibng Birth Date - end
//	processing DOJ - start
	$inlineAddOption = true;
	$inlineAddOption = $inlineadd!=ADD_INLINE;
	if($inlineAddOption)
	{
		$value = postvalue("value_DOJ_".$id);
		$type=postvalue("type_DOJ_".$id);
		if (FieldSubmitted("DOJ_".$id))
		{
				$value=prepare_for_db("DOJ",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "DOJ"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["DOJ"]=$value;
		}
		}
//	processibng DOJ - end
//	processing username - start
	$inlineAddOption = true;
	$inlineAddOption = $inlineadd!=ADD_INLINE;
	if($inlineAddOption)
	{
		$value = postvalue("value_username_".$id);
		$type=postvalue("type_username_".$id);
		if (FieldSubmitted("username_".$id))
		{
				$value=prepare_for_db("username",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "username"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["username"]=$value;
		}
		}
//	processibng username - end
//	processing Userlevel - start
	$inlineAddOption = true;
	$inlineAddOption = $inlineadd!=ADD_INLINE;
	if($inlineAddOption)
	{
		$value = postvalue("value_Userlevel_".$id);
		$type=postvalue("type_Userlevel_".$id);
		if (FieldSubmitted("Userlevel_".$id))
		{
				$value=prepare_for_db("Userlevel",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "Userlevel"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["Userlevel"]=$value;
		}
		}
//	processibng Userlevel - end
//	processing Reporting Manager - start
	$inlineAddOption = true;
	$inlineAddOption = $inlineadd!=ADD_INLINE;
	if($inlineAddOption)
	{
		$value = postvalue("value_Reporting_Manager_".$id);
		$type=postvalue("type_Reporting_Manager_".$id);
		if (FieldSubmitted("Reporting Manager_".$id))
		{
				$value=prepare_for_db("Reporting Manager",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "Reporting Manager"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["Reporting Manager"]=$value;
		}
		}
//	processibng Reporting Manager - end




	$failed_inline_add=false;
//	add filenames to values
	foreach($afilename_values as $akey=>$value)
		$avalues[$akey]=$value;
	
//	before Add event
	$retval = true;
	if($eventObj->exists("BeforeAdd"))
		$retval=$eventObj->BeforeAdd($avalues,$usermessage,(bool)$inlineadd);
	if($retval && $pageObject->isCaptchaOk)
	{
		$_SESSION[$strTableName."_count_captcha"] = $_SESSION[$strTableName."_count_captcha"]+1;
		if(DoInsertRecord($strOriginalTableName,$avalues,$blobfields,$id,$pageObject))
		{
			$IsSaved=true;
//	after edit event
			if($auditObj || $eventObj->exists("AfterAdd"))
			{
				foreach($keys as $idx=>$val)
					$avalues[$idx]=$val;
			}
			
			if($auditObj)
				$auditObj->LogAdd($strTableName,$avalues,$keys);

			if($eventObj->exists("AfterAdd"))
				$eventObj->AfterAdd($avalues,$keys,(bool)$inlineadd);
				
			if($inlineadd==ADD_SIMPLE || $inlineadd==ADD_MASTER)
			{
				$permis = array();
				$keylink = "";$k = 0;
				foreach($keys as $idx=>$val)
				{
					if($k!=0)
						$keylink .="&";
					$keylink .="editid".(++$k)."=".htmlspecialchars(rawurlencode(@$val));
				}
				$permis = $pageObject->getPermissions();				
				if (count($keys))
				{
					$message .="</br>";
					if(GetTableData($strTableName,".edit",false) && $permis['edit'])
						$message .='&nbsp;<a href=\'employee_list_edit.php?'.$keylink.'\'>'."Edit".'</a>&nbsp;';
					if(GetTableData($strTableName,".view",false) && $permis['search'])
						$message .='&nbsp;<a href=\'employee_list_view.php?'.$keylink.'\'>'."View".'</a>&nbsp;';
				}
				$mesClass = "mes_ok";	
			}
		}
		elseif($inlineadd!=ADD_INLINE)
			$mesClass = "mes_not";	
	}
	else
	{
		$message = $usermessage;
		$status="DECLINED";
		$readavalues=true;
	}
}

$message = "<div class='message ".$mesClass."'>".$message."</div>";

// PRG rule, to avoid POSTDATA resend
if (no_output_done() && $inlineadd==ADD_SIMPLE && $IsSaved)
{
	// saving message
	$_SESSION["message"] = ($message ? $message : "");
	// redirect
	header("Location: employee_list_".$pageObject->getPageType().".php");
	// turned on output buffering, so we need to stop script
	exit();
}

if($inlineadd==ADD_MASTER && $IsSaved)
	$_SESSION["message"] = ($message ? $message : "");
	
// for PRG rule, to avoid POSTDATA resend. Saving mess in session
if($inlineadd==ADD_SIMPLE && isset($_SESSION["message"]))
{
	$message = $_SESSION["message"];
	unset($_SESSION["message"]);
}

$defvalues=array();

//	copy record
if(array_key_exists("copyid1",$_REQUEST) || array_key_exists("editid1",$_REQUEST))
{
	$copykeys=array();
	if(array_key_exists("copyid1",$_REQUEST))
	{
		$copykeys["idemplist"]=postvalue("copyid1");
	}
	else
	{
		$copykeys["idemplist"]=postvalue("editid1");
	}
	$strWhere=KeyWhere($copykeys);
	$strSQL = gSQLWhere($strWhere);

	LogInfo($strSQL);
	$rs=db_query($strSQL,$conn);
	$defvalues=db_fetch_array($rs);
	if(!$defvalues)
		$defvalues=array();
//	clear key fields
	$defvalues["idemplist"]="";
//call CopyOnLoad event
	if($eventObj->exists("CopyOnLoad"))
		$eventObj->CopyOnLoad($defvalues,$strWhere);
}
else
{
	$defvalues["Country"]="India";
}


if($readavalues)
{
	$defvalues["Full Name"]=@$avalues["Full Name"];
	$defvalues["Father / Husband Name"]=@$avalues["Father / Husband Name"];
	$defvalues["Job Title"]=@$avalues["Job Title"];
	$defvalues["Address"]=@$avalues["Address"];
	$defvalues["City"]=@$avalues["City"];
	$defvalues["State"]=@$avalues["State"];
	$defvalues["Country"]=@$avalues["Country"];
	$defvalues["Postal Code"]=@$avalues["Postal Code"];
	$defvalues["Business Phone"]=@$avalues["Business Phone"];
	$defvalues["Home Phone"]=@$avalues["Home Phone"];
	$defvalues["Mobile Phone"]=@$avalues["Mobile Phone"];
	$defvalues["E-Mail Address"]=@$avalues["E-Mail Address"];
	$defvalues["Notes"]=@$avalues["Notes"];
	$defvalues["Department"]=@$avalues["Department"];
	$defvalues["Region"]=@$avalues["Region"];
	$defvalues["Birth Date"]=@$avalues["Birth Date"];
	$defvalues["DOJ"]=@$avalues["DOJ"];
	$defvalues["username"]=@$avalues["username"];
	$defvalues["Userlevel"]=@$avalues["Userlevel"];
	$defvalues["Reporting Manager"]=@$avalues["Reporting Manager"];
}

if($eventObj->exists("ProcessValuesAdd"))
	$eventObj->ProcessValuesAdd($defvalues);


//for basic files
$includes="";

if($inlineadd!=ADD_INLINE)
{
	if($inlineadd!=ADD_ONTHEFLY && $inlineadd!=ADD_POPUP)
	{
		$includes .="<script language=\"JavaScript\" src=\"include/jquery.js\"></script>\r\n";
		if ($pageObject->debugJSMode===true)
		{
			/*$includes.="<script type=\"text/javascript\" src=\"include/runnerJS/Runner.js\"></script>\r\n".
				"<script type=\"text/javascript\" src=\"include/runnerJS/Util.js\"></script>\r\n".
				"<script type=\"text/javascript\" src=\"include/runnerJS/Observer.js\"></script>\r\n";*/
			$includes.="<script language=\"JavaScript\" src=\"include/runnerJS/RunnerBase.js\"></script>\r\n";
		}
		else 
			$includes .= "<script type=\"text/javascript\" src=\"include/runnerJS/RunnerBase.js\"></script>";
		
		$includes.="<script language=\"JavaScript\" src=\"include/jsfunctions.js\"></script>\r\n";
		$includes.="<div id=\"search_suggest\"></div>\r\n";
	}
	
	if(!$pageObject->isAppearOnTabs("Full Name"))
		$xt->assign("Full_Name_fieldblock",true);
	else
		$xt->assign("Full_Name_tabfieldblock",true);
	$xt->assign("Full_Name_label",true);
	if(isEnableSection508())
		$xt->assign_section("Full_Name_label","<label for=\"".GetInputElementId("Full Name", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("Father / Husband Name"))
		$xt->assign("Father___Husband_Name_fieldblock",true);
	else
		$xt->assign("Father___Husband_Name_tabfieldblock",true);
	$xt->assign("Father___Husband_Name_label",true);
	if(isEnableSection508())
		$xt->assign_section("Father___Husband_Name_label","<label for=\"".GetInputElementId("Father / Husband Name", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("Job Title"))
		$xt->assign("Job_Title_fieldblock",true);
	else
		$xt->assign("Job_Title_tabfieldblock",true);
	$xt->assign("Job_Title_label",true);
	if(isEnableSection508())
		$xt->assign_section("Job_Title_label","<label for=\"".GetInputElementId("Job Title", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("Address"))
		$xt->assign("Address_fieldblock",true);
	else
		$xt->assign("Address_tabfieldblock",true);
	$xt->assign("Address_label",true);
	if(isEnableSection508())
		$xt->assign_section("Address_label","<label for=\"".GetInputElementId("Address", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("City"))
		$xt->assign("City_fieldblock",true);
	else
		$xt->assign("City_tabfieldblock",true);
	$xt->assign("City_label",true);
	if(isEnableSection508())
		$xt->assign_section("City_label","<label for=\"".GetInputElementId("City", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("State"))
		$xt->assign("State_fieldblock",true);
	else
		$xt->assign("State_tabfieldblock",true);
	$xt->assign("State_label",true);
	if(isEnableSection508())
		$xt->assign_section("State_label","<label for=\"".GetInputElementId("State", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("Country"))
		$xt->assign("Country_fieldblock",true);
	else
		$xt->assign("Country_tabfieldblock",true);
	$xt->assign("Country_label",true);
	if(isEnableSection508())
		$xt->assign_section("Country_label","<label for=\"".GetInputElementId("Country", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("Postal Code"))
		$xt->assign("Postal_Code_fieldblock",true);
	else
		$xt->assign("Postal_Code_tabfieldblock",true);
	$xt->assign("Postal_Code_label",true);
	if(isEnableSection508())
		$xt->assign_section("Postal_Code_label","<label for=\"".GetInputElementId("Postal Code", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("Business Phone"))
		$xt->assign("Business_Phone_fieldblock",true);
	else
		$xt->assign("Business_Phone_tabfieldblock",true);
	$xt->assign("Business_Phone_label",true);
	if(isEnableSection508())
		$xt->assign_section("Business_Phone_label","<label for=\"".GetInputElementId("Business Phone", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("Home Phone"))
		$xt->assign("Home_Phone_fieldblock",true);
	else
		$xt->assign("Home_Phone_tabfieldblock",true);
	$xt->assign("Home_Phone_label",true);
	if(isEnableSection508())
		$xt->assign_section("Home_Phone_label","<label for=\"".GetInputElementId("Home Phone", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("Mobile Phone"))
		$xt->assign("Mobile_Phone_fieldblock",true);
	else
		$xt->assign("Mobile_Phone_tabfieldblock",true);
	$xt->assign("Mobile_Phone_label",true);
	if(isEnableSection508())
		$xt->assign_section("Mobile_Phone_label","<label for=\"".GetInputElementId("Mobile Phone", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("E-Mail Address"))
		$xt->assign("E_Mail_Address_fieldblock",true);
	else
		$xt->assign("E_Mail_Address_tabfieldblock",true);
	$xt->assign("E_Mail_Address_label",true);
	if(isEnableSection508())
		$xt->assign_section("E_Mail_Address_label","<label for=\"".GetInputElementId("E-Mail Address", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("Notes"))
		$xt->assign("Notes_fieldblock",true);
	else
		$xt->assign("Notes_tabfieldblock",true);
	$xt->assign("Notes_label",true);
	if(isEnableSection508())
		$xt->assign_section("Notes_label","<label for=\"".GetInputElementId("Notes", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("Department"))
		$xt->assign("Department_fieldblock",true);
	else
		$xt->assign("Department_tabfieldblock",true);
	$xt->assign("Department_label",true);
	if(isEnableSection508())
		$xt->assign_section("Department_label","<label for=\"".GetInputElementId("Department", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("Region"))
		$xt->assign("Region_fieldblock",true);
	else
		$xt->assign("Region_tabfieldblock",true);
	$xt->assign("Region_label",true);
	if(isEnableSection508())
		$xt->assign_section("Region_label","<label for=\"".GetInputElementId("Region", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("Birth Date"))
		$xt->assign("Birth_Date_fieldblock",true);
	else
		$xt->assign("Birth_Date_tabfieldblock",true);
	$xt->assign("Birth_Date_label",true);
	if(isEnableSection508())
		$xt->assign_section("Birth_Date_label","<label for=\"".GetInputElementId("Birth Date", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("DOJ"))
		$xt->assign("DOJ_fieldblock",true);
	else
		$xt->assign("DOJ_tabfieldblock",true);
	$xt->assign("DOJ_label",true);
	if(isEnableSection508())
		$xt->assign_section("DOJ_label","<label for=\"".GetInputElementId("DOJ", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("username"))
		$xt->assign("username_fieldblock",true);
	else
		$xt->assign("username_tabfieldblock",true);
	$xt->assign("username_label",true);
	if(isEnableSection508())
		$xt->assign_section("username_label","<label for=\"".GetInputElementId("username", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("Userlevel"))
		$xt->assign("Userlevel_fieldblock",true);
	else
		$xt->assign("Userlevel_tabfieldblock",true);
	$xt->assign("Userlevel_label",true);
	if(isEnableSection508())
		$xt->assign_section("Userlevel_label","<label for=\"".GetInputElementId("Userlevel", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("Reporting Manager"))
		$xt->assign("Reporting_Manager_fieldblock",true);
	else
		$xt->assign("Reporting_Manager_tabfieldblock",true);
	$xt->assign("Reporting_Manager_label",true);
	if(isEnableSection508())
		$xt->assign_section("Reporting_Manager_label","<label for=\"".GetInputElementId("Reporting Manager", $id)."\">","</label>");
	
	
	
	if($inlineadd!=ADD_ONTHEFLY && $inlineadd!=ADD_POPUP)
	{
		$pageObject->body["begin"] .= $includes;
		if($pageObject->isShowDetailTables)
			$pageObject->body["begin"].= "<div id=\"master_details\" onmouseover=\"RollDetailsLink.showPopup();\" onmouseout=\"RollDetailsLink.hidePopup();\"> </div>";
		$xt->assign("backbutton_attrs","id=\"backButton".$id."\"");
		$xt->assign("back_button",true);
		//$xt->assign('addForm', true);
	}
	else
	{		
		$xt->assign("cancelbutton_attrs", "id=\"cancelButton".$id."\"");
		$xt->assign("cancel_button",true);
		$xt->assign("header","");
	}
	$xt->assign("save_button",true);
}
$xt->assign("savebutton_attrs","id=\"saveButton".$id."\"");
if($message)
{
	$xt->assign("message_block",true);
	$xt->assign("message",$message);
}
/*
if($inlineadd == ADD_ONTHEFLY || $inlineadd == ADD_POPUP)
{
	$xt->assign("message_block",true);
}
*/

$readonlyfields=array();

//	show readonly fields
$linkdata="";

if(@$_POST["a"]=="added" && $inlineadd==ADD_ONTHEFLY)
{
	if( !$error_happened && $status!="DECLINED")
	{
		$LookupSQL = "";
		$linkfield = "";
		$dispfield = "";
		if($LookupSQL)
			$LookupSQL.=" from ".AddTableWrappers($strOriginalTableName);

		$data=0;
		if(count($keys) && $LookupSQL)
		{
			$where=KeyWhere($keys);
			$LookupSQL.=" where ".$where;
			$rs=db_query($LookupSQL,$conn);
			$data=db_fetch_numarray($rs);
		}
		if($data)
		{
			$respData = array($linkfield=>@$data[0], $dispfield=>@$data[1]);
		}
		else
		{
			$respData = array($linkfield=>@$avalues[$linkfield], $dispfield=>@$avalues[$dispfield]);
		}		
		$returnJSON['success'] = true;
		$returnJSON['keys'] = $keys;
		$returnJSON['vals'] = $respData;
		$returnJSON['fields'] = $showFields;
	}
	else
	{
		$returnJSON['success'] = false;
		$returnJSON['message'] = $message;
	}
	echo "<textarea>".htmlspecialchars(my_json_encode($returnJSON))."</textarea>";
	exit();
}

if(@$_POST["a"]=="added" && ($inlineadd == ADD_INLINE || $inlineadd == ADD_MASTER || $inlineadd==ADD_POPUP)) 
{
	//Preparation   view values
	//	get current values and show edit controls
	$dispFieldAlias = "";
	$data=0;
	if(count($keys))
	{
		$where=KeyWhere($keys);
			
		$sqlHead = $gQuery->HeadToSql();
		$sqlGroupBy = $gQuery->GroupByToSql();
		$oHaving = $gQuery->Having();
		$sqlHaving = $oHaving->toSql($gQuery);
		
		$dispFieldAlias = postvalue('dispFieldAlias');
		$dispField = postvalue('dispField');
		
		if ($dispFieldAlias)
		{
			$sqlHead.=", ".($dispField)." as ".AddFieldWrappers($dispFieldAlias)." ";
		}
		$strSQL = gSQLWhere_having($sqlHead, $gsqlFrom, $gsqlWhereExpr, $sqlGroupBy, $sqlHaving, $where, '');		
		
		LogInfo($strSQL);
		$rs=db_query($strSQL,$conn);
		$data=db_fetch_array($rs);
	}
	if(!$data)
	{
		$data=$avalues;
		$HaveData=false;
	}
	//check if correct values added

	$keylink="";
	$keylink.="&key1=".htmlspecialchars(rawurlencode(@$data["idemplist"]));
	
////////////////////////////////////////////
//	idemplist - 
	$display = false;
	if($inlineadd==ADD_INLINE || $inlineadd==ADD_ONTHEFLY || $inlineadd==ADD_POPUP)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"idemplist", ""),"field=idemplist".$keylink,"",MODE_LIST);
	$showValues["idemplist"] = $value;
	$showFields[] = "idemplist";
		$showRawValues["idemplist"] = substr($data["idemplist"],0,100);
	}	
//	Full Name - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($inlineadd==ADD_INLINE || $inlineadd==ADD_ONTHEFLY || $inlineadd==ADD_POPUP)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"Full Name", ""),"field=Full+Name".$keylink,"",MODE_LIST);
	$showValues["Full Name"] = $value;
	$showFields[] = "Full Name";
		$showRawValues["Full Name"] = substr($data["Full Name"],0,100);
	}	
//	Father / Husband Name - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($inlineadd==ADD_INLINE || $inlineadd==ADD_ONTHEFLY || $inlineadd==ADD_POPUP)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"Father / Husband Name", ""),"field=Father+%2F+Husband+Name".$keylink,"",MODE_LIST);
	$showValues["Father / Husband Name"] = $value;
	$showFields[] = "Father / Husband Name";
		$showRawValues["Father / Husband Name"] = substr($data["Father / Husband Name"],0,100);
	}	
//	Job Title - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($inlineadd==ADD_INLINE || $inlineadd==ADD_ONTHEFLY || $inlineadd==ADD_POPUP)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"Job Title", ""),"field=Job+Title".$keylink,"",MODE_LIST);
	$showValues["Job Title"] = $value;
	$showFields[] = "Job Title";
		$showRawValues["Job Title"] = substr($data["Job Title"],0,100);
	}	
//	Address - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"Address", ""),"field=Address".$keylink,"",MODE_LIST);
	$showValues["Address"] = $value;
	$showFields[] = "Address";
		$showRawValues["Address"] = substr($data["Address"],0,100);
	}	
//	City - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"City", ""),"field=City".$keylink,"",MODE_LIST);
	$showValues["City"] = $value;
	$showFields[] = "City";
		$showRawValues["City"] = substr($data["City"],0,100);
	}	
//	State - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"State", ""),"field=State".$keylink,"",MODE_LIST);
	$showValues["State"] = $value;
	$showFields[] = "State";
		$showRawValues["State"] = substr($data["State"],0,100);
	}	
//	Country - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"Country", ""),"field=Country".$keylink,"",MODE_LIST);
	$showValues["Country"] = $value;
	$showFields[] = "Country";
		$showRawValues["Country"] = substr($data["Country"],0,100);
	}	
//	Postal Code - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"Postal Code", ""),"field=Postal+Code".$keylink,"",MODE_LIST);
	$showValues["Postal Code"] = $value;
	$showFields[] = "Postal Code";
		$showRawValues["Postal Code"] = substr($data["Postal Code"],0,100);
	}	
//	Business Phone - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"Business Phone", ""),"field=Business+Phone".$keylink,"",MODE_LIST);
	$showValues["Business Phone"] = $value;
	$showFields[] = "Business Phone";
		$showRawValues["Business Phone"] = substr($data["Business Phone"],0,100);
	}	
//	Home Phone - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"Home Phone", ""),"field=Home+Phone".$keylink,"",MODE_LIST);
	$showValues["Home Phone"] = $value;
	$showFields[] = "Home Phone";
		$showRawValues["Home Phone"] = substr($data["Home Phone"],0,100);
	}	
//	Mobile Phone - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($inlineadd==ADD_INLINE || $inlineadd==ADD_ONTHEFLY || $inlineadd==ADD_POPUP)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"Mobile Phone", ""),"field=Mobile+Phone".$keylink,"",MODE_LIST);
	$showValues["Mobile Phone"] = $value;
	$showFields[] = "Mobile Phone";
		$showRawValues["Mobile Phone"] = substr($data["Mobile Phone"],0,100);
	}	
//	E-Mail Address - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($inlineadd==ADD_INLINE || $inlineadd==ADD_ONTHEFLY || $inlineadd==ADD_POPUP)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"E-Mail Address", ""),"field=E%2DMail+Address".$keylink,"",MODE_LIST);
	$showValues["E-Mail Address"] = $value;
	$showFields[] = "E-Mail Address";
		$showRawValues["E-Mail Address"] = substr($data["E-Mail Address"],0,100);
	}	
//	Notes - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"Notes", ""),"field=Notes".$keylink,"",MODE_LIST);
	$showValues["Notes"] = $value;
	$showFields[] = "Notes";
		$showRawValues["Notes"] = substr($data["Notes"],0,100);
	}	
//	Department - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"Department", ""),"field=Department".$keylink,"",MODE_LIST);
	$showValues["Department"] = $value;
	$showFields[] = "Department";
		$showRawValues["Department"] = substr($data["Department"],0,100);
	}	
//	Region - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"Region", ""),"field=Region".$keylink,"",MODE_LIST);
	$showValues["Region"] = $value;
	$showFields[] = "Region";
		$showRawValues["Region"] = substr($data["Region"],0,100);
	}	
//	Birth Date - Short Date
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"Birth Date", "Short Date"),"field=Birth+Date".$keylink,"",MODE_LIST);
	$showValues["Birth Date"] = $value;
	$showFields[] = "Birth Date";
		$showRawValues["Birth Date"] = substr($data["Birth Date"],0,100);
	}	
//	DOJ - Short Date
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"DOJ", "Short Date"),"field=DOJ".$keylink,"",MODE_LIST);
	$showValues["DOJ"] = $value;
	$showFields[] = "DOJ";
		$showRawValues["DOJ"] = substr($data["DOJ"],0,100);
	}	
//	username - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"username", ""),"field=username".$keylink,"",MODE_LIST);
	$showValues["username"] = $value;
	$showFields[] = "username";
		$showRawValues["username"] = substr($data["username"],0,100);
	}	
//	Userlevel - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"Userlevel", ""),"field=Userlevel".$keylink,"",MODE_LIST);
	$showValues["Userlevel"] = $value;
	$showFields[] = "Userlevel";
		$showRawValues["Userlevel"] = substr($data["Userlevel"],0,100);
	}	
//	Reporting Manager - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($display)
	{	
		$value="";
				$value = DisplayLookupWizard("Reporting Manager",$data["Reporting Manager"],$data,$keylink,MODE_LIST);
	$showValues["Reporting Manager"] = $value;
	$showFields[] = "Reporting Manager";
		$showRawValues["Reporting Manager"] = substr($data["Reporting Manager"],0,100);
	}	
	
	// for custom expression for display field
	if ($dispFieldAlias)
	{
		$showValues[] = $data[$dispFieldAlias];	
		$showFields[] = $dispFieldAlias;
		$showRawValues[] = substr($data[$dispFieldAlias],0,100);
	}		
	
	if($inlineadd==ADD_INLINE || $inlineadd==ADD_POPUP)
	{	
		if($IsSaved && count($showValues))
		{		
			$returnJSON['success'] = true;	
			if($HaveData){
				$returnJSON['noKeys'] = false;
			}else{
				$returnJSON['noKeys'] = true;
			}
				
			$returnJSON['keys'] = $keys;
			$returnJSON['vals'] = $showValues;
			$returnJSON['fields'] = $showFields;
			$returnJSON['rawVals'] = $showRawValues;
			$returnJSON['detKeys'] = $showDetailKeys;
			$returnJSON['userMess'] = $usermessage;
		}
		else
		{
			$returnJSON['success'] = false;
			$returnJSON['message'] = $message;
		}
		echo "<textarea>".htmlspecialchars(my_json_encode($returnJSON))."</textarea>";
		exit();
	}	
} 

/////////////////////////////////////////////////////////////
if($inlineadd==ADD_MASTER)
{		
	$respJSON = array();
	if(($_POST["a"]=="added" && $IsSaved))
	{
		$respJSON['success'] = true;
		$respJSON['fields'] = $showFields;
		$respJSON['vals'] = $showValues;
		if($onFly){
			if($HaveData)
				$returnJSON['noKeys'] = false;
			else
				$returnJSON['noKeys'] = true;
			$respJSON['keys'] = $keys;
			$respJSON['rawVals'] = $showRawValues;
			$respJSON['detKeys'] = $showDetailKeys;
			$respJSON['userMess'] = $usermessage;
		}
		$respJSON['mKeys'] = array();	
		for($i=0;$i<count($dpParams['ids']);$i++)
		{
			$data=0;
			if(count($keys))
			{
				$where=KeyWhere($keys);
							$strSQL = gSQLWhere($where);
				LogInfo($strSQL);
				$rs=db_query($strSQL,$conn);
				$data=db_fetch_array($rs);
			}
			if(!$data)
				$data=$avalues;
			
			$mKeyId = 1;
			foreach($mKeys[$dpParams['strTableNames'][$i]] as $mk)	
			{
				if($data[$mk])
					$respJSON['mKeys'][$dpParams['strTableNames'][$i]]['masterkey'.$mKeyId++] = $data[$mk];
				else
					$respJSON['mKeys'][$dpParams['strTableNames'][$i]]['masterkey'.$mKeyId++] = '';
			}		
		}
		if((isset($_SESSION[$strTableName."_count_captcha"])) or ($_SESSION[$strTableName."_count_captcha"]>0) or ($_SESSION[$strTableName."_count_captcha"]<5))
			$respJSON['hideCaptha'] = true;
	}
	else{
			$respJSON['success'] = false;
			if(!$pageObject->isCaptchaOk)
				$respJSON['captha'] = false;
			else		
				$respJSON['error'] = $message;
			if($onFly)
				$respJSON['message'] = $message;				
		}
	echo "<textarea>".htmlspecialchars(my_json_encode($respJSON))."</textarea>";	
	exit();
}

/////////////////////////////////////////////////////////////
//	prepare Edit Controls
/////////////////////////////////////////////////////////////

//	validation stuff
$regex='';
$regexmessage='';
$regextype = '';
$control = array();

foreach($addFields as $fName)
{
	$gfName = GoodFieldName($fName);
	$controls = array('controls'=>array());
	if(!$detailKeys || !in_array($fName, $detailKeys) || $fName == postvalue("category"))
	{		
		$control[$gfName] = array();
		$control[$gfName]["func"]="xt_buildeditcontrol";
		$control[$gfName]["params"] = array();
		$control[$gfName]["params"]["id"]= $id;
		$control[$gfName]["params"]["field"]=$fName;
		$control[$gfName]["params"]["value"]=@$defvalues[$fName];
		if(UseRTE($fName))
			$_SESSION[$strTableName."_".$fName."_rte"]=@$defvalues[$fName];
		
		//	Begin Add validation
		$arrValidate = getValidation($fName,$strTableName);	
		$control[$gfName]["params"]["validate"] = $arrValidate;
		//	End Add validation	
	}
	$controls["controls"]['ctrlInd'] = 0;
	$controls["controls"]['id'] = $id;
	$controls["controls"]['fieldName'] = $fName;
	
	if(UseRTEFCK($fName) || UseRTEInnova($fName) || UseRTEBasic($fName))
	{
		if(!$detailKeys || !in_array($fName, $detailKeys))	
			$control[$gfName]["params"]["mode"]="add";
		$controls["controls"]['mode'] = "add";
	}
	else
	{
		if($inlineadd==ADD_ONTHEFLY || $inlineadd==ADD_POPUP)
		{
			if(!$detailKeys || !in_array($fName, $detailKeys) || $fName == postvalue("category"))	
				$control[$gfName]["params"]["mode"]="inline_add";
			$controls["controls"]['mode'] = "inline_add";
		}
		else
		{
			if(!$detailKeys || !in_array($fName, $detailKeys) || $fName == postvalue("category"))	
				$control[$gfName]["params"]["mode"]="add";
			$controls["controls"]['mode'] = "add";
		}
	}
			
	if(!$detailKeys || !in_array($fName, $detailKeys))
		$xt->assignbyref($gfName."_editcontrol",$control[$gfName]);
	elseif($detailKeys && in_array($fName, $detailKeys))
		$controls["controls"]['value'] = @$defvalues[$fName];
		
	// category control field
	$strCategoryControl = $pageObject->hasDependField($fName);
	
	if($strCategoryControl!==false && in_array($strCategoryControl, $addFields))
		$vals = array($fName => @$defvalues[$fName],$strCategoryControl => @$defvalues[$strCategoryControl]);
	else
		$vals = array($fName => @$defvalues[$fName]);
	
	$preload = $pageObject->fillPreload($fName, $vals);
	if($preload!==false)
		$controls["controls"]['preloadData'] = $preload;
	
	$pageObject->fillControlsMap($controls);
	
	//fill field tool tips
	$pageObject->fillFieldToolTips($fName);
	
	// fill special settings for timepicker 	
	if(GetEditFormat($fName) == 'Time')	
		$pageObject->fillTimePickSettings($fName, @$defvalues[$fName]);
	
	if((($detailKeys && in_array($fName, $detailKeys)) || $fName == postvalue("category")) && array_key_exists($fName, $defvalues))
	{
		if((GetEditFormat($fName)==EDIT_FORMAT_LOOKUP_WIZARD || GetEditFormat($fName)==EDIT_FORMAT_RADIO) && GetpLookupType($fName) == LT_LOOKUPTABLE)
			$value=DisplayLookupWizard($fName,$defvalues[$fName],$defvalues,"",MODE_VIEW);
		elseif(NeedEncode($fName))
			$value = ProcessLargeText(GetData($defvalues,$fName, ViewFormat($fName)),"field=".rawurlencode(htmlspecialchars($fName)),"",MODE_VIEW);
		else
			$value = GetData($defvalues,$fName, ViewFormat($fName));
		
		$xt->assign($gfName."_editcontrol", $value);
	}
}
//fill tab groups name and sections name to controls
$pageObject->fillCntrlTabGroups();

//fill jsSettings and ControlsHTMLMap
$pageObject->fillSetCntrlMaps();

/////////////////////////////////////////////////////////////
if($pageObject->isShowDetailTables && ($inlineadd==ADD_SIMPLE || $inlineadd==ADD_POPUP))
{
	$options = array();
	//array of params for classes
	$options["mode"] = LIST_DETAILS;
	$options["pageType"] = PAGE_LIST;
	$options["masterPageType"] = PAGE_ADD;
	$options["mainMasterPageType"] = PAGE_ADD;
	$options['masterTable'] = $strTableName;
	$options['firstTime'] = 1;

	if(count($dpParams['ids']))
	{
		$xt->assign("detail_tables",true);
		include('classes/listpage.php');
		include('classes/listpage_embed.php');
		include('classes/listpage_dpinline.php');
		include("classes/searchclause.php");
	}
	
	$dControlsMap = array();
		
	$flyId = $ids+1;
	for($d=0;$d<count($dpParams['ids']);$d++)
	{
		$strTableName = $dpParams['strTableNames'][$d];
		include("include/".GetTableURL($strTableName)."_settings.php");
		$options['xt'] = new Xtempl();
		$options['id'] = $dpParams['ids'][$d];
		$options['flyId'] = $flyId++;
		$mkr=1;
		
		foreach($mKeys[$strTableName] as $mk)
		{
			if($defvalues[$mk])
				$options['masterKeysReq'][$mkr++] = $defvalues[$mk];
			else
				$options['masterKeysReq'][$mkr++] = '';
		}
		
		$listPageObject = ListPage::createListPage($strTableName,$options);
		// prepare code
		$listPageObject->prepareForBuildPage();
		$flyId = $listPageObject->recId+1;
		
		if($listPageObject->isDispGrid())
		{
			//add detail settings to master settings
			$listPageObject->fillSetCntrlMaps();
			$pageObject->jsSettings['tableSettings'][$strTableName]	= $listPageObject->jsSettings['tableSettings'][$strTableName];		
			$dControlsMap[$strTableName]['gridRows'] = $listPageObject->controlsHTMLMap[$strTableName][PAGE_LIST][$dpParams['ids'][$d]]['gridRows'];
			$dControlsMap[$strTableName]['video'] = $listPageObject->controlsHTMLMap[$strTableName][PAGE_LIST][$dpParams['ids'][$d]]['video'];
			$dControlsMap[$strTableName]['gMaps'] = $listPageObject->controlsHTMLMap[$strTableName][PAGE_LIST][$dpParams['ids'][$d]]['gMaps'];
			foreach($listPageObject->jsSettings['global']['shortTNames'] as $key=>$val)
			{
				if(!array_key_exists($key,$pageObject->jsSettings['global']['shortTNames']))
					$pageObject->jsSettings['global']['shortTNames'][$key] = $val;
			}	
			
			//Add detail's js files to master's files
			$pageObject->copyAllJSFiles($listPageObject->grabAllJSFiles());
			
			//Add detail's css files to master's files	
			$pageObject->copyAllCSSFiles($listPageObject->grabAllCSSFiles());
		}
		$xt->assign("displayDetailTable_".GoodFieldName($strTableName), array("func" => "showDetailTable","params" => array("dpObject" => $listPageObject, "dpParams" => $strTableName)));
	}
	$strTableName = "employee_list";
	$pageObject->controlsHTMLMap[$strTableName][PAGE_ADD][$id]['dControlsMap'] = $dControlsMap;	
}
/////////////////////////////////////////////////////////////

if($inlineadd == ADD_SIMPLE)
{
	$pageObject->body['end'] .= '<script>';
	$pageObject->body['end'] .= "window.controlsMap = '".jsreplace(my_json_encode($pageObject->controlsHTMLMap))."';";
	$pageObject->body['end'] .= "window.settings = '".jsreplace(my_json_encode($pageObject->jsSettings))."';";
	$pageObject->body['end'] .= '</script>';
}
else{
		$returnJSON['controlsMap'] = $pageObject->controlsHTMLMap;
		//if($isNeedSettings)
		$returnJSON['settings'] = $pageObject->jsSettings;	
	}

$pageObject->addCommonJs();

$jscode = $pageObject->PrepareJS();
if($inlineadd==ADD_SIMPLE)
{
	$pageObject->body["end"] .= "<script>".$jscode."</script>";
	$xt->assign("body",$pageObject->body);
	$xt->assign("flybody",true);
}
elseif($inlineadd==ADD_ONTHEFLY || $inlineadd==ADD_MASTER || $inlineadd==ADD_POPUP)
{ 
	$xt->assign("footer","");
	$xt->assign("flybody",$pageObject->body);
	$xt->assign("body",true);
}	

$xt->assign("style_block",true);
$pageObject->xt->assign("legend", true);

if($eventObj->exists("BeforeShowAdd"))
	$eventObj->BeforeShowAdd($xt,$templatefile);

if($inlineadd==ADD_ONTHEFLY || $inlineadd==ADD_POPUP)
{
	$xt->load_template($templatefile);
	$returnJSON['html'] = $xt->fetch_loaded('style_block').$xt->fetch_loaded('flybody');
	if($inlineadd==ADD_POPUP && $pageObject->isShowDetailTables)
		$returnJSON['html'].= $xt->fetch_loaded('detail_tables');
	$returnJSON['idStartFrom'] = $id+1;	
	echo (my_json_encode($returnJSON)); 
}
elseif ($inlineadd == ADD_INLINE)
{
	$xt->load_template($templatefile);
	$returnJSON["html"] = array();
	foreach($addFields as $fName)
	{
		$returnJSON["html"][$fName] = $xt->fetchVar(GoodFieldName($fName)."_editcontrol");	
	}	
	echo (my_json_encode($returnJSON)); 
}
else
	$xt->display($templatefile);

?>
