// ******************************************
// * $Id$
// *
// *   connect()
// *   disconnect()
// *
// * Maak een verbinding voor File, FTP, HTTP, HTTPS of Mail.
// * Zet hierbij ook de variabelen:
// *   Connected
// *   ConnectionType
// *   Hostname
// *   Username
// *   Password
// *   Port
// *   Proxy
// *   Subfolder
// *   isFile
// *   isFTP
// *   isHTTP
// *   isHTTPS
// *   isMail
// *
// * Uses: scr="./puo_shared.js"
// *       scr="./puo_settings.js"
// *       scr="./CrackURL.js"
// *       scr="../wsf_shared.js"
// *
// ******************************************
// puo_connector.js
//
function ConnectorCls()
{
    // Private variabelen
    var CONNECTION_NONE  = 0;
    var CONNECTION_FILE  = 1;
    var CONNECTION_FTP   = 2;
    var CONNECTION_HTTP  = 3;
    var CONNECTION_HTTPS = 4;
    var CONNECTION_MAIL  = 5;

    // Public variabelen
    this.Connected = false;
    this.isFile    = false;
    this.isFTP     = false;
    this.isHTTP    = false;
    this.isHTTPS   = false;
    this.isMail    = false;
    this.ConnectionType = CONNECTION_NONE;
    this.CurrentAddress = "";

    // Public functies
    this.connect    = _connect;
    this.disconnect = _disconnect;

    function _connect(p_bedrijfadres) //BEDRIJF_ORDER_ADRES
    {
        // voorbeeld url's:
        //   file:   url = "d:\\websites\\default web site\\facilitor5iwork\\cust\\xxxx\\flexfiles\\" DEPRECATED!
        //   ftp:    url = "ftp://example.com/folder/"
        //   mail:   url = "mailto:helpdesk@facilitor.nl"
        //   http:   url = "http://sgf12/facilitor5iwork/appl/exp/exp_soap.asp"
        //   Let op: username/password moet je

        this.url = p_bedrijfadres.url;
        try
        {
            this.disconnect();
            Log2File(1, "Connecting to " + this.url + "...");

            if (S("puo_forceorderaddress"))
            {
                this.url = S("puo_forceorderaddress");
                Log2File(1, "effective: " + this.url + "...");
            }

            var URLAdres = this.url;
            var URLParts = parseUrl(this.url);
            if (URLParts.user && URLParts.password) // Dit hoop ik met FSN#34131 weer te kunnen verwijderen
            {
                p_bedrijfadres.username = URLParts.user;
                p_bedrijfadres.password = URLParts.password;
                URLAdres = URLAdres.replace(URLParts.user + ":" + URLParts.password + "@", ""); // strip die er uit
            }
            //
            if (!URLAdres)
            {
                this.ConnectionType = CONNECTION_NONE;
            }
            else
            {
                if (!URLParts.protocol && this.url.indexOf("@") > 0)
                {
                    this.url = "mailto:" + this.url;
                    URLParts.protocol = "mailto";
                    Log2File(1, "Configuration error: missing protocol in url. Assuming mailto:");
                }
                switch (URLParts.protocol.toLowerCase())
                {
                    case "mailto":
                        {
                            this.ConnectionType = CONNECTION_MAIL;
                            // Let op dat mailto:jan@example.com;klaas@example.com goed blijft gaan!
                            this.mailto = this.url.split(":")[1]; // alles achter de mailto
                            //
                            Log2File(2, "protocol: mailto");
                            this.Connected = true;
                            if (S("puo_lcl_friendly_mailto"))
                                this.friendlyurl = S("puo_lcl_friendly_mailto").format(this.mailto);
                            break;
                        }
                    case "http":
                        {
                            this.ConnectionType = CONNECTION_HTTP;
                            //
                            Log2File(2, "protocol: http");
                            this.Connected = true;
                            if (S("puo_lcl_friendly_http"))
                                this.friendlyurl = S("puo_lcl_friendly_http").format(URLParts.protocol + "://" + URLParts.host);
                            break;
                        }
                    case "https":
                        {
                            this.ConnectionType = CONNECTION_HTTPS;
                            //
                            Log2File(2, "protocol: https");
                            this.Connected = true;
                            if (S("puo_lcl_friendly_http"))
                                this.friendlyurl = S("puo_lcl_friendly_http").format(URLParts.protocol + "://" + URLParts.host);
                            break;
                        }
                    case "ftp":
                        {   // voorbeeld: "ftp://example.com/folder/"
                            this.ConnectionType = CONNECTION_FTP;
                            Log2File(2, "Creating ftp connector...");
                            var about = new ActiveXObject("SLNKDWF.About");
                            Log2File(2, "Geïnstalleerde SLNKDWF Versie: " + about.VersionString);
                            Log2File(2, about.All);
                            //
                            this.ftpconnection           = new ActiveXObject("SLNKDWF.FTP");
                            this.ftpconnection.Hostname  = URLParts.host;
                            this.ftpconnection.Username  = p_bedrijfadres.username;
                            this.ftpconnection.Password  = p_bedrijfadres.password;
                            this.ftpconnection.Port      = URLParts.port || 21;
                            this.ftpconnection.Proxyname = (S("puo_useproxy") ? S("puo_proxyserveripaddress") : "");
                            this.Subfolder               = URLParts.directory;
                            this.ftpconnection.Flags     = S("puo_connectionflag")||0;
                            //
                            Log2File(2, "protocol: ftp");
                            Log2File(2, "FTPServer: "   + this.ftpconnection.Hostname);
                            Log2File(2, "FTPPort: "     + this.ftpconnection.Port);
                            Log2File(2, "FTPProxy: "    + this.ftpconnection.Proxyname);
                            Log2File(2, "FTPDir: "      + this.Subfolder);
                            Log2File(2, "Flags: "       + this.ftpconnection.Flags);
                            //
                            if (S("puo_lcl_friendly_ftp"))
                                this.friendlyurl = S("puo_lcl_friendly_ftp").format(URLParts.host, URLParts.directory, URLParts.port);
                            try
                            {
                                // Probeer nu de FTP verbinding tot stand te brengen.
                                this.ftpconnection.Open();
                                // Change FTP dir
                                if (URLParts.directory)
                                {
                                    try
                                    {
                                        this.ftpconnection.SetCurrentDir(URLParts.directory);
                                        this.Connected = true;
                                    }
                                    catch(err)
                                    {
                                        Log2File(0, "Error in changing directory to: " + URLParts.directory);
                                    }
                                }
                                else
                                {
                                    this.Connected = true;
                                }
                            }
                            catch(err)
                            {
                                Log2File(0, "Error in InternetOpen: " + err.message);
                            }
                            break;
                        }
                    default:
                        {
                            if (!URLAdres.match(/\\\$/))
                            {
                                URLAdres = URLAdres + "\\";
                            }
                            var fso = new ActiveXObject("Scripting.FileSystemObject");
                            if (fso.FolderExists(URLAdres))
                            {
                                this.Connected = true;
                                if (S("puo_lcl_friendly_file"))
                                    this.friendlyurl = S("puo_lcl_friendly_file").format(URLAdres);
                            }

                            this.ConnectionType = (this.Connected ? CONNECTION_FILE : CONNECTION_NONE);
                            Log2File(2, "protocol: " + (this.Connected ? "file" : "none"));
                        }
                } // switch
            }
        }
        catch(err)
        {
            Log2File(0, "Error connecting to " + this.url + ": " + err.message);
        }
        finally
        {
            if (this.Connected)
            {
                this.CurrentAddress = URLAdres;
                this.isFile  = (this.ConnectionType == CONNECTION_FILE);
                this.isFTP   = (this.ConnectionType == CONNECTION_FTP);
                this.isHTTP  = (this.ConnectionType == CONNECTION_HTTP);
                this.isHTTPS = (this.ConnectionType == CONNECTION_HTTPS);
                this.isMail  = (this.ConnectionType == CONNECTION_MAIL);
            }
        }
    }

    function _disconnect()
    {
        if (this.ftpconnection)
        {
            try
            {
                Log2File(2, "Disconnect...");
                this.ftpconnection.Close(); // Let op: effectief sluit deze alleen de SLNKDWF.FTP verbinding.
                this.Connected = false;
            }
            catch(err)
            {
                Log2File(0, "Error during disconnection: " + err.message);
            }
        }
    }

}
