<?php

// Global variable for table object
$Usuario = NULL;

//
// Table class for Usuario
//
class cUsuario extends cTable {
	var $Tipo_doc;
	var $Documento;
	var $Codigo_eps;
	var $Tipo_Usuario;
	var $Primer_ap;
	var $Segundo_ap;
	var $Primer_nom;
	var $Segundo_nom;
	var $Edad;
	var $Unidad_edad;
	var $Sexo;
	var $Codigo_dpto;
	var $Cod_municipio;
	var $Zona_residencia;

	//
	// Table class constructor
	//
	function __construct() {
		global $Language;

		// Language object
		if (!isset($Language)) $Language = new cLanguage();
		$this->TableVar = 'Usuario';
		$this->TableName = 'Usuario';
		$this->TableType = 'TABLE';

		// Update Table
		$this->UpdateTable = "`Usuario`";
		$this->DBID = 'DB';
		$this->ExportAll = TRUE;
		$this->ExportPageBreakCount = 0; // Page break per every n record (PDF only)
		$this->ExportPageOrientation = "portrait"; // Page orientation (PDF only)
		$this->ExportPageSize = "a4"; // Page size (PDF only)
		$this->ExportExcelPageOrientation = ""; // Page orientation (PHPExcel only)
		$this->ExportExcelPageSize = ""; // Page size (PHPExcel only)
		$this->DetailAdd = FALSE; // Allow detail add
		$this->DetailEdit = FALSE; // Allow detail edit
		$this->DetailView = FALSE; // Allow detail view
		$this->ShowMultipleDetails = FALSE; // Show multiple details
		$this->GridAddRowCount = 5;
		$this->AllowAddDeleteRow = ew_AllowAddDeleteRow(); // Allow add/delete row
		$this->UserIDAllowSecurity = 0; // User ID Allow
		$this->BasicSearch = new cBasicSearch($this->TableVar);

		// Tipo_doc
		$this->Tipo_doc = new cField('Usuario', 'Usuario', 'x_Tipo_doc', 'Tipo_doc', '`Tipo_doc`', '`Tipo_doc`', 200, -1, FALSE, '`Tipo_doc`', FALSE, FALSE, FALSE, 'FORMATTED TEXT', 'TEXT');
		$this->fields['Tipo_doc'] = &$this->Tipo_doc;

		// Documento
		$this->Documento = new cField('Usuario', 'Usuario', 'x_Documento', 'Documento', '`Documento`', '`Documento`', 200, -1, FALSE, '`Documento`', FALSE, FALSE, FALSE, 'FORMATTED TEXT', 'TEXT');
		$this->fields['Documento'] = &$this->Documento;

		// Codigo_eps
		$this->Codigo_eps = new cField('Usuario', 'Usuario', 'x_Codigo_eps', 'Codigo_eps', '`Codigo_eps`', '`Codigo_eps`', 200, -1, FALSE, '`Codigo_eps`', FALSE, FALSE, FALSE, 'FORMATTED TEXT', 'TEXT');
		$this->fields['Codigo_eps'] = &$this->Codigo_eps;

		// Tipo_Usuario
		$this->Tipo_Usuario = new cField('Usuario', 'Usuario', 'x_Tipo_Usuario', 'Tipo_Usuario', '`Tipo_Usuario`', '`Tipo_Usuario`', 3, -1, FALSE, '`Tipo_Usuario`', FALSE, FALSE, FALSE, 'FORMATTED TEXT', 'TEXT');
		$this->Tipo_Usuario->FldDefaultErrMsg = $Language->Phrase("IncorrectInteger");
		$this->fields['Tipo_Usuario'] = &$this->Tipo_Usuario;

		// Primer_ap
		$this->Primer_ap = new cField('Usuario', 'Usuario', 'x_Primer_ap', 'Primer_ap', '`Primer_ap`', '`Primer_ap`', 200, -1, FALSE, '`Primer_ap`', FALSE, FALSE, FALSE, 'FORMATTED TEXT', 'TEXT');
		$this->fields['Primer_ap'] = &$this->Primer_ap;

		// Segundo_ap
		$this->Segundo_ap = new cField('Usuario', 'Usuario', 'x_Segundo_ap', 'Segundo_ap', '`Segundo_ap`', '`Segundo_ap`', 200, -1, FALSE, '`Segundo_ap`', FALSE, FALSE, FALSE, 'FORMATTED TEXT', 'TEXT');
		$this->fields['Segundo_ap'] = &$this->Segundo_ap;

		// Primer_nom
		$this->Primer_nom = new cField('Usuario', 'Usuario', 'x_Primer_nom', 'Primer_nom', '`Primer_nom`', '`Primer_nom`', 200, -1, FALSE, '`Primer_nom`', FALSE, FALSE, FALSE, 'FORMATTED TEXT', 'TEXT');
		$this->fields['Primer_nom'] = &$this->Primer_nom;

		// Segundo_nom
		$this->Segundo_nom = new cField('Usuario', 'Usuario', 'x_Segundo_nom', 'Segundo_nom', '`Segundo_nom`', '`Segundo_nom`', 200, -1, FALSE, '`Segundo_nom`', FALSE, FALSE, FALSE, 'FORMATTED TEXT', 'TEXT');
		$this->fields['Segundo_nom'] = &$this->Segundo_nom;

		// Edad
		$this->Edad = new cField('Usuario', 'Usuario', 'x_Edad', 'Edad', '`Edad`', '`Edad`', 3, -1, FALSE, '`Edad`', FALSE, FALSE, FALSE, 'FORMATTED TEXT', 'TEXT');
		$this->Edad->FldDefaultErrMsg = $Language->Phrase("IncorrectInteger");
		$this->fields['Edad'] = &$this->Edad;

		// Unidad_edad
		$this->Unidad_edad = new cField('Usuario', 'Usuario', 'x_Unidad_edad', 'Unidad_edad', '`Unidad_edad`', '`Unidad_edad`', 3, -1, FALSE, '`Unidad_edad`', FALSE, FALSE, FALSE, 'FORMATTED TEXT', 'TEXT');
		$this->Unidad_edad->FldDefaultErrMsg = $Language->Phrase("IncorrectInteger");
		$this->fields['Unidad_edad'] = &$this->Unidad_edad;

		// Sexo
		$this->Sexo = new cField('Usuario', 'Usuario', 'x_Sexo', 'Sexo', '`Sexo`', '`Sexo`', 200, -1, FALSE, '`Sexo`', FALSE, FALSE, FALSE, 'FORMATTED TEXT', 'TEXT');
		$this->fields['Sexo'] = &$this->Sexo;

		// Codigo_dpto
		$this->Codigo_dpto = new cField('Usuario', 'Usuario', 'x_Codigo_dpto', 'Codigo_dpto', '`Codigo_dpto`', '`Codigo_dpto`', 3, -1, FALSE, '`Codigo_dpto`', FALSE, FALSE, FALSE, 'FORMATTED TEXT', 'TEXT');
		$this->Codigo_dpto->FldDefaultErrMsg = $Language->Phrase("IncorrectInteger");
		$this->fields['Codigo_dpto'] = &$this->Codigo_dpto;

		// Cod_municipio
		$this->Cod_municipio = new cField('Usuario', 'Usuario', 'x_Cod_municipio', 'Cod_municipio', '`Cod_municipio`', '`Cod_municipio`', 3, -1, FALSE, '`Cod_municipio`', FALSE, FALSE, FALSE, 'FORMATTED TEXT', 'TEXT');
		$this->Cod_municipio->FldDefaultErrMsg = $Language->Phrase("IncorrectInteger");
		$this->fields['Cod_municipio'] = &$this->Cod_municipio;

		// Zona_residencia
		$this->Zona_residencia = new cField('Usuario', 'Usuario', 'x_Zona_residencia', 'Zona_residencia', '`Zona_residencia`', '`Zona_residencia`', 200, -1, FALSE, '`Zona_residencia`', FALSE, FALSE, FALSE, 'FORMATTED TEXT', 'TEXT');
		$this->fields['Zona_residencia'] = &$this->Zona_residencia;
	}

	// Single column sort
	function UpdateSort(&$ofld) {
		if ($this->CurrentOrder == $ofld->FldName) {
			$sSortField = $ofld->FldExpression;
			$sLastSort = $ofld->getSort();
			if ($this->CurrentOrderType == "ASC" || $this->CurrentOrderType == "DESC") {
				$sThisSort = $this->CurrentOrderType;
			} else {
				$sThisSort = ($sLastSort == "ASC") ? "DESC" : "ASC";
			}
			$ofld->setSort($sThisSort);
			$this->setSessionOrderBy($sSortField . " " . $sThisSort); // Save to Session
		} else {
			$ofld->setSort("");
		}
	}

	// Table level SQL
	var $_SqlFrom = "";

	function getSqlFrom() { // From
		return ($this->_SqlFrom <> "") ? $this->_SqlFrom : "`Usuario`";
	}

	function SqlFrom() { // For backward compatibility
    	return $this->getSqlFrom();
	}

	function setSqlFrom($v) {
    	$this->_SqlFrom = $v;
	}
	var $_SqlSelect = "";

	function getSqlSelect() { // Select
		return ($this->_SqlSelect <> "") ? $this->_SqlSelect : "SELECT * FROM " . $this->getSqlFrom();
	}

	function SqlSelect() { // For backward compatibility
    	return $this->getSqlSelect();
	}

	function setSqlSelect($v) {
    	$this->_SqlSelect = $v;
	}
	var $_SqlWhere = "";

	function getSqlWhere() { // Where
		$sWhere = ($this->_SqlWhere <> "") ? $this->_SqlWhere : "";
		$this->TableFilter = "";
		ew_AddFilter($sWhere, $this->TableFilter);
		return $sWhere;
	}

	function SqlWhere() { // For backward compatibility
    	return $this->getSqlWhere();
	}

	function setSqlWhere($v) {
    	$this->_SqlWhere = $v;
	}
	var $_SqlGroupBy = "";

	function getSqlGroupBy() { // Group By
		return ($this->_SqlGroupBy <> "") ? $this->_SqlGroupBy : "";
	}

	function SqlGroupBy() { // For backward compatibility
    	return $this->getSqlGroupBy();
	}

	function setSqlGroupBy($v) {
    	$this->_SqlGroupBy = $v;
	}
	var $_SqlHaving = "";

	function getSqlHaving() { // Having
		return ($this->_SqlHaving <> "") ? $this->_SqlHaving : "";
	}

	function SqlHaving() { // For backward compatibility
    	return $this->getSqlHaving();
	}

	function setSqlHaving($v) {
    	$this->_SqlHaving = $v;
	}
	var $_SqlOrderBy = "";

	function getSqlOrderBy() { // Order By
		return ($this->_SqlOrderBy <> "") ? $this->_SqlOrderBy : "";
	}

	function SqlOrderBy() { // For backward compatibility
    	return $this->getSqlOrderBy();
	}

	function setSqlOrderBy($v) {
    	$this->_SqlOrderBy = $v;
	}

	// Apply User ID filters
	function ApplyUserIDFilters($sFilter) {
		return $sFilter;
	}

	// Check if User ID security allows view all
	function UserIDAllow($id = "") {
		$allow = EW_USER_ID_ALLOW;
		switch ($id) {
			case "add":
			case "copy":
			case "gridadd":
			case "register":
			case "addopt":
				return (($allow & 1) == 1);
			case "edit":
			case "gridedit":
			case "update":
			case "changepwd":
			case "forgotpwd":
				return (($allow & 4) == 4);
			case "delete":
				return (($allow & 2) == 2);
			case "view":
				return (($allow & 32) == 32);
			case "search":
				return (($allow & 64) == 64);
			default:
				return (($allow & 8) == 8);
		}
	}

	// Get SQL
	function GetSQL($where, $orderby) {
		return ew_BuildSelectSql($this->getSqlSelect(), $this->getSqlWhere(),
			$this->getSqlGroupBy(), $this->getSqlHaving(), $this->getSqlOrderBy(),
			$where, $orderby);
	}

	// Table SQL
	function SQL() {
		$sFilter = $this->CurrentFilter;
		$sFilter = $this->ApplyUserIDFilters($sFilter);
		$sSort = $this->getSessionOrderBy();
		return ew_BuildSelectSql($this->getSqlSelect(), $this->getSqlWhere(),
			$this->getSqlGroupBy(), $this->getSqlHaving(), $this->getSqlOrderBy(),
			$sFilter, $sSort);
	}

	// Table SQL with List page filter
	function SelectSQL() {
		$sFilter = $this->getSessionWhere();
		ew_AddFilter($sFilter, $this->CurrentFilter);
		$sFilter = $this->ApplyUserIDFilters($sFilter);
		$this->Recordset_Selecting($sFilter);
		$sSort = $this->getSessionOrderBy();
		return ew_BuildSelectSql($this->getSqlSelect(), $this->getSqlWhere(), $this->getSqlGroupBy(),
			$this->getSqlHaving(), $this->getSqlOrderBy(), $sFilter, $sSort);
	}

	// Get ORDER BY clause
	function GetOrderBy() {
		$sSort = $this->getSessionOrderBy();
		return ew_BuildSelectSql("", "", "", "", $this->getSqlOrderBy(), "", $sSort);
	}

	// Try to get record count
	function TryGetRecordCount($sSql) {
		$cnt = -1;
		if (($this->TableType == 'TABLE' || $this->TableType == 'VIEW' || $this->TableType == 'LINKTABLE') && preg_match("/^SELECT \* FROM/i", $sSql)) {
			$sSql = "SELECT COUNT(*) FROM" . preg_replace('/^SELECT\s([\s\S]+)?\*\sFROM/i', "", $sSql);
			$sOrderBy = $this->GetOrderBy();
			if (substr($sSql, strlen($sOrderBy) * -1) == $sOrderBy)
				$sSql = substr($sSql, 0, strlen($sSql) - strlen($sOrderBy)); // Remove ORDER BY clause
		} else {
			$sSql = "SELECT COUNT(*) FROM (" . $sSql . ") EW_COUNT_TABLE";
		}
		$conn = &$this->Connection();
		if ($rs = $conn->Execute($sSql)) {
			if (!$rs->EOF && $rs->FieldCount() > 0) {
				$cnt = $rs->fields[0];
				$rs->Close();
			}
		}
		return intval($cnt);
	}

	// Get record count based on filter (for detail record count in master table pages)
	function LoadRecordCount($sFilter) {
		$origFilter = $this->CurrentFilter;
		$this->CurrentFilter = $sFilter;
		$this->Recordset_Selecting($this->CurrentFilter);

		//$sSql = $this->SQL();
		$sSql = $this->GetSQL($this->CurrentFilter, "");
		$cnt = $this->TryGetRecordCount($sSql);
		if ($cnt == -1) {
			if ($rs = $this->LoadRs($this->CurrentFilter)) {
				$cnt = $rs->RecordCount();
				$rs->Close();
			}
		}
		$this->CurrentFilter = $origFilter;
		return intval($cnt);
	}

	// Get record count (for current List page)
	function SelectRecordCount() {
		$sSql = $this->SelectSQL();
		$cnt = $this->TryGetRecordCount($sSql);
		if ($cnt == -1) {
			$conn = &$this->Connection();
			if ($rs = $conn->Execute($sSql)) {
				$cnt = $rs->RecordCount();
				$rs->Close();
			}
		}
		return intval($cnt);
	}

	// INSERT statement
	function InsertSQL(&$rs) {
		$names = "";
		$values = "";
		foreach ($rs as $name => $value) {
			if (!isset($this->fields[$name]) || $this->fields[$name]->FldIsCustom)
				continue;
			$names .= $this->fields[$name]->FldExpression . ",";
			$values .= ew_QuotedValue($value, $this->fields[$name]->FldDataType, $this->DBID) . ",";
		}
		while (substr($names, -1) == ",")
			$names = substr($names, 0, -1);
		while (substr($values, -1) == ",")
			$values = substr($values, 0, -1);
		return "INSERT INTO " . $this->UpdateTable . " ($names) VALUES ($values)";
	}

	// Insert
	function Insert(&$rs) {
		$conn = &$this->Connection();
		return $conn->Execute($this->InsertSQL($rs));
	}

	// UPDATE statement
	function UpdateSQL(&$rs, $where = "", $curfilter = TRUE) {
		$sql = "UPDATE " . $this->UpdateTable . " SET ";
		foreach ($rs as $name => $value) {
			if (!isset($this->fields[$name]) || $this->fields[$name]->FldIsCustom)
				continue;
			$sql .= $this->fields[$name]->FldExpression . "=";
			$sql .= ew_QuotedValue($value, $this->fields[$name]->FldDataType, $this->DBID) . ",";
		}
		while (substr($sql, -1) == ",")
			$sql = substr($sql, 0, -1);
		$filter = ($curfilter) ? $this->CurrentFilter : "";
		if (is_array($where))
			$where = $this->ArrayToFilter($where);
		ew_AddFilter($filter, $where);
		if ($filter <> "")	$sql .= " WHERE " . $filter;
		return $sql;
	}

	// Update
	function Update(&$rs, $where = "", $rsold = NULL, $curfilter = TRUE) {
		$conn = &$this->Connection();
		return $conn->Execute($this->UpdateSQL($rs, $where, $curfilter));
	}

	// DELETE statement
	function DeleteSQL(&$rs, $where = "", $curfilter = TRUE) {
		$sql = "DELETE FROM " . $this->UpdateTable . " WHERE ";
		if (is_array($where))
			$where = $this->ArrayToFilter($where);
		if ($rs) {
			if (array_key_exists('Documento', $rs))
				ew_AddFilter($where, ew_QuotedName('Documento', $this->DBID) . '=' . ew_QuotedValue($rs['Documento'], $this->Documento->FldDataType, $this->DBID));
		}
		$filter = ($curfilter) ? $this->CurrentFilter : "";
		ew_AddFilter($filter, $where);
		if ($filter <> "")
			$sql .= $filter;
		else
			$sql .= "0=1"; // Avoid delete
		return $sql;
	}

	// Delete
	function Delete(&$rs, $where = "", $curfilter = TRUE) {
		$conn = &$this->Connection();
		return $conn->Execute($this->DeleteSQL($rs, $where, $curfilter));
	}

	// Key filter WHERE clause
	function SqlKeyFilter() {
		return "`Documento` = '@Documento@'";
	}

	// Key filter
	function KeyFilter() {
		$sKeyFilter = $this->SqlKeyFilter();
		$sKeyFilter = str_replace("@Documento@", ew_AdjustSql($this->Documento->CurrentValue, $this->DBID), $sKeyFilter); // Replace key value
		return $sKeyFilter;
	}

	// Return page URL
	function getReturnUrl() {
		$name = EW_PROJECT_NAME . "_" . $this->TableVar . "_" . EW_TABLE_RETURN_URL;

		// Get referer URL automatically
		if (ew_ServerVar("HTTP_REFERER") <> "" && ew_ReferPage() <> ew_CurrentPage() && ew_ReferPage() <> "login.php") // Referer not same page or login page
			$_SESSION[$name] = ew_ServerVar("HTTP_REFERER"); // Save to Session
		if (@$_SESSION[$name] <> "") {
			return $_SESSION[$name];
		} else {
			return "Usuariolist.php";
		}
	}

	function setReturnUrl($v) {
		$_SESSION[EW_PROJECT_NAME . "_" . $this->TableVar . "_" . EW_TABLE_RETURN_URL] = $v;
	}

	// List URL
	function GetListUrl() {
		return "Usuariolist.php";
	}

	// View URL
	function GetViewUrl($parm = "") {
		if ($parm <> "")
			$url = $this->KeyUrl("Usuarioview.php", $this->UrlParm($parm));
		else
			$url = $this->KeyUrl("Usuarioview.php", $this->UrlParm(EW_TABLE_SHOW_DETAIL . "="));
		return $this->AddMasterUrl($url);
	}

	// Add URL
	function GetAddUrl($parm = "") {
		if ($parm <> "")
			$url = "Usuarioadd.php?" . $this->UrlParm($parm);
		else
			$url = "Usuarioadd.php";
		return $this->AddMasterUrl($url);
	}

	// Edit URL
	function GetEditUrl($parm = "") {
		$url = $this->KeyUrl("Usuarioedit.php", $this->UrlParm($parm));
		return $this->AddMasterUrl($url);
	}

	// Inline edit URL
	function GetInlineEditUrl() {
		$url = $this->KeyUrl(ew_CurrentPage(), $this->UrlParm("a=edit"));
		return $this->AddMasterUrl($url);
	}

	// Copy URL
	function GetCopyUrl($parm = "") {
		$url = $this->KeyUrl("Usuarioadd.php", $this->UrlParm($parm));
		return $this->AddMasterUrl($url);
	}

	// Inline copy URL
	function GetInlineCopyUrl() {
		$url = $this->KeyUrl(ew_CurrentPage(), $this->UrlParm("a=copy"));
		return $this->AddMasterUrl($url);
	}

	// Delete URL
	function GetDeleteUrl() {
		return $this->KeyUrl("Usuariodelete.php", $this->UrlParm());
	}

	// Add master url
	function AddMasterUrl($url) {
		return $url;
	}

	function KeyToJson() {
		$json = "";
		$json .= "Documento:" . ew_VarToJson($this->Documento->CurrentValue, "string", "'");
		return "{" . $json . "}";
	}

	// Add key value to URL
	function KeyUrl($url, $parm = "") {
		$sUrl = $url . "?";
		if ($parm <> "") $sUrl .= $parm . "&";
		if (!is_null($this->Documento->CurrentValue)) {
			$sUrl .= "Documento=" . urlencode($this->Documento->CurrentValue);
		} else {
			return "javascript:ew_Alert(ewLanguage.Phrase('InvalidRecord'));";
		}
		return $sUrl;
	}

	// Sort URL
	function SortUrl(&$fld) {
		if ($this->CurrentAction <> "" || $this->Export <> "" ||
			in_array($fld->FldType, array(128, 204, 205))) { // Unsortable data type
				return "";
		} elseif ($fld->Sortable) {
			$sUrlParm = $this->UrlParm("order=" . urlencode($fld->FldName) . "&amp;ordertype=" . $fld->ReverseSort());
			return ew_CurrentPage() . "?" . $sUrlParm;
		} else {
			return "";
		}
	}

	// Get record keys from $_POST/$_GET/$_SESSION
	function GetRecordKeys() {
		global $EW_COMPOSITE_KEY_SEPARATOR;
		$arKeys = array();
		$arKey = array();
		if (isset($_POST["key_m"])) {
			$arKeys = ew_StripSlashes($_POST["key_m"]);
			$cnt = count($arKeys);
		} elseif (isset($_GET["key_m"])) {
			$arKeys = ew_StripSlashes($_GET["key_m"]);
			$cnt = count($arKeys);
		} elseif (!empty($_GET) || !empty($_POST)) {
			$isPost = ew_IsHttpPost();
			if ($isPost && isset($_POST["Documento"]))
				$arKeys[] = ew_StripSlashes($_POST["Documento"]);
			elseif (isset($_GET["Documento"]))
				$arKeys[] = ew_StripSlashes($_GET["Documento"]);
			else
				$arKeys = NULL; // Do not setup

			//return $arKeys; // Do not return yet, so the values will also be checked by the following code
		}

		// Check keys
		$ar = array();
		if (is_array($arKeys)) {
			foreach ($arKeys as $key) {
				$ar[] = $key;
			}
		}
		return $ar;
	}

	// Get key filter
	function GetKeyFilter() {
		$arKeys = $this->GetRecordKeys();
		$sKeyFilter = "";
		foreach ($arKeys as $key) {
			if ($sKeyFilter <> "") $sKeyFilter .= " OR ";
			$this->Documento->CurrentValue = $key;
			$sKeyFilter .= "(" . $this->KeyFilter() . ")";
		}
		return $sKeyFilter;
	}

	// Load rows based on filter
	function &LoadRs($sFilter) {

		// Set up filter (SQL WHERE clause) and get return SQL
		//$this->CurrentFilter = $sFilter;
		//$sSql = $this->SQL();

		$sSql = $this->GetSQL($sFilter, "");
		$conn = &$this->Connection();
		$rs = $conn->Execute($sSql);
		return $rs;
	}

	// Load row values from recordset
	function LoadListRowValues(&$rs) {
		$this->Tipo_doc->setDbValue($rs->fields('Tipo_doc'));
		$this->Documento->setDbValue($rs->fields('Documento'));
		$this->Codigo_eps->setDbValue($rs->fields('Codigo_eps'));
		$this->Tipo_Usuario->setDbValue($rs->fields('Tipo_Usuario'));
		$this->Primer_ap->setDbValue($rs->fields('Primer_ap'));
		$this->Segundo_ap->setDbValue($rs->fields('Segundo_ap'));
		$this->Primer_nom->setDbValue($rs->fields('Primer_nom'));
		$this->Segundo_nom->setDbValue($rs->fields('Segundo_nom'));
		$this->Edad->setDbValue($rs->fields('Edad'));
		$this->Unidad_edad->setDbValue($rs->fields('Unidad_edad'));
		$this->Sexo->setDbValue($rs->fields('Sexo'));
		$this->Codigo_dpto->setDbValue($rs->fields('Codigo_dpto'));
		$this->Cod_municipio->setDbValue($rs->fields('Cod_municipio'));
		$this->Zona_residencia->setDbValue($rs->fields('Zona_residencia'));
	}

	// Render list row values
	function RenderListRow() {
		global $Security, $gsLanguage, $Language;

		// Call Row Rendering event
		$this->Row_Rendering();

   // Common render codes
		// Tipo_doc
		// Documento
		// Codigo_eps
		// Tipo_Usuario
		// Primer_ap
		// Segundo_ap
		// Primer_nom
		// Segundo_nom
		// Edad
		// Unidad_edad
		// Sexo
		// Codigo_dpto
		// Cod_municipio
		// Zona_residencia
		// Tipo_doc

		$this->Tipo_doc->ViewValue = $this->Tipo_doc->CurrentValue;
		$this->Tipo_doc->ViewCustomAttributes = "";

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

		// Call Row Rendered event
		$this->Row_Rendered();
	}

	// Render edit row values
	function RenderEditRow() {
		global $Security, $gsLanguage, $Language;

		// Call Row Rendering event
		$this->Row_Rendering();

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

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

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

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

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

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

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

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

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

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

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

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

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

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

		// Call Row Rendered event
		$this->Row_Rendered();
	}

	// Aggregate list row values
	function AggregateListRowValues() {
	}

	// Aggregate list row (for rendering)
	function AggregateListRow() {

		// Call Row Rendered event
		$this->Row_Rendered();
	}
	var $ExportDoc;

	// Export data in HTML/CSV/Word/Excel/Email/PDF format
	function ExportDocument(&$Doc, &$Recordset, $StartRec, $StopRec, $ExportPageType = "") {
		if (!$Recordset || !$Doc)
			return;
		if (!$Doc->ExportCustom) {

			// Write header
			$Doc->ExportTableHeader();
			if ($Doc->Horizontal) { // Horizontal format, write header
				$Doc->BeginExportRow();
				if ($ExportPageType == "view") {
					if ($this->Tipo_doc->Exportable) $Doc->ExportCaption($this->Tipo_doc);
					if ($this->Documento->Exportable) $Doc->ExportCaption($this->Documento);
					if ($this->Codigo_eps->Exportable) $Doc->ExportCaption($this->Codigo_eps);
					if ($this->Tipo_Usuario->Exportable) $Doc->ExportCaption($this->Tipo_Usuario);
					if ($this->Primer_ap->Exportable) $Doc->ExportCaption($this->Primer_ap);
					if ($this->Segundo_ap->Exportable) $Doc->ExportCaption($this->Segundo_ap);
					if ($this->Primer_nom->Exportable) $Doc->ExportCaption($this->Primer_nom);
					if ($this->Segundo_nom->Exportable) $Doc->ExportCaption($this->Segundo_nom);
					if ($this->Edad->Exportable) $Doc->ExportCaption($this->Edad);
					if ($this->Unidad_edad->Exportable) $Doc->ExportCaption($this->Unidad_edad);
					if ($this->Sexo->Exportable) $Doc->ExportCaption($this->Sexo);
					if ($this->Codigo_dpto->Exportable) $Doc->ExportCaption($this->Codigo_dpto);
					if ($this->Cod_municipio->Exportable) $Doc->ExportCaption($this->Cod_municipio);
					if ($this->Zona_residencia->Exportable) $Doc->ExportCaption($this->Zona_residencia);
				} else {
					if ($this->Tipo_doc->Exportable) $Doc->ExportCaption($this->Tipo_doc);
					if ($this->Documento->Exportable) $Doc->ExportCaption($this->Documento);
					if ($this->Codigo_eps->Exportable) $Doc->ExportCaption($this->Codigo_eps);
					if ($this->Tipo_Usuario->Exportable) $Doc->ExportCaption($this->Tipo_Usuario);
					if ($this->Primer_ap->Exportable) $Doc->ExportCaption($this->Primer_ap);
					if ($this->Segundo_ap->Exportable) $Doc->ExportCaption($this->Segundo_ap);
					if ($this->Primer_nom->Exportable) $Doc->ExportCaption($this->Primer_nom);
					if ($this->Segundo_nom->Exportable) $Doc->ExportCaption($this->Segundo_nom);
					if ($this->Edad->Exportable) $Doc->ExportCaption($this->Edad);
					if ($this->Unidad_edad->Exportable) $Doc->ExportCaption($this->Unidad_edad);
					if ($this->Sexo->Exportable) $Doc->ExportCaption($this->Sexo);
					if ($this->Codigo_dpto->Exportable) $Doc->ExportCaption($this->Codigo_dpto);
					if ($this->Cod_municipio->Exportable) $Doc->ExportCaption($this->Cod_municipio);
					if ($this->Zona_residencia->Exportable) $Doc->ExportCaption($this->Zona_residencia);
				}
				$Doc->EndExportRow();
			}
		}

		// Move to first record
		$RecCnt = $StartRec - 1;
		if (!$Recordset->EOF) {
			$Recordset->MoveFirst();
			if ($StartRec > 1)
				$Recordset->Move($StartRec - 1);
		}
		while (!$Recordset->EOF && $RecCnt < $StopRec) {
			$RecCnt++;
			if (intval($RecCnt) >= intval($StartRec)) {
				$RowCnt = intval($RecCnt) - intval($StartRec) + 1;

				// Page break
				if ($this->ExportPageBreakCount > 0) {
					if ($RowCnt > 1 && ($RowCnt - 1) % $this->ExportPageBreakCount == 0)
						$Doc->ExportPageBreak();
				}
				$this->LoadListRowValues($Recordset);

				// Render row
				$this->RowType = EW_ROWTYPE_VIEW; // Render view
				$this->ResetAttrs();
				$this->RenderListRow();
				if (!$Doc->ExportCustom) {
					$Doc->BeginExportRow($RowCnt); // Allow CSS styles if enabled
					if ($ExportPageType == "view") {
						if ($this->Tipo_doc->Exportable) $Doc->ExportField($this->Tipo_doc);
						if ($this->Documento->Exportable) $Doc->ExportField($this->Documento);
						if ($this->Codigo_eps->Exportable) $Doc->ExportField($this->Codigo_eps);
						if ($this->Tipo_Usuario->Exportable) $Doc->ExportField($this->Tipo_Usuario);
						if ($this->Primer_ap->Exportable) $Doc->ExportField($this->Primer_ap);
						if ($this->Segundo_ap->Exportable) $Doc->ExportField($this->Segundo_ap);
						if ($this->Primer_nom->Exportable) $Doc->ExportField($this->Primer_nom);
						if ($this->Segundo_nom->Exportable) $Doc->ExportField($this->Segundo_nom);
						if ($this->Edad->Exportable) $Doc->ExportField($this->Edad);
						if ($this->Unidad_edad->Exportable) $Doc->ExportField($this->Unidad_edad);
						if ($this->Sexo->Exportable) $Doc->ExportField($this->Sexo);
						if ($this->Codigo_dpto->Exportable) $Doc->ExportField($this->Codigo_dpto);
						if ($this->Cod_municipio->Exportable) $Doc->ExportField($this->Cod_municipio);
						if ($this->Zona_residencia->Exportable) $Doc->ExportField($this->Zona_residencia);
					} else {
						if ($this->Tipo_doc->Exportable) $Doc->ExportField($this->Tipo_doc);
						if ($this->Documento->Exportable) $Doc->ExportField($this->Documento);
						if ($this->Codigo_eps->Exportable) $Doc->ExportField($this->Codigo_eps);
						if ($this->Tipo_Usuario->Exportable) $Doc->ExportField($this->Tipo_Usuario);
						if ($this->Primer_ap->Exportable) $Doc->ExportField($this->Primer_ap);
						if ($this->Segundo_ap->Exportable) $Doc->ExportField($this->Segundo_ap);
						if ($this->Primer_nom->Exportable) $Doc->ExportField($this->Primer_nom);
						if ($this->Segundo_nom->Exportable) $Doc->ExportField($this->Segundo_nom);
						if ($this->Edad->Exportable) $Doc->ExportField($this->Edad);
						if ($this->Unidad_edad->Exportable) $Doc->ExportField($this->Unidad_edad);
						if ($this->Sexo->Exportable) $Doc->ExportField($this->Sexo);
						if ($this->Codigo_dpto->Exportable) $Doc->ExportField($this->Codigo_dpto);
						if ($this->Cod_municipio->Exportable) $Doc->ExportField($this->Cod_municipio);
						if ($this->Zona_residencia->Exportable) $Doc->ExportField($this->Zona_residencia);
					}
					$Doc->EndExportRow();
				}
			}

			// Call Row Export server event
			if ($Doc->ExportCustom)
				$this->Row_Export($Recordset->fields);
			$Recordset->MoveNext();
		}
		if (!$Doc->ExportCustom) {
			$Doc->ExportTableFooter();
		}
	}

	// Get auto fill value
	function GetAutoFill($id, $val) {
		$rsarr = array();
		$rowcnt = 0;

		// Output
		if (is_array($rsarr) && $rowcnt > 0) {
			$fldcnt = count($rsarr[0]);
			for ($i = 0; $i < $rowcnt; $i++) {
				for ($j = 0; $j < $fldcnt; $j++) {
					$str = strval($rsarr[$i][$j]);
					$str = ew_ConvertToUtf8($str);
					if (isset($post["keepCRLF"])) {
						$str = str_replace(array("\r", "\n"), array("\\r", "\\n"), $str);
					} else {
						$str = str_replace(array("\r", "\n"), array(" ", " "), $str);
					}
					$rsarr[$i][$j] = $str;
				}
			}
			return ew_ArrayToJson($rsarr);
		} else {
			return FALSE;
		}
	}

	// Table level events
	// Recordset Selecting event
	function Recordset_Selecting(&$filter) {

		// Enter your code here	
	}

	// Recordset Selected event
	function Recordset_Selected(&$rs) {

		//echo "Recordset Selected";
	}

	// Recordset Search Validated event
	function Recordset_SearchValidated() {

		// Example:
		//$this->MyField1->AdvancedSearch->SearchValue = "your search criteria"; // Search value

	}

	// Recordset Searching event
	function Recordset_Searching(&$filter) {

		// Enter your code here	
	}

	// Row_Selecting event
	function Row_Selecting(&$filter) {

		// Enter your code here	
	}

	// Row Selected event
	function Row_Selected(&$rs) {

		//echo "Row Selected";
	}

	// Row Inserting event
	function Row_Inserting($rsold, &$rsnew) {

		// Enter your code here
		// To cancel, set return value to FALSE

		return TRUE;
	}

	// Row Inserted event
	function Row_Inserted($rsold, &$rsnew) {

		//echo "Row Inserted"
	}

	// Row Updating event
	function Row_Updating($rsold, &$rsnew) {

		// Enter your code here
		// To cancel, set return value to FALSE

		return TRUE;
	}

	// Row Updated event
	function Row_Updated($rsold, &$rsnew) {

		//echo "Row Updated";
	}

	// Row Update Conflict event
	function Row_UpdateConflict($rsold, &$rsnew) {

		// Enter your code here
		// To ignore conflict, set return value to FALSE

		return TRUE;
	}

	// Grid Inserting event
	function Grid_Inserting() {

		// Enter your code here
		// To reject grid insert, set return value to FALSE

		return TRUE;
	}

	// Grid Inserted event
	function Grid_Inserted($rsnew) {

		//echo "Grid Inserted";
	}

	// Grid Updating event
	function Grid_Updating($rsold) {

		// Enter your code here
		// To reject grid update, set return value to FALSE

		return TRUE;
	}

	// Grid Updated event
	function Grid_Updated($rsold, $rsnew) {

		//echo "Grid Updated";
	}

	// Row Deleting event
	function Row_Deleting(&$rs) {

		// Enter your code here
		// To cancel, set return value to False

		return TRUE;
	}

	// Row Deleted event
	function Row_Deleted(&$rs) {

		//echo "Row Deleted";
	}

	// Email Sending event
	function Email_Sending(&$Email, &$Args) {

		//var_dump($Email); var_dump($Args); exit();
		return TRUE;
	}

	// Lookup Selecting event
	function Lookup_Selecting($fld, &$filter) {

		//var_dump($fld->FldName, $fld->LookupFilters, $filter); // Uncomment to view the filter
		// Enter your code here

	}

	// Row Rendering event
	function Row_Rendering() {

		// Enter your code here	
	}

	// Row Rendered event
	function Row_Rendered() {

		// To view properties of field class, use:
		//var_dump($this-><FieldName>); 

	}

	// User ID Filtering event
	function UserID_Filtering(&$filter) {

		// Enter your code here
	}
}
?>
