%
On Error Resume Next
action_result = ""
error_dump = ""%>
<%
Set rs = Server.Createobject("ADODB.Recordset")
rs.CursorType = adOpenStatic
rs.LockType = adLockOptimistic
action = Request.QueryString("a")
If Session("RP") = "" then
	Session("RP") = Request.QueryString("return_page")
	Session("QS") = Request.ServerVariables("QUERY_STRING")
end if
if action = "p" then
	'*******************************
	'* Process the sign-in attempt *
	'*******************************
	Session("num_tries") = Session("num_tries") + 1
	if Session("num_tries") < 4 then
		'**************
		'* Process it *
		'**************
		rs.Open  "SELECT id,username,access,date_last_visit,date_nexttolast_visit,ip FROM cam_users WHERE username = '" & Request.Form("username") &"' AND passwd = '" & Request.Form("passwd") & "'", Con
		check_for_error()
		if NOT rs.EOF then
			'*******************************************************
			'* Registered user. Assign access level and log them   *
			'*******************************************************
			the_date = DateAdd("n",Session("timediff"),Now)
      		Con.BeginTrans
				Session("user_id") = 			rs("id")
				Session("access_level") = 		rs("access")
				Session("username") =			rs("username")
            	Session("accepted") =         	0
				rs("date_nexttolast_visit") = 	rs("date_last_visit")
				rs("date_last_visit") = 		the_date
				rs("ip") = 						Request.ServerVariables("REMOTE_ADDR")
				rs.Update
				check_for_error()
				rs.Close
				'*******************
				'* Log the sign-in *
				'*******************
				rs.Open "SELECT * FROM cam_access_log WHERE 1<>1", Con
				rs.AddNew
					rs("user_id") =			Session("user_id")
					rs("log_type") = 		1
					rs("ip") = 				Request.ServerVariables("REMOTE_ADDR")
					rs("event_id") =		e_id
					rs("date_entered") = 	the_date
				rs.Update
				check_for_error()
            rs.Close
			Con.CommitTrans
		else
			'*******************
			'*   	ERROR!       *
			'* Invalid attempt *
			'*******************
			Session("code") = 1
			Response.Redirect "/intranet/apps/sign_in.asp?a=d"
		end if
	else
		'******************
		'*   	ERROR!      *
		'* Too many tries *
		'******************
		Session("code") = 2
		Response.Redirect "/intranet/apps/sign_in.asp?a=d"
	end if
elseif action = "d" or action = "" then
	'*******************************
	'* Display the Sign-in Form    *
	'*******************************
	if Session("num_tries") = "" then
		Session("num_tries") = 0
		Session("code") = 0
	end if
	
	'********************************
	'*       SIGN-IN FORM           *
	'********************************%>
 	
	
   
		
   	CAM Sign-in
			
     		
				<%if Session("code") = 1 then
					'*******************
					'* Invalid sign-in *
					'*******************
					response.write("
Username and password combination is invalid.
")
					response.write("
Please check them and try again.
")
				elseif Session("code") = 2 then
					'***********************
					'* Too many attempts   *
					'***********************
					response.write("
You have exceeded the maximum number of sign-in attempts allowed.
")
				end if
				if Session("code") <> 2 then%>
					
					
				<%end if%>
			
  
      
	<%response.end
end if
	
Session("code") = ""
Session("num_tries") = ""
if Session("QS") = "" then
	redirect_string = Session("RP")
	Session("RP")= ""
else
	redirect_string = Session("RP")&"?"&Session("QS")
	Session("RP") = ""
	Session("QS") = ""
end if
Set rs=Nothing
response.redirect redirect_string
%>