Ext.onReady(function() {
	Ext.QuickTips.init();
	document.onkeypress = acentosCancel;
	
	
	function acentosCancel(e){
	     
	       //	
	      // function checkKeycode(e) {
	             var keycode;
	             if (event) keycode = event.keyCode;
	             else if (e) keycode = e.which;
	           // alert(keycode);
	            if(keycode == 180){
	            	//alert("apostrofe");
	            	event.keyCode=0;
	            	
	            }
	            
	             if(keycode == 225 ){//tecla �
	            	 event.keyCode=97; 
	             }
	             if(keycode == 193 ){//tecla A
	            	 event.keyCode=65; 
	             }
	             if(keycode == 233 ){//tecla �
	            	 event.keyCode=101; 
	             }
	             if(keycode == 201 ){//tecla E
	            	 event.keyCode=69; 
	             }
	             if(keycode == 237 ){//tecla i
	            	 event.keyCode=105; 
	             }
	             if(keycode == 205 ){//tecla I
	            	 event.keyCode=73; 
	             }
	             if(keycode == 243 ){//tecla o
	            	 event.keyCode=111; 
	             }
	             if(keycode == 211 ){//tecla O
	            	 event.keyCode=79; 
	             }
	             if(keycode == 250 ){//tecla u
	            	 event.keyCode=117; 
	             }
	             if(keycode == 218 ){//tecla U
	            	 event.keyCode=85; 
	             }
	             
	             
	            	 
	               
	    //  }
	}//ENDCAPTURAR
	
	
	//Definicion de variables Generales para el .js
	var opcionesOcupacion = new Ext.data.SimpleStore({

        fields: ['id', 'nombre'],

        data: [
               ['1', 'Mama'], 
               ['2', 'Papa'],
               ['3', 'Maestro'],
               ['4', 'Psicologo'],
               ['4', 'Pediatra'],
               ['4', 'Otro']
                
                ]

    });
	
	var opcionesMedio = new Ext.data.SimpleStore({

        fields: ['id', 'nombre'],

        data: [
               ['1', 'Google'],
               ['2', 'Yahoo'],
               ['3', 'Facebook'],
               ['4', 'Youtube'],
               ['5', 'Folleto'],
               ['6', 'Revista'],
               ['7', 'Recomendacion'],
               ['8', 'Otro']
                
                ]

    });
	
	


function getDataStore(){
var storeAdd = new Ext.data.JsonStore({
  //autoDestroy: true,
   url: 'ManejadorAjax?accion=getListaPaises',
    //reader: new Ext.data.JsonReader({}, ['id', 'pais'])
    fields: ['id','nombre']


});
return storeAdd;
}


function muestraRegistro(obj){
	var countryStore = getDataStore();
	var stateStore = getDataStore();
	countryStore.load();
	
		
	Ext.apply(Ext.form.VTypes, {   
		  
		password : function(val, field) {   
		                                 if (field.initialPassField) {   
		                                 var login = Ext.getCmp(field.initialPassField); 
		                                  return (val == login.getValue());   
		                                 }   
		                          return true;   
		             },//END function   
		passwordText : 'contrase&ntilde;as no coinciden' //alert if you enter a password that is not the same   
		});
	
	var centerLayout = {
			id: 'center-panel'
		    
		};
	
	var panelRegistro=new Ext.Panel({
 	   title: '&nbsp;',
	   x: 100,
	   y: 70,
	   width: 300,
	   autoHeight: false,
	   frame: true,
	   items:[{
		       xtype: 'fieldset',
		       autoHeight: true,
		       frame: false,
		       border: false,
		       items: [{///////////////      
                    xtype: 'hidden',   
                     name : 'accion',
                     value : 'registrarUsuario' 
                    },{///////////////      
                      xtype: 'hidden',   
                      name : 'idLink',
                      value : 'regRapid' 
                    },{
 		           fieldLabel: 'Nombre*',
  	               name: 'persona.nombre',
  	               xtype:'textfield',
  	               value: '',
  	               width: 100,
  	              // height: 23,
  	               allowBlank:false
	                  // enableKeyEvents: true,
	                  // listeners: {
                              //  keypress : acentosCancel
                            //  }
  	              },{
    		       fieldLabel: 'Apellidos',
     		       xtype: 'textfield',
	       	       name : 'persona.apellidoPaterno',  
      	           width: 100,
      	           allowBlank: true	                   
      	          },{
    		       fieldLabel: 'Correo*',
     		       xtype: 'textfield',
	       	       vtype: 'email',
	       	       name : 'persona.mail',  
	       	       value: obj,
      	           width: 100,
      	           //height: 23,
      	           allowBlank:false
      	            	                   
      	          },{
                       	fieldLabel : 'Edad de tus hijos',
                              xtype: 'textfield',  
                              name : 'persona.edadHijos',   
                              allowBlank:true,   
                              width : 100 
                         },{
                            	fieldLabel : 'ocupaci&oacute;n',
  	                                xtype: 'combo',
  	                                //hiddenName: 'id',
  	                                store: opcionesOcupacion,
  	                                mode: 'local',
  	                                name : 'persona.ocupacion',   
  	                                displayField: 'nombre',
  	                                hiddenName: 'persona.ocupacion',
  	                                typeAhead: true,
  	                                triggerAction: 'all',   
  	                                emptyText: 'Seleccione...',   
  	                                allowBlank:true,   
                                width : 100 
                                
                                 },{
                           fieldLabel : 'Como te enteraste',
	                            xtype: 'combo',
	                            store: opcionesMedio,
	                            mode: 'local',
	                            name : 'persona.medio',   
	                            displayField: 'nombre',
	                            hiddenName: 'persona.medio',
	                            typeAhead: true,
	                            triggerAction: 'all',   
	                            emptyText: 'Seleccione...',   
	                            allowBlank:true,   
                        width : 100 
                            
                          }]
		   
	          }]
	   });
	
	var panelMsg=new Ext.Panel({
		  // title: '&nbsp;',
		  layout: 'absolute',
		   x: 10,
		   y: 100,
		   width: 400,
		   autoHeight: true,
		   frame: true,
		   html: "<font size='2'>Te recomendamos la opci&oacute;n de <a href='javascript:;' onclick='Ext.getCmp(\"panelAccord\").layout.setActiveItem(0);'>crear una cuenta</a> para que tengas acceso a areas que son s&oacute;lo " +
		   		"para usuarios como:  Foros de opini&oacute;n y de consejos, mayores opciones de material de apoyo, " +
		   		"boletines pasados y atenci&oacute;n personalizada.  Gracias por unirte a nuestra familia!</font>"
			   
		          
		   });
	
	
	var formRegRapid = new Ext.FormPanel({ 
        frame : true,
        autoScroll: true,
        layout: 'absolute',
        y: 100,
        width : 'auto',
        height: 325,  
        waitMsgTarget : true, 
        items : [{x: 100,items:panelRegistro},{x: 50,y: 200,items:panelMsg}]
  	         });
		
		var formCategories = new Ext.FormPanel({ 
	        frame : true,
	        autoScroll: false,
	        layout:'column',
	        width : 'auto',
	        height: 325,   
	        waitMsgTarget : true, 
	        items : [{//columna 1
	        	      columnWidth: .5,
	        	      xtype:'fieldset',
	        	      title: 'Log in',
	        	      autoHeight:true,
	        	      items: [{//********** 
	        	               layout:'form',
      	                       defaultType: "textfield",
      	                       //id: 'login',
      	         	           defaults: {allowBlank: false},
      	                       items: [{///////////////      
      	                                xtype: 'hidden',   
      	                                name : 'accion',
      	                                value : 'registrarUsuario' 
      	                              },{///////////////      
        	                            xtype: 'hidden',   
          	                            name : 'idLink',
          	                            value : 'regComp' 
          	                          },{
      		                            fieldLabel: 'Usuario*',
      	            	                name: 'usuario',
      	            	                value: '',
      	            	                allowBlank:false,
      	            	                width: 100
      	            	
                                      },{
                      	                fieldLabel : 'Contrase&ntilde;a*',
                      	                id: 'passwd',
       	                                xtype: 'textfield',
       	                                inputType: 'password',
       	                                name : 'password', 
       	                                value: '',
       	                                allowBlank:false,   
       	                                width : 100 
                                      },{
                      	                fieldLabel : 'Repetir Contrase&ntilde;a*',
       	                                xtype: 'textfield',
       	                                inputType: 'password',
       	                                name : 'rePassTxt', 
       	                                vtype:'password',
       	                                initialPassField: 'passwd',
       	                                value: '',
       	                                allowBlank:false,   
       	                                width : 100
                                      }]////////////////////
	        	             },{
	        	            	 xtype:'fieldset',
	   		        	      title: 'Datos Personales',
	   		        	      autoHeight:true,
	   		        	      items: [{//********** 
	   		        	               layout:'form',
	   	      	                       defaultType: "textfield",
	   	      	                       defaults: {allowBlank: false},
	   	      	                       items: [{///////////////
	   	      		                            fieldLabel: 'Nombre*',
	   	      	            	                name: 'persona.nombre',
	   	      	            	                value: '',
	   	      	            	                //allowBlank:false,
	   	      	            	                width: 100
	   	      	            	
	   	                                      },{
	   	                      	                fieldLabel : '1erApellido*',
	   	       	                                xtype: 'textfield',  
	   	       	                                name : 'persona.apellidoPaterno', 
	   	       	                                value: '',
	   	       	                                //allowBlank:false,   
	   	       	                                width : 100 
	   	                                      },{
		   	                      	            fieldLabel : '2doApellido',
		   	       	                            xtype: 'textfield',  
		   	       	                            name : 'persona.apellidoMaterno',   
		   	       	                            allowBlank:true,   
		   	       	                                width : 100 
		   	                                  },{
	   	                                    	fieldLabel : 'Fecha de nacimiento',
	   	       	                                xtype: 'datefield', 
	   	       	                                format: 'Y-m-d',
	   	       	                                renderer: Ext.util.Format.dateRenderer('Y-m-d'),
	   	       	                                name : 'persona.fechaNacimiento',   
	   	       	                                allowBlank:true,   
	   	       	                                width : 100 
	   	                                      },{
		   	                                    	fieldLabel : 'ocupaci&oacute;n',
		   	       	                                xtype: 'combo',
		   	       	                                //hiddenName: 'id',
		   	       	                                store: opcionesOcupacion,
		   	       	                                mode: 'local',
		   	       	                                name : 'persona.ocupacion',   
		   	       	                                displayField: 'nombre',
		   	       	                                hiddenName: 'persona.ocupacion',
		   	       	                                typeAhead: true,
		   	       	                                triggerAction: 'all',   
		   	       	                                emptyText: 'Seleccione...',   
		   	       	                                allowBlank:true,   
                                                    width : 100 
                                                    
		   	                                      },{
			   	                                    fieldLabel : 'Como te enteraste',
			   	       	                            xtype: 'combo',
			   	       	                            store: opcionesMedio,
			   	       	                            mode: 'local',
			   	       	                            name : 'persona.medio',   
			   	       	                            displayField: 'nombre',
			   	       	                            hiddenName: 'persona.medio',
			   	       	                            typeAhead: true,
			   	       	                            triggerAction: 'all',   
			   	       	                            emptyText: 'Seleccione...',   
			   	       	                            allowBlank:true,   
	                                                width : 100 
	                                                    
			   	                                   },{
			   	                                    	fieldLabel : 'Edad de tus hijos',
			   	       	                                xtype: 'textfield',  
			   	       	                                name : 'persona.edadHijos',   
			   	       	                                allowBlank:true,   
			   	       	                                width : 100 
			   	                                      }]////////////////////
	   		        	             }]//END item fieldset Contacto
	        	             }]//END item fieldset LOG IN
	        	      },{//columna 2
	        	    	  columnWidth: .5,
		        	      xtype:'fieldset',
		        	      title: 'Contacto',
		        	      autoHeight:true,
		        	      items: [{//********** 
		        	               layout:'form',
	      	                       defaultType: "textfield",
	      	                       defaults: {allowBlank: false},
	      	                       items: [{///////////////
	      		                            fieldLabel: 'Telefono 1',
	      		                            xtype: 'textfield',
	      	            	                name: 'persona.telefono',
	      	            	                allowBlank:true,
	      	            	                width: 100,
	      	            	                enableKeyEvents: true,
	      	            	                listeners: {
      	                                                keypress :  function(){
	      	                    	                                    AceptaSoloTeclaNumerica();
	      	                                                                }
	      	                                           }
	      	            	
	                                      },{
		      		                        fieldLabel: 'Telefono 2',
		      		                        xtype: 'textfield',
		      	            	            name: 'persona.telefono2',
		      	                            allowBlank:true,
		      	          	                width: 100,
		      	          	                enableKeyEvents: true,
      	            	                    listeners: {
  	                                                    keypress :  function(){
      	                    	                                           AceptaSoloTeclaNumerica();
      	                                                                      }
      	                                               }
		      	            	
		                                  },{
			      		                    fieldLabel: 'Movil',
			      		                    xtype: 'textfield',
			      	            	        name: 'persona.movil',
			      	                        allowBlank:true,
			      	          	            width: 100,
			      	          	            enableKeyEvents: true,
  	            	                        listeners: {
	                                                    keypress :  function(){
  	                    	                                                  AceptaSoloTeclaNumerica();
  	                                                                          }
  	                                                   }
			      	            	
			                             },{
	                      	                fieldLabel : 'Correo*',
	                      	                xtype: 'textfield',
	       	                                vtype: 'email',
	       	                                name : 'persona.mail',
	       	                                value: obj,
	                      	                width: 100,
	       	                                allowBlank:false  
	       	                                
	                                      }]////////////////////
		        	             },{
		        	            	 
		        	            	
		   		        	      xtype:'fieldset',
		   		        	      title: 'Direcci&oacute;n',
		   		        	      autoHeight:true,
		   		        	      collapsible: true,
		   		        	      animCollapse: true,
		   		        	      collapsed: true,
		   		        	      items: [{
		   		        	    	   layout:'form',
		   		        	    	   id: 'formu',
		      	                       defaultType: "textfield",
		      	                       defaults: {allowBlank: false},
		      	                       items: [{///////////////
		      		                            fieldLabel : 'Pa&iacute;s',
		      		                            //cls: 'inpuText',
	   	       	                                xtype: 'combo',
	   	       	                                store: countryStore,
	   	       	                                mode: 'remote',
	   	       	                                name : 'persona.pais',
	   	       	                                hiddenName: 'persona.pais', 
	   	       	                                displayField: 'nombre',
	   	       	                                valueField: 'id',
	   	       	                                triggerAction: 'all',
	   	       	                                typeAhead: true,
	   	       	                                allowBlank:true,
	   	       	                                emptyText: 'Seleccione...',
	   	       	                                listeners: {
	   	       	                                            select : function(){
		      	                    	                                  var comboDistrict = Ext.getCmp('formu').getComponent('comboboxEstado');
		      	                    	                               
		      	                    	                                  comboDistrict.clearValue();
		      	                    	                                  stateStore.load({params:{country:this.value}})
		      	                    	                                  return false;
		      	                    	                                         }
	   	       	                                
	   	       	                                 },
                                                width : 100,
                                                height: 15
		      	            	
		                                      },{
		                      	                fieldLabel : 'Edo./Provincia',
		                      	                itemId: 'comboboxEstado',
		                      	                xtype: 'combo',
	   	       	                                store: stateStore,
	   	       	                                mode: 'local',
	   	       	                                name : 'persona.estado',
	   	       	                                hiddenName: 'persona.estado', 
	   	       	                                displayField: 'nombre',
	   	       	                                triggerAction: 'all', 
	   	       	                                allowBlank:true,
	   	       	                                emptyText: 'Seleccione...',
	   	       	                                width : 100
		   		        	    	  
		   		        	                },{
		                      	                fieldLabel : 'Calle y numero',
		       	                                xtype: 'textfield',  
		       	                                name : 'persona.calle',   
		       	                                allowBlank:true,   
		       	                                width : 100 
		   		        	    	  
		   		        	                },{
		                      	                fieldLabel : 'Colonia',
		   		        	                    //id: 'colonia',
		                      	                tpl: '<tpl for="."><div class="x-form-text"></div></tpl>',
		       	                                xtype: 'textfield',  
		       	                                name : 'persona.colonia',   
		       	                                allowBlank:true,   
		       	                                width : 100 
		   		        	    	  
		   		        	                },{
		                      	                fieldLabel : 'Delegaci&oacute;n<br>/Municipio',
		       	                                xtype: 'textfield',  
		       	                                name : 'persona.delegacion',   
		       	                                allowBlank:true,   
		       	                                width : 100 
		   		        	    	  
		   		        	                },{
		                      	                fieldLabel : 'Codigo Postal',
		       	                                xtype: 'textfield',  
		       	                                name : 'persona.cpostal',   
		       	                                allowBlank:true,   
		       	                                width : 100 
		   		        	    	  
		   		        	                }] 
		        	            	 
		        	             }]//END item fieldset Contacto
		        	             }]
	        	    	  
	        	    	  
	        	      }]//End 1 Item Columnas    
                    
	    }); //End formcategories
		

		var formPanel = new Ext.Panel({
	        //baseCls: 'x-plain',
	        layout:'accordion',
	        layoutConfig: {animate: true },
	        id: 'panelAccord',
	       // url:'save-form.php',
	       // defaultType: 'textfield',

	        items:[{
	            	title: "<font size='2'><b><i>Crea una cuenta</i></b></font>",
		            id: 'regCompForm',
		            items: formCategories
	           },{
	        	    title: 'Registro r&aacute;pido',
		            id: 'regRapidForm',
		            items: formRegRapid
	           }]

	    });
		
		
		
		var window = new Ext.Window({
	        title: 'Registrate',
	        resizable: false,
	        width: 530,
	        height:450,
	        autoScroll: true,
	        //minWidth: 300,
	       // minHeight: 200,
	        layout: 'fit',
	        plain:true,
	        bodyStyle:'padding:5px;',
	        buttonAlign:'center',
	        items: formPanel,
	        closeAction: 'destroy',

	        buttons: [{
	        	text: 'Enviar',
	        	//handler : submitFormExtjs.createDelegate(this, ['ventanaRegistro', document.usuarioForm, 'registrarUsuario'], false),
	        	handler : function(){
	        	
	    
	        	
	        	
	        	if(formPanel.layout.activeItem.id == 'regRapidForm'){
	        		
	        		formRegRapid.getForm().submit({ 
		        		url : 'usuarioForm.midiat',
		                waitMsg : 'Salvando datos...',   
		                failure: function (form, action) {  
		                           Ext.MessageBox.show({   
		                            title: 'Algunos campos estan vacios o incorrectos',   
		                            msg: 'Para poder registrarte debes corregir la informaci&oacute;n',   
		                            buttons: Ext.MessageBox.OK,   
		                            icon: Ext.MessageBox.ERROR   
		                           });   
		                      },   
		                success: function (form, request) {   
		                    Ext.MessageBox.show({   
		                        title: 'Datos guardados',   
		                        msg: request.result.msg,   
		                        buttons: Ext.MessageBox.OK,   
		                        icon: Ext.MessageBox.INFO   
		                    });  
		                    window.destroy();
		                    
		                   
		                      
		                }   
		            });
	        		
	        		
	        		
	        	}else if(formPanel.layout.activeItem.id == 'regCompForm'){
	        		
	        		
	        	formCategories.getForm().submit({ 
	        		url : 'usuarioForm.midiat',
	                waitMsg : 'Salvando datos...',   
	                failure: function (form, action) {
	                           Ext.MessageBox.show({   
	                            title: 'Algunos campos estan vacios o incorrectos',   
	                            msg: 'Para poder registrarte debes corregir la informaci&oacute;n',   
	                            buttons: Ext.MessageBox.OK,   
	                            icon: Ext.MessageBox.ERROR   
	                           });   
	                      },   
	                success: function (form, request) {   
	                    Ext.MessageBox.show({   
	                        title: 'Datos guardados',   
	                        msg: request.result.msg,   
	                        buttons: Ext.MessageBox.OK,   
	                        icon: Ext.MessageBox.INFO   
	                    }); 
	                   // responseData = Ext.util.JSON.decode(request.response.responseText);   
	                     //alert(request.result.msg); 
	                    window.destroy();
	                    
	                   
	                      
	                }   
	            });
	        	}
	        	
	        }, 
	            scope : this 

	        	  },{
	            text: 'Cancelar',
	            handler: function(){
	              window.destroy();
	        		 // formPanel.layout.setActiveItem(1);
	            }
	        	}]  
	    });

	   
	    
	   /* new Ext.ToolTip({
	        target: 'trackCallout',
	        anchor: 'left',
	        trackMouse: true,
	        html: 'Tracking while you move the mouse'
	    });*/

	    window.show();
	}//END function       
              

function suscripcion(){

	var mailText =document.getElementById("mailTxt");
	var mailTextVal = mailText.value;
	//alert(mailText);
	
	if(mailTextVal=="" || mailText.style.background=="#f7c8c4"){
		Ext.MessageBox.show({   
            title: 'Algunos campos estan vacios o incorrectos',   
            msg: 'Para poder registrarte debes corregir la informaci&oacute;n',   
            buttons: Ext.MessageBox.OK,   
            icon: Ext.MessageBox.ERROR   
           });
		return false;
		
	}
	
	var conn = new Ext.data.Connection();
	   conn.request({
	       url: 'usuarioForm.midiat',
	       method: 'POST',
	       params: { "accion":'registrarUsuario', "idLink":'', "persona.mail": mailTextVal},
	       success: function (responseObject) {
	    	   var res=Ext.util.JSON.decode(responseObject.responseText);
	    	   
	    	   if(res.success){
	    	   
	    	   var window = new Ext.Window({
	   	        title: 'Registro Exitoso!',
	   	        resizable: false,
	   	        width: 350,
	   	        height:220,
	   	        layout: 'fit',
	   	        plain:true,
	   	        bodyStyle:'padding:5px;',
	   	        buttonAlign:'center',
	   	        items: {
	    		   html:"<div style=' background-color: #ccd9e8; height: 150px;'><font size='2'>En breve recibir&aacute;s nuestro pr&oacute;ximo bolet&iacute;n; no olvides revisar tu bandeja de correo no deseado.<br>" +
                    "Te recomendamos la opci&oacute;n de <a href='javascript:;' onclick='muestraRegistro(\""+mailTextVal+"\");'>crear una cuenta</a> para que tengas acceso a areas que son s&oacute;lo " +
		   		     "para usuarios como:  Foros de opini&oacute;n y de consejos, mayores opciones de material de apoyo," +
		   		     "boletines pasados y atenci&oacute;n personalizada.<br>�Gracias por unirte a nuestra familia!</font></div>"
	    	   },
	   	        closeAction: 'hide',

	   	        buttons: [{
	   	        	        text: 'Crear una cuenta',
	   	        	        handler: function(){
	   	        	                  muestraRegistro(mailTextVal);
	   	        	               window.destroy();
	   	        	
	   	                             }
	   	                  },{
	   	        	        text: 'No, gracias.',
	   	        	        handler: function(){
	   	                	         window.destroy();
	   	                	  
	   	                  }
	   	                  }]
	    	     });
	    	   window.show();
	           
	           
	            
	       }else{
	    	   Ext.MessageBox.show({   
                   title: '',   
                   msg: 'El correo que proporcionaste ya se encuentra registrado!', 
                   buttons: Ext.MessageBox.OK,
                   icon: Ext.MessageBox.ERROR   
                  });
	    	   
	       }
	    	   mailText.value="";     
	        },
	        failure: function(form, action) {
	    	   Ext.MessageBox.show({   
                   title: 'Algunos campos estan vacios o incorrectos',   
                   msg: 'Para poder registrarte debes corregir la informaci&oacute;n', 
                   animEl: 'suscBtn',
                   buttons: Ext.MessageBox.OK,
                   icon: Ext.MessageBox.ERROR   
                  });
	        }
	   });
	
}

Ext.get("creaCuentaBtn").on('click', muestraRegistro.createDelegate(this, [''], false));

Ext.get("suscBtn").on('click', suscripcion);
//Ext.get("suscBtn").on('click', suscripcion.createDelegate(this, ['mailTxt'], false));


});	




