<% 
' Copyright 2000 Phone.com, Inc.  All rights reserved.
'
'   Subject to the terms and conditions of the SDK License Agreement,
'   Phone.com, Inc. hereby grants you the right to use the UP.SDK
'   software and its related documentation.
'
'   The Phone.com name and logo and the family of terms carrying the "UP."
'   prefix are trademarks of Phone.com, Inc. All other brands and product
'   names may be trademarks of the respective companies with which they
'   are associated.
'
' fetch.asp
'
'   This example implements the companion ""prefetch URL" for the 
'   sendAlert.asp sample WML application. This script is called
'   by the UP.Link Messenger service when it receives the prefetch
'   notification request from the sendalert.cgi script, per the
'   URL that is passed as an argument with the prefetch operation.
'
'   Append ?NEXT=<value> to the URL when calling this script to 
'   indicate which kind of alert to send.  These are the possible
'   values for the NEXT variable:
'
'         DISPATCH:     Truck dispatch update
'         MEETING:      Schedule reminder
'         STOCK:        Stock quote
'
'   In contrast with sendAlert.asp, this script will introduce an
'   alert when used in HTTP-Direct mode with an SDK 4.0 Simulator
'   if accessed by entering its URL into the Sim "Go: " text field.
'
'   Key concepts illustrated include:
'
'       - Creating an 'alert' digest for delivery as a pre-fetch
'         notification. 

' Declarations 
' 
Dim deckA, cardM, cardS, cardD, deckI
Dim demo, CardSet, aCard, Titles, aTitle

' Constants
'
' Alert Type
'
' See further: 
'
'   Tools and API Reference, 
'   COM Notification Library Reference, 
'   Digest Class, DigestAddAlert
'
'     An alert type is specified by a 4-char code. 
'     The first character specifies the Default, 
'     the second specifies the type of sound, 
'     the third indicates the type of visual indication, 
'     and the fourth indicates the type of vibrate mode. 
'
alertType   = "D---"

' A newline (linefeed) is appended to each line of WML to
' aid legibility of source dumped to the Sim Info Window.
' 
nl  = Chr(10)

' WML Content
'
'   A deck to report an alert, with one of the meeting cards
'   embedded within it, deckA
'
Sub CreateAlertDeck(aCard)
    deckA =         "<wml>" & nl
    deckA = deckA &    aCard
    deckA = deckA & "  <card id=""time"">" & nl
    deckA = deckA & "    <p>" & nl
    deckA = deckA & "      Fetched: " & nl
    deckA = deckA & "    </p>" & nl
    deckA = deckA & "    <p align=""center"">" & nl
    deckA = deckA & "      " & Now() & nl
    deckA = deckA & "    </p>" & nl
    deckA = deckA & "  </card>" & nl
    deckA = deckA & "</wml>" & nl
End Sub

'   A card to advertise a meeting, cardM
'
cardM =         "  <card>" & nl
cardM = cardM & "    <do type=""options"" label=""Time"">" & nl
cardM = cardM & "      <go href=""#time""/>" & nl
cardM = cardM & "    </do>" & nl
cardM = cardM & "    <p>" & nl
cardM = cardM & "      Staff meeting in 10 minutes!" & nl
cardM = cardM & "    </p>" & nl
cardM = cardM & "  </card>" & nl

'   A card to report a stock quote change, cardS
'
cardS =         "  <card>" & nl
cardS = cardS & "    <do type=""options"" label=""Time"">" & nl
cardS = cardS & "      <go href=""#time""/>" & nl
cardS = cardS & "    </do>" & nl
cardS = cardS & "    <p>" & nl
cardS = cardS & "      Symbol  UPLN" & nl
cardS = cardS & "    </p>" & nl
cardS = cardS & "    <p mode=""nowrap"">Last 52 1/4</p>" & nl
cardS = cardS & "    <p mode=""nowrap"">Chg +4</p>" & nl
cardS = cardS & "    <p mode=""nowrap"">Vol 2,505,900</p>" & nl
cardS = cardS & "  </card>" & nl

'   A card to report a dispatch message, cardD
'
cardD =         "  <card>" & nl
cardD = cardD & "    <do type=""options"" label=""Time"">" & nl
cardD = cardD & "      <go href=""#time""/>" & nl
cardD = cardD & "    </do>" & nl
cardD = cardD & "    <do type=""accept"" label=""Where"">" & nl
cardD = cardD & "      <go href=""#details""/>" & nl
cardD = cardD & "    </do>" & nl
cardD = cardD & "    <p>" & nl
cardD = cardD & "      Dispatch: "& nl
cardD = cardD & "      <select name=""addr"">" & nl
cardD = cardD & "        <option value=""445 Geary"">" 
cardD = cardD &            "  1:00p Svc - SF Downtown</option>" & nl
cardD = cardD & "        <option value=""2389 Mission"">"
cardD = cardD &            "  2:00p Maint - SF South</option>" & nl
cardD = cardD & "        <option value=""329 Lombard"">"
cardD = cardD &            "  3:00p Svc - SF North</option>" & nl
cardD = cardD & "        <option value=""857 La Playa"">"
cardD = cardD &            "  4:00p Maint - SF West</option>" & nl
cardD = cardD & "      </select>" & nl
cardD = cardD & "    </p>" & nl
cardD = cardD & "  </card>" & nl
cardD = cardD & "  <card id=""details"">" & nl
cardD = cardD & "    <do type=""accept"" label=""Call"">" & nl
cardD = cardD & "      <go href=""wtai://wp/mc;4155552222""/>" & nl
cardD = cardD & "    </do>" & nl
cardD = cardD & "    <p mode=""nowrap"">$(addr)</p>" & nl
cardD = cardD & "    <p mode=""nowrap"">San Francisco</p>" & nl
cardD = cardD & "    <p mode=""nowrap"">415-555-2222</p>" & nl
cardD = cardD & "  </card>" & nl

'   A deck to describe usage, an Info deck, deckI
'
deckI =         "<wml>" & nl
deckI = deckI & "  <card>" & nl
deckI = deckI & "    <p>" & nl
deckI = deckI & "      Call this script using:" & nl
deckI = deckI & "    </p>" & nl
deckI = deckI & "    <p mode=""nowrap"">"
deckI = deckI & "      fetch.asp?NEXT=DemoType" & nl
deckI = deckI & "    </p>" & nl
deckI = deckI & "  </card>" & nl
deckI = deckI & "</wml>" & nl

Sub CreateDemoAndMessageDictionaries()
    set CardSet = CreateObject("Scripting.Dictionary")
    CardSet.Add "MEETING",  cardM
    CardSet.Add "STOCK",    cardS
    CardSet.Add "DISPATCH", cardD

    set Titles  = CreateObject("Scripting.Dictionary")
    Titles.Add  "MEETING",  "Meeting Reminder"
    Titles.Add  "STOCK",    "Stock Update"
    Titles.Add  "DISPATCH", "Dispatch Notice"
End Sub

' Main routine
'
Sub Main()
    Dim aDeck
    ' Check whether a query string was appended to the Request
    '
    qCount  = Request.QueryString.Count
    If qCount = 0 Then
        ' No, so just write out the Info deck.
        '
        call OutputDeck(deckI)
    End If
    If qCount > 0 Then
        ' Check for and extract query strings
        '
        If Request.QueryString("NEXT") <> "" Then
            demo = Request.QueryString("NEXT")(1)
            If CardSet.Exists(demo) Then
                ' Matched the card with its name
                ' found in the CardSet Dictionary
                aCard = CardSet.Item(demo)

                If Request.QueryString("REFETCH") <> "" Then
                    call CreateAlertDeck(aCard)
                    call OutputDeck(deckA)
                Else
                    ' Match the card name with a title
                    ' found in the Titles Dictionary
                    aTitle    = Titles.Item(demo)
                    call CreateAlertDeck(aCard)
                    call OutputDeckAndAlert(deckA)
                End If
            End If
        End If
    End If
End Sub

' Subroutine to dispatch a deck in a digest to the subscriber
'
Sub OutputDeck(aDeck)

    ' Create a new COM Digest object.
    '
    set myDigest  = Server.CreateObject("PDCDigestUtils.CPDCDigest")
    
    ' Add the deck to the digest object.
    '
    myDigest.DigestAddDeck "?", aDeck
    
    ' Serialize the digest into a single output buffer, digestString.
    '
    digestString            = myDigest.DigestSerialize(0)
    
    ' Specify the deck type in the digest object as WML.
    ' Use a constant while awaiting fix to DigestUtils
    ' to introduce selection by enumerated deck types.
    '
    '    myDigest.DeckType         = 0
    
    ' Suppress content type output within the digest to prevent a 
    ' server from encapsulating WML content within a second boundary.
    '
    myDigest.DigestSuppressContentType 1
    
    ' Specify content type through the server response, 
    ' rather than within the digest.
    '
    Response.ContentType    = myDigest.DigestGetContentType
    
    ' Write the response buffer to the client.
    '
    Response.BinaryWrite(digestString)

End Sub

' Subroutine to dispatch a deck in a digest to the subscriber
'
Sub OutputDeckAndAlert(aDeck)

    Dim anURI
    
    anURI       = "?NEXT=" & demo & "&REFETCH=YES"

    ' Create a new COM Digest object.
    '
    set myDigest  = Server.CreateObject("PDCDigestUtils.CPDCDigest")
    
    ' Add the deck to the digest object.
    '
    myDigest.DigestAddDeck anURI, aDeck
    
    ' Add an alert to the digest object.
    '
    myDigest.DigestAddAlert alertType, anURI, aTitle
    
    ' Suppress content type output within the digest to prevent a 
    ' server from encapsulating WML content within a second boundary.
    '
    myDigest.DigestSuppressContentType 1
    
    ' Serialize the digest into a single output buffer, digestString.
    '
    digestString    = myDigest.DigestSerialize(0)
    
    ' Specify a deck of type WML to the digest object.
    '
    myDigest.DeckType         = 1
    
    ' Specify content type through the server response, 
    ' rather than within the digest.
    '
    Response.ContentType    = myDigest.DigestGetContentType
    
    ' Write the response buffer to the client.
    '
    Response.BinaryWrite(digestString)

End Sub

' Main routine starts here
'
call CreateDemoAndMessageDictionaries()
call Main
%>
