/**
 * @author Janhavi
 */
Tp.ui.view.user.MentorRegView = Class
		.create( {

			/**
			 * default constructor
			 * 
			 * @param handlers
			 */

			initialize : function(mentorConfig, handlers) {
				Ext.QuickTips.init();
				this.config = mentorConfig;
				this.handlers = handlers;
				this.logoPath = mentorConfig.filePath;
				this.isFileToBeUploaded = false;
				this.initView();
			},

			initView : function() {
				this.initForm();
				this.mentorPanel = new Ext.Window( {
					layout :'anchor',
					modal :true,
					title :'Mentor Registration',
					stateful :false,
					closable :true,
					autoScroll :true,
					loadMask :true,
					maximizable :false,
					draggable :false,
					resizable :false,
					width :765,
					autoScroll :false,
					autoHeight :true,
					items : [ this.mentorRegForm ]
				});
				this.mentorPanel.show();
				this.initUploadFramework();
				this.loadDropDown();
				// this.mentorRegForm.getForm().findField("firstName").focus();
			},

			loadDropDown : function() {
				var localObj = this;
				Tp.service.GeneralService.getEducationQualificationList(null, {
					onSuccess : function(qualifications) {
					
					
					qualifications.key='10000';
					qualifications.remove(qualifications);					
					qualifications.push(qualifications);
					
						localObj.qualification.loadData(qualifications);
					}
				});
				Tp.service.GeneralService.getIndustryTypes(null, {
					onSuccess : function(industries) {
						localObj.industryTypeStore.loadData(industries);
					}
				});

				Tp.service.GeneralService.getCountries(null, {
					onSuccess : function(countires) {
						localObj.countryStore.loadData(countires);
					}
				});

			},
			initUploadFramework : function() {

				var localObj = this;

			},

			initForm : function() {
				var localObject = this;

				this.otherIndustryType = new Ext.form.TextField( {
					id :'industryOther',
					fieldLabel :'Others',
					labelSeparator :':',
					disabled :true,
					allowBlank :false,
					blankText :'Other Industry Type is required',
					cls :'qualificationOther',
					msgTarget :'side',
					maxLength :50,
					width :190,
					height :20
				});

				var keyPairModelRecord = Ext.data.Record.create( [ {
					name :'key'
				}, {
					name :"value"
				} ]);

				this.statesStore = new Ext.data.Store( {
					reader :new Ext.data.JsonReader( {
						idProperty :"key"
					}, keyPairModelRecord)
				});

				this.industryTypeStore = new Ext.data.Store( {
					reader :new Ext.data.JsonReader( {
						idProperty :"key"
					}, keyPairModelRecord)
				});

				this.qualification = new Ext.data.Store( {
					reader :new Ext.data.JsonReader( {
						idProperty :"key"
					}, keyPairModelRecord)
				});

				this.countryStore = new Ext.data.Store( {
					reader :new Ext.data.JsonReader( {
						idProperty :"key"
					}, keyPairModelRecord)
				});

				this.mentorRegForm = new Ext.FormPanel(
						{
							padding :'10px',
							id :'regMentor',
							width :750,
							height :450,
							frame :true,
							autoHeight :false,
							autoScroll :true,
							fileUpload :true,
							items : [

									{

										layout :'column',
										items : [
												{
													columnWidth :.5,
													layout :'form',
													items : [ this
															.createTextField(
																	'Name',
																	190,
																	20,
																	"firstName",
																	false,
																	30,
																	/^[a-zA-Z\s.]*$/,
																	'First name is required') ]
												},
												{
													columnWidth :.5,
													layout :'form',
													items : [ this
															.createTextField(
																	'SurName',
																	190,
																	20,
																	"lastName",
																	true,
																	30,
																	/^[a-zA-Z\s.]*$/,
																	'Last name is required') ]
												} ]

									},
									
									
									
									{

										layout :'column',
										items : [
												
												{
													columnWidth :.5,
													layout :'form',
													items : [ this.createEmail('Email', 190, 20, "email",
															false, 'Email is required', 50)]
												},
												
												{
													columnWidth :.5,
													layout :'form',
													items : [ this.createComboBox('Country', 190, 20,
															"country", this.countryStore, '',
															false, 'Country is required') ]
												}
												
												]

									},
									
									
									
									
									
									
									{

										layout :'column',
										items : [
												{
													columnWidth :.28,
													layout :'form',
													items : [ this
															.createCountryCodeTextField(
																	'Mobile No',
																	50,
																	20,
																	"countryCode",
																	false, 03,
																	'',
																	'Country Code is required') ]
												},
												{
													columnWidth :.30,
													layout :'form',
													items : [ this
															.createTextFieldwithOutLabel(

																	"mobileNo",
																	/^[0-9-]*$/,
																	false,
																	30,
																	'Mobile No is required',
																	129, 20) ]
												} ]

									},
									{
										fieldLabel :'Image Logo',
										inputType :'file',
										cls :'imageLogo',
										id :'form-file',
										name :'file',
										xtype :'textfield',
										autoShow :true,
										// anchor :'52%',
										height :25
									},

									this.createListBox(
											'Educational Qualification', 190,
											95, "qualifications",
											this.qualification, true),

									this
											.createTextArea(
													'Your Summary Profile',
													535, 100, "summaryProfile",
													false, 4000,
													'Summary Profile is required'),
									this
											.createKeyAttributeTextField(
													'What are the key attributes that helped you reach where you have reached today',
													172, 20, "keyAttributes",
													true, 30, /^[a-zA-Z\s.]*$/,
													''),
									{

										items : [ {
											layout :'column',
											cls :'industry-type',
											items : [
													{
														columnWidth :.5,
														layout :'form',
														items : [ {
															xtype :'combo',
															id :'industryType',
															fieldLabel :'<span class="mandatory">*</span>' + 'Industry Type',
															msgTarget :'side',
															emptyText :'Select',
															blankText :'Industry Type is required',
															triggerAction :'all',
															height :20,
															width :190,
															forceSelection :true,
															allowBlank :false,
															mode :'local',
															store :this.industryTypeStore,
															valueField :'key',
															displayField :'value',
															listeners : {
																select : function(
																		f, r, i) {
																	if (r.data.key == 9999) {
																		localObject.otherIndustryType
																				.setDisabled(false);
																		localObject.otherIndustryType
																				.validate()
																	} else {
																		localObject.otherIndustryType
																				.reset();
																		localObject.otherIndustryType
																				.setDisabled(true)
																	}
																}
															}
														}

														/*
														 * this .createComboBox(
														 * 'Industry Type', 190,
														 * 20, "industryType",
														 * this.industryTypeStore,
														 * '', false, 'Industry
														 * type is required')
														 */]
													},
													{
														columnWidth :.499,
														layout :'form',
														items : [ this.otherIndustryType ]
													} ]
										} ]

									},

									this.createDomainTextArea(
											'Domain Expertiese', 535, 100,
											"domain", true, 4000),

									checkboxs = new Ext.form.CheckboxGroup(
											{
												fieldLabel :'<span class="mandatory">*</span>You would like to contribute to the eco-system as a ',
												id :'GetChkBoxValue',
												itemCls :'eco-system',
												allowBlank :false,
												msgTarget :'side',
												// blankText :'This field is
												// required',
												columns :2,

												items : [

														{
															boxLabel :'Trainer',
															cls :'system-unit',
															id :'Trainer',
															name :'contributeToecosystem'
														},

														{
															boxLabel :'Domain Expert',
															cls :'system-unit',
															id :'Domain Expert',
															name :'contributeToecosystem'
														},

														{
															boxLabel :'Fellow TAC',
															cls :'system-unit',
															id :'Fellow TAC',
															name :'contributeToecosystem'
														},

														{
															boxLabel :'Research Contributor',
															cls :'system-unit',
															id :'Research Contributor',
															name :'contributeToecosystem'
														}

												]

											}),
									checkboxs = new Ext.form.CheckboxGroup(
											{

												fieldLabel :'<span class="mandatory">*</span>How much Time per week can you dedicate to the ecosystem remotely ',
												id :'dedicateToecosystem',
												allowBlank :false,
												msgTarget :'side',
												// blankText :'This field is
												// required',
												itemCls :'eco-system-remotely',
												columns :2,

												items : [

														{
															boxLabel :'Up To 30mins',
															id :'Up To 30mins',
															baseCls :'ecosystem-unit',
															name :'dedicate-To-ecosystem'
														},

														{
															boxLabel :'30mins to 1hr',
															id :'30mins to 1hr',
															baseCls :'ecosystem-unit',
															name :'dedicate-To-ecosystem'
														},

														{
															boxLabel :'1 hr to 2hrs',
															id :'1 hr to 2hrs',
															baseCls :'ecosystem-unit',
															name :'dedicate-To-ecosystem'
														},

														{
															boxLabel :'2-5hrs',
															id :'2-5hrs',
															baseCls :'ecosystem-unit',
															name :'dedicate-To-ecosystem'
														}

												]

											}),
									/**
									 * this .createTextArea( 'What would you
									 * like to contribute', 553, 100,
									 * "contribution", true, 1000),
									 */

									
									{
										xtype :'label',
										fieldLabel :'Terms And Conditions',

										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',
													190, 40, "tnc", "") ],

							buttons : [ {
								text :'Register',
								handler : function(btn) {
									localObject.onFormDataEntered(btn);
								}
							}, {
								text :'Reset',
								handler : function() {

									localObject.onDataCleared();
								}
							} ]
						});

				this.mentorRegForm.getForm().findField("industryType").reset();
				this.mentorRegForm.getForm().findField("country").reset();
			},

			onFileUploaded : function(fileName) {

				this.uploadedFileId = fileName;
			},
			onFormDataEntered : function(btn) {
				
				
				var localObj = this;
				if (!this.mentorRegForm.getForm().isValid()) {
					return;
				}

				if (document.getElementById("form-file").value) {
					
					if (this.mentorRegForm.getForm().findField("tnc")
							.getValue()) {
						btn.disable();
						localObj.mentorRegForm
								.getForm()
								.submit(
										{
											url :'commonupload/upload.do',
											enctype :'multipart/form-data',
											params :'folderPath=' + localObj.logoPath,
											waitMsg :'Uploading your file...',
											success : function(form, action) {

											var imageFile = action.result.object;
											if (imageFile
													.substring(
															(imageFile
																	.lastIndexOf(".") + 1),
															imageFile.length) == 'jpg'
													|| imageFile
															.substring(
																	(imageFile
																			.lastIndexOf(".") + 1),
																	imageFile.length) == 'JPG' 
													|| imageFile
													.substring(
															(imageFile
																	.lastIndexOf(".") + 1),
															imageFile.length) == 'PNG'
													|| imageFile
													.substring(
															(imageFile
																	.lastIndexOf(".") + 1),
															imageFile.length) == 'png'
													|| imageFile
													.substring(
															(imageFile
																	.lastIndexOf(".") + 1),
															imageFile.length) == 'TIF'
													|| imageFile
													.substring(
															(imageFile
																	.lastIndexOf(".") + 1),
															imageFile.length) == 'tif'	
													|| imageFile
													.substring(
															(imageFile
																	.lastIndexOf(".") + 1),
															imageFile.length) == 'GIF'	
													|| 	imageFile
													.substring(
															(imageFile
																	.lastIndexOf(".") + 1),
															imageFile.length) == 'gif'		
													|| imageFile
													.substring(
															(imageFile
																	.lastIndexOf(".") + 1),
															imageFile.length) == 'BMP'		
													|| imageFile
													.substring(
															(imageFile
																	.lastIndexOf(".") + 1),
															imageFile.length) == 'bmp'				) {
												localObj
														.onFilesUploadedSuccessfully(action.result.object);
											} else {
												Ext.Msg
														.alert('Failure',
																'You can upload JPG,PNG,TIF,GIF,BMP (.jpg,.png,.tif,.gif,.bmp) file only');
											}
										},
											failure : function(form, action) {
												Ext.Msg
														.alert('Failure',
																'Unable to upload mentor Logo');
											}
										});
					} else {
						Ext.MessageBox.show( {
							title :'Error',
							msg :"Please accept terms and conditions",
							buttons :Ext.MessageBox.OK,
							animEl :'mb9',
							icon :Ext.MessageBox.ERROR
						});
					}

				} else {
					if (this.mentorRegForm.getForm().findField("tnc")
							.getValue()) {
						btn.disable();
						localObj.mentorRegForm
								.getForm()
								.submit(
										{
											url :'commonupload/upload.do',
											enctype :'multipart/form-data',
											params :'folderPath=' + localObj.logoPath,
											success : function(form, action) {

												localObj
														.onFilesUploadedSuccessfully(action.result.object);
											},
											failure : function(form, action) {
												Ext.Msg
														.alert('Failure',
																'Unable to upload company Logo');
											}
										});
					} else {
						Ext.MessageBox.show( {
							title :'Error',
							msg :"Please accept terms and conditions",
							buttons :Ext.MessageBox.OK,
							animEl :'mb9',
							icon :Ext.MessageBox.ERROR
						});
					}
				}

			},

			onFilesUploadedSuccessfully : function(uploadedFileId) {

				var checkBox = this.mentorRegForm.getForm().findField(
						"GetChkBoxValue").getValue();
				var values = '';
				for ( var i = 0; i < checkBox.length; i++) {
					if (values.length > 0) {

						values = values + "," + checkBox[i].getId();
					} else {

						values = checkBox[i].getId();

					}

				}

				var dedicateToecosystemCheckBox = this.mentorRegForm.getForm()
						.findField("dedicateToecosystem").getValue();
				var checkedValues = '';
				for ( var j = 0; j < dedicateToecosystemCheckBox.length; j++) {
					if (checkedValues.length > 0) {
						checkedValues = checkedValues + ","
								+ dedicateToecosystemCheckBox[j].getId();
					} else {
						checkedValues = dedicateToecosystemCheckBox[j].getId();
					}
				}

				var countryCode = this.mentorRegForm.getForm().findField(
						"countryCode").getValue();
				var mobile = this.mentorRegForm.getForm().findField("mobileNo")
						.getValue();

				var contactNumber = countryCode + "-" + mobile;
				var userObj = {

					roleId :"6",
					email :this.mentorRegForm.getForm().findField("email")
							.getValue(),
					firstName :this.mentorRegForm.getForm().findField(
							"firstName").getValue(),
					lastName :this.mentorRegForm.getForm()
							.findField("lastName").getValue(),
					domainExpertise :this.mentorRegForm.getForm().findField(
							"domain").getValue(),
					mobileNo :contactNumber,
					qualification :this.mentorRegForm.getForm().findField(
							"qualifications").getValue(),
					summaryProfile :this.mentorRegForm.getForm().findField(
							"summaryProfile").getValue(),
					keyAttributes :this.mentorRegForm.getForm().findField(
							"keyAttributes").getValue(),
					industryType :this.mentorRegForm.getForm().findField(
							"industryType").getValue(),
					industryOther :this.mentorRegForm.getForm().findField(
							"industryOther").getValue(),
					contribution :values,
					dedicationTimes :checkedValues,
					country :this.mentorRegForm.getForm().findField("country")
							.getValue(),
					image :uploadedFileId
				};

				var u = this.mentorPanel;

				Tp.service.user.UserService
						.registerMentor(
								{
									userObj :userObj
								},

								{
									onSuccess : function() {
										u.close();
										Ext.MessageBox
												.show( {
													title :'Registration',
													msg :"Welcome  to TalentBridge. <BR/> Your registration has been sucessful  and your user details has been sent to your provided  emailid.",
													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
											});
										}
									}
								});

			},

			onDataCleared : function() {

				this.mentorRegForm.getForm().reset();
				this.otherIndustryType.setDisabled(true);

			},

			createTextField : function(label, labelWidth, labelHeight, id,
					allowBlank, maxLength, nameExp, blankText) {
				var fieldLabel = label;
				if (!allowBlank) {
					fieldLabel = '<span class="mandatory">*</span>' + label;
				}

				return textVar = new Ext.form.TextField( {
					id :id,
					fieldLabel :fieldLabel,
					labelSeparator :':',
					allowBlank :allowBlank,
					blankText :blankText,
					msgTarget :'side',
					maxLength :maxLength,
					width :labelWidth,
					height :labelHeight,
					regex :nameExp
				});
			},
			createCountryCodeTextField : function(label, labelWidth,
					labelHeight, id, allowBlank, maxLength, nameExp, blankText) {
				var fieldLabel = label;
				if (!allowBlank) {
					fieldLabel = '<span class="mandatory">*</span>' + label;
				}

				return textVar = new Ext.form.TextField( {
					id :id,
					fieldLabel :fieldLabel,
					labelSeparator :':',
					allowBlank :allowBlank,
					maxLength :maxLength,
					width :labelWidth,
					height :labelHeight,
					regex :nameExp
				});
			},

			createKeyAttributeTextField : function(label, labelWidth,
					labelHeight, id, allowBlank, maxLength, nameExp, blankText) {
				var fieldLabel = label;
				if (!allowBlank) {
					fieldLabel = '<span class="mandatory">*</span>' + label;
				}

				return textVar = new Ext.form.TextField( {
					id :id,
					fieldLabel :fieldLabel,
					itemCls :'key-attributes',
					labelSeparator :':',
					allowBlank :allowBlank,
					blankText :blankText,
					msgTarget :'side',
					maxLength :maxLength,
					width :labelWidth,
					height :labelHeight,
					regex :nameExp
				});
			},

			createTextFieldwithOutLabel : function(id, nameExp, allowBlank,
					maxLength, blankText, labelWidth, labelHeight) {

				return textVar = new Ext.form.TextField( {
					id :id,
					// fieldLabel :fieldLabel,
					labelSeparator :':',
					allowBlank :allowBlank,
					hideLabel :true,
					// blankText :blankText,
					msgTarget :'side',
					maxLength :maxLength,
					width :labelWidth,
					height :labelHeight,
					regex :nameExp,
					blankText :blankText
				});
			},

			createTextArea : function(label, labelWidth, labelHeight, id,
					allowBlank, maxLength, blankText) {
				var fieldLabel = label;
				if (!allowBlank) {
					fieldLabel = '<span class="mandatory">*</span>' + label;
				}

				return textVar = new Ext.form.TextArea( {
					id :id,
					fieldLabel :fieldLabel,
					labelSeparator :':',
					allowBlank :allowBlank,
					msgTarget :'side',
					maxLength :maxLength,
					width :labelWidth,
					height :labelHeight,
					blankText :blankText
				});
			},

			createDomainTextArea : function(label, labelWidth, labelHeight, id,
					allowBlank, maxLength, blankText) {
				var fieldLabel = label;
				if (!allowBlank) {
					fieldLabel = '<span class="mandatory">*</span>' + label;
				}

				return textVar = new Ext.form.TextArea( {
					id :id,
					fieldLabel :fieldLabel,
					labelSeparator :':',
					allowBlank :allowBlank,
					msgTarget :'side',
					maxLength :maxLength,
					width :labelWidth,
					height :labelHeight,
					blankText :blankText
				});
			},

			createCheckBox : function(label, labelWidth, labelHeight, id,
					labelStyle) {
				return textVar = new Ext.form.Checkbox( {
					id :id,
					boxLabel :label,
					labelStyle :labelStyle,
					// labelSeparator :':',
					width :labelWidth,
					height :labelHeight
				});
			},
			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,
					maxLength :maxLength,
					blankText :blankText,
					msgTarget :'side',
					fieldLabel :fieldLabel,
					labelSeparator :':',
					width :labelWidth,
					height :labelHeight
				});
			},
			createListBox : function(label, labelWidth, labelHeight, id, store,
					allowBlank) {
				return textVar = {
					xtype :'multiselect',
					fieldLabel :label,
					id :id,
					width :labelWidth,
					height :labelHeight,
					allowBlank :allowBlank,
					msgTarget :'side',
					store :store,
					valueField :'key',
					displayField :'value',
					hiddenField :'displayText'
				};
			},
			createComboBox : function(label, labelWidth, labelHeight, id,
					store, selectedValue, allowBlank, blankText) {
				var fieldLabel = label;
				if (!allowBlank) {
					fieldLabel = '<span class="mandatory">*</span>' + label;
				}

				return textVar = new Ext.form.ComboBox( {
					typeAhead :true,
					id :id,
					fieldLabel :fieldLabel,
					labelSeparator :':',
					allowBlank :allowBlank,
					msgTarget :'side',
					blankText :blankText,
					forceSelection :true,
					emptyText :'Select',
					width :labelWidth,
					height :labelHeight,
					triggerAction :'all',
					mode :'local',
					value :selectedValue,
					store :store,
					valueField :'key',
					displayField :'value'
				});
			},
			createTextFieldOthers : 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,
					disabled :true,
					labelSeparator :':',
					width :labelWidth,
					height :labelHeight,
					regex :nameExp
				});
			}
		});
