Imports System.Net
Imports System.Text
Imports System.IO

Public Class Demo

    Dim username As String = ""
    Dim password As String = ""

    Dim scheduleid_list As New ArrayList
    Dim schedulename_list As New ArrayList
    Dim scheduledesc_list As New ArrayList

    Dim webClient As New System.Net.WebClient
    Dim result As String

    Private Sub Demo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        recipient1_cmb.SelectedIndex = 0
        messagetype1_cmb.SelectedIndex = 0

        recipient2_cmb.SelectedIndex = 0
        messagetype2_cmb.SelectedIndex = 0

        TabControl1.TabPages(1).Enabled = False
        TabControl1.TabPages(2).Enabled = False
        TabControl1.TabPages(3).Enabled = False
        TabControl1.TabPages(4).Enabled = False

        DataGridView1.ColumnCount = 3
        DataGridView1.Columns(0).Name = "Schedule ID"
        DataGridView1.Columns(1).Name = "Schedule Name"
        DataGridView1.Columns(2).Name = "Schedule Description"

    End Sub

    Private Sub setScheduleTable(ByVal url As String)

        scheduleid_list.Clear()
        schedulename_list.Clear()
        scheduledesc_list.Clear()

        DataGridView1.Rows.Clear()

        result = webClient.DownloadString(url)
        response_area.AppendText("----- Command Breaker -----" + vbLf)
        response_area.AppendText(url + vbLf)
        response_area.AppendText(result + vbLf)
        response_area.SelectionStart = response_area.Text.Length
        response_area.ScrollToCaret()

        Dim result_index As Integer = -1
        Dim new_result As String = result

        For i As Integer = 0 To 1 Step 0

            result_index = new_result.IndexOf("|v|")

            If (result_index > -1) Then

                scheduleid_list.Add(new_result.Substring(0, result_index))

                new_result = new_result.Remove(0, result_index + 3)

                result_index = new_result.IndexOf("|v|")

                If (result_index > -1) Then

                    schedulename_list.Add(new_result.Substring(0, result_index))

                    new_result = new_result.Remove(0, result_index + 3)

                    result_index = new_result.IndexOf("|v|")

                    If (result_index > -1) Then

                        scheduledesc_list.Add(new_result.Substring(0, result_index))

                        new_result = new_result.Remove(0, result_index + 3)

                        result_index = new_result.IndexOf("|v|")

                        If (result_index > -1) Then

                            new_result = new_result.Remove(0, result_index + 3)
                            result_index = new_result.IndexOf("|v|")

                            If (result_index > -1) Then

                                new_result = new_result.Remove(0, result_index + 3)
                                result_index = new_result.IndexOf("|v|")

                                If (result_index > -1) Then

                                    new_result = new_result.Remove(0, result_index + 3)
                                    result_index = new_result.IndexOf("|v|")

                                    If (result_index > -1) Then

                                        new_result = new_result.Remove(0, result_index + 3)
                                        result_index = new_result.IndexOf("|v|")

                                        If (result_index > -1) Then

                                            new_result = new_result.Remove(0, result_index + 3)
                                            result_index = new_result.IndexOf("|v|")

                                            If (result_index > -1) Then

                                                new_result = new_result.Remove(0, result_index + 3)
                                                result_index = new_result.IndexOf("|v|")

                                                If (result_index > -1) Then

                                                    new_result = new_result.Remove(0, result_index + 3)

                                                    result_index = new_result.IndexOf("||")

                                                    If (result_index > -1) Then

                                                        new_result = new_result.Remove(0, result_index + 2)
                                                        result_index = -1

                                                    End If

                                                End If

                                            End If

                                        End If

                                    End If

                                End If

                            End If

                        End If

                    End If

                End If

            Else

                Exit For

            End If

        Next

        Dim content As String
        Dim cntr As Integer = 0

        For Each content In scheduleid_list

            Dim row As String() = New String() {scheduleid_list(cntr), schedulename_list(cntr), scheduledesc_list(cntr)}
            DataGridView1.Rows.Add(row)

            cntr = cntr + 1

        Next

        DataGridView1.ClearSelection()

    End Sub

    Private Sub reset_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles reset_btn.Click

        username_txt.Text = ""
        password_txt.Text = ""

        TabControl1.TabPages(1).Enabled = False
        TabControl1.TabPages(2).Enabled = False
        TabControl1.TabPages(3).Enabled = False
        TabControl1.TabPages(4).Enabled = False

    End Sub

    Private Sub set_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles set_btn.Click

        username = username_txt.Text.Trim.Replace(" ", "%20")
        password = password_txt.Text.Trim.Replace(" ", "%20")

        result = webClient.DownloadString("http://www.isms.com.my/isms_balance.php?un=" + username + "&pwd=" + password + "")

        If result = "-1008 = MISSING PARAMETER" Then

            MsgBox("Invalid iSMS credential inserted.")

            TabControl1.TabPages(1).Enabled = False
            TabControl1.TabPages(2).Enabled = False
            TabControl1.TabPages(3).Enabled = False
            TabControl1.TabPages(4).Enabled = False

        Else

            TabControl1.TabPages(1).Enabled = True
            TabControl1.TabPages(2).Enabled = True
            TabControl1.TabPages(3).Enabled = True
            TabControl1.TabPages(4).Enabled = True

            setScheduleTable("http://www.isms.com.my/api_list_of_schedule.php?un=" + username + "&pwd=" + password + "")

        End If

    End Sub

    Private Sub message1_area_KeyUP(ByVal sender As Object, ByVal e As KeyEventArgs) Handles message1_area.KeyUp

        countwords1_lbl.Text = message1_area.TextLength

    End Sub

    Private Sub recipient1_cmb_IndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles recipient1_cmb.SelectedIndexChanged

        If recipient1_cmb.SelectedItem.ToString() = "Name" Then

            recipient_lbl.Text = "Recipient name"
            phonebook1_btn.Enabled = False
            phoneno1_txt.Enabled = True

        End If

        If recipient1_cmb.SelectedItem.ToString() = "Phone Number" Then

            recipient_lbl.Text = "Recipient phone number"
            phonebook1_btn.Enabled = False
            phoneno1_txt.Enabled = True

        End If

        If recipient1_cmb.SelectedItem.ToString() = "Phonebook" Then

            recipient_lbl.Text = "Phonebook"
            phonebook1_btn.Enabled = True
            phoneno1_txt.Enabled = False

        End If

    End Sub

    Public groupid_phonebook As String
    Public contactname_phonebook As String

    Dim recipient1 As String = ""
    Dim message1 As String = ""
    Dim senderid1 As String = ""
    Dim type1 As String = ""

    Private Sub send_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles send_btn.Click

        If messagetype1_cmb.SelectedItem.ToString() = "1 - ASCII (English, Bahasa Melayu, etc)" Then

            type1 = "1"

        Else

            type1 = "2"

        End If

        message1 = message1_area.Text.Trim.Replace(" ", "%20")

        If message1 = "" Then

            MsgBox("Please insert message")

        End If

        senderid1 = senderid1_txt.Text.Trim.Replace(" ", "%20")

        If senderid1 = "" Then

            MsgBox("Please insert sender ID")

        End If

        If recipient1_cmb.SelectedItem.ToString() = "Phonebook" And groupid_phonebook = "" And contactname_phonebook = "" Then

            MsgBox("Please select recipient from phonebook")

        Else

            If groupid_phonebook <> "" Then

                setResponse("http://www.isms.com.my/api_send_sms_by_group.php?un=" + username + "&pwd=" + password + "&groupid=" + groupid_phonebook + "&msg=" + message1 + "&type=" + type1 + "&sendid=" + senderid1 + "")

            End If

            If contactname_phonebook <> "" Then

                setResponse("http://www.isms.com.my/api_send_sms_by_name.php?un=" + username + "&pwd=" + password + "&name=" + contactname_phonebook + "&msg=" + message1 + "&type=" + type1 + "&sendid=" + senderid1 + "")

            End If

        End If

        If recipient1_cmb.SelectedItem.ToString() = "Name" Then

            If phoneno1_txt.Text = "" Then

                MsgBox("Please insert recipient contact name")

            Else

                setResponse("http://www.isms.com.my/api_send_sms_by_name.php?un=" + username + "&pwd=" + password + "&name=" + phoneno1_txt.Text.Trim.Replace(" ", "%20") + "&msg=" + message1 + "&type=" + type1 + "&sendid=" + senderid1 + "")

            End If

        End If

        If recipient1_cmb.SelectedItem.ToString() = "Phone Number" Then

            If phoneno1_txt.Text = "" Then

                MsgBox("Please insert recipient phone number")

            Else

                setResponse("http://www.isms.com.my/isms_send.php?un=" + username + "&pwd=" + password + "&dstno=" + phoneno1_txt.Text.Trim.Replace(" ", "%20") + "&msg=" + message1 + "&type=" + type1 + "&sendid=" + senderid1 + "")

            End If

        End If

    End Sub

    Private Sub phonebook1_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles phonebook1_btn.Click

        Phonebook.username = username
        Phonebook.password = password

        Phonebook.ShowDialog()

    End Sub

    Private Sub setResponse(ByVal url As String)

        result = webClient.DownloadString(url)
        response_area.AppendText("----- Command Breaker -----" + vbLf)
        response_area.AppendText(url + vbLf)
        response_area.AppendText(result + vbLf)
        response_area.SelectionStart = response_area.Text.Length
        response_area.ScrollToCaret()

    End Sub

    Private Sub checkbalance_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles checkbalance_btn.Click

        setResponse("http://www.isms.com.my/isms_balance.php?un=" + username + "&pwd=" + password + "")

    End Sub

    Private Sub recipient2_cmb_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles recipient2_cmb.SelectedIndexChanged

        If recipient2_cmb.SelectedItem.ToString() = "Name" Then

            recipient2_lbl.Text = "Recipient name"
            phonebook2_btn.Enabled = False
            phoneno2_txt.Enabled = True

        End If

        If recipient2_cmb.SelectedItem.ToString() = "Phone Number" Then

            recipient2_lbl.Text = "Recipient phone number"
            phonebook2_btn.Enabled = False
            phoneno2_txt.Enabled = True

        End If

        If recipient2_cmb.SelectedItem.ToString() = "Phonebook" Then

            recipient2_lbl.Text = "Phonebook"
            phonebook2_btn.Enabled = True
            phoneno2_txt.Enabled = False

        End If

    End Sub

    Private Sub phonebook2_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles phonebook2_btn.Click

        Phonebook.username = username
        Phonebook.password = password

        Phonebook.ShowDialog()

    End Sub

    Private Sub send2_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles send2_btn.Click

        If messagetype2_cmb.SelectedItem.ToString() = "1 - ASCII (English, Bahasa Melayu, etc)" Then

            type1 = "1"

        Else

            type1 = "2"

        End If

        message1 = message2_area.Text.Trim.Replace("number", Int((100000 * Rnd()) + 1))
        message1 = message1.Replace(" ", "%20")

        If message1 = "" Then

            MsgBox("Please insert message")

        End If

        senderid1 = senderid2_txt.Text.Trim.Replace(" ", "%20")

        If senderid1 = "" Then

            MsgBox("Please insert sender ID")

        End If

        If recipient2_cmb.SelectedItem.ToString() = "Phonebook" And groupid_phonebook = "" And contactname_phonebook = "" Then

            MsgBox("Please select recipient from phonebook")

        Else

            If groupid_phonebook <> "" Then

                setResponse("http://www.isms.com.my/api_send_sms_by_group.php?un=" + username + "&pwd=" + password + "&groupid=" + groupid_phonebook + "&msg=" + message1 + "&type=" + type1 + "&sendid=" + senderid1 + "")

            End If

            If contactname_phonebook <> "" Then

                setResponse("http://www.isms.com.my/api_send_sms_by_name.php?un=" + username + "&pwd=" + password + "&name=" + contactname_phonebook + "&msg=" + message1 + "&type=" + type1 + "&sendid=" + senderid1 + "")

            End If

        End If

        If recipient2_cmb.SelectedItem.ToString() = "Name" Then

            If phoneno2_txt.Text = "" Then

                MsgBox("Please insert recipient contact name")

            Else

                setResponse("http://www.isms.com.my/api_send_sms_by_name.php?un=" + username + "&pwd=" + password + "&name=" + phoneno2_txt.Text.Trim.Replace(" ", "%20") + "&msg=" + message1 + "&type=" + type1 + "&sendid=" + senderid1 + "")

            End If

        End If

        If recipient2_cmb.SelectedItem.ToString() = "Phone Number" Then

            If phoneno2_txt.Text = "" Then

                MsgBox("Please insert recipient phone number")

            Else

                setResponse("http://www.isms.com.my/isms_send.php?un=" + username + "&pwd=" + password + "&dstno=" + phoneno2_txt.Text.Trim.Replace(" ", "%20") + "&msg=" + message1 + "&type=" + type1 + "&sendid=" + senderid1 + "")

            End If

        End If

    End Sub

    Private Sub remove_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles remove_btn.Click

        If schedular_id = "" Then

            MsgBox("Please select a schedule from the table")

        Else

            result = webClient.DownloadString("http://www.isms.com.my/isms_scheduler.php?un=" + username + "&pwd=" + password + "&scid=" + schedular_id + "&action=delete")

            response_area.AppendText("----- Command Breaker -----" + vbLf)
            response_area.AppendText("http://www.isms.com.my/isms_scheduler.php?un=" + username + "&pwd=" + password + "&scid=" + schedular_id + "&action=delete" + vbLf)
            response_area.AppendText(result + vbLf)

            setScheduleTable("http://www.isms.com.my/api_list_of_schedule.php?un=" + username + "&pwd=" + password + "")

        End If

    End Sub

    Dim schedular_id As String

    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

        Dim value As Object = DataGridView1.Rows(e.RowIndex).Cells(0).Value

        schedular_id = value.ToString

    End Sub

    Private Sub add_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles add_btn.Click

        SchedularEditor.username = username
        SchedularEditor.password = password
        SchedularEditor.action = "add"

        SchedularEditor.ShowDialog()

        setScheduleTable("http://www.isms.com.my/api_list_of_schedule.php?un=" + username + "&pwd=" + password + "")

    End Sub

    Private Sub update_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles update_btn.Click

        If schedular_id = "" Then

            MsgBox("Please select a schedule from the table")

        Else

            SchedularEditor.username = username
            SchedularEditor.password = password
            SchedularEditor.action = "update"
            SchedularEditor.scheduleid = schedular_id

            SchedularEditor.ShowDialog()

            setScheduleTable("http://www.isms.com.my/api_list_of_schedule.php?un=" + username + "&pwd=" + password + "")

        End If

    End Sub
End Class
