
/**
*
*/
IRIS.auth = function() {
	
	return {
		/**
		*
		*/
		init	:	function() {
			var loginScreen = Ext.get("screen");
			
			loginScreen.fadeIn({
			    endOpacity: 1, //can be any value between 0 and 1 (e.g. .5)
			    easing: 'easeOut',
			    duration: 3
			});
			
			Ext.get("login_button").on('click', IRIS.auth.login );
			
			var un_map = new Ext.KeyMap("user_name", {
			    key: Ext.EventObject.ENTER, 
			    fn: IRIS.auth.login
			});
			
			var pw_map = new Ext.KeyMap("password", {
			    key: Ext.EventObject.ENTER, 
			    fn: IRIS.auth.login
			});

			
		},
		
		/**
		*
		*/
		login	:	function() {
			
			var username 	= Ext.get("user_name").getValue();
			var pass 		= Ext.get("password").getValue();
			
			Ext.Ajax.request({
			   url: '/login.php',
			   params: { u: username, p: pass },
			   success: IRIS.auth.handleSuccess
			});
		},
		
		/**
		*
		*/
		logout	:	function() {
			
		},
		
		handleSuccess : function(response) {
		    
		  
			
			
			
			var loginResult = Ext.decode(response.responseText);
			
			if ( loginResult.authorized ) {
				
				
			      //Pasted from green alpine login
		    
                        var startDate = new Date;
							startDate = startDate.format('Y-m') + '-01';
							
						var monthAgo = (new Date).add(Date.DAY, -30);	
							monthAgo = monthAgo.format('Y-m-d');
							
                        var endDate = new Date;
							endDate = endDate.format('Y-m-d');
			
			/*
			var compileBox = Ext.MessageBox.show({
            
            msg: "<b>Please wait</b><br>Alpine is taking a snapshot of your data. This should only take a moment.",
            progressText: "Connecting",
            width: 300,
            height:300,
            progress: true,
            modal: true,
            closable: false,
            animEl: "mb6"
            });
            
           
            var f = function(v){ 
                return function(){
                    if (v == 150) {
                        Ext.MessageBox.hide();
                   }else {
                       var i = v / 150;
                       Ext.MessageBox.updateProgress(i, Math.round(150 * i) + "%");
                   }}};
                for (var i = 1; i < 150; i++) {
                    setTimeout(f(i), i * 1100);
                }
			
			*/
			
				var messageBox = Ext.get("flash_message");
				var ali = "login.php?ali=true&start=" + monthAgo + "&end=" + endDate+"&monthago="+monthAgo;
				
				//alert(ali);
				
				messageBox.update('Login Successful. Loading data...');
			    window.location.href = ali;
            
				
			} else {
			     var messageBox = Ext.get("flash_message");
			     messageBox.update('Invalid Credential. Please try again.');
			}
		}
	};
	
}();