import it.gotoandplay.smartfoxserver.*
 
stop()
 
 
 
//----------------------------------------------------------
// Setup global variables
//----------------------------------------------------------
var stageW:Number = 700
var stageH:Number = 490
 
// hide semi-transparent panel
// used when a dialog box is shown
disabler._visible = false
 
// isBusy is true when the application
// is requiring input through a dialog box
// When true all other controls are temporarily disabled
_global.isBusy = false
 
// An event queue
var evtQueue:Array = []
 
 
//----------------------------------------------------------
// Setup global variables
//----------------------------------------------------------
showLogin(false)
status_txt.text = "Connecting..."
 
 
//----------------------------------------------------------
var ip:String       = "ipserver"
var port:Number     = 9339
var zone:String     = "simpleChat"
 
 
//------------------------------------------------------------------
// NOTE:
// Change this url to your local or online webserver and poin it to
// the webpage that verifies the login and password.
// You will receive 2 POST variables called "login" and "pass"
//
// The server should respond with a "res" variable:
// res=OK (if the user was recognized)
// res=KO (if login failed)
//------------------------------------------------------------------
var serverPage:String   = "login.php"
var serverIn:LoadVars   = new LoadVars()
var serverOut:LoadVars  = new LoadVars()
 
 
var smartfox:SmartFoxClient = new SmartFoxClient()
smartfox.debug = true
 
smartfox.onConnection = handleConnection
smartfox.connect(ip, port)
 
 
//----------------------------------------------------------
// Handle connection response from server
//----------------------------------------------------------
function handleConnection(success)
{
    if (success)
    {
        status_txt.text = "Connected, please login:"
        showLogin(true)
        butt_login.onRelease = validateLogin
    }
    else
    {
        status_txt.text = "Can't connect!"
    }
}
 
//----------------------------------------------------------
// Send login data to the server page
//----------------------------------------------------------
function validateLogin()
{
    if (login_txt.text.length > 0 && pwd_txt.text.length > 0)
    {
        serverOut.name = login_txt.text
        serverOut.pass = pwd_txt.text
        serverOut.sendAndLoad(serverPage, serverIn, "post")
    }
}
 
 
 
//----------------------------------------------------------
// Handle the PHP server login response
//----------------------------------------------------------
serverIn.onLoad = function(success)
{
    if (success)
    {
        if (this.res == "OK")
        {
            sendLogin()
        }
        else
        {
            var win:MovieClip = showWindow("errorWindow")
            win.errorMsg.text = "Wrong name or passord"
        }
    }
    else
    {
        var win:MovieClip = showWindow("errorWindow")
        win.errorMsg.text = "Connection failed"
    }
}
 
 
 
//----------------------------------------------------------
// Send login params to the server
// server.login(zone, nickName, password)
//----------------------------------------------------------
function sendLogin()
{
    if (!_global.isBusy)
        smartfox.login(zone, login_txt.text, pwd_txt.text)
}
 
 
 
//----------------------------------------------------------
// Handle login response from server
//----------------------------------------------------------
smartfox.onLogin = function(resObj:Object)
{
    if (resObj.success)
    {
        // Login Successfull
        _global.myName = resObj.name
    }
    else
    {
        // Login Failed
        _gloabl.isBusy = true
        
        // Show an error window
        var win = showWindow("errorWindow")
        win.errorMsg.text = resObj.error
    }
}
 
//----------------------------------------------------------
// Handle the onRoomListUpdate here and keep it in the
// queue. We'll handle it in the next frame.
//----------------------------------------------------------
smartfox.onRoomListUpdate = function(o:Object)
{
    evtQueue.push(o)
    gotoAndStop("chat")
}
 
 
//----------------------------------------------------------
// Handle unexpected server disconnection
//----------------------------------------------------------
smartfox.onConnectionLost = function()
{
    gotoAndStop("connect")
}
 
 
 
//----------------------------------------------------------
// Show / Hides the login input field and submit button
//----------------------------------------------------------
function showLogin(bool:Boolean)
{
    butt_login._visible = bool
    login_txt._visible = bool
    pwd_txt._visible = bool
    
    if (bool)
        Selection.setFocus("login_txt")
}
 
 
 
//----------------------------------------------------------
// Shows a popup window and disables all other controls
//----------------------------------------------------------
function showWindow(linkageName:String):MovieClip
{
    _global.isBusy = true
    
    userList_lb.setEnabled(false)
    disabler._visible = true
    
    var win = _root.attachMovie(linkageName, linkageName, 9999)
        
    win._x = (stageW / 2) - (win._width / 2)
    win._y = (stageH / 2) - (win._height / 2)
    
    return win
}
 
 
 
//----------------------------------------------------------
// Hides a popup window and re-enable the controls
//----------------------------------------------------------
function hideWindow(wName:String)
{
    this[wName].removeMovieClip()
    disabler._visible = false
    _global.isBusy = false
    
    userList_lb.setEnabled(true)
}