<%
Response.Buffer = EW_RESPONSE_BUFFER
%>
<!--#include file="ewcfg11.asp"-->
<!--#include file="UserLevelPermissionsinfo.asp"-->
<!--#include file="zlogininfo.asp"-->
<!--#include file="aspfn11.asp"-->
<!--#include file="md5.asp"-->
<!--#include file="userfn11.asp"-->
<% Session.Timeout = 900 %>
<% Call ew_Header(False, EW_CHARSET) %>
<%

' Define page object
Dim UserLevelPermissions_add
Set UserLevelPermissions_add = New cUserLevelPermissions_add
Set Page = UserLevelPermissions_add

' Page init processing
UserLevelPermissions_add.Page_Init()

' Page main processing
UserLevelPermissions_add.Page_Main()

' Global Page Rendering event (in userfn*.asp)
Page_Rendering()

' Page Rendering event
UserLevelPermissions_add.Page_Render()
%>
<!--#include file="header.asp"-->
<script type="text/javascript">
// Page object
var UserLevelPermissions_add = new ew_Page("UserLevelPermissions_add");
UserLevelPermissions_add.PageID = "add"; // Page ID
var EW_PAGE_ID = UserLevelPermissions_add.PageID; // For backward compatibility
// Form object
var fUserLevelPermissionsadd = new ew_Form("fUserLevelPermissionsadd");
// Validate form
fUserLevelPermissionsadd.Validate = function() {
	if (!this.ValidateRequired)
		return true; // Ignore validation
	var $ = jQuery, fobj = this.GetForm(), $fobj = $(fobj);
	this.PostAutoSuggest();
	if ($fobj.find("#a_confirm").val() == "F")
		return true;
	var elm, felm, uelm, addcnt = 0;
	var $k = $fobj.find("#" + this.FormKeyCountName); // Get key_count
	var rowcnt = ($k[0]) ? parseInt($k.val(), 10) : 1;
	var startcnt = (rowcnt == 0) ? 0 : 1; // Check rowcnt == 0 => Inline-Add
	var gridinsert = $fobj.find("#a_list").val() == "gridinsert";
	for (var i = startcnt; i <= rowcnt; i++) {
		var infix = ($k[0]) ? String(i) : "";
		$fobj.data("rowindex", infix);
			elm = this.GetElements("x" + infix + "_UserLevelID");
			if (elm && !ew_HasValue(elm))
				return this.OnError(elm, ewLanguage.Phrase("EnterRequiredField") + " - <%= ew_JsEncode2(UserLevelPermissions.UserLevelID.FldCaption) %>");
			elm = this.GetElements("x" + infix + "_UserLevelID");
			if (elm && !ew_CheckInteger(elm.value))
				return this.OnError(elm, "<%= ew_JsEncode2(UserLevelPermissions.UserLevelID.FldErrMsg) %>");
			elm = this.GetElements("x" + infix + "_UserLevelTableName");
			if (elm && !ew_HasValue(elm))
				return this.OnError(elm, ewLanguage.Phrase("EnterRequiredField") + " - <%= ew_JsEncode2(UserLevelPermissions.UserLevelTableName.FldCaption) %>");
			elm = this.GetElements("x" + infix + "_UserLevelPermission");
			if (elm && !ew_HasValue(elm))
				return this.OnError(elm, ewLanguage.Phrase("EnterRequiredField") + " - <%= ew_JsEncode2(UserLevelPermissions.UserLevelPermission.FldCaption) %>");
			elm = this.GetElements("x" + infix + "_UserLevelPermission");
			if (elm && !ew_CheckInteger(elm.value))
				return this.OnError(elm, "<%= ew_JsEncode2(UserLevelPermissions.UserLevelPermission.FldErrMsg) %>");
			// Set up row object
			ew_ElementsToRow(fobj);
			// Fire Form_CustomValidate event
			if (!this.Form_CustomValidate(fobj))
				return false;
	}
	// Process detail forms
	var dfs = $fobj.find("input[name='detailpage']").get();
	for (var i = 0; i < dfs.length; i++) {
		var df = dfs[i], val = df.value;
		if (val && ewForms[val])
			if (!ewForms[val].Validate())
				return false;
	}
	return true;
}
// Form_CustomValidate event
fUserLevelPermissionsadd.Form_CustomValidate = 
 function(fobj) { // DO NOT CHANGE THIS LINE!
 	// Your custom validation code here, return false if invalid. 
 	return true;
 }
// Use JavaScript validation or not
<% If EW_CLIENT_VALIDATE Then %>
fUserLevelPermissionsadd.ValidateRequired = true; // Use JavaScript validation
<% Else %>
fUserLevelPermissionsadd.ValidateRequired = false; // No JavaScript validation
<% End If %>
// Dynamic selection lists
// Form object for search
</script>
<script type="text/javascript">
// Write your client script here, no need to add script tags.
</script>
<% If UserLevelPermissions.Export = "" Then %>
<% Breadcrumb.Render() %>
<% End If %>
<% UserLevelPermissions_add.ShowPageHeader() %>
<% UserLevelPermissions_add.ShowMessage %>
<form name="fUserLevelPermissionsadd" id="fUserLevelPermissionsadd" class="ewForm form-inline" action="<%= ew_CurrentPage() %>" method="post">
<input type="hidden" name="t" value="UserLevelPermissions">
<input type="hidden" name="a_add" id="a_add" value="A">
<table class="ewGrid"><tr><td>
<table id="tbl_UserLevelPermissionsadd" class="table table-bordered table-striped">
<% If UserLevelPermissions.UserLevelID.Visible Then ' UserLevelID %>
	<tr id="r_UserLevelID">
		<td><span id="elh_UserLevelPermissions_UserLevelID"><%= UserLevelPermissions.UserLevelID.FldCaption %><%= Language.Phrase("FieldRequiredIndicator") %></span></td>
		<td<%= UserLevelPermissions.UserLevelID.CellAttributes %>>
<span id="el_UserLevelPermissions_UserLevelID" class="control-group">
<input type="text" data-field="x_UserLevelID" name="x_UserLevelID" id="x_UserLevelID" size="30" placeholder="<%= UserLevelPermissions.UserLevelID.PlaceHolder %>" value="<%= UserLevelPermissions.UserLevelID.EditValue %>"<%= UserLevelPermissions.UserLevelID.EditAttributes %>>
</span>
<%= UserLevelPermissions.UserLevelID.CustomMsg %></td>
	</tr>
<% End If %>
<% If UserLevelPermissions.UserLevelTableName.Visible Then ' UserLevelTableName %>
	<tr id="r_UserLevelTableName">
		<td><span id="elh_UserLevelPermissions_UserLevelTableName"><%= UserLevelPermissions.UserLevelTableName.FldCaption %><%= Language.Phrase("FieldRequiredIndicator") %></span></td>
		<td<%= UserLevelPermissions.UserLevelTableName.CellAttributes %>>
<span id="el_UserLevelPermissions_UserLevelTableName" class="control-group">
<input type="text" data-field="x_UserLevelTableName" name="x_UserLevelTableName" id="x_UserLevelTableName" size="30" maxlength="50" placeholder="<%= UserLevelPermissions.UserLevelTableName.PlaceHolder %>" value="<%= UserLevelPermissions.UserLevelTableName.EditValue %>"<%= UserLevelPermissions.UserLevelTableName.EditAttributes %>>
</span>
<%= UserLevelPermissions.UserLevelTableName.CustomMsg %></td>
	</tr>
<% End If %>
<% If UserLevelPermissions.UserLevelPermission.Visible Then ' UserLevelPermission %>
	<tr id="r_UserLevelPermission">
		<td><span id="elh_UserLevelPermissions_UserLevelPermission"><%= UserLevelPermissions.UserLevelPermission.FldCaption %><%= Language.Phrase("FieldRequiredIndicator") %></span></td>
		<td<%= UserLevelPermissions.UserLevelPermission.CellAttributes %>>
<span id="el_UserLevelPermissions_UserLevelPermission" class="control-group">
<input type="text" data-field="x_UserLevelPermission" name="x_UserLevelPermission" id="x_UserLevelPermission" size="30" placeholder="<%= UserLevelPermissions.UserLevelPermission.PlaceHolder %>" value="<%= UserLevelPermissions.UserLevelPermission.EditValue %>"<%= UserLevelPermissions.UserLevelPermission.EditAttributes %>>
</span>
<%= UserLevelPermissions.UserLevelPermission.CustomMsg %></td>
	</tr>
<% End If %>
</table>
</td></tr></table>
<button class="btn btn-primary ewButton" name="btnAction" id="btnAction" type="submit"><%= Language.Phrase("AddBtn") %></button>
</form>
<script type="text/javascript">
fUserLevelPermissionsadd.Init();
<% If EW_MOBILE_REFLOW And ew_IsMobile() Then %>
ew_Reflow();
<% End If %>
</script>
<%
UserLevelPermissions_add.ShowPageFooter()
If EW_DEBUG_ENABLED Then Response.Write ew_DebugMsg()
%>
<script type="text/javascript">
// Write your table-specific startup script here
// document.write("page loaded");
</script>
<!--#include file="footer.asp"-->
<%

' Drop page object
Set UserLevelPermissions_add = Nothing
%>
<%

' -----------------------------------------------------------------
' Page Class
'
Class cUserLevelPermissions_add

	' Page ID
	Public Property Get PageID()
		PageID = "add"
	End Property

	' Project ID
	Public Property Get ProjectID()
		ProjectID = "{9AC218D3-4571-402E-9408-D679EC504540}"
	End Property

	' Table Name
	Public Property Get TableName()
		TableName = "UserLevelPermissions"
	End Property

	' Page Object Name
	Public Property Get PageObjName()
		PageObjName = "UserLevelPermissions_add"
	End Property

	' Page Name
	Public Property Get PageName()
		PageName = ew_CurrentPage()
	End Property

	' Page Url
	Public Property Get PageUrl()
		PageUrl = ew_CurrentPage() & "?"
		If UserLevelPermissions.UseTokenInUrl Then PageUrl = PageUrl & "t=" & UserLevelPermissions.TableVar & "&" ' add page token
	End Property

	' Message
	Public Property Get Message()
		Message = Session(EW_SESSION_MESSAGE)
	End Property

	Public Property Let Message(v)
		Dim msg
		msg = Session(EW_SESSION_MESSAGE)
		Call ew_AddMessage(msg, v)
		Session(EW_SESSION_MESSAGE) = msg
	End Property

	Public Property Get FailureMessage()
		FailureMessage = Session(EW_SESSION_FAILURE_MESSAGE)
	End Property

	Public Property Let FailureMessage(v)
		Dim msg
		msg = Session(EW_SESSION_FAILURE_MESSAGE)
		Call ew_AddMessage(msg, v)
		Session(EW_SESSION_FAILURE_MESSAGE) = msg
	End Property

	Public Property Get SuccessMessage()
		SuccessMessage = Session(EW_SESSION_SUCCESS_MESSAGE)
	End Property

	Public Property Let SuccessMessage(v)
		Dim msg
		msg = Session(EW_SESSION_SUCCESS_MESSAGE)
		Call ew_AddMessage(msg, v)
		Session(EW_SESSION_SUCCESS_MESSAGE) = msg
	End Property

	Public Property Get WarningMessage()
		WarningMessage = Session(EW_SESSION_WARNING_MESSAGE)
	End Property

	Public Property Let WarningMessage(v)
		Dim msg
		msg = Session(EW_SESSION_WARNING_MESSAGE)
		Call ew_AddMessage(msg, v)
		Session(EW_SESSION_WARNING_MESSAGE) = msg
	End Property

	' Show Message
	Public Sub ShowMessage()
		Dim hidden, html, sMessage
		hidden = False
		html = ""

		' Message
		sMessage = Message
		Call Message_Showing(sMessage, "")
		If sMessage <> "" Then ' Message in Session, display
			If Not hidden Then sMessage = "<button type=""button"" class=""close"" data-dismiss=""alert"">&times;</button>" & sMessage
			html = html & "<div class=""alert alert-success ewSuccess"">" & sMessage & "</div>"
			Session(EW_SESSION_MESSAGE) = "" ' Clear message in Session
		End If

		' Warning message
		Dim sWarningMessage
		sWarningMessage = WarningMessage
		Call Message_Showing(sWarningMessage, "warning")
		If sWarningMessage <> "" Then ' Message in Session, display
			If Not hidden Then sWarningMessage = "<button type=""button"" class=""close"" data-dismiss=""alert"">&times;</button>" & sWarningMessage
			html = html & "<div class=""alert alert-warning ewWarning"">" & sWarningMessage & "</div>"
			Session(EW_SESSION_WARNING_MESSAGE) = "" ' Clear message in Session
		End If

		' Success message
		Dim sSuccessMessage
		sSuccessMessage = SuccessMessage
		Call Message_Showing(sSuccessMessage, "success")
		If sSuccessMessage <> "" Then ' Message in Session, display
			If Not hidden Then sSuccessMessage = "<button type=""button"" class=""close"" data-dismiss=""alert"">&times;</button>" & sSuccessMessage
			html = html & "<div class=""alert alert-success ewSuccess"">" & sSuccessMessage & "</div>"
			Session(EW_SESSION_SUCCESS_MESSAGE) = "" ' Clear message in Session
		End If

		' Failure message
		Dim sErrorMessage
		sErrorMessage = FailureMessage
		Call Message_Showing(sErrorMessage, "failure")
		If sErrorMessage <> "" Then ' Message in Session, display
			If Not hidden Then sErrorMessage = "<button type=""button"" class=""close"" data-dismiss=""alert"">&times;</button>" & sErrorMessage
			html = html & "<div class=""alert alert-error ewError"">" & sErrorMessage & "</div>"
			Session(EW_SESSION_FAILURE_MESSAGE) = "" ' Clear message in Session
		End If
		Response.Write "<table class=""ewStdTable""><tr><td><div class=""ewMessageDialog""" & ew_IIf(hidden, " style=""display: none;""", "") & ">" & html & "</div></td></tr></table>"
	End Sub
	Dim PageHeader
	Dim PageFooter

	' Show Page Header
	Public Sub ShowPageHeader()
		Dim sHeader
		sHeader = PageHeader
		Call Page_DataRendering(sHeader)
		If sHeader <> "" Then ' Header exists, display
			Response.Write "<p>" & sHeader & "</p>"
		End If
	End Sub

	' Show Page Footer
	Public Sub ShowPageFooter()
		Dim sFooter
		sFooter = PageFooter
		Call Page_DataRendered(sFooter)
		If sFooter <> "" Then ' Footer exists, display
			Response.Write "<p>" & sFooter & "</p>"
		End If
	End Sub

	' -----------------------
	'  Validate Page request
	'
	Public Function IsPageRequest()
		If UserLevelPermissions.UseTokenInUrl Then
			IsPageRequest = False
			If Not (ObjForm Is Nothing) Then
				IsPageRequest = (UserLevelPermissions.TableVar = ObjForm.GetValue("t"))
			End If
			If Request.QueryString("t").Count > 0 Then
				IsPageRequest = (UserLevelPermissions.TableVar = Request.QueryString("t"))
			End If
		Else
			IsPageRequest = True
		End If
	End Function

	' -----------------------------------------------------------------
	'  Class initialize
	'  - init objects
	'  - open ADO connection
	'
	Private Sub Class_Initialize()
		If IsEmpty(StartTimer) Then StartTimer = Timer ' Init start time

		' Initialize language object
		If IsEmpty(Language) Then
			Set Language = New cLanguage
			Call Language.LoadPhrases()
		End If

		' Initialize table object
		If IsEmpty(UserLevelPermissions) Then Set UserLevelPermissions = New cUserLevelPermissions
		Set Table = UserLevelPermissions

		' Initialize urls
		' Initialize other table object

		If IsEmpty(zlogin) Then Set zlogin = New czlogin

		' Initialize form object
		Set ObjForm = Nothing

		' Intialize page id (for backward compatibility)
		EW_PAGE_ID = "add"

		' Initialize table name (for backward compatibility)
		EW_TABLE_NAME = "UserLevelPermissions"

		' Open connection to the database
		If IsEmpty(Conn) Then Call ew_Connect()
	End Sub

	' -----------------------------------------------------------------
	'  Subroutine Page_Init
	'  - called before page main
	'  - check Security
	'  - set up response header
	'  - call page load events
	'
	Sub Page_Init()
		Set Security = New cAdvancedSecurity
		If Not Security.IsLoggedIn() Then Call Security.AutoLogin()
		If Not Security.IsLoggedIn() Then
			Call Security.SaveLastUrl()
			Call Page_Terminate("login.asp")
		End If

		' Table Permission loading event
		Call Security.TablePermission_Loading()
		Call Security.LoadCurrentUserLevel(ProjectID & TableName)

		' Table Permission loaded event
		Call Security.TablePermission_Loaded()
		If Not Security.CanAdmin Then
			Call Security.SaveLastUrl()
			Call Page_Terminate( "login.asp")
		End If

		' Create form object
		'If Request.ServerVariables("HTTP_CONTENT_TYPE") = "application/x-www-form-urlencoded" Then

			Set ObjForm = New cFormObj

		'Else
		'	Set ObjForm = ew_GetUploadObj()
		'End If

		UserLevelPermissions.CurrentAction = ew_IIf(Request.QueryString("a").Count > 0, Request.QueryString("a") & "", ObjForm.GetValue("a_list") & "") ' Set up current action

		' Global page loading event (in userfn7.asp)
		Page_Loading()

		' Page load event, used in current page
		Page_Load()
	End Sub

	' -----------------------------------------------------------------
	'  Class terminate
	'  - clean up page object
	'
	Private Sub Class_Terminate()
		Call Page_Terminate("")
	End Sub

	' -----------------------------------------------------------------
	'  Subroutine Page_Terminate
	'  - called when exit page
	'  - clean up ADO connection and objects
	'  - if url specified, redirect to url
	'
	Sub Page_Terminate(url)

		' Page unload event, used in current page
		Call Page_Unload()

		' Global page unloaded event (in userfn60.asp)
		Call Page_Unloaded()
		Dim sRedirectUrl
		sReDirectUrl = url
		Call Page_Redirecting(sReDirectUrl)
		If Not (Conn Is Nothing) Then Conn.Close ' Close Connection
		Set Conn = Nothing
		Set Security = Nothing
		Set UserLevelPermissions = Nothing
		Set ObjForm = Nothing

		' Go to url if specified
		If sReDirectUrl <> "" Then
			If Response.Buffer Then Response.Clear
			Response.Redirect sReDirectUrl
		End If
	End Sub

	'
	'  Subroutine Page_Terminate (End)
	' ----------------------------------------

	Dim DbMasterFilter, DbDetailFilter
	Dim Priv
	Dim OldRecordset
	Dim CopyRecord

	' -----------------------------------------------------------------
	' Page main processing
	'
	Sub Page_Main()

		' Process form if post back
		If ObjForm.GetValue("a_add")&"" <> "" Then
			UserLevelPermissions.CurrentAction = ObjForm.GetValue("a_add") ' Get form action
			CopyRecord = LoadOldRecord() ' Load old recordset
			Call LoadFormValues() ' Load form values

		' Not post back
		Else

			' Load key values from QueryString
			CopyRecord = True
			If Request.QueryString("UserLevelID").Count > 0 Then
				UserLevelPermissions.UserLevelID.QueryStringValue = Request.QueryString("UserLevelID")
				Call UserLevelPermissions.SetKey("UserLevelID", UserLevelPermissions.UserLevelID.CurrentValue) ' Set up key
			Else
				Call UserLevelPermissions.SetKey("UserLevelID", "") ' Clear key
				CopyRecord = False
			End If
			If Request.QueryString("UserLevelTableName").Count > 0 Then
				UserLevelPermissions.UserLevelTableName.QueryStringValue = Request.QueryString("UserLevelTableName")
				Call UserLevelPermissions.SetKey("UserLevelTableName", UserLevelPermissions.UserLevelTableName.CurrentValue) ' Set up key
			Else
				Call UserLevelPermissions.SetKey("UserLevelTableName", "") ' Clear key
				CopyRecord = False
			End If
			If CopyRecord Then
				UserLevelPermissions.CurrentAction = "C" ' Copy Record
			Else
				UserLevelPermissions.CurrentAction = "I" ' Display Blank Record
				Call LoadDefaultValues() ' Load default values
			End If
		End If

		' Set up Breadcrumb
		SetupBreadcrumb()

		' Validate form if post back
		If ObjForm.GetValue("a_add")&"" <> "" Then
			If Not ValidateForm() Then
				UserLevelPermissions.CurrentAction = "I" ' Form error, reset action
				UserLevelPermissions.EventCancelled = True ' Event cancelled
				Call RestoreFormValues() ' Restore form values
				FailureMessage = gsFormError
			End If
		End If

		' Perform action based on action code
		Select Case UserLevelPermissions.CurrentAction
			Case "I" ' Blank record, no action required
			Case "C" ' Copy an existing record
				If Not LoadRow() Then ' Load record based on key
					If FailureMessage = "" Then FailureMessage = Language.Phrase("NoRecord") ' No record found
					Call Page_Terminate("UserLevelPermissionslist.asp") ' No matching record, return to list
				End If
			Case "A" ' Add new record
				UserLevelPermissions.SendEmail = True ' Send email on add success
				If AddRow(OldRecordset) Then ' Add successful
					SuccessMessage = Language.Phrase("AddSuccess") ' Set up success message
					Dim sReturnUrl
					sReturnUrl = UserLevelPermissions.ReturnUrl
					If ew_GetPageName(sReturnUrl) = "UserLevelPermissionsview.asp" Then sReturnUrl = UserLevelPermissions.ViewUrl("") ' View paging, return to view page with keyurl directly
					Call Page_Terminate(sReturnUrl) ' Clean up and return
				Else
					UserLevelPermissions.EventCancelled = True ' Event cancelled
					Call RestoreFormValues() ' Add failed, restore form values
				End If
		End Select

		' Render row based on row type
		UserLevelPermissions.RowType = EW_ROWTYPE_ADD ' Render add type

		' Render row
		Call UserLevelPermissions.ResetAttrs()
		Call RenderRow()
	End Sub

	' -----------------------------------------------------------------
	' Function Get upload files
	'
	Function GetUploadFiles()

		' Get upload data
	End Function

	' -----------------------------------------------------------------
	' Load default values
	'
	Function LoadDefaultValues()
		UserLevelPermissions.UserLevelID.CurrentValue = Null
		UserLevelPermissions.UserLevelID.OldValue = UserLevelPermissions.UserLevelID.CurrentValue
		UserLevelPermissions.UserLevelTableName.CurrentValue = Null
		UserLevelPermissions.UserLevelTableName.OldValue = UserLevelPermissions.UserLevelTableName.CurrentValue
		UserLevelPermissions.UserLevelPermission.CurrentValue = Null
		UserLevelPermissions.UserLevelPermission.OldValue = UserLevelPermissions.UserLevelPermission.CurrentValue
	End Function

	' -----------------------------------------------------------------
	' Load form values
	'
	Function LoadFormValues()

		' Load values from form
		If Not UserLevelPermissions.UserLevelID.FldIsDetailKey Then UserLevelPermissions.UserLevelID.FormValue = ObjForm.GetValue("x_UserLevelID")
		If Not UserLevelPermissions.UserLevelTableName.FldIsDetailKey Then UserLevelPermissions.UserLevelTableName.FormValue = ObjForm.GetValue("x_UserLevelTableName")
		If Not UserLevelPermissions.UserLevelPermission.FldIsDetailKey Then UserLevelPermissions.UserLevelPermission.FormValue = ObjForm.GetValue("x_UserLevelPermission")
	End Function

	' -----------------------------------------------------------------
	' Restore form values
	'
	Function RestoreFormValues()
		Call LoadOldRecord()
		UserLevelPermissions.UserLevelID.CurrentValue = UserLevelPermissions.UserLevelID.FormValue
		UserLevelPermissions.UserLevelTableName.CurrentValue = UserLevelPermissions.UserLevelTableName.FormValue
		UserLevelPermissions.UserLevelPermission.CurrentValue = UserLevelPermissions.UserLevelPermission.FormValue
	End Function

	' -----------------------------------------------------------------
	' Load row based on key values
	'
	Function LoadRow()
		Dim RsRow, sSql, sFilter
		sFilter = UserLevelPermissions.KeyFilter

		' Call Row Selecting event
		Call UserLevelPermissions.Row_Selecting(sFilter)

		' Load sql based on filter
		UserLevelPermissions.CurrentFilter = sFilter
		sSql = UserLevelPermissions.SQL
		Call ew_SetDebugMsg("LoadRow: " & sSql) ' Show SQL for debugging
		Set RsRow = ew_LoadRow(sSql)
		If RsRow.Eof Then
			LoadRow = False
		Else
			LoadRow = True
			RsRow.MoveFirst
			Call LoadRowValues(RsRow) ' Load row values
		End If
		RsRow.Close
		Set RsRow = Nothing
	End Function

	' -----------------------------------------------------------------
	' Load row values from recordset
	'
	Sub LoadRowValues(RsRow)
		Dim sDetailFilter
		If Not EW_DEBUG_ENABLED Then On Error Resume Next
		If RsRow.Eof Then Exit Sub

		' Call Row Selected event
		Call UserLevelPermissions.Row_Selected(RsRow)
		UserLevelPermissions.UserLevelID.DbValue = RsRow("UserLevelID")
		UserLevelPermissions.UserLevelTableName.DbValue = RsRow("UserLevelTableName")
		UserLevelPermissions.UserLevelPermission.DbValue = RsRow("UserLevelPermission")
	End Sub

	' Load DbValue from recordset
	Sub LoadDbValues(Rs)
		If Not EW_DEBUG_ENABLED Then On Error Resume Next
		If Rs.Eof Then Exit Sub
		UserLevelPermissions.UserLevelID.m_DbValue = Rs("UserLevelID")
		UserLevelPermissions.UserLevelTableName.m_DbValue = Rs("UserLevelTableName")
		UserLevelPermissions.UserLevelPermission.m_DbValue = Rs("UserLevelPermission")
	End Sub

	' Load old record
	Function LoadOldRecord()

		' Load key values from Session
		Dim bValidKey
		bValidKey = True
		If UserLevelPermissions.GetKey("UserLevelID")&"" <> "" Then
			UserLevelPermissions.UserLevelID.CurrentValue = UserLevelPermissions.GetKey("UserLevelID") ' UserLevelID
		Else
			bValidKey = False
		End If
		If UserLevelPermissions.GetKey("UserLevelTableName")&"" <> "" Then
			UserLevelPermissions.UserLevelTableName.CurrentValue = UserLevelPermissions.GetKey("UserLevelTableName") ' UserLevelTableName
		Else
			bValidKey = False
		End If

		' Load old recordset
		If bValidKey Then
			UserLevelPermissions.CurrentFilter = UserLevelPermissions.KeyFilter
			Dim sSql
			sSql = UserLevelPermissions.SQL
			Set OldRecordset = ew_LoadRecordset(sSql)
			Call LoadRowValues(OldRecordset) ' Load row values
		Else
			OldRecordset = Null
		End If
		LoadOldRecord = bValidKey
	End Function

	' -----------------------------------------------------------------
	' Render row values based on field settings
	'
	Sub RenderRow()

		' Initialize urls
		' Call Row Rendering event

		Call UserLevelPermissions.Row_Rendering()

		' ---------------------------------------
		'  Common render codes for all row types
		' ---------------------------------------
		' UserLevelID
		' UserLevelTableName
		' UserLevelPermission
		' -----------
		'  View  Row
		' -----------

		If UserLevelPermissions.RowType = EW_ROWTYPE_VIEW Then ' View row

			' UserLevelID
			UserLevelPermissions.UserLevelID.ViewValue = UserLevelPermissions.UserLevelID.CurrentValue
			UserLevelPermissions.UserLevelID.ViewCustomAttributes = ""

			' UserLevelTableName
			UserLevelPermissions.UserLevelTableName.ViewValue = UserLevelPermissions.UserLevelTableName.CurrentValue
			UserLevelPermissions.UserLevelTableName.ViewCustomAttributes = ""

			' UserLevelPermission
			UserLevelPermissions.UserLevelPermission.ViewValue = UserLevelPermissions.UserLevelPermission.CurrentValue
			UserLevelPermissions.UserLevelPermission.ViewCustomAttributes = ""

			' View refer script
			' UserLevelID

			UserLevelPermissions.UserLevelID.LinkCustomAttributes = ""
			UserLevelPermissions.UserLevelID.HrefValue = ""
			UserLevelPermissions.UserLevelID.TooltipValue = ""

			' UserLevelTableName
			UserLevelPermissions.UserLevelTableName.LinkCustomAttributes = ""
			UserLevelPermissions.UserLevelTableName.HrefValue = ""
			UserLevelPermissions.UserLevelTableName.TooltipValue = ""

			' UserLevelPermission
			UserLevelPermissions.UserLevelPermission.LinkCustomAttributes = ""
			UserLevelPermissions.UserLevelPermission.HrefValue = ""
			UserLevelPermissions.UserLevelPermission.TooltipValue = ""

		' ---------
		'  Add Row
		' ---------

		ElseIf UserLevelPermissions.RowType = EW_ROWTYPE_ADD Then ' Add row

			' UserLevelID
			UserLevelPermissions.UserLevelID.EditCustomAttributes = ""
			UserLevelPermissions.UserLevelID.EditValue = ew_HtmlEncode(UserLevelPermissions.UserLevelID.CurrentValue)
			UserLevelPermissions.UserLevelID.PlaceHolder = ew_HtmlEncode(ew_RemoveHtml(UserLevelPermissions.UserLevelID.FldCaption))

			' UserLevelTableName
			UserLevelPermissions.UserLevelTableName.EditCustomAttributes = ""
			UserLevelPermissions.UserLevelTableName.EditValue = ew_HtmlEncode(UserLevelPermissions.UserLevelTableName.CurrentValue)
			UserLevelPermissions.UserLevelTableName.PlaceHolder = ew_HtmlEncode(ew_RemoveHtml(UserLevelPermissions.UserLevelTableName.FldCaption))

			' UserLevelPermission
			UserLevelPermissions.UserLevelPermission.EditCustomAttributes = ""
			UserLevelPermissions.UserLevelPermission.EditValue = ew_HtmlEncode(UserLevelPermissions.UserLevelPermission.CurrentValue)
			UserLevelPermissions.UserLevelPermission.PlaceHolder = ew_HtmlEncode(ew_RemoveHtml(UserLevelPermissions.UserLevelPermission.FldCaption))

			' Edit refer script
			' UserLevelID

			UserLevelPermissions.UserLevelID.HrefValue = ""

			' UserLevelTableName
			UserLevelPermissions.UserLevelTableName.HrefValue = ""

			' UserLevelPermission
			UserLevelPermissions.UserLevelPermission.HrefValue = ""
		End If
		If UserLevelPermissions.RowType = EW_ROWTYPE_ADD Or UserLevelPermissions.RowType = EW_ROWTYPE_EDIT Or UserLevelPermissions.RowType = EW_ROWTYPE_SEARCH Then ' Add / Edit / Search row
			Call UserLevelPermissions.SetupFieldTitles()
		End If

		' Call Row Rendered event
		If UserLevelPermissions.RowType <> EW_ROWTYPE_AGGREGATEINIT Then
			Call UserLevelPermissions.Row_Rendered()
		End If
	End Sub

	' -----------------------------------------------------------------
	' Validate form
	'
	Function ValidateForm()

		' Initialize
		gsFormError = ""

		' Check if validation required
		If Not EW_SERVER_VALIDATE Then
			ValidateForm = (gsFormError = "")
			Exit Function
		End If
		If Not UserLevelPermissions.UserLevelID.FldIsDetailKey And Not IsNull(UserLevelPermissions.UserLevelID.FormValue) And UserLevelPermissions.UserLevelID.FormValue&"" = "" Then
			Call ew_AddMessage(gsFormError, Language.Phrase("EnterRequiredField") & " - " & UserLevelPermissions.UserLevelID.FldCaption)
		End If
		If Not ew_CheckInteger(UserLevelPermissions.UserLevelID.FormValue) Then
			Call ew_AddMessage(gsFormError, UserLevelPermissions.UserLevelID.FldErrMsg)
		End If
		If Not UserLevelPermissions.UserLevelTableName.FldIsDetailKey And Not IsNull(UserLevelPermissions.UserLevelTableName.FormValue) And UserLevelPermissions.UserLevelTableName.FormValue&"" = "" Then
			Call ew_AddMessage(gsFormError, Language.Phrase("EnterRequiredField") & " - " & UserLevelPermissions.UserLevelTableName.FldCaption)
		End If
		If Not UserLevelPermissions.UserLevelPermission.FldIsDetailKey And Not IsNull(UserLevelPermissions.UserLevelPermission.FormValue) And UserLevelPermissions.UserLevelPermission.FormValue&"" = "" Then
			Call ew_AddMessage(gsFormError, Language.Phrase("EnterRequiredField") & " - " & UserLevelPermissions.UserLevelPermission.FldCaption)
		End If
		If Not ew_CheckInteger(UserLevelPermissions.UserLevelPermission.FormValue) Then
			Call ew_AddMessage(gsFormError, UserLevelPermissions.UserLevelPermission.FldErrMsg)
		End If

		' Return validate result
		ValidateForm = (gsFormError = "")

		' Call Form Custom Validate event
		Dim sFormCustomError
		sFormCustomError = ""
		ValidateForm = ValidateForm And Form_CustomValidate(sFormCustomError)
		If sFormCustomError <> "" Then
			Call ew_AddMessage(gsFormError, sFormCustomError)
		End If
	End Function

	' -----------------------------------------------------------------
	' Add record
	'
	Function AddRow(RsOld)
		If Not EW_DEBUG_ENABLED Then On Error Resume Next
		Dim Rs, sSql, sFilter
		Dim RsNew
		Dim bInsertRow
		Dim RsChk
		Dim sIdxErrMsg

		' Clear any previous errors
		Err.Clear
		Dim RsMaster, sMasterUserIdMsg, sMasterFilter, bCheckMasterRecord

		' Load db values from rsold
		If Not IsNull(RsOld) Then
			Call LoadDbValues(RsOld)
		End If

		' Add new record
		sFilter = "(0 = 1)"
		UserLevelPermissions.CurrentFilter = sFilter
		sSql = UserLevelPermissions.SQL
		Set Rs = Server.CreateObject("ADODB.Recordset")
		Rs.CursorLocation = EW_CURSORLOCATION
		Rs.Open sSql, Conn, 1, EW_RECORDSET_LOCKTYPE
		Rs.AddNew
		If Err.Number <> 0 Then
			Message = Err.Description
			Rs.Close
			Set Rs = Nothing
			AddRow = False
			Exit Function
		End If

		' Field UserLevelID
		Call UserLevelPermissions.UserLevelID.SetDbValue(Rs, UserLevelPermissions.UserLevelID.CurrentValue, 0, False)

		' Field UserLevelTableName
		Call UserLevelPermissions.UserLevelTableName.SetDbValue(Rs, UserLevelPermissions.UserLevelTableName.CurrentValue, "", False)

		' Field UserLevelPermission
		Call UserLevelPermissions.UserLevelPermission.SetDbValue(Rs, UserLevelPermissions.UserLevelPermission.CurrentValue, 0, False)

		' Check recordset update error
		If Err.Number <> 0 Then
			FailureMessage = Err.Description
			Rs.Close
			Set Rs = Nothing
			AddRow = False
			Exit Function
		End If

		' Call Row Inserting event
		bInsertRow = UserLevelPermissions.Row_Inserting(RsOld, Rs)

		' Check if key value entered
		If bInsertRow And UserLevelPermissions.ValidateKey And UserLevelPermissions.UserLevelID.CurrentValue = "" And UserLevelPermissions.UserLevelID.SessionValue = "" Then
			FailureMessage = Language.Phrase("InvalidKeyValue")
			bInsertRow = False
		End If

		' Check if key value entered
		If bInsertRow And UserLevelPermissions.ValidateKey And UserLevelPermissions.UserLevelTableName.CurrentValue = "" And UserLevelPermissions.UserLevelTableName.SessionValue = "" Then
			FailureMessage = Language.Phrase("InvalidKeyValue")
			bInsertRow = False
		End If

		' Check for duplicate key
		Dim sKeyErrMsg
		If bInsertRow And UserLevelPermissions.ValidateKey Then
			sFilter = UserLevelPermissions.KeyFilter
			Set RsChk = UserLevelPermissions.LoadRs(sFilter)
			If Not (RsChk Is Nothing) Then
				sKeyErrMsg = Replace(Language.Phrase("DupKey"), "%f", sFilter)
				FailureMessage = sKeyErrMsg
				RsChk.Close
				Set RsChk = Nothing
				bInsertRow = False
			End If
		End If
		If bInsertRow Then

			' Clone new recordset object
			Set RsNew = ew_CloneRs(Rs)
			Rs.Update
			If Err.Number <> 0 Then
				FailureMessage = Err.Description
				AddRow = False
			Else
				AddRow = True
			End If
			If AddRow Then
			End If
		Else
			Rs.CancelUpdate

			' Set up error message
			If SuccessMessage <> "" Or FailureMessage <> "" Then

				' Use the message, do nothing
			ElseIf UserLevelPermissions.CancelMessage <> "" Then
				FailureMessage = UserLevelPermissions.CancelMessage
				UserLevelPermissions.CancelMessage = ""
			Else
				FailureMessage = Language.Phrase("InsertCancelled")
			End If
			AddRow = False
		End If
		Rs.Close
		Set Rs = Nothing
		If AddRow Then
		End If
		If AddRow Then

			' Call Row Inserted event
			Call UserLevelPermissions.Row_Inserted(RsOld, RsNew)
		End If
		If IsObject(RsNew) Then
			RsNew.Close
			Set RsNew = Nothing
		End If
	End Function

	' Set up Breadcrumb
	Sub SetupBreadcrumb()
		Dim PageId, url
		Set Breadcrumb = New cBreadcrumb
		Call Breadcrumb.Add("list", UserLevelPermissions.TableVar, "UserLevelPermissionslist.asp", UserLevelPermissions.TableVar, True)
		PageId = ew_IIf(UserLevelPermissions.CurrentAction = "C", "Copy", "Add")
		Call Breadcrumb.Add("add", PageId, ew_CurrentUrl, "", False)
	End Sub

	' Page Load event
	Sub Page_Load()

		'Response.Write "Page Load"
	End Sub

	' Page Unload event
	Sub Page_Unload()

		'Response.Write "Page Unload"
	End Sub

	' Page Redirecting event
	Sub Page_Redirecting(url)

		'url = newurl
	End Sub

	' Message Showing event
	' typ = ""|"success"|"failure"|"warning"
	Sub Message_Showing(msg, typ)

		' Example:
		'If typ = "success" Then
		'	msg = "your success message"
		'ElseIf typ = "failure" Then
		'	msg = "your failure message"
		'ElseIf typ = "warning" Then
		'	msg = "your warning message"
		'Else
		'	msg = "your message"
		'End If

	End Sub

	' Page Render event
	Sub Page_Render()

		'Response.Write "Page Render"
	End Sub

	' Page Data Rendering event
	Sub Page_DataRendering(header)

		' Example:
		'header = "your header"

	End Sub

	' Page Data Rendered event
	Sub Page_DataRendered(footer)

		' Example:
		'footer = "your footer"

	End Sub

	' Form Custom Validate event
	Function Form_CustomValidate(CustomError)

		'Return error message in CustomError
		Form_CustomValidate = True
	End Function
End Class
%>
