Location: PHPKode > projects > Science Lab Inventory & Order Management > sciencelabinv/ordersedit.php
<?php
session_start(); // Initialize Session data
ob_start(); // Turn on output buffering
?>
<?php include "ewcfg7.php" ?>
<?php include "ewmysql7.php" ?>
<?php include "phpfn7.php" ?>
<?php include "ordersinfo.php" ?>
<?php include "roomsinfo.php" ?>
<?php include "useraccountsinfo.php" ?>
<?php include "lessoninfo.php" ?>
<?php include "userfn7.php" ?>
<?php

// Create page object
$orders_edit = new corders_edit();
$Page =& $orders_edit;

// Page init
$orders_edit->Page_Init();

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

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

// page properties
orders_edit.PageID = "edit"; // page ID
orders_edit.FormID = "fordersedit"; // form ID
var EW_PAGE_ID = orders_edit.PageID; // for backward compatibility

// extend page with ValidateForm function
orders_edit.ValidateForm = function(fobj) {
	ew_PostAutoSuggest(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 + "_lesson"];
		if (elm && !ew_HasValue(elm))
			return ew_OnError(this, elm, ewLanguage.Phrase("EnterRequiredField") + " - <?php echo ew_JsEncode2($orders->lesson->FldCaption()) ?>");
		elm = fobj.elements["x" + infix + "_title"];
		if (elm && !ew_HasValue(elm))
			return ew_OnError(this, elm, ewLanguage.Phrase("EnterRequiredField") + " - <?php echo ew_JsEncode2($orders->title->FldCaption()) ?>");
		elm = fobj.elements["x" + infix + "_request_date"];
		if (elm && !ew_HasValue(elm))
			return ew_OnError(this, elm, ewLanguage.Phrase("EnterRequiredField") + " - <?php echo ew_JsEncode2($orders->request_date->FldCaption()) ?>");
		elm = fobj.elements["x" + infix + "_request_date"];
		if (elm && !ew_CheckDate(elm.value))
			return ew_OnError(this, elm, "<?php echo ew_JsEncode2($orders->request_date->FldErrMsg()) ?>");
		elm = fobj.elements["x" + infix + "_return_date"];
		if (elm && !ew_HasValue(elm))
			return ew_OnError(this, elm, ewLanguage.Phrase("EnterRequiredField") + " - <?php echo ew_JsEncode2($orders->return_date->FldCaption()) ?>");
		elm = fobj.elements["x" + infix + "_return_date"];
		if (elm && !ew_CheckDate(elm.value))
			return ew_OnError(this, elm, "<?php echo ew_JsEncode2($orders->return_date->FldErrMsg()) ?>");
		elm = fobj.elements["x" + infix + "_attachment"];
		if (elm && !ew_CheckFileType(elm.value))
			return ew_OnError(this, elm, ewLanguage.Phrase("WrongFileType"));

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

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

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

// multi page properties
orders_edit.MultiPage = new ew_MultiPage();
orders_edit.MultiPage.AddElement("x_id", 1);
orders_edit.MultiPage.AddElement("x_lesson", 1);
orders_edit.MultiPage.AddElement("x_title", 1);
orders_edit.MultiPage.AddElement("x_room", 1);
orders_edit.MultiPage.AddElement("x_request_date", 1);
orders_edit.MultiPage.AddElement("x_return_date", 1);
orders_edit.MultiPage.AddElement("x_attachment", 2);
orders_edit.MultiPage.AddElement("x_note", 2);

//-->
</script>
<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
<script type="text/javascript">
<!--
_width_multiplier = 20;
_height_multiplier = 60;
var ew_DHTMLEditors = [];

// update value from editor to textarea
function ew_UpdateTextArea() {
	if (typeof ew_DHTMLEditors != 'undefined' && typeof CKEDITOR != 'undefined') {			
		var inst;			
		for (inst in CKEDITOR.instances)
			CKEDITOR.instances[inst].updateElement();
	}
}

// update value from textarea to editor
function ew_UpdateDHTMLEditor(name) {
	if (typeof ew_DHTMLEditors != 'undefined' && typeof CKEDITOR != 'undefined') {
		var inst = CKEDITOR.instances[name];		
		if (inst)
			inst.setData(inst.element.value);
	}
}

// focus editor
function ew_FocusDHTMLEditor(name) {
	if (typeof ew_DHTMLEditors != 'undefined' && typeof CKEDITOR != 'undefined') {
		var inst = CKEDITOR.instances[name];	
		if (inst)
			inst.focus();
	}
}

//-->
</script>
<link rel="stylesheet" type="text/css" media="all" href="calendar/calendar-win2k-cold-1.css" title="win2k-1">
<script type="text/javascript" src="calendar/calendar.js"></script>
<script type="text/javascript" src="calendar/lang/calendar-en.js"></script>
<script type="text/javascript" src="calendar/calendar-setup.js"></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"><?php echo $Language->Phrase("Edit") ?>&nbsp;<?php echo $Language->Phrase("TblTypeTABLE") ?><?php echo $orders->TableCaption() ?><br><br>
<a href="<?php echo $orders->getReturnUrl() ?>"><?php echo $Language->Phrase("GoBack") ?></a></span></p>
<?php
if (EW_DEBUG_ENABLED)
	echo ew_DebugMsg();
$orders_edit->ShowMessage();
?>
<form name="fordersedit" id="fordersedit" action="<?php echo ew_CurrentPage() ?>" method="post" enctype="multipart/form-data" onsubmit="return orders_edit.ValidateForm(this);">
<p>
<input type="hidden" name="a_table" id="a_table" value="orders">
<input type="hidden" name="a_edit" id="a_edit" value="U">
<input type="hidden" name="k_hash" id="k_hash" value="<?php echo $orders_edit->HashValue ?>">
<table cellspacing="0" cellpadding="0"><tr><td>
<div id="orders_edit" class="yui-navset">
	<ul class="yui-nav">
		<li class="selected"><a href="#tab_orders_1"><em><span class="phpmaker"><?php echo $orders->PageCaption(1) ?></span></em></a></li>
		<li><a href="#tab_orders_2"><em><span class="phpmaker"><?php echo $orders->PageCaption(2) ?></span></em></a></li>
	</ul>            
	<div class="yui-content">
		<div id="tab_orders_1">
<table cellspacing="0" class="ewGrid" style="width: 100%"><tr><td class="ewGridContent">
<div class="ewGridMiddlePanel">
<table cellspacing="0" class="ewTable">
<?php if ($orders->id->Visible) { // id ?>
	<tr<?php echo $orders->id->RowAttributes ?>>
		<td class="ewTableHeader"><?php echo $orders->id->FldCaption() ?></td>
		<td<?php echo $orders->id->CellAttributes() ?>><span id="el_id">
<div<?php echo $orders->id->ViewAttributes() ?>><?php echo $orders->id->EditValue ?></div><input type="hidden" name="x_id" id="x_id" value="<?php echo ew_HtmlEncode($orders->id->CurrentValue) ?>">
</span><?php echo $orders->id->CustomMsg ?></td>
	</tr>
<?php } ?>
<?php if ($orders->lesson->Visible) { // lesson ?>
	<tr<?php echo $orders->lesson->RowAttributes ?>>
		<td class="ewTableHeader"><?php echo $orders->lesson->FldCaption() ?><?php echo $Language->Phrase("FieldRequiredIndicator") ?></td>
		<td<?php echo $orders->lesson->CellAttributes() ?>><span id="el_lesson">
<?php if ($orders->lesson->getSessionValue() <> "") { ?>
<div<?php echo $orders->lesson->ViewAttributes() ?>>
<?php if ($orders->lesson->HrefValue <> "" || $orders->lesson->TooltipValue <> "" || $orders->note->ViewValue <> "") { ?>
<a href="javascript:void(0);" onmouseover="ew_ShowTooltip(this, 'tt_orders_x_lesson');" onmouseout="ew_HideTooltip();"><?php echo $orders->lesson->ViewValue ?></a>
<?php } else { ?>
<?php echo $orders->lesson->ViewValue ?>
<?php } ?>
</div><div id="tt_orders_x_lesson" style="display: none">
<?php if ($orders->lesson->TooltipValue <> "") { ?>
<?php echo $orders->lesson->TooltipValue ?>
<?php } else { ?>
<div<?php echo $orders->note->ViewAttributes() ?>><?php echo $orders->note->ViewValue ?></div>
<?php } ?>
</div>
<input type="hidden" id="x_lesson" name="x_lesson" value="<?php echo ew_HtmlEncode($orders->lesson->CurrentValue) ?>">
<?php } else { ?>
<select id="x_lesson" name="x_lesson" title="<?php echo $orders->lesson->FldTitle() ?>"<?php echo $orders->lesson->EditAttributes() ?>>
<?php
if (is_array($orders->lesson->EditValue)) {
	$arwrk = $orders->lesson->EditValue;
	$rowswrk = count($arwrk);
	$emptywrk = TRUE;
	for ($rowcntwrk = 0; $rowcntwrk < $rowswrk; $rowcntwrk++) {
		$selwrk = (strval($orders->lesson->CurrentValue) == strval($arwrk[$rowcntwrk][0])) ? " selected=\"selected\"" : "";
		if ($selwrk <> "") $emptywrk = FALSE;
?>
<option value="<?php echo ew_HtmlEncode($arwrk[$rowcntwrk][0]) ?>"<?php echo $selwrk ?>>
<?php echo $arwrk[$rowcntwrk][1] ?>
</option>
<?php
	}
}
?>
</select>
<?php } ?>
</span><?php echo $orders->lesson->CustomMsg ?></td>
	</tr>
<?php } ?>
<?php if ($orders->title->Visible) { // title ?>
	<tr<?php echo $orders->title->RowAttributes ?>>
		<td class="ewTableHeader"><?php echo $orders->title->FldCaption() ?><?php echo $Language->Phrase("FieldRequiredIndicator") ?></td>
		<td<?php echo $orders->title->CellAttributes() ?>><span id="el_title">
<input type="text" name="x_title" id="x_title" title="<?php echo $orders->title->FldTitle() ?>" size="30" maxlength="25" value="<?php echo $orders->title->EditValue ?>"<?php echo $orders->title->EditAttributes() ?>>
</span><?php echo $orders->title->CustomMsg ?></td>
	</tr>
<?php } ?>
<?php if ($orders->room->Visible) { // room ?>
	<tr<?php echo $orders->room->RowAttributes ?>>
		<td class="ewTableHeader"><?php echo $orders->room->FldCaption() ?></td>
		<td<?php echo $orders->room->CellAttributes() ?>><span id="el_room">
<?php if ($orders->room->getSessionValue() <> "") { ?>
<div<?php echo $orders->room->ViewAttributes() ?>><?php echo $orders->room->ViewValue ?></div>
<input type="hidden" id="x_room" name="x_room" value="<?php echo ew_HtmlEncode($orders->room->CurrentValue) ?>">
<?php } else { ?>
<select id="x_room" name="x_room" title="<?php echo $orders->room->FldTitle() ?>"<?php echo $orders->room->EditAttributes() ?>>
<?php
if (is_array($orders->room->EditValue)) {
	$arwrk = $orders->room->EditValue;
	$rowswrk = count($arwrk);
	$emptywrk = TRUE;
	for ($rowcntwrk = 0; $rowcntwrk < $rowswrk; $rowcntwrk++) {
		$selwrk = (strval($orders->room->CurrentValue) == strval($arwrk[$rowcntwrk][0])) ? " selected=\"selected\"" : "";
		if ($selwrk <> "") $emptywrk = FALSE;
?>
<option value="<?php echo ew_HtmlEncode($arwrk[$rowcntwrk][0]) ?>"<?php echo $selwrk ?>>
<?php echo $arwrk[$rowcntwrk][1] ?>
</option>
<?php
	}
}
?>
</select>
<?php } ?>
</span><?php echo $orders->room->CustomMsg ?></td>
	</tr>
<?php } ?>
<?php if ($orders->request_date->Visible) { // request_date ?>
	<tr<?php echo $orders->request_date->RowAttributes ?>>
		<td class="ewTableHeader"><?php echo $orders->request_date->FldCaption() ?><?php echo $Language->Phrase("FieldRequiredIndicator") ?></td>
		<td<?php echo $orders->request_date->CellAttributes() ?>><span id="el_request_date">
<input type="text" name="x_request_date" id="x_request_date" title="<?php echo $orders->request_date->FldTitle() ?>" value="<?php echo $orders->request_date->EditValue ?>"<?php echo $orders->request_date->EditAttributes() ?>>
&nbsp;<img src="images/calendar.png" id="cal_x_request_date" name="cal_x_request_date" alt="<?php echo $Language->Phrase("PickDate") ?>" title="<?php echo $Language->Phrase("PickDate") ?>" style="cursor:pointer;cursor:hand;">
<script type="text/javascript">
Calendar.setup({
	inputField: "x_request_date", // input field id
	ifFormat: "%Y/%m/%d", // date format
	button: "cal_x_request_date" // button id
});
</script>
</span><?php echo $orders->request_date->CustomMsg ?></td>
	</tr>
<?php } ?>
<?php if ($orders->return_date->Visible) { // return_date ?>
	<tr<?php echo $orders->return_date->RowAttributes ?>>
		<td class="ewTableHeader"><?php echo $orders->return_date->FldCaption() ?><?php echo $Language->Phrase("FieldRequiredIndicator") ?></td>
		<td<?php echo $orders->return_date->CellAttributes() ?>><span id="el_return_date">
<input type="text" name="x_return_date" id="x_return_date" title="<?php echo $orders->return_date->FldTitle() ?>" value="<?php echo $orders->return_date->EditValue ?>"<?php echo $orders->return_date->EditAttributes() ?>>
&nbsp;<img src="images/calendar.png" id="cal_x_return_date" name="cal_x_return_date" alt="<?php echo $Language->Phrase("PickDate") ?>" title="<?php echo $Language->Phrase("PickDate") ?>" style="cursor:pointer;cursor:hand;">
<script type="text/javascript">
Calendar.setup({
	inputField: "x_return_date", // input field id
	ifFormat: "%Y/%m/%d", // date format
	button: "cal_x_return_date" // button id
});
</script>
</span><?php echo $orders->return_date->CustomMsg ?></td>
	</tr>
<?php } ?>
</table>
</div>
</td></tr></table>
		</div>
		<div id="tab_orders_2">
<table cellspacing="0" class="ewGrid" style="width: 100%"><tr><td class="ewGridContent">
<div class="ewGridMiddlePanel">
<table cellspacing="0" class="ewTable">
<?php if ($orders->attachment->Visible) { // attachment ?>
	<tr<?php echo $orders->attachment->RowAttributes ?>>
		<td class="ewTableHeader"><?php echo $orders->attachment->FldCaption() ?></td>
		<td<?php echo $orders->attachment->CellAttributes() ?>><span id="el_attachment">
<div id="old_x_attachment">
<?php if ($orders->attachment->HrefValue <> "" || $orders->attachment->TooltipValue <> "") { ?>
<?php if (!empty($orders->attachment->Upload->DbValue)) { ?>
<a href="<?php echo $orders->attachment->HrefValue ?>"><?php echo $orders->attachment->EditValue ?></a>
<?php } elseif (!in_array($orders->CurrentAction, array("I", "edit", "gridedit"))) { ?>	
&nbsp;
<?php } ?>
<?php } else { ?>
<?php if (!empty($orders->attachment->Upload->DbValue)) { ?>
<?php echo $orders->attachment->EditValue ?>
<?php } elseif (!in_array($orders->CurrentAction, array("I", "edit", "gridedit"))) { ?>	
&nbsp;
<?php } ?>
<?php } ?>
</div>
<div id="new_x_attachment">
<?php if (!empty($orders->attachment->Upload->DbValue)) { ?>
<label><input type="radio" name="a_attachment" id="a_attachment" value="1" checked="checked"><?php echo $Language->Phrase("Keep") ?></label>&nbsp;
<label><input type="radio" name="a_attachment" id="a_attachment" value="2"><?php echo $Language->Phrase("Remove") ?></label>&nbsp;
<label><input type="radio" name="a_attachment" id="a_attachment" value="3"><?php echo $Language->Phrase("Replace") ?><br></label>
<?php $orders->attachment->EditAttrs["onchange"] = "this.form.a_attachment[2].checked=true;" . @$orders->attachment->EditAttrs["onchange"]; ?>
<?php } else { ?>
<input type="hidden" name="a_attachment" id="a_attachment" value="3">
<?php } ?>
<input type="file" name="x_attachment" id="x_attachment" title="<?php echo $orders->attachment->FldTitle() ?>" size="30"<?php echo $orders->attachment->EditAttributes() ?>>
</div>
</span><?php echo $orders->attachment->CustomMsg ?></td>
	</tr>
<?php } ?>
<?php if ($orders->note->Visible) { // note ?>
	<tr<?php echo $orders->note->RowAttributes ?>>
		<td class="ewTableHeader"><?php echo $orders->note->FldCaption() ?></td>
		<td<?php echo $orders->note->CellAttributes() ?>><span id="el_note">
<textarea name="x_note" id="x_note" title="<?php echo $orders->note->FldTitle() ?>" cols="35" rows="4"<?php echo $orders->note->EditAttributes() ?>><?php echo $orders->note->EditValue ?></textarea>
<script type="text/javascript">
<!--
ew_DHTMLEditors.push(new ew_DHTMLEditor("x_note", function() {
	var oCKeditor = CKEDITOR.replace('x_note', { width: 35*_width_multiplier, height: 4*_height_multiplier, autoUpdateElement: false, baseHref: 'ckeditor/'});
	this.active = true;
}));
-->
</script>
</span><?php echo $orders->note->CustomMsg ?></td>
	</tr>
<?php } ?>
</table>
</div>
</td></tr></table>
		</div>
	</div>
</div>
</td></tr></table>
<script type="text/javascript">
<!--
ew_TabView(orders_edit);

//-->
</script>	
<p>
<?php if ($orders->UpdateConflict == "U") { // Record already updated by other user ?>
<input type="submit" name="btnReload" id="btnReload" value="<?php echo ew_BtnCaption($Language->Phrase("ReloadBtn")) ?>" onclick="this.form.a_edit.value='I';">
<input type="submit" name="btnAction" id="btnAction" value="<?php echo ew_BtnCaption($Language->Phrase("OverwriteBtn")) ?>" onclick="this.form.a_edit.value='overwrite';">
<?php } else { ?>
<input type="submit" name="btnAction" id="btnAction" value="<?php echo ew_BtnCaption($Language->Phrase("EditBtn")) ?>">
<?php } ?>
</form>
<script language="JavaScript" type="text/javascript">
<!--

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

</script>
<?php include "footer.php" ?>
<?php
$orders_edit->Page_Terminate();
?>
<?php

//
// Page class
//
class corders_edit {

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

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

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

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

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

	// Page URLs
	var $AddUrl;
	var $EditUrl;
	var $CopyUrl;
	var $DeleteUrl;
	var $ViewUrl;
	var $ListUrl;

	// Export URLs
	var $ExportPrintUrl;
	var $ExportHtmlUrl;
	var $ExportExcelUrl;
	var $ExportWordUrl;
	var $ExportXmlUrl;
	var $ExportCsvUrl;

	// Update URLs
	var $InlineAddUrl;
	var $InlineCopyUrl;
	var $InlineEditUrl;
	var $GridAddUrl;
	var $GridEditUrl;
	var $MultiDeleteUrl;
	var $MultiUpdateUrl;

	// 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() {
		$sMessage = $this->getMessage();
		$this->Message_Showing($sMessage);
		if ($sMessage <> "") { // Message in Session, display
			echo "<p><span class=\"ewMessage\">" . $sMessage . "</span></p>";
			$_SESSION[EW_SESSION_MESSAGE] = ""; // Clear message in Session
		}
	}

	// Validate page request
	function IsPageRequest() {
		global $objForm, $orders;
		if ($orders->UseTokenInUrl) {
			if ($objForm)
				return ($orders->TableVar == $objForm->GetValue("t"));
			if (@$_GET["t"] <> "")
				return ($orders->TableVar == $_GET["t"]);
		} else {
			return TRUE;
		}
	}

	//
	// Page class constructor
	//
	function corders_edit() {
		global $conn, $Language;

		// Language object
		$Language = new cLanguage();

		// Table object (orders)
		$GLOBALS["orders"] = new corders();

		// Table object (rooms)
		$GLOBALS['rooms'] = new crooms();

		// Table object (useraccounts)
		$GLOBALS['useraccounts'] = new cuseraccounts();

		// Table object (lesson)
		$GLOBALS['lesson'] = new clesson();

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

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

		// Start timer
		$GLOBALS["gsTimer"] = new cTimer();

		// Open connection
		$conn = ew_Connect();
	}

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

		// Security
		$Security = new cAdvancedSecurity();
		if (!$Security->IsLoggedIn()) $Security->AutoLogin();
		if (!$Security->IsLoggedIn()) {
			$Security->SaveLastUrl();
			$this->Page_Terminate("login.php");
		}
		$Security->TablePermission_Loading();
		$Security->LoadCurrentUserLevel($this->TableName);
		$Security->TablePermission_Loaded();
		if (!$Security->IsLoggedIn()) {
			$Security->SaveLastUrl();
			$this->Page_Terminate("login.php");
		}
		if (!$Security->CanEdit()) {
			$Security->SaveLastUrl();
			$this->Page_Terminate("orderslist.php");
		}
		$Security->UserID_Loading();
		if ($Security->IsLoggedIn()) $Security->LoadUserID();
		$Security->UserID_Loaded();
		if ($Security->IsLoggedIn() && $Security->CurrentUserID() == "") {
			$_SESSION[EW_SESSION_MESSAGE] = $Language->Phrase("NoPermission");
			$this->Page_Terminate("orderslist.php");
		}

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

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

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

	//
	// Page_Terminate
	//
	function Page_Terminate($url = "") {
		global $conn;

		// Page Unload event
		$this->Page_Unload();

		// Global Page Unloaded event (in userfn*.php)
		Page_Unloaded();

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

		// Go to URL if specified
		$this->Page_Redirecting($url);
		if ($url <> "") {
			if (!EW_DEBUG_ENABLED && ob_get_length())
				ob_end_clean();
			header("Location: " . $url);
		}
		exit();
	}
	var $sDbMasterFilter;
	var $sDbDetailFilter;
	var $HashValue; // Hash Value

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

		// Load key from QueryString
		if (@$_GET["id"] <> "")
			$orders->id->setQueryStringValue($_GET["id"]);

		// Set up master detail parameters
		$this->SetUpMasterDetail();
		if (@$_POST["a_edit"] <> "") {
			$orders->CurrentAction = $_POST["a_edit"]; // Get action code
			$this->GetUploadFiles(); // Get upload files
			$this->LoadFormValues(); // Get form values

			// Overwrite record, reload hash value
			if ($orders->CurrentAction == "overwrite") {
				$this->LoadRowHash();
				$orders->CurrentAction = "U";
			}

			// Validate form
			if (!$this->ValidateForm()) {
				$orders->CurrentAction = ""; // Form error, reset action
				$this->setMessage($gsFormError);
				$orders->EventCancelled = TRUE; // Event cancelled
				$this->RestoreFormValues();
			}
		} else {
			$orders->CurrentAction = "I"; // Default action is display
		}

		// Check if valid key
		if ($orders->id->CurrentValue == "")
			$this->Page_Terminate("orderslist.php"); // Invalid key, return to list
		switch ($orders->CurrentAction) {
			case "I": // Get a record to display
				if (!$this->LoadRow()) { // Load record based on key
					$this->setMessage($Language->Phrase("NoRecord")); // No record found
					$this->Page_Terminate("orderslist.php"); // No matching record, return to list
				}
				break;
			Case "U": // Update
				$orders->SendEmail = TRUE; // Send email on update success
				if ($this->EditRow()) { // Update record based on key
					$this->setMessage($Language->Phrase("UpdateSuccess")); // Update success
					$sReturnUrl = $orders->getReturnUrl();
					if (ew_GetPageName($sReturnUrl) == "ordersview.php")
						$sReturnUrl = $orders->ViewUrl(); // View paging, return to View page directly
					$this->Page_Terminate($sReturnUrl); // Return to caller
				} else {
					$orders->EventCancelled = TRUE; // Event cancelled
					$this->RestoreFormValues(); // Restore form values if update failed
				}
		}

		// Render the record
		$orders->RowType = EW_ROWTYPE_EDIT; // Render as Edit
		$this->RenderRow();
	}

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

		// Get upload data
			$orders->attachment->Upload->Index = $objForm->Index;
			if ($orders->attachment->Upload->UploadFile()) {

				// No action required
			} else {
				echo $orders->attachment->Upload->Message;
				$this->Page_Terminate();
				exit();
			}
	}

	// Load form values
	function LoadFormValues() {

		// Load from form
		global $objForm, $orders;
		$orders->id->setFormValue($objForm->GetValue("x_id"));
		$orders->lesson->setFormValue($objForm->GetValue("x_lesson"));
		$orders->title->setFormValue($objForm->GetValue("x_title"));
		$orders->room->setFormValue($objForm->GetValue("x_room"));
		$orders->order_date->setFormValue($objForm->GetValue("x_order_date"));
		$orders->order_date->CurrentValue = ew_UnFormatDateTime($orders->order_date->CurrentValue, 5);
		$orders->request_date->setFormValue($objForm->GetValue("x_request_date"));
		$orders->request_date->CurrentValue = ew_UnFormatDateTime($orders->request_date->CurrentValue, 5);
		$orders->return_date->setFormValue($objForm->GetValue("x_return_date"));
		$orders->return_date->CurrentValue = ew_UnFormatDateTime($orders->return_date->CurrentValue, 5);
		$orders->teacher->setFormValue($objForm->GetValue("x_teacher"));
		$orders->note->setFormValue($objForm->GetValue("x_note"));
		if ($orders->CurrentAction <> "overwrite")
			$this->HashValue = $objForm->GetValue("k_hash");
	}

	// Restore form values
	function RestoreFormValues() {
		global $objForm, $orders;
		$this->LoadRow();
		$orders->id->CurrentValue = $orders->id->FormValue;
		$orders->lesson->CurrentValue = $orders->lesson->FormValue;
		$orders->title->CurrentValue = $orders->title->FormValue;
		$orders->room->CurrentValue = $orders->room->FormValue;
		$orders->order_date->CurrentValue = $orders->order_date->FormValue;
		$orders->order_date->CurrentValue = ew_UnFormatDateTime($orders->order_date->CurrentValue, 5);
		$orders->request_date->CurrentValue = $orders->request_date->FormValue;
		$orders->request_date->CurrentValue = ew_UnFormatDateTime($orders->request_date->CurrentValue, 5);
		$orders->return_date->CurrentValue = $orders->return_date->FormValue;
		$orders->return_date->CurrentValue = ew_UnFormatDateTime($orders->return_date->CurrentValue, 5);
		$orders->teacher->CurrentValue = $orders->teacher->FormValue;
		$orders->note->CurrentValue = $orders->note->FormValue;
		if ($orders->CurrentAction <> "overwrite")
			$this->HashValue = $objForm->GetValue("k_hash");
	}

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

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

		// Load SQL based on filter
		$orders->CurrentFilter = $sFilter;
		$sSql = $orders->SQL();
		$res = FALSE;
		$rs = ew_LoadRecordset($sSql);
		if ($rs && !$rs->EOF) {
			$res = TRUE;
			$this->LoadRowValues($rs); // Load row values
			if (!$orders->EventCancelled)
				$this->HashValue = $this->GetRsHash($rs); // Get hash value for record

			// Call Row Selected event
			$orders->Row_Selected($rs);
			$rs->Close();
		}
		return $res;
	}

	// Load row values from recordset
	function LoadRowValues(&$rs) {
		global $conn, $orders;
		$orders->id->setDbValue($rs->fields('id'));
		$orders->lesson->setDbValue($rs->fields('lesson'));
		$orders->title->setDbValue($rs->fields('title'));
		$orders->room->setDbValue($rs->fields('room'));
		$orders->order_date->setDbValue($rs->fields('order_date'));
		$orders->request_date->setDbValue($rs->fields('request_date'));
		$orders->return_date->setDbValue($rs->fields('return_date'));
		$orders->teacher->setDbValue($rs->fields('teacher'));
		$orders->attachment->Upload->DbValue = $rs->fields('attachment');
		$orders->note->setDbValue($rs->fields('note'));
	}

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

		// Initialize URLs
		// Call Row_Rendering event

		$orders->Row_Rendering();

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

		$orders->id->CellCssStyle = ""; $orders->id->CellCssClass = "";
		$orders->id->CellAttrs = array(); $orders->id->ViewAttrs = array(); $orders->id->EditAttrs = array();

		// lesson
		$orders->lesson->CellCssStyle = ""; $orders->lesson->CellCssClass = "";
		$orders->lesson->CellAttrs = array(); $orders->lesson->ViewAttrs = array(); $orders->lesson->EditAttrs = array();

		// title
		$orders->title->CellCssStyle = ""; $orders->title->CellCssClass = "";
		$orders->title->CellAttrs = array(); $orders->title->ViewAttrs = array(); $orders->title->EditAttrs = array();

		// room
		$orders->room->CellCssStyle = ""; $orders->room->CellCssClass = "";
		$orders->room->CellAttrs = array(); $orders->room->ViewAttrs = array(); $orders->room->EditAttrs = array();

		// order_date
		$orders->order_date->CellCssStyle = ""; $orders->order_date->CellCssClass = "";
		$orders->order_date->CellAttrs = array(); $orders->order_date->ViewAttrs = array(); $orders->order_date->EditAttrs = array();

		// request_date
		$orders->request_date->CellCssStyle = ""; $orders->request_date->CellCssClass = "";
		$orders->request_date->CellAttrs = array(); $orders->request_date->ViewAttrs = array(); $orders->request_date->EditAttrs = array();

		// return_date
		$orders->return_date->CellCssStyle = ""; $orders->return_date->CellCssClass = "";
		$orders->return_date->CellAttrs = array(); $orders->return_date->ViewAttrs = array(); $orders->return_date->EditAttrs = array();

		// teacher
		$orders->teacher->CellCssStyle = ""; $orders->teacher->CellCssClass = "";
		$orders->teacher->CellAttrs = array(); $orders->teacher->ViewAttrs = array(); $orders->teacher->EditAttrs = array();

		// attachment
		$orders->attachment->CellCssStyle = ""; $orders->attachment->CellCssClass = "";
		$orders->attachment->CellAttrs = array(); $orders->attachment->ViewAttrs = array(); $orders->attachment->EditAttrs = array();

		// note
		$orders->note->CellCssStyle = ""; $orders->note->CellCssClass = "";
		$orders->note->CellAttrs = array(); $orders->note->ViewAttrs = array(); $orders->note->EditAttrs = array();
		if ($orders->RowType == EW_ROWTYPE_VIEW) { // View row

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

			// lesson
			if (strval($orders->lesson->CurrentValue) <> "") {
				$sFilterWrk = "`id` = " . ew_AdjustSql($orders->lesson->CurrentValue) . "";
			$sSqlWrk = "SELECT `name` FROM `lesson`";
			$sWhereWrk = "";
			if ($sFilterWrk <> "") {
				if ($sWhereWrk <> "") $sWhereWrk .= " AND ";
				$sWhereWrk .= "(" . $sFilterWrk . ")";
			}
			if ($sWhereWrk <> "") $sSqlWrk .= " WHERE " . $sWhereWrk;
			$sSqlWrk .= " ORDER BY `id` Asc";
				$rswrk = $conn->Execute($sSqlWrk);
				if ($rswrk && !$rswrk->EOF) { // Lookup values found
					$orders->lesson->ViewValue = $rswrk->fields('name');
					$rswrk->Close();
				} else {
					$orders->lesson->ViewValue = $orders->lesson->CurrentValue;
				}
			} else {
				$orders->lesson->ViewValue = NULL;
			}
			$orders->lesson->CssStyle = "";
			$orders->lesson->CssClass = "";
			$orders->lesson->ViewCustomAttributes = "";

			// title
			$orders->title->ViewValue = $orders->title->CurrentValue;
			$orders->title->CssStyle = "";
			$orders->title->CssClass = "";
			$orders->title->ViewCustomAttributes = "";

			// room
			if (strval($orders->room->CurrentValue) <> "") {
				$sFilterWrk = "`id` = '" . ew_AdjustSql($orders->room->CurrentValue) . "'";
			$sSqlWrk = "SELECT `name` FROM `rooms`";
			$sWhereWrk = "";
			if ($sFilterWrk <> "") {
				if ($sWhereWrk <> "") $sWhereWrk .= " AND ";
				$sWhereWrk .= "(" . $sFilterWrk . ")";
			}
			if ($sWhereWrk <> "") $sSqlWrk .= " WHERE " . $sWhereWrk;
			$sSqlWrk .= " ORDER BY `name` Asc";
				$rswrk = $conn->Execute($sSqlWrk);
				if ($rswrk && !$rswrk->EOF) { // Lookup values found
					$orders->room->ViewValue = $rswrk->fields('name');
					$rswrk->Close();
				} else {
					$orders->room->ViewValue = $orders->room->CurrentValue;
				}
			} else {
				$orders->room->ViewValue = NULL;
			}
			$orders->room->CssStyle = "";
			$orders->room->CssClass = "";
			$orders->room->ViewCustomAttributes = "";

			// order_date
			$orders->order_date->ViewValue = $orders->order_date->CurrentValue;
			$orders->order_date->ViewValue = ew_FormatDateTime($orders->order_date->ViewValue, 5);
			$orders->order_date->CssStyle = "";
			$orders->order_date->CssClass = "";
			$orders->order_date->ViewCustomAttributes = "";

			// request_date
			$orders->request_date->ViewValue = $orders->request_date->CurrentValue;
			$orders->request_date->ViewValue = ew_FormatDateTime($orders->request_date->ViewValue, 5);
			$orders->request_date->CssStyle = "";
			$orders->request_date->CssClass = "";
			$orders->request_date->ViewCustomAttributes = "";

			// return_date
			$orders->return_date->ViewValue = $orders->return_date->CurrentValue;
			$orders->return_date->ViewValue = ew_FormatDateTime($orders->return_date->ViewValue, 5);
			$orders->return_date->CssStyle = "";
			$orders->return_date->CssClass = "";
			$orders->return_date->ViewCustomAttributes = "";

			// teacher
			$orders->teacher->ViewValue = $orders->teacher->CurrentValue;
			$orders->teacher->CssStyle = "";
			$orders->teacher->CssClass = "";
			$orders->teacher->ViewCustomAttributes = "";

			// attachment
			if (!ew_Empty($orders->attachment->Upload->DbValue)) {
				$orders->attachment->ViewValue = $orders->attachment->Upload->DbValue;
			} else {
				$orders->attachment->ViewValue = "";
			}
			$orders->attachment->CssStyle = "";
			$orders->attachment->CssClass = "";
			$orders->attachment->ViewCustomAttributes = "";

			// note
			$orders->note->ViewValue = $orders->note->CurrentValue;
			$orders->note->CssStyle = "";
			$orders->note->CssClass = "";
			$orders->note->ViewCustomAttributes = "";

			// id
			$orders->id->HrefValue = "";
			$orders->id->TooltipValue = "";

			// lesson
			$orders->lesson->HrefValue = "";
			$orders->lesson->TooltipValue = $orders->note->CurrentValue;

			// title
			$orders->title->HrefValue = "";
			$orders->title->TooltipValue = "";

			// room
			$orders->room->HrefValue = "";
			$orders->room->TooltipValue = "";

			// order_date
			$orders->order_date->HrefValue = "";
			$orders->order_date->TooltipValue = "";

			// request_date
			$orders->request_date->HrefValue = "";
			$orders->request_date->TooltipValue = "";

			// return_date
			$orders->return_date->HrefValue = "";
			$orders->return_date->TooltipValue = "";

			// teacher
			$orders->teacher->HrefValue = "";
			$orders->teacher->TooltipValue = "";

			// attachment
			if (!ew_Empty($orders->attachment->Upload->DbValue)) {
				$orders->attachment->HrefValue = ew_UploadPathEx(FALSE, $orders->attachment->UploadPath) . ((!empty($orders->attachment->ViewValue)) ? $orders->attachment->ViewValue : $orders->attachment->CurrentValue);
				if ($orders->Export <> "") $orders->attachment->HrefValue = ew_ConvertFullUrl($orders->attachment->HrefValue);
			} else {
				$orders->attachment->HrefValue = "";
			}
			$orders->attachment->TooltipValue = "";

			// note
			$orders->note->HrefValue = "";
			$orders->note->TooltipValue = "";
		} elseif ($orders->RowType == EW_ROWTYPE_EDIT) { // Edit row

			// id
			$orders->id->EditCustomAttributes = "";
			$orders->id->EditValue = $orders->id->CurrentValue;
			$orders->id->CssStyle = "";
			$orders->id->CssClass = "";
			$orders->id->ViewCustomAttributes = "";

			// lesson
			$orders->lesson->EditCustomAttributes = "";
			if ($orders->lesson->getSessionValue() <> "") {
				$orders->lesson->CurrentValue = $orders->lesson->getSessionValue();
			if (strval($orders->lesson->CurrentValue) <> "") {
				$sFilterWrk = "`id` = " . ew_AdjustSql($orders->lesson->CurrentValue) . "";
			$sSqlWrk = "SELECT `name` FROM `lesson`";
			$sWhereWrk = "";
			if ($sFilterWrk <> "") {
				if ($sWhereWrk <> "") $sWhereWrk .= " AND ";
				$sWhereWrk .= "(" . $sFilterWrk . ")";
			}
			if ($sWhereWrk <> "") $sSqlWrk .= " WHERE " . $sWhereWrk;
			$sSqlWrk .= " ORDER BY `id` Asc";
				$rswrk = $conn->Execute($sSqlWrk);
				if ($rswrk && !$rswrk->EOF) { // Lookup values found
					$orders->lesson->ViewValue = $rswrk->fields('name');
					$rswrk->Close();
				} else {
					$orders->lesson->ViewValue = $orders->lesson->CurrentValue;
				}
			} else {
				$orders->lesson->ViewValue = NULL;
			}
			$orders->lesson->CssStyle = "";
			$orders->lesson->CssClass = "";
			$orders->lesson->ViewCustomAttributes = "";
			} else {
				$sFilterWrk = "";
			$sSqlWrk = "SELECT `id`, `name`, '' AS Disp2Fld, '' AS SelectFilterFld FROM `lesson`";
			$sWhereWrk = "";
			if ($sFilterWrk <> "") {
				if ($sWhereWrk <> "") $sWhereWrk .= " AND ";
				$sWhereWrk .= "(" . $sFilterWrk . ")";
			}
			if ($sWhereWrk <> "") $sSqlWrk .= " WHERE " . $sWhereWrk;
			$sSqlWrk .= " ORDER BY `id` Asc";
			$rswrk = $conn->Execute($sSqlWrk);
			$arwrk = ($rswrk) ? $rswrk->GetRows() : array();
			if ($rswrk) $rswrk->Close();
			array_unshift($arwrk, array("", $Language->Phrase("PleaseSelect")));
			$orders->lesson->EditValue = $arwrk;
			}

			// title
			$orders->title->EditCustomAttributes = "";
			$orders->title->EditValue = ew_HtmlEncode($orders->title->CurrentValue);

			// room
			$orders->room->EditCustomAttributes = "";
			if ($orders->room->getSessionValue() <> "") {
				$orders->room->CurrentValue = $orders->room->getSessionValue();
			if (strval($orders->room->CurrentValue) <> "") {
				$sFilterWrk = "`id` = '" . ew_AdjustSql($orders->room->CurrentValue) . "'";
			$sSqlWrk = "SELECT `name` FROM `rooms`";
			$sWhereWrk = "";
			if ($sFilterWrk <> "") {
				if ($sWhereWrk <> "") $sWhereWrk .= " AND ";
				$sWhereWrk .= "(" . $sFilterWrk . ")";
			}
			if ($sWhereWrk <> "") $sSqlWrk .= " WHERE " . $sWhereWrk;
			$sSqlWrk .= " ORDER BY `name` Asc";
				$rswrk = $conn->Execute($sSqlWrk);
				if ($rswrk && !$rswrk->EOF) { // Lookup values found
					$orders->room->ViewValue = $rswrk->fields('name');
					$rswrk->Close();
				} else {
					$orders->room->ViewValue = $orders->room->CurrentValue;
				}
			} else {
				$orders->room->ViewValue = NULL;
			}
			$orders->room->CssStyle = "";
			$orders->room->CssClass = "";
			$orders->room->ViewCustomAttributes = "";
			} else {
				$sFilterWrk = "";
			$sSqlWrk = "SELECT `id`, `name`, '' AS Disp2Fld, '' AS SelectFilterFld FROM `rooms`";
			$sWhereWrk = "";
			if ($sFilterWrk <> "") {
				if ($sWhereWrk <> "") $sWhereWrk .= " AND ";
				$sWhereWrk .= "(" . $sFilterWrk . ")";
			}
			if ($sWhereWrk <> "") $sSqlWrk .= " WHERE " . $sWhereWrk;
			$sSqlWrk .= " ORDER BY `name` Asc";
			$rswrk = $conn->Execute($sSqlWrk);
			$arwrk = ($rswrk) ? $rswrk->GetRows() : array();
			if ($rswrk) $rswrk->Close();
			array_unshift($arwrk, array("", $Language->Phrase("PleaseSelect")));
			$orders->room->EditValue = $arwrk;
			}

			// order_date
			// request_date

			$orders->request_date->EditCustomAttributes = "";
			$orders->request_date->EditValue = ew_HtmlEncode(ew_FormatDateTime($orders->request_date->CurrentValue, 5));

			// return_date
			$orders->return_date->EditCustomAttributes = "";
			$orders->return_date->EditValue = ew_HtmlEncode(ew_FormatDateTime($orders->return_date->CurrentValue, 5));

			// teacher
			// attachment

			$orders->attachment->EditCustomAttributes = "";
			if (!ew_Empty($orders->attachment->Upload->DbValue)) {
				$orders->attachment->EditValue = $orders->attachment->Upload->DbValue;
			} else {
				$orders->attachment->EditValue = "";
			}

			// note
			$orders->note->EditCustomAttributes = "";
			$orders->note->EditValue = ew_HtmlEncode($orders->note->CurrentValue);

			// Edit refer script
			// id

			$orders->id->HrefValue = "";

			// lesson
			$orders->lesson->HrefValue = "";

			// title
			$orders->title->HrefValue = "";

			// room
			$orders->room->HrefValue = "";

			// order_date
			$orders->order_date->HrefValue = "";

			// request_date
			$orders->request_date->HrefValue = "";

			// return_date
			$orders->return_date->HrefValue = "";

			// teacher
			$orders->teacher->HrefValue = "";

			// attachment
			if (!ew_Empty($orders->attachment->Upload->DbValue)) {
				$orders->attachment->HrefValue = ew_UploadPathEx(FALSE, $orders->attachment->UploadPath) . ((!empty($orders->attachment->EditValue)) ? $orders->attachment->EditValue : $orders->attachment->CurrentValue);
				if ($orders->Export <> "") $orders->attachment->HrefValue = ew_ConvertFullUrl($orders->attachment->HrefValue);
			} else {
				$orders->attachment->HrefValue = "";
			}

			// note
			$orders->note->HrefValue = "";
		}

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

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

		// Initialize form error message
		$gsFormError = "";
		if (!ew_CheckFileType($orders->attachment->Upload->FileName)) {
			$gsFormError .= ($gsFormError <> "") ? "<br>" : "";
			$gsFormError .= $Language->Phrase("WrongFileType");
		}
		if ($orders->attachment->Upload->FileSize > 0 && EW_MAX_FILE_SIZE > 0 && $orders->attachment->Upload->FileSize > EW_MAX_FILE_SIZE) {
			$gsFormError .= ($gsFormError <> "") ? "<br>" : "";
			$gsFormError .= str_replace("%s", EW_MAX_FILE_SIZE, $Language->Phrase("MaxFileSize"));
		}

		// Check if validation required
		if (!EW_SERVER_VALIDATE)
			return ($gsFormError == "");
		if (!is_null($orders->lesson->FormValue) && $orders->lesson->FormValue == "") {
			$gsFormError .= ($gsFormError <> "") ? "<br>" : "";
			$gsFormError .= $Language->Phrase("EnterRequiredField") . " - " . $orders->lesson->FldCaption();
		}
		if (!is_null($orders->title->FormValue) && $orders->title->FormValue == "") {
			$gsFormError .= ($gsFormError <> "") ? "<br>" : "";
			$gsFormError .= $Language->Phrase("EnterRequiredField") . " - " . $orders->title->FldCaption();
		}
		if (!is_null($orders->request_date->FormValue) && $orders->request_date->FormValue == "") {
			$gsFormError .= ($gsFormError <> "") ? "<br>" : "";
			$gsFormError .= $Language->Phrase("EnterRequiredField") . " - " . $orders->request_date->FldCaption();
		}
		if (!ew_CheckDate($orders->request_date->FormValue)) {
			if ($gsFormError <> "") $gsFormError .= "<br>";
			$gsFormError .= $orders->request_date->FldErrMsg();
		}
		if (!is_null($orders->return_date->FormValue) && $orders->return_date->FormValue == "") {
			$gsFormError .= ($gsFormError <> "") ? "<br>" : "";
			$gsFormError .= $Language->Phrase("EnterRequiredField") . " - " . $orders->return_date->FldCaption();
		}
		if (!ew_CheckDate($orders->return_date->FormValue)) {
			if ($gsFormError <> "") $gsFormError .= "<br>";
			$gsFormError .= $orders->return_date->FldErrMsg();
		}

		// 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;
	}

	// Update record based on key values
	function EditRow() {
		global $conn, $Security, $Language, $orders;
		$sFilter = $orders->KeyFilter();
		$orders->CurrentFilter = $sFilter;
		$sSql = $orders->SQL();
		$conn->raiseErrorFn = 'ew_ErrorFn';
		$rs = $conn->Execute($sSql);
		$conn->raiseErrorFn = '';
		if ($rs === FALSE)
			return FALSE;
		if ($rs->EOF) {
			$EditRow = FALSE; // Update Failed
		} else {

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

			// lesson
			$orders->lesson->SetDbValueDef($rsnew, $orders->lesson->CurrentValue, 0, FALSE);

			// title
			$orders->title->SetDbValueDef($rsnew, $orders->title->CurrentValue, "", FALSE);

			// room
			$orders->room->SetDbValueDef($rsnew, $orders->room->CurrentValue, NULL, FALSE);

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

			// request_date
			$orders->request_date->SetDbValueDef($rsnew, ew_UnFormatDateTime($orders->request_date->CurrentValue, 5, FALSE), ew_CurrentDate());

			// return_date
			$orders->return_date->SetDbValueDef($rsnew, ew_UnFormatDateTime($orders->return_date->CurrentValue, 5, FALSE), ew_CurrentDate());

			// teacher
			$orders->teacher->SetDbValueDef($rsnew, CurrentUserName(), "");
			$rsnew['teacher'] =& $orders->teacher->DbValue;

			// attachment
			$orders->attachment->Upload->SaveToSession(); // Save file value to Session
						if ($orders->attachment->Upload->Action == "2" || $orders->attachment->Upload->Action == "3") { // Update/Remove
			$orders->attachment->Upload->DbValue = $rs->fields('attachment'); // Get original value
			if (is_null($orders->attachment->Upload->Value)) {
				$rsnew['attachment'] = NULL;
			} else {
				if ($orders->attachment->Upload->FileName == $orders->attachment->Upload->DbValue) { // Upload file name same as old file name
					$rsnew['attachment'] = $orders->attachment->Upload->FileName;
				} else {
					$rsnew['attachment'] = ew_UploadFileNameEx(ew_UploadPathEx(TRUE, $orders->attachment->UploadPath), $orders->attachment->Upload->FileName);
				}
			}
			}

			// note
			$orders->note->SetDbValueDef($rsnew, $orders->note->CurrentValue, NULL, FALSE);

			// Check hash value
			$bRowHasConflict = ($this->GetRsHash($rs) <> $this->HashValue);

			// Call Row Update Conflict event
			if ($bRowHasConflict)
				$bRowHasConflict = $orders->Row_UpdateConflict($rsold, $rsnew);
			if ($bRowHasConflict) {
				$this->setMessage($Language->Phrase("RecordChangedByOtherUser"));
				$orders->UpdateConflict = "U";
				$rs->Close();
				return FALSE; // Update Failed
			}

			// Call Row Updating event
			$bUpdateRow = $orders->Row_Updating($rsold, $rsnew);
			if ($bUpdateRow) {
			if (!ew_Empty($orders->attachment->Upload->Value)) {
				if ($orders->attachment->Upload->FileName == $orders->attachment->Upload->DbValue) { // Overwrite if same file name
					$orders->attachment->Upload->SaveToFile($orders->attachment->UploadPath, $rsnew['attachment'], TRUE);
					$orders->attachment->Upload->DbValue = ""; // No need to delete any more
				} else {
					$orders->attachment->Upload->SaveToFile($orders->attachment->UploadPath, $rsnew['attachment'], FALSE);
				}
			}
			if ($orders->attachment->Upload->Action == "2" || $orders->attachment->Upload->Action == "3") { // Update/Remove
				if ($orders->attachment->Upload->DbValue <> "")
					@unlink(ew_UploadPathEx(TRUE, $orders->attachment->UploadPath) . $orders->attachment->Upload->DbValue);
			}
				$conn->raiseErrorFn = 'ew_ErrorFn';
				$EditRow = $conn->Execute($orders->UpdateSQL($rsnew));
				$conn->raiseErrorFn = '';
			} else {
				if ($orders->CancelMessage <> "") {
					$this->setMessage($orders->CancelMessage);
					$orders->CancelMessage = "";
				} else {
					$this->setMessage($Language->Phrase("UpdateCancelled"));
				}
				$EditRow = FALSE;
			}
		}

		// Call Row_Updated event
		if ($EditRow)
			$orders->Row_Updated($rsold, $rsnew);
		if ($EditRow) {
			if ($orders->SendEmail)
				$this->SendEmailOnEdit($rsold, $rsnew);
		}
		$rs->Close();

		// attachment
		$orders->attachment->Upload->RemoveFromSession(); // Remove file value from Session
		return $EditRow;
	}

	// Load row hash
	function LoadRowHash() {
		global $conn, $orders;
		$sFilter = $orders->KeyFilter();

		// Load SQL based on filter
		$orders->CurrentFilter = $sFilter;
		$sSql = $orders->SQL();
		$RsRow = $conn->Execute($sSql);
		$this->HashValue = ($RsRow && !$RsRow->EOF) ? $this->GetRsHash($RsRow) : ""; // Get hash value for record
		$RsRow->Close();
	}

	// Get Rs Hash
	function GetRsHash(&$rs) {
		if (!$rs)
			return "";
		$sHash = "";
		$sHash .= ew_GetRsHash($rs, 'lesson'); // lesson
		$sHash .= ew_GetRsHash($rs, 'title'); // title
		$sHash .= ew_GetRsHash($rs, 'room'); // room
		$sHash .= ew_GetRsHash($rs, 'order_date'); // order_date
		$sHash .= ew_GetRsHash($rs, 'request_date'); // request_date
		$sHash .= ew_GetRsHash($rs, 'return_date'); // return_date
		$sHash .= ew_GetRsHash($rs, 'teacher'); // teacher
		$sHash .= ew_GetRsHash($rs, 'attachment'); // attachment
		$sHash .= ew_GetRsHash($rs, 'note'); // note
		return md5($sHash);
	}

	// Set up master/detail based on QueryString
	function SetUpMasterDetail() {
		global $orders;
		$bValidMaster = FALSE;

		// Get the keys for master table
		if (@$_GET[EW_TABLE_SHOW_MASTER] <> "") {
			$sMasterTblVar = $_GET[EW_TABLE_SHOW_MASTER];
			if ($sMasterTblVar == "") {
				$bValidMaster = TRUE;
				$this->sDbMasterFilter = "";
				$this->sDbDetailFilter = "";
			}
			if ($sMasterTblVar == "lesson") {
				$bValidMaster = TRUE;
				$this->sDbMasterFilter = $orders->SqlMasterFilter_lesson();
				$this->sDbDetailFilter = $orders->SqlDetailFilter_lesson();
				if (@$_GET["id"] <> "") {
					$GLOBALS["lesson"]->id->setQueryStringValue($_GET["id"]);
					$orders->lesson->setQueryStringValue($GLOBALS["lesson"]->id->QueryStringValue);
					$orders->lesson->setSessionValue($orders->lesson->QueryStringValue);
					if (!is_numeric($GLOBALS["lesson"]->id->QueryStringValue)) $bValidMaster = FALSE;
					$this->sDbMasterFilter = str_replace("@id@", ew_AdjustSql($GLOBALS["lesson"]->id->QueryStringValue), $this->sDbMasterFilter);
					$this->sDbDetailFilter = str_replace("@lesson@", ew_AdjustSql($GLOBALS["lesson"]->id->QueryStringValue), $this->sDbDetailFilter);
				} else {
					$bValidMaster = FALSE;
				}
			}
			if ($sMasterTblVar == "rooms") {
				$bValidMaster = TRUE;
				$this->sDbMasterFilter = $orders->SqlMasterFilter_rooms();
				$this->sDbDetailFilter = $orders->SqlDetailFilter_rooms();
				if (@$_GET["id"] <> "") {
					$GLOBALS["rooms"]->id->setQueryStringValue($_GET["id"]);
					$orders->room->setQueryStringValue($GLOBALS["rooms"]->id->QueryStringValue);
					$orders->room->setSessionValue($orders->room->QueryStringValue);
					$this->sDbMasterFilter = str_replace("@id@", ew_AdjustSql($GLOBALS["rooms"]->id->QueryStringValue), $this->sDbMasterFilter);
					$this->sDbDetailFilter = str_replace("@room@", ew_AdjustSql($GLOBALS["rooms"]->id->QueryStringValue), $this->sDbDetailFilter);
				} else {
					$bValidMaster = FALSE;
				}
			}
			if ($sMasterTblVar == "useraccounts") {
				$bValidMaster = TRUE;
				$this->sDbMasterFilter = $orders->SqlMasterFilter_useraccounts();
				$this->sDbDetailFilter = $orders->SqlDetailFilter_useraccounts();
				if (@$_GET["username"] <> "") {
					$GLOBALS["useraccounts"]->username->setQueryStringValue($_GET["username"]);
					$orders->teacher->setQueryStringValue($GLOBALS["useraccounts"]->username->QueryStringValue);
					$orders->teacher->setSessionValue($orders->teacher->QueryStringValue);
					$this->sDbMasterFilter = str_replace("@username@", ew_AdjustSql($GLOBALS["useraccounts"]->username->QueryStringValue), $this->sDbMasterFilter);
					$this->sDbDetailFilter = str_replace("@teacher@", ew_AdjustSql($GLOBALS["useraccounts"]->username->QueryStringValue), $this->sDbDetailFilter);
				} else {
					$bValidMaster = FALSE;
				}
			}
		}
		if ($bValidMaster) {

			// Save current master table
			$orders->setCurrentMasterTable($sMasterTblVar);

			// Reset start record counter (new master key)
			$this->lStartRec = 1;
			$orders->setStartRecordNumber($this->lStartRec);
			$orders->setMasterFilter($this->sDbMasterFilter); // Set up master filter
			$orders->setDetailFilter($this->sDbDetailFilter); // Set up detail filter

			// Clear previous master key from Session
			if ($sMasterTblVar <> "lesson") {
				if ($orders->lesson->QueryStringValue == "") $orders->lesson->setSessionValue("");
			}
			if ($sMasterTblVar <> "rooms") {
				if ($orders->room->QueryStringValue == "") $orders->room->setSessionValue("");
			}
			if ($sMasterTblVar <> "useraccounts") {
				if ($orders->teacher->QueryStringValue == "") $orders->teacher->setSessionValue("");
			}
		} else {
			$this->sDbMasterFilter = $orders->getMasterFilter(); //  Restore master filter
			$this->sDbDetailFilter = $orders->getDetailFilter(); // Restore detail filter
		}
	}

	// Send email after update success
	function SendEmailOnEdit(&$rsold, &$rsnew) {
		global $Language, $orders;
		$sFn = "txt/notify.txt";
		$sTable = 'orders';
		$sSubject = $sTable . " ". $Language->Phrase("RecordUpdated");
		$sAction = $Language->Phrase("ActionUpdated");

		// Get key value
		$sKey = "";
		if ($sKey <> "") $sKey .= EW_COMPOSITE_KEY_SEPARATOR;
		$sKey .= $rsold['id'];
		$Email = new cEmail();
		$Email->Load($sFn);
		$Email->ReplaceSender(EW_SENDER_EMAIL); // Replace Sender
		$Email->ReplaceRecipient(EW_RECIPIENT_EMAIL); // Replace Recipient
		$Email->ReplaceSubject($sSubject); // Replace Subject
		$Email->ReplaceContent("<!--table-->", $sTable);
		$Email->ReplaceContent("<!--key-->", $sKey);
		$Email->ReplaceContent("<!--action-->", $sAction);
		$Email->Charset = EW_EMAIL_CHARSET;
		$Args = array();
		$Args["rsold"] =& $rsold;
		$Args["rsnew"] =& $rsnew;
		$bEmailSent = FALSE;
		if ($orders->Email_Sending($Email, $Args))
			$bEmailSent = $Email->Send();

		// Send email failed
		if (!$bEmailSent)
			$this->setMessage($Email->SendErrDescription);
	}

	// 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
	function Message_Showing(&$msg) {

		// Example:
		//$msg = "your new message";

	}

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

		// Return error message in CustomError
		return TRUE;
	}
}
?>
Return current item: Science Lab Inventory & Order Management