<%
Response.Expires = 60
Response.Expiresabsolute = Now() - 1
Response.AddHeader "pragma","no-cache"
Response.AddHeader "cache-control","private"
Response.CacheControl = "no-cache"


Function Restore_Variables (Realm_ID)

'* Session Variable Section 

	Session.Timeout = 120
	
	SQL = "Get_Realms_By_Realm @Realm_ID = '" & Replace(Realm_ID, "'", "''") & "'"
	'Response.Write SQL
	Set RSRealm = Conn.Execute(SQL)
	Session("System_Realm") = RSRealm("Realm_ID_1")
	
	Site_Count = 0
	Do While Not RSRealm.EOF
		Select Case CInt(RSRealm("Realm_Type_ID_2"))
			Case 1	'1 admin
				Session("Admin_Realm") = RSRealm("Realm_ID_2")
			Case 2	'2 site
				Site_Count = Site_Count + 1
				Site_Realm = RSRealm("Realm_ID_2")
			Case 3	'3 merchant
				Session("Merchant_Realm") = RSRealm("Realm_ID_2")
			Case 6	'6 partner
				Session("Partner_Realm") = RSRealm("Realm_ID_2")
			Case 8	'8 staff
				Session("Staff_Realm") = RSRealm("Realm_ID_2")
		End Select
	
		RSRealm.MoveNext
	Loop
	
	Session("Site_Count") = Site_Count
	
	
	SQL = "Get_Realm_Group @Realm_ID='" & Session("System_Realm") & "'"
	' Response.Write SQL
	Set RSGroup = Conn.Execute(SQL)
		Session("Entity_Group_ID") = RSGroup("Group_ID")
		Session("Entity_Name") = RSGroup("Name")
		Session("Entity_Lookup_Group") = RSGroup("Lookup_ID")
	Set RSGroup = Nothing
	
	
	SQL = "Get_Merchant @Group_ID = '" & Session("Entity_Group_ID") & "'"
	Set RSMerchant = Conn.Execute(SQL)
	    	Session("Merchant_ID") = "1" 'Vacation in Florida.com Merchant ID
	Set RSMerchant = Nothing
	
	If Session("Site_Count") = 1 Then 
		Session("Site_Realm") = Site_Realm
	Else
		Session("Site_Realm") = ""
	End If

End Function


%>
<%
Request_URL = Request("URL")
If Request("URL") = "/Default.asp" Then Request_URL = ""
If lcase(Request("URL")) = "/verify.asp" Then Request_URL = ""

' issue with admin realm.. if you bypass realm selection.. we don't know a realm id for the area! so.. disabled for now..
Request_URL = ""


'currently secured areas.. 
'demo
'admin
'partner

Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open "DRIVER={SQL Server};SERVER=WEBSRV01;UID=web;pwd=w3bs1t3;DATABASE=AVANTE"


If Request("lo") = "1" Then
' log out
	Session("Authenticated") = ""
	Session("User_ID") = ""
	Session("User_Name") = ""

	Response.Redirect ("/")
End If


If Not Request("Realm_ID") = "" And Session("Realm_Present") = True Then


	Session("Realm_Present") = ""
	Session("Realm_ID") = Replace(Request("Realm_ID"), "'", "''")

	SQL = "Select Realm_Entry_URL, User_Enabled from Realm Where Realm_ID='" & Replace(Request("Realm_ID"), "'", "''") & "'"
	Set RSGetRealm = Conn.Execute(SQL)
	SQL = "Select Start_Page from User_Realm_Start_Page Where Realm_ID='" & Replace(Request("Realm_ID"), "'", "''") & "' And User_ID = '" & Session("User_ID") & "'"
	Set RSUser = Conn.Execute(SQL)
	X = Restore_Variables (Replace(Request("Realm_ID"), "'", "''"))
	If Not RSUser.EOF Then
		URL = RSUser("Start_Page")
		Response.Redirect (URL)
	ElseIf Not RSGetRealm.EOF Then
		URL = RSGetRealm("Realm_Entry_URL")
		Response.Write URL
		If Not URL = "" Then
			Response.Redirect(URL)
		Else
	%>		"You have successfully authenticated!"   <%
		End IF
	End If
End If



GoodLogin = False
	
If UCase(Request.ServerVariables("REQUEST_METHOD")) = "POST" Then

	Session("User_ID") = ""

	SQL = "Validate_User @User_Name='" & Replace(Request("User_Name"), "'", "''") & "', @User_PWD='" & Replace(Request("User_PWD"), "'", "''") & "'"
'	Response.Write SQL
'Response.End
	Set RSUser = Conn.Execute(SQL)

	If RSUser.EOF Then
		GoodLogin = False
	Else
		GoodLogin = True
' this portion is to validate whether the user has access, given that a @Key_ID or a @Key_Ring_ID was passed to the validate_user proc
'		If isNull(RSUser("Key_Ring_ID")) Then 
'			'user is not a User... because of key id 9 passed in statement.. and key ring id being null
'			Good_Login = False
'		End If
	End If

	If GoodLogin = True Then
		Session("User_ID") = RSUser("User_ID")
		Session("User_Name") = RSUser("User_Name") 
		If Session("User_Name") = "" Then Session("User_Name") = "No User Name Provided"
	End If

End If

If GoodLogin = True Then 

	Session("Authenticated") = True 

	
		Count = 0
		Do While Not RSUser.EOF
			Count = Count + 1
			RSUser.MoveNext
		Loop
		RSUser.MoveFirst

		Session("Realm_Present") = True

		If Count > 1 Then
%>

		<form name="form1" method="post" action = ""><Br>
		<input type="hidden" name="Request_URL" value="<%=Request_URL%>">
		<table border="0" cellspacing="0" width="90%" align="center">
		<tr><td align="center" width="300">
		Please select your realm of entry:<br><br>
			<select name="Realm_ID" id="Realm_ID" size="5" onChange="document.form1.submit();">
<%
		Do While Not RSUser.EOF
			If Not CStr(RSUser("User_Enabled")) = "False"  Then
			%>
			<option value="<%=RSUser("Realm_ID")%>"><%=RSUser("Realm_Name")%></option>
			<%
			End IF
			RSUser.MoveNext
		Loop
		RSUser.MoveFirst

%>			</select><br><br>
<!--		<input type="submit" name="submit" onClick="return if (!document.form1.Realm_ID.selected[selectedIndex] == '')" value="Click here to enter!">-->
		</td><td>&nbsp;</td></tr>
		</table>
		</form>		

<%
		Else
			X = Restore_Variables (RSUser("Realm_ID"))

			If Not RSUser("Start_Page") = "" Or Not IsNull(RSUser("Start_Page")) Then
				Response.Redirect (RSUser("Start_Page"))
			Else
				Response.Redirect (RSUser("Realm_Entry_URL"))
			End If

		End If

Else
	If Not Request_URL = "" Then
		Response.Redirect ("/?URL=" & Request_URL)
	Else
		Response.Redirect ("/")
	End If
End If


%>