var Geoscape = Geoscape ? Geoscape : {
    GIS : {}
};

Geoscape.GIS.Auth = function() {
	
	// namespace privates
	var private_var;
	function private_method() {
		// do stuff here
	}
	
	// Default page login handlers
	function handleLoginSuccess(redirectPage) {
	    window.open(redirectPage, "_self");
	}
		
	function handleLoginFailure(errMsgBoxId, errMsg) {
	    //jQuery(errMsgBoxId).text(errMsg);
	    Geoscape.GIS.Util.ShowModalDialog("Geoscape", errMsg, true);
	}
	
	function validateCredentials(credentials){
	    // TODO: implement validation	
	    if(credentials == null) {
	        return "1";
	    }    
	    
	    if (credentials.username == null || credentials.username.length == 0)
	    {
	        return Geoscape.GIS.Lang.GetErrorString("51") + ": <b>" + document.getElementById("0020").innerHTML + "</b>";
	    }
	    
	    if (credentials.passwd == null || credentials.passwd.length == 0)
	    {
		    return Geoscape.GIS.Lang.GetErrorString("51") + ": <b>" + document.getElementById("0021").innerHTML + "</b>";
	    }  
	    	    
	    return null;
	}
	
		
	
	// namespace publics
	return {
	    // Namespace objects
	    Credentials : {
	        /* 
	        username : null,
	        passwd : null,
	        referrer : null
	        */
	    },
	    
	    LoginConfig : {
	       /*
	        redirectUrl : null,
	        errorBoxId : null,
	        onLoginSuccess : null,
	        onLoginFailed : null
	        */
	    },
	    
	    mainFocus: function() {
        jQuery("#username").focus();
        var lastUsr = Geoscape.GIS.Util.GetCurrentUser();
        if (lastUsr)
            {
            jQuery("#username").val(lastUsr);
            if (jQuery("#username").val().length > 0) jQuery("#passwd").focus();
            }
	    },
	    
	    // Namespace static functions	    
		login : function(credentials, loginConfig) {
		
		    // Validate username and password before sending to server
		    var error = validateCredentials(credentials);
		    
		    if(error != null) {
		        handleLoginFailure("#" + loginConfig.errorBoxId, error);
		        return;
		    }
		    		    
		    // Send login request to server
		    Geoscape.GIS.Core.postJson(
		                     "http://gis.geoscape.eu/aaservices/login.gis4", 
		                      credentials,
		                      function(){
                                 handleLoginSuccess(loginConfig.redirectUrl);
                              }, 
		                      function(errorCode){
		                        errMsg = Geoscape.GIS.Lang.GetErrorString(errorCode);
                                handleLoginFailure("#" + loginConfig.errorBoxId, errMsg)
                              }
		                     );
		}
	};
}();

jQuery(document).ready(function() {
    // Set focus on username field
    Geoscape.GIS.Auth.mainFocus();
    
    // Handle login click
    jQuery("#btnLogin").click(function() {
         Geoscape.GIS.Util.StartBlinds();
         Geoscape.GIS.Auth.login({
                          username : jQuery("#username").val(), 
                          passwd : jQuery("#passwd").val(), 
                          referrer : document.referrer
                         }, 
                         {
                          redirectUrl : "/aapages/index.htm",
                          errorBoxId : "errormsg"
                         });
    });

jQuery(document).keypress(function(e) {

	keyCode = e.which ? e.which : e.keyCode;
    
    if (e.keyCode == 13)
    {
        if ((jQuery("#username").val().length > 0) && (jQuery("#passwd").val().length == 0)) jQuery("#passwd").focus();
        
        else if ((jQuery("#passwd").val().length >= 0) && (jQuery("#username").val().length == 0)) jQuery("#username").focus();
        
        else
	    {
	        Geoscape.GIS.Util.StartBlinds();
	        Geoscape.GIS.Auth.login({
                                        username : jQuery("#username").val(), 
                                        passwd : jQuery("#passwd").val(), 
                                        referrer : document.referrer
                                    },
                                    {
                                        redirectUrl : "/aapages/index.htm",
                                        errorBoxId : "errormsg"
                                    });
        }
    }
});
});