/**
* Auto-login Class
* Written By Augusto Mathias Adams
* This class is intended for checking if the user exists in the ajax_im database and fill the username field in the login form
* If not, put into the registration form.
**/

var registeredUsername = null;

var registeredPasword = null;

var isRegistered = false;

var AutoLogin = {
    
	/**
         *Checks if the user is in the ajaxim database
         *If yes, put it in the login form, fill the user
	**/
        
        state: 'defaultLogin',
        
	isUser: function(username) {
		
		var xhConn = new XHConn();
                
		xhConn.connect(pingTo, "POST", "call=autologin&username="+username,
                
		function(xh) {
                        
                        registeredUsername = username;
                        
                        registerPassword = '';
                        
                        if(xh.responseText=='yes'){
                                
                                isRegistered = true;
                                
                                //user registered
                                
                                switch(AutoLogin.state){
                                        
                                        case 'specialLogin':
                                                
                                                $('specialuserregisterednotice').innerHTML = Languages.get('specialUserRegisteredNotice').replace('%',registeredUsername);
                                                
                                                $('specialuseranonymousnotice').innerHTML = Languages.get('specialAnonymousNotice');
                                                
                                                Dialogs.specialLink();
                                                
                                                break;
                                                
                                        case 'registeredUser':
                                                
                                                Dialogs.userIsRegistered();
                                                
                                                break;
                                        default:
                                                
                                                Dialogs.login();
                                        
                                }
                                
                        }else{
                                
                                isRegistered = false;
                                
                                if(AutoLogin.state!=='specialLogin'){
                                        
                                        Dialogs.userIsAnonymous();
                                        
                                }else{
                                        
                                        Dialogs.login();
                                        
                                }
                                
                        }
                        
		}
                
		);
	},
        
        /*
        Helper function to get the browser uri parameters
        */
        
        getBrowserURI: function(){
            
            var location = document.location;
            
            // Build an empty URL structure in which we will store
            // the individual query values by key.
            
            var objURL = new Object();
             
            // Use the String::replace method to iterate over each
            
            // name-value pair in the query string. Location.search
            
            // gives us the query string (if it exists).
            
            window.location.search.replace(
                
            new RegExp( "([^?=&]+)(=([^&]*))?", "g" ),
                // For each matched query string pair, add that
                // pair to the URL struct using the pre-equals
                // value as the key.
                function( $0, $1, $2, $3 ){
                    objURL[ $1 ] = $3;
                }
                
            );
            
            return objURL;
            
        },
        
        /*
        
        */
        
        doInit: function(){
                
                AutoLogin.objURL = new Object();
                
                AutoLogin.objURL = AutoLogin.getBrowserURI();
                
                AutoLogin.state = 'defaultLogin';
                
                if(!isNull(AutoLogin.objURL['username'])){
                	
						/*----start display gg return link----*/
						$('return_to_gg').show();
						/*----end display gg return link----*/
                        
                        AutoLogin.state = 'specialLogin';
                        
                        isRegistered = false;
                        
                        setTimeout("try{AutoLogin.isUser('"+AutoLogin.objURL['username']+"');}catch(e){}",1000);
                        
                }else{
                        
                        setTimeout("try{AutoLogin.reset();}catch(e){}",1000);
                        
                }
            
        },
        
        /*
        callback to the dialogs login function
        */
        
        login: function(){
                
                AutoLogin.state = 'registeredUser';
                
                var username = registeredUsername;
                
                var password = $('registeredpassword').value;
                
                var isregistered = 'y';
                
                System.login(username,password,isregistered);
                
        },
        
        specialLogin: function(){
                
                AutoLogin.state = 'specialLogin';
                
                var username = registeredUsername;
                
                var password = $('specialpassword').value;
                
                var isregistered = 'y';
                
                System.login(username,password,isregistered);
                
        },
        
        anonymousLogin: function(){
                
                AutoLogin.state = 'anonymousUser';
                
                AutoLogin.objURL = new Object();
                
                var username = registeredUsername;
                
                var password = 'none';
                
                var isregistered = 'n';
                
                System.login(username,password,isregistered);
                
                
        },
        
        specialAnonymousLogin: function(){
                
                AutoLogin.state = 'registeredUser';
                
                var username = $('specialanonymouslogin').value;
                
                if(username.length >=3 && username.length <=16){
                        
                        AutoLogin.isUser(username);
                        
                }
                
                
        },
        
        userCheck: function(){
                
                AutoLogin.state = 'registeredUser';
                
                var username = $('defaultusername').value;
                
                if(username.length >= 3 && username.length <= 16){
                        
                        AutoLogin.isUser(username);
                        
                }
                
        },
        
        reset: function(){
                
                AutoLogin.state = 'defaultLogin';
                
                registeredUsername = null;
                
                Dialogs.login();
                
        }

};