'
' Example script for 2BrightSparks SyncBackPro V8 that sends an SMS
' if a profile fails (if the run is simulated then it does not).
'
' IMPORTANT, READ THE FOLLOWING:
'
' - You must first create an account at http://www.clickatell.com/
' - You will receive an API ID. You must set the profile to use that App ID.
'
' SBLang=Basic
'
' http://www.2BrightSparks.com/
'

'-----------------------------------------------------------------------------
'-DO NOT MODIFY ANYTHING BELOW THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING !-
'-----------------------------------------------------------------------------

'
' This is a profile config and runtime script 
'
Function Description(ByRef ScriptType)
  Description = "Send SMS on profile failure"
  ScriptType = SCRIPTTYPE_RUN + SCRIPTTYPE_CONFIG
End Function

'
' This routined is called after the profile has finished.
'
Sub RunProfileResult(ProfileResult, ErrMsg)
  Dim sUrl
  Dim sText
  Dim oXMLHTTP, sPostData, sResult
  Dim SendSMS

  ' Check to see if the profile has run
  If SBRunning.Simulated then
    Exit Sub
  End If

  ' Failed?
  SendSMS = True
  Select Case ProfileResult
  Case 0
    ' 0 = No result (ELR_None)
    SendSMS = False
  Case 1
    ' 1 = Unknown profile (ELR_UnknownProfile)
    SendSMS = False
  Case 2
    ' 2 = Profile is already running (ELR_AlreadyRunning)
    SendSMS = False
  Case 3
    ' 3 = Profile has been imported and not run yet (ELR_Imported)
    SendSMS = False
  Case 4
    ' 4 = Profile is running (ELR_Running)
    SendSMS = False
  Case 11
    ' 11 = Restore was a success (ELR_RestSuccess)
    SendSMS = False
  Case 14
    ' 14 = Run was a success (ELR_Success)
    SendSMS = False
  End Select

  ' Are we going to send an SMS?
  If SendSMS = False then
    Exit Sub
  End If

  ' Send SMS
  sText = "Profile " & SBRunning.Name & " has failed (code " & ProfileResult & ")."
  if ErrMsg <> "" then
    sText = sText & " Error message: " & ErrMsg
  End If
  sUrl = "http://api.clickatell.com/http/sendmsg"
  sPostData = "api_id=" & CStr(SBVariables.GetProperty("SMSAppID", "", FALSE))
  sPostData = sPostData & "&user=" & CStr(SBVariables.GetProperty("SMSUsername", "", FALSE))
  sPostData = sPostData & "&password=" & CStr(SBVariables.GetProperty("SMSPassword", "", FALSE))
  sPostData = sPostData & "&to=" & CStr(SBVariables.GetProperty("SMSMobileNum", "", FALSE))
  sPostData = sPostData & "&text=" & sText
  Set oXMLHTTP = CreateObject("Microsoft.XMLHTTP")
  oXMLHTTP.Open("POST", sUrl, false)
  oXMLHTTP.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
  oXMLHTTP.Send(sPostData)
  sResult = oXMLHTTP.responseText
  SBRunning.DebugOut("SMSResult", sResult, 1)
  Set oXMLHTTP = nothing
End Sub

'
' Configuration part of the script
'

' Not for group profiles, and only display setup page if the profile
' is using the script.
Function ConfigWantSetupNode(IsGroup)
  ConfigWantSetupNode = not IsGroup and SBProfile.UsesScript
End Function

' Reset to factory defaults
Sub ConfigFactoryDefaults
  SBVariables.DeleteProperty("SMSUsername")
  SBVariables.DeleteProperty("SMSPassword")
  SBVariables.DeleteProperty("SMSMobileNum")
  SBVariables.DeleteProperty("SMSAppID")
End Sub

' Always allow the setup page to close
Function ConfigCanClose
  ConfigCanClose = TRUE
End Function

' Can revert to factory settings
Function ConfigCanRevert
  ConfigCanRevert = TRUE
End Function

' Setup node caption
Function ConfigNodeCaption
  ConfigNodeCaption = "SMS Result"
End Function

' Create setup window display
Sub ConfigSetupDisplay
  SBProfile.AddLabel("1. You must first create an account at http://www.clickatell.com/", 1)
  SBProfile.AddLabel("2. Login to your account, go to 'Manage my Products', and select 'HTTP' from the drop-down list", 1)
  SBProfile.AddLabel("3. Enter SyncBack for the 'Name' and click the Submit button", 1)
  SBProfile.AddLabel("4. You will receive an API ID. You must set SMSAppID below to that ID.", 1)
  SBProfile.AddLabel("", 1)

  SBProfile.AddEdit("Username", 128, FALSE, FALSE, 2)
  SBProfile.AddEdit("Password", 128, FALSE, TRUE, 3)
  SBProfile.AddEdit("Mobile Number (it is recommended that you include the country code)", 128, FALSE, FALSE, 4)
  SBProfile.AddEdit("SMS App ID", 128, FALSE, FALSE, 5)
End Sub

' Load settings. Return error message on failure.
Function ConfigLoadSettings
  ConfigLoadSettings = ""

  SBProfile.SetEdit(SBVariables.GetProperty("SMSUsername", "", FALSE), 2)
  SBProfile.SetEdit(SBVariables.GetProperty("SMSPassword", "", FALSE), 3)
  SBProfile.SetEdit(SBVariables.GetProperty("SMSMobileNum", "", FALSE), 4)
  SBProfile.SetEdit(SBVariables.GetProperty("SMSAppID", "", FALSE), 5)
End Function

' Save settings. Return error message on failure.
Function ConfigSaveSettings(Silent)
  ConfigSaveSettings = ""
 
  if SBProfile.GetEdit(2) = "" then
    ConfigSaveSettings = "The username cannot be empty!"
    Exit Function
  end if

  if SBProfile.GetEdit(3) = "" then
    ConfigSaveSettings = "The password cannot be empty!"
    Exit Function
  end if

  if SBProfile.GetEdit(4) = "" then
    ConfigSaveSettings = "The mobile number cannot be empty!"
    Exit Function
  end if

  if SBProfile.GetEdit(5) = "" then
    ConfigSaveSettings = "The SMS App ID cannot be empty!"
    Exit Function
  end if

  SBVariables.SetProperty("SMSUsername", SBProfile.GetEdit(2))
  SBVariables.SetProperty("SMSPassword", SBProfile.GetEdit(3))
  SBVariables.SetProperty("SMSMobileNum", SBProfile.GetEdit(4))
  SBVariables.SetProperty("SMSAppID", SBProfile.GetEdit(5))
End Function

' The settings have changed
Sub ConfigUpdateConditionals
End Sub
