<?php include_once "seguimiento_individual3info.php" ?>
<?php include_once "ficha_usuarioinfo.php" ?>
<?php include_once "registroinfo.php" ?>
<?php

//
// Page class
//

$seguimiento_individual3_grid = NULL; // Initialize page object first

class cseguimiento_individual3_grid extends cseguimiento_individual3 {

	// Page ID
	var $PageID = 'grid';

	// Project ID
	var $ProjectID = "{43FC4B6C-8131-46B1-916C-14DB52B1691D}";

	// Table name
	var $TableName = 'seguimiento_individual3';

	// Page object name
	var $PageObjName = 'seguimiento_individual3_grid';

	// Grid form hidden field names
	var $FormName = 'fseguimiento_individual3grid';
	var $FormActionName = 'k_action';
	var $FormKeyName = 'k_key';
	var $FormOldKeyName = 'k_oldkey';
	var $FormBlankRowName = 'k_blankrow';
	var $FormKeyCountName = 'key_count';

	// Page name
	function PageName() {
		return ew_CurrentPage();
	}

	// Page URL
	function PageUrl() {
		$PageUrl = ew_CurrentPage() . "?";
		if ($this->UseTokenInUrl) $PageUrl .= "t=" . $this->TableVar . "&"; // Add page token
		return $PageUrl;
	}

	// Message
	function getMessage() {
		return @$_SESSION[EW_SESSION_MESSAGE];
	}

	function setMessage($v) {
		ew_AddMessage($_SESSION[EW_SESSION_MESSAGE], $v);
	}

	function getFailureMessage() {
		return @$_SESSION[EW_SESSION_FAILURE_MESSAGE];
	}

	function setFailureMessage($v) {
		ew_AddMessage($_SESSION[EW_SESSION_FAILURE_MESSAGE], $v);
	}

	function getSuccessMessage() {
		return @$_SESSION[EW_SESSION_SUCCESS_MESSAGE];
	}

	function setSuccessMessage($v) {
		ew_AddMessage($_SESSION[EW_SESSION_SUCCESS_MESSAGE], $v);
	}

	function getWarningMessage() {
		return @$_SESSION[EW_SESSION_WARNING_MESSAGE];
	}

	function setWarningMessage($v) {
		ew_AddMessage($_SESSION[EW_SESSION_WARNING_MESSAGE], $v);
	}

	// Methods to clear message
	function ClearMessage() {
		$_SESSION[EW_SESSION_MESSAGE] = "";
	}

	function ClearFailureMessage() {
		$_SESSION[EW_SESSION_FAILURE_MESSAGE] = "";
	}

	function ClearSuccessMessage() {
		$_SESSION[EW_SESSION_SUCCESS_MESSAGE] = "";
	}

	function ClearWarningMessage() {
		$_SESSION[EW_SESSION_WARNING_MESSAGE] = "";
	}

	function ClearMessages() {
		$_SESSION[EW_SESSION_MESSAGE] = "";
		$_SESSION[EW_SESSION_FAILURE_MESSAGE] = "";
		$_SESSION[EW_SESSION_SUCCESS_MESSAGE] = "";
		$_SESSION[EW_SESSION_WARNING_MESSAGE] = "";
	}

	// Show message
	function ShowMessage() {
		$hidden = FALSE;
		$html = "";

		// Message
		$sMessage = $this->getMessage();
		$this->Message_Showing($sMessage, "");
		if ($sMessage <> "") { // Message in Session, display
			if (!$hidden)
				$sMessage = "<button type=\"button\" class=\"close\" data-dismiss=\"alert\">&times;</button>" . $sMessage;
			$html .= "<div class=\"alert alert-info ewInfo\">" . $sMessage . "</div>";
			$_SESSION[EW_SESSION_MESSAGE] = ""; // Clear message in Session
		}

		// Warning message
		$sWarningMessage = $this->getWarningMessage();
		$this->Message_Showing($sWarningMessage, "warning");
		if ($sWarningMessage <> "") { // Message in Session, display
			if (!$hidden)
				$sWarningMessage = "<button type=\"button\" class=\"close\" data-dismiss=\"alert\">&times;</button>" . $sWarningMessage;
			$html .= "<div class=\"alert alert-warning ewWarning\">" . $sWarningMessage . "</div>";
			$_SESSION[EW_SESSION_WARNING_MESSAGE] = ""; // Clear message in Session
		}

		// Success message
		$sSuccessMessage = $this->getSuccessMessage();
		$this->Message_Showing($sSuccessMessage, "success");
		if ($sSuccessMessage <> "") { // Message in Session, display
			if (!$hidden)
				$sSuccessMessage = "<button type=\"button\" class=\"close\" data-dismiss=\"alert\">&times;</button>" . $sSuccessMessage;
			$html .= "<div class=\"alert alert-success ewSuccess\">" . $sSuccessMessage . "</div>";
			$_SESSION[EW_SESSION_SUCCESS_MESSAGE] = ""; // Clear message in Session
		}

		// Failure message
		$sErrorMessage = $this->getFailureMessage();
		$this->Message_Showing($sErrorMessage, "failure");
		if ($sErrorMessage <> "") { // Message in Session, display
			if (!$hidden)
				$sErrorMessage = "<button type=\"button\" class=\"close\" data-dismiss=\"alert\">&times;</button>" . $sErrorMessage;
			$html .= "<div class=\"alert alert-danger ewError\">" . $sErrorMessage . "</div>";
			$_SESSION[EW_SESSION_FAILURE_MESSAGE] = ""; // Clear message in Session
		}
		echo "<div class=\"ewMessageDialog\"" . (($hidden) ? " style=\"display: none;\"" : "") . ">" . $html . "</div>";
	}
	var $PageHeader;
	var $PageFooter;

	// Show Page Header
	function ShowPageHeader() {
		$sHeader = $this->PageHeader;
		$this->Page_DataRendering($sHeader);
		if ($sHeader <> "") { // Header exists, display
			echo "<p>" . $sHeader . "</p>";
		}
	}

	// Show Page Footer
	function ShowPageFooter() {
		$sFooter = $this->PageFooter;
		$this->Page_DataRendered($sFooter);
		if ($sFooter <> "") { // Footer exists, display
			echo "<p>" . $sFooter . "</p>";
		}
	}

	// Validate page request
	function IsPageRequest() {
		global $objForm;
		if ($this->UseTokenInUrl) {
			if ($objForm)
				return ($this->TableVar == $objForm->GetValue("t"));
			if (@$_GET["t"] <> "")
				return ($this->TableVar == $_GET["t"]);
		} else {
			return TRUE;
		}
	}
	var $Token = "";
	var $TokenTimeout = 0;
	var $CheckToken = EW_CHECK_TOKEN;
	var $CheckTokenFn = "ew_CheckToken";
	var $CreateTokenFn = "ew_CreateToken";

	// Valid Post
	function ValidPost() {
		if (!$this->CheckToken || !ew_IsHttpPost())
			return TRUE;
		if (!isset($_POST[EW_TOKEN_NAME]))
			return FALSE;
		$fn = $this->CheckTokenFn;
		if (is_callable($fn))
			return $fn($_POST[EW_TOKEN_NAME], $this->TokenTimeout);
		return FALSE;
	}

	// Create Token
	function CreateToken() {
		global $gsToken;
		if ($this->CheckToken) {
			$fn = $this->CreateTokenFn;
			if ($this->Token == "" && is_callable($fn)) // Create token
				$this->Token = $fn();
			$gsToken = $this->Token; // Save to global variable
		}
	}

	//
	// Page class constructor
	//
	function __construct() {
		global $conn, $Language;
		global $UserTable, $UserTableConn;
		$this->FormActionName .= '_' . $this->FormName;
		$this->FormKeyName .= '_' . $this->FormName;
		$this->FormOldKeyName .= '_' . $this->FormName;
		$this->FormBlankRowName .= '_' . $this->FormName;
		$this->FormKeyCountName .= '_' . $this->FormName;
		$GLOBALS["Grid"] = &$this;
		$this->TokenTimeout = ew_SessionTimeoutTime();

		// Language object
		if (!isset($Language)) $Language = new cLanguage();

		// Parent constuctor
		parent::__construct();

		// Table object (seguimiento_individual3)
		if (!isset($GLOBALS["seguimiento_individual3"]) || get_class($GLOBALS["seguimiento_individual3"]) == "cseguimiento_individual3") {
			$GLOBALS["seguimiento_individual3"] = &$this;

//			$GLOBALS["MasterTable"] = &$GLOBALS["Table"];
//			if (!isset($GLOBALS["Table"])) $GLOBALS["Table"] = &$GLOBALS["seguimiento_individual3"];

		}

		// Table object (ficha_usuario)
		if (!isset($GLOBALS['ficha_usuario'])) $GLOBALS['ficha_usuario'] = new cficha_usuario();

		// Table object (registro)
		if (!isset($GLOBALS['registro'])) $GLOBALS['registro'] = new cregistro();

		// Page ID
		if (!defined("EW_PAGE_ID"))
			define("EW_PAGE_ID", 'grid', TRUE);

		// Table name (for backward compatibility)
		if (!defined("EW_TABLE_NAME"))
			define("EW_TABLE_NAME", 'seguimiento_individual3', TRUE);

		// Start timer
		if (!isset($GLOBALS["gTimer"])) $GLOBALS["gTimer"] = new cTimer();

		// Open connection
		if (!isset($conn)) $conn = ew_Connect($this->DBID);

		// User table object (ficha_usuario)
		if (!isset($UserTable)) {
			$UserTable = new cficha_usuario();
			$UserTableConn = Conn($UserTable->DBID);
		}

		// List options
		$this->ListOptions = new cListOptions();
		$this->ListOptions->TableVar = $this->TableVar;

		// Other options
		$this->OtherOptions['addedit'] = new cListOptions();
		$this->OtherOptions['addedit']->Tag = "div";
		$this->OtherOptions['addedit']->TagClassName = "ewAddEditOption";
	}

	// 
	//  Page_Init
	//
	function Page_Init() {
		global $gsExport, $gsCustomExport, $gsExportFile, $UserProfile, $Language, $Security, $objForm;

		// Security
		$Security = new cAdvancedSecurity();
		if (!$Security->IsLoggedIn()) $Security->AutoLogin();
		if ($Security->IsLoggedIn()) $Security->TablePermission_Loading();
		$Security->LoadCurrentUserLevel($this->ProjectID . $this->TableName);
		if ($Security->IsLoggedIn()) $Security->TablePermission_Loaded();
		if (!$Security->CanList()) {
			$Security->SaveLastUrl();
			$this->setFailureMessage(ew_DeniedMsg()); // Set no permission
			$this->Page_Terminate(ew_GetUrl("index.php"));
		}
		if ($Security->IsLoggedIn()) {
			$Security->UserID_Loading();
			$Security->LoadUserID();
			$Security->UserID_Loaded();
		}

		// Get grid add count
		$gridaddcnt = @$_GET[EW_TABLE_GRID_ADD_ROW_COUNT];
		if (is_numeric($gridaddcnt) && $gridaddcnt > 0)
			$this->GridAddRowCount = $gridaddcnt;

		// Set up list options
		$this->SetupListOptions();
		$this->Id->Visible = !$this->IsAdd() && !$this->IsCopy() && !$this->IsGridAdd();
		$this->Fecha_dig->Visible = !$this->IsAddOrEdit();

		// Global Page Loading event (in userfn*.php)
		Page_Loading();

		// Page Load event
		$this->Page_Load();

		// Check token
		if (!$this->ValidPost()) {
			echo $Language->Phrase("InvalidPostRequest");
			$this->Page_Terminate();
			exit();
		}

		// Process auto fill
		if (@$_POST["ajax"] == "autofill") {
			$results = $this->GetAutoFill(@$_POST["name"], @$_POST["q"]);
			if ($results) {

				// Clean output buffer
				if (!EW_DEBUG_ENABLED && ob_get_length())
					ob_end_clean();
				echo $results;
				$this->Page_Terminate();
				exit();
			}
		}

		// Create Token
		$this->CreateToken();

		// Set up master detail parameters
		$this->SetUpMasterParms();

		// Setup other options
		$this->SetupOtherOptions();
	}

	//
	// Page_Terminate
	//
	function Page_Terminate($url = "") {
		global $gsExportFile, $gTmpImages;

		// Export
		global $EW_EXPORT, $seguimiento_individual3;
		if ($this->CustomExport <> "" && $this->CustomExport == $this->Export && array_key_exists($this->CustomExport, $EW_EXPORT)) {
				$sContent = ob_get_contents();
			if ($gsExportFile == "") $gsExportFile = $this->TableVar;
			$class = $EW_EXPORT[$this->CustomExport];
			if (class_exists($class)) {
				$doc = new $class($seguimiento_individual3);
				$doc->Text = $sContent;
				if ($this->Export == "email")
					echo $this->ExportEmail($doc->Text);
				else
					$doc->Export();
				ew_DeleteTmpImages(); // Delete temp images
				exit();
			}
		}

//		$GLOBALS["Table"] = &$GLOBALS["MasterTable"];
		unset($GLOBALS["Grid"]);
		if ($url == "")
			return;
		$this->Page_Redirecting($url);

		// Go to URL if specified
		if ($url <> "") {
			if (!EW_DEBUG_ENABLED && ob_get_length())
				ob_end_clean();
			header("Location: " . $url);
		}
		exit();
	}

	// Class variables
	var $ListOptions; // List options
	var $ExportOptions; // Export options
	var $SearchOptions; // Search options
	var $OtherOptions = array(); // Other options
	var $FilterOptions; // Filter options
	var $ListActions; // List actions
	var $SelectedCount = 0;
	var $SelectedIndex = 0;
	var $ShowOtherOptions = FALSE;
	var $DisplayRecs = 20;
	var $StartRec;
	var $StopRec;
	var $TotalRecs = 0;
	var $RecRange = 10;
	var $Pager;
	var $DefaultSearchWhere = ""; // Default search WHERE clause
	var $SearchWhere = ""; // Search WHERE clause
	var $RecCnt = 0; // Record count
	var $EditRowCnt;
	var $StartRowCnt = 1;
	var $RowCnt = 0;
	var $Attrs = array(); // Row attributes and cell attributes
	var $RowIndex = 0; // Row index
	var $KeyCount = 0; // Key count
	var $RowAction = ""; // Row action
	var $RowOldKey = ""; // Row old key (for copy)
	var $RecPerRow = 0;
	var $MultiColumnClass;
	var $MultiColumnEditClass = "col-sm-12";
	var $MultiColumnCnt = 12;
	var $MultiColumnEditCnt = 12;
	var $GridCnt = 0;
	var $ColCnt = 0;
	var $DbMasterFilter = ""; // Master filter
	var $DbDetailFilter = ""; // Detail filter
	var $MasterRecordExists;	
	var $MultiSelectKey;
	var $Command;
	var $RestoreSearch = FALSE;
	var $DetailPages;
	var $Recordset;
	var $OldRecordset;

	//
	// Page main
	//
	function Page_Main() {
		global $objForm, $Language, $gsFormError, $gsSearchError, $Security;

		// Search filters
		$sSrchAdvanced = ""; // Advanced search filter
		$sSrchBasic = ""; // Basic search filter
		$sFilter = "";

		// Get command
		$this->Command = strtolower(@$_GET["cmd"]);
		if ($this->IsPageRequest()) { // Validate request

			// Set up records per page
			$this->SetUpDisplayRecs();

			// Handle reset command
			$this->ResetCmd();

			// Hide list options
			if ($this->Export <> "") {
				$this->ListOptions->HideAllOptions(array("sequence"));
				$this->ListOptions->UseDropDownButton = FALSE; // Disable drop down button
				$this->ListOptions->UseButtonGroup = FALSE; // Disable button group
			} elseif ($this->CurrentAction == "gridadd" || $this->CurrentAction == "gridedit") {
				$this->ListOptions->HideAllOptions();
				$this->ListOptions->UseDropDownButton = FALSE; // Disable drop down button
				$this->ListOptions->UseButtonGroup = FALSE; // Disable button group
			}

			// Show grid delete link for grid add / grid edit
			if ($this->AllowAddDeleteRow) {
				if ($this->CurrentAction == "gridadd" || $this->CurrentAction == "gridedit") {
					$item = $this->ListOptions->GetItem("griddelete");
					if ($item) $item->Visible = TRUE;
				}
			}

			// Set up sorting order
			$this->SetUpSortOrder();
		}

		// Restore display records
		if ($this->getRecordsPerPage() <> "") {
			$this->DisplayRecs = $this->getRecordsPerPage(); // Restore from Session
		} else {
			$this->DisplayRecs = 20; // Load default
		}

		// Load Sorting Order
		$this->LoadSortOrder();

		// Build filter
		$sFilter = "";
		if (!$Security->CanList())
			$sFilter = "(0=1)"; // Filter all records

		// Restore master/detail filter
		$this->DbMasterFilter = $this->GetMasterFilter(); // Restore master filter
		$this->DbDetailFilter = $this->GetDetailFilter(); // Restore detail filter

		// Add master User ID filter
		if ($Security->CurrentUserID() <> "" && !$Security->IsAdmin()) { // Non system admin
			if ($this->getCurrentMasterTable() == "registro")
				$this->DbMasterFilter = $this->AddMasterUserIDFilter($this->DbMasterFilter, "registro"); // Add master User ID filter
		}
		ew_AddFilter($sFilter, $this->DbDetailFilter);
		ew_AddFilter($sFilter, $this->SearchWhere);

		// Load master record
		if ($this->CurrentMode <> "add" && $this->GetMasterFilter() <> "" && $this->getCurrentMasterTable() == "registro") {
			global $registro;
			$rsmaster = $registro->LoadRs($this->DbMasterFilter);
			$this->MasterRecordExists = ($rsmaster && !$rsmaster->EOF);
			if (!$this->MasterRecordExists) {
				$this->setFailureMessage($Language->Phrase("NoRecord")); // Set no record found
				$this->Page_Terminate("registrolist.php"); // Return to master page
			} else {
				$registro->LoadListRowValues($rsmaster);
				$registro->RowType = EW_ROWTYPE_MASTER; // Master row
				$registro->RenderListRow();
				$rsmaster->Close();
			}
		}

		// Set up filter in session
		$this->setSessionWhere($sFilter);
		$this->CurrentFilter = "";

		// Load record count first
		if (!$this->IsAddOrEdit()) {
			$bSelectLimit = $this->UseSelectLimit;
			if ($bSelectLimit) {
				$this->TotalRecs = $this->SelectRecordCount();
			} else {
				if ($this->Recordset = $this->LoadRecordset())
					$this->TotalRecs = $this->Recordset->RecordCount();
			}
		}
	}

	// Set up number of records displayed per page
	function SetUpDisplayRecs() {
		$sWrk = @$_GET[EW_TABLE_REC_PER_PAGE];
		if ($sWrk <> "") {
			if (is_numeric($sWrk)) {
				$this->DisplayRecs = intval($sWrk);
			} else {
				if (strtolower($sWrk) == "all") { // Display all records
					$this->DisplayRecs = -1;
				} else {
					$this->DisplayRecs = 20; // Non-numeric, load default
				}
			}
			$this->setRecordsPerPage($this->DisplayRecs); // Save to Session

			// Reset start position
			$this->StartRec = 1;
			$this->setStartRecordNumber($this->StartRec);
		}
	}

	//  Exit inline mode
	function ClearInlineMode() {
		$this->LastAction = $this->CurrentAction; // Save last action
		$this->CurrentAction = ""; // Clear action
		$_SESSION[EW_SESSION_INLINE_MODE] = ""; // Clear inline mode
	}

	// Switch to Grid Add mode
	function GridAddMode() {
		$_SESSION[EW_SESSION_INLINE_MODE] = "gridadd"; // Enabled grid add
	}

	// Switch to Grid Edit mode
	function GridEditMode() {
		$_SESSION[EW_SESSION_INLINE_MODE] = "gridedit"; // Enable grid edit
	}

	// Perform update to grid
	function GridUpdate() {
		global $Language, $objForm, $gsFormError;
		$bGridUpdate = TRUE;

		// Get old recordset
		$this->CurrentFilter = $this->BuildKeyFilter();
		if ($this->CurrentFilter == "")
			$this->CurrentFilter = "0=1";
		$sSql = $this->SQL();
		$conn = &$this->Connection();
		if ($rs = $conn->Execute($sSql)) {
			$rsold = $rs->GetRows();
			$rs->Close();
		}

		// Call Grid Updating event
		if (!$this->Grid_Updating($rsold)) {
			if ($this->getFailureMessage() == "")
				$this->setFailureMessage($Language->Phrase("GridEditCancelled")); // Set grid edit cancelled message
			return FALSE;
		}
		$sKey = "";

		// Update row index and get row key
		$objForm->Index = -1;
		$rowcnt = strval($objForm->GetValue($this->FormKeyCountName));
		if ($rowcnt == "" || !is_numeric($rowcnt))
			$rowcnt = 0;

		// Update all rows based on key
		for ($rowindex = 1; $rowindex <= $rowcnt; $rowindex++) {
			$objForm->Index = $rowindex;
			$rowkey = strval($objForm->GetValue($this->FormKeyName));
			$rowaction = strval($objForm->GetValue($this->FormActionName));

			// Load all values and keys
			if ($rowaction <> "insertdelete") { // Skip insert then deleted rows
				$this->LoadFormValues(); // Get form values
				if ($rowaction == "" || $rowaction == "edit" || $rowaction == "delete") {
					$bGridUpdate = $this->SetupKeyValues($rowkey); // Set up key values
				} else {
					$bGridUpdate = TRUE;
				}

				// Skip empty row
				if ($rowaction == "insert" && $this->EmptyRow()) {

					// No action required
				// Validate form and insert/update/delete record

				} elseif ($bGridUpdate) {
					if ($rowaction == "delete") {
						$this->CurrentFilter = $this->KeyFilter();
						$bGridUpdate = $this->DeleteRows(); // Delete this row
					} else if (!$this->ValidateForm()) {
						$bGridUpdate = FALSE; // Form error, reset action
						$this->setFailureMessage($gsFormError);
					} else {
						if ($rowaction == "insert") {
							$bGridUpdate = $this->AddRow(); // Insert this row
						} else {
							if ($rowkey <> "") {
								$this->SendEmail = FALSE; // Do not send email on update success
								$bGridUpdate = $this->EditRow(); // Update this row
							}
						} // End update
					}
				}
				if ($bGridUpdate) {
					if ($sKey <> "") $sKey .= ", ";
					$sKey .= $rowkey;
				} else {
					break;
				}
			}
		}
		if ($bGridUpdate) {

			// Get new recordset
			if ($rs = $conn->Execute($sSql)) {
				$rsnew = $rs->GetRows();
				$rs->Close();
			}

			// Call Grid_Updated event
			$this->Grid_Updated($rsold, $rsnew);
			$this->ClearInlineMode(); // Clear inline edit mode
		} else {
			if ($this->getFailureMessage() == "")
				$this->setFailureMessage($Language->Phrase("UpdateFailed")); // Set update failed message
		}
		return $bGridUpdate;
	}

	// Build filter for all keys
	function BuildKeyFilter() {
		global $objForm;
		$sWrkFilter = "";

		// Update row index and get row key
		$rowindex = 1;
		$objForm->Index = $rowindex;
		$sThisKey = strval($objForm->GetValue($this->FormKeyName));
		while ($sThisKey <> "") {
			if ($this->SetupKeyValues($sThisKey)) {
				$sFilter = $this->KeyFilter();
				if ($sWrkFilter <> "") $sWrkFilter .= " OR ";
				$sWrkFilter .= $sFilter;
			} else {
				$sWrkFilter = "0=1";
				break;
			}

			// Update row index and get row key
			$rowindex++; // Next row
			$objForm->Index = $rowindex;
			$sThisKey = strval($objForm->GetValue($this->FormKeyName));
		}
		return $sWrkFilter;
	}

	// Set up key values
	function SetupKeyValues($key) {
		$arrKeyFlds = explode($GLOBALS["EW_COMPOSITE_KEY_SEPARATOR"], $key);
		if (count($arrKeyFlds) >= 1) {
			$this->Id->setFormValue($arrKeyFlds[0]);
			if (!is_numeric($this->Id->FormValue))
				return FALSE;
		}
		return TRUE;
	}

	// Perform Grid Add
	function GridInsert() {
		global $Language, $objForm, $gsFormError;
		$rowindex = 1;
		$bGridInsert = FALSE;
		$conn = &$this->Connection();

		// Call Grid Inserting event
		if (!$this->Grid_Inserting()) {
			if ($this->getFailureMessage() == "") {
				$this->setFailureMessage($Language->Phrase("GridAddCancelled")); // Set grid add cancelled message
			}
			return FALSE;
		}

		// Init key filter
		$sWrkFilter = "";
		$addcnt = 0;
		$sKey = "";

		// Get row count
		$objForm->Index = -1;
		$rowcnt = strval($objForm->GetValue($this->FormKeyCountName));
		if ($rowcnt == "" || !is_numeric($rowcnt))
			$rowcnt = 0;

		// Insert all rows
		for ($rowindex = 1; $rowindex <= $rowcnt; $rowindex++) {

			// Load current row values
			$objForm->Index = $rowindex;
			$rowaction = strval($objForm->GetValue($this->FormActionName));
			if ($rowaction <> "" && $rowaction <> "insert")
				continue; // Skip
			if ($rowaction == "insert") {
				$this->RowOldKey = strval($objForm->GetValue($this->FormOldKeyName));
				$this->LoadOldRecord(); // Load old recordset
			}
			$this->LoadFormValues(); // Get form values
			if (!$this->EmptyRow()) {
				$addcnt++;
				$this->SendEmail = FALSE; // Do not send email on insert success

				// Validate form
				if (!$this->ValidateForm()) {
					$bGridInsert = FALSE; // Form error, reset action
					$this->setFailureMessage($gsFormError);
				} else {
					$bGridInsert = $this->AddRow($this->OldRecordset); // Insert this row
				}
				if ($bGridInsert) {
					if ($sKey <> "") $sKey .= $GLOBALS["EW_COMPOSITE_KEY_SEPARATOR"];
					$sKey .= $this->Id->CurrentValue;

					// Add filter for this record
					$sFilter = $this->KeyFilter();
					if ($sWrkFilter <> "") $sWrkFilter .= " OR ";
					$sWrkFilter .= $sFilter;
				} else {
					break;
				}
			}
		}
		if ($addcnt == 0) { // No record inserted
			$this->ClearInlineMode(); // Clear grid add mode and return
			return TRUE;
		}
		if ($bGridInsert) {

			// Get new recordset
			$this->CurrentFilter = $sWrkFilter;
			$sSql = $this->SQL();
			if ($rs = $conn->Execute($sSql)) {
				$rsnew = $rs->GetRows();
				$rs->Close();
			}

			// Call Grid_Inserted event
			$this->Grid_Inserted($rsnew);
			$this->ClearInlineMode(); // Clear grid add mode
		} else {
			if ($this->getFailureMessage() == "") {
				$this->setFailureMessage($Language->Phrase("InsertFailed")); // Set insert failed message
			}
		}
		return $bGridInsert;
	}

	// Check if empty row
	function EmptyRow() {
		global $objForm;
		if ($objForm->HasValue("x_Id_Registro") && $objForm->HasValue("o_Id_Registro") && $this->Id_Registro->CurrentValue <> $this->Id_Registro->OldValue)
			return FALSE;
		if ($objForm->HasValue("x_Codigo_Eps") && $objForm->HasValue("o_Codigo_Eps") && $this->Codigo_Eps->CurrentValue <> $this->Codigo_Eps->OldValue)
			return FALSE;
		if ($objForm->HasValue("x_Tipo_regimen") && $objForm->HasValue("o_Tipo_regimen") && $this->Tipo_regimen->CurrentValue <> $this->Tipo_regimen->OldValue)
			return FALSE;
		if ($objForm->HasValue("x_Codigo_Ips") && $objForm->HasValue("o_Codigo_Ips") && $this->Codigo_Ips->CurrentValue <> $this->Codigo_Ips->OldValue)
			return FALSE;
		if ($objForm->HasValue("x_Fecha_Confirmatorio") && $objForm->HasValue("o_Fecha_Confirmatorio") && $this->Fecha_Confirmatorio->CurrentValue <> $this->Fecha_Confirmatorio->OldValue)
			return FALSE;
		if ($objForm->HasValue("x_Estadio") && $objForm->HasValue("o_Estadio") && $this->Estadio->CurrentValue <> $this->Estadio->OldValue)
			return FALSE;
		if ($objForm->HasValue("x_Numero_Carga_Viral") && $objForm->HasValue("o_Numero_Carga_Viral") && $this->Numero_Carga_Viral->CurrentValue <> $this->Numero_Carga_Viral->OldValue)
			return FALSE;
		if ($objForm->HasValue("x_Fecha_Primera_Carga_Viral") && $objForm->HasValue("o_Fecha_Primera_Carga_Viral") && $this->Fecha_Primera_Carga_Viral->CurrentValue <> $this->Fecha_Primera_Carga_Viral->OldValue)
			return FALSE;
		if ($objForm->HasValue("x_CD4") && $objForm->HasValue("o_CD4") && $this->CD4->CurrentValue <> $this->CD4->OldValue)
			return FALSE;
		if ($objForm->HasValue("x_Fecha_CD4") && $objForm->HasValue("o_Fecha_CD4") && $this->Fecha_CD4->CurrentValue <> $this->Fecha_CD4->OldValue)
			return FALSE;
		if ($objForm->HasValue("x_adherencia") && $objForm->HasValue("o_adherencia") && $this->adherencia->CurrentValue <> $this->adherencia->OldValue)
			return FALSE;
		if ($objForm->HasValue("x_Tratamiento_Antirretroviral") && $objForm->HasValue("o_Tratamiento_Antirretroviral") && $this->Tratamiento_Antirretroviral->CurrentValue <> $this->Tratamiento_Antirretroviral->OldValue)
			return FALSE;
		if ($objForm->HasValue("x_Porque_No_Antirretroviral") && $objForm->HasValue("o_Porque_No_Antirretroviral") && $this->Porque_No_Antirretroviral->CurrentValue <> $this->Porque_No_Antirretroviral->OldValue)
			return FALSE;
		if ($objForm->HasValue("x_Porque_no_adherente") && $objForm->HasValue("o_Porque_no_adherente") && $this->Porque_no_adherente->CurrentValue <> $this->Porque_no_adherente->OldValue)
			return FALSE;
		if ($objForm->HasValue("x_Valor_ultima_cargaviral") && $objForm->HasValue("o_Valor_ultima_cargaviral") && $this->Valor_ultima_cargaviral->CurrentValue <> $this->Valor_ultima_cargaviral->OldValue)
			return FALSE;
		if ($objForm->HasValue("x_Fecha_Ultima_Cargaviral") && $objForm->HasValue("o_Fecha_Ultima_Cargaviral") && $this->Fecha_Ultima_Cargaviral->CurrentValue <> $this->Fecha_Ultima_Cargaviral->OldValue)
			return FALSE;
		if ($objForm->HasValue("x_Estado_Final") && $objForm->HasValue("o_Estado_Final") && $this->Estado_Final->CurrentValue <> $this->Estado_Final->OldValue)
			return FALSE;
		if ($objForm->HasValue("x_Cambio_Municipio") && $objForm->HasValue("o_Cambio_Municipio") && $this->Cambio_Municipio->CurrentValue <> $this->Cambio_Municipio->OldValue)
			return FALSE;
		return TRUE;
	}

	// Validate grid form
	function ValidateGridForm() {
		global $objForm;

		// Get row count
		$objForm->Index = -1;
		$rowcnt = strval($objForm->GetValue($this->FormKeyCountName));
		if ($rowcnt == "" || !is_numeric($rowcnt))
			$rowcnt = 0;

		// Validate all records
		for ($rowindex = 1; $rowindex <= $rowcnt; $rowindex++) {

			// Load current row values
			$objForm->Index = $rowindex;
			$rowaction = strval($objForm->GetValue($this->FormActionName));
			if ($rowaction <> "delete" && $rowaction <> "insertdelete") {
				$this->LoadFormValues(); // Get form values
				if ($rowaction == "insert" && $this->EmptyRow()) {

					// Ignore
				} else if (!$this->ValidateForm()) {
					return FALSE;
				}
			}
		}
		return TRUE;
	}

	// Get all form values of the grid
	function GetGridFormValues() {
		global $objForm;

		// Get row count
		$objForm->Index = -1;
		$rowcnt = strval($objForm->GetValue($this->FormKeyCountName));
		if ($rowcnt == "" || !is_numeric($rowcnt))
			$rowcnt = 0;
		$rows = array();

		// Loop through all records
		for ($rowindex = 1; $rowindex <= $rowcnt; $rowindex++) {

			// Load current row values
			$objForm->Index = $rowindex;
			$rowaction = strval($objForm->GetValue($this->FormActionName));
			if ($rowaction <> "delete" && $rowaction <> "insertdelete") {
				$this->LoadFormValues(); // Get form values
				if ($rowaction == "insert" && $this->EmptyRow()) {

					// Ignore
				} else {
					$rows[] = $this->GetFieldValues("FormValue"); // Return row as array
				}
			}
		}
		return $rows; // Return as array of array
	}

	// Restore form values for current row
	function RestoreCurrentRowFormValues($idx) {
		global $objForm;

		// Get row based on current index
		$objForm->Index = $idx;
		$this->LoadFormValues(); // Load form values
	}

	// Set up sort parameters
	function SetUpSortOrder() {

		// Check for "order" parameter
		if (@$_GET["order"] <> "") {
			$this->CurrentOrder = ew_StripSlashes(@$_GET["order"]);
			$this->CurrentOrderType = @$_GET["ordertype"];
			$this->setStartRecordNumber(1); // Reset start position
		}
	}

	// Load sort order parameters
	function LoadSortOrder() {
		$sOrderBy = $this->getSessionOrderBy(); // Get ORDER BY from Session
		if ($sOrderBy == "") {
			if ($this->getSqlOrderBy() <> "") {
				$sOrderBy = $this->getSqlOrderBy();
				$this->setSessionOrderBy($sOrderBy);
			}
		}
	}

	// Reset command
	// - cmd=reset (Reset search parameters)
	// - cmd=resetall (Reset search and master/detail parameters)
	// - cmd=resetsort (Reset sort parameters)
	function ResetCmd() {

		// Check if reset command
		if (substr($this->Command,0,5) == "reset") {

			// Reset master/detail keys
			if ($this->Command == "resetall") {
				$this->setCurrentMasterTable(""); // Clear master table
				$this->DbMasterFilter = "";
				$this->DbDetailFilter = "";
				$this->Id_Registro->setSessionValue("");
			}

			// Reset sorting order
			if ($this->Command == "resetsort") {
				$sOrderBy = "";
				$this->setSessionOrderBy($sOrderBy);
			}

			// Reset start position
			$this->StartRec = 1;
			$this->setStartRecordNumber($this->StartRec);
		}
	}

	// Set up list options
	function SetupListOptions() {
		global $Security, $Language;

		// "griddelete"
		if ($this->AllowAddDeleteRow) {
			$item = &$this->ListOptions->Add("griddelete");
			$item->CssStyle = "white-space: nowrap;";
			$item->OnLeft = TRUE;
			$item->Visible = FALSE; // Default hidden
		}

		// Add group option item
		$item = &$this->ListOptions->Add($this->ListOptions->GroupOptionName);
		$item->Body = "";
		$item->OnLeft = TRUE;
		$item->Visible = FALSE;

		// Drop down button for ListOptions
		$this->ListOptions->UseImageAndText = TRUE;
		$this->ListOptions->UseDropDownButton = FALSE;
		$this->ListOptions->DropDownButtonPhrase = $Language->Phrase("ButtonListOptions");
		$this->ListOptions->UseButtonGroup = FALSE;
		if ($this->ListOptions->UseButtonGroup && ew_IsMobile())
			$this->ListOptions->UseDropDownButton = TRUE;
		$this->ListOptions->ButtonClass = "btn-sm"; // Class for button group
		$item = &$this->ListOptions->GetItem($this->ListOptions->GroupOptionName);
		$item->Visible = $this->ListOptions->GroupOptionVisible();
	}

	// Render list options
	function RenderListOptions() {
		global $Security, $Language, $objForm;
		$this->ListOptions->LoadDefault();

		// Set up row action and key
		if (is_numeric($this->RowIndex) && $this->CurrentMode <> "view") {
			$objForm->Index = $this->RowIndex;
			$ActionName = str_replace("k_", "k" . $this->RowIndex . "_", $this->FormActionName);
			$OldKeyName = str_replace("k_", "k" . $this->RowIndex . "_", $this->FormOldKeyName);
			$KeyName = str_replace("k_", "k" . $this->RowIndex . "_", $this->FormKeyName);
			$BlankRowName = str_replace("k_", "k" . $this->RowIndex . "_", $this->FormBlankRowName);
			if ($this->RowAction <> "")
				$this->MultiSelectKey .= "<input type=\"hidden\" name=\"" . $ActionName . "\" id=\"" . $ActionName . "\" value=\"" . $this->RowAction . "\">";
			if ($objForm->HasValue($this->FormOldKeyName))
				$this->RowOldKey = strval($objForm->GetValue($this->FormOldKeyName));
			if ($this->RowOldKey <> "")
				$this->MultiSelectKey .= "<input type=\"hidden\" name=\"" . $OldKeyName . "\" id=\"" . $OldKeyName . "\" value=\"" . ew_HtmlEncode($this->RowOldKey) . "\">";
			if ($this->RowAction == "delete") {
				$rowkey = $objForm->GetValue($this->FormKeyName);
				$this->SetupKeyValues($rowkey);
			}
			if ($this->RowAction == "insert" && $this->CurrentAction == "F" && $this->EmptyRow())
				$this->MultiSelectKey .= "<input type=\"hidden\" name=\"" . $BlankRowName . "\" id=\"" . $BlankRowName . "\" value=\"1\">";
		}

		// "delete"
		if ($this->AllowAddDeleteRow) {
			if ($this->CurrentMode == "add" || $this->CurrentMode == "copy" || $this->CurrentMode == "edit") {
				$option = &$this->ListOptions;
				$option->UseButtonGroup = TRUE; // Use button group for grid delete button
				$option->UseImageAndText = TRUE; // Use image and text for grid delete button
				$oListOpt = &$option->Items["griddelete"];
				if (!$Security->CanDelete() && is_numeric($this->RowIndex) && ($this->RowAction == "" || $this->RowAction == "edit")) { // Do not allow delete existing record
					$oListOpt->Body = "&nbsp;";
				} else {
					$oListOpt->Body = "<a class=\"ewGridLink ewGridDelete\" title=\"" . ew_HtmlTitle($Language->Phrase("DeleteLink")) . "\" data-caption=\"" . ew_HtmlTitle($Language->Phrase("DeleteLink")) . "\" onclick=\"return ew_DeleteGridRow(this, " . $this->RowIndex . ");\">" . $Language->Phrase("DeleteLink") . "</a>";
				}
			}
		}
		if ($this->CurrentMode == "edit" && is_numeric($this->RowIndex)) {
			$this->MultiSelectKey .= "<input type=\"hidden\" name=\"" . $KeyName . "\" id=\"" . $KeyName . "\" value=\"" . $this->Id->CurrentValue . "\">";
		}
		$this->RenderListOptionsExt();
	}

	// Set record key
	function SetRecordKey(&$key, $rs) {
		$key = "";
		if ($key <> "") $key .= $GLOBALS["EW_COMPOSITE_KEY_SEPARATOR"];
		$key .= $rs->fields('Id');
	}

	// Set up other options
	function SetupOtherOptions() {
		global $Language, $Security;
		$option = &$this->OtherOptions["addedit"];
		$option->UseDropDownButton = FALSE;
		$option->DropDownButtonPhrase = $Language->Phrase("ButtonAddEdit");
		$option->UseButtonGroup = TRUE;
		$option->ButtonClass = "btn-sm"; // Class for button group
		$item = &$option->Add($option->GroupOptionName);
		$item->Body = "";
		$item->Visible = FALSE;
	}

	// Render other options
	function RenderOtherOptions() {
		global $Language, $Security;
		$options = &$this->OtherOptions;
		if (($this->CurrentMode == "add" || $this->CurrentMode == "copy" || $this->CurrentMode == "edit") && $this->CurrentAction != "F") { // Check add/copy/edit mode
			if ($this->AllowAddDeleteRow) {
				$option = &$options["addedit"];
				$option->UseDropDownButton = FALSE;
				$option->UseImageAndText = TRUE;
				$item = &$option->Add("addblankrow");
				$item->Body = "<a class=\"ewAddEdit ewAddBlankRow\" title=\"" . ew_HtmlTitle($Language->Phrase("AddBlankRow")) . "\" data-caption=\"" . ew_HtmlTitle($Language->Phrase("AddBlankRow")) . "\" href=\"javascript:void(0);\" onclick=\"ew_AddGridRow(this);\">" . $Language->Phrase("AddBlankRow") . "</a>";
				$item->Visible = $Security->CanAdd();
				$this->ShowOtherOptions = $item->Visible;
			}
		}
	}

	function RenderListOptionsExt() {
		global $Security, $Language;
	}

	// Set up starting record parameters
	function SetUpStartRec() {
		if ($this->DisplayRecs == 0)
			return;
		if ($this->IsPageRequest()) { // Validate request
			if (@$_GET[EW_TABLE_START_REC] <> "") { // Check for "start" parameter
				$this->StartRec = $_GET[EW_TABLE_START_REC];
				$this->setStartRecordNumber($this->StartRec);
			} elseif (@$_GET[EW_TABLE_PAGE_NO] <> "") {
				$PageNo = $_GET[EW_TABLE_PAGE_NO];
				if (is_numeric($PageNo)) {
					$this->StartRec = ($PageNo-1)*$this->DisplayRecs+1;
					if ($this->StartRec <= 0) {
						$this->StartRec = 1;
					} elseif ($this->StartRec >= intval(($this->TotalRecs-1)/$this->DisplayRecs)*$this->DisplayRecs+1) {
						$this->StartRec = intval(($this->TotalRecs-1)/$this->DisplayRecs)*$this->DisplayRecs+1;
					}
					$this->setStartRecordNumber($this->StartRec);
				}
			}
		}
		$this->StartRec = $this->getStartRecordNumber();

		// Check if correct start record counter
		if (!is_numeric($this->StartRec) || $this->StartRec == "") { // Avoid invalid start record counter
			$this->StartRec = 1; // Reset start record counter
			$this->setStartRecordNumber($this->StartRec);
		} elseif (intval($this->StartRec) > intval($this->TotalRecs)) { // Avoid starting record > total records
			$this->StartRec = intval(($this->TotalRecs-1)/$this->DisplayRecs)*$this->DisplayRecs+1; // Point to last page first record
			$this->setStartRecordNumber($this->StartRec);
		} elseif (($this->StartRec-1) % $this->DisplayRecs <> 0) {
			$this->StartRec = intval(($this->StartRec-1)/$this->DisplayRecs)*$this->DisplayRecs+1; // Point to page boundary
			$this->setStartRecordNumber($this->StartRec);
		}
	}

	// Get upload files
	function GetUploadFiles() {
		global $objForm, $Language;

		// Get upload data
	}

	// Load default values
	function LoadDefaultValues() {
		$this->Id->CurrentValue = NULL;
		$this->Id->OldValue = $this->Id->CurrentValue;
		$this->Id_Registro->CurrentValue = NULL;
		$this->Id_Registro->OldValue = $this->Id_Registro->CurrentValue;
		$this->Codigo_Eps->CurrentValue = NULL;
		$this->Codigo_Eps->OldValue = $this->Codigo_Eps->CurrentValue;
		$this->Tipo_regimen->CurrentValue = NULL;
		$this->Tipo_regimen->OldValue = $this->Tipo_regimen->CurrentValue;
		$this->Codigo_Ips->CurrentValue = NULL;
		$this->Codigo_Ips->OldValue = $this->Codigo_Ips->CurrentValue;
		$this->Fecha_Confirmatorio->CurrentValue = NULL;
		$this->Fecha_Confirmatorio->OldValue = $this->Fecha_Confirmatorio->CurrentValue;
		$this->Estadio->CurrentValue = NULL;
		$this->Estadio->OldValue = $this->Estadio->CurrentValue;
		$this->Numero_Carga_Viral->CurrentValue = NULL;
		$this->Numero_Carga_Viral->OldValue = $this->Numero_Carga_Viral->CurrentValue;
		$this->Fecha_Primera_Carga_Viral->CurrentValue = NULL;
		$this->Fecha_Primera_Carga_Viral->OldValue = $this->Fecha_Primera_Carga_Viral->CurrentValue;
		$this->CD4->CurrentValue = NULL;
		$this->CD4->OldValue = $this->CD4->CurrentValue;
		$this->Fecha_CD4->CurrentValue = NULL;
		$this->Fecha_CD4->OldValue = $this->Fecha_CD4->CurrentValue;
		$this->adherencia->CurrentValue = NULL;
		$this->adherencia->OldValue = $this->adherencia->CurrentValue;
		$this->Tratamiento_Antirretroviral->CurrentValue = NULL;
		$this->Tratamiento_Antirretroviral->OldValue = $this->Tratamiento_Antirretroviral->CurrentValue;
		$this->Porque_No_Antirretroviral->CurrentValue = NULL;
		$this->Porque_No_Antirretroviral->OldValue = $this->Porque_No_Antirretroviral->CurrentValue;
		$this->Porque_no_adherente->CurrentValue = NULL;
		$this->Porque_no_adherente->OldValue = $this->Porque_no_adherente->CurrentValue;
		$this->Valor_ultima_cargaviral->CurrentValue = NULL;
		$this->Valor_ultima_cargaviral->OldValue = $this->Valor_ultima_cargaviral->CurrentValue;
		$this->Fecha_Ultima_Cargaviral->CurrentValue = NULL;
		$this->Fecha_Ultima_Cargaviral->OldValue = $this->Fecha_Ultima_Cargaviral->CurrentValue;
		$this->Estado_Final->CurrentValue = NULL;
		$this->Estado_Final->OldValue = $this->Estado_Final->CurrentValue;
		$this->Cambio_Municipio->CurrentValue = NULL;
		$this->Cambio_Municipio->OldValue = $this->Cambio_Municipio->CurrentValue;
		$this->Fecha_dig->CurrentValue = ew_CurrentDate();
		$this->Fecha_dig->OldValue = $this->Fecha_dig->CurrentValue;
	}

	// Load form values
	function LoadFormValues() {

		// Load from form
		global $objForm;
		$objForm->FormName = $this->FormName;
		if (!$this->Id->FldIsDetailKey && $this->CurrentAction <> "gridadd" && $this->CurrentAction <> "add")
			$this->Id->setFormValue($objForm->GetValue("x_Id"));
		if (!$this->Id_Registro->FldIsDetailKey) {
			$this->Id_Registro->setFormValue($objForm->GetValue("x_Id_Registro"));
		}
		$this->Id_Registro->setOldValue($objForm->GetValue("o_Id_Registro"));
		if (!$this->Codigo_Eps->FldIsDetailKey) {
			$this->Codigo_Eps->setFormValue($objForm->GetValue("x_Codigo_Eps"));
		}
		$this->Codigo_Eps->setOldValue($objForm->GetValue("o_Codigo_Eps"));
		if (!$this->Tipo_regimen->FldIsDetailKey) {
			$this->Tipo_regimen->setFormValue($objForm->GetValue("x_Tipo_regimen"));
		}
		$this->Tipo_regimen->setOldValue($objForm->GetValue("o_Tipo_regimen"));
		if (!$this->Codigo_Ips->FldIsDetailKey) {
			$this->Codigo_Ips->setFormValue($objForm->GetValue("x_Codigo_Ips"));
		}
		$this->Codigo_Ips->setOldValue($objForm->GetValue("o_Codigo_Ips"));
		if (!$this->Fecha_Confirmatorio->FldIsDetailKey) {
			$this->Fecha_Confirmatorio->setFormValue($objForm->GetValue("x_Fecha_Confirmatorio"));
			$this->Fecha_Confirmatorio->CurrentValue = ew_UnFormatDateTime($this->Fecha_Confirmatorio->CurrentValue, 5);
		}
		$this->Fecha_Confirmatorio->setOldValue($objForm->GetValue("o_Fecha_Confirmatorio"));
		if (!$this->Estadio->FldIsDetailKey) {
			$this->Estadio->setFormValue($objForm->GetValue("x_Estadio"));
		}
		$this->Estadio->setOldValue($objForm->GetValue("o_Estadio"));
		if (!$this->Numero_Carga_Viral->FldIsDetailKey) {
			$this->Numero_Carga_Viral->setFormValue($objForm->GetValue("x_Numero_Carga_Viral"));
		}
		$this->Numero_Carga_Viral->setOldValue($objForm->GetValue("o_Numero_Carga_Viral"));
		if (!$this->Fecha_Primera_Carga_Viral->FldIsDetailKey) {
			$this->Fecha_Primera_Carga_Viral->setFormValue($objForm->GetValue("x_Fecha_Primera_Carga_Viral"));
			$this->Fecha_Primera_Carga_Viral->CurrentValue = ew_UnFormatDateTime($this->Fecha_Primera_Carga_Viral->CurrentValue, 5);
		}
		$this->Fecha_Primera_Carga_Viral->setOldValue($objForm->GetValue("o_Fecha_Primera_Carga_Viral"));
		if (!$this->CD4->FldIsDetailKey) {
			$this->CD4->setFormValue($objForm->GetValue("x_CD4"));
		}
		$this->CD4->setOldValue($objForm->GetValue("o_CD4"));
		if (!$this->Fecha_CD4->FldIsDetailKey) {
			$this->Fecha_CD4->setFormValue($objForm->GetValue("x_Fecha_CD4"));
			$this->Fecha_CD4->CurrentValue = ew_UnFormatDateTime($this->Fecha_CD4->CurrentValue, 5);
		}
		$this->Fecha_CD4->setOldValue($objForm->GetValue("o_Fecha_CD4"));
		if (!$this->adherencia->FldIsDetailKey) {
			$this->adherencia->setFormValue($objForm->GetValue("x_adherencia"));
		}
		$this->adherencia->setOldValue($objForm->GetValue("o_adherencia"));
		if (!$this->Tratamiento_Antirretroviral->FldIsDetailKey) {
			$this->Tratamiento_Antirretroviral->setFormValue($objForm->GetValue("x_Tratamiento_Antirretroviral"));
		}
		$this->Tratamiento_Antirretroviral->setOldValue($objForm->GetValue("o_Tratamiento_Antirretroviral"));
		if (!$this->Porque_No_Antirretroviral->FldIsDetailKey) {
			$this->Porque_No_Antirretroviral->setFormValue($objForm->GetValue("x_Porque_No_Antirretroviral"));
		}
		$this->Porque_No_Antirretroviral->setOldValue($objForm->GetValue("o_Porque_No_Antirretroviral"));
		if (!$this->Porque_no_adherente->FldIsDetailKey) {
			$this->Porque_no_adherente->setFormValue($objForm->GetValue("x_Porque_no_adherente"));
		}
		$this->Porque_no_adherente->setOldValue($objForm->GetValue("o_Porque_no_adherente"));
		if (!$this->Valor_ultima_cargaviral->FldIsDetailKey) {
			$this->Valor_ultima_cargaviral->setFormValue($objForm->GetValue("x_Valor_ultima_cargaviral"));
		}
		$this->Valor_ultima_cargaviral->setOldValue($objForm->GetValue("o_Valor_ultima_cargaviral"));
		if (!$this->Fecha_Ultima_Cargaviral->FldIsDetailKey) {
			$this->Fecha_Ultima_Cargaviral->setFormValue($objForm->GetValue("x_Fecha_Ultima_Cargaviral"));
			$this->Fecha_Ultima_Cargaviral->CurrentValue = ew_UnFormatDateTime($this->Fecha_Ultima_Cargaviral->CurrentValue, 5);
		}
		$this->Fecha_Ultima_Cargaviral->setOldValue($objForm->GetValue("o_Fecha_Ultima_Cargaviral"));
		if (!$this->Estado_Final->FldIsDetailKey) {
			$this->Estado_Final->setFormValue($objForm->GetValue("x_Estado_Final"));
		}
		$this->Estado_Final->setOldValue($objForm->GetValue("o_Estado_Final"));
		if (!$this->Cambio_Municipio->FldIsDetailKey) {
			$this->Cambio_Municipio->setFormValue($objForm->GetValue("x_Cambio_Municipio"));
		}
		$this->Cambio_Municipio->setOldValue($objForm->GetValue("o_Cambio_Municipio"));
		if (!$this->Fecha_dig->FldIsDetailKey) {
			$this->Fecha_dig->setFormValue($objForm->GetValue("x_Fecha_dig"));
			$this->Fecha_dig->CurrentValue = ew_UnFormatDateTime($this->Fecha_dig->CurrentValue, 5);
		}
		$this->Fecha_dig->setOldValue($objForm->GetValue("o_Fecha_dig"));
	}

	// Restore form values
	function RestoreFormValues() {
		global $objForm;
		if ($this->CurrentAction <> "gridadd" && $this->CurrentAction <> "add")
			$this->Id->CurrentValue = $this->Id->FormValue;
		$this->Id_Registro->CurrentValue = $this->Id_Registro->FormValue;
		$this->Codigo_Eps->CurrentValue = $this->Codigo_Eps->FormValue;
		$this->Tipo_regimen->CurrentValue = $this->Tipo_regimen->FormValue;
		$this->Codigo_Ips->CurrentValue = $this->Codigo_Ips->FormValue;
		$this->Fecha_Confirmatorio->CurrentValue = $this->Fecha_Confirmatorio->FormValue;
		$this->Fecha_Confirmatorio->CurrentValue = ew_UnFormatDateTime($this->Fecha_Confirmatorio->CurrentValue, 5);
		$this->Estadio->CurrentValue = $this->Estadio->FormValue;
		$this->Numero_Carga_Viral->CurrentValue = $this->Numero_Carga_Viral->FormValue;
		$this->Fecha_Primera_Carga_Viral->CurrentValue = $this->Fecha_Primera_Carga_Viral->FormValue;
		$this->Fecha_Primera_Carga_Viral->CurrentValue = ew_UnFormatDateTime($this->Fecha_Primera_Carga_Viral->CurrentValue, 5);
		$this->CD4->CurrentValue = $this->CD4->FormValue;
		$this->Fecha_CD4->CurrentValue = $this->Fecha_CD4->FormValue;
		$this->Fecha_CD4->CurrentValue = ew_UnFormatDateTime($this->Fecha_CD4->CurrentValue, 5);
		$this->adherencia->CurrentValue = $this->adherencia->FormValue;
		$this->Tratamiento_Antirretroviral->CurrentValue = $this->Tratamiento_Antirretroviral->FormValue;
		$this->Porque_No_Antirretroviral->CurrentValue = $this->Porque_No_Antirretroviral->FormValue;
		$this->Porque_no_adherente->CurrentValue = $this->Porque_no_adherente->FormValue;
		$this->Valor_ultima_cargaviral->CurrentValue = $this->Valor_ultima_cargaviral->FormValue;
		$this->Fecha_Ultima_Cargaviral->CurrentValue = $this->Fecha_Ultima_Cargaviral->FormValue;
		$this->Fecha_Ultima_Cargaviral->CurrentValue = ew_UnFormatDateTime($this->Fecha_Ultima_Cargaviral->CurrentValue, 5);
		$this->Estado_Final->CurrentValue = $this->Estado_Final->FormValue;
		$this->Cambio_Municipio->CurrentValue = $this->Cambio_Municipio->FormValue;
		$this->Fecha_dig->CurrentValue = $this->Fecha_dig->FormValue;
		$this->Fecha_dig->CurrentValue = ew_UnFormatDateTime($this->Fecha_dig->CurrentValue, 5);
	}

	// Load recordset
	function LoadRecordset($offset = -1, $rowcnt = -1) {

		// Load List page SQL
		$sSql = $this->SelectSQL();
		$conn = &$this->Connection();

		// Load recordset
		$dbtype = ew_GetConnectionType($this->DBID);
		if ($this->UseSelectLimit) {
			$conn->raiseErrorFn = $GLOBALS["EW_ERROR_FN"];
			if ($dbtype == "MSSQL") {
				$rs = $conn->SelectLimit($sSql, $rowcnt, $offset, array("_hasOrderBy" => trim($this->getOrderBy()) || trim($this->getSessionOrderBy())));
			} else {
				$rs = $conn->SelectLimit($sSql, $rowcnt, $offset);
			}
			$conn->raiseErrorFn = '';
		} else {
			$rs = ew_LoadRecordset($sSql, $conn);
		}

		// Call Recordset Selected event
		$this->Recordset_Selected($rs);
		return $rs;
	}

	// Load row based on key values
	function LoadRow() {
		global $Security, $Language;
		$sFilter = $this->KeyFilter();

		// Call Row Selecting event
		$this->Row_Selecting($sFilter);

		// Load SQL based on filter
		$this->CurrentFilter = $sFilter;
		$sSql = $this->SQL();
		$conn = &$this->Connection();
		$res = FALSE;
		$rs = ew_LoadRecordset($sSql, $conn);
		if ($rs && !$rs->EOF) {
			$res = TRUE;
			$this->LoadRowValues($rs); // Load row values
			$rs->Close();
		}
		return $res;
	}

	// Load row values from recordset
	function LoadRowValues(&$rs) {
		if (!$rs || $rs->EOF) return;

		// Call Row Selected event
		$row = &$rs->fields;
		$this->Row_Selected($row);
		$this->Id->setDbValue($rs->fields('Id'));
		$this->Id_Registro->setDbValue($rs->fields('Id_Registro'));
		$this->Codigo_Eps->setDbValue($rs->fields('Codigo_Eps'));
		$this->Tipo_regimen->setDbValue($rs->fields('Tipo_regimen'));
		$this->Codigo_Ips->setDbValue($rs->fields('Codigo_Ips'));
		$this->Fecha_Confirmatorio->setDbValue($rs->fields('Fecha_Confirmatorio'));
		$this->Escolaridad->setDbValue($rs->fields('Escolaridad'));
		$this->Estado_Civil->setDbValue($rs->fields('Estado_Civil'));
		$this->Fecha_Nacimiento->setDbValue($rs->fields('Fecha_Nacimiento'));
		$this->Estadio->setDbValue($rs->fields('Estadio'));
		$this->Primera_Carga_Viral->setDbValue($rs->fields('Primera_Carga_Viral'));
		$this->Numero_Carga_Viral->setDbValue($rs->fields('Numero_Carga Viral'));
		$this->Fecha_Primera_Carga_Viral->setDbValue($rs->fields('Fecha_Primera_Carga_Viral'));
		$this->CD4->setDbValue($rs->fields('CD4'));
		$this->Fecha_CD4->setDbValue($rs->fields('Fecha_CD4'));
		$this->adherencia->setDbValue($rs->fields('adherencia'));
		$this->Tratamiento_Antirretroviral->setDbValue($rs->fields('Tratamiento_Antirretroviral'));
		$this->Descripcion_antirretroviral->setDbValue($rs->fields('Descripcion_antirretroviral'));
		$this->Cambio_Esquema->setDbValue($rs->fields('Cambio_Esquema'));
		$this->Porque_No_Antirretroviral->setDbValue($rs->fields('Porque_No_Antirretroviral'));
		$this->Porque_no_adherente->setDbValue($rs->fields('Porque_no_adherente'));
		$this->Valor_ultima_cargaviral->setDbValue($rs->fields('Valor_ultima_cargaviral'));
		$this->Fecha_Ultima_Cargaviral->setDbValue($rs->fields('Fecha_Ultima_Cargaviral'));
		$this->Estado_Final->setDbValue($rs->fields('Estado_Final'));
		$this->Cambio_Municipio->setDbValue($rs->fields('Cambio_Municipio'));
		$this->Observaciones->setDbValue($rs->fields('Observaciones'));
		$this->Primer_Nombre->setDbValue($rs->fields('Primer_Nombre'));
		$this->Segundo_Nombre->setDbValue($rs->fields('Segundo_Nombre'));
		$this->Primer_Apellido->setDbValue($rs->fields('Primer_Apellido'));
		$this->Segundo_Apellido->setDbValue($rs->fields('Segundo_Apellido'));
		$this->Registrado_Por->setDbValue($rs->fields('Registrado_Por'));
		$this->Documento->setDbValue($rs->fields('Documento'));
		$this->Fecha_dig->setDbValue($rs->fields('Fecha_dig'));
		$this->CD3->setDbValue($rs->fields('CD3'));
		$this->CD8->setDbValue($rs->fields('CD8'));
	}

	// Load DbValue from recordset
	function LoadDbValues(&$rs) {
		if (!$rs || !is_array($rs) && $rs->EOF) return;
		$row = is_array($rs) ? $rs : $rs->fields;
		$this->Id->DbValue = $row['Id'];
		$this->Id_Registro->DbValue = $row['Id_Registro'];
		$this->Codigo_Eps->DbValue = $row['Codigo_Eps'];
		$this->Tipo_regimen->DbValue = $row['Tipo_regimen'];
		$this->Codigo_Ips->DbValue = $row['Codigo_Ips'];
		$this->Fecha_Confirmatorio->DbValue = $row['Fecha_Confirmatorio'];
		$this->Escolaridad->DbValue = $row['Escolaridad'];
		$this->Estado_Civil->DbValue = $row['Estado_Civil'];
		$this->Fecha_Nacimiento->DbValue = $row['Fecha_Nacimiento'];
		$this->Estadio->DbValue = $row['Estadio'];
		$this->Primera_Carga_Viral->DbValue = $row['Primera_Carga_Viral'];
		$this->Numero_Carga_Viral->DbValue = $row['Numero_Carga Viral'];
		$this->Fecha_Primera_Carga_Viral->DbValue = $row['Fecha_Primera_Carga_Viral'];
		$this->CD4->DbValue = $row['CD4'];
		$this->Fecha_CD4->DbValue = $row['Fecha_CD4'];
		$this->adherencia->DbValue = $row['adherencia'];
		$this->Tratamiento_Antirretroviral->DbValue = $row['Tratamiento_Antirretroviral'];
		$this->Descripcion_antirretroviral->DbValue = $row['Descripcion_antirretroviral'];
		$this->Cambio_Esquema->DbValue = $row['Cambio_Esquema'];
		$this->Porque_No_Antirretroviral->DbValue = $row['Porque_No_Antirretroviral'];
		$this->Porque_no_adherente->DbValue = $row['Porque_no_adherente'];
		$this->Valor_ultima_cargaviral->DbValue = $row['Valor_ultima_cargaviral'];
		$this->Fecha_Ultima_Cargaviral->DbValue = $row['Fecha_Ultima_Cargaviral'];
		$this->Estado_Final->DbValue = $row['Estado_Final'];
		$this->Cambio_Municipio->DbValue = $row['Cambio_Municipio'];
		$this->Observaciones->DbValue = $row['Observaciones'];
		$this->Primer_Nombre->DbValue = $row['Primer_Nombre'];
		$this->Segundo_Nombre->DbValue = $row['Segundo_Nombre'];
		$this->Primer_Apellido->DbValue = $row['Primer_Apellido'];
		$this->Segundo_Apellido->DbValue = $row['Segundo_Apellido'];
		$this->Registrado_Por->DbValue = $row['Registrado_Por'];
		$this->Documento->DbValue = $row['Documento'];
		$this->Fecha_dig->DbValue = $row['Fecha_dig'];
		$this->CD3->DbValue = $row['CD3'];
		$this->CD8->DbValue = $row['CD8'];
	}

	// Load old record
	function LoadOldRecord() {

		// Load key values from Session
		$bValidKey = TRUE;
		$arKeys[] = $this->RowOldKey;
		$cnt = count($arKeys);
		if ($cnt >= 1) {
			if (strval($arKeys[0]) <> "")
				$this->Id->CurrentValue = strval($arKeys[0]); // Id
			else
				$bValidKey = FALSE;
		} else {
			$bValidKey = FALSE;
		}

		// Load old recordset
		if ($bValidKey) {
			$this->CurrentFilter = $this->KeyFilter();
			$sSql = $this->SQL();
			$conn = &$this->Connection();
			$this->OldRecordset = ew_LoadRecordset($sSql, $conn);
			$this->LoadRowValues($this->OldRecordset); // Load row values
		} else {
			$this->OldRecordset = NULL;
		}
		return $bValidKey;
	}

	// Render row values based on field settings
	function RenderRow() {
		global $Security, $Language, $gsLanguage;

		// Initialize URLs
		// Call Row_Rendering event

		$this->Row_Rendering();

		// Common render codes for all row types
		// Id
		// Id_Registro
		// Codigo_Eps
		// Tipo_regimen
		// Codigo_Ips
		// Fecha_Confirmatorio
		// Escolaridad
		// Estado_Civil
		// Fecha_Nacimiento
		// Estadio
		// Primera_Carga_Viral

		$this->Primera_Carga_Viral->CellCssStyle = "white-space: nowrap;";

		// Numero_Carga Viral
		// Fecha_Primera_Carga_Viral
		// CD4
		// Fecha_CD4
		// adherencia
		// Tratamiento_Antirretroviral
		// Descripcion_antirretroviral
		// Cambio_Esquema
		// Porque_No_Antirretroviral
		// Porque_no_adherente
		// Valor_ultima_cargaviral
		// Fecha_Ultima_Cargaviral
		// Estado_Final
		// Cambio_Municipio
		// Observaciones
		// Primer_Nombre
		// Segundo_Nombre
		// Primer_Apellido
		// Segundo_Apellido
		// Registrado_Por
		// Documento
		// Fecha_dig
		// CD3

		$this->CD3->CellCssStyle = "white-space: nowrap;";

		// CD8
		$this->CD8->CellCssStyle = "white-space: nowrap;";
		if ($this->RowType == EW_ROWTYPE_VIEW) { // View row

		// Id
		$this->Id->ViewValue = $this->Id->CurrentValue;
		$this->Id->ViewCustomAttributes = "";

		// Id_Registro
		if (strval($this->Id_Registro->CurrentValue) <> "") {
			$sFilterWrk = "`Consecutivo`" . ew_SearchString("=", $this->Id_Registro->CurrentValue, EW_DATATYPE_NUMBER, "");
		$sSqlWrk = "SELECT `Consecutivo`, `Primer_Nombre` AS `DispFld`, `Segundo_Nombre` AS `Disp2Fld`, `Primer_Apellido` AS `Disp3Fld`, `Numero_Identificacion` AS `Disp4Fld` FROM `registro`";
		$sWhereWrk = "";
		ew_AddFilter($sWhereWrk, $sFilterWrk);
		$this->Lookup_Selecting($this->Id_Registro, $sWhereWrk); // Call Lookup selecting
		if ($sWhereWrk <> "") $sSqlWrk .= " WHERE " . $sWhereWrk;
			$rswrk = Conn()->Execute($sSqlWrk);
			if ($rswrk && !$rswrk->EOF) { // Lookup values found
				$arwrk = array();
				$arwrk[1] = $rswrk->fields('DispFld');
				$arwrk[2] = $rswrk->fields('Disp2Fld');
				$arwrk[3] = $rswrk->fields('Disp3Fld');
				$arwrk[4] = $rswrk->fields('Disp4Fld');
				$this->Id_Registro->ViewValue = $this->Id_Registro->DisplayValue($arwrk);
				$rswrk->Close();
			} else {
				$this->Id_Registro->ViewValue = $this->Id_Registro->CurrentValue;
			}
		} else {
			$this->Id_Registro->ViewValue = NULL;
		}
		$this->Id_Registro->ViewCustomAttributes = "";

		// Codigo_Eps
		$this->Codigo_Eps->ViewValue = $this->Codigo_Eps->CurrentValue;
		if (strval($this->Codigo_Eps->CurrentValue) <> "") {
			$sFilterWrk = "`Codigo`" . ew_SearchString("=", $this->Codigo_Eps->CurrentValue, EW_DATATYPE_STRING, "");
		$sSqlWrk = "SELECT `Codigo`, `Codigo` AS `DispFld`, `Administradora_Salud` AS `Disp2Fld`, '' AS `Disp3Fld`, '' AS `Disp4Fld` FROM `administradora_salud`";
		$sWhereWrk = "";
		ew_AddFilter($sWhereWrk, $sFilterWrk);
		$this->Lookup_Selecting($this->Codigo_Eps, $sWhereWrk); // Call Lookup selecting
		if ($sWhereWrk <> "") $sSqlWrk .= " WHERE " . $sWhereWrk;
			$rswrk = Conn()->Execute($sSqlWrk);
			if ($rswrk && !$rswrk->EOF) { // Lookup values found
				$arwrk = array();
				$arwrk[1] = $rswrk->fields('DispFld');
				$arwrk[2] = $rswrk->fields('Disp2Fld');
				$this->Codigo_Eps->ViewValue = $this->Codigo_Eps->DisplayValue($arwrk);
				$rswrk->Close();
			} else {
				$this->Codigo_Eps->ViewValue = $this->Codigo_Eps->CurrentValue;
			}
		} else {
			$this->Codigo_Eps->ViewValue = NULL;
		}
		$this->Codigo_Eps->ViewCustomAttributes = "";

		// Tipo_regimen
		if (strval($this->Tipo_regimen->CurrentValue) <> "") {
			$sFilterWrk = "`Codigo_Regimen`" . ew_SearchString("=", $this->Tipo_regimen->CurrentValue, EW_DATATYPE_STRING, "");
		$sSqlWrk = "SELECT `Codigo_Regimen`, `Regimen_Salud` AS `DispFld`, '' AS `Disp2Fld`, '' AS `Disp3Fld`, '' AS `Disp4Fld` FROM `tipo_regimen_salud`";
		$sWhereWrk = "";
		ew_AddFilter($sWhereWrk, $sFilterWrk);
		$this->Lookup_Selecting($this->Tipo_regimen, $sWhereWrk); // Call Lookup selecting
		if ($sWhereWrk <> "") $sSqlWrk .= " WHERE " . $sWhereWrk;
			$rswrk = Conn()->Execute($sSqlWrk);
			if ($rswrk && !$rswrk->EOF) { // Lookup values found
				$arwrk = array();
				$arwrk[1] = $rswrk->fields('DispFld');
				$this->Tipo_regimen->ViewValue = $this->Tipo_regimen->DisplayValue($arwrk);
				$rswrk->Close();
			} else {
				$this->Tipo_regimen->ViewValue = $this->Tipo_regimen->CurrentValue;
			}
		} else {
			$this->Tipo_regimen->ViewValue = NULL;
		}
		$this->Tipo_regimen->ViewCustomAttributes = "";

		// Codigo_Ips
		$this->Codigo_Ips->ViewValue = $this->Codigo_Ips->CurrentValue;
		if (strval($this->Codigo_Ips->CurrentValue) <> "") {
			$sFilterWrk = "`Codigo_Upgd`" . ew_SearchString("=", $this->Codigo_Ips->CurrentValue, EW_DATATYPE_STRING, "");
		$sSqlWrk = "SELECT `Codigo_Upgd`, `Codigo_Upgd` AS `DispFld`, `Upgd` AS `Disp2Fld`, '' AS `Disp3Fld`, '' AS `Disp4Fld` FROM `entidad_upgd`";
		$sWhereWrk = "";
		ew_AddFilter($sWhereWrk, $sFilterWrk);
		$this->Lookup_Selecting($this->Codigo_Ips, $sWhereWrk); // Call Lookup selecting
		if ($sWhereWrk <> "") $sSqlWrk .= " WHERE " . $sWhereWrk;
			$rswrk = Conn()->Execute($sSqlWrk);
			if ($rswrk && !$rswrk->EOF) { // Lookup values found
				$arwrk = array();
				$arwrk[1] = $rswrk->fields('DispFld');
				$arwrk[2] = $rswrk->fields('Disp2Fld');
				$this->Codigo_Ips->ViewValue = $this->Codigo_Ips->DisplayValue($arwrk);
				$rswrk->Close();
			} else {
				$this->Codigo_Ips->ViewValue = $this->Codigo_Ips->CurrentValue;
			}
		} else {
			$this->Codigo_Ips->ViewValue = NULL;
		}
		$this->Codigo_Ips->ViewCustomAttributes = "";

		// Fecha_Confirmatorio
		$this->Fecha_Confirmatorio->ViewValue = $this->Fecha_Confirmatorio->CurrentValue;
		$this->Fecha_Confirmatorio->ViewValue = ew_FormatDateTime($this->Fecha_Confirmatorio->ViewValue, 5);
		$this->Fecha_Confirmatorio->ViewCustomAttributes = "";

		// Escolaridad
		if (strval($this->Escolaridad->CurrentValue) <> "") {
			$sFilterWrk = "`Codigo`" . ew_SearchString("=", $this->Escolaridad->CurrentValue, EW_DATATYPE_NUMBER, "");
		$sSqlWrk = "SELECT `Codigo`, `Nivel_Educativo` AS `DispFld`, '' AS `Disp2Fld`, '' AS `Disp3Fld`, '' AS `Disp4Fld` FROM `nivel_educativo`";
		$sWhereWrk = "";
		ew_AddFilter($sWhereWrk, $sFilterWrk);
		$this->Lookup_Selecting($this->Escolaridad, $sWhereWrk); // Call Lookup selecting
		if ($sWhereWrk <> "") $sSqlWrk .= " WHERE " . $sWhereWrk;
			$rswrk = Conn()->Execute($sSqlWrk);
			if ($rswrk && !$rswrk->EOF) { // Lookup values found
				$arwrk = array();
				$arwrk[1] = $rswrk->fields('DispFld');
				$this->Escolaridad->ViewValue = $this->Escolaridad->DisplayValue($arwrk);
				$rswrk->Close();
			} else {
				$this->Escolaridad->ViewValue = $this->Escolaridad->CurrentValue;
			}
		} else {
			$this->Escolaridad->ViewValue = NULL;
		}
		$this->Escolaridad->ViewCustomAttributes = "";

		// Estado_Civil
		if (strval($this->Estado_Civil->CurrentValue) <> "") {
			$sFilterWrk = "`Id`" . ew_SearchString("=", $this->Estado_Civil->CurrentValue, EW_DATATYPE_NUMBER, "");
		$sSqlWrk = "SELECT `Id`, `Estado_Civil` AS `DispFld`, '' AS `Disp2Fld`, '' AS `Disp3Fld`, '' AS `Disp4Fld` FROM `estado_civil`";
		$sWhereWrk = "";
		ew_AddFilter($sWhereWrk, $sFilterWrk);
		$this->Lookup_Selecting($this->Estado_Civil, $sWhereWrk); // Call Lookup selecting
		if ($sWhereWrk <> "") $sSqlWrk .= " WHERE " . $sWhereWrk;
			$rswrk = Conn()->Execute($sSqlWrk);
			if ($rswrk && !$rswrk->EOF) { // Lookup values found
				$arwrk = array();
				$arwrk[1] = $rswrk->fields('DispFld');
				$this->Estado_Civil->ViewValue = $this->Estado_Civil->DisplayValue($arwrk);
				$rswrk->Close();
			} else {
				$this->Estado_Civil->ViewValue = $this->Estado_Civil->CurrentValue;
			}
		} else {
			$this->Estado_Civil->ViewValue = NULL;
		}
		$this->Estado_Civil->ViewCustomAttributes = "";

		// Fecha_Nacimiento
		$this->Fecha_Nacimiento->ViewValue = $this->Fecha_Nacimiento->CurrentValue;
		$this->Fecha_Nacimiento->ViewValue = ew_FormatDateTime($this->Fecha_Nacimiento->ViewValue, 5);
		$this->Fecha_Nacimiento->ViewCustomAttributes = "";

		// Estadio
		if (strval($this->Estadio->CurrentValue) <> "") {
			$sFilterWrk = "`Id`" . ew_SearchString("=", $this->Estadio->CurrentValue, EW_DATATYPE_NUMBER, "");
		$sSqlWrk = "SELECT `Id`, `Estadio` AS `DispFld`, '' AS `Disp2Fld`, '' AS `Disp3Fld`, '' AS `Disp4Fld` FROM `estadio`";
		$sWhereWrk = "";
		ew_AddFilter($sWhereWrk, $sFilterWrk);
		$this->Lookup_Selecting($this->Estadio, $sWhereWrk); // Call Lookup selecting
		if ($sWhereWrk <> "") $sSqlWrk .= " WHERE " . $sWhereWrk;
			$rswrk = Conn()->Execute($sSqlWrk);
			if ($rswrk && !$rswrk->EOF) { // Lookup values found
				$arwrk = array();
				$arwrk[1] = $rswrk->fields('DispFld');
				$this->Estadio->ViewValue = $this->Estadio->DisplayValue($arwrk);
				$rswrk->Close();
			} else {
				$this->Estadio->ViewValue = $this->Estadio->CurrentValue;
			}
		} else {
			$this->Estadio->ViewValue = NULL;
		}
		$this->Estadio->ViewCustomAttributes = "";

		// Numero_Carga Viral
		if (strval($this->Numero_Carga_Viral->CurrentValue) <> "") {
			$sFilterWrk = "`Carga_viral`" . ew_SearchString("=", $this->Numero_Carga_Viral->CurrentValue, EW_DATATYPE_STRING, "");
		$sSqlWrk = "SELECT `Carga_viral`, `Carga_viral` AS `DispFld`, '' AS `Disp2Fld`, '' AS `Disp3Fld`, '' AS `Disp4Fld` FROM `carga_viral`";
		$sWhereWrk = "";
		ew_AddFilter($sWhereWrk, $sFilterWrk);
		$this->Lookup_Selecting($this->Numero_Carga_Viral, $sWhereWrk); // Call Lookup selecting
		if ($sWhereWrk <> "") $sSqlWrk .= " WHERE " . $sWhereWrk;
			$rswrk = Conn()->Execute($sSqlWrk);
			if ($rswrk && !$rswrk->EOF) { // Lookup values found
				$arwrk = array();
				$arwrk[1] = $rswrk->fields('DispFld');
				$this->Numero_Carga_Viral->ViewValue = $this->Numero_Carga_Viral->DisplayValue($arwrk);
				$rswrk->Close();
			} else {
				$this->Numero_Carga_Viral->ViewValue = $this->Numero_Carga_Viral->CurrentValue;
			}
		} else {
			$this->Numero_Carga_Viral->ViewValue = NULL;
		}
		$this->Numero_Carga_Viral->ViewCustomAttributes = "";

		// Fecha_Primera_Carga_Viral
		$this->Fecha_Primera_Carga_Viral->ViewValue = $this->Fecha_Primera_Carga_Viral->CurrentValue;
		$this->Fecha_Primera_Carga_Viral->ViewValue = ew_FormatDateTime($this->Fecha_Primera_Carga_Viral->ViewValue, 5);
		$this->Fecha_Primera_Carga_Viral->ViewCustomAttributes = "";

		// CD4
		$this->CD4->ViewValue = $this->CD4->CurrentValue;
		$this->CD4->ViewCustomAttributes = "";

		// Fecha_CD4
		$this->Fecha_CD4->ViewValue = $this->Fecha_CD4->CurrentValue;
		$this->Fecha_CD4->ViewValue = ew_FormatDateTime($this->Fecha_CD4->ViewValue, 5);
		$this->Fecha_CD4->ViewCustomAttributes = "";

		// adherencia
		if (strval($this->adherencia->CurrentValue) <> "") {
			$sFilterWrk = "`id`" . ew_SearchString("=", $this->adherencia->CurrentValue, EW_DATATYPE_NUMBER, "");
		$sSqlWrk = "SELECT `id`, `adherencia` AS `DispFld`, '' AS `Disp2Fld`, '' AS `Disp3Fld`, '' AS `Disp4Fld` FROM `adherencia`";
		$sWhereWrk = "";
		ew_AddFilter($sWhereWrk, $sFilterWrk);
		$this->Lookup_Selecting($this->adherencia, $sWhereWrk); // Call Lookup selecting
		if ($sWhereWrk <> "") $sSqlWrk .= " WHERE " . $sWhereWrk;
			$rswrk = Conn()->Execute($sSqlWrk);
			if ($rswrk && !$rswrk->EOF) { // Lookup values found
				$arwrk = array();
				$arwrk[1] = $rswrk->fields('DispFld');
				$this->adherencia->ViewValue = $this->adherencia->DisplayValue($arwrk);
				$rswrk->Close();
			} else {
				$this->adherencia->ViewValue = $this->adherencia->CurrentValue;
			}
		} else {
			$this->adherencia->ViewValue = NULL;
		}
		$this->adherencia->ViewCustomAttributes = "";

		// Tratamiento_Antirretroviral
		if (strval($this->Tratamiento_Antirretroviral->CurrentValue) <> "") {
			$sFilterWrk = "`Consecutivo`" . ew_SearchString("=", $this->Tratamiento_Antirretroviral->CurrentValue, EW_DATATYPE_NUMBER, "");
		$sSqlWrk = "SELECT `Consecutivo`, `Si_No` AS `DispFld`, '' AS `Disp2Fld`, '' AS `Disp3Fld`, '' AS `Disp4Fld` FROM `si_no`";
		$sWhereWrk = "";
		ew_AddFilter($sWhereWrk, $sFilterWrk);
		$this->Lookup_Selecting($this->Tratamiento_Antirretroviral, $sWhereWrk); // Call Lookup selecting
		if ($sWhereWrk <> "") $sSqlWrk .= " WHERE " . $sWhereWrk;
			$rswrk = Conn()->Execute($sSqlWrk);
			if ($rswrk && !$rswrk->EOF) { // Lookup values found
				$arwrk = array();
				$arwrk[1] = $rswrk->fields('DispFld');
				$this->Tratamiento_Antirretroviral->ViewValue = $this->Tratamiento_Antirretroviral->DisplayValue($arwrk);
				$rswrk->Close();
			} else {
				$this->Tratamiento_Antirretroviral->ViewValue = $this->Tratamiento_Antirretroviral->CurrentValue;
			}
		} else {
			$this->Tratamiento_Antirretroviral->ViewValue = NULL;
		}
		$this->Tratamiento_Antirretroviral->ViewCustomAttributes = "";

		// Descripcion_antirretroviral
		if (strval($this->Descripcion_antirretroviral->CurrentValue) <> "") {
			$arwrk = explode(",", $this->Descripcion_antirretroviral->CurrentValue);
			$sFilterWrk = "";
			foreach ($arwrk as $wrk) {
				if ($sFilterWrk <> "") $sFilterWrk .= " OR ";
				$sFilterWrk .= "`Id`" . ew_SearchString("=", trim($wrk), EW_DATATYPE_NUMBER, "");
			}
		$sSqlWrk = "SELECT `Id`, `Medicamentos` AS `DispFld`, '' AS `Disp2Fld`, '' AS `Disp3Fld`, '' AS `Disp4Fld` FROM `medicamentos_antiretrovirales`";
		$sWhereWrk = "";
		ew_AddFilter($sWhereWrk, $sFilterWrk);
		$this->Lookup_Selecting($this->Descripcion_antirretroviral, $sWhereWrk); // Call Lookup selecting
		if ($sWhereWrk <> "") $sSqlWrk .= " WHERE " . $sWhereWrk;
			$rswrk = Conn()->Execute($sSqlWrk);
			if ($rswrk && !$rswrk->EOF) { // Lookup values found
				$this->Descripcion_antirretroviral->ViewValue = "";
				$ari = 0;
				while (!$rswrk->EOF) {
					$arwrk = array();
					$arwrk[1] = $rswrk->fields('DispFld');
					$this->Descripcion_antirretroviral->ViewValue .= $this->Descripcion_antirretroviral->DisplayValue($arwrk);
					$rswrk->MoveNext();
					if (!$rswrk->EOF) $this->Descripcion_antirretroviral->ViewValue .= ew_ViewOptionSeparator($ari); // Separate Options
					$ari++;
				}
				$rswrk->Close();
			} else {
				$this->Descripcion_antirretroviral->ViewValue = $this->Descripcion_antirretroviral->CurrentValue;
			}
		} else {
			$this->Descripcion_antirretroviral->ViewValue = NULL;
		}
		$this->Descripcion_antirretroviral->ViewCustomAttributes = "";

		// Cambio_Esquema
		if (strval($this->Cambio_Esquema->CurrentValue) <> "") {
			$sFilterWrk = "`Consecutivo`" . ew_SearchString("=", $this->Cambio_Esquema->CurrentValue, EW_DATATYPE_NUMBER, "");
		$sSqlWrk = "SELECT `Consecutivo`, `Si_No` AS `DispFld`, '' AS `Disp2Fld`, '' AS `Disp3Fld`, '' AS `Disp4Fld` FROM `si_no`";
		$sWhereWrk = "";
		ew_AddFilter($sWhereWrk, $sFilterWrk);
		$this->Lookup_Selecting($this->Cambio_Esquema, $sWhereWrk); // Call Lookup selecting
		if ($sWhereWrk <> "") $sSqlWrk .= " WHERE " . $sWhereWrk;
			$rswrk = Conn()->Execute($sSqlWrk);
			if ($rswrk && !$rswrk->EOF) { // Lookup values found
				$arwrk = array();
				$arwrk[1] = $rswrk->fields('DispFld');
				$this->Cambio_Esquema->ViewValue = $this->Cambio_Esquema->DisplayValue($arwrk);
				$rswrk->Close();
			} else {
				$this->Cambio_Esquema->ViewValue = $this->Cambio_Esquema->CurrentValue;
			}
		} else {
			$this->Cambio_Esquema->ViewValue = NULL;
		}
		$this->Cambio_Esquema->ViewCustomAttributes = "";

		// Porque_No_Antirretroviral
		if (strval($this->Porque_No_Antirretroviral->CurrentValue) <> "") {
			$this->Porque_No_Antirretroviral->ViewValue = $this->Porque_No_Antirretroviral->OptionCaption($this->Porque_No_Antirretroviral->CurrentValue);
		} else {
			$this->Porque_No_Antirretroviral->ViewValue = NULL;
		}
		$this->Porque_No_Antirretroviral->ViewCustomAttributes = "";

		// Porque_no_adherente
		if (strval($this->Porque_no_adherente->CurrentValue) <> "") {
			$this->Porque_no_adherente->ViewValue = $this->Porque_no_adherente->OptionCaption($this->Porque_no_adherente->CurrentValue);
		} else {
			$this->Porque_no_adherente->ViewValue = NULL;
		}
		$this->Porque_no_adherente->ViewCustomAttributes = "";

		// Valor_ultima_cargaviral
		if (strval($this->Valor_ultima_cargaviral->CurrentValue) <> "") {
			$sFilterWrk = "`Carga_viral`" . ew_SearchString("=", $this->Valor_ultima_cargaviral->CurrentValue, EW_DATATYPE_STRING, "");
		$sSqlWrk = "SELECT `Carga_viral`, `Carga_viral` AS `DispFld`, '' AS `Disp2Fld`, '' AS `Disp3Fld`, '' AS `Disp4Fld` FROM `carga_viral`";
		$sWhereWrk = "";
		ew_AddFilter($sWhereWrk, $sFilterWrk);
		$this->Lookup_Selecting($this->Valor_ultima_cargaviral, $sWhereWrk); // Call Lookup selecting
		if ($sWhereWrk <> "") $sSqlWrk .= " WHERE " . $sWhereWrk;
			$rswrk = Conn()->Execute($sSqlWrk);
			if ($rswrk && !$rswrk->EOF) { // Lookup values found
				$arwrk = array();
				$arwrk[1] = $rswrk->fields('DispFld');
				$this->Valor_ultima_cargaviral->ViewValue = $this->Valor_ultima_cargaviral->DisplayValue($arwrk);
				$rswrk->Close();
			} else {
				$this->Valor_ultima_cargaviral->ViewValue = $this->Valor_ultima_cargaviral->CurrentValue;
			}
		} else {
			$this->Valor_ultima_cargaviral->ViewValue = NULL;
		}
		$this->Valor_ultima_cargaviral->ViewCustomAttributes = "";

		// Fecha_Ultima_Cargaviral
		$this->Fecha_Ultima_Cargaviral->ViewValue = $this->Fecha_Ultima_Cargaviral->CurrentValue;
		$this->Fecha_Ultima_Cargaviral->ViewValue = ew_FormatDateTime($this->Fecha_Ultima_Cargaviral->ViewValue, 5);
		$this->Fecha_Ultima_Cargaviral->ViewCustomAttributes = "";

		// Estado_Final
		if (strval($this->Estado_Final->CurrentValue) <> "") {
			$this->Estado_Final->ViewValue = $this->Estado_Final->OptionCaption($this->Estado_Final->CurrentValue);
		} else {
			$this->Estado_Final->ViewValue = NULL;
		}
		$this->Estado_Final->ViewCustomAttributes = "";

		// Cambio_Municipio
		if (strval($this->Cambio_Municipio->CurrentValue) <> "") {
			$sFilterWrk = "`Consecutivo`" . ew_SearchString("=", $this->Cambio_Municipio->CurrentValue, EW_DATATYPE_NUMBER, "");
		$sSqlWrk = "SELECT `Consecutivo`, `Si_No` AS `DispFld`, '' AS `Disp2Fld`, '' AS `Disp3Fld`, '' AS `Disp4Fld` FROM `si_no`";
		$sWhereWrk = "";
		ew_AddFilter($sWhereWrk, $sFilterWrk);
		$this->Lookup_Selecting($this->Cambio_Municipio, $sWhereWrk); // Call Lookup selecting
		if ($sWhereWrk <> "") $sSqlWrk .= " WHERE " . $sWhereWrk;
			$rswrk = Conn()->Execute($sSqlWrk);
			if ($rswrk && !$rswrk->EOF) { // Lookup values found
				$arwrk = array();
				$arwrk[1] = $rswrk->fields('DispFld');
				$this->Cambio_Municipio->ViewValue = $this->Cambio_Municipio->DisplayValue($arwrk);
				$rswrk->Close();
			} else {
				$this->Cambio_Municipio->ViewValue = $this->Cambio_Municipio->CurrentValue;
			}
		} else {
			$this->Cambio_Municipio->ViewValue = NULL;
		}
		$this->Cambio_Municipio->ViewCustomAttributes = "";

		// Observaciones
		$this->Observaciones->ViewValue = $this->Observaciones->CurrentValue;
		$this->Observaciones->ViewCustomAttributes = "";

		// Primer_Nombre
		$this->Primer_Nombre->ViewValue = $this->Primer_Nombre->CurrentValue;
		$this->Primer_Nombre->ViewCustomAttributes = "";

		// Segundo_Nombre
		$this->Segundo_Nombre->ViewValue = $this->Segundo_Nombre->CurrentValue;
		$this->Segundo_Nombre->ViewCustomAttributes = "";

		// Primer_Apellido
		$this->Primer_Apellido->ViewValue = $this->Primer_Apellido->CurrentValue;
		$this->Primer_Apellido->ViewCustomAttributes = "";

		// Segundo_Apellido
		$this->Segundo_Apellido->ViewValue = $this->Segundo_Apellido->CurrentValue;
		$this->Segundo_Apellido->ViewCustomAttributes = "";

		// Registrado_Por
		$this->Registrado_Por->ViewValue = $this->Registrado_Por->CurrentValue;
		$this->Registrado_Por->ViewCustomAttributes = "";

		// Documento
		$this->Documento->ViewValue = $this->Documento->CurrentValue;
		$this->Documento->ViewCustomAttributes = "";

		// Fecha_dig
		$this->Fecha_dig->ViewValue = $this->Fecha_dig->CurrentValue;
		$this->Fecha_dig->ViewValue = ew_FormatDateTime($this->Fecha_dig->ViewValue, 5);
		$this->Fecha_dig->ViewCustomAttributes = "";

			// Id
			$this->Id->LinkCustomAttributes = "";
			$this->Id->HrefValue = "";
			$this->Id->TooltipValue = "";

			// Id_Registro
			$this->Id_Registro->LinkCustomAttributes = "";
			$this->Id_Registro->HrefValue = "";
			$this->Id_Registro->TooltipValue = "";

			// Codigo_Eps
			$this->Codigo_Eps->LinkCustomAttributes = "";
			$this->Codigo_Eps->HrefValue = "";
			$this->Codigo_Eps->TooltipValue = "";

			// Tipo_regimen
			$this->Tipo_regimen->LinkCustomAttributes = "";
			$this->Tipo_regimen->HrefValue = "";
			$this->Tipo_regimen->TooltipValue = "";

			// Codigo_Ips
			$this->Codigo_Ips->LinkCustomAttributes = "";
			$this->Codigo_Ips->HrefValue = "";
			$this->Codigo_Ips->TooltipValue = "";

			// Fecha_Confirmatorio
			$this->Fecha_Confirmatorio->LinkCustomAttributes = "";
			$this->Fecha_Confirmatorio->HrefValue = "";
			$this->Fecha_Confirmatorio->TooltipValue = "";

			// Estadio
			$this->Estadio->LinkCustomAttributes = "";
			$this->Estadio->HrefValue = "";
			$this->Estadio->TooltipValue = "";

			// Numero_Carga Viral
			$this->Numero_Carga_Viral->LinkCustomAttributes = "";
			$this->Numero_Carga_Viral->HrefValue = "";
			$this->Numero_Carga_Viral->TooltipValue = "";

			// Fecha_Primera_Carga_Viral
			$this->Fecha_Primera_Carga_Viral->LinkCustomAttributes = "";
			$this->Fecha_Primera_Carga_Viral->HrefValue = "";
			$this->Fecha_Primera_Carga_Viral->TooltipValue = "";

			// CD4
			$this->CD4->LinkCustomAttributes = "";
			$this->CD4->HrefValue = "";
			$this->CD4->TooltipValue = "";

			// Fecha_CD4
			$this->Fecha_CD4->LinkCustomAttributes = "";
			$this->Fecha_CD4->HrefValue = "";
			$this->Fecha_CD4->TooltipValue = "";

			// adherencia
			$this->adherencia->LinkCustomAttributes = "";
			$this->adherencia->HrefValue = "";
			$this->adherencia->TooltipValue = "";

			// Tratamiento_Antirretroviral
			$this->Tratamiento_Antirretroviral->LinkCustomAttributes = "";
			$this->Tratamiento_Antirretroviral->HrefValue = "";
			$this->Tratamiento_Antirretroviral->TooltipValue = "";

			// Porque_No_Antirretroviral
			$this->Porque_No_Antirretroviral->LinkCustomAttributes = "";
			$this->Porque_No_Antirretroviral->HrefValue = "";
			$this->Porque_No_Antirretroviral->TooltipValue = "";

			// Porque_no_adherente
			$this->Porque_no_adherente->LinkCustomAttributes = "";
			$this->Porque_no_adherente->HrefValue = "";
			$this->Porque_no_adherente->TooltipValue = "";

			// Valor_ultima_cargaviral
			$this->Valor_ultima_cargaviral->LinkCustomAttributes = "";
			$this->Valor_ultima_cargaviral->HrefValue = "";
			$this->Valor_ultima_cargaviral->TooltipValue = "";

			// Fecha_Ultima_Cargaviral
			$this->Fecha_Ultima_Cargaviral->LinkCustomAttributes = "";
			$this->Fecha_Ultima_Cargaviral->HrefValue = "";
			$this->Fecha_Ultima_Cargaviral->TooltipValue = "";

			// Estado_Final
			$this->Estado_Final->LinkCustomAttributes = "";
			$this->Estado_Final->HrefValue = "";
			$this->Estado_Final->TooltipValue = "";

			// Cambio_Municipio
			$this->Cambio_Municipio->LinkCustomAttributes = "";
			$this->Cambio_Municipio->HrefValue = "";
			$this->Cambio_Municipio->TooltipValue = "";

			// Fecha_dig
			$this->Fecha_dig->LinkCustomAttributes = "";
			$this->Fecha_dig->HrefValue = "";
			$this->Fecha_dig->TooltipValue = "";
		} elseif ($this->RowType == EW_ROWTYPE_ADD) { // Add row

			// Id
			// Id_Registro

			$this->Id_Registro->EditAttrs["class"] = "form-control";
			$this->Id_Registro->EditCustomAttributes = "";
			if ($this->Id_Registro->getSessionValue() <> "") {
				$this->Id_Registro->CurrentValue = $this->Id_Registro->getSessionValue();
				$this->Id_Registro->OldValue = $this->Id_Registro->CurrentValue;
			if (strval($this->Id_Registro->CurrentValue) <> "") {
				$sFilterWrk = "`Consecutivo`" . ew_SearchString("=", $this->Id_Registro->CurrentValue, EW_DATATYPE_NUMBER, "");
			$sSqlWrk = "SELECT `Consecutivo`, `Primer_Nombre` AS `DispFld`, `Segundo_Nombre` AS `Disp2Fld`, `Primer_Apellido` AS `Disp3Fld`, `Numero_Identificacion` AS `Disp4Fld` FROM `registro`";
			$sWhereWrk = "";
			ew_AddFilter($sWhereWrk, $sFilterWrk);
			$this->Lookup_Selecting($this->Id_Registro, $sWhereWrk); // Call Lookup selecting
			if ($sWhereWrk <> "") $sSqlWrk .= " WHERE " . $sWhereWrk;
				$rswrk = Conn()->Execute($sSqlWrk);
				if ($rswrk && !$rswrk->EOF) { // Lookup values found
					$arwrk = array();
					$arwrk[1] = $rswrk->fields('DispFld');
					$arwrk[2] = $rswrk->fields('Disp2Fld');
					$arwrk[3] = $rswrk->fields('Disp3Fld');
					$arwrk[4] = $rswrk->fields('Disp4Fld');
					$this->Id_Registro->ViewValue = $this->Id_Registro->DisplayValue($arwrk);
					$rswrk->Close();
				} else {
					$this->Id_Registro->ViewValue = $this->Id_Registro->CurrentValue;
				}
			} else {
				$this->Id_Registro->ViewValue = NULL;
			}
			$this->Id_Registro->ViewCustomAttributes = "";
			} else {
			if (trim(strval($this->Id_Registro->CurrentValue)) == "") {
				$sFilterWrk = "0=1";
			} else {
				$sFilterWrk = "`Consecutivo`" . ew_SearchString("=", $this->Id_Registro->CurrentValue, EW_DATATYPE_NUMBER, "");
			}
			$sSqlWrk = "SELECT `Consecutivo`, `Primer_Nombre` AS `DispFld`, `Segundo_Nombre` AS `Disp2Fld`, `Primer_Apellido` AS `Disp3Fld`, `Numero_Identificacion` AS `Disp4Fld`, '' AS `SelectFilterFld`, '' AS `SelectFilterFld2`, '' AS `SelectFilterFld3`, '' AS `SelectFilterFld4` FROM `registro`";
			$sWhereWrk = "";
			ew_AddFilter($sWhereWrk, $sFilterWrk);
			if (!$GLOBALS["seguimiento_individual3"]->UserIDAllow("gridcls")) $sWhereWrk = $GLOBALS["registro"]->AddUserIDFilter($sWhereWrk);
			$this->Lookup_Selecting($this->Id_Registro, $sWhereWrk); // Call Lookup selecting
			if ($sWhereWrk <> "") $sSqlWrk .= " WHERE " . $sWhereWrk;
			$rswrk = Conn()->Execute($sSqlWrk);
			$arwrk = ($rswrk) ? $rswrk->GetRows() : array();
			if ($rswrk) $rswrk->Close();
			array_unshift($arwrk, array("", $Language->Phrase("PleaseSelect"), "", "", "", "", "", "", ""));
			$this->Id_Registro->EditValue = $arwrk;
			}

			// Codigo_Eps
			$this->Codigo_Eps->EditAttrs["class"] = "form-control";
			$this->Codigo_Eps->EditCustomAttributes = "";
			$this->Codigo_Eps->EditValue = ew_HtmlEncode($this->Codigo_Eps->CurrentValue);
			if (strval($this->Codigo_Eps->CurrentValue) <> "") {
				$sFilterWrk = "`Codigo`" . ew_SearchString("=", $this->Codigo_Eps->CurrentValue, EW_DATATYPE_STRING, "");
			$sSqlWrk = "SELECT `Codigo`, `Codigo` AS `DispFld`, `Administradora_Salud` AS `Disp2Fld`, '' AS `Disp3Fld`, '' AS `Disp4Fld` FROM `administradora_salud`";
			$sWhereWrk = "";
			ew_AddFilter($sWhereWrk, $sFilterWrk);
			$this->Lookup_Selecting($this->Codigo_Eps, $sWhereWrk); // Call Lookup selecting
			if ($sWhereWrk <> "") $sSqlWrk .= " WHERE " . $sWhereWrk;
				$rswrk = Conn()->Execute($sSqlWrk);
				if ($rswrk && !$rswrk->EOF) { // Lookup values found
					$arwrk = array();
					$arwrk[1] = ew_HtmlEncode($rswrk->fields('DispFld'));
					$arwrk[2] = ew_HtmlEncode($rswrk->fields('Disp2Fld'));
					$this->Codigo_Eps->EditValue = $this->Codigo_Eps->DisplayValue($arwrk);
					$rswrk->Close();
				} else {
					$this->Codigo_Eps->EditValue = ew_HtmlEncode($this->Codigo_Eps->CurrentValue);
				}
			} else {
				$this->Codigo_Eps->EditValue = NULL;
			}
			$this->Codigo_Eps->PlaceHolder = ew_RemoveHtml($this->Codigo_Eps->FldCaption());

			// Tipo_regimen
			$this->Tipo_regimen->EditCustomAttributes = "";
			if (trim(strval($this->Tipo_regimen->CurrentValue)) == "") {
				$sFilterWrk = "0=1";
			} else {
				$sFilterWrk = "`Codigo_Regimen`" . ew_SearchString("=", $this->Tipo_regimen->CurrentValue, EW_DATATYPE_STRING, "");
			}
			$sSqlWrk = "SELECT `Codigo_Regimen`, `Regimen_Salud` AS `DispFld`, '' AS `Disp2Fld`, '' AS `Disp3Fld`, '' AS `Disp4Fld`, '' AS `SelectFilterFld`, '' AS `SelectFilterFld2`, '' AS `SelectFilterFld3`, '' AS `SelectFilterFld4` FROM `tipo_regimen_salud`";
			$sWhereWrk = "";
			ew_AddFilter($sWhereWrk, $sFilterWrk);
			$this->Lookup_Selecting($this->Tipo_regimen, $sWhereWrk); // Call Lookup selecting
			if ($sWhereWrk <> "") $sSqlWrk .= " WHERE " . $sWhereWrk;
			$rswrk = Conn()->Execute($sSqlWrk);
			$arwrk = ($rswrk) ? $rswrk->GetRows() : array();
			if ($rswrk) $rswrk->Close();
			$this->Tipo_regimen->EditValue = $arwrk;

			// Codigo_Ips
			$this->Codigo_Ips->EditAttrs["class"] = "form-control";
			$this->Codigo_Ips->EditCustomAttributes = "";
			$this->Codigo_Ips->EditValue = ew_HtmlEncode($this->Codigo_Ips->CurrentValue);
			if (strval($this->Codigo_Ips->CurrentValue) <> "") {
				$sFilterWrk = "`Codigo_Upgd`" . ew_SearchString("=", $this->Codigo_Ips->CurrentValue, EW_DATATYPE_STRING, "");
			$sSqlWrk = "SELECT `Codigo_Upgd`, `Codigo_Upgd` AS `DispFld`, `Upgd` AS `Disp2Fld`, '' AS `Disp3Fld`, '' AS `Disp4Fld` FROM `entidad_upgd`";
			$sWhereWrk = "";
			ew_AddFilter($sWhereWrk, $sFilterWrk);
			$this->Lookup_Selecting($this->Codigo_Ips, $sWhereWrk); // Call Lookup selecting
			if ($sWhereWrk <> "") $sSqlWrk .= " WHERE " . $sWhereWrk;
				$rswrk = Conn()->Execute($sSqlWrk);
				if ($rswrk && !$rswrk->EOF) { // Lookup values found
					$arwrk = array();
					$arwrk[1] = ew_HtmlEncode($rswrk->fields('DispFld'));
					$arwrk[2] = ew_HtmlEncode($rswrk->fields('Disp2Fld'));
					$this->Codigo_Ips->EditValue = $this->Codigo_Ips->DisplayValue($arwrk);
					$rswrk->Close();
				} else {
					$this->Codigo_Ips->EditValue = ew_HtmlEncode($this->Codigo_Ips->CurrentValue);
				}
			} else {
				$this->Codigo_Ips->EditValue = NULL;
			}
			$this->Codigo_Ips->PlaceHolder = ew_RemoveHtml($this->Codigo_Ips->FldCaption());

			// Fecha_Confirmatorio
			$this->Fecha_Confirmatorio->EditAttrs["class"] = "form-control";
			$this->Fecha_Confirmatorio->EditCustomAttributes = "";
			$this->Fecha_Confirmatorio->EditValue = ew_HtmlEncode(ew_FormatDateTime($this->Fecha_Confirmatorio->CurrentValue, 5));
			$this->Fecha_Confirmatorio->PlaceHolder = ew_RemoveHtml($this->Fecha_Confirmatorio->FldCaption());

			// Estadio
			$this->Estadio->EditAttrs["class"] = "form-control";
			$this->Estadio->EditCustomAttributes = "";
			if (trim(strval($this->Estadio->CurrentValue)) == "") {
				$sFilterWrk = "0=1";
			} else {
				$sFilterWrk = "`Id`" . ew_SearchString("=", $this->Estadio->CurrentValue, EW_DATATYPE_NUMBER, "");
			}
			$sSqlWrk = "SELECT `Id`, `Estadio` AS `DispFld`, '' AS `Disp2Fld`, '' AS `Disp3Fld`, '' AS `Disp4Fld`, '' AS `SelectFilterFld`, '' AS `SelectFilterFld2`, '' AS `SelectFilterFld3`, '' AS `SelectFilterFld4` FROM `estadio`";
			$sWhereWrk = "";
			ew_AddFilter($sWhereWrk, $sFilterWrk);
			$this->Lookup_Selecting($this->Estadio, $sWhereWrk); // Call Lookup selecting
			if ($sWhereWrk <> "") $sSqlWrk .= " WHERE " . $sWhereWrk;
			$rswrk = Conn()->Execute($sSqlWrk);
			$arwrk = ($rswrk) ? $rswrk->GetRows() : array();
			if ($rswrk) $rswrk->Close();
			array_unshift($arwrk, array("", $Language->Phrase("PleaseSelect"), "", "", "", "", "", "", ""));
			$this->Estadio->EditValue = $arwrk;

			// Numero_Carga Viral
			$this->Numero_Carga_Viral->EditAttrs["class"] = "form-control";
			$this->Numero_Carga_Viral->EditCustomAttributes = "";
			if (trim(strval($this->Numero_Carga_Viral->CurrentValue)) == "") {
				$sFilterWrk = "0=1";
			} else {
				$sFilterWrk = "`Carga_viral`" . ew_SearchString("=", $this->Numero_Carga_Viral->CurrentValue, EW_DATATYPE_STRING, "");
			}
			$sSqlWrk = "SELECT `Carga_viral`, `Carga_viral` AS `DispFld`, '' AS `Disp2Fld`, '' AS `Disp3Fld`, '' AS `Disp4Fld`, '' AS `SelectFilterFld`, '' AS `SelectFilterFld2`, '' AS `SelectFilterFld3`, '' AS `SelectFilterFld4` FROM `carga_viral`";
			$sWhereWrk = "";
			ew_AddFilter($sWhereWrk, $sFilterWrk);
			$this->Lookup_Selecting($this->Numero_Carga_Viral, $sWhereWrk); // Call Lookup selecting
			if ($sWhereWrk <> "") $sSqlWrk .= " WHERE " . $sWhereWrk;
			$rswrk = Conn()->Execute($sSqlWrk);
			$arwrk = ($rswrk) ? $rswrk->GetRows() : array();
			if ($rswrk) $rswrk->Close();
			array_unshift($arwrk, array("", $Language->Phrase("PleaseSelect"), "", "", "", "", "", "", ""));
			$this->Numero_Carga_Viral->EditValue = $arwrk;

			// Fecha_Primera_Carga_Viral
			$this->Fecha_Primera_Carga_Viral->EditAttrs["class"] = "form-control";
			$this->Fecha_Primera_Carga_Viral->EditCustomAttributes = "";
			$this->Fecha_Primera_Carga_Viral->EditValue = ew_HtmlEncode(ew_FormatDateTime($this->Fecha_Primera_Carga_Viral->CurrentValue, 5));
			$this->Fecha_Primera_Carga_Viral->PlaceHolder = ew_RemoveHtml($this->Fecha_Primera_Carga_Viral->FldCaption());

			// CD4
			$this->CD4->EditAttrs["class"] = "form-control";
			$this->CD4->EditCustomAttributes = "";
			$this->CD4->EditValue = ew_HtmlEncode($this->CD4->CurrentValue);
			$this->CD4->PlaceHolder = ew_RemoveHtml($this->CD4->FldCaption());

			// Fecha_CD4
			$this->Fecha_CD4->EditAttrs["class"] = "form-control";
			$this->Fecha_CD4->EditCustomAttributes = "";
			$this->Fecha_CD4->EditValue = ew_HtmlEncode(ew_FormatDateTime($this->Fecha_CD4->CurrentValue, 5));
			$this->Fecha_CD4->PlaceHolder = ew_RemoveHtml($this->Fecha_CD4->FldCaption());

			// adherencia
			$this->adherencia->EditAttrs["class"] = "form-control";
			$this->adherencia->EditCustomAttributes = "";
			if (trim(strval($this->adherencia->CurrentValue)) == "") {
				$sFilterWrk = "0=1";
			} else {
				$sFilterWrk = "`id`" . ew_SearchString("=", $this->adherencia->CurrentValue, EW_DATATYPE_NUMBER, "");
			}
			$sSqlWrk = "SELECT `id`, `adherencia` AS `DispFld`, '' AS `Disp2Fld`, '' AS `Disp3Fld`, '' AS `Disp4Fld`, '' AS `SelectFilterFld`, '' AS `SelectFilterFld2`, '' AS `SelectFilterFld3`, '' AS `SelectFilterFld4` FROM `adherencia`";
			$sWhereWrk = "";
			ew_AddFilter($sWhereWrk, $sFilterWrk);
			$this->Lookup_Selecting($this->adherencia, $sWhereWrk); // Call Lookup selecting
			if ($sWhereWrk <> "") $sSqlWrk .= " WHERE " . $sWhereWrk;
			$rswrk = Conn()->Execute($sSqlWrk);
			$arwrk = ($rswrk) ? $rswrk->GetRows() : array();
			if ($rswrk) $rswrk->Close();
			array_unshift($arwrk, array("", $Language->Phrase("PleaseSelect"), "", "", "", "", "", "", ""));
			$this->adherencia->EditValue = $arwrk;

			// Tratamiento_Antirretroviral
			$this->Tratamiento_Antirretroviral->EditCustomAttributes = "";
			if (trim(strval($this->Tratamiento_Antirretroviral->CurrentValue)) == "") {
				$sFilterWrk = "0=1";
			} else {
				$sFilterWrk = "`Consecutivo`" . ew_SearchString("=", $this->Tratamiento_Antirretroviral->CurrentValue, EW_DATATYPE_NUMBER, "");
			}
			$sSqlWrk = "SELECT `Consecutivo`, `Si_No` AS `DispFld`, '' AS `Disp2Fld`, '' AS `Disp3Fld`, '' AS `Disp4Fld`, '' AS `SelectFilterFld`, '' AS `SelectFilterFld2`, '' AS `SelectFilterFld3`, '' AS `SelectFilterFld4` FROM `si_no`";
			$sWhereWrk = "";
			ew_AddFilter($sWhereWrk, $sFilterWrk);
			$this->Lookup_Selecting($this->Tratamiento_Antirretroviral, $sWhereWrk); // Call Lookup selecting
			if ($sWhereWrk <> "") $sSqlWrk .= " WHERE " . $sWhereWrk;
			$rswrk = Conn()->Execute($sSqlWrk);
			$arwrk = ($rswrk) ? $rswrk->GetRows() : array();
			if ($rswrk) $rswrk->Close();
			$this->Tratamiento_Antirretroviral->EditValue = $arwrk;

			// Porque_No_Antirretroviral
			$this->Porque_No_Antirretroviral->EditCustomAttributes = "";
			$this->Porque_No_Antirretroviral->EditValue = $this->Porque_No_Antirretroviral->Options(FALSE);

			// Porque_no_adherente
			$this->Porque_no_adherente->EditCustomAttributes = "";
			$this->Porque_no_adherente->EditValue = $this->Porque_no_adherente->Options(FALSE);

			// Valor_ultima_cargaviral
			$this->Valor_ultima_cargaviral->EditAttrs["class"] = "form-control";
			$this->Valor_ultima_cargaviral->EditCustomAttributes = "";
			if (trim(strval($this->Valor_ultima_cargaviral->CurrentValue)) == "") {
				$sFilterWrk = "0=1";
			} else {
				$sFilterWrk = "`Carga_viral`" . ew_SearchString("=", $this->Valor_ultima_cargaviral->CurrentValue, EW_DATATYPE_STRING, "");
			}
			$sSqlWrk = "SELECT `Carga_viral`, `Carga_viral` AS `DispFld`, '' AS `Disp2Fld`, '' AS `Disp3Fld`, '' AS `Disp4Fld`, '' AS `SelectFilterFld`, '' AS `SelectFilterFld2`, '' AS `SelectFilterFld3`, '' AS `SelectFilterFld4` FROM `carga_viral`";
			$sWhereWrk = "";
			ew_AddFilter($sWhereWrk, $sFilterWrk);
			$this->Lookup_Selecting($this->Valor_ultima_cargaviral, $sWhereWrk); // Call Lookup selecting
			if ($sWhereWrk <> "") $sSqlWrk .= " WHERE " . $sWhereWrk;
			$rswrk = Conn()->Execute($sSqlWrk);
			$arwrk = ($rswrk) ? $rswrk->GetRows() : array();
			if ($rswrk) $rswrk->Close();
			array_unshift($arwrk, array("", $Language->Phrase("PleaseSelect"), "", "", "", "", "", "", ""));
			$this->Valor_ultima_cargaviral->EditValue = $arwrk;

			// Fecha_Ultima_Cargaviral
			$this->Fecha_Ultima_Cargaviral->EditAttrs["class"] = "form-control";
			$this->Fecha_Ultima_Cargaviral->EditCustomAttributes = "";
			$this->Fecha_Ultima_Cargaviral->EditValue = ew_HtmlEncode(ew_FormatDateTime($this->Fecha_Ultima_Cargaviral->CurrentValue, 5));
			$this->Fecha_Ultima_Cargaviral->PlaceHolder = ew_RemoveHtml($this->Fecha_Ultima_Cargaviral->FldCaption());

			// Estado_Final
			$this->Estado_Final->EditCustomAttributes = "";
			$this->Estado_Final->EditValue = $this->Estado_Final->Options(FALSE);

			// Cambio_Municipio
			$this->Cambio_Municipio->EditCustomAttributes = "";
			if (trim(strval($this->Cambio_Municipio->CurrentValue)) == "") {
				$sFilterWrk = "0=1";
			} else {
				$sFilterWrk = "`Consecutivo`" . ew_SearchString("=", $this->Cambio_Municipio->CurrentValue, EW_DATATYPE_NUMBER, "");
			}
			$sSqlWrk = "SELECT `Consecutivo`, `Si_No` AS `DispFld`, '' AS `Disp2Fld`, '' AS `Disp3Fld`, '' AS `Disp4Fld`, '' AS `SelectFilterFld`, '' AS `SelectFilterFld2`, '' AS `SelectFilterFld3`, '' AS `SelectFilterFld4` FROM `si_no`";
			$sWhereWrk = "";
			ew_AddFilter($sWhereWrk, $sFilterWrk);
			$this->Lookup_Selecting($this->Cambio_Municipio, $sWhereWrk); // Call Lookup selecting
			if ($sWhereWrk <> "") $sSqlWrk .= " WHERE " . $sWhereWrk;
			$rswrk = Conn()->Execute($sSqlWrk);
			$arwrk = ($rswrk) ? $rswrk->GetRows() : array();
			if ($rswrk) $rswrk->Close();
			$this->Cambio_Municipio->EditValue = $arwrk;

			// Fecha_dig
			// Add refer script
			// Id

			$this->Id->LinkCustomAttributes = "";
			$this->Id->HrefValue = "";

			// Id_Registro
			$this->Id_Registro->LinkCustomAttributes = "";
			$this->Id_Registro->HrefValue = "";

			// Codigo_Eps
			$this->Codigo_Eps->LinkCustomAttributes = "";
			$this->Codigo_Eps->HrefValue = "";

			// Tipo_regimen
			$this->Tipo_regimen->LinkCustomAttributes = "";
			$this->Tipo_regimen->HrefValue = "";

			// Codigo_Ips
			$this->Codigo_Ips->LinkCustomAttributes = "";
			$this->Codigo_Ips->HrefValue = "";

			// Fecha_Confirmatorio
			$this->Fecha_Confirmatorio->LinkCustomAttributes = "";
			$this->Fecha_Confirmatorio->HrefValue = "";

			// Estadio
			$this->Estadio->LinkCustomAttributes = "";
			$this->Estadio->HrefValue = "";

			// Numero_Carga Viral
			$this->Numero_Carga_Viral->LinkCustomAttributes = "";
			$this->Numero_Carga_Viral->HrefValue = "";

			// Fecha_Primera_Carga_Viral
			$this->Fecha_Primera_Carga_Viral->LinkCustomAttributes = "";
			$this->Fecha_Primera_Carga_Viral->HrefValue = "";

			// CD4
			$this->CD4->LinkCustomAttributes = "";
			$this->CD4->HrefValue = "";

			// Fecha_CD4
			$this->Fecha_CD4->LinkCustomAttributes = "";
			$this->Fecha_CD4->HrefValue = "";

			// adherencia
			$this->adherencia->LinkCustomAttributes = "";
			$this->adherencia->HrefValue = "";

			// Tratamiento_Antirretroviral
			$this->Tratamiento_Antirretroviral->LinkCustomAttributes = "";
			$this->Tratamiento_Antirretroviral->HrefValue = "";

			// Porque_No_Antirretroviral
			$this->Porque_No_Antirretroviral->LinkCustomAttributes = "";
			$this->Porque_No_Antirretroviral->HrefValue = "";

			// Porque_no_adherente
			$this->Porque_no_adherente->LinkCustomAttributes = "";
			$this->Porque_no_adherente->HrefValue = "";

			// Valor_ultima_cargaviral
			$this->Valor_ultima_cargaviral->LinkCustomAttributes = "";
			$this->Valor_ultima_cargaviral->HrefValue = "";

			// Fecha_Ultima_Cargaviral
			$this->Fecha_Ultima_Cargaviral->LinkCustomAttributes = "";
			$this->Fecha_Ultima_Cargaviral->HrefValue = "";

			// Estado_Final
			$this->Estado_Final->LinkCustomAttributes = "";
			$this->Estado_Final->HrefValue = "";

			// Cambio_Municipio
			$this->Cambio_Municipio->LinkCustomAttributes = "";
			$this->Cambio_Municipio->HrefValue = "";

			// Fecha_dig
			$this->Fecha_dig->LinkCustomAttributes = "";
			$this->Fecha_dig->HrefValue = "";
		} elseif ($this->RowType == EW_ROWTYPE_EDIT) { // Edit row

			// Id
			$this->Id->EditAttrs["class"] = "form-control";
			$this->Id->EditCustomAttributes = "";
			$this->Id->EditValue = $this->Id->CurrentValue;
			$this->Id->ViewCustomAttributes = "";

			// Id_Registro
			$this->Id_Registro->EditAttrs["class"] = "form-control";
			$this->Id_Registro->EditCustomAttributes = "";
			if ($this->Id_Registro->getSessionValue() <> "") {
				$this->Id_Registro->CurrentValue = $this->Id_Registro->getSessionValue();
				$this->Id_Registro->OldValue = $this->Id_Registro->CurrentValue;
			if (strval($this->Id_Registro->CurrentValue) <> "") {
				$sFilterWrk = "`Consecutivo`" . ew_SearchString("=", $this->Id_Registro->CurrentValue, EW_DATATYPE_NUMBER, "");
			$sSqlWrk = "SELECT `Consecutivo`, `Primer_Nombre` AS `DispFld`, `Segundo_Nombre` AS `Disp2Fld`, `Primer_Apellido` AS `Disp3Fld`, `Numero_Identificacion` AS `Disp4Fld` FROM `registro`";
			$sWhereWrk = "";
			ew_AddFilter($sWhereWrk, $sFilterWrk);
			$this->Lookup_Selecting($this->Id_Registro, $sWhereWrk); // Call Lookup selecting
			if ($sWhereWrk <> "") $sSqlWrk .= " WHERE " . $sWhereWrk;
				$rswrk = Conn()->Execute($sSqlWrk);
				if ($rswrk && !$rswrk->EOF) { // Lookup values found
					$arwrk = array();
					$arwrk[1] = $rswrk->fields('DispFld');
					$arwrk[2] = $rswrk->fields('Disp2Fld');
					$arwrk[3] = $rswrk->fields('Disp3Fld');
					$arwrk[4] = $rswrk->fields('Disp4Fld');
					$this->Id_Registro->ViewValue = $this->Id_Registro->DisplayValue($arwrk);
					$rswrk->Close();
				} else {
					$this->Id_Registro->ViewValue = $this->Id_Registro->CurrentValue;
				}
			} else {
				$this->Id_Registro->ViewValue = NULL;
			}
			$this->Id_Registro->ViewCustomAttributes = "";
			} else {
			if (trim(strval($this->Id_Registro->CurrentValue)) == "") {
				$sFilterWrk = "0=1";
			} else {
				$sFilterWrk = "`Consecutivo`" . ew_SearchString("=", $this->Id_Registro->CurrentValue, EW_DATATYPE_NUMBER, "");
			}
			$sSqlWrk = "SELECT `Consecutivo`, `Primer_Nombre` AS `DispFld`, `Segundo_Nombre` AS `Disp2Fld`, `Primer_Apellido` AS `Disp3Fld`, `Numero_Identificacion` AS `Disp4Fld`, '' AS `SelectFilterFld`, '' AS `SelectFilterFld2`, '' AS `SelectFilterFld3`, '' AS `SelectFilterFld4` FROM `registro`";
			$sWhereWrk = "";
			ew_AddFilter($sWhereWrk, $sFilterWrk);
			if (!$GLOBALS["seguimiento_individual3"]->UserIDAllow("gridcls")) $sWhereWrk = $GLOBALS["registro"]->AddUserIDFilter($sWhereWrk);
			$this->Lookup_Selecting($this->Id_Registro, $sWhereWrk); // Call Lookup selecting
			if ($sWhereWrk <> "") $sSqlWrk .= " WHERE " . $sWhereWrk;
			$rswrk = Conn()->Execute($sSqlWrk);
			$arwrk = ($rswrk) ? $rswrk->GetRows() : array();
			if ($rswrk) $rswrk->Close();
			array_unshift($arwrk, array("", $Language->Phrase("PleaseSelect"), "", "", "", "", "", "", ""));
			$this->Id_Registro->EditValue = $arwrk;
			}

			// Codigo_Eps
			$this->Codigo_Eps->EditAttrs["class"] = "form-control";
			$this->Codigo_Eps->EditCustomAttributes = "";
			$this->Codigo_Eps->EditValue = ew_HtmlEncode($this->Codigo_Eps->CurrentValue);
			if (strval($this->Codigo_Eps->CurrentValue) <> "") {
				$sFilterWrk = "`Codigo`" . ew_SearchString("=", $this->Codigo_Eps->CurrentValue, EW_DATATYPE_STRING, "");
			$sSqlWrk = "SELECT `Codigo`, `Codigo` AS `DispFld`, `Administradora_Salud` AS `Disp2Fld`, '' AS `Disp3Fld`, '' AS `Disp4Fld` FROM `administradora_salud`";
			$sWhereWrk = "";
			ew_AddFilter($sWhereWrk, $sFilterWrk);
			$this->Lookup_Selecting($this->Codigo_Eps, $sWhereWrk); // Call Lookup selecting
			if ($sWhereWrk <> "") $sSqlWrk .= " WHERE " . $sWhereWrk;
				$rswrk = Conn()->Execute($sSqlWrk);
				if ($rswrk && !$rswrk->EOF) { // Lookup values found
					$arwrk = array();
					$arwrk[1] = ew_HtmlEncode($rswrk->fields('DispFld'));
					$arwrk[2] = ew_HtmlEncode($rswrk->fields('Disp2Fld'));
					$this->Codigo_Eps->EditValue = $this->Codigo_Eps->DisplayValue($arwrk);
					$rswrk->Close();
				} else {
					$this->Codigo_Eps->EditValue = ew_HtmlEncode($this->Codigo_Eps->CurrentValue);
				}
			} else {
				$this->Codigo_Eps->EditValue = NULL;
			}
			$this->Codigo_Eps->PlaceHolder = ew_RemoveHtml($this->Codigo_Eps->FldCaption());

			// Tipo_regimen
			$this->Tipo_regimen->EditCustomAttributes = "";
			if (trim(strval($this->Tipo_regimen->CurrentValue)) == "") {
				$sFilterWrk = "0=1";
			} else {
				$sFilterWrk = "`Codigo_Regimen`" . ew_SearchString("=", $this->Tipo_regimen->CurrentValue, EW_DATATYPE_STRING, "");
			}
			$sSqlWrk = "SELECT `Codigo_Regimen`, `Regimen_Salud` AS `DispFld`, '' AS `Disp2Fld`, '' AS `Disp3Fld`, '' AS `Disp4Fld`, '' AS `SelectFilterFld`, '' AS `SelectFilterFld2`, '' AS `SelectFilterFld3`, '' AS `SelectFilterFld4` FROM `tipo_regimen_salud`";
			$sWhereWrk = "";
			ew_AddFilter($sWhereWrk, $sFilterWrk);
			$this->Lookup_Selecting($this->Tipo_regimen, $sWhereWrk); // Call Lookup selecting
			if ($sWhereWrk <> "") $sSqlWrk .= " WHERE " . $sWhereWrk;
			$rswrk = Conn()->Execute($sSqlWrk);
			$arwrk = ($rswrk) ? $rswrk->GetRows() : array();
			if ($rswrk) $rswrk->Close();
			$this->Tipo_regimen->EditValue = $arwrk;

			// Codigo_Ips
			$this->Codigo_Ips->EditAttrs["class"] = "form-control";
			$this->Codigo_Ips->EditCustomAttributes = "";
			$this->Codigo_Ips->EditValue = ew_HtmlEncode($this->Codigo_Ips->CurrentValue);
			if (strval($this->Codigo_Ips->CurrentValue) <> "") {
				$sFilterWrk = "`Codigo_Upgd`" . ew_SearchString("=", $this->Codigo_Ips->CurrentValue, EW_DATATYPE_STRING, "");
			$sSqlWrk = "SELECT `Codigo_Upgd`, `Codigo_Upgd` AS `DispFld`, `Upgd` AS `Disp2Fld`, '' AS `Disp3Fld`, '' AS `Disp4Fld` FROM `entidad_upgd`";
			$sWhereWrk = "";
			ew_AddFilter($sWhereWrk, $sFilterWrk);
			$this->Lookup_Selecting($this->Codigo_Ips, $sWhereWrk); // Call Lookup selecting
			if ($sWhereWrk <> "") $sSqlWrk .= " WHERE " . $sWhereWrk;
				$rswrk = Conn()->Execute($sSqlWrk);
				if ($rswrk && !$rswrk->EOF) { // Lookup values found
					$arwrk = array();
					$arwrk[1] = ew_HtmlEncode($rswrk->fields('DispFld'));
					$arwrk[2] = ew_HtmlEncode($rswrk->fields('Disp2Fld'));
					$this->Codigo_Ips->EditValue = $this->Codigo_Ips->DisplayValue($arwrk);
					$rswrk->Close();
				} else {
					$this->Codigo_Ips->EditValue = ew_HtmlEncode($this->Codigo_Ips->CurrentValue);
				}
			} else {
				$this->Codigo_Ips->EditValue = NULL;
			}
			$this->Codigo_Ips->PlaceHolder = ew_RemoveHtml($this->Codigo_Ips->FldCaption());

			// Fecha_Confirmatorio
			$this->Fecha_Confirmatorio->EditAttrs["class"] = "form-control";
			$this->Fecha_Confirmatorio->EditCustomAttributes = "";
			$this->Fecha_Confirmatorio->EditValue = ew_HtmlEncode(ew_FormatDateTime($this->Fecha_Confirmatorio->CurrentValue, 5));
			$this->Fecha_Confirmatorio->PlaceHolder = ew_RemoveHtml($this->Fecha_Confirmatorio->FldCaption());

			// Estadio
			$this->Estadio->EditAttrs["class"] = "form-control";
			$this->Estadio->EditCustomAttributes = "";
			if (trim(strval($this->Estadio->CurrentValue)) == "") {
				$sFilterWrk = "0=1";
			} else {
				$sFilterWrk = "`Id`" . ew_SearchString("=", $this->Estadio->CurrentValue, EW_DATATYPE_NUMBER, "");
			}
			$sSqlWrk = "SELECT `Id`, `Estadio` AS `DispFld`, '' AS `Disp2Fld`, '' AS `Disp3Fld`, '' AS `Disp4Fld`, '' AS `SelectFilterFld`, '' AS `SelectFilterFld2`, '' AS `SelectFilterFld3`, '' AS `SelectFilterFld4` FROM `estadio`";
			$sWhereWrk = "";
			ew_AddFilter($sWhereWrk, $sFilterWrk);
			$this->Lookup_Selecting($this->Estadio, $sWhereWrk); // Call Lookup selecting
			if ($sWhereWrk <> "") $sSqlWrk .= " WHERE " . $sWhereWrk;
			$rswrk = Conn()->Execute($sSqlWrk);
			$arwrk = ($rswrk) ? $rswrk->GetRows() : array();
			if ($rswrk) $rswrk->Close();
			array_unshift($arwrk, array("", $Language->Phrase("PleaseSelect"), "", "", "", "", "", "", ""));
			$this->Estadio->EditValue = $arwrk;

			// Numero_Carga Viral
			$this->Numero_Carga_Viral->EditAttrs["class"] = "form-control";
			$this->Numero_Carga_Viral->EditCustomAttributes = "";
			if (trim(strval($this->Numero_Carga_Viral->CurrentValue)) == "") {
				$sFilterWrk = "0=1";
			} else {
				$sFilterWrk = "`Carga_viral`" . ew_SearchString("=", $this->Numero_Carga_Viral->CurrentValue, EW_DATATYPE_STRING, "");
			}
			$sSqlWrk = "SELECT `Carga_viral`, `Carga_viral` AS `DispFld`, '' AS `Disp2Fld`, '' AS `Disp3Fld`, '' AS `Disp4Fld`, '' AS `SelectFilterFld`, '' AS `SelectFilterFld2`, '' AS `SelectFilterFld3`, '' AS `SelectFilterFld4` FROM `carga_viral`";
			$sWhereWrk = "";
			ew_AddFilter($sWhereWrk, $sFilterWrk);
			$this->Lookup_Selecting($this->Numero_Carga_Viral, $sWhereWrk); // Call Lookup selecting
			if ($sWhereWrk <> "") $sSqlWrk .= " WHERE " . $sWhereWrk;
			$rswrk = Conn()->Execute($sSqlWrk);
			$arwrk = ($rswrk) ? $rswrk->GetRows() : array();
			if ($rswrk) $rswrk->Close();
			array_unshift($arwrk, array("", $Language->Phrase("PleaseSelect"), "", "", "", "", "", "", ""));
			$this->Numero_Carga_Viral->EditValue = $arwrk;

			// Fecha_Primera_Carga_Viral
			$this->Fecha_Primera_Carga_Viral->EditAttrs["class"] = "form-control";
			$this->Fecha_Primera_Carga_Viral->EditCustomAttributes = "";
			$this->Fecha_Primera_Carga_Viral->EditValue = ew_HtmlEncode(ew_FormatDateTime($this->Fecha_Primera_Carga_Viral->CurrentValue, 5));
			$this->Fecha_Primera_Carga_Viral->PlaceHolder = ew_RemoveHtml($this->Fecha_Primera_Carga_Viral->FldCaption());

			// CD4
			$this->CD4->EditAttrs["class"] = "form-control";
			$this->CD4->EditCustomAttributes = "";
			$this->CD4->EditValue = ew_HtmlEncode($this->CD4->CurrentValue);
			$this->CD4->PlaceHolder = ew_RemoveHtml($this->CD4->FldCaption());

			// Fecha_CD4
			$this->Fecha_CD4->EditAttrs["class"] = "form-control";
			$this->Fecha_CD4->EditCustomAttributes = "";
			$this->Fecha_CD4->EditValue = ew_HtmlEncode(ew_FormatDateTime($this->Fecha_CD4->CurrentValue, 5));
			$this->Fecha_CD4->PlaceHolder = ew_RemoveHtml($this->Fecha_CD4->FldCaption());

			// adherencia
			$this->adherencia->EditAttrs["class"] = "form-control";
			$this->adherencia->EditCustomAttributes = "";
			if (trim(strval($this->adherencia->CurrentValue)) == "") {
				$sFilterWrk = "0=1";
			} else {
				$sFilterWrk = "`id`" . ew_SearchString("=", $this->adherencia->CurrentValue, EW_DATATYPE_NUMBER, "");
			}
			$sSqlWrk = "SELECT `id`, `adherencia` AS `DispFld`, '' AS `Disp2Fld`, '' AS `Disp3Fld`, '' AS `Disp4Fld`, '' AS `SelectFilterFld`, '' AS `SelectFilterFld2`, '' AS `SelectFilterFld3`, '' AS `SelectFilterFld4` FROM `adherencia`";
			$sWhereWrk = "";
			ew_AddFilter($sWhereWrk, $sFilterWrk);
			$this->Lookup_Selecting($this->adherencia, $sWhereWrk); // Call Lookup selecting
			if ($sWhereWrk <> "") $sSqlWrk .= " WHERE " . $sWhereWrk;
			$rswrk = Conn()->Execute($sSqlWrk);
			$arwrk = ($rswrk) ? $rswrk->GetRows() : array();
			if ($rswrk) $rswrk->Close();
			array_unshift($arwrk, array("", $Language->Phrase("PleaseSelect"), "", "", "", "", "", "", ""));
			$this->adherencia->EditValue = $arwrk;

			// Tratamiento_Antirretroviral
			$this->Tratamiento_Antirretroviral->EditCustomAttributes = "";
			if (trim(strval($this->Tratamiento_Antirretroviral->CurrentValue)) == "") {
				$sFilterWrk = "0=1";
			} else {
				$sFilterWrk = "`Consecutivo`" . ew_SearchString("=", $this->Tratamiento_Antirretroviral->CurrentValue, EW_DATATYPE_NUMBER, "");
			}
			$sSqlWrk = "SELECT `Consecutivo`, `Si_No` AS `DispFld`, '' AS `Disp2Fld`, '' AS `Disp3Fld`, '' AS `Disp4Fld`, '' AS `SelectFilterFld`, '' AS `SelectFilterFld2`, '' AS `SelectFilterFld3`, '' AS `SelectFilterFld4` FROM `si_no`";
			$sWhereWrk = "";
			ew_AddFilter($sWhereWrk, $sFilterWrk);
			$this->Lookup_Selecting($this->Tratamiento_Antirretroviral, $sWhereWrk); // Call Lookup selecting
			if ($sWhereWrk <> "") $sSqlWrk .= " WHERE " . $sWhereWrk;
			$rswrk = Conn()->Execute($sSqlWrk);
			$arwrk = ($rswrk) ? $rswrk->GetRows() : array();
			if ($rswrk) $rswrk->Close();
			$this->Tratamiento_Antirretroviral->EditValue = $arwrk;

			// Porque_No_Antirretroviral
			$this->Porque_No_Antirretroviral->EditCustomAttributes = "";
			$this->Porque_No_Antirretroviral->EditValue = $this->Porque_No_Antirretroviral->Options(FALSE);

			// Porque_no_adherente
			$this->Porque_no_adherente->EditCustomAttributes = "";
			$this->Porque_no_adherente->EditValue = $this->Porque_no_adherente->Options(FALSE);

			// Valor_ultima_cargaviral
			$this->Valor_ultima_cargaviral->EditAttrs["class"] = "form-control";
			$this->Valor_ultima_cargaviral->EditCustomAttributes = "";
			if (trim(strval($this->Valor_ultima_cargaviral->CurrentValue)) == "") {
				$sFilterWrk = "0=1";
			} else {
				$sFilterWrk = "`Carga_viral`" . ew_SearchString("=", $this->Valor_ultima_cargaviral->CurrentValue, EW_DATATYPE_STRING, "");
			}
			$sSqlWrk = "SELECT `Carga_viral`, `Carga_viral` AS `DispFld`, '' AS `Disp2Fld`, '' AS `Disp3Fld`, '' AS `Disp4Fld`, '' AS `SelectFilterFld`, '' AS `SelectFilterFld2`, '' AS `SelectFilterFld3`, '' AS `SelectFilterFld4` FROM `carga_viral`";
			$sWhereWrk = "";
			ew_AddFilter($sWhereWrk, $sFilterWrk);
			$this->Lookup_Selecting($this->Valor_ultima_cargaviral, $sWhereWrk); // Call Lookup selecting
			if ($sWhereWrk <> "") $sSqlWrk .= " WHERE " . $sWhereWrk;
			$rswrk = Conn()->Execute($sSqlWrk);
			$arwrk = ($rswrk) ? $rswrk->GetRows() : array();
			if ($rswrk) $rswrk->Close();
			array_unshift($arwrk, array("", $Language->Phrase("PleaseSelect"), "", "", "", "", "", "", ""));
			$this->Valor_ultima_cargaviral->EditValue = $arwrk;

			// Fecha_Ultima_Cargaviral
			$this->Fecha_Ultima_Cargaviral->EditAttrs["class"] = "form-control";
			$this->Fecha_Ultima_Cargaviral->EditCustomAttributes = "";
			$this->Fecha_Ultima_Cargaviral->EditValue = ew_HtmlEncode(ew_FormatDateTime($this->Fecha_Ultima_Cargaviral->CurrentValue, 5));
			$this->Fecha_Ultima_Cargaviral->PlaceHolder = ew_RemoveHtml($this->Fecha_Ultima_Cargaviral->FldCaption());

			// Estado_Final
			$this->Estado_Final->EditCustomAttributes = "";
			$this->Estado_Final->EditValue = $this->Estado_Final->Options(FALSE);

			// Cambio_Municipio
			$this->Cambio_Municipio->EditCustomAttributes = "";
			if (trim(strval($this->Cambio_Municipio->CurrentValue)) == "") {
				$sFilterWrk = "0=1";
			} else {
				$sFilterWrk = "`Consecutivo`" . ew_SearchString("=", $this->Cambio_Municipio->CurrentValue, EW_DATATYPE_NUMBER, "");
			}
			$sSqlWrk = "SELECT `Consecutivo`, `Si_No` AS `DispFld`, '' AS `Disp2Fld`, '' AS `Disp3Fld`, '' AS `Disp4Fld`, '' AS `SelectFilterFld`, '' AS `SelectFilterFld2`, '' AS `SelectFilterFld3`, '' AS `SelectFilterFld4` FROM `si_no`";
			$sWhereWrk = "";
			ew_AddFilter($sWhereWrk, $sFilterWrk);
			$this->Lookup_Selecting($this->Cambio_Municipio, $sWhereWrk); // Call Lookup selecting
			if ($sWhereWrk <> "") $sSqlWrk .= " WHERE " . $sWhereWrk;
			$rswrk = Conn()->Execute($sSqlWrk);
			$arwrk = ($rswrk) ? $rswrk->GetRows() : array();
			if ($rswrk) $rswrk->Close();
			$this->Cambio_Municipio->EditValue = $arwrk;

			// Fecha_dig
			// Edit refer script
			// Id

			$this->Id->LinkCustomAttributes = "";
			$this->Id->HrefValue = "";

			// Id_Registro
			$this->Id_Registro->LinkCustomAttributes = "";
			$this->Id_Registro->HrefValue = "";

			// Codigo_Eps
			$this->Codigo_Eps->LinkCustomAttributes = "";
			$this->Codigo_Eps->HrefValue = "";

			// Tipo_regimen
			$this->Tipo_regimen->LinkCustomAttributes = "";
			$this->Tipo_regimen->HrefValue = "";

			// Codigo_Ips
			$this->Codigo_Ips->LinkCustomAttributes = "";
			$this->Codigo_Ips->HrefValue = "";

			// Fecha_Confirmatorio
			$this->Fecha_Confirmatorio->LinkCustomAttributes = "";
			$this->Fecha_Confirmatorio->HrefValue = "";

			// Estadio
			$this->Estadio->LinkCustomAttributes = "";
			$this->Estadio->HrefValue = "";

			// Numero_Carga Viral
			$this->Numero_Carga_Viral->LinkCustomAttributes = "";
			$this->Numero_Carga_Viral->HrefValue = "";

			// Fecha_Primera_Carga_Viral
			$this->Fecha_Primera_Carga_Viral->LinkCustomAttributes = "";
			$this->Fecha_Primera_Carga_Viral->HrefValue = "";

			// CD4
			$this->CD4->LinkCustomAttributes = "";
			$this->CD4->HrefValue = "";

			// Fecha_CD4
			$this->Fecha_CD4->LinkCustomAttributes = "";
			$this->Fecha_CD4->HrefValue = "";

			// adherencia
			$this->adherencia->LinkCustomAttributes = "";
			$this->adherencia->HrefValue = "";

			// Tratamiento_Antirretroviral
			$this->Tratamiento_Antirretroviral->LinkCustomAttributes = "";
			$this->Tratamiento_Antirretroviral->HrefValue = "";

			// Porque_No_Antirretroviral
			$this->Porque_No_Antirretroviral->LinkCustomAttributes = "";
			$this->Porque_No_Antirretroviral->HrefValue = "";

			// Porque_no_adherente
			$this->Porque_no_adherente->LinkCustomAttributes = "";
			$this->Porque_no_adherente->HrefValue = "";

			// Valor_ultima_cargaviral
			$this->Valor_ultima_cargaviral->LinkCustomAttributes = "";
			$this->Valor_ultima_cargaviral->HrefValue = "";

			// Fecha_Ultima_Cargaviral
			$this->Fecha_Ultima_Cargaviral->LinkCustomAttributes = "";
			$this->Fecha_Ultima_Cargaviral->HrefValue = "";

			// Estado_Final
			$this->Estado_Final->LinkCustomAttributes = "";
			$this->Estado_Final->HrefValue = "";

			// Cambio_Municipio
			$this->Cambio_Municipio->LinkCustomAttributes = "";
			$this->Cambio_Municipio->HrefValue = "";

			// Fecha_dig
			$this->Fecha_dig->LinkCustomAttributes = "";
			$this->Fecha_dig->HrefValue = "";
		}
		if ($this->RowType == EW_ROWTYPE_ADD ||
			$this->RowType == EW_ROWTYPE_EDIT ||
			$this->RowType == EW_ROWTYPE_SEARCH) { // Add / Edit / Search row
			$this->SetupFieldTitles();
		}

		// Call Row Rendered event
		if ($this->RowType <> EW_ROWTYPE_AGGREGATEINIT)
			$this->Row_Rendered();
	}

	// Validate form
	function ValidateForm() {
		global $Language, $gsFormError;

		// Check if validation required
		if (!EW_SERVER_VALIDATE)
			return ($gsFormError == "");
		if (!$this->Id_Registro->FldIsDetailKey && !is_null($this->Id_Registro->FormValue) && $this->Id_Registro->FormValue == "") {
			ew_AddMessage($gsFormError, str_replace("%s", $this->Id_Registro->FldCaption(), $this->Id_Registro->ReqErrMsg));
		}
		if (!$this->Codigo_Eps->FldIsDetailKey && !is_null($this->Codigo_Eps->FormValue) && $this->Codigo_Eps->FormValue == "") {
			ew_AddMessage($gsFormError, str_replace("%s", $this->Codigo_Eps->FldCaption(), $this->Codigo_Eps->ReqErrMsg));
		}
		if (!$this->Codigo_Ips->FldIsDetailKey && !is_null($this->Codigo_Ips->FormValue) && $this->Codigo_Ips->FormValue == "") {
			ew_AddMessage($gsFormError, str_replace("%s", $this->Codigo_Ips->FldCaption(), $this->Codigo_Ips->ReqErrMsg));
		}
		if (!ew_CheckDate($this->Fecha_Confirmatorio->FormValue)) {
			ew_AddMessage($gsFormError, $this->Fecha_Confirmatorio->FldErrMsg());
		}
		if (!ew_CheckDate($this->Fecha_Primera_Carga_Viral->FormValue)) {
			ew_AddMessage($gsFormError, $this->Fecha_Primera_Carga_Viral->FldErrMsg());
		}
		if (!$this->CD4->FldIsDetailKey && !is_null($this->CD4->FormValue) && $this->CD4->FormValue == "") {
			ew_AddMessage($gsFormError, str_replace("%s", $this->CD4->FldCaption(), $this->CD4->ReqErrMsg));
		}
		if (!ew_CheckInteger($this->CD4->FormValue)) {
			ew_AddMessage($gsFormError, $this->CD4->FldErrMsg());
		}
		if (!ew_CheckDate($this->Fecha_CD4->FormValue)) {
			ew_AddMessage($gsFormError, $this->Fecha_CD4->FldErrMsg());
		}
		if (!$this->adherencia->FldIsDetailKey && !is_null($this->adherencia->FormValue) && $this->adherencia->FormValue == "") {
			ew_AddMessage($gsFormError, str_replace("%s", $this->adherencia->FldCaption(), $this->adherencia->ReqErrMsg));
		}
		if ($this->Tratamiento_Antirretroviral->FormValue == "") {
			ew_AddMessage($gsFormError, str_replace("%s", $this->Tratamiento_Antirretroviral->FldCaption(), $this->Tratamiento_Antirretroviral->ReqErrMsg));
		}
		if (!ew_CheckDate($this->Fecha_Ultima_Cargaviral->FormValue)) {
			ew_AddMessage($gsFormError, $this->Fecha_Ultima_Cargaviral->FldErrMsg());
		}
		if ($this->Estado_Final->FormValue == "") {
			ew_AddMessage($gsFormError, str_replace("%s", $this->Estado_Final->FldCaption(), $this->Estado_Final->ReqErrMsg));
		}

		// Return validate result
		$ValidateForm = ($gsFormError == "");

		// Call Form_CustomValidate event
		$sFormCustomError = "";
		$ValidateForm = $ValidateForm && $this->Form_CustomValidate($sFormCustomError);
		if ($sFormCustomError <> "") {
			ew_AddMessage($gsFormError, $sFormCustomError);
		}
		return $ValidateForm;
	}

	//
	// Delete records based on current filter
	//
	function DeleteRows() {
		global $Language, $Security;
		if (!$Security->CanDelete()) {
			$this->setFailureMessage($Language->Phrase("NoDeletePermission")); // No delete permission
			return FALSE;
		}
		$DeleteRows = TRUE;
		$sSql = $this->SQL();
		$conn = &$this->Connection();
		$conn->raiseErrorFn = $GLOBALS["EW_ERROR_FN"];
		$rs = $conn->Execute($sSql);
		$conn->raiseErrorFn = '';
		if ($rs === FALSE) {
			return FALSE;
		} elseif ($rs->EOF) {
			$this->setFailureMessage($Language->Phrase("NoRecord")); // No record found
			$rs->Close();
			return FALSE;

		//} else {
		//	$this->LoadRowValues($rs); // Load row values

		}
		$rows = ($rs) ? $rs->GetRows() : array();

		// Clone old rows
		$rsold = $rows;
		if ($rs)
			$rs->Close();

		// Call row deleting event
		if ($DeleteRows) {
			foreach ($rsold as $row) {
				$DeleteRows = $this->Row_Deleting($row);
				if (!$DeleteRows) break;
			}
		}
		if ($DeleteRows) {
			$sKey = "";
			foreach ($rsold as $row) {
				$sThisKey = "";
				if ($sThisKey <> "") $sThisKey .= $GLOBALS["EW_COMPOSITE_KEY_SEPARATOR"];
				$sThisKey .= $row['Id'];
				$conn->raiseErrorFn = $GLOBALS["EW_ERROR_FN"];
				$DeleteRows = $this->Delete($row); // Delete
				$conn->raiseErrorFn = '';
				if ($DeleteRows === FALSE)
					break;
				if ($sKey <> "") $sKey .= ", ";
				$sKey .= $sThisKey;
			}
		} else {

			// Set up error message
			if ($this->getSuccessMessage() <> "" || $this->getFailureMessage() <> "") {

				// Use the message, do nothing
			} elseif ($this->CancelMessage <> "") {
				$this->setFailureMessage($this->CancelMessage);
				$this->CancelMessage = "";
			} else {
				$this->setFailureMessage($Language->Phrase("DeleteCancelled"));
			}
		}
		if ($DeleteRows) {
		} else {
		}

		// Call Row Deleted event
		if ($DeleteRows) {
			foreach ($rsold as $row) {
				$this->Row_Deleted($row);
			}
		}
		return $DeleteRows;
	}

	// Update record based on key values
	function EditRow() {
		global $Security, $Language;
		$sFilter = $this->KeyFilter();
		$sFilter = $this->ApplyUserIDFilters($sFilter);
		$conn = &$this->Connection();
		$this->CurrentFilter = $sFilter;
		$sSql = $this->SQL();
		$conn->raiseErrorFn = $GLOBALS["EW_ERROR_FN"];
		$rs = $conn->Execute($sSql);
		$conn->raiseErrorFn = '';
		if ($rs === FALSE)
			return FALSE;
		if ($rs->EOF) {
			$this->setFailureMessage($Language->Phrase("NoRecord")); // Set no record message
			$EditRow = FALSE; // Update Failed
		} else {

			// Save old values
			$rsold = &$rs->fields;
			$this->LoadDbValues($rsold);
			$rsnew = array();

			// Id_Registro
			$this->Id_Registro->SetDbValueDef($rsnew, $this->Id_Registro->CurrentValue, 0, $this->Id_Registro->ReadOnly);

			// Codigo_Eps
			$this->Codigo_Eps->SetDbValueDef($rsnew, $this->Codigo_Eps->CurrentValue, "", $this->Codigo_Eps->ReadOnly);

			// Tipo_regimen
			$this->Tipo_regimen->SetDbValueDef($rsnew, $this->Tipo_regimen->CurrentValue, NULL, $this->Tipo_regimen->ReadOnly);

			// Codigo_Ips
			$this->Codigo_Ips->SetDbValueDef($rsnew, $this->Codigo_Ips->CurrentValue, "", $this->Codigo_Ips->ReadOnly);

			// Fecha_Confirmatorio
			$this->Fecha_Confirmatorio->SetDbValueDef($rsnew, ew_UnFormatDateTime($this->Fecha_Confirmatorio->CurrentValue, 5), NULL, $this->Fecha_Confirmatorio->ReadOnly);

			// Estadio
			$this->Estadio->SetDbValueDef($rsnew, $this->Estadio->CurrentValue, NULL, $this->Estadio->ReadOnly);

			// Numero_Carga Viral
			$this->Numero_Carga_Viral->SetDbValueDef($rsnew, $this->Numero_Carga_Viral->CurrentValue, NULL, $this->Numero_Carga_Viral->ReadOnly);

			// Fecha_Primera_Carga_Viral
			$this->Fecha_Primera_Carga_Viral->SetDbValueDef($rsnew, ew_UnFormatDateTime($this->Fecha_Primera_Carga_Viral->CurrentValue, 5), NULL, $this->Fecha_Primera_Carga_Viral->ReadOnly);

			// CD4
			$this->CD4->SetDbValueDef($rsnew, $this->CD4->CurrentValue, NULL, $this->CD4->ReadOnly);

			// Fecha_CD4
			$this->Fecha_CD4->SetDbValueDef($rsnew, ew_UnFormatDateTime($this->Fecha_CD4->CurrentValue, 5), NULL, $this->Fecha_CD4->ReadOnly);

			// adherencia
			$this->adherencia->SetDbValueDef($rsnew, $this->adherencia->CurrentValue, 0, $this->adherencia->ReadOnly);

			// Tratamiento_Antirretroviral
			$this->Tratamiento_Antirretroviral->SetDbValueDef($rsnew, $this->Tratamiento_Antirretroviral->CurrentValue, 0, $this->Tratamiento_Antirretroviral->ReadOnly);

			// Porque_No_Antirretroviral
			$this->Porque_No_Antirretroviral->SetDbValueDef($rsnew, $this->Porque_No_Antirretroviral->CurrentValue, NULL, $this->Porque_No_Antirretroviral->ReadOnly);

			// Porque_no_adherente
			$this->Porque_no_adherente->SetDbValueDef($rsnew, $this->Porque_no_adherente->CurrentValue, NULL, $this->Porque_no_adherente->ReadOnly);

			// Valor_ultima_cargaviral
			$this->Valor_ultima_cargaviral->SetDbValueDef($rsnew, $this->Valor_ultima_cargaviral->CurrentValue, NULL, $this->Valor_ultima_cargaviral->ReadOnly);

			// Fecha_Ultima_Cargaviral
			$this->Fecha_Ultima_Cargaviral->SetDbValueDef($rsnew, ew_UnFormatDateTime($this->Fecha_Ultima_Cargaviral->CurrentValue, 5), NULL, $this->Fecha_Ultima_Cargaviral->ReadOnly);

			// Estado_Final
			$this->Estado_Final->SetDbValueDef($rsnew, $this->Estado_Final->CurrentValue, 0, $this->Estado_Final->ReadOnly);

			// Cambio_Municipio
			$this->Cambio_Municipio->SetDbValueDef($rsnew, $this->Cambio_Municipio->CurrentValue, NULL, $this->Cambio_Municipio->ReadOnly);

			// Fecha_dig
			$this->Fecha_dig->SetDbValueDef($rsnew, ew_CurrentDate(), ew_CurrentDate());
			$rsnew['Fecha_dig'] = &$this->Fecha_dig->DbValue;

			// Check referential integrity for master table 'registro'
			$bValidMasterRecord = TRUE;
			$sMasterFilter = $this->SqlMasterFilter_registro();
			$KeyValue = isset($rsnew['Id_Registro']) ? $rsnew['Id_Registro'] : $rsold['Id_Registro'];
			if (strval($KeyValue) <> "") {
				$sMasterFilter = str_replace("@Consecutivo@", ew_AdjustSql($KeyValue), $sMasterFilter);
			} else {
				$bValidMasterRecord = FALSE;
			}
			if ($bValidMasterRecord) {
				$rsmaster = $GLOBALS["registro"]->LoadRs($sMasterFilter);
				$bValidMasterRecord = ($rsmaster && !$rsmaster->EOF);
				$rsmaster->Close();
			}
			if (!$bValidMasterRecord) {
				$sRelatedRecordMsg = str_replace("%t", "registro", $Language->Phrase("RelatedRecordRequired"));
				$this->setFailureMessage($sRelatedRecordMsg);
				$rs->Close();
				return FALSE;
			}

			// Call Row Updating event
			$bUpdateRow = $this->Row_Updating($rsold, $rsnew);
			if ($bUpdateRow) {
				$conn->raiseErrorFn = $GLOBALS["EW_ERROR_FN"];
				if (count($rsnew) > 0)
					$EditRow = $this->Update($rsnew, "", $rsold);
				else
					$EditRow = TRUE; // No field to update
				$conn->raiseErrorFn = '';
				if ($EditRow) {
				}
			} else {
				if ($this->getSuccessMessage() <> "" || $this->getFailureMessage() <> "") {

					// Use the message, do nothing
				} elseif ($this->CancelMessage <> "") {
					$this->setFailureMessage($this->CancelMessage);
					$this->CancelMessage = "";
				} else {
					$this->setFailureMessage($Language->Phrase("UpdateCancelled"));
				}
				$EditRow = FALSE;
			}
		}

		// Call Row_Updated event
		if ($EditRow)
			$this->Row_Updated($rsold, $rsnew);
		$rs->Close();
		return $EditRow;
	}

	// Add record
	function AddRow($rsold = NULL) {
		global $Language, $Security;

		// Check if valid key values for master user
		if ($Security->CurrentUserID() <> "" && !$Security->IsAdmin()) { // Non system admin
			$sMasterFilter = $this->SqlMasterFilter_registro();
			if (strval($this->Id_Registro->CurrentValue) <> "") {
				$sMasterFilter = str_replace("@Consecutivo@", ew_AdjustSql($this->Id_Registro->CurrentValue, "DB"), $sMasterFilter);
			} else {
				$sMasterFilter = "";
			}
			if ($sMasterFilter <> "") {
				$rsmaster = $GLOBALS["registro"]->LoadRs($sMasterFilter);
				$this->MasterRecordExists = ($rsmaster && !$rsmaster->EOF);
				$bValidMasterKey = TRUE;
				if ($this->MasterRecordExists) {
					$bValidMasterKey = $Security->IsValidUserID($rsmaster->fields['Codigo_centro']);
				} elseif ($this->getCurrentMasterTable() == "registro") {
					$bValidMasterKey = FALSE;
				}
				if (!$bValidMasterKey) {
					$sMasterUserIdMsg = str_replace("%c", CurrentUserID(), $Language->Phrase("UnAuthorizedMasterUserID"));
					$sMasterUserIdMsg = str_replace("%f", $sMasterFilter, $sMasterUserIdMsg);
					$this->setFailureMessage($sMasterUserIdMsg);
					return FALSE;
				}
				if ($rsmaster) $rsmaster->Close();
			}
		}

		// Set up foreign key field value from Session
			if ($this->getCurrentMasterTable() == "registro") {
				$this->Id_Registro->CurrentValue = $this->Id_Registro->getSessionValue();
			}

		// Check referential integrity for master table 'registro'
		$bValidMasterRecord = TRUE;
		$sMasterFilter = $this->SqlMasterFilter_registro();
		if (strval($this->Id_Registro->CurrentValue) <> "") {
			$sMasterFilter = str_replace("@Consecutivo@", ew_AdjustSql($this->Id_Registro->CurrentValue, "DB"), $sMasterFilter);
		} else {
			$bValidMasterRecord = FALSE;
		}
		if ($bValidMasterRecord) {
			$rsmaster = $GLOBALS["registro"]->LoadRs($sMasterFilter);
			$bValidMasterRecord = ($rsmaster && !$rsmaster->EOF);
			$rsmaster->Close();
		}
		if (!$bValidMasterRecord) {
			$sRelatedRecordMsg = str_replace("%t", "registro", $Language->Phrase("RelatedRecordRequired"));
			$this->setFailureMessage($sRelatedRecordMsg);
			return FALSE;
		}
		$conn = &$this->Connection();

		// Load db values from rsold
		if ($rsold) {
			$this->LoadDbValues($rsold);
		}
		$rsnew = array();

		// Id_Registro
		$this->Id_Registro->SetDbValueDef($rsnew, $this->Id_Registro->CurrentValue, 0, FALSE);

		// Codigo_Eps
		$this->Codigo_Eps->SetDbValueDef($rsnew, $this->Codigo_Eps->CurrentValue, "", FALSE);

		// Tipo_regimen
		$this->Tipo_regimen->SetDbValueDef($rsnew, $this->Tipo_regimen->CurrentValue, NULL, FALSE);

		// Codigo_Ips
		$this->Codigo_Ips->SetDbValueDef($rsnew, $this->Codigo_Ips->CurrentValue, "", FALSE);

		// Fecha_Confirmatorio
		$this->Fecha_Confirmatorio->SetDbValueDef($rsnew, ew_UnFormatDateTime($this->Fecha_Confirmatorio->CurrentValue, 5), NULL, FALSE);

		// Estadio
		$this->Estadio->SetDbValueDef($rsnew, $this->Estadio->CurrentValue, NULL, FALSE);

		// Numero_Carga Viral
		$this->Numero_Carga_Viral->SetDbValueDef($rsnew, $this->Numero_Carga_Viral->CurrentValue, NULL, FALSE);

		// Fecha_Primera_Carga_Viral
		$this->Fecha_Primera_Carga_Viral->SetDbValueDef($rsnew, ew_UnFormatDateTime($this->Fecha_Primera_Carga_Viral->CurrentValue, 5), NULL, FALSE);

		// CD4
		$this->CD4->SetDbValueDef($rsnew, $this->CD4->CurrentValue, NULL, FALSE);

		// Fecha_CD4
		$this->Fecha_CD4->SetDbValueDef($rsnew, ew_UnFormatDateTime($this->Fecha_CD4->CurrentValue, 5), NULL, FALSE);

		// adherencia
		$this->adherencia->SetDbValueDef($rsnew, $this->adherencia->CurrentValue, 0, FALSE);

		// Tratamiento_Antirretroviral
		$this->Tratamiento_Antirretroviral->SetDbValueDef($rsnew, $this->Tratamiento_Antirretroviral->CurrentValue, 0, FALSE);

		// Porque_No_Antirretroviral
		$this->Porque_No_Antirretroviral->SetDbValueDef($rsnew, $this->Porque_No_Antirretroviral->CurrentValue, NULL, FALSE);

		// Porque_no_adherente
		$this->Porque_no_adherente->SetDbValueDef($rsnew, $this->Porque_no_adherente->CurrentValue, NULL, FALSE);

		// Valor_ultima_cargaviral
		$this->Valor_ultima_cargaviral->SetDbValueDef($rsnew, $this->Valor_ultima_cargaviral->CurrentValue, NULL, FALSE);

		// Fecha_Ultima_Cargaviral
		$this->Fecha_Ultima_Cargaviral->SetDbValueDef($rsnew, ew_UnFormatDateTime($this->Fecha_Ultima_Cargaviral->CurrentValue, 5), NULL, FALSE);

		// Estado_Final
		$this->Estado_Final->SetDbValueDef($rsnew, $this->Estado_Final->CurrentValue, 0, FALSE);

		// Cambio_Municipio
		$this->Cambio_Municipio->SetDbValueDef($rsnew, $this->Cambio_Municipio->CurrentValue, NULL, FALSE);

		// Fecha_dig
		$this->Fecha_dig->SetDbValueDef($rsnew, ew_CurrentDate(), ew_CurrentDate());
		$rsnew['Fecha_dig'] = &$this->Fecha_dig->DbValue;

		// Call Row Inserting event
		$rs = ($rsold == NULL) ? NULL : $rsold->fields;
		$bInsertRow = $this->Row_Inserting($rs, $rsnew);
		if ($bInsertRow) {
			$conn->raiseErrorFn = $GLOBALS["EW_ERROR_FN"];
			$AddRow = $this->Insert($rsnew);
			$conn->raiseErrorFn = '';
			if ($AddRow) {

				// Get insert id if necessary
				$this->Id->setDbValue($conn->Insert_ID());
				$rsnew['Id'] = $this->Id->DbValue;
			}
		} else {
			if ($this->getSuccessMessage() <> "" || $this->getFailureMessage() <> "") {

				// Use the message, do nothing
			} elseif ($this->CancelMessage <> "") {
				$this->setFailureMessage($this->CancelMessage);
				$this->CancelMessage = "";
			} else {
				$this->setFailureMessage($Language->Phrase("InsertCancelled"));
			}
			$AddRow = FALSE;
		}
		if ($AddRow) {

			// Call Row Inserted event
			$rs = ($rsold == NULL) ? NULL : $rsold->fields;
			$this->Row_Inserted($rs, $rsnew);
		}
		return $AddRow;
	}

	// Set up master/detail based on QueryString
	function SetUpMasterParms() {

		// Hide foreign keys
		$sMasterTblVar = $this->getCurrentMasterTable();
		if ($sMasterTblVar == "registro") {
			$this->Id_Registro->Visible = FALSE;
			if ($GLOBALS["registro"]->EventCancelled) $this->EventCancelled = TRUE;
		}
		$this->DbMasterFilter = $this->GetMasterFilter(); // Get master filter
		$this->DbDetailFilter = $this->GetDetailFilter(); // Get detail filter
	}

	// Page Load event
	function Page_Load() {

		//echo "Page Load";
	}

	// Page Unload event
	function Page_Unload() {

		//echo "Page Unload";
	}

	// Page Redirecting event
	function Page_Redirecting(&$url) {

		// Example:
		//$url = "your URL";

	}

	// Message Showing event
	// $type = ''|'success'|'failure'|'warning'
	function Message_Showing(&$msg, $type) {
		if ($type == 'success') {

			//$msg = "your success message";
		} elseif ($type == 'failure') {

			//$msg = "your failure message";
		} elseif ($type == 'warning') {

			//$msg = "your warning message";
		} else {

			//$msg = "your message";
		}
	}

	// Page Render event
	function Page_Render() {

		//echo "Page Render";
	}

	// Page Data Rendering event
	function Page_DataRendering(&$header) {

		// Example:
		//$header = "your header";

	}

	// Page Data Rendered event
	function Page_DataRendered(&$footer) {

		// Example:
		//$footer = "your footer";

	}

	// Form Custom Validate event
	function Form_CustomValidate(&$CustomError) {

		// Return error message in CustomError
		return TRUE;
	}
}
?>
