<%
'=======================================================================================================================
' Maps a parsed-JSON answers Dictionary (from the SurveyJS order-details form) onto a
' POBO_OrderDetails instance, and validates it first - the client-side SurveyJS
' required/enum rules in continue.asp are a UX convenience, not something the server can trust.
'=======================================================================================================================

' Returns "" if valid, or a "; "-joined list of validation error messages.
Function ValidateOrderDetailsAnswers(answers)
    Dim errors() : ReDim errors(-1)

    RequireNonEmptyString errors, answers, "ContactName", "Contact name is required."
    RequireNonEmptyString errors, answers, "Municipality", "Municipality is required."
    RequireNonEmptyString errors, answers, "Phone", "Phone number is required."

    RequireEnum errors, answers, "PurpleEnvelopeStock", Array("KCIStock", "OwnStock")
    RequireEnum errors, answers, "PurplePrintOption", Array("AddressesAndPermit", "PermitOnly")
    RequireEnum errors, answers, "PurpleBlueProvider", Array("ElectionSource", "PSI", "Spectrum", "Other")
    RequireEnum errors, answers, "PurplePrintStyle", Array("ColorCoding", "BlackOnly")
    RequireEnum errors, answers, "PurpleColorCodingBy", Array("Precinct", "Election")
    RequireEnum errors, answers, "BluePermitOwnership", Array("KCIPermit", "OwnPermit")
    RequireEnum errors, answers, "BlueHasNonprofitStatus", Array("Yes", "No", "Unsure")
    RequireEnum errors, answers, "MailingPostageOption", Array("FirstClass", "NonprofitRate", "PresortStandard", "No")
    RequireEnum errors, answers, "MailingHasNonprofitStatus", Array("Yes", "No", "NotSure")

    RequireBoolean errors, answers, "MailingNonprofitAuthCodeUnknown"

    RequireBoolean errors, answers, "PurpleWantsQuote"
    RequireBoolean errors, answers, "PurpleWantsBlueEnvelopes"
    RequireBoolean errors, answers, "PurpleTrackMIBallot"
    RequireBoolean errors, answers, "BlueWantsQuote"
    RequireBoolean errors, answers, "BluePrintPermit"
    RequireBoolean errors, answers, "MailingWantsService"

    RequireNonNegativeInteger errors, answers, "PurplePermanentBallotListCount"
    RequireNonNegativeInteger errors, answers, "PurplePrecinctCount"
    RequireNonNegativeInteger errors, answers, "PurpleExtraEnvelopeQty"
    RequireNonNegativeInteger errors, answers, "MailingEstimatedQuantity"
    RequireNonNegativeInteger errors, answers, "SecrecySleevesQty"
    RequireNonNegativeInteger errors, answers, "IVotedStickerRolls"
    RequireNonNegativeInteger errors, answers, "FutureVoterStickerRolls"

    RequireValidDate errors, answers, "MailingPickupDate"

    If UBound(errors) >= 0 Then
        ValidateOrderDetailsAnswers = Join(errors, "; ")
    Else
        ValidateOrderDetailsAnswers = ""
    End If
End Function

Sub MapOrderDetailsAnswers(ByRef model, answers)
    ' Contact
    model.ContactName   = DictValue(answers, "ContactName")
    model.Municipality  = DictValue(answers, "Municipality")
    model.Phone         = DictValue(answers, "Phone")

    ' Purple envelope
    model.PurpleWantsQuote          = DictValue(answers, "PurpleWantsQuote")
    model.PurpleEnvelopeStock       = DictValue(answers, "PurpleEnvelopeStock")
    model.PurplePrintOption         = DictValue(answers, "PurplePrintOption")
    model.PurpleWantsBlueEnvelopes  = DictValue(answers, "PurpleWantsBlueEnvelopes")
    model.PurpleBlueProvider        = DictValue(answers, "PurpleBlueProvider")
    model.PurpleBlueProviderOther   = DictValue(answers, "PurpleBlueProviderOther")
    model.PurplePrintStyle          = DictValue(answers, "PurplePrintStyle")
    model.PurpleColorCodingBy       = DictValue(answers, "PurpleColorCodingBy")
    model.PurplePrecinctCount       = DictValue(answers, "PurplePrecinctCount")
    model.PurpleColorNames          = DictValue(answers, "PurpleColorNames")
    model.PurpleTrackMIBallot       = DictValue(answers, "PurpleTrackMIBallot")
    model.PurpleExtraEnvelopeQty    = DictValue(answers, "PurpleExtraEnvelopeQty")
    model.PurplePermanentBallotListCount = DictValue(answers, "PurplePermanentBallotListCount")

    ' Blue envelope
    model.BlueWantsQuote          = DictValue(answers, "BlueWantsQuote")
    model.BluePrintPermit         = DictValue(answers, "BluePrintPermit")
    model.BluePermitOwnership     = DictValue(answers, "BluePermitOwnership")
    model.BlueHasNonprofitStatus  = DictValue(answers, "BlueHasNonprofitStatus")
    model.BlueNonprofitAuthCode   = DictValue(answers, "BlueNonprofitAuthCode")
    model.BluePermitCity          = DictValue(answers, "BluePermitCity")
    model.BluePermitNumber        = DictValue(answers, "BluePermitNumber")

    ' Mailing service
    model.MailingWantsService        = DictValue(answers, "MailingWantsService")
    model.MailingPostageOption       = DictValue(answers, "MailingPostageOption")
    model.MailingHasNonprofitStatus  = DictValue(answers, "MailingHasNonprofitStatus")
    model.MailingNonprofitAuthCode   = DictValue(answers, "MailingNonprofitAuthCode")
    model.MailingNonprofitAuthCodeUnknown = DictValue(answers, "MailingNonprofitAuthCodeUnknown")
    model.MailingEstimatedQuantity   = DictValue(answers, "MailingEstimatedQuantity")
    model.MailingPickupDate          = DictValue(answers, "MailingPickupDate")

    ' Additional items
    model.SecrecySleevesQty       = DictValue(answers, "SecrecySleevesQty")
    model.IVotedStickerRolls      = DictValue(answers, "IVotedStickerRolls")
    model.FutureVoterStickerRolls = DictValue(answers, "FutureVoterStickerRolls")
    model.SpecialRequests         = DictValue(answers, "SpecialRequests")
End Sub
%>
