<!--#include virtual="/includes/topofpage.asp"-->
<!--#include file="includes/globalvariables.asp"-->
<%
'Response.Write "Will be here soon! <A HREF=""javascript:history.back()"">Click here to Return</a>"
'Response.End

'  If Not Request.QueryString("ID") = "" Then Session("Group_ID") = Request.QueryString("ID")

Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open Application("conn_string")

Page="64"

If Request("CID") = "" Then 
	Response.Redirect ("contact_select.asp?GID=" & Request("GID") & "&RID=" & Request("RID"))
Else
	Contact_ID = Request("CID")
	Group_ID = Request("GID")
	Realm_ID = Request("RID")
	User_ID = Request("UID")
End If

If LCase(Request("Act")) = "new" Then User_ID = ""

If (UCase(Request.ServerVariables("REQUEST_METHOD")) = "POST" And Request.Form("page") = Page) Then

	SQL = "Update_User "
	If Request("UID") = "" Then
		SQL = SQL & " @User_ID=Null,"
	Else
		SQL = SQL & " @User_ID='" & Replace(Request("UID"), "'", "''") & "',"
	End If

	SQL = SQL & " @Contact_ID = '" & Contact_ID & "', "
	If Not Request("User_PWD") = "" Then
		SQL = SQL & " @User_PWD = '" & Replace(Request("User_PWD"), "'", "''") & "', "
	End If

'		SQL = SQL & " @Start_Page = '" & Replace(Request("Start_Page"), "'", "''") & "', "

	SQL = SQL & " @User_Name = '" & Replace(Request("User_Name"), "'", "''") & "' "
	Set RSUser = Conn.Execute(SQL)
	
	User_ID = RSUser("User_ID")
	
	If isNull(User_ID) Then
		Response.Write "Problem updating the user.. please contact support@emagineering.com."
		Response.End
	Else
	
		SQL = "Delete_User_Realm_Map @User_ID='" & User_ID & "', @Realm_ID='%'"
		Conn.Execute(SQL)
		For each Realm_ID in Request("Realms")
			If Not Trim(Realm_ID) = "" Then
				SQL = "Add_User_Realm_Map @User_ID='" & User_ID & "', @Realm_ID='" & Realm_ID &"'"
				Conn.Execute(SQL)
			End If
		Next
		
	End If
	
	Response.Redirect ("user_admin.asp?GID=" & Request("GID") & "&RID=" & Request("RID") & "&CID=" & Request("CID"))
	
End If

Function Get_Realms_By_Group_DD (Group_ID, Sel_ID, Exclude_ID)
   SQL = "Get_Realms_By_Group @Group_ID='" & Group_ID & "'"
   If Not Trim(Exclude_ID) = "" Then SQL = SQL & ", @Exclude_Type_ID='" & Exclude_ID & "'"
   HTML =	""
'Response.Write SQL
	Set Realms = Conn.Execute(SQL)
	Do While Not Realms.EOF
		ID = Realms("Realm_ID")
		HTML = HTML & "<option value=""" & ID & """"
		On Error Resume Next
		For each Sel in Request(Sel_ID)
			If ID = Int(Sel) Then HTML = HTML & " selected"
			If ID = Sel Then HTML = HTML & " selected"
		Next
		On Error Goto 0
		HTML = HTML & ">" & Realms("Realm_Name") & "</option>" & Chr(13)
		Realms.MoveNext
	Loop			

   Set Realm = Nothing
   Get_Realms_By_Group_DD = HTML
End Function

Function Get_Realms_By_User_Group_DD (User_ID, Group_ID, Sel_ID, Exclude_ID)
   SQL = "Get_Realms_By_Group @Group_ID='" & Group_ID & "'"
Response.Write SQL
   If Not Trim(Exclude_ID) = "" Then SQL = SQL & ", @Exclude_Type_ID='" & Exclude_ID & "'"
   Set Realms = Conn.Execute(SQL)

   SQLUser = "Get_User_Realms @User_ID='" & User_ID & "'"
   Set Users = Conn.Execute(SQLUser)

   HTML =	""

   Do While Not Realms.EOF
	ID = Realms("Realm_ID")
	HTML = HTML & "<option value=""" & ID & """"
	
	If Not Users.EOF Then UserClear = True	
	Do While Not Users.EOF
		Response.Write Users("Realm_ID")
		If CStr(Users("Realm_ID")) = CStr(Realms("Realm_ID")) Then HTML = HTML & " selected"
		Users.MoveNext
	Loop
	If UserClear = True Then Users.MoveFirst

	For each Sel in Request(Sel_ID)
		If CStr(ID) = CStr(Sel) Then HTML = HTML & " selected"
	Next

	HTML = HTML & ">" & Realms("Realm_Name") & "</option>" & Chr(13)
	Realms.MoveNext
   Loop			

   Set Realm = Nothing
   Get_Realms_By_User_Group_DD = HTML
End Function

SQL = "Get_Contact @Contact_ID='" & Contact_ID & "'"
Set RS = Conn.Execute(SQL)
Contact_Name = RS("Name_First") & " " & RS("Name_Last")

If Not User_ID = "" Then
	Edit = True
	SQL = "Get_User @User_ID='" & User_ID & "'"
	Set RS = Conn.Execute(SQL)
	User_Name = RS("User_Name")
End If

%>


<html>
<STYLE>
BODY {SCROLLBAR-FACE-COLOR: #5e5ebe; SCROLLBAR-HIGHLIGHT-COLOR: #5e5ebe; SCROLLBAR-SHADOW-COLOR: #5e5ebe; SCROLLBAR-3DLIGHT-COLOR: #5e5ebe; SCROLLBAR-ARROW-COLOR:  #6666CC; SCROLLBAR-TRACK-COLOR: #E3E3E3; SCROLLBAR-DARKSHADOW-COLOR: #666666; }
</STYLE>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">






<SCRIPT ID=clientEventHandlersJS LANGUAGE=javascript>
<!--

function SubmitForm() {
var errmsg='';
if (form1.Name_First.value=='') errmsg=errmsg+'Must Specify a First Name\n';
if (form1.Name_Last.value=='') errmsg=errmsg+'Must Specify a Last Name\n';
if (form1.Address_1.value=='') errmsg=errmsg+'Must Specify An Address\n';
if (form1.City.value=='') errmsg=errmsg+'Must Specify a City\n';
if (form1.State.value=='') errmsg=errmsg+'Must Specify a State\n';
if (form1.Country.value=='') errmsg=errmsg+'Must Specify a Country\n';
if (form1.Zip.value=='') errmsg=errmsg+'Must Specify a Zip Code\n';
if (form1.Email_Home.value=='' && form1.Email_Work.value=='' && form1.Email_Other.value=='') errmsg=errmsg+'Must Specify At Least One Email Address\n';

if (form1.createuser.checked) {
  if (!form1.User_PWD.value == form1.User_PWD_2.value) errmsg=errmsg+'Passwords do not match. Please re-type your password\n'
  if (form1.User_PWD.value == '') errmsg += 'Must specify a Password\n'
  if (form1.User_Name.value == '') errmsg += 'Must choose a User Name\n'
  if (!document.form1.Key_Name[0].checked && !document.form1.Key_Name[1].checked && !document.form1.Key_Name[2].checked) errmsg += 'Must select an access type (i.e., Administrator)\n'
}

if (errmsg) alert(errmsg);
else form1.submit();
}


function createuser() {
	var errmsg='';
	
	if (errmsg) alert(errmsg);
	else {
		form1.action="";
		form1.cont.value='1';
		form1.submit();
	}

}

function updateuser() {
	var errmsg='';
	
	if (errmsg) alert(errmsg);
	else {
		form1.action="";
		form1.cont.value='1';
		form1.submit();
	}

}


//-->
</SCRIPT>

<link rel="stylesheet" href="/Style-sheets/standard-input-form.css" type="text/css">
<link rel="stylesheet" href="/Style-sheets/headline.css" type="text/css">
<link rel="stylesheet" href="/Style-sheets/galley-text-indent.css" type="text/css">
<link rel="stylesheet" href="/Style-sheets/Lodging-display.css" type="text/css">
</head>
<body bgcolor="#ffffff" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<FORM action="" method=post id=form1 name=form1>
<input type="hidden" name="page" value="<%=Page%>">
<input type="hidden" name="cont" value="">
<input type="hidden" name="cid" value="<%=Request("cid")%>">
<input type="hidden" name="rid" value="<%=Request("rid")%>">
<input type="hidden" name="gid" value="<%=Request("gid")%>">
<input type="hidden" name="uid" value="<%=User_ID%>">
<Br>  
<table border="0" cellspacing="0" cellpadding="0" width="590" style="WIDTH: 290px" align="center">
  <tr>
	<td width="100%"></td>
  </tr>
  <tr> 
    <td colspan="2" valign="top" align="center" bgcolor="#77aadd"><font color="#e7e7e7">Create a new User</font></td>
  </tr>
  <tr>
	<td bgcolor="#e7e7e7" colspan="2"><img src="/images/shim.gif" height="1" width="1"></td>
  </tr>
  <tr>
	<td align="right" valign="top" bgcolor="#e7e7e7"><br><div class="manage-menu">
<div align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Contact Record:</div>
<div align="center"><font color="#000000"><%=Contact_Name%></font>&nbsp;</div>
<br>
User Name: <input type="text" name="user_name" value="<%=User_Name%>" style="">&nbsp;<br>
Password: <input type="text" name="user_pwd" value="" style="">&nbsp;<br>
<!--Start Page: <input type="text" name="start_page" value="<%=Start_Page%>" style="">&nbsp; (i.e. /directory)&nbsp;<br>-->
</div><br><div align="center" class="manage-menu">
This user is active in:<br><br>
<select name="realms" SIZE="5" MULTIPLE>
<% If Edit = True Then %>

<% Else %>
	<option value="" selected>User is not Active</option>
<% End If %>

	<%= Get_Realms_By_User_Group_DD(User_ID, Group_ID, Request("Realms"), "3")%>	
</select>
</div><br>
	</td>
  </tr>
  <tr>
	<td align="center" valign="top" bgcolor="#cccccc"><div valign="top" align="center">
		<a href="user_admin.asp<%="?GID=" & Request("GID") & "&RID=" & Request("RID") & "&CID=" & Request("CID")%>"><font face="Verdana, Arial, Helvetica, sans-serif" size="1"><b><font color="#000099">Return to Management</font></b></font></a>&nbsp;&nbsp;&nbsp;&nbsp;
		<% If Edit = True Then %>
			<a href="javascript:updateuser();"><font face="Verdana, Arial, Helvetica, sans-serif" size="1"><b><font color="#000099">Update Record</font></b></font></a>&nbsp;
		<% Else %>
			<a href="javascript:createuser();"><font face="Verdana, Arial, Helvetica, sans-serif" size="1"><b><font color="#000099">Create this User</font></b></font></a>&nbsp;
		<% End If %>
		</div></td>
  </tr>
 </table>
</FORM>
</body>
</html>
