<%
	On Error Resume Next
	Response.Expires = 0
	Dim objConn, objRS, strQuery
	Dim strConnection
	Dim tDateFrom, tDateTo, tOrder
	Dim tFrom, tTo
	Dim DFobjRS, DFstrQuery, DTobjRS, DTstrQuery
	Dim tDD, tMM, tYYYY, tDate
	Dim tQuantity
	
    Set objConn = Server.CreateObject("ADODB.Connection")
    strConnection = "DSN=pedersens;Database=WebDB;"
  
    objConn.Open strConnection
  
	tDateFrom = Request.QueryString("dateFrom")
	tDateTo = Request.QueryString("dateTo")
	tOrder = Request.QueryString("order")
	
	if tDateFrom = "" then
		DFstrQuery = "SELECT OrderDate FROM OrderMaster ORDER BY OrderDate ASC"
    	Set DFobjRS = objConn.Execute(DFstrQuery)
		if DFobjRS.EOF then
			tFrom = ""
		else
			tFrom = DFobjRS("OrderDate")
			tCFDD = Day(tFrom)
			tCFMM = Month(tFrom)
			tCFYYYY = Year(tFrom)
			if tCFDD < 10 then 
				tCFDD = "0" & CStr(tCFDD)
			end if
			if tCFMM < 10 then 
				tCFMM = "0" & CStr(tCFMM)
			end if
			tFrom = tCFDD & "/" & tCFMM & "/" & tCFYYYY
		end if
	else
		tFrom = Day(tDateFrom) & "/" & Month(tDateFrom) & "/" & Year(tDateFrom)
	end if
		
	if tDateTo = "" then
		DTstrQuery = "SELECT OrderDate FROM OrderMaster ORDER BY OrderDate DESC"
    	Set DTobjRS = objConn.Execute(DTstrQuery)
		if DTobjRS.EOF then
			tTo = ""
		else
			tTo = DTobjRS("OrderDate")
			tCTDD = Day(tTo)
			tCTMM = Month(tTo)
			tCTYYYY = Year(tTo)
			if tCTDD < 10 then 
				tCTDD = "0" & CStr(tCTDD)
			end if
			if tCTMM < 10 then 
				tCTMM = "0" & CStr(tCTMM)
			end if
			tTo = tCTDD & "/" & tCTMM & "/" & tCTYYYY
		end if
	else
		tTo = Day(tDateTo) & "/" & Month(tDateTo) & "/" & Year(tDateTo)
	end if
  
	strQuery = "SELECT * FROM OrderMaster WHERE (OrderDate BETWEEN #" & tFrom & "# AND #" & tTo & "#) AND TotalAmount > 0"

	if not tOrder = "" then
		strQuery = strQuery & " AND ID = " & tOrder
	end if
	
	strQuery = strQuery & " ORDER BY ID"

    Set objRS = objConn.Execute(strQuery)
    
	Response.ContentType = "application/vnd.ms-excel"
	Response.AddHeader "Content-Disposition", "attachment; filename=reportQuote.xls" 
  
    if not objRS.EOF then
%>
			<tr>
				<td><table border="2" bordercolor="CCCCCC" cellpadding="0" cellspacing="0">
			   		<tr height="25">
						<td class="h2">Quote ID</td>
						<td class="h2">Quote Date</td>
						<td class="h2">Customer</td>
						<td class="h2">Email</td>
						<td class="h2">Phone</td>
						<td class="h2">Total</td>
      				</tr>
<%
		tQuantity = 0

        Do While not objRS.EOF
		
		  tDD = Day(objRS("OrderDate"))
		  tMM = Month(objRS("OrderDate"))
		  tYYYY = Year(objRS("OrderDate"))
		  if tDD < 10 then 
			tDD = "0" & CStr(tDD)
		  end if
		  if tMM < 10 then 
			tMM = "0" & CStr(tMM)
		  end if
		  tDate = tDD & "/" & tMM & "/" & tYYYY
		  
          	Response.Write "<tr height=""25"">"
		  	Response.Write "<td class=""p"">" & objRS("ID") & "&nbsp;</td>"
          	Response.Write "<td class=""p"">" & tDate & "&nbsp;</td>"
          	Response.Write "<td class=""p"">" & objRS("FirstName") & " " & objRS("LastName") & "&nbsp;</td>"
          	Response.Write "<td class=""p"">" & objRS("Email") & "&nbsp;</td>"
          	Response.Write "<td class=""p"">" & objRS("Phone") & "&nbsp;</td>"
          	Response.Write "<td class=""p"">" & FormatCurrency(objRS("TotalAmount"),2) & "&nbsp;</td>"
		  	Response.Write "</tr>"
    
          	tQuantity = tQuantity + 1
          	objRS.MoveNext
        Loop
%>
				</table></td>
            </tr>
			<tr>
				<td HEIGHT="10"></td>
			</tr>
			<tr>
				<td height="25" align="center" class="h2">Number of Quotes: <%= tQuantity %></td>
			</tr>
<%
    end if
	
	objRS.close
    objConn.close
    Set objRS = Nothing
    Set objConn = Nothing
%>