<%@ Language=VBScript %>
<%
Response.ContentType = "application/json"
Response.Charset = "UTF-8"
Response.AddHeader "Access-Control-Allow-Origin", "*"
Response.AddHeader "Cache-Control", "no-cache, no-store, must-revalidate"
%><!--#include file="./includes/sql.asp"--><%

Dim strSQL, jsonOutput, isFirst

strSQL = "SELECT businessunitid, businessunit " & _
         "FROM businessunits " & _
         "WHERE isactive = 1 " & _
         "ORDER BY businessunit ASC"

Set rs = objConn.Execute(strSQL)

jsonOutput = "{""success"":true,""businessunits"":["
isFirst = True

Do While Not rs.EOF
    If Not isFirst Then jsonOutput = jsonOutput & ","
    isFirst = False

    jsonOutput = jsonOutput & "{"
    jsonOutput = jsonOutput & """businessunitid"":" & rs("businessunitid") & ","
    jsonOutput = jsonOutput & """businessunit"":""" & JSEscape(rs("businessunit") & "") & """"
    jsonOutput = jsonOutput & "}"

    rs.MoveNext
Loop

rs.Close
jsonOutput = jsonOutput & "]}"

Response.Write jsonOutput

Function JSEscape(s)
    Dim r
    r = s
    r = Replace(r, "\", "\\")
    r = Replace(r, """", "\""")
    r = Replace(r, Chr(13), "")
    r = Replace(r, Chr(10), "\n")
    r = Replace(r, Chr(9), "\t")
    JSEscape = r
End Function
%>
