/**
 * External login_auto script useful for Demo purposes
 * @Installation:
 *                      copy this script into webmail directory and change sServerURL and sClientUrl paths below
 * @Date:       4.7.2007 9:04:47
 **/

function oExt_Login (){

        this.sServerURL = 'proxy.php';                          //proxy.php path (or webmail.php path if it is in the same domain)
        this.sClientUrl = 'http://200.211.26.40/webmail/';      //WebClient Pro full URL
        this.sBasicUrl  = 'http://200.211.26.40/webmail/basic/';        //WebClient Basic full URL

        //Automatically switch between protorols
        //this.sClientUrl       = document.location.protocol+'//200.211.26.40/webmail/';           //WebClient Pro full URL
        //this.sBasicUrl        = document.location.protocol+'//200.211.26.40/webmail/basic/';     //WebClient Basic full URL



        //Mozilla & MSIE7+
        if (window.XMLHttpRequest)
                this.oXMLHttp = new XMLHttpRequest();
        else
        //MSIE 6
        if(!navigator.__ice_version && window.ActiveXObject)
                this.oXMLHttp = new ActiveXObject('Microsoft.XMLHTTP');
        else
            alert("Your Browser doesn't support HTTPRequest");
};

oExt_Login.prototype.response = function(sInput){

        var xOutput = null;
        try {
                xOutput = new ActiveXObject('Microsoft.XMLDOM');
                xOutput.async = false;
                xOutput.resolveExternals = false;
                xOutput.validateOnParse = false;
                xOutput.loadXML(sInput);

                if (xOutput.parseError.errorCode)
                        alert(  "Error code: "+ xOutput.parseError.errorCode +
                                        "\nLine: " + xOutput.parseError.line + ':'+ xOutput.parseError.linePos +
                                        "\nReason: "+ xOutput.parseError.reason +
                                        "\n" + xOutput.parseError.srcText);
        }
        catch (e) {
                var xParser = new DOMParser();
                xOutput = xParser.parseFromString(sInput, 'text/xml');
        }

        this.oXMLHttp.open('POST', this.sServerURL, false);
        this.oXMLHttp.send(xOutput);

        return this.oXMLHttp.responseXML;
};

//oExt_Login.prototype.login = function(user,pass,ip,bMode,rsa){
oExt_Login.prototype.login = function(user,pass,ip,bMode,rsa){
        if (!this.oXMLHttp || !user || (!pass && !rsa)) return;


        var tmp,sid;
        try{
                if (rsa)
                        pass = rsa;
                else
                if (!(pass = this.getrsa(user,pass)))
                        throw 'Invalid RSA';

                //SET LOGIN info
                tmp = this.response('<iq type="set"><query xmlns="webmail:iq:auth"><username>'+this.__escape(user)+'</username><digest>'+pass+'</digest><method>RSA</method><ip>'+this.__escape(ip)+'</ip></query></iq>');

                if (!(sid = tmp.getElementsByTagName('iq')[0].getAttribute('sid')))
                        throw 'Invalid Login';

                this.redirect(sid,bMode);
        }
        catch(e){
                alert('Error - '+e);
        }
};

oExt_Login.prototype.__escape = function(str){
        var div = document.createElement('div');
                div.appendChild(document.createTextNode(str));
        return div.innerHTML;
};

oExt_Login.prototype.getrsa = function(user,pass){
                //GET RSA public key
                var tmp = this.response('<iq type="get" format="xml"><query xmlns="webmail:iq:auth"><username>'+this.__escape(user)+'</username><method>RSA</method></query></iq>');
                try{
                        var key = tmp.getElementsByTagName('hashid')[0].firstChild.nodeValue;

                        //Prepare RSA library
                        var rsa = new RSAKey();
                                rsa.setPublic(key, '10001');
                }
                catch(r){
                        return;
                }

                return rsa.encrypt(pass);
};

oExt_Login.prototype.loginform = function(elm){
        var sError = '';
        if (!elm.username.value)
                sError = "USERNAME is blank\n";
        if (!elm.password.value)
                sError = "USERNAME is blank\n";
        if (!elm.ip.value)
                sError = "IP is blank (enable PHP scripting)";

        if (sError)
            alert(sError);
        else
                this.login(elm.username.value,elm.password.value,elm.ip.value,elm.mode.value>0?true:false);

        return false;
};

oExt_Login.prototype.redirect = function(sid,bMode){
        document.location.href = (bMode?this.sClientUrl:this.sBasicUrl)+'?sid='+sid+(document.referrer?'':'&ref='+encodeURIComponent(document.location.protocol+'//'+document.location.hostname+'/'+document.location.pathname));
};

Ext_Login = new oExt_Login();

