CSCI 4230
Internet Applicaiton Development
Fall 2001
Suggested Solution to Homework #6

(1)    For example:

<%   Option Explicit %>
<!--
 h6.asp
 Kwok-bun Yue October 24, 2001
-->
<html>
<head>
<title>Homework #6, CSCI 4230, Fall 2001</title>
<link rel="stylesheet" type="text/css" href="styles/h6.css">
<script>

// firstCharDirectory["A"] is the HTML directory information of
//  employee with last name starting with "A".
var firstCharDirectory = new Object();
//  Populate firstCharDirectory
<%
 ' Porting variables
 Dim DSN
 DSN = "yuep"

 ' Open database connection
 Dim conn
 set conn = Server.createObject("adodb.connection")
 conn.open DSN
 Dim strSql
 strSql = "select EmployeeLastName, EmployeeFirstName, EmployeeEmail, EmployeePhone, EmployeeURL " & _
       "from f01h6 " & _
    "order by EmployeeLastName, EmployeeFirstName"

 ' Total number of records.
    Dim count
 count = 0

 ' directory{"a"} stores all employee information with last
 ' name starting with "A"
 Dim directory
 Set directory = Server.CreateObject("Scripting.Dictionary")

 Dim CurrFirstChar

 Dim ResultRS
 Set ResultRS = conn.execute(strSql)
 if not ResultRS.eof then
  do while not ResultRS.eof
   count = count + 1
   CurrFirstChar = UCase(Mid(ResultRS.fields("EmployeeLastName"),1,1))
   Dim CurrentEmployee
   CurrentEmployee = "<a href=""mailto:" & ResultRS.fields("EmployeeEmail") _
        & """>" & ResultRS.fields("EmployeeFirstName") & " " _
        & ResultRS.fields("EmployeeLastName") & "</a>: " _
        & ResultRS.fields("EmployeePhone") & ", <a href=""" _
        & ResultRS.fields("EmployeeURL") & """>"  _
        & ResultRS.fields("EmployeeURL") & "</a>" _
        & ".<br>"
   if directory.Exists(CurrFirstChar) then
    CurrentEmployee = directory.Item(CurrFirstChar) & CurrentEmployee
    directory.Remove CurrFirstChar
    directory.Add CurrFirstChar, CurrentEmployee
   else
    directory.Add CurrFirstChar, CurrentEmployee
   end if
     ResultRS.movenext
  loop
 end if

 ' Print out the contents of VBScript directory to
 ' Client side Javascript hash firstCharDirectory
 Dim FirstCharacters
 FirstCharacters = directory.Keys
 Dim i
 Dim tempStr
 For i = 0 To directory.Count - 1
  tempStr = directory.Item(FirstCharacters(i))
        tempStr = Replace(tempStr,"\","\\")
        tempStr = Replace(tempStr,"""","\""")

  Response.Write("firstCharDirectory[""" & FirstCharacters(i) & """] = " _
      & """" & tempStr & """;" & vbCrLf)
 Next ' i
%>

// show directory with first char ch using dynamic HTML.
function showDirectory(ch) {
    var directoryResultHtmlCode = '<div id="directoryResult" class="result" style="position:absolute">' + firstCharDirectory[ch] + '</div>';
    if (document.layers){
        document.directoryResult.document.write(directoryResultHtmlCode);
        document.directoryResult.document.close();
    }
    else if (document.all)
       directoryResult.innerHTML = firstCharDirectory[ch];
}
</script>

</head>
<body>
<h3>Employee directory</h3>
Click the button to see the employee information:
<p>
<form>
<script>
// Print all first character buttons of last name
//  <input type="button" value="A" onClick="showDirectory('A')">&nbsp;&nbsp;
var chars = new Array();
for (var prop in firstCharDirectory)
 chars[chars.length] = prop;
chars.sort();

for (var i = 0; i <chars.length; i++) {
 document.write("<input type=\"button\" value=\"" + chars[i] + "\" onClick=\"showDirectory('" +
     chars[i] + "')\">&nbsp;&nbsp;\n");
}
</script>
</form>
<p>
<div id="directoryResult" class="result" style="position:absolute">Directory result will be listed here.</div>
</body>
</html>

CSS file used:

body {font-size:10pt; font-family:Arial;}
h1, h2, h3, h4, h5, h6 {font-family:Arial;}
body {background-color:#ccccff;}
A:link     {color:#80000; text-decoration:none;}
A:visited {color:#008000; text-decoration:none;}
A:active  {color:#000080; text-decoration:none;}
A:hover  {color:#c00000; text-decoration:none;}

.result {background-color:#ccccff; font-family:verdana; border:1px solid; padding: 5px; width:500px}

(2)    For example:

<%   Option Explicit %>
<% On Error Resume Next %>
<!--
 h6admin.asp
 Kwok-bun Yue  November 4, 2001

 There is no error checking in this version.
-->
<% ' Porting variables
 Dim DSN
 DSN = "yuep"
%>

<%
' display the login form.
sub LoginForm
%>
<form method="post">
Username:
<input type="text" name="User" size="10" maxlength="12">
<br>
Password:
<input type="password" name="Pass" size="10" maxlength="12">
<p>
<input type="submit" value="Submit">&nbsp;&nbsp;&nbsp;
<input type="reset">
<br>
</form>
<%
end sub 'LoginForm

' Initial Login form page
sub DisplayLoginFormPage()
%>
<body>
<h3>Please login to the employee directory administration utility:</h3>
<p>
<% LoginForm  %>
</body>
</html>
<%
end sub ' DisplayLoginFormPage

' Insert an employee record: no error checking.
function insertNewEmployee(EmployeeLastName, EmployeeFirstName, EmployeeEmail, EmployeePhone, EmployeeURL)
 Dim conn
 set conn = Server.createObject("adodb.connection")
 conn.open DSN
 Dim strSql
 strSql = "insert into f01h6(EmployeeLastName, EmployeeFirstName, EmployeeEmail, EmployeePhone, EmployeeURL) " & _
    "values ('" & replace(EmployeeLastName,"'","''") & "','" & _
     replace(EmployeeFirstName,"'","''") & "','" &  replace(EmployeeEmail,"'","''") & _
     "','" &  replace(EmployeePhone,"'","''") & "','" & _
     replace(EmployeeURL,"'","''") & "')"

 ' debug:  Response.write strSql
 Dim ResultRS
 ResultRS = conn.execute(strSql)
 insertNewEmployee = "The record for employee " & EmployeeFirstName & " " & EmployeeLastName & _
      " has been added successfully."
end function ' insertNewEmployee

' Main administration page after attempting to
' insert an employee result.  If InsertionResult
' is empty, then previous employee record insertion
' is not successful.
sub adminPage(InsertionResult)
%>
<body>
<h3>Employee directory administration utility</h3>
<% = InsertionResult %>
<p>
Fill in the following form to add a new employee record:
<br>
<form method="post">
Employee last name:
<input type="text" name="LName" size="20" maxlength="50">
<br>
Employee first name:
<input type="text" name="FName" size="20" maxlength="50">
<br>
Employee phone number:
<input type="text" name="Phone" size="12" maxlength="12">
<br>
Employee email address:
<input type="text" name="Email" size="30" maxlength="50">
<br>
Employee URL:
<input type="text" name="URL" size="50" maxlength="255">
<p>
<input type="submit" value="Add Record">&nbsp;&nbsp;&nbsp;
<input type="reset">
<br>
</form>
</body>
</html>
<%
end sub ' adminPage

' Authenticate username and password by database access.
sub AuthenticatePage(User, Pass)
 Dim conn
 set conn = Server.createObject("adodb.connection")
 conn.open DSN
 Dim strSql
 strSql = "select * " & _
       "from f01h6admin " & _
    "where username = '" & User & "' and password ='" & Pass & "'"

 Dim ResultRS
 Set ResultRS = conn.execute(strSql)
 if not ResultRS.eof then
  ' Successful Authentication.
  Session("User") = User
  Session("Pass") = Pass
  adminPage("")
 else
%>
<body>
<h3>Incorrect login</h3>
Sorry, incorrect user name or password, please try again.
<p>
<% LoginForm  %>
</body>
</html>
<%
 end if
end sub ' AuthenticatePage

%>
<html>
<head>
<title>Homework #6, CSCI 4230, Fall 2001 Administration Page</title>
<link rel="stylesheet" type="text/css" href="styles/h6.css">
</head>

<% ' Get session variables.
 Dim SessionUser, SessionPass
 SessionUser = Session("User")
 ' Session password not used currently.  For future uses.
 SessionPass = Session("Pass")

 ' HTTP Parameters.
 Dim EmployeeLastName, EmployeeFirstName, EmployeeEmail, EmployeePhone, EmployeeURL
 Dim User, Pass
 EmployeeLastName = Request.Form("LName")
 EmployeeFirstName = Request.Form("FName")
 EmployeeEmail = Request.Form("Email")
 EmployeePhone = Request.Form("Phone")
 EmployeeURL = Request.Form("URL")
 User = Request.Form("User")
 Pass = Request.Form("Pass")

 Dim InsertionResult
 InsertionResult = ""

 if (SessionUser = "") then
  if (User = "" or Pass = "") then
   DisplayLoginFormPage
  else
   AuthenticatePage USer, Pass
  end if
 else
  if (EmployeeLastName <> "") then  ' More sophisticated error handling to be added.
   InsertionResult = insertNewEmployee(EmployeeLastName, EmployeeFirstName, EmployeeEmail, EmployeePhone, EmployeeURL)
  end if
  adminPage(InsertionResult)
 end if
%>