package modules.agendamento
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import genericos.*;
	import flash.net.*;
	import modules.menu.Menu;
		//Matuto
	import classes.fl.motion.easing.*;
	import classes.greensock.*;
	import classes.matuto.core.*;
	import classes.matuto.core.module.*;
	import classes.matuto.events.*;
	import classes.matuto.interfaces.*;
	import classes.matuto.model.*;
	import classes.matuto.mtween.textinho.*;
	import classes.matuto.ui.*;
	import classes.matuto.utils.display.*;
	import classes.matuto.utils.model.*;
	import flash.utils.*;
	import com.freshplanet.ane.AirDatePicker;
	
	public class FormAgendamento extends MovieClip
	{
		
		
		private var form		:Form;

		public function FormAgendamento() 
		{

			form = new Form("contato", "Enviado com sucesso", "enviando mensagem");	
			form.addField("nome", _nome, "Seu nome", Form.ALL);
			form.addField("nascimento",_nascimento,"Data de nascimento (99/99/99)", Form.ALL);
			form.addField("sexo",_sexo,"Sexo", Form.STRING);

			form.addField("cirugia",_cirugia,"Tipo de Cirurgia", Form.STRING);
			form.addField("pretendida",_pretendida,"Data Pretendida (99/99/99)", Form.ALL);
			form.addField("horario",_horario,"Horário pretendido", Form.ALL);
		
			form.addField("email", _email, "contato@email.com", Form.MAIL);
			form.addField("ddd", _ddd, "DDD", Form.NUMBER,null,2);
			form.addField("celular", _celular, "Cel.", Form.NUMBER);
			form.addField("observacao",_observacao, "Observações adicionais", Form.ALL);
			
			new Mask(_celular, "99999-9999");
			_celular.text = "Cel."

			new Mask(_nascimento, "99/99/99");
			_nascimento.text = "Data de nascimento (99/99/99)"
	
			new Mask(_pretendida, "99/99/99");
			_pretendida.text = "Data Pretendida (99/99/99)"
			
			form.addEventListener(FormEvent.STATUS, status);
			
			_send.addEventListener(MouseEvent.CLICK, onClick);
			
			_data.addEventListener(MouseEvent.CLICK, clickData);

			
		}
		
		private function clickData(e:MouseEvent):void 
		{
							// Required params
				var currentDate : Date = new Date();
				var callback : Function = function( selectedDate:String ) : void {
					trace("selected date = ", selectedDate.toString());
				}
				// Native extension call
				
				
				AirDatePicker.getInstance().displayDatePicker(currentDate, callback);
		}
		
		private function onClick(e:MouseEvent):void 
		{
			
			if (!form.validation()) {

				Menu.INSTANCE.DIALOG_OK(form.message);
				return;
			}
				
			if (form.validation())
			{
				
				Menu.INSTANCE.DIALOG_SIMPLES("Enviando.");
				var urlvars		:URLVariables = new URLVariables;
				
				urlvars.nome 			=  _nome.text;
				
				urlvars.nascimento 		=_nascimento.text;
				urlvars.sexo 			=_sexo.text;

				urlvars.cirugia 		=_cirugia.text;
				urlvars.pretendida 		=_pretendida.text;
				urlvars.horario 		=_horario.text;
		
				urlvars.email 			=_email.text;
				urlvars.ddd 			=_ddd.text;
				urlvars.celular 		=_celular.text;
				urlvars.observacao 		=_observacao.text;
				
				Model.PHP_CONNECT(APP.MODEL_AGENDAMENTO,urlvars, onComplete, function(e:Object) { } );	
		
				
			}
			
		}
		
		public function onComplete(e:Event)
		{
			
			
			Menu.INSTANCE.simples.click();
			
			if (e.currentTarget.data.status == 1)
			{
				
				Menu.INSTANCE.DIALOG_OK_FORM();
				
				setTimeout(function() {
				
					limparCampos();
					
					if (Menu.INSTANCE.formOk != null)
					Menu.INSTANCE.formOk.click();
					
				}, 3000);
				
			}else {
				
				Menu.INSTANCE.DIALOG_ERRO_FORM();
				
				setTimeout(function() {
				
					if (Menu.INSTANCE.formErro != null)
					Menu.INSTANCE.formErro.click();
					
				}, 3000);
				
			}
			
		}
		
		private function limparCampos()
		{

			 
			_nome.text = "Seu nome";
			_nascimento.text ="Data de nascimento (99/99/99)";
			_sexo.text ="Sexo";

			_cirugia.text = "Tipo de Cirurgia";
			_pretendida.text = "Data Pretendida";
			_horario.text = "Horário pretendido";
		
			_email.text = "contato@email.com";
			_ddd.text = "DDD";
			_celular.text = "Cel.";
			_observacao.text = "Observações adicionais";
			
		}
		
		private function status(e:FormEvent):void
		{
			var resp:String 		= e.params.message;
			
		}
		

		
	}//END CLASS

}