<%@ LANGUAGE=VBScript EnableSessionState=false LCID=1033 %>
<!--#include virtual="/lib/lib_global.asp"-->
<%
' -------------------------------- PRELUDE -------------------------------------
Dim Email, Password, AutoLogin
Dim FromSection, NextPage, SessionExpired, ExpiredAt


' ------------------------------- PRE-PAGE -------------------------------------
Sub PrePage

   General_PrePage()

   ' --------------------------
   ' Read Input Variables
   ' --------------------------
   FromSection     = ParseRequestText(Request("From"), null, null)
   NextPage        = ParseRequestText(Request("NextPage"), null, null)
   SessionExpired  = ParseRequestBoolean(Request("SessionExpired"), False, null)
   ExpiredAt       = ParseRequestText(Request("ExpiredAt"), null, null)


   ' --------------------------
   ' Setup Page Variables
   ' --------------------------
   Page.Title = "Login"
   Page.HideToolbar = HIDE_TOOLBAR


   ' --------------------------
   ' Check If Already Logged In
   ' --------------------------
   If (Session.UserID > 0) Then
      RedirectTo("/apps/clients/home.asp")
   End If

End Sub


' ------------------------------- POST-PAGE ------------------------------------
Sub PostPage

   General_PostPage()

End Sub


' ------------------------------ VALIDATION ------------------------------------
Sub Validation

   Dim Rs, SQL
   Dim ClientID, DBPassword, IsActive
   Dim RecordLogin


   ' --------------------------
   ' Read Form Inputs
   ' --------------------------
	Email      = ParseFormText("Email", "", null)
	Password   = ParseFormText("Password", "", null)
   AutoLogin  = ParseFormBoolean("AutoLogin", False, null)   


   ' --------------------------
   ' Validate Form Fields
   ' --------------------------
   FormCheckRequired "Email",    "You must enter your e-mail address."
   FormCheckRequired "Password", "You must enter your password."


   If (Not ErrorsExist()) Then

      ' --------------------------
      ' Check Username & Password
      ' --------------------------
      SQL = "SELECT client_id, password, is_active" &_
             " FROM t_Main_Clients" &_
            " WHERE email = " & FormatSQLText(Email)

      Set Rs = Database.GetSingleRecord(SQL)
      If (Not Rs.EOF) Then
         DBPassword = ParseSQLText(Rs("password"))
         IsActive   = ParseSQLBoolean(Rs("is_active"))

         If (Password <> DBPassword) Then
            AddError "Password", "The password you entered is incorrect."
         ElseIf (Not IsActive) Then
            AddError "IsActive", "Your account has been de-activated. Please contact support for more information."
         Else         
            ClientID = ParseSQLInteger(Rs("client_id"))
         End If
      Else
         AddError "Email", "The e-mail address you entered is not registered."
      End If
      Rs.Close
   

      ' --------------------------
      ' Log User In & Redirect
      ' --------------------------
      If (Not ErrorsExist()) Then

         ' --------------------------
         ' Record Login
         ' --------------------------
         RecordLogin = True
         
         DoLogin ClientID, Email, RecordLogin
         

         ' --------------------------
         ' Check for autologin
         ' --------------------------
         If (AutoLogin) Then
            SetAutoLogin()
         End If

         ' ***TODO***
         ' Add hook in here for when ads purchased == ads used to redirect
         ' to resubscribe page. Possible add the hook in the DoLogin
         ' function.


         ' --------------------------
         ' Redirect
         ' --------------------------
         If (Not IsBlank(NextPage)) Then
            RedirectTo(NextPage)
         Else
   			RedirectTo("/apps/clients/home.asp")
   	   End If
      End If

   End If
End Sub


' -------------------------------- OUTPUT --------------------------------------
Sub Output

   If (SessionExpired) Then
%>
      <CENTER>
      <P>
      <TABLE CELLSPACING=0 CELLPADDING=8 BORDER=1 WIDTH="75%"><TR><TD>
      <%= bt_NormalText %>
      Your session has expired.  You will need to log in again.

      <P>
      <B>NOTE:</B> Sessions expire after 20 minutes of inactivity as a security 
      feature.  This prevents someone from sitting down at a computer that you
      were logged into and accessing your account.
      <%= et_NormalText %>
      </TD></TR></TABLE>
      </CENTER>
<%
   End If
%>

   <CENTER>
<%
   DisplayErrors()
%>

   <% AddHiddenFormVar "NextPage", NextPage %>
   <%= FormStart() %>
   
   <TABLE BORDER=0>
      <TR>
         <TD ALIGN=right><%= bt_FormFieldLabel %>
            <% HighlightOnError "Email", "E-mail:" %>
         <%= et_FormFieldLabel %></TD>

         <TD><INPUT TYPE=text NAME="Email" VALUE="<%= Email %>" SIZE=20 MAXLENGTH=50></TD>
      </TR>
      
      <TR>
         <TD ALIGN=right><%= bt_FormFieldLabel %>
            <% HighlightOnError "Password", "Password:" %>
         <%= et_FormFieldLabel %></TD>

         <TD><INPUT TYPE=password NAME="Password" VALUE="<%= Password %>" SIZE=20 MAXLENGTH=20></TD>
      </TR>

      <TR><TD><IMG SRC="/images/spacer.gif" WIDTH=1 HEIGHT=4></TD></TR>
      
      <TR>
        <TD ALIGN=right VALIGN=top><%= FormCheckbox("AutoLogin", AutoLogin, True) %></TD>
        <TD><%= bt_NormalText %>
           Remember my e-mail and<BR>
           password on this computer<%= et_NormalText %>
        </TD>
      </TR>

      <TR>
         <TD COLSPAN="2" ALIGN="center">
            <BR>
            <INPUT TYPE=submit NAME="login" VALUE="Login">
         </TD>
      </TR>

      <TR>
         <TD COLSPAN="2" ALIGN="center"><%= bt_NormalText %>
            <BR>
            <A HREF="<%= EncodeURL("register.asp") %>">Don't Have An Account? Register Now!</A>
         <%= et_NormalText %></TD>
      </TR>
   </TABLE>

   </FORM>
<%
   End Sub
%>

<!--#include virtual="/lib/lib_page-processor.asp"-->

