<%
	'************************************************************************************
	'*																					*
	'*	Sets the global var telling if the person is logged in or not.					*
	'*																					*
	'************************************************************************************
	Sub ACCNT_LoginDetect()
'|43|250|242|73|14|125

		iLoginAccountUnq	= C_NONMEMBER_ID
		bHasAccount			= FALSE
		
		' pw is already encrypted within the session variable
		sSQLText = "SELECT * FROM owAccounts WHERE Login = '" & SQLEncode(Session("Login")) & "' AND Password = '" & Session("Password") & "' AND (AddDate > RemoveDate OR RemoveDate IS NULL) AND Authenticated = 'Y' AND (ExpireDate > #" & CONVERSION_FormatDate(Now(), "S") & "# OR ExpireDate IS NULL)"
		Call DB_getFromDatabase(sSQLText, rsStoreData, "")
		If ( rsStoreData.EOF ) Then
			' if the login/pw combo for the sessions you have isn't in the database, this clears them out so you don't keep trying to
			'	login using them.
			Session("Login")		= ""
			Session("Password")		= ""
			Session("AccountUnq")	= ""
			Session("Auto")			= ""
		Else
			bHasAccount			= True
			iLoginAccountUnq	= CDbl(Trim(rsStoreData("AccountUnq")))
		End If
		Call DB_closeRecordset(rsStoreData)
		
		
	End Sub
	'************************************************************************************
	
	
	'************************************************************************************
	'*																					*
	'*	Checks to see if a user has thr Rights Level on ANY Domain.						*
	'*	This is used when displaying buttons to edit info. Sometimes a person doesn't	*
	'*		have rights on the current domain, but they do on another domain and that	*
	'*		domain will show up in a selection on the page the button accesses, 		*
	'*		therefore we need to allow them access to that page.						*
	'*																					*
	'************************************************************************************
	Function ACCNT_CheckRights(iRightsLvl)
		Dim sSQLText, rsStoreData

		If ( bHasAccount ) Then
			' check to see if it's been saved in the session already
			If ( Session(iLoginAccountUnq & "_" & iRightsLvl) = 1 ) Then
				' the session is already set and it's True
				ACCNT_CheckRights = TRUE
			ElseIf ( Session(iLoginAccountUnq & "_" & iRightsLvl) = -1 ) Then
				' the session is already set and it's False
				ACCNT_CheckRights = FALSE
			Else
				Set rsStoreData	= CreateObject("ADODB.Recordset")
				sSQLText = "SELECT AccountUnq, RightsLvl FROM owARights WHERE RightsLvl = '" & iRightsLvl & "' AND AccountUnq = " & iLoginAccountUnq & " AND (RevokeDate IS NULL OR RevokeDate < GrantDate) UNION SELECT R.AccountUnq, R.RightsLvl FROM owARights R (NOLOCK), owAGroupMap M (NOLOCK), owAGroups G WHERE G.GroupUnq = M.GroupUnq AND M.GroupUnq = R.GroupUnq AND M.AccountUnq = " & iLoginAccountUnq & " AND (R.RevokeDate IS NULL OR R.RevokeDate < R.GrantDate) AND R.RightsLvl = '" & iRightsLvl & "'"
				Call DB_getFromDatabase(sSQLText, rsStoreData, "")
				If ( rsStoreData.EOF ) Then
					ACCNT_CheckRights = FALSE
					Session(iLoginAccountUnq & "_" & iRightsLvl) = -1	' cannot set a session to False - nondeclared sessions are False by definition
				Else
					ACCNT_CheckRights = TRUE
					Session(iLoginAccountUnq & "_" & iRightsLvl) = 1
				End If
				Call DB_closeRecordset(rsStoreData)
				Set rsStoreData	= Nothing
			End If
		Else
			ACCNT_CheckRights = FALSE
		End If
		
	End Function
	'************************************************************************************
	
	
	'************************************************************************************
	'*																					*
	'*	Creates a new rights level on the specified domain for the specified system.	*
	'*																					*
	'************************************************************************************
	Function ACCNT_CreateRightsLvl(sRightLvl, sSystemUnq, iDomainUnq, sRightsConst)
		Dim sSQLText, rsStoreData
		
		If ( Trim(sRightsLvl) <> "" ) And ( Trim(sSystemUnq) <> "" ) And ( iDomainUnq <> "" ) And ( IsNumeric(sSystemUnq) ) And ( IsNumeric(iDomainUnq) ) Then
			Set rsStoreData	= CreateObject("ADODB.Recordset")
			sSQLText = "SELECT * FROM RightsLookup WHERE RightsLvl = '" & sRightsLvl & "' AND DomainUnq = " & iDomainUnq
			Call DB_getFromDatabase(sSQLText, rsStoreData, "")
			If ( NOT rsStoreData.EOF ) Then
				' It doesn't exist, so create it.
				Call DB_updateDatabase("INSERT INTO RightsLookup VALUES (" & iDomainUnq & ", '" & sRightsLvl & "', '" & sRightsConst & "', " & sSystemUnq & ")")
			Else
				ACCNT_CreateRightsLvl = FALSE
			End If
			Call DB_closeRecordset(rsStoreData)
			Set rsStoreData	= Nothing
		Else
			ACCNT_CreateRightsLvl = FALSE
		End If
	End Function
	'************************************************************************************
	
	
	'************************************************************************************
	'*																					*
	'*	This returns the login name of the user whose AccountUnq is passed in.			*
	'*																					*
	'************************************************************************************
	Function ACCNT_UserName(iAccountUnq)
		Dim rsStoreData, sSQLText

		If ( iAccountUnq = "" ) Then
			ACCNT_UserName = ""
		Else
			Set rsStoreData	= CreateObject("ADODB.Recordset")
			sSQLText = "SELECT Login FROM owAccounts WHERE AccountUnq = " & iAccountUnq
			Call DB_getFromDatabase(sSQLText, rsStoreData, "")
			If ( NOT rsStoreData.EOF ) Then
				ACCNT_UserName = rsStoreData("Login")
			End If
			Call DB_closeRecordset(rsStoreData)
			Set rsStoreData	= Nothing
		End If
		
	End Function
	'************************************************************************************
	
	
	'************************************************************************************
	'*																					*
	'*	Writes a value to an ADV. If the ADV doesn't exist, insert it.					*
	'*																					*
	'************************************************************************************
	Sub ACCNT_WriteADV(sADV, sValue, sType, iAccountUnq, sViewLvl)
		Dim rsStoreData, sSQLText, sTempHomeDomain, sVarCharData, sTextData
		
		Set rsStoreData	= CreateObject("ADODB.Recordset")
		
		sSQLText = "SELECT * FROM owAccountData WHERE AccountUnq = " & iAccountUnq & " AND MapName = '" & SQLEncode(sADV) & "'"
		Call DB_getFromDatabase(sSQLText, rsStoreData, "")
		If ( rsStoreData.EOF ) Then
			If ( sViewLvl <> C_PUBLIC_ADV ) And ( sViewLvl <> C_PRIVATE_ADV ) Then
				' if the ViewLvl passed in is not either public or private, set it to private as a default
				sViewLvl = C_PRIVATE_ADV
			End If
			If ( sType = C_VARCHAR_ADV ) Then
				Call DB_updateDatabase("INSERT INTO owAccountData VALUES ('" & SQLEncode(sADV) & "'," & iAccountUnq & ",'" & SQLEncode(sValue) & "','','" & sViewLvl & "', 0, 'N', #" & CONVERSION_FormatDate(Now(), "S") & "#)")
			Else
				Call DB_updateDatabase("INSERT INTO owAccountData VALUES ('" & SQLEncode(sADV) & "'," & iAccountUnq & ",'','" & SQLEncode(sValue) & "','" & sViewLvl & "', 0, 'N', #" & CONVERSION_FormatDate(Now(), "S") & "#)")
			End If
		Else
			sVarCharData	= Trim(rsStoreData("VarCharData"))
			sTextData		= Trim(rsStoreData("TextData"))
			' It exists so UPDATE instead
			If ( sType = C_VARCHAR_ADV ) Then
				If ( sVarCharData = Trim(sValue) ) Then
					' if the data is the same as what's already there, don't update the chage date.
					Call DB_updateDatabase("UPDATE owAccountData SET VarCharData = '" & SQLEncode(sValue) & "' WHERE MapName = '" & SQLEncode(sADV) & "' AND AccountUnq = " & iAccountUnq)
				Else
					Call DB_updateDatabase("UPDATE owAccountData SET VarCharData = '" & SQLEncode(sValue) & "', LastChange = GetDate() WHERE MapName = '" & SQLEncode(sADV) & "' AND AccountUnq = " & iAccountUnq)
				End If
			Else
				If ( sTextData = Trim(sValue) ) Then
					' if the data is the same as what's already there, don't update the change date.
					Call DB_updateDatabase("UPDATE owAccountData SET TextData = '" & SQLEncode(sValue) & "' WHERE MapName = '" & SQLEncode(sADV) & "' AND AccountUnq = " & iAccountUnq)
				Else
					Call DB_updateDatabase("UPDATE owAccountData SET TextData = '" & SQLEncode(sValue) & "', LastChange = GetDate() WHERE MapName = '" & SQLEncode(sADV) & "' AND AccountUnq = " & iAccountUnq)
				End If
			End If
		End If
		Call DB_closeRecordset(rsStoreData)
		Set rsStoreData	= Nothing
		
	End Sub
	'************************************************************************************
	
	
	'************************************************************************************
	'*																					*
	'*	Returns an ADV for a specific domain.											*
	'*																					*
	'************************************************************************************
	Function ACCNT_ReturnADV(sADV, sType, iAccountUnq, sViewLvl)

		Dim rsStoreData, rsStoreData2, sSQLText, dLastChangeDate, bEnd, bSecure
		
		Set rsStoreData = CreateObject("ADODB.Recordset")
		Set rsStoreData2 = CreateObject("ADODB.Recordset")
		ACCNT_ReturnADV = ""
		dLastChangeDate = CDate("01/01/1960")
		bEnd = FALSE
		bSecure = FALSE
		sType = Trim(sType)

		' check to make sure the person logged in has rights (on the current domain) to access this ADVs data
		' check to make sure the system passed in corresponds to the ADVs system
		' see if there is any data in this ADV for this domain, if not:
			' see if this ADV is in a Group
			' see if any of the other ADVs in this ADVs Group has any data
				' use the most recently changed data if more than one ADV in the Group has data
				' make sure whichever one is used has rights that the current user has...or use a different one from a different domain
				' make sure it has the viewlevel passed
				
		sSQLText = "SELECT M.RightsLvl FROM owAccountMap M (NOLOCK), owAccountMap A WHERE M.MapName = '" & sADV & "' AND A.MapName = '" & sADV & "' AND M.GroupUnq = A.GroupUnq AND A.GroupUnq > 0"
		Call DB_getFromDatabase(sSQLText, rsStoreData, "")
		If ( rsStoreData.EOF ) Then
			' since none were found in any Group, look for one not in a Group - on the current domain
			Call DB_closeRecordset(rsStoreData)
			sSQLText = "SELECT RightsLvl FROM owAccountMap WHERE GroupUnq = 0 AND MapName = '" & sADV & "'"
			Call DB_getFromDatabase(sSQLText, rsStoreData, "")
		End If

		While ( NOT rsStoreData.EOF And bEnd = FALSE )
			' loop through all the results to find the first one that the user has the rightslvl (or they are blank), and the newestly changed one and viewlvl
			If ( ACCNT_CheckRights(rsStoreData("RightsLvl")) ) Or ( ( Trim(rsStoreData("RightsLvl")) = "" ) Or ( IsNull(rsStoreData("RightsLvl")) ) ) Then
				bHasADVRights = TRUE
				If ( iAccountUnq = "" ) Then
					sSQLText = "SELECT * FROM owAccountData WHERE MapName = '" & SQLEncode(sADV) & "' AND AccountUnq = -1"
				Else
					sSQLText = "SELECT * FROM owAccountData WHERE MapName = '" & SQLEncode(sADV) & "' AND AccountUnq = " & iAccountUnq
				End If
				Call DB_getFromDatabase(sSQLText, rsStoreData2, "")
				If ( NOT rsStoreData2.EOF ) Then
					If ( sViewLvl = C_OVERRIDE_ADV ) Then	' the developer wants to return the data even if the accountunq's are different AND the user made the data private - this is great for sending msgboard email notifications
						sViewLvl = rsStoreData2("ViewLvl")	' this is populated and can be used in the calling sub or function.
						bSecure = TRUE
					Else
						sViewLvl = rsStoreData2("ViewLvl")	' this is populated and can be used in the calling sub or function.
						If ( Trim(sViewLvl) = C_PRIVATE_ADV ) Then
							If ( CDbl(Trim(iAccountUnq)) = CDbl(iLoginAccountUnq) ) Then
								bSecure = TRUE
							Else
								bSecure = FALSE
							End If
						Else
							bSecure = TRUE
						End If
					End If
					If ( bSecure = TRUE ) Then
						' it's data from the current domain, so just use it and end.
						If ( sType = C_TEXT_ADV ) Then
							ACCNT_ReturnADV = rsStoreData2("TextData")
						ElseIf ( sType = C_VARCHAR_ADV ) Then
							ACCNT_ReturnADV = rsStoreData2("VarCharData")
						End If
						bEnd = TRUE
					End If
				End If
				rsStoreData2.Close
			Else
				bHasADVRights = FALSE
			end if
			rsStoreData.MoveNext
		Wend
		Set rsStoreData	= Nothing
		Set rsStoreData2 = Nothing

	End Function
	'************************************************************************************
	
	
	'************************************************************************************
	'*																					*
	'*	This returns True if the account is in the group passed in.						*
	'*																					*
	'************************************************************************************
	Function ACCNT_InGroup(iAccountUnq, iGroupUnq)
		Dim rsStoreData, sSQLText

		If ( iAccountUnq = "" ) Or ( iAccountUnq = C_NONMEMBER_ID ) Or ( NOT bHasAccount ) Then
			ACCNT_InGroup = False
'response.write "falsehere"
		Else
			If ( iGroupUnq = "" ) Then
				ACCNT_InGroup = False
'response.write "falsehere2"
			Else
				ACCNT_InGroup = Trim(Session("GROUP_" & iGroupUnq & "_" & iAccountUnq))		' check caching
' leaving this here until the problem w/ customer management is cleared up -- where changing the Type and/or Extended Type doesn't change the session
'	-specifically when deleting
Session.Contents.Remove("GROUP_" & iGroupUnq & "_" & iAccountUnq)
'response.write "<BR>a:" & Session("GROUP_" & iGroupUnq & "_" & iAccountUnq) & ":<BR>"
				If ( ACCNT_InGroup = "" ) Then
					Set rsStoreData	= CreateObject("ADODB.Recordset")
					sSQLText = "SELECT AccountUnq FROM owAGroupMap WHERE AccountUnq = " & iAccountUnq & " AND GroupUnq = " & iGroupUnq
					Call DB_getFromDatabase(sSQLText, rsStoreData, "")
					If ( NOT rsStoreData.EOF ) Then
						ACCNT_InGroup = True
					Else
						ACCNT_InGroup = False
'response.write "falsehere3"
					End If
					Session("GROUP_" & iGroupUnq & "_" & iAccountUnq) = ACCNT_InGroup
					Call DB_closeRecordset(rsStoreData)
					Set rsStoreData	= Nothing
				End If
			End If
		End If
		
	End Function
	'************************************************************************************
%>