% Option Explicit%>
<%
Call HandleCookies()
'Call WriteLog()
Dim strPostcode, fltLat, fltLong
Dim bSuccess
g_intCurrentTab = 3
Dim objRS, objConn, objPostcodeConn, strSQL, objrsResults
Dim intRadius, fltMapClickedLong
Dim intNumResults, intChoice
Dim bShowCircle, bHasSearched
Dim intCurrentPage
Dim aryResults, intPageCount
Dim intFaithID, intDenominationID, intGroupID
Dim aryGroups(50, 1)
Dim intNumGroups
Dim aryFaiths(8)
Dim aryDenominations(8)
Dim bRoom
Dim bKitchen
Dim bToilets
Dim bAccess
Dim bParking
Call PopulateFaithData()
bShowCircle = False
Set objRS = Server.CreateObject("ADODB.RecordSet")
Set objConn = Server.CreateObject("ADODB.Connection")
Set objPostcodeConn = Server.CreateObject("ADODB.Connection")
objConn.Open("DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" & Server.Mappath("Faith Resources.mdb") & ";")
objPostcodeConn.Open("DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" & Server.Mappath("postcodes.mdb") & ";")
strPostcode = FormatPostcode(Request("postcode"))
bHasSearched = (Request("submit") = "Search")
If Request("Page") <> "" Then bHasSearched = True
intCurrentPage = CInt(GetNumericFormData("Page", 1))
intChoice = CInt(GetNumericFormData("Choice", 2))
intRadius = CInt(GetNumericFormData("radius", 10))
fltLat = CSng(GetNumericFormData("lat", 52.296))
fltLong = CSng(GetNumericFormData("long", -1.53))
intFaithID = CInt(GetNumericFormData("faith", -1))
intDenominationID = CInt(GetNumericFormData("denomination", -1))
intGroupID = CInt(GetNumericFormData("group", -1))
bRoom = (Request("room") = "on")
bKitchen = (Request("kitchen") = "on")
bToilets = (Request("toilets") = "on")
bAccess = (Request("access") = "on")
bParking = (Request("parking") = "on")
' ------------------------------------------------------------------------------------
Const RADIAN_MULTIPLIER = 57.2957795130823
Const HALF_PI = 1.57079632679489661923
CONST PI = 3.14159265
Sub PopulateFaithData()
aryFaiths(0) = "-1|Don't Mind"
aryDenominations(0) = "-1|Don't Mind"
aryFaiths(1) = "5|Bahá'í"
aryDenominations(1) = "-1|Don't Mind"
aryFaiths(2) = "6|Buddhist"
aryDenominations(2) = "-1|Don't Mind"
aryFaiths(3) = "1|Christian"
aryDenominations(3) = "-1|Don't Mind"
aryDenominations(3) = aryDenominations(3) & "|3|Anglican"
aryDenominations(3) = aryDenominations(3) & "|8|Baptist"
aryDenominations(3) = aryDenominations(3) & "|16|Christian Science"
aryDenominations(3) = aryDenominations(3) & "|1|Church of England"
aryDenominations(3) = aryDenominations(3) & "|6|Congregational"
aryDenominations(3) = aryDenominations(3) & "|9|Independent Evangelical"
aryDenominations(3) = aryDenominations(3) & "|7|Methodist"
aryDenominations(3) = aryDenominations(3) & "|12|Pentecostal"
aryDenominations(3) = aryDenominations(3) & "|4|Roman Catholic"
aryDenominations(3) = aryDenominations(3) & "|11|Seventh-Day Adventist"
aryDenominations(3) = aryDenominations(3) & "|15|Spiritualist"
aryDenominations(3) = aryDenominations(3) & "|10|United Reformed Church"
aryFaiths(4) = "6|Buddhist"
aryDenominations(4) = "-1|Don't Mind"
aryFaiths(5) = "3|Hindu"
aryDenominations(5) = "-1|Don't Mind"
aryFaiths(6) = "2|Moslem"
aryDenominations(6) = "-1|Don't Mind"
aryDenominations(6) = aryDenominations(6) & "|14|Sunni"
aryFaiths(7) = "4|Quaker"
aryDenominations(7) = "-1|Don't Mind"
aryFaiths(8) = "7|Sikh"
aryDenominations(8) = "-1|Don't Mind"
End Sub
Sub OutputFaithDataJS()
Dim i, j, arySplit
Response.Write("var faiths = new Array();" & VbCrLf)
Response.Write("var denominations = new Array();" & VbCrLf)
For i=0 To UBound(aryFaiths)
arySplit = split(aryFaiths(i), "|")
Response.Write("faiths.push(new Array(" & arySplit(0) & ", """ & arySplit(1) & """));" & VbCrLf)
arySplit = split(aryDenominations(i), "|")
Response.Write("denominations.push(new Array(")
For j=0 to UBound(arySplit) Step 2
If j>1 Then Response.Write(", ")
Response.Write(arySplit(j) & ", """ & arySplit(j+1) & """")
Next
Response.Write("));" & VbCrLf)
Next
End Sub
Sub OutputCheckbox(strName, bChecked)
Response.Write("")
End Sub
Function GetNumericFormData(strName, intDefault)
If Request(strName) <> "" Then
If IsNumeric(Request(strName)) Then
GetNumericFormData = Request(strName)
Else
GetNumericFormData = intDefault
End If
Else
GetNumericFormData = intDefault
End If
End Function
Function FormatPostcode(strPostcode)
Dim i, strChar
For i=1 To Len(strPostcode)
strChar = UCase(Mid(strPostcode, i,1))
If (strChar >="A" And strChar <="Z") OR (strChar >="0" And strChar <="9") Then
FormatPostcode = FormatPostcode + strChar
End If
Next
End Function
Function GetPostcodeFromLatLong(strPostcode, fltLat, fltLong)
' fltLat, fltLong contains LatLong for desired postcode
' Returns True for success
' strPostcode will contain closest postcode
Dim objXMLHTTP, strHTML, intStart, intEnd
GetPostcodeFromLatLong = False
On Error Resume Next
Set objXMLHTTP = Server.CreateObject("Microsoft.XMLHTTP")
objXMLHTTP.Open "GET", "http://www.streetmap.co.uk/streetmap.dll?GridConvert?name=" & fltLat & "," & fltLong & "&type=LatLong", False, "", ""
objXMLHTTP.Send
strHTML = objXMLHTTP.ResponseText
Set objXMLHTTP = Nothing
intStart = Instr(strHTML, "Post Code")
If intStart = 0 Then Exit Function
intStart = Instr(intStart, strHTML, "
")
If intStart = 0 Then Exit Function
intEnd = Instr(intStart, strHTML, "
")
If intEnd = 0 Then Exit Function
If intStart> 0 And intEnd>intStart+5 Then
strPostcode = Mid(strHTML, intStart+1, intEnd-(intStart+1))
End If
End Function
Function GetLatLongFromPostcode(strPostcode, fltLat, fltLong)
' strPostcode, eg. CV32 or CV32 7QG
' Returns True for success
' fltLat, fltLong will contain LatLong for supplied postcode if successful
Dim objXMLHTTP, strHTML, intStart, intEnd, arySplit
GetLatLongFromPostcode = False
If Trim(strPostcode) = "" Then Exit Function
On Error Resume Next
Set objXMLHTTP = Server.CreateObject("Microsoft.XMLHTTP")
If g_IsLive Then
objXMLHTTP.Open "GET", "http://maps.google.com/maps/geo?q=" & strPostcode & "&output=csv&key=ABQIAAAA_eOPojEmVJ3WWpbHWvHcDxSv9yBxQiPMABp8Px_eQiv5DgFmNxQVkGjdgTMETajllIeTH6buhbxpzw", False, "", ""
Else
objXMLHTTP.Open "GET", "http://maps.google.com/maps/geo?q=" & strPostcode & "&output=csv&key=ABQIAAAA_eOPojEmVJ3WWpbHWvHcDxT2yXp_ZAY8_ufC3CFXhHIE1NvwkxR8_VAnNLkardDUM9aaGWAoAFLSww", False, "", ""
End If
objXMLHTTP.Send
strHTML = objXMLHTTP.ResponseText
Set objXMLHTTP = Nothing
If strHTML <> "" Then
arySplit = Split(strHTML, ",")
If UBound(arySplit) = 3 Then
If arySplit(0) = 200 Then 'Success
fltLat = arySplit(2)
fltLong = arySplit(3)
GetLatLongFromPostcode = True
End If
End If
End If
End Function
Function OLD_GetLatLongFromPostcode(strPostcode, fltLat, fltLong)
' strPostcode, eg. CV32 or CV32 7QG
' Returns True for success
' fltLat, fltLong will contain LatLong for supplied postcode if successful
Dim objXMLHTTP, strHTML, intStart, intEnd
GetLatLongFromPostcode = False
On Error Resume Next
Set objXMLHTTP = Server.CreateObject("Microsoft.XMLHTTP")
objXMLHTTP.Open "GET", "http://www.streetmap.co.uk/streetmap.dll?MfcISAPICommand=GridConvert&name=" & strPostcode & "&type=PostCode", False, "", ""
objXMLHTTP.Send
strHTML = objXMLHTTP.ResponseText
Set objXMLHTTP = Nothing
intStart = Instr(strHTML, "Post Code")
If intStart = 0 Then Exit Function
intStart = Instr(intStart, strHTML, "Lat")
If intStart = 0 Then Exit Function
intStart = Instr(intStart, strHTML, "
")
If intStart = 0 Then Exit Function
intStart = Instr(intStart, strHTML, "(") + 1
If intStart = 0 Then Exit Function
intEnd = Instr(intStart, strHTML, ")") - 1
If intEnd = 0 Then Exit Function
If intStart> 0 And intEnd>intStart+5 Then
fltLat = Mid(strHTML, intStart+1, intEnd-(intStart+1))
Else
Exit Function
End If
intStart = Instr(intStart, strHTML, "Long")
If intStart = 0 Then Exit Function
intStart = Instr(intStart, strHTML, "
")
If intStart = 0 Then Exit Function
intStart = Instr(intStart, strHTML, "(") + 1
If intStart = 0 Then Exit Function
intEnd = Instr(intStart, strHTML, ")") - 1
If intEnd = 0 Then Exit Function
If intStart> 0 And intEnd>intStart+5 Then
fltLong = Mid(strHTML, intStart+1, intEnd-(intStart+1))
Else
Exit Function
End If
Set objXMLHTTP = Nothing
GetLatLongFromPostcode = True
End Function
Sub GetDistanceRS(fltLat, fltLong, intRadius)
Dim fltLongDist, fltLatDist, strSQL
Dim fltLat1, fltLong1
Dim fltLat2, fltLong2
Dim fltDistance, i, j, bFound
' Compute the square of lat/longs ----------------------------------------
fltLongDist = intRadius / (69.1 * cos(fltLat/RADIAN_MULTIPLIER))
fltLatDist = intRadius/70
fltLat1 = fltLat - fltLatDist
fltLat2 = fltLat + fltLatDist
fltLong1 = fltLong - fltLongDist
fltLong2 = fltLong + fltLongDist
' ----------------------------------------------------------------------
' Find all venues within our square of lat/longs ------------------------
If intGroupID <> -1 Then
strSQL = "SELECT DISTINCT Venues.ID, Venues.Name, Venues.Latitude, Venues.Longitude, Towns.Name AS Town, Venues.Address1, Venues.Address2, Venues.Postcode, Venues.Leader "
strSQL = strSQL & "FROM (Venues INNER JOIN Towns ON Venues.TownID = Towns.ID) INNER JOIN JoinActivity ON Venues.ID = JoinActivity.VenueID WHERE"
Else
strSQL = "SELECT Venues.ID, Venues.Name, Venues.Latitude, Venues.Longitude, Towns.Name AS Town, Address1, Address2, Postcode, Leader "
strSQL = strSQL & "FROM Towns INNER JOIN Venues ON Towns.ID = Venues.TownID WHERE"
End If
If intFaithID <> -1 Then
strSQL = strSQL & " FaithID = " & intFaithID & " AND"
If intDenominationID <> -1 Then
strSQL = strSQL & " DenominationID = " & intDenominationID & " AND"
End If
End If
strSQL = strSQL & " Latitude>=" & fltLat1 & " AND Latitude<=" & fltLat2
strSQL = strSQL & " AND Longitude>=" & fltLong1 & " AND Longitude<=" & fltLong2
If intGroupID <> -1 Then
strSQL = strSQL & " AND JoinActivity.ActivityID = " & intGroupID
End If
If bRoom Then strSQL = strSQL & " AND HasRooms = 1"
If bKitchen Then strSQL = strSQL & " AND HasKitchen = 1"
If bToilets Then strSQL = strSQL & " AND HasToilets = 1"
If bAccess Then strSQL = strSQL & " AND HasAccessibilty = 1"
If bParking Then strSQL = strSQL & " AND HasParking = 1"
'Response.Write(strSQL)
ReDim aryResults(200, 9)
intNumResults = 0
objRS.Open strSQL, objConn, 0, 1
Do While Not objRS.EOF
fltDistance = FormatNumber(Distance(fltLat, fltLong, objRS("Latitude"), objRS("Longitude")), 1)
If fltDistance*10 <= intRadius*10 Then
If intNumResults = 0 Then
aryResults(intNumResults, 0) = GetRSValue(objRS,"ID")
aryResults(intNumResults, 1) = GetRSValue(objRS,"Name")
aryResults(intNumResults, 2) = GetRSValue(objRS,"Town")
aryResults(intNumResults, 3) = GetRSValue(objRS,"Address1")
aryResults(intNumResults, 4) = GetRSValue(objRS,"Address2")
aryResults(intNumResults, 5) = GetRSValue(objRS,"Postcode")
aryResults(intNumResults, 6) = GetRSValue(objRS,"Leader")
aryResults(intNumResults, 7) = objRS("Latitude")
aryResults(intNumResults, 8) = objRS("Longitude")
aryResults(intNumResults, 9) = fltDistance
Else
' Sort as we build
bFound = False
For i=0 To intNumResults-1
If CSng(fltDistance) <= CSng(aryResults(i, 9)) Then
' Shift to make space
For j=intNumResults-1 To i Step -1
aryResults(j+1, 0) = aryResults(j, 0)
aryResults(j+1, 1) = aryResults(j, 1)
aryResults(j+1, 2) = aryResults(j, 2)
aryResults(j+1, 3) = aryResults(j, 3)
aryResults(j+1, 4) = aryResults(j, 4)
aryResults(j+1, 5) = aryResults(j, 5)
aryResults(j+1, 6) = aryResults(j, 6)
aryResults(j+1, 7) = aryResults(j, 7)
aryResults(j+1, 8) = aryResults(j, 8)
aryResults(j+1, 9) = aryResults(j, 9)
Next
bFound = True
aryResults(i, 0) = GetRSValue(objRS,"ID")
aryResults(i, 1) = GetRSValue(objRS,"Name")
aryResults(i, 2) = GetRSValue(objRS,"Town")
aryResults(i, 3) = GetRSValue(objRS,"Address1")
aryResults(i, 4) = GetRSValue(objRS,"Address2")
aryResults(i, 5) = GetRSValue(objRS,"Postcode")
aryResults(i, 6) = GetRSValue(objRS,"Leader")
aryResults(i, 7) = objRS("Latitude")
aryResults(i, 8) = objRS("Longitude")
aryResults(i, 9) = fltDistance
Exit For
End If
Next
If Not bFound Then
' Put it on the end of the list
aryResults(intNumResults, 0) = GetRSValue(objRS,"ID")
aryResults(intNumResults, 1) = GetRSValue(objRS,"Name")
aryResults(intNumResults, 2) = GetRSValue(objRS,"Town")
aryResults(intNumResults, 3) = GetRSValue(objRS,"Address1")
aryResults(intNumResults, 4) = GetRSValue(objRS,"Address2")
aryResults(intNumResults, 5) = GetRSValue(objRS,"Postcode")
aryResults(intNumResults, 6) = GetRSValue(objRS,"Leader")
aryResults(intNumResults, 7) = objRS("Latitude")
aryResults(intNumResults, 8) = objRS("Longitude")
aryResults(intNumResults, 9) = fltDistance
End If
End If
intNumResults = intNumResults + 1
End If
objRS.MoveNext
Loop
intPageCount = intNumResults\10
If intNumResults Mod 10 <> 0 Then intPageCount=intPageCount+1
objRS.Close
End Sub
Function Distance(fltLat1, fltLon1, fltLat2, fltLon2)
' Distance = Sin(DegToRad(fltLat1)) * Sin(DegToRad(fltLat2)) + Cos(DegToRad(fltLat1)) * Cos(DegToRad(fltLat2)) * Cos(DegToRad(fltLon1 - fltLon2))
Distance = Sin(fltLat1/RADIAN_MULTIPLIER) * Sin(fltLat2/RADIAN_MULTIPLIER) + Cos(fltLat1/RADIAN_MULTIPLIER) * Cos(fltLat2/RADIAN_MULTIPLIER) * Cos((fltLon1 - fltLon2)/RADIAN_MULTIPLIER)
Distance = (ACos(Distance)*RADIAN_MULTIPLIER) * 69.1
End Function
Function ACos(fltRadians)
If abs(fltRadians) <> 1 Then
ACos = HALF_PI - Atn(fltRadians / Sqr(1 - fltRadians * fltRadians))
ElseIf fltRadians = -1 Then
ACos = PI
End If
End function
Function calcAngle(x1, y1, x2, y2)
Dim dx, dy
dx = x2-x1
dy = y2-y1
calcAngle = 0.0
' Calculate angle
If dx = 0.0 Then
If dy = 0.0 Then
calcAngle = 0.0
ElseIf dy > 0.0 Then
calcAngle = PI / 2.0
Else
calcAngle = PI * 3.0 / 2.0
End If
ElseIf dy = 0.0 Then
If dx > 0.0 Then
calcAngle = 0.0
Else
calcAngle = PI
End If
Else
If dx < 0.0 Then
calcAngle = atn(dy/dx) + PI
ElseIf dy < 0.0 Then
calcAngle = atn(dy/dx) + (2*PI)
Else
calcAngle = atn(dy/dx)
End If
End If
' Convert to degrees
calcAngle = calcAngle * 180 / PI
End Function
Function GetDirectionImage(x1, y1, x2, y2)
Dim fltAngle
fltAngle = calcAngle(x1, y1, x2, y2)
If fltAngle < 15 Or fltAngle > 345 Then
GetDirectionImage = "E"
ElseIf fltAngle >= 15 And fltAngle < 75 Then
GetDirectionImage = "NE"
ElseIf fltAngle >= 75 And fltAngle < 105 Then
GetDirectionImage = "N"
ElseIf fltAngle >= 105 And fltAngle < 165 Then
GetDirectionImage = "NW"
ElseIf fltAngle >= 165 And fltAngle < 195 Then
GetDirectionImage = "W"
ElseIf fltAngle >= 195 And fltAngle < 255 Then
GetDirectionImage = "SW"
ElseIf fltAngle >= 255 And fltAngle < 285 Then
GetDirectionImage = "S"
ElseIf fltAngle >= 285 And fltAngle <= 345 Then
GetDirectionImage = "SE"
End If
GetDirectionImage = "images/dir/" & GetDirectionImage & ".gif"
End Function
Function GetHREF(intPage)
GetHREF = "search.asp?page=" & intPage
GetHREF = GetHREF & "&postcode=" & strPostcode
GetHREF = GetHREF & "&choice=" & intChoice
GetHREF = GetHREF & "&radius=" & intRadius
GetHREF = GetHREF & "&lat=" & fltLat
GetHREF = GetHREF & "&long=" & fltLong
GetHREF = GetHREF & "&faith=" & intFaithID
GetHREF = GetHREF & "&denomination=" & intDenominationID
GetHREF = GetHREF & "&group=" & intGroupID
End Function
Function GetChosenFaith()
Dim i, intFaithIndex, arySplit
intFaithIndex = -1
For i=0 To UBound(aryFaiths)
arySplit = Split(aryFaiths(i), "|")
If intFaithID = CInt(arySplit(0)) Then
GetChosenFaith = arySplit(1)
intFaithIndex = i
Exit For
End If
Next
If intDenominationID <> -1 And intFaithIndex <> -1 Then
arySplit = Split( aryDenominations(intFaithIndex), "|")
For i=0 To UBound(arySplit) Step 2
If intDenominationID = CInt(arySplit(i)) Then
GetChosenFaith = GetChosenFaith & "(" & arySplit(i+1) & ")"
Exit For
End If
Next
End If
End Function
Function GetChosenGroup()
Dim i
For i=0 to intNumGroups
If intGroupID = CInt(aryGroups(i,0)) Then
GetChosenGroup = aryGroups(i,1)
Exit For
End If
Next
End Function
Sub OutputResults()
Dim strAddress, i, strHREF
Dim intIndex, strGroup, strFacilities, aryFacilities
Call GetDistanceRS(fltLat, fltLong, intRadius)
If intNumResults=0 Then
Response.Write("
We couldn't find any venues that matched your search requirements. Please try again.
")
Else
Response.Write("
There ")
If intNumResults=1 Then
Response.Write("is just ")
Else
Response.Write("are a total of ")
End If
Response.Write(intNumResults)
If intFaithID <> -1 Then
Response.Write(" " & GetChosenFaith() & "")
End If
If intNumResults=1 Then
Response.Write(" venue ")
Else
Response.Write(" venues ")
End If
Response.Write("within " & intRadius & " miles of ")
If intChoice = 1 Then
Response.Write("" & strPostcode & "")
ElseIf intChoice = 2 Then
Response.Write("your marker")
End If
strFacilities = ""
If bRoom Or bKitchen Or bToilets Or bAccess Or bParking Then
Response.Write(" that have ")
If bRoom Then
strFacilities = strFacilities & "room(s) for hire|"
End If
If bKitchen Then
strFacilities = strFacilities & "kitchen facilities|"
End If
If bToilets Then
strFacilities = strFacilities & "toilets|"
End If
If bAccess Then
strFacilities = strFacilities & "disabled access|"
End If
If bParking Then
strFacilities = strFacilities & "parking|"
End If
strFacilities = Left(strFacilities, Len(strFacilities)-1)
aryFacilities = Split(strFacilities, "|")
For i=0 To UBound(aryFacilities)
If i>0 And i0 Then
Response.Write(" and ")
End If
Response.Write("" & aryFacilities(i) & "")
Next
End If
If intGroupID <> -1 Then
strGroup = GetChosenGroup()
If intNumResults=1 Then
Response.Write(" that runs ")
Else
Response.Write(" that run ")
End If
If LCase(Left(strGroup, 1)) = "a" Then
Response.Write("an")
Else
Response.Write("a")
End If
Response.Write(" " & strGroup & " group")
End If
Response.Write(".
")
If intNumResults>10 Then
' Page navigation -----------------------------------------------------------------
Response.Write("
")
If intCurrentPage > 1 Then
Response.Write("Previous ")
Else
Response.Write("Previous ")
End If
For i=1 To intPageCount
If i <> intCurrentPage Then
Response.Write("" & i & " ")
Else
Response.Write("" & i & " ")
End If
Next
If intCurrentPage < intPageCount Then
Response.Write("Next")
Else
Response.Write("Next")
End If
Response.Write("
")
' ----------------------------------------------------------------------------------
End If
i=0
For intIndex = ((intCurrentPage-1)*10) To ((intCurrentPage*10)-1)
If intIndex < intNumResults Then
Response.Write("
")
strAddress = ""
If Trim(aryResults(intIndex, 3)) <> "" Then
strAddress = strAddress + Trim(aryResults(intIndex, 3))
End If
If Trim(aryResults(intIndex, 4)) <> "" Then
If strAddress <> "" Then strAddress = strAddress + ", "
strAddress = strAddress + Trim(aryResults(intIndex, 4))
End If
If strAddress <> "" Then strAddress = strAddress + ", "
strAddress = strAddress + Trim(aryResults(intIndex, 2))
If Trim(aryResults(intIndex, 5)) <> "" Then
If strAddress <> "" Then strAddress = strAddress + ", "
strAddress = strAddress + Trim(aryResults(intIndex, 5))
End If
Response.Write("