Location: PHPKode > projects > Science Lab Inventory & Order Management > sciencelabinv/ordersadd.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_add = new corders_add();
$Page =& $orders_add;

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

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

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

// page properties
orders_add.PageID = "add"; // page ID
orders_add.FormID = "fordersadd"; // form ID
var EW_PAGE_ID = orders_add.PageID; // for backward compatibility

// extend page with ValidateForm function
orders_add.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_add.Form_CustomValidate =  
 function(fobj) { // DO NOT CHANGE THIS LINE!

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

// multi page properties
orders_add.MultiPage = new ew_MultiPage();
orders_add.MultiPage.AddElement("x_lesson", 1);
orders_add.MultiPage.AddElement("x_title", 1);
orders_add.MultiPage.AddElement("x_room", 1);
orders_add.MultiPage.AddElement("x_request_date", 1);
orders_add.MultiPage.AddElement("x_return_date", 1);
orders_add.MultiPage.AddElement("x_attachment", 2);
orders_add.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("Add") ?>&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_add->ShowMessage();
?>
<form name="fordersadd" id="fordersadd" action="<?php echo ew_CurrentPage() ?>" method="post" enctype="multipart/form-data" onsubmit="return orders_add.ValidateForm(this);">
<p>
<input type="hidden" name="t" id="t" value="orders">
<input type="hidden" name="a_add" id="a_add" value="A">
<table cellspacing="0" cellpadding="0"><tr><td>
<div id="orders_add" 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->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">
<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_add);

//-->
</script>	
<p>
<input type="submit" name="btnAction" id="btnAction" value="<?php echo ew_BtnCaption($Language->Phrase("AddBtn")) ?>">
</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_add->Page_Terminate();
?>
<?php

//
// Page class
//
class corders_add {

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

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

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

	// 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_add() {
		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", 'add', 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->CanAdd()) {
			$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 $lPriv = 0;

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

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

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

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

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

		// Perform action based on action code
		switch ($orders->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($Language->Phrase("NoRecord")); // No record found
		      $this->Page_Terminate("orderslist.php"); // No matching record, return to list
		    }
				break;
		  case "A": // ' Add new record
				$orders->SendEmail = TRUE; // Send email on add success
		    if ($this->AddRow()) { // Add successful
		      $this->setMessage($Language->Phrase("AddSuccess")); // Set up success message
					$sReturnUrl = $orders->getReturnUrl();
					if (ew_GetPageName($sReturnUrl) == "ordersview.php")
						$sReturnUrl = $orders->ViewUrl(); // View paging, return to view page with keyurl directly
					$this->Page_Terminate($sReturnUrl); // Clean up and return
		    } else {
		      $this->RestoreFormValues(); // Add failed, restore form values
		    }
		}

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

		// Render row
		$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 default values
	function LoadDefaultValues() {
		global $orders;
		$orders->attachment->CurrentValue = NULL; // Clear file related field
	}

	// Load form values
	function LoadFormValues() {

		// Load from form
		global $objForm, $orders;
		$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"));
		$orders->id->setFormValue($objForm->GetValue("x_id"));
	}

	// Restore form values
	function RestoreFormValues() {
		global $objForm, $orders;
		$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;
	}

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

			// 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
		// 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 = "";

			// 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_ADD) { // Add row

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

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

	// Add record
	function AddRow() {
		global $conn, $Language, $Security, $orders;

		// Check if valid key values for master user
		if ($Security->CurrentUserID() <> "" && !$Security->IsAdmin()) { // Non system admin
			$sFilter = $orders->SqlMasterFilter_lesson();
			if (strval($orders->lesson->CurrentValue) <> "" &&
				$orders->getCurrentMasterTable() == "lesson") {
				$sFilter = str_replace("@id@", ew_AdjustSql($orders->lesson->CurrentValue), $sFilter);
			} else {
				$sFilter = "";
			}
			if ($sFilter <> "") {			
				$rsmaster = $GLOBALS["lesson"]->LoadRs($sFilter);
				$this->bMasterRecordExists = ($rsmaster && !$rsmaster->EOF);
				if (!$this->bMasterRecordExists) {
					$sMasterUserIdMsg = str_replace("%c", CurrentUserID(), $Language->Phrase("UnAuthorizedMasterUserID"));
					$sMasterUserIdMsg = str_replace("%f", $sFilter, $sMasterUserIdMsg);
					$this->setMessage($sMasterUserIdMsg);					
					return FALSE;
				} else {
					$rsmaster->Close();
				}
			}
			$sFilter = $orders->SqlMasterFilter_rooms();
			if (strval($orders->room->CurrentValue) <> "" &&
				$orders->getCurrentMasterTable() == "rooms") {
				$sFilter = str_replace("@id@", ew_AdjustSql($orders->room->CurrentValue), $sFilter);
			} else {
				$sFilter = "";
			}
			if ($sFilter <> "") {			
				$rsmaster = $GLOBALS["rooms"]->LoadRs($sFilter);
				$this->bMasterRecordExists = ($rsmaster && !$rsmaster->EOF);
				if (!$this->bMasterRecordExists) {
					$sMasterUserIdMsg = str_replace("%c", CurrentUserID(), $Language->Phrase("UnAuthorizedMasterUserID"));
					$sMasterUserIdMsg = str_replace("%f", $sFilter, $sMasterUserIdMsg);
					$this->setMessage($sMasterUserIdMsg);					
					return FALSE;
				} else {
					$rsmaster->Close();
				}
			}
			$sFilter = $orders->SqlMasterFilter_useraccounts();
			if (strval($orders->teacher->CurrentValue) <> "" &&
				$orders->getCurrentMasterTable() == "useraccounts") {
				$sFilter = str_replace("@username@", ew_AdjustSql($orders->teacher->CurrentValue), $sFilter);
			} else {
				$sFilter = "";
			}
			if ($sFilter <> "") {			
				$rsmaster = $GLOBALS["useraccounts"]->LoadRs($sFilter);
				$this->bMasterRecordExists = ($rsmaster && !$rsmaster->EOF);
				if (!$this->bMasterRecordExists) {
					$sMasterUserIdMsg = str_replace("%c", CurrentUserID(), $Language->Phrase("UnAuthorizedMasterUserID"));
					$sMasterUserIdMsg = str_replace("%f", $sFilter, $sMasterUserIdMsg);
					$this->setMessage($sMasterUserIdMsg);					
					return FALSE;
				} else {
					$rsmaster->Close();
				}
			}
		}
		$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 (is_null($orders->attachment->Upload->Value)) {
			$rsnew['attachment'] = NULL;
		} 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);

		// Call Row Inserting event
		$bInsertRow = $orders->Row_Inserting($rsnew);
		if ($bInsertRow) {
			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';
			$AddRow = $conn->Execute($orders->InsertSQL($rsnew));
			$conn->raiseErrorFn = '';
		} else {
			if ($orders->CancelMessage <> "") {
				$this->setMessage($orders->CancelMessage);
				$orders->CancelMessage = "";
			} else {
				$this->setMessage($Language->Phrase("InsertCancelled"));
			}
			$AddRow = FALSE;
		}
		if ($AddRow) {
			$orders->id->setDbValue($conn->Insert_ID());
			$rsnew['id'] = $orders->id->DbValue;

			// Call Row Inserted event
			$orders->Row_Inserted($rsnew);
			if ($orders->SendEmail)
				$this->SendEmailOnAdd($rsnew);
		}

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

	// 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 add success
	function SendEmailOnAdd(&$rs) {
		global $Language, $orders;
		$sFn = "txt/notify.txt";
		$sTable = 'orders';
		$sSubject = $sTable . " " . $Language->Phrase("RecordInserted");
		$sAction = $Language->Phrase("ActionInserted");

		// Get key value
		$sKey = "";
		if ($sKey <> "") $sKey .= EW_COMPOSITE_KEY_SEPARATOR;
		$sKey .= $rs['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("rs" => $rs);
		$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