<% 
	' enter this script in one of 6 ways
	' register -> insert ok or fail
	' change profile -> update ok or fail
	
  dim status, formsubmitted, changeProfile, pwFileName1, pwFieldName2
  dim userName, pw, realName, email
  status=0
  formsubmitted=0
  changeProfile=0

  if Request("profile")="-1" or Request.QueryString("profile")=1 then changeProfile=-1
  if Request("content_length")<>0 then formsubmitted=-1 ' content_length doesn't include q.string

  ' if form submitted then save element values for later use in form
  if formsubmitted then
    userName=Request("UserName") ' doesn't throw error if element not present
    pw=Request("PW")
    realName=Request("myName")
    email=Request("email")
  end if
  
  ' profile mode userName is always current user  
  if changeProfile then userName=Session("User")
  
  ' must connect to db if formsubmitted or if changeProfile=1 (to get current settings)
  if formsubmitted or changeProfile then

    Set Conn = Server.CreateObject("ADODB.Connection")
    Conn.Open "Driver={MySQL};DATABASE=consultdb; UID=consult;PASSWORD=jemima64" 

    ' get info about userName
    SQL="SELECT * FROM Users WHERE userName='" & userName & "'"
    set RS=Conn.Execute(SQL)

    if not formsubmitted then
		' get current profile data from database
		' NB DON'T SHOW THE CURRENT PASSWORD
		realName=RS("realName")
		email=RS("email")
	else
		' do something with the data submitted in the form
		if changeProfile then
			if Request("oldpassword")<>RS("password") then
				' password check failed
				status=-2
			else
				' update profile
				status=2
				' find out what needs to be updated
				SQL=""
				if pw<>"" then SQL = SQL & "password='" & pw & "'"
				if realName<>"" then
				   if SQL<>"" then SQL=SQL & "," 
				   SQL = SQL & "realName='" & realName & "'"
				end if
				if email<>"" then
				   if SQL<>"" then SQL=SQL & "," 
				   SQL = SQL & "email='" & email & "' "
				end if
				if SQL<>"" then
				   SQL="UPDATE Users SET " & SQL & "WHERE userName='" & userName & "'"
				   Conn.Execute(SQL)
				end if
			end if
		else
			' must be a new registration - check whether userName already exists
			' set status=1 for not found, -1 for found
			if RS.EOF then 
				status=1
			    Session("User")=userName
			    SQL="INSERT INTO Users (userName,password,realName,email,sessionID) values ("
			    SQL = SQL & "'" & userName & "',"
			    SQL = SQL & "'" & Request("PW") & "',"
			    SQL = SQL & "'" & realName & "',"
			    SQL = SQL & "'" & email & "',"
			    SQL = SQL & "'" & Session.SessionID & "')"
			    Conn.Execute(SQL)
			else 
				status=-1
		    end if
		end if
	end if
	RS.Close
	Conn.Close
	' status=1 indicates a new registration
	' status=2 indicates updated profile
	if status>0 then Response.Redirect "loginok.asp?type="&status
  end if
%>

<script language="JavaScript" type="text/javascript">
<!-- 
	// i=0 for checking register form
	// i=1 for change profile form (only check here is for old password, and newpw=retyped)
	function checkform(i) 
	{
		if (i==1)
		{
			if (regForm.oldpassword.value+""=="")
			{
				alert("Please specify your password")
				regForm.oldpassword.focus()
				return false;
			}
			if (regForm.PW.value+""!="" && regForm.PW.value!=regForm.PW2.value) 
			{
				alert("New passwords don't match")
				regForm.PW.value=""
				regForm.PW2.value=""
				regForm.PW.focus()
				return false;
			}
			regForm.submit()
			return true;
		}
		if (regForm.UserName && regForm.UserName.value.length==0)
		{
			alert("No user name")
			regForm.UserName.focus()
			return false;
		}
		if (regForm.PW.value+""=="")
		{
			alert("No password")
			regForm.PW.focus()
			return false;
		}
		if (regForm.PW.value!=regForm.PW2.value) 
		{
			alert("Passwords don't match")
			regForm.PW.value=""
			regForm.PW2.value=""
			regForm.PW.focus()
			return false;
		}
		if (regForm.myName.value+""=="")
		{
			alert("Please enter your name")
			regForm.myName.focus()
			return false;
		}
		if (regForm.email.value+""=="")
		{
			alert("Please enter your email address")
			regForm.email.focus()
			return false;
		}
		regForm.submit()
	}
// -->
</script>



<HTML>
<BODY>
<FONT  face="Tahoma,Arial,sans-serif">



<!-- 
	4 ways to get here: 
	start registration: status=0, changeProfile=0
	failed registration attempt: status=-1, changeProfile=0
	start changeProfile: status=0, changeProfile=-1
	failed changeProfile attempt: status=-2, changeProfile=-1
-->

<% if status=-1 then %>
<H3>User name '<%= userName %>' is already in use. Please select another one.</H3>
<% elseif status=-2 then %>
<H3>Old password incorrect. Please try again.</H3>
<% end if %>

<FORM NAME=regForm METHOD=POST ACTION=register.asp>
<INPUT type=hidden name=profile value='<%= changeProfile %>'>
<TABLE>

<TR><TD>User name</TD><TD>
<%	if changeProfile then 
		pwFieldName1="New password"
		pwFieldName2="Retype new password"
		Response.Write(userName & "</TD></TR>") %>
<TR><TD>Old password</TD><TD><INPUT type=password NAME=oldpassword></TD></TR>
<%	else 
		pwFieldName1="Password"
		pwFieldName2="Retype password" %>
<INPUT type=text NAME=UserName></TD></TR>
<%	end if %>

<TR><TD><%= pwFieldName1 %></TD><TD><INPUT type=password NAME=PW VALUE='<%= pw %>'></TD></TR>
<TR><TD><%= pwFieldName2 %></TD><TD><INPUT type=password NAME=PW2 VALUE='<%= pw %>'></TD></TR>
<TR><TD>Real name</TD><TD><INPUT type=text NAME=myName VALUE='<%= realName %>'></TD></TR>
<TR><TD>e-mail</TD><TD><INPUT type=text NAME=email VALUE='<%= email %>'></TD></TR>
<TR><TD>&nbsp;</TD><TD>
<%	if changeProfile then %>
<INPUT TYPE=button Value=Submit ONCLICK='javascript:checkform(1)' id=button1 name=button1></TD></TR>
<% else %>
<INPUT TYPE=button Value=Submit ONCLICK='javascript:checkform(0)' id=button2 name=button2></TD></TR>
<% end if %>
</TABLE>
</FORM>

</FONT>
</BODY>
</HTML>

