/**
 * @author Ramalingesha ML
 */
Tp.ui.view.user.StudentRegView = Class
		.create( {

			/**
			 * default constructor
			 * 
			 * @param handlers = {
			 *            onProjectSelection: <BR>
			 *            onNewProjectCreation: <BR>}
			 */
			initialize : function(projectConfig, handlers) {
				Ext.QuickTips.init();
				this.config = projectConfig;
				this.handlers = handlers;
				this.uploadedFileId = "";
				this.isFileToBeUploaded = false;
				this.initView();
			},

			initView : function() {
				this.initForm();

				this.candidatePanel = new Ext.Window( {
					layout :'anchor',
					title :'Student Registration',
					modal :true,
					stateful :false,
					closable :true,
					loadMask :true,
					maximizable :false,
					draggable :false,
					resizable :false,
					width :750,
					items : [ this.candidateRegForm ]
				});
				this.candidatePanel.show();
				// this.candidateRegForm.getForm().findField("firstName").focus();
			},

			initForm : function() {
				var localObject = this;

				Ext.apply(Ext.form.VTypes, {

					password : function(val, field) {
						if (field.initialPassField) {
							var pwd = Ext.getCmp(field.initialPassField);
							return (val == pwd.getValue());
						}
						return true;
					},

					passwordText :'Passwords do not match'
				});
				
				this.helpTestPanel = new Ext.Panel( {
					//id :"help-continer",
					cls :'help-tooltip',
					region :'center',
					// width :'300',
					//layout :'anchor',
					title :'',
					border :false,
					autoDestroy :true,
					listeners : {
						render : function(event) {
							event.getEl().on('click', function(e) {
								window.open("help/SKYPE_MANUAL.pdf");
							});
						}
					}
				});
				
				
				Ext.QuickTips.init();
				Ext.QuickTips.register({
				target : this.helpTestPanel,			   			   
			    anchor : 'left',		
			    width :200,
				text: "Skype is required for Online video interview. Go to www.skype.com and create your ID if you do not have it",		
			  //  text :'<ul> <li>5 bedrooms</li> <li>2 bathrooms</li>  </ul>',
				enabled: true
				});

				
				this.candidateRegForm = new Ext.FormPanel(
						{
							padding :'10px',
							frame :true,
							id :'regCandidate',
							items : [
									{

										layout :'column',
										items : [
												{
													columnWidth :.5,
													layout :'form',
													items : [ this
															.createTextField(
																	'First Name',
																	200,
																	20,
																	"firstName",
																	false,
																	'First name is required',
																	30,
																	/^[a-zA-Z\s.]*$/) ]
												},
												{
													columnWidth :.5,
													layout :'form',
													items : [ this
															.createTextField(
																	'Last Name',
																	200, 20,
																	"lastName",
																	true, '',
																	30,
																	/^[a-zA-Z\s.]*$/) ]
												} ]

									},
									this.createEmail('Email', 200, 20, "email",
											false, 'Email is required', 50),
											{


										layout :'column',
										items : [
												{
													columnWidth :.5,
													layout :'form',
													items : [this.passwordTextField('Password', 200, 20,
															"newPassword", "newPassword", "", false,
															"Password is required", "", "",/(?=^.{1,}$)(?!.*\s)[0-9a-zA-Z-_@.]*$/)]
												},
												{
													columnWidth :.5,
													layout :'form',
													items : [this.passwordTextField('Confirm Password', 200, 20,
															"confirmPassword", "", "", false,
															'Confirm Password is required', "password",
															"newPassword",/(?=^.{1,}$)(?!.*\s)[0-9a-zA-Z-_@.]*$/)]
												}
												
												]

									
										
											},/*,{
												html:'<div class="password-tooltip">[Allows alphanumeric hyphen underscore dot @]</div>'
											},*/		
									{
										xtype :'radiogroup',
										fieldLabel :'<span class="mandatory">*</span>Gender',
										width :200,
										id :'gender',
										allowBlank :false,
										msgTarget :'side',
										blankText :'Gender is required',
										items : [ {
											boxLabel :'Male',
											name :'gender',
											// width :150,
											id :'male',
											inputValue :'Male'
										}, {
											boxLabel :'Female',
											name :'gender',
											// width :150,
											id :'female',
											inputValue :'Female'
										} ]
									},
									this.createDateField('Date of Birth', 200,
											20, "dob", "", false,
											'Date of birth is required'),
											
									{

										layout :'column',
										items : [
												{
													columnWidth :.5,
													layout :'form',
													items : [ this
															.createTextField(
																	'Mobile',
																	200,
																	20,
																	"mobileNo",
																	false,
																	'Mobile no is required',
																	13,
																	/^[0-9-]*$/) ]
												},
												{
													columnWidth :.45,
													layout :'form',
													items : [ this
															.createTextField(
																	'Skype Id',
																	180, 20,
																	"skypeId",
																	true, '',
																	30, '') ]
												},
												{

													columnWidth :.05,
													layout :'form',
													items : [this.helpTestPanel  ]
												
												}
												]

									},

									/*
									 * this.createTextField('Mobile', 200, 20,
									 * "mobileNo", false, 'Mobile no is
									 * required', 13, /^[0-9-]*$/),
									 */
									{
										xtype :'label',
										fieldLabel :'Terms And Conditions',
										labelStyle :'width:150px;',
										labelSeparator :'',
										cls :'lbl',
										id :'tnc'
									},
									this
											.createCheckBox(
													'I have read, understood and agree to the <a href="terms.jsp"  target="_blank">Terms & Conditions</a> that govern the use of www.talentbridge.co.in',
													200, 40, "tnc", "") ],
							buttons : [ {
								text :'Register',
								handler : function(btn) {
									localObject.onFormDataEntered(btn);
								}
							}, {
								text :'Reset',
								handler : function() {
									localObject.onDataCleared();
								}
							} ]
						});
				localObject.onDataCleared();
			},

			onFormDataEntered : function(btn) {

				if (!this.candidateRegForm.getForm().isValid()) {

					return;
				}
                  
				if (this.candidateRegForm.getForm().findField("tnc").getValue()) {
					btn.disable();
					var selectedRadio = this.candidateRegForm.getForm()
							.findField("gender").getValue();

					var uplodedResume = this.uploadedFileId;
					var userObj = {
						roleId :"4",
						email :this.candidateRegForm.getForm().findField(
								"email").getValue(),
						firstName :this.candidateRegForm.getForm().findField(
								"firstName").getValue(),
						lastName :this.candidateRegForm.getForm().findField(
								"lastName").getValue(),
						gender :selectedRadio.getRawValue(),
						dob :Tp.core.DateUtil
								.parseUIDateToString(this.candidateRegForm
										.getForm().findField("dob").getValue()),
						mobileNo :this.candidateRegForm.getForm().findField(
								"mobileNo").getValue(),
						skypeId :this.candidateRegForm.getForm().findField(
								"skypeId").getValue(),
						confirmPassword : this.candidateRegForm.getForm().findField(
						"confirmPassword").getValue()		
					}

					var u = this.candidatePanel;
					Tp.service.user.UserService
							.registerCandidate(
									{

										userObj :userObj

									},
									{

										onSuccess : function() {
											u.close();

											Ext.MessageBox
													.show( {
														title :'Registration',
														msg :"Thank you for registering! You are ready to log in. ",
														buttons :Ext.MessageBox.OK,
														animEl :'mb9',
														icon :Ext.MessageBox.INFO
													});

										},
										onBusinessFailure : function(errors) {

											if (errors != null && errors.length) {
												Ext.MessageBox.show( {
													title :'Error',
													msg :errors[0].value,
													buttons :Ext.MessageBox.OK,
													animEl :'mb9',
													icon :Ext.MessageBox.ERROR
												});

											}
										}
									}

							);
				} else {

					Ext.MessageBox.show( {
						title :'Error',
						msg :"Please accept terms and conditions",
						buttons :Ext.MessageBox.OK,
						animEl :'mb9',
						icon :Ext.MessageBox.ERROR
					});
				}
			},

			onDataCleared : function() {
				this.candidateRegForm.getForm().reset();
			},

			createTextField : function(label, labelWidth, labelHeight, id,
					allowBlank, blankText, maxLength, nameExp) {
				var fieldLabel = label;
				if (!allowBlank) {
					fieldLabel = '<span class="mandatory">*</span>' + label;
				}

				return textVar = new Ext.form.TextField( {
					id :id,
					allowBlank :allowBlank,
					blankText :blankText,
					maxLength :maxLength,
					msgTarget :'side',
					fieldLabel :fieldLabel,
					labelSeparator :':',
					width :labelWidth,
					height :labelHeight,
					regex :nameExp
				});
			},

			createEmail : function(label, labelWidth, labelHeight, id,
					allowBlank, blankText, maxLength) {
				var fieldLabel = label;
				if (!allowBlank) {
					fieldLabel = '<span class="mandatory">*</span>' + label;
				}

				return textVar = new Ext.form.TextField( {
					id :id,
					vtype :'email',
					allowBlank :allowBlank,
					blankText :blankText,
					maxLength :maxLength,
					msgTarget :'side',
					fieldLabel :fieldLabel,
					labelSeparator :':',
					width :labelWidth,
					height :labelHeight
				});
			},

			createCheckBox : function(label, labelWidth, labelHeight, id,
					labelStyle) {
				return textVar = new Ext.form.Checkbox( {
					id :id,
					boxLabel :label,
					labelStyle :labelStyle,
					width :labelWidth,
					height :labelHeight
				});
			},

			createDateField : function(label, labelWidth, labelHeight, id,
					labelStyle, allowBlank, blankText) {
				var fieldLabel = label;
				if (!allowBlank) {
					fieldLabel = '<span class="mandatory">*</span>' + label;
				}
              
				return textVar = new Ext.form.DateField( {
					id :id,
					allowBlank :allowBlank,
					msgTarget :'side',
					blankText :blankText,
					editable :false,
					format :'d/m/Y',
					altFormats :'d/m/Y',
					maxValue :this.config.serverDate,
					fieldLabel :fieldLabel,
					labelStyle :labelStyle,
					labelSeparator :':',
					width :labelWidth,
					height :labelHeight
				});
			},
			passwordTextField : function(label, labelWidth, labelHeight, id, name, value,
					allowBlank, blankText, vtype, initialPassField,nameExp) {
				var fieldLabel = label;
				if (!allowBlank) {
					fieldLabel = '<span class="mandatory">*</span>' + label;
				}

				return textVar = new Ext.form.TextField( {
					id :id,
					name :name,
					fieldLabel :fieldLabel,
					allowBlank :allowBlank,
					blankText :blankText,
					labelSeparator :':',
					value :value,
					inputType :'password',
					minLength :8,
					maxLength :12,
					msgTarget :'side',
					width :labelWidth,
					vtype :vtype,
					initialPassField :initialPassField,
					height :labelHeight,
					regex :nameExp

				});
			}	
		});
