<%
Response.Buffer = EW_RESPONSE_BUFFER
%>
<!--#include file="ewcfg7.asp"-->
<!--#include file="zlogininfo.asp"-->
<!--#include file="aspfn7.asp"-->
<!--#include file="userfn7.asp"-->
<% Session.Timeout = 900 %>
<%
Response.Expires = 0
Response.ExpiresAbsolute = Now() - 1
Response.AddHeader "pragma", "no-cache"
Response.AddHeader "cache-control", "private, no-cache, no-store, must-revalidate"
%>
<%

' Define page object
Dim zlogin_add
Set zlogin_add = New czlogin_add
Set Page = zlogin_add

' Page init processing
Call zlogin_add.Page_Init()

' Page main processing
Call zlogin_add.Page_Main()
%>
<!--#include file="header.asp"-->
<script type="text/javascript">
<!--
// Create page object
var zlogin_add = new ew_Page("zlogin_add");
// page properties
zlogin_add.PageID = "add"; // page ID
var EW_PAGE_ID = zlogin_add.PageID; // for backward compatibility
// extend page with ValidateForm function
zlogin_add.ValidateForm = function(fobj) {
	if (!this.ValidateRequired)
		return true; // ignore validation
	if (fobj.a_confirm && fobj.a_confirm.value == "F")
		return true;
	var i, elm, aelm, infix;
	var rowcnt = (fobj.key_count) ? Number(fobj.key_count.value) : 1;
	for (i=0; i<rowcnt; i++) {
		infix = (fobj.key_count) ? String(i+1) : "";
		// Call Form Custom Validate event
		if (!this.Form_CustomValidate(fobj)) return false;
	}
	return true;
}
// extend page with Form_CustomValidate function
zlogin_add.Form_CustomValidate = 
 function(fobj) { // DO NOT CHANGE THIS LINE!
 	// Your custom validation code here, return false if invalid. 
 	return true;
 }
zlogin_add.SelectAllKey = function(elem) {
	ew_SelectAll(elem);
}
<% If EW_CLIENT_VALIDATE Then %>
zlogin_add.ValidateRequired = true; // uses JavaScript validation
<% Else %>
zlogin_add.ValidateRequired = false; // no JavaScript validation
<% End If %>
//-->
</script>
<script type="text/javascript">
<!--
var ew_DHTMLEditors = [];
//-->
</script>
<script language="JavaScript" type="text/javascript">
<!--
// Write your client script here, no need to add script tags.
// To include another .js script, use:
// ew_ClientScriptInclude("my_javascript.js"); 
//-->
</script>
<p><span class="aspmaker">增加 表: 管理员<br><br>
<a href="<%= zlogin.ReturnUrl %>">返回</a></span></p>
<% zlogin_add.ShowMessage %>
<form name="fzloginadd" id="fzloginadd" action="<%= ew_CurrentPage %>" method="post" onsubmit="return zlogin_add.ValidateForm(this);">
<p>
<input type="hidden" name="t" id="t" value="zlogin">
<input type="hidden" name="a_add" id="a_add" value="A">
<table cellspacing="0" class="ewGrid"><tr><td class="ewGridContent">
<div class="ewGridMiddlePanel">
<table cellspacing="0" class="ewTable">
<% If zlogin.username.Visible Then ' username %>
	<tr<%= zlogin.username.RowAttributes %>>
		<td class="ewTableHeader">用户名</td>
		<td<%= zlogin.username.CellAttributes %>><span id="el_username">
<input type="text" name="x_username" id="x_username" size="30" maxlength="50" value="<%= zlogin.username.EditValue %>"<%= zlogin.username.EditAttributes %>>
</span><%= zlogin.username.CustomMsg %></td>
	</tr>
<% End If %>
<% If zlogin.password.Visible Then ' password %>
	<tr<%= zlogin.password.RowAttributes %>>
		<td class="ewTableHeader">密码</td>
		<td<%= zlogin.password.CellAttributes %>><span id="el_password">
<input type="text" name="x_password" id="x_password" size="30" maxlength="50" value="<%= zlogin.password.EditValue %>"<%= zlogin.password.EditAttributes %>>
</span><%= zlogin.password.CustomMsg %></td>
	</tr>
<% End If %>
<% If zlogin.levels.Visible Then ' levels %>
	<tr<%= zlogin.levels.RowAttributes %>>
		<td class="ewTableHeader">等级</td>
		<td<%= zlogin.levels.CellAttributes %>><span id="el_levels">
<% If Not Security.IsAdmin And Security.IsLoggedIn() Then ' Non system admin %>
<div<%= zlogin.levels.ViewAttributes %>><%= zlogin.levels.EditValue %></div>
<% Else %>
<select id="x_levels" name="x_levels"<%= zlogin.levels.EditAttributes %>>
<%
emptywrk = True
If IsArray(zlogin.levels.EditValue) Then
	arwrk = zlogin.levels.EditValue
	For rowcntwrk = 0 To UBound(arwrk, 2)
		If arwrk(0, rowcntwrk)&"" = zlogin.levels.CurrentValue&"" Then
			selwrk = " selected=""selected"""
			emptywrk = False
		Else
			selwrk = ""
		End If
%>
<option value="<%= Server.HtmlEncode(arwrk(0, rowcntwrk)&"") %>"<%= selwrk %>>
<%= arwrk(1, rowcntwrk) %>
</option>
<%
	Next
End If
%>
</select>
<% End If %>
</span><%= zlogin.levels.CustomMsg %></td>
	</tr>
<% End If %>
</table>
</div>
</td></tr></table>
<p>
<input type="submit" name="btnAction" id="btnAction" value="    增加    ">
</form>
<script language="JavaScript" type="text/javascript">
<!--
// Write your table-specific startup script here
// document.write("page loaded");
//-->
</script>
<!--#include file="footer.asp"-->
<%

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

' -----------------------------------------------------------------
' Page Class
'
Class czlogin_add

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

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

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

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

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

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

	Public Property Let Message(v)
		If Session(EW_SESSION_MESSAGE) <> "" Then ' Append
			Session(EW_SESSION_MESSAGE) = Session(EW_SESSION_MESSAGE) & "<br>" & v
		Else
			Session(EW_SESSION_MESSAGE) = v
		End If
	End Property

	' Show Message
	Public Sub ShowMessage()
		If Message <> "" Then ' Message in Session, display
			Response.Write "<p><span class=""ewMessage"">" & Message & "</span></p>"
			Session(EW_SESSION_MESSAGE) = "" ' Clear message in Session
		End If
	End Sub

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

	' -----------------------------------------------------------------
	'  Class initialize
	'  - init objects
	'  - open ADO connection
	'
	Private Sub Class_Initialize()

		' Initialize table object
		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 = "login"

		' Open connection to the database
		Set Conn = Server.CreateObject("ADODB.Connection")
		Conn.Open EW_DB_CONNECTION_STRING
	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(TableName)

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

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

		' Page load event, used in current page
		Call 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()
		If Not (Conn Is Nothing) Then Conn.Close ' Close Connection
		Set Conn = Nothing
		Set Security = Nothing
		Set zlogin = Nothing

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

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

	Dim x_ewPriv

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

		' Load key values from QueryString
		Dim bCopy
		bCopy = True
		If Request.QueryString("id").Count > 0 Then
			zlogin.id.QueryStringValue = Request.QueryString("id")
		Else
			bCopy = False
		End If

		' Create form object
		Set ObjForm = New cFormObj

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

			' Validate Form
			If Not ValidateForm() Then
				zlogin.CurrentAction = "I" ' Form error, reset action
				Message = gsFormError
			End If

		' Not post back
		Else
			If bCopy Then
				zlogin.CurrentAction = "C" ' Copy Record
			Else
				zlogin.CurrentAction = "I" ' Display Blank Record
				Call LoadDefaultValues() ' Load default values
			End If
		End If

		' Close form object
		Set ObjForm = Nothing

		' Perform action based on action code
		Select Case zlogin.CurrentAction
			Case "I" ' Blank record, no action required
			Case "C" ' Copy an existing record
				If Not LoadRow() Then ' Load record based on key
					Message = "没有找到记录" ' No record found
					Call Page_Terminate("zloginlist.asp") ' No matching record, return to list
				End If
			Case "A" ' Add new record
				zlogin.SendEmail = True ' Send email on add success
				If AddRow() Then ' Add successful
					Message = "记录增加成功" ' Set up success message
					Dim sReturnUrl
					sReturnUrl = zlogin.ReturnUrl
					If ew_GetPageName(sReturnUrl) = "zloginview.asp" Then sReturnUrl = zlogin.ViewUrl ' View paging, return to view page with keyurl directly
					Call Page_Terminate(sReturnUrl) ' Clean up and return
				Else
					Call RestoreFormValues() ' Add failed, restore form values
				End If
		End Select

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

		' Render row
		Call RenderRow()
	End Sub

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

		' Get upload data
	End Function

	' -----------------------------------------------------------------
	' Load default values
	'
	Function LoadDefaultValues()
		zlogin.levels.CurrentValue = 0
	End Function

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

		' Load values from form
		zlogin.username.FormValue = ObjForm.GetValue("x_username")
		zlogin.username.OldValue = ObjForm.GetValue("o_username")
		zlogin.password.FormValue = ObjForm.GetValue("x_password")
		zlogin.password.OldValue = ObjForm.GetValue("o_password")
		zlogin.levels.FormValue = ObjForm.GetValue("x_levels")
		zlogin.levels.OldValue = ObjForm.GetValue("o_levels")
		zlogin.id.FormValue = ObjForm.GetValue("x_id")
	End Function

	' -----------------------------------------------------------------
	' Restore form values
	'
	Function RestoreFormValues()
		zlogin.username.CurrentValue = zlogin.username.FormValue
		zlogin.password.CurrentValue = zlogin.password.FormValue
		zlogin.levels.CurrentValue = zlogin.levels.FormValue
		zlogin.id.CurrentValue = zlogin.id.FormValue
	End Function

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

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

		' Load sql based on filter
		zlogin.CurrentFilter = sFilter
		sSql = zlogin.SQL
		If EW_DEBUG_ENABLED Then Response.Write sSql ' Show SQL for debugging
		Set RsRow = Server.CreateObject("ADODB.Recordset")
		RsRow.Open sSql, Conn
		If RsRow.Eof Then
			LoadRow = False
		Else
			LoadRow = True
			RsRow.MoveFirst
			Call LoadRowValues(RsRow) ' Load row values

			' Call Row Selected event
			Call zlogin.Row_Selected(RsRow)
		End If
		RsRow.Close
		Set RsRow = Nothing
	End Function

	' -----------------------------------------------------------------
	' Load row values from recordset
	'
	Sub LoadRowValues(RsRow)
		zlogin.id.DbValue = RsRow("id")
		zlogin.username.DbValue = RsRow("username")
		zlogin.password.DbValue = RsRow("password")
		zlogin.levels.DbValue = RsRow("levels")
	End Sub

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

		' Call Row Rendering event
		Call zlogin.Row_Rendering()

		' ---------------------------------------
		'  Common render codes for all row types
		' ---------------------------------------
		' username

		zlogin.username.CellCssStyle = ""
		zlogin.username.CellCssClass = ""

		' password
		zlogin.password.CellCssStyle = ""
		zlogin.password.CellCssClass = ""

		' levels
		zlogin.levels.CellCssStyle = ""
		zlogin.levels.CellCssClass = ""

		' -----------
		'  View  Row
		' -----------

		If zlogin.RowType = EW_ROWTYPE_VIEW Then ' View row

			' id
			zlogin.id.ViewValue = zlogin.id.CurrentValue
			zlogin.id.CssStyle = ""
			zlogin.id.CssClass = ""
			zlogin.id.ViewCustomAttributes = ""

			' username
			zlogin.username.ViewValue = zlogin.username.CurrentValue
			zlogin.username.CssStyle = ""
			zlogin.username.CssClass = ""
			zlogin.username.ViewCustomAttributes = ""

			' password
			zlogin.password.ViewValue = zlogin.password.CurrentValue
			zlogin.password.CssStyle = ""
			zlogin.password.CssClass = ""
			zlogin.password.ViewCustomAttributes = ""

			' levels
			If (Security.CurrentUserLevel And EW_ALLOW_ADMIN) = EW_ALLOW_ADMIN Then ' System admin
			If zlogin.levels.CurrentValue & "" <> "" Then
				sSqlWrk = "SELECT [UserLevelName] FROM [UserLevels] WHERE [UserLevelID] = " & ew_AdjustSql(zlogin.levels.CurrentValue) & ""
				Set RsWrk = Conn.Execute(sSqlWrk)
				If Not RsWrk.Eof Then
					zlogin.levels.ViewValue = RsWrk("UserLevelName")
				Else
					zlogin.levels.ViewValue = zlogin.levels.CurrentValue
				End If
				RsWrk.Close
				Set RsWrk = Nothing
			Else
				zlogin.levels.ViewValue = Null
			End If
			Else
				zlogin.levels.ViewValue = "********"
			End If
			zlogin.levels.CssStyle = ""
			zlogin.levels.CssClass = ""
			zlogin.levels.ViewCustomAttributes = ""

			' View refer script
			' username

			zlogin.username.HrefValue = ""

			' password
			zlogin.password.HrefValue = ""

			' levels
			zlogin.levels.HrefValue = ""

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

		ElseIf zlogin.RowType = EW_ROWTYPE_ADD Then ' Add row

			' username
			zlogin.username.EditCustomAttributes = ""
			zlogin.username.EditValue = ew_HtmlEncode(zlogin.username.CurrentValue)

			' password
			zlogin.password.EditCustomAttributes = ""
			zlogin.password.EditValue = ew_HtmlEncode(zlogin.password.CurrentValue)

			' levels
			zlogin.levels.EditCustomAttributes = ""
			If Not Security.CanAdmin Then ' System admin
				zlogin.levels.EditValue = "********"
			Else
			sSqlWrk = "SELECT [UserLevelID], [UserLevelName], '' AS Disp2Fld, '' AS SelectFilterFld FROM [UserLevels]"
			sWhereWrk = ""
			If sWhereWrk <> "" Then sSqlWrk = sSqlWrk & " WHERE " & sWhereWrk
			Set RsWrk = Server.CreateObject("ADODB.Recordset")
			RsWrk.Open sSqlWrk, Conn
			If Not RsWrk.Eof Then
				arwrk = RsWrk.GetRows
			Else
				arwrk = ""
			End If
			RsWrk.Close
			Set RsWrk = Nothing
			arwrk = ew_AddItemToArray(arwrk, 0, Array("", "请选择"))
			zlogin.levels.EditValue = arwrk
			End If
		End If

		' Call Row Rendered event
		Call zlogin.Row_Rendered()
	End Sub

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

		' Initialize
		gsFormError = ""

		' Check if validation required
		If Not EW_SERVER_VALIDATE Then
			ValidateForm = (gsFormError = "")
			Exit Function
		End If

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

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

	' -----------------------------------------------------------------
	' Add record
	'
	Function AddRow()
		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

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

		' Field username
		Call zlogin.username.SetDbValue(zlogin.username.CurrentValue, Null)
		Rs("username") = zlogin.username.DbValue

		' Field password
		Call zlogin.password.SetDbValue(zlogin.password.CurrentValue, Null)
		If Not EW_CASE_SENSITIVE_PASSWORD And Not IsNull(zlogin.password.DbValue) Then zlogin.password.DbValue = LCase(zlogin.password.DbValue)
		If EW_MD5_PASSWORD And Not IsNull(zlogin.password.DbValue) And (zlogin.password.DbValue <> Rs("password")) Then zlogin.password.DbValue = MD5(zlogin.password.DbValue)
		Rs("password") = zlogin.password.DbValue

		' Field levels
		If Security.CanAdmin Then ' System admin
		Call zlogin.levels.SetDbValue(zlogin.levels.CurrentValue, Null)
		Rs("levels") = zlogin.levels.DbValue
		End If

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

		' Call Row Inserting event
		bInsertRow = zlogin.Row_Inserting(Rs)
		If bInsertRow Then

			' Clone new recordset object
			Set RsNew = ew_CloneRs(Rs)
			Rs.Update
			If Err.Number <> 0 Then
				Message = Err.Description
				AddRow = False
			Else
				AddRow = True
			End If
		Else
			Rs.CancelUpdate
			If zlogin.CancelMessage <> "" Then
				Message = zlogin.CancelMessage
				zlogin.CancelMessage = ""
			Else
				Message = "插入已取消"
			End If
			AddRow = False
		End If
		Rs.Close
		Set Rs = Nothing
		If AddRow Then
			zlogin.id.DbValue = RsNew("id")

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

	' Page Load event
	Sub Page_Load()

		'Response.Write "Page Load"
	End Sub

	' Page Unload event
	Sub Page_Unload()

		'Response.Write "Page Unload"
	End Sub

	' Form Custom Validate event
	Function Form_CustomValidate(CustomError)

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