@name Dial Computer
@inputs Active Open Chevron Inbound CS:wirelink KB ActiveIris
@outputs DialAddress Close DialMode DisableAutoClose Iris
@persist MenuSelector OldKB DialAdr:string Dialing InboundClosed Closed CloseIrisOnInbound CloseOnDisCnt CloseOnInbound
@trigger all

interval(200)

if(first())
{
    CS[2041] = 1
    MenuSelector = 1
    DisableAutoClose = 0
    DialMode = 0
    Close = 0
    Closed = 0
    DialAddress = 0
}

Iris = ActiveIris
Close = 0
if(Inbound == 1 & InboundClosed == 0)
{
    Iris = 1
    InboundClosed = 1
}
elseif(Inbound == 0)
{
    InboundClosed = 0
}

if(Active == 1 | Open == 1)
{
    Closed = 0
}
elseif(Closed == 0 & Active == 0 & Open == 0)
{
    Iris = 0
    Closed = 1
}

CS:writeString("Robotboy655's SG Dial Computer",0,0,9)

#Menu:
CS:writeString("  ",0,5,0,555)

CS:writeString(MenuSelector == 1 ? ">" : " ",0,6,999,0)
CS:writeString(" ",1,6,0,DisableAutoClose == 1 ? 333 : 393)
CS:writeString("Auto Close                ",2,6,999,555)

CS:writeString(MenuSelector == 2 ? ">" : " ",0,7,999,0)
CS:writeString(" ",1,7,0,DialMode == 1 ? 393 : 333)
CS:writeString("Fast Dial Mode            ",2,7,999,555)

CS:writeString(MenuSelector == 3 ? ">" : " ",0,8,999,0)
CS:writeString(" ",1,8,0,ActiveIris == 1 ? 393 : 333)
CS:writeString("Iris                      ",2,8,999,555)

CS:writeString(MenuSelector == 4 ? ">" : " ",0,9,999,0)
CS:writeString(" ",1,9,0,CloseIrisOnInbound == 1 ? 393 : 333)
CS:writeString("Close Iris If Inbound     ",2,9,999,555)

CS:writeString(MenuSelector == 5 ? ">" : " ",0,10,999,0)
CS:writeString(" ",1,10,0,CloseOnInbound == 1 ? 393 : 333)
CS:writeString("Close Inbound Connection  ",2,10,999,555)

CS:writeString(MenuSelector == 6 ? ">" : " ",0,11,999,0)
CS:writeString(" ",1,11,0,CloseOnDisCnt == 1 ? 393 : 333)
CS:writeString("Close Iris On Disconnect  ",2,11,999,555)

if(Active == 1)
{
    CS:writeString(MenuSelector == 7 ? ">" : " ",0,12,999,0)
    CS:writeString(" ",1,12,0,333)
    CS:writeString("Close                        ",2,12,Active == 1 ? 999 : 0,Active == 1 ? 555 : 0)
}
else
{
    CS:writeString(MenuSelector == 7 ? ">" : " ",0,12,999,0)
    CS:writeString(" ",1,12,0,333)
    CS:writeString("Dial                      ",2,12,Active == 1 | Dialing >= 1 ? 0 : 999,Active == 1 | Dialing >= 1 ? 0 : 555)
    CS:writeString("Dial " + DialAdr,2,12,Active == 1 | Dialing >= 1 ? 0 : 999,Active == 1 | Dialing >= 1 ? 0 : 555)
}

CS:writeString("  ",0,13,0,555)

#Chevrons:
CS:writeString("  ",28,5,0,555)

CS:writeString(" ",28,6,0,Chevron >= 1 ? 393 : 333)
CS:writeString(Chevron >= 1 & Inbound == 0 & DialAdr:length() == 6 ? DialAdr:index(1) : Chevron >= 1 ? "1" : " ",29,6,999,0)

CS:writeString(" ",28,7,0,Chevron >= 2 ? 393 : 333)
CS:writeString(Chevron >= 2 & Inbound == 0 & DialAdr:length() == 6 ? DialAdr:index(2) : Chevron >= 2 ? "2" : " ",29,7,999,0)

CS:writeString(" ",28,8,0,Chevron >= 3 ? 393 : 333)
CS:writeString(Chevron >= 3 & Inbound == 0 & DialAdr:length() == 6 ? DialAdr:index(3) : Chevron >= 3 ? "3" : " ",29,8,999,0)

CS:writeString(" ",28,9,0,Chevron >= 4 ? 393 : 333)
CS:writeString(Chevron >= 4 & Inbound == 0 & DialAdr:length() == 6 ? DialAdr:index(4) : Chevron >= 4 ? "4" : " ",29,9,999,0)

CS:writeString(" ",28,10,0,Chevron >= 5 ? 393 : 333)
CS:writeString(Chevron >= 5 & Inbound == 0 & DialAdr:length() == 6 ? DialAdr:index(5) : Chevron >= 5 ? "5" : " ",29,10,999,0)

CS:writeString(" ",28,11,0,Chevron >= 6 ? 393 : 333)
CS:writeString(Chevron >= 6 & Inbound == 0 & DialAdr:length() == 6 ? DialAdr:index(6) : Chevron >= 6 ? "6" : " ",29,11,999,0)

CS:writeString(" ",28,12,0,Chevron >= 7 ? 393 : 333)
CS:writeString(Chevron >= 7 ? "#" : " ",29,12,999,0)


CS:writeString("  ",28,13,0,555)


CS:writeString("                       ",4,14,0)
if(Inbound == 1)
{
    CS:writeString("> INCOMING CONNECTION <",4,14,933)
}
elseif(Dialing >= 1)
{
    CS:writeString("> PROCESSING <",7,14,993)
}
elseif(Active == 1 & Open == 0)
{
    CS:writeString("> DIALING <",9,14,993)
}
elseif(Active == 1 & Open == 1)
{

    CS:writeString("> CONNECTED <",8,14,393)
}
else
{
    CS:writeString("> DISCONNECTED <",7,14,933)
}

#Keyboard:
if(KB == 18 & MenuSelector < 7 & OldKB != KB)
{
    MenuSelector += 1
}
elseif(KB == 18 & MenuSelector == 7 & OldKB != KB)
{
    MenuSelector = 1
}

if(KB == 17 & MenuSelector > 1 & OldKB != KB)
{
    MenuSelector -= 1
}
elseif(KB == 17 & MenuSelector == 1 & OldKB != KB)
{
    MenuSelector = 7
}

if(KB == 13 & OldKB != KB)
{
    if(MenuSelector == 1)
    {
        DisableAutoClose = !DisableAutoClose
    }
    if(MenuSelector == 2)
    {
        DialMode = !DialMode
    }
    if(MenuSelector == 3)
    {
        Iris = !Iris
    }
    if(MenuSelector == 7)
    {
        if(Active == 1)
        {
            Close = 1
        }
        if(Dialing == 0 & Active == 0 & DialAdr:length() == 6)
        {
            Dialing = 1
        }
    }
}

if(KB != 127 & KB != 154 & KB != 17 & KB != 18 & KB !=13 & DialAdr:length() < 6 & OldKB != KB & MenuSelector == 7 & Dialing == 0 & Active == 0)
{
    if(!DialAdr:find(toChar(KB):upper()))
    {
        DialAdr += toChar(KB):upper()
    }
}

if(KB == 127 & OldKB != KB & MenuSelector == 7 & Dialing == 0 & Active == 0)
{
    DialAdr = DialAdr:left(DialAdr:length() - 1)
}

OldKB = KB

if(Dialing == 1)
{
    DialAddress = 0
    Dialing = 2
}
elseif(Dialing == 2)
{
    DialAddress = toByte(DialAdr:index(1))
    Dialing = 3
}
elseif(Dialing == 3)
{
    DialAddress = 0
    Dialing = 4
}
elseif(Dialing == 4)
{
    DialAddress = toByte(DialAdr:index(2))
    Dialing = 5
}
elseif(Dialing == 5)
{
    DialAddress = 0
    Dialing = 6
}
elseif(Dialing == 6)
{
    DialAddress = toByte(DialAdr:index(3))
    Dialing = 7
}
elseif(Dialing == 7)
{
    DialAddress = 0
    Dialing = 8
}
elseif(Dialing == 8)
{
    DialAddress = toByte(DialAdr:index(4))
    Dialing = 9
}
elseif(Dialing == 9)
{
    DialAddress = 0
    Dialing = 10
}
elseif(Dialing == 10)
{
    DialAddress = toByte(DialAdr:index(5))
    Dialing = 11
}
elseif(Dialing == 11)
{
    DialAddress = 0
    Dialing = 12
}
elseif(Dialing == 12)
{
    DialAddress = toByte(DialAdr:index(6))
    Dialing = 13
}
elseif(Dialing == 13)
{
    DialAddress = 0
    Dialing = 14
}
elseif(Dialing == 14)
{
    DialAddress = -1
    Dialing = 15
}
elseif(Dialing == 15)
{
    DialAddress = 0
    Dialing = 16
}
elseif(Dialing == 16)
{
    DialAddress = 13
    Dialing = 0
}

