// Create a new FTPClient object
var ftp = new FTPClient();

// Grab references to all the form elements
var serverText = document.getElementById("Server");
var userNameText = document.getElementById("UserName");
var passwordText = document.getElementById("Password");
var serverDirText = document.getElementById("ServerDir");
var dirList = document.getElementById("DirList");

// This function is called when the 'Get Directory' button is pressed.
function listDirectory()
{
    // Validate the text-boxes.
    if (serverText.value==="" || userNameText.value==="" 
        || passwordText.value==="" || serverDirText.value==="")
    {
        alert("Please enter all fields.");
        return;
    }

    // Set up event-handlers.  
    // These are called when FTPClient completes particular operations.
    ftp.onInitialize = onInitialize;             // called when initialization is complete
    ftp.onConnect = onConnect;                   // called when a connection has been made (or failed)
    ftp.onDisconnect = onDisconnect;             // called when a connection has been closed
    ftp.onChangeDirectory = onChangeDirectory;   // called when the working directory has changed
    ftp.onDirectoryList = onDirectoryList;       // called when a directory listing has been retrieved
    ftp.onError = onError;                       // called when an error occurs

    // Set connection parameters.    
    ftp.remoteHost = serverText.value;
    ftp.userName = userNameText.value;
    ftp.password = passwordText.value;
    
    // if we haven't yet initialized then do so, otherwise connect.
    setStatus("Initializing");
    if (!ftp.isInitialized())
    {
        ftp.initialize("IntegralFTP.jar", true, "DEBUG", false);
        // the function, onInitialize, will called when initialization is complete
    }
    else
    {
        ftp.connect();
        // the function, onConnect, will called when the connection attempt is complete
    }
}

// Called when initialization is complete
function onInitialize(status)
{
    setStatus("Connecting");
    ftp.connect();
    // onInitialize will called when a connection has been made (or failed)
}

// Called when a connection has been made (or failed)
function onConnect(status)
{
    if (status.success)
    {
        setStatus("Changing directory");
        ftp.changeDirectory(serverDirText.value);
        // onChangeDirectory will called when the working directory has changed
    }
    else
    {
        setStatus("Could not connect.");
    }
}

// Called when the working directory has changed
function onChangeDirectory(status)
{
    if (status.success)
    {
        setStatus("Getting directory listing");
        ftp.directoryList();
        // onDirectoryList will be called when a directory listing has been retrieved
    }
    else
    {
        setStatus("Could not change directory.");
    }
}

// Called when a directory listing has been retrieved
function onDirectoryList(status, dirPath, fileList)
{
    if (status.success)
    {
        setStatus("Directory listing received");
        while (dirList.length>0) 
        {
            dirList.remove(0);
        }
        for (i in fileList.files) 
        {
            var file = fileList.files[i];
            var option = new Option();
            if (file.isDirectory) 
            {
                option.text = "<dir> " + file.name;
            } else {
                option.text = file.name;
            }
            option.value = file.path!==null ? file.path : file.name;
            try 
            {
                dirList.add(option, null);  // JavaScript
            } 
            catch(ex) 
            {
                dirList.add(option);  // JScript
            }
        }
    }
    else
    {
        setStatus("Could not get directory listing.");
    }
    
    // onDisconnect will be called when a connection has been closed
    ftp.disconnect();
}

// Called when a connection has been closed
function onDisconnect(status)
{
    setStatus("Disconnected");
}

// Called when an error occurs
function onError(status)
{
    alert("Error: " + status.errorMessage);
}

// Updates the text in the status text-box
function setStatus(text)
{
    var statusText = document.getElementById("Status");
    statusText.value = text;
}

