package modules.login
{
	
	import classes.fl.motion.easing.*;
	import classes.greensock.*;
	import classes.greensock.plugins.*;
	import classes.matuto.core.*;
	import classes.matuto.core.module.*;
	import classes.matuto.events.*;
	import classes.matuto.interfaces.*;
	import classes.matuto.model.*;
	import classes.matuto.model.Model;
	import classes.matuto.mtween.textinho.*;
	import classes.matuto.ui.*;
	import classes.matuto.utils.display.*;
	import classes.matuto.utils.model.*;
	import classes.matutoDesktop.view.BaseView;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.BlurFilter;
	import flash.net.URLVariables;
	import genericos.APP;
	import genericos.Mask;
	import modules.menu.Menu;
	import classes.fl.motion.easing.Exponential;
	import classes.greensock.TweenMax;
	import classes.greensock.plugins.*;
	import classes.matutoDesktop.core.Core;
	import classes.matutoDesktop.view.BaseView;
	
	import flash.events.MouseEvent;
	import flash.utils.setTimeout;
	
	import genericos.ModelApp;
	
	import modules.login.Login;
	import modules.menu.Menu;	
	
	

	//Matuto

	public class Login extends BaseView 
	{
		
		private var model:ModelApp = new ModelApp();

		private var users		:XMLList;
		private var form		:Form;
	
		public function Login() 
		{
			super();
		}
		
		public function init():void 
		{
			
			_login.visible = true;
			_recuperarSenha.visible = false;
			_recuperarSenha.login = _login;
			
			alpha = 0;
			y = 200;
			filters = [new BlurFilter(0, 20)];
			
			width  = Main.INSTANCE.stage.stageWidth;
			height = Main.INSTANCE.stage.stageHeight;
			
			
			
			TweenMax.to(this,.7,{autoAlpha:1,y:0,blurFilter:{blurY:0},ease:Exponential.easeInOut});
			
		
				form = new Form("contato","Enviado com sucesso","enviando mensagem");	
				form.addField("CPF",_login._user,"CPF",Form.CPF);
				form.addField("SENHA", _login._password, "SENHA", Form.ALL,null,14);
				new Mask(_login._user, "999.999.999-99");
				_login._user.text = "CPF"
				//new Mask( _login._password, "999999");
				form.addEventListener(FormEvent.STATUS, status);
				_login._enviar.addEventListener(MouseEvent.CLICK, onClick);
				_login._recuperar.addEventListener(MouseEvent.CLICK, onClickRecuperar);
				
				_recuperarSenha.INICIALIZE();

		}
		
		private function onClickRecuperar(e:MouseEvent):void 
		{
			TweenMax.to(_login,1,{autoAlpha:0,ease:Exponential.easeInOut});
	
			_login._recuperar.visible = false;
			_recuperarSenha.init();
		
		}
		
		private function status(e:FormEvent):void
		{
			var resp:String 	= e.params.message;
			_login._report.text 		= resp;
		}
		
		private function onClick(e:MouseEvent):void 
		{
	
		
				
				if(!form.validation())
				return;
	
				_login._report.text = "Autenticando..."
				
				var urlvars		:URLVariables = new URLVariables;
				
				urlvars.login 		=  _login._user.text;
				urlvars.senha 		=  _login._password.text;
				Model.PHP_CONNECT("http://gfv.com.br/santajoana/portalmedico/app/login/login.php",urlvars, onComplete, function(e:Object) { } );	


				/*
				classes.matuto.model.Model.XML_CONNECT(APP.MODEL_LOGIN,function(xml:XML){
					
					users = xml.data;
					
					if(validationModel())
					{
						_login._report.text = "Seja bem-vindo."
						
						model.SAVE_LOCAL("login","ok");
						
						Core.ref.updateLocal(new Menu());

						
					}
					
					//trace("TOTAL: ",users.length());
					//trace("USER: ",users[0].user);
					//trace("PASSWORD: ",users[0].password);
					
				},"ERRO LOGIN");
				
				*/
			
			
		
			
			
		}
		
		public function onComplete(e:Event)
		{
			
			
			try
			{
			
				var data:String = e.currentTarget.data;
				var a:Array  = data.split("&");
				
				
				if (a.length == 1)
				{
					
					_login._report.text = "Usuario invalido."

					
				}else {
					
					//trace(e.currentTarget.data.status);
					//trace(e.currentTarget.data.nome);
					trace(e.currentTarget.data.foto);
					
					model.SAVE_LOCAL("nome", e.currentTarget.data.nome);
					model.SAVE_LOCAL("login","ok");
					Core.ref.updateLocal(new Menu());
					
				}
				
				

			}catch (e:Error)
			{
			   
				_login._report.text = "Usuario invalido";

			}
			
			

			
			
			
			
		}
		
		
	
		
		
		
		
		private function validationModel():Boolean
		{
			
			try
			{
	
			
				var user:XML; 
				
				for (var i:int = 0; i < users.length(); i++) 
				{
					user = users[i];
					if(user.user == _login._user.text && user.password == _login._password.text)
					return true;	
				}
				
				_login._report.text = "Acesso invalido.";
				
				return false;
			
			
			} 
			catch(error:Error) 
			{
				return false;
			}
			
			return false;
			
		}
		
		//######################################################################################//
		//#RECUPERAR
		//######################################################################################//
		
		
		private function initRecuperar()
		{
	
		}
		
		private function closeRecuperar()
		{
			
		}
		
		
		
		//######################################################################################//
		//######################################################################################//
		
		
		
		
		
		
		
		
		public function close():void 
		{
			//setTimeout(Core.ref.updateLocal, 800, new Foto());
		}
		
		public function dispose():void 
		{
			
			
		}
		
		public function resize():void 
		{
			
		}
		
	}//END CLASS

}