var getProfessionsURL =  siteUrl + 'getInsitutionProfession';
var signupURL =  siteUrl + 'signup';
var loginUrl = siteUrl + 'login';

function signup(email){
	var popup;
	var spot= new Ext.Spotlight({
			        easing: 'easeOut',
			        duration: .3
			    });;
	Ext.QuickTips.init();
	Ext.form.Field.prototype.msgTarget = 'under';

		var professionReader = new Ext.data.XmlReader({record: 'nameValue'}, ['value', 'name']);
  	
  	var professionStore = new Ext.data.Store({
  	    					url: getProfessionsURL,
  	        	 reader: professionReader,
  	        	 fields: ['value', 'name']
  						});
  	
  	var professionCombo = new Ext.form.ComboBox({
  	    store: professionStore,
  	    fieldLabel: 'Profession',
  	    displayField:'name',
  	    valueField:'value',
  	    hiddenName:'profession',
  	    allowBlank : false,
				mode: 'local',
  	    triggerAction: 'all',
  	    emptyText:'Select your profession',
  	    selectOnFocus:true,
  	    width:180,
  	    editable:false,
  	    listeners:{
  	    	'select':function(e){
  	    			specialtyCombo.reset();
  	    			professionId = e.value;
  	    			specialtyStore.load({params:{'method':'specialty','professionID':professionId}});
  	    		}
  	    }
  	});
  	
		var specialtyReader = new Ext.data.XmlReader({record: 'nameValue'}, ['value', 'name']);
  	
  	var specialtyStore = new Ext.data.Store({
  	    					url: getProfessionsURL,
  	        	 reader: specialtyReader,
  	        	 fields: ['value', 'name']
  						});
  	
  	var specialtyCombo = new Ext.form.ComboBox({
  	    store: specialtyStore,
  	    fieldLabel: 'Specialty',
  	    displayField:'name',
  	    valueField:'name',
  	    hiddenName:'specialty',
  	    allowBlank : false,
				mode: 'local',
  	    triggerAction: 'all',
  	    emptyText:'Select your specialty',
  	    selectOnFocus:true,
  	    width:180,
  	    editable:false
  	});
  	
		popup = new Ext.Window({
			id: 'popup',
			width: 480,
			height: 400,
			title: 'New Member Registration', 
			layout:'absolute',
    	resizable: false,
			hideBorders : false,
			closable: false,
			shadow: true,
			shadowOffset: 10,
			items:[
					new Ext.form.FormPanel({
					id:'signupForm',
  	      labelWidth: 160,
  	      labelAlign: 'right',
  	      border: false,
					x:20,
					y:20,
  	      width: 350,
  	      defaults: {width: 250},
  	      defaultType: 'textfield',
  	      height:380,
					autoWidth:true,
					timeout:6,
					errorReader: new Ext.form.XmlErrorReader(),
					url:loginUrl,
  	      items: [
  	      		{
  	              fieldLabel: 'First Name',
  	              name: 'firstname',
  	              maxLength:50,
  	              allowBlank:false
  	          },{
  	              fieldLabel: 'Last Name',
  	              name: 'lastname',
  	              maxLength:50,
  	              allowBlank:false
  	          },{
  	              fieldLabel: 'Email',
  	              name: 'email',
  	              maxLength:64,
  	              vtype:'email',
  	              id:'email',
  	              allowBlank:false
  	          }, {
  	          		fieldLabel:'Password',
			            name:'password',
  	  		        inputType: 'password',
  	              maxLength:10,
  	      		    vtype:'passwordStrength',
			            id: 'pass',
  	  		        allowBlank:false
  	      		}, {
  	  		        fieldLabel:'Confirm Password',
  	      		    name:'password2',
  	          		inputType: 'password',
  	              maxLength:10,
  	  		        vtype:'password',
  	      		    initialPassField: 'pass',
  	          		allowBlank:false
  	      		},new Ext.form.ComboBox({
  	                      fieldLabel: 'Site',
  	      								editable:false,
  	                      hiddenName:'organizationGeoDiv',
  	                      store: lwhaSiteStore,
  	                      valueField:'site',
  	                      displayField:'site',
  	                      mode: 'local',
  	                      triggerAction: 'all',
  	                      emptyText:'--',
  	                      selectOnFocus:true,
  	          						allowBlank:false
  	          }),{
  	  		        fieldLabel:'Department',
  	      		    name:'department',
  	              maxLength:64
  	      		}, {
  	  		        fieldLabel:'Manager Email Address',
  	      		    name:'managerEmpID',
  	              vtype:'email',
  	              maxLength:64,
  	          		allowBlank:false
  	      		},{
  	  		        fieldLabel:'Job Title(Optional)',
  	      		    name:'jobTitle',
  	              maxLength:64
  	      		}, new Ext.form.RadioGroup(
    						 {id:'genderRadioGroup',
                	defaultType: 'radio',
                	fieldLabel: 'Gender',
  	          		allowBlank:false,
    	            height : 60,
									items:[
    								{
                        boxLabel: 'Male',
	                      name: 'gender',
  	                    id:'male',
    	                  inputValue: 'male'
                   	},{
                        boxLabel: 'Female',
            	          name: 'gender',
              	        id:'female',
                	      inputValue: 'female'
                  		}
                  	]
                 })
  	      	]
  	
  	  })],
  	  buttons:[{
					text: 'Submit',
  	      handler: function(){
  	          Ext.ComponentMgr.get('signupForm').getForm().submit({
																					url:signupURL,
																					waitMsg:'Saving User Data...',
  	                                      success: function(f,a){
																						var lf = document.loginForm;
																						var sf = Ext.ComponentMgr.get('signupForm').getForm().getValues();
																						
																						lf.email.value = sf.email;
																						lf.password.value = sf.password;
																						lf.submit();
  	                                      },
  	                                      failure: function(r,a){
  	                                      	var errorID = a.result.errors[0].id;
  	                                      	if(errorID == 'system'){
  	                                      		Ext.Msg.alert('Registration Failed', a.result.errors[0].msg);
  	                                      		closeWindow();
  	                                      	}else{
  	                                      		Ext.Msg.alert('Registration Failed', 'Please check your input');
  	                                      	}
  	                                      }
  	                                      });
  	      }
			},
			{
				text:"Cancel",
				handler: closeWindow
			}
			]

  	});
  	if(email != null){
      Ext.ComponentMgr.get('email').value = email;
    }
  	popup.show(document.body);
    spot.show('popup');

	
	function closeWindow(){
					popup.close();
					spot.destroy();
  }
};