<html>
<head>
<title>Pedersens Hire Management System</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link type="text/css" rel="stylesheet" href="style.css">
<script language="JavaScript" type="text/javascript" src="../editor/scripts/wysiwyg.js"></script>
<script language="JavaScript" type="text/javascript" src="../editor/scripts/wysiwyg-settings.js"></script>
<script language="javascript1.2">
   WYSIWYG.attach('all', mid);
</script>
<script language="JavaScript" type='text/javascript' src="../js/popUp.js"></script>
<script language="JavaScript" type="text/javascript" src="../js/confirmDelete.js"></script>
<!-- Google analytics -->
<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-83423726-1', 'auto');
  ga('send', 'pageview');

</script>
<!-- end of Google Analytics -->
</head>
<body>
<table width="800" align="center" border="0" cellpadding="0" cellspacing="0">
<!-- #include file="includes/top.asp" -->
	<tr>
		<td width="800" height="400" bgcolor="#FFFFFF" valign="top"><table width="800" border="0" cellpadding="0" cellspacing="0">
			<tr>
				<td width="10"></td>
				<td width="780"><table width="780" border="0" cellpadding="0" cellspacing="0">
<%
On Error Resume Next
Response.Expires = 0
Dim objConn, objRS, strQuery, UobjRS, UstrQuery, UstrQuery2, UstrQuery1, DobjRS, DstrQuery, DstrQuery2, DstrQuery1
Dim strConnection
Dim intLoop, ActualID
Dim tID, tB, tU, tD
Dim count, tRank1, tRank2

if Session("Admin") = "" then
	Response.Write "<tr><td width=""780"" height=""80""></td></tr>"
  	Response.Write "<tr><td width=""780"" height=""25"" align=""center"" class=""h2"">You are not authorized to view this page.</td></tr>"
    Response.Write "<tr><td width=""780"" height=""20""></td></tr>"
    Response.Write "<tr><td width=""780"" height=""25"" align=""center""><input TYPE=""button"" NAME=""Login"" value=""Login"" class=""submit"" ONCLICK=""JavaScript:window.open('index.asp', target='_self');""></td></tr>"
else

  	Set objConn = Server.CreateObject("ADODB.Connection")
	strConnection = "DSN=pedersens;Database=WebDB;"

  	objConn.Open strConnection

	tID = Request.QueryString("id")
	
	if tID = "" then

 	  for intLoop = 1 to Session("TNumber")
		tID = Request.Form("ID" & intLoop)
		tU = Request.Form("U" & intLoop)
		tD = Request.Form("D" & intLoop)
		tB = Request.Form("B" & intLoop) 
  
		if tU <> "" then
			  strQuery = "SELECT * FROM Product WHERE ID = " & tID
			  Set objRS = objConn.Execute(strQuery)
					  
			  UstrQuery = "SELECT * FROM Product WHERE CategoryID = " & objRS("CategoryID") & " ORDER BY Rank DESC"
			  Set UobjRS = objConn.Execute(UstrQuery)
			  
			  if not UobjRS.EOF then
				count = 1
				Do While not UobjRS.EOF
				  if count = 2 then
					tRank2 = UobjRS("Rank")
					UstrQuery2 = "UPDATE Product SET Rank = '" & tRank1 & "' WHERE ID = " & UobjRS("ID")
					objConn.Execute(UstrQuery2)
					UstrQuery1 = "UPDATE Product SET Rank = '" & tRank2 & "' WHERE ID = " & objRS("ID")
					objConn.Execute(UstrQuery1)
					exit do
				  end if
				  if UobjRS("ID") = objRS("ID") then
					  tRank1 = UobjRS("Rank")
					  count = count + 1
				  end if
				  UobjRS.MoveNext
				Loop
			  end if
			  
			  Response.Redirect("productDetailView.asp?ID=" & objRS("CategoryID"))
		End If
	
		if tD <> "" then
			  strQuery = "SELECT * FROM Product WHERE ID = " & tID
			  Set objRS = objConn.Execute(strQuery)
					  
			  DstrQuery = "SELECT * FROM Product WHERE CategoryID = " & objRS("CategoryID") & " ORDER BY Rank"
			  Set DobjRS = objConn.Execute(DstrQuery)
			  
			  if not DobjRS.EOF then
				count = 1
				Do While not DobjRS.EOF
				  if count = 2 then
					tRank2 = DobjRS("Rank")
					DstrQuery2 = "UPDATE Product SET Rank = '" & tRank1 & "' WHERE ID = " & DobjRS("ID")
					objConn.Execute(DstrQuery2)
					DstrQuery1 = "UPDATE Product SET Rank = '" & tRank2 & "' WHERE ID = " & objRS("ID")
					objConn.Execute(DstrQuery1)
					exit do
				  end if
				  if DobjRS("ID") = objRS("ID") then
					  tRank1 = DobjRS("Rank")
					  count = count + 1
				  end if
				  DobjRS.MoveNext
				Loop
			  end if
			  
			  Response.Redirect("productDetailView.asp?ID=" & objRS("CategoryID"))
		End If
	
		if tB <> "" then
		  strQuery = "SELECT * FROM Product WHERE ID = " & tID
		  ActualID = tID
		End If
	
	  Next
	  
	else
		strQuery = "SELECT * FROM Product WHERE ID = " & tID
	end if
	  
  	Set objRS = objConn.Execute(strQuery)	

    CstrQuery = "SELECT * FROM Category ORDER BY Rank"
    Set CobjRS = objConn.Execute(CstrQuery)
%>
    				<tr>
      					<td width="780" height="25" align="center" class="head">Hello <%= Session("Admin") %>!</td>
					</tr>
    				<tr>
      					<td width="780" height="25" align="right"><table height="25" border="0" cellpadding="0" cellspacing="0">
							<tr>
								<td height="25"><a href="javascript:history.back()" class="h2">Back to List</a></td>
								<td width="20" height="25"></td>
								<td height="25"><a href="home.asp" class="h2">Back to Home</a></td>
								<td width="20" height="25"></td>
								<td height="25"><a href="logout.asp" class="h2">Logout</a></td>
							</tr>
						</table></td>
					</tr>
    				<tr>
      					<td width="780" height="10"></td>
					</tr>
    				<tr>
      					<td width="780" height="25" align="center" class="h1">Edit Product</td>
					</tr>
    				<tr>
      					<td width="780" height="10"></td>
					</tr>
					<form name="fmEditProduct" method="post" action="productUpdate.asp">
    				<tr>
      					<td width="780" height="20" align="center"><table border="0" cellpadding="0" cellspacing="0">
                            <tr height="30">
								<td align="right" class="p">Category:</td>
                                <td width="10"></td>
								<td><input type="hidden" name="ID" value="<%= objRS("ID") %>"><select name="category" class="newstext">
<%
    Do While not CobjRS.EOF
      Response.Write "<option value=""" & CobjRS("ID") & """"
		if CobjRS("ID") = objRS("CategoryID") then
			Response.Write " selected"
		end if
	  Response.Write ">" & CobjRS("Name") & "</option>"
      CobjRS.MoveNext
    Loop
%>
    							</select></td>
							</tr>
                            <tr height="30">
								<td align="right" class="p">Name:</td>
                                <td width="10"></td>
								<td><input name="name" type="text" class="newstext" value="<%= objRS("Name")%>"></td>
							</tr>
		    				<tr height="185">
      							<td align="right" valign="top" class="p">Description:</td>
                                <td width="10"></td>
      							<td><textarea id="textarea1" name="description"><%= objRS("Description")%></textarea></td>
    						</tr>
                            <tr height="30">
								<td align="right" class="p">Price $:</td>
                                <td width="10"></td>
								<td><input name="price" type="text" class="newstext" value="<%= objRS("Price")%>"></td>
							</tr>
                            <tr height="30">
								<td align="right" class="p"></td>
                                <td width="10"></td>
								<td align="center"><A HREF="javascript:popUpGroup('productPhotoEdit.asp?pid=<%= objRS("ID") %>')" class="p">Edit Photo</a> &nbsp;&nbsp;&nbsp;&nbsp; <A HREF="javascript:popUpGroup('productRelationEdit.asp?pid=<%= objRS("ID") %>')" class="p">Edit Group</a></td>
							</tr>
                        </table></td>
					</tr>
					<tr>
      					<td width="780" height="10"></td>
    				</tr>
    				<tr>
      					<td width="780" height="25" align="center"><table height="25" border="0" cellpadding="0" cellspacing="0">
							<tr>
		                	    <td width="80"></td>
								<td height="25"><input type="reset" value="Reset" name="reset" class="submit"></td>
								<td width="30" height="25"></td>
								<td height="25"><input name="B1" type="submit" class="submit" value="Update"></td>
								<td width="30" height="25"></td>
								<td height="25"><input type="submit" value="Delete" name="D1" class="submit" onClick="return confirmDelete()"></td>
				    		</tr>
						</table></td>
					</tr>
					</form>
<%
  objRS.close
  objConn.close
  Set objRS = Nothing
  Set objConn = Nothing
end if
%>
				</table></td>
				<td width="10"></td>
			</tr>
		</table></td>
	</tr>
<!-- #include file="includes/bottom.asp" -->
</table>
</body>
</html>