<?php
session_start(); // Initialize session data
ob_start(); // Turn on output buffering
?>
<?php include "ewcfg6.php" ?>
<?php include "ewmysql6.php" ?>
<?php include "phpfn6.php" ?>
<?php include "pv_deltron_catinfo.php" ?>
<?php include "usuarioinfo.php" ?>
<?php include "userfn6.php" ?>
<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // Always modified
header("Cache-Control: private, no-store, no-cache, must-revalidate"); // HTTP/1.1 
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0
?>
<?php

// Define page object
$pv_deltron_cat_add = new cpv_deltron_cat_add();
$Page =& $pv_deltron_cat_add;

// Page init processing
$pv_deltron_cat_add->Page_Init();

// Page main processing
$pv_deltron_cat_add->Page_Main();
?>
<?php include "header.php" ?>
<script type="text/javascript">
<!--

// Create page object
var pv_deltron_cat_add = new ew_Page("pv_deltron_cat_add");

// page properties
pv_deltron_cat_add.PageID = "add"; // page ID
var EW_PAGE_ID = pv_deltron_cat_add.PageID; // for backward compatibility

// extend page with ValidateForm function
pv_deltron_cat_add.ValidateForm = function(fobj) {
	if (!this.ValidateRequired)
		return true; // ignore validation
	if (fobj.a_confirm && fobj.a_confirm.value == "F")
		return true;
	var i, elm, aelm, infix;
	var rowcnt = (fobj.key_count) ? Number(fobj.key_count.value) : 1;
	for (i=0; i<rowcnt; i++) {
		infix = (fobj.key_count) ? String(i+1) : "";
		elm = fobj.elements["x" + infix + "_margen"];
		if (elm && !ew_CheckNumber(elm.value))
			return ew_OnError(this, elm, "Incorrect floating point number - Margen");

		// Call Form Custom Validate event
		if (!this.Form_CustomValidate(fobj)) return false;
	}
	return true;
}

// extend page with Form_CustomValidate function
pv_deltron_cat_add.Form_CustomValidate = 
 function(fobj) { // DO NOT CHANGE THIS LINE!

 	// Your custom validation code here, return false if invalid. 
 	return true;
 }
<?php if (EW_CLIENT_VALIDATE) { ?>
pv_deltron_cat_add.ValidateRequired = true; // uses JavaScript validation
<?php } else { ?>
pv_deltron_cat_add.ValidateRequired = false; // no JavaScript validation
<?php } ?>

//-->
</script>
<script type="text/javascript">
<!--
var ew_DHTMLEditors = [];

//-->
</script>
<script language="JavaScript" type="text/javascript">
<!--

// Write your client script here, no need to add script tags.
// To include another .js script, use:
// ew_ClientScriptInclude("my_javascript.js"); 
//-->

</script>
<p><span class="phpmaker">Agregar a TABLA: DTcat<br><br>
<a href="<?php echo $pv_deltron_cat->getReturnUrl() ?>">Regresar</a></span></p>
<?php $pv_deltron_cat_add->ShowMessage() ?>
<form name="fpv_deltron_catadd" id="fpv_deltron_catadd" action="<?php echo ew_CurrentPage() ?>" method="post" onsubmit="return pv_deltron_cat_add.ValidateForm(this);">
<p>
<input type="hidden" name="t" id="t" value="pv_deltron_cat">
<input type="hidden" name="a_add" id="a_add" value="A">
<table cellspacing="0" class="ewGrid"><tr><td class="ewGridContent">
<div class="ewGridMiddlePanel">
<table cellspacing="0" class="ewTable">
<?php if ($pv_deltron_cat->categoria->Visible) { // categoria ?>
	<tr<?php echo $pv_deltron_cat->categoria->RowAttributes ?>>
		<td class="ewTableHeader">Categoria</td>
		<td<?php echo $pv_deltron_cat->categoria->CellAttributes() ?>><span id="el_categoria">
<input type="text" name="x_categoria" id="x_categoria" size="30" maxlength="255" value="<?php echo $pv_deltron_cat->categoria->EditValue ?>"<?php echo $pv_deltron_cat->categoria->EditAttributes() ?>>
</span><?php echo $pv_deltron_cat->categoria->CustomMsg ?></td>
	</tr>
<?php } ?>
<?php if ($pv_deltron_cat->margen->Visible) { // margen ?>
	<tr<?php echo $pv_deltron_cat->margen->RowAttributes ?>>
		<td class="ewTableHeader">Margen</td>
		<td<?php echo $pv_deltron_cat->margen->CellAttributes() ?>><span id="el_margen">
<input type="text" name="x_margen" id="x_margen" size="30" value="<?php echo $pv_deltron_cat->margen->EditValue ?>"<?php echo $pv_deltron_cat->margen->EditAttributes() ?>>
</span><?php echo $pv_deltron_cat->margen->CustomMsg ?></td>
	</tr>
<?php } ?>
</table>
</div>
</td></tr></table>
<p>
<input type="submit" name="btnAction" id="btnAction" value="  Agregar  ">
</form>
<script language="JavaScript" type="text/javascript">
<!--

// Write your table-specific startup script here
// document.write("page loaded");
//-->

</script>
<?php include "footer.php" ?>
<?php

//
// Page Class
//
class cpv_deltron_cat_add {

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

	// Table Name
	var $TableName = 'pv_deltron_cat';

	// Page Object Name
	var $PageObjName = 'pv_deltron_cat_add';

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

	// Page Url
	function PageUrl() {
		$PageUrl = ew_CurrentPage() . "?";
		global $pv_deltron_cat;
		if ($pv_deltron_cat->UseTokenInUrl) $PageUrl .= "t=" . $pv_deltron_cat->TableVar . "&"; // add page token
		return $PageUrl;
	}

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

	function setMessage($v) {
		if (@$_SESSION[EW_SESSION_MESSAGE] <> "") { // Append
			$_SESSION[EW_SESSION_MESSAGE] .= "<br>" . $v;
		} else {
			$_SESSION[EW_SESSION_MESSAGE] = $v;
		}
	}

	// Show Message
	function ShowMessage() {
		if ($this->getMessage() <> "") { // Message in Session, display
			echo "<p><span class=\"ewMessage\">" . $this->getMessage() . "</span></p>";
			$_SESSION[EW_SESSION_MESSAGE] = ""; // Clear message in Session
		}
	}

	// Validate Page request
	function IsPageRequest() {
		global $objForm, $pv_deltron_cat;
		if ($pv_deltron_cat->UseTokenInUrl) {

			//IsPageRequest = False
			if ($objForm)
				return ($pv_deltron_cat->TableVar == $objForm->GetValue("t"));
			if (@$_GET["t"] <> "")
				return ($pv_deltron_cat->TableVar == $_GET["t"]);
		} else {
			return TRUE;
		}
	}

	//
	//  Class initialize
	//  - init objects
	//  - open connection
	//
	function cpv_deltron_cat_add() {
		global $conn;

		// Initialize table object
		$GLOBALS["pv_deltron_cat"] = new cpv_deltron_cat();

		// Initialize other table object
		$GLOBALS['usuario'] = new cusuario();

		// Intialize page id (for backward compatibility)
		if (!defined("EW_PAGE_ID"))
			define("EW_PAGE_ID", 'add', TRUE);

		// Initialize table name (for backward compatibility)
		if (!defined("EW_TABLE_NAME"))
			define("EW_TABLE_NAME", 'pv_deltron_cat', TRUE);

		// Open connection to the database
		$conn = ew_Connect();
	}

	// 
	//  Page_Init
	//
	function Page_Init() {
		global $gsExport, $gsExportFile, $pv_deltron_cat;
		global $Security;
		$Security = new cAdvancedSecurity();
		if (!$Security->IsLoggedIn()) $Security->AutoLogin();
		if (!$Security->IsLoggedIn()) {
			$Security->SaveLastUrl();
			$this->Page_Terminate("login.php");
		}

		// Global page loading event (in userfn6.php)
		Page_Loading();

		// Page load event, used in current page
		$this->Page_Load();
	}

	//
	//  Page_Terminate
	//  - called when exit page
	//  - if URL specified, redirect to the URL
	//
	function Page_Terminate($url = "") {
		global $conn;

		// Page unload event, used in current page
		$this->Page_Unload();

		// Global page unloaded event (in userfn*.php)
		Page_Unloaded();

		 // Close Connection
		$conn->Close();

		// Go to URL if specified
		if ($url <> "") {
			ob_end_clean();
			header("Location: $url");
		}
		exit();
	}
	var $x_ewPriv = 0;

	// 
	// Page main processing
	//
	function Page_Main() {
		global $objForm, $gsFormError, $pv_deltron_cat;

		// Load key values from QueryString
		$bCopy = TRUE;
		if (@$_GET["id"] != "") {
		  $pv_deltron_cat->id->setQueryStringValue($_GET["id"]);
		} else {
		  $bCopy = FALSE;
		}

		// Create form object
		$objForm = new cFormObj();

		// Process form if post back
		if (@$_POST["a_add"] <> "") {
		   $pv_deltron_cat->CurrentAction = $_POST["a_add"]; // Get form action
		  $this->LoadFormValues(); // Load form values

			// Validate Form
			if (!$this->ValidateForm()) {
				$pv_deltron_cat->CurrentAction = "I"; // Form error, reset action
				$this->setMessage($gsFormError);
			}
		} else { // Not post back
		  if ($bCopy) {
		    $pv_deltron_cat->CurrentAction = "C"; // Copy Record
		  } else {
		    $pv_deltron_cat->CurrentAction = "I"; // Display Blank Record
		    $this->LoadDefaultValues(); // Load default values
		  }
		}

		// Perform action based on action code
		switch ($pv_deltron_cat->CurrentAction) {
		  case "I": // Blank record, no action required
				break;
		  case "C": // Copy an existing record
		   if (!$this->LoadRow()) { // Load record based on key
		      $this->setMessage("No se encontraron registros"); // No record found
		      $this->Page_Terminate("pv_deltron_catlist.php"); // No matching record, return to list
		    }
				break;
		  case "A": // ' Add new record
				$pv_deltron_cat->SendEmail = TRUE; // Send email on add success
		    if ($this->AddRow()) { // Add successful
		      $this->setMessage("Agregado con exito"); // Set up success message
					$sReturnUrl = $pv_deltron_cat->getReturnUrl();
					$this->Page_Terminate($sReturnUrl); // Clean up and return
		    } else {
		      $this->RestoreFormValues(); // Add failed, restore form values
		    }
		}

		// Render row based on row type
		$pv_deltron_cat->RowType = EW_ROWTYPE_ADD;  // Render add type

		// Render row
		$this->RenderRow();
	}

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

		// Get upload data
	}

	// Load default values
	function LoadDefaultValues() {
		global $pv_deltron_cat;
		$pv_deltron_cat->margen->CurrentValue = 1.150;
	}

	// Load form values
	function LoadFormValues() {

		// Load from form
		global $objForm, $pv_deltron_cat;
		$pv_deltron_cat->categoria->setFormValue($objForm->GetValue("x_categoria"));
		$pv_deltron_cat->margen->setFormValue($objForm->GetValue("x_margen"));
		$pv_deltron_cat->id->setFormValue($objForm->GetValue("x_id"));
	}

	// Restore form values
	function RestoreFormValues() {
		global $pv_deltron_cat;
		$pv_deltron_cat->id->CurrentValue = $pv_deltron_cat->id->FormValue;
		$pv_deltron_cat->categoria->CurrentValue = $pv_deltron_cat->categoria->FormValue;
		$pv_deltron_cat->margen->CurrentValue = $pv_deltron_cat->margen->FormValue;
	}

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

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

		// Load sql based on filter
		$pv_deltron_cat->CurrentFilter = $sFilter;
		$sSql = $pv_deltron_cat->SQL();
		if ($rs = $conn->Execute($sSql)) {
			if ($rs->EOF) {
				$LoadRow = FALSE;
			} else {
				$LoadRow = TRUE;
				$rs->MoveFirst();
				$this->LoadRowValues($rs); // Load row values

				// Call Row Selected event
				$pv_deltron_cat->Row_Selected($rs);
			}
			$rs->Close();
		} else {
			$LoadRow = FALSE;
		}
		return $LoadRow;
	}

	// Load row values from recordset
	function LoadRowValues(&$rs) {
		global $pv_deltron_cat;
		$pv_deltron_cat->id->setDbValue($rs->fields('id'));
		$pv_deltron_cat->categoria->setDbValue($rs->fields('categoria'));
		$pv_deltron_cat->margen->setDbValue($rs->fields('margen'));
	}

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

		// Call Row_Rendering event
		$pv_deltron_cat->Row_Rendering();

		// Common render codes for all row types
		// categoria

		$pv_deltron_cat->categoria->CellCssStyle = "";
		$pv_deltron_cat->categoria->CellCssClass = "";

		// margen
		$pv_deltron_cat->margen->CellCssStyle = "";
		$pv_deltron_cat->margen->CellCssClass = "";
		if ($pv_deltron_cat->RowType == EW_ROWTYPE_VIEW) { // View row

			// id
			$pv_deltron_cat->id->ViewValue = $pv_deltron_cat->id->CurrentValue;
			$pv_deltron_cat->id->CssStyle = "";
			$pv_deltron_cat->id->CssClass = "";
			$pv_deltron_cat->id->ViewCustomAttributes = "";

			// categoria
			$pv_deltron_cat->categoria->ViewValue = $pv_deltron_cat->categoria->CurrentValue;
			$pv_deltron_cat->categoria->CssStyle = "";
			$pv_deltron_cat->categoria->CssClass = "";
			$pv_deltron_cat->categoria->ViewCustomAttributes = "";

			// margen
			$pv_deltron_cat->margen->ViewValue = $pv_deltron_cat->margen->CurrentValue;
			$pv_deltron_cat->margen->CssStyle = "";
			$pv_deltron_cat->margen->CssClass = "";
			$pv_deltron_cat->margen->ViewCustomAttributes = "";

			// categoria
			$pv_deltron_cat->categoria->HrefValue = "";

			// margen
			$pv_deltron_cat->margen->HrefValue = "";
		} elseif ($pv_deltron_cat->RowType == EW_ROWTYPE_ADD) { // Add row

			// categoria
			$pv_deltron_cat->categoria->EditCustomAttributes = "";
			$pv_deltron_cat->categoria->EditValue = ew_HtmlEncode($pv_deltron_cat->categoria->CurrentValue);

			// margen
			$pv_deltron_cat->margen->EditCustomAttributes = "";
			$pv_deltron_cat->margen->EditValue = ew_HtmlEncode($pv_deltron_cat->margen->CurrentValue);
		}

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

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

		// Initialize
		$gsFormError = "";

		// Check if validation required
		if (!EW_SERVER_VALIDATE)
			return ($gsFormError == "");
		if (!ew_CheckNumber($pv_deltron_cat->margen->FormValue)) {
			if ($gsFormError <> "") $gsFormError .= "<br>";
			$gsFormError .= "Incorrect floating point number - Margen";
		}

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

		// Call Form_CustomValidate event
		$sFormCustomError = "";
		$ValidateForm = $ValidateForm && $this->Form_CustomValidate($sFormCustomError);
		if ($sFormCustomError <> "") {
			$gsFormError .= ($gsFormError <> "") ? "<br>" : "";
			$gsFormError .= $sFormCustomError;
		}
		return $ValidateForm;
	}

	// Add record
	function AddRow() {
		global $conn, $Security, $pv_deltron_cat;
		$rsnew = array();

		// Field categoria
		$pv_deltron_cat->categoria->SetDbValueDef($pv_deltron_cat->categoria->CurrentValue, NULL);
		$rsnew['categoria'] =& $pv_deltron_cat->categoria->DbValue;

		// Field margen
		$pv_deltron_cat->margen->SetDbValueDef($pv_deltron_cat->margen->CurrentValue, NULL);
		$rsnew['margen'] =& $pv_deltron_cat->margen->DbValue;

		// Call Row Inserting event
		$bInsertRow = $pv_deltron_cat->Row_Inserting($rsnew);
		if ($bInsertRow) {
			$conn->raiseErrorFn = 'ew_ErrorFn';
			$AddRow = $conn->Execute($pv_deltron_cat->InsertSQL($rsnew));
			$conn->raiseErrorFn = '';
		} else {
			if ($pv_deltron_cat->CancelMessage <> "") {
				$this->setMessage($pv_deltron_cat->CancelMessage);
				$pv_deltron_cat->CancelMessage = "";
			} else {
				$this->setMessage("Insertar cancelado");
			}
			$AddRow = FALSE;
		}
		if ($AddRow) {
			$pv_deltron_cat->id->setDbValue($conn->Insert_ID());
			$rsnew['id'] =& $pv_deltron_cat->id->DbValue;

			// Call Row Inserted event
			$pv_deltron_cat->Row_Inserted($rsnew);
		}
		return $AddRow;
	}

	// Page Load event
	function Page_Load() {

		//echo "Page Load";
	}

	// Page Unload event
	function Page_Unload() {

		//echo "Page Unload";
	}

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

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