/**
 * @author Janhavi
 */
Tp.ui.view.user.TpoRegView = Class
		.create( {

			/**
			 * default constructor
			 * 
			 * @param handlers
			 */

			initialize : function(projectConfig, handlers) {
				Ext.QuickTips.init();
				this.config = projectConfig;
				this.handlers = handlers;
				this.logoPath = projectConfig.filePath;
				this.isFileToBeUploaded = false;
				this.initView();
			},

			initView : function() {
				this.initForm();
				this.tpoPanel = new Ext.Window( {
					layout :'anchor',
					title :'Institute Registration',
					modal :true,
					stateful :false,
					closable :true,
					loadMask :true,
					maximizable :false,
					draggable :false,
					resizable :false,
					width :750,
					autoScroll :false,
					autoHeight :true,
					items : [ this.tpoRegForm ]
				});
				this.tpoPanel.show();
				this.initUploadFramework();

				this.loadDropDown();
				// this.tpoRegForm.getForm().findField("email").focus();
			},

			loadDropDown : function() {
				var localObj = this;

				Tp.service.GeneralService.getCities(null, {
					onSuccess : function(cities) {

						cities.key = '195';
						cities.remove(cities);
						cities.push(cities);
						localObj.statesStore.loadData(cities);
					}
				});

				Tp.service.GeneralService.getQualifications(null, {
					onSuccess : function(qualifications) {
					
					qualifications.key = '10000';
					qualifications.remove(qualifications);
					qualifications.push(qualifications);
						localObj.qualification.loadData(qualifications);
					}
				});
			},
			initUploadFramework : function() {

				var localObj = this;

			},

			initForm : function() {
				var localObject = this;
				
				
				
				var myValidFn = function(v) {
					   var myRegex = /(^(((http{1}|https{1}|ftp{1}|ftps{1})(\:\/\/{1})+w{3})|w{3})\.+(\w+)\.((\w+){2,3}|((\w+){2,3}\.(\w+){2,3}))+(\/[\w\- \.\/\?%&=]*)?)/;
					   return myRegex.test(v);
					}

					Ext.apply(Ext.form.VTypes, {
					    urlOnly     : myValidFn,
					    urlOnlyText : 'This field should be a URL in the format "http://www.example.com"'
					});
				

				var keyPairModelRecord = Ext.data.Record.create( [ {
					name :'key'
				}, {
					name :"value"
				} ]);

				// create the Data Store to load States
				this.statesStore = 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.tpoRegForm = new Ext.FormPanel(
						{
							padding :'10px',
							id :'regInstitute',
							frame :true,
							fileUpload :true,
							width :735,
							height :450,
							frame :true,
							autoHeight :false,
							autoScroll :true,
							items : [
									this.createTextField('Institute Name', 200,
											20, "instituteName", false, 100,
											/^[a-zA-Z\s.]*$/,
											'Institute name is required'),
									this.createTextField('Short Name', 200, 20,
											"shortName", true, 50,
											/^[a-zA-Z\s.]*$/),
									this.createTextField('TPO Name', 200, 20,
											"tpoName", false, 20,
											/^[a-zA-Z\s.]*$/,
											'TPO name is required'),

									this.createEmail('Email', 200, 20, "email",
											false, 'Email is required', 50),

									this.createComboBox('Location', 200, 20,
											"city", this.statesStore, true),

									this.createTextArea('Address', 520, 60,
											"address", true, 250),

									this.createTextField('Phone No', 200, 20,
											"phoneNo", true, 20, /^[0-9-]*$/,
											'Phone number is required'),

									this.createTextField('Mobile', 200, 20,
											"mobileNo", true, 13, /^[0-9-]*$/,
											'Mobile number is required'),

									this.createTextArea('About Institute', 520,
											60, "aboutInstitute", true, 500),

									this
											.createTextField(
													'Affiliated University',
													200, 20,
													"affiliatedUniversity",
													true, 30, /^[a-zA-Z\s.]*$/,
													'Affiliated university is required'),

									{
										fieldLabel :'Institute Logo',
										inputType :'file',
										id :'form-file',
										cls :'instituteLogo',
										name :'file',
										xtype :'textfield',
										autoShow :true,
										// anchor :'54%'
										height :25
									},

									this
											.createUrlField('URL', 200, 20,
													"url", true, '', 100,
													''),
									{
										items : [ {
											layout :'column',
											items : [
													{
														columnWidth :.5,
														layout :'form',
														items : [ this
																.createListBox(
																		'Courses Offered',
																		200,
																		95,
																		"courses",
																		this.qualification) ]
													},
													{
														columnWidth :.5,
														layout :'form',
														items : [ this
																.createTextField(
																		'Others',
																		180,
																		20,
																		"others",true,50,'', '') ]
													} ]
										} ]
									},

									{
										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',
													200, 40, "tnc", "") ],

							buttons : [ {
								id	 :'regButton',
								text :'Register',
								handler : function(btn) {
								//btn.disable();
								localObject.onFormDataEntered(btn);
								}
							}, {
								text :'Reset',
								handler : function() {
									localObject.onDataCleared();
								}
							} ]
						});
			},
			onFileUploaded : function(fileName) {

				this.uploadedFileId = fileName;
			},
			onFormDataEntered : function(btn) {

				var localObj = this;
				if (!this.tpoRegForm.getForm().isValid()) {
					
					return;
				}
				if (document.getElementById("form-file").value) {
					if (this.tpoRegForm.getForm().findField("tnc").getValue()) {
						alert(btn);
						btn.disable();	
						localObj.tpoRegForm
								.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 institute 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.tpoRegForm.getForm().findField("tnc").getValue()) {
						btn.disable();
						localObj.tpoRegForm
								.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 userObj = {

					roleId :"3",
					city :this.tpoRegForm.getForm().findField("city")
							.getRawValue(),
					cityId :this.tpoRegForm.getForm().findField("city")
							.getValue(),

					email :this.tpoRegForm.getForm().findField("email")
							.getValue(),
					instituteName :this.tpoRegForm.getForm().findField(
							"instituteName").getValue(),
					shortName :this.tpoRegForm.getForm().findField("shortName")
							.getValue(),
					tpoName :this.tpoRegForm.getForm().findField("tpoName")
							.getValue(),
					address :this.tpoRegForm.getForm().findField("address")
							.getValue(),
					mobileNo :this.tpoRegForm.getForm().findField("mobileNo")
							.getValue(),
					telNo :this.tpoRegForm.getForm().findField("phoneNo")
							.getValue(),
					comments :this.tpoRegForm.getForm().findField(
							"aboutInstitute").getValue(),
					affiliatedUniversity :this.tpoRegForm.getForm().findField(
							"affiliatedUniversity").getValue(),
					website :this.tpoRegForm.getForm().findField("url")
							.getValue(),

					course :this.tpoRegForm.getForm().findField("courses")
							.getValue(),
					courseOther :this.tpoRegForm.getForm().findField("others")
							.getValue(),
					logo :uploadedFileId
				};

				var u = this.tpoPanel;

				Tp.service.user.UserService
						.registerTpo(
								{
									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 email id.",
													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.tpoRegForm.getForm().reset();
			},

			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,
					maxLength :maxLength,
					msgTarget :'side',
					width :labelWidth,
					height :labelHeight,
					regex :nameExp
				});
			},

			createTextArea : function(label, labelWidth, labelHeight, id,
					allowBlank, maxLength) {
				var fieldLabel = label;
				if (!allowBlank) {
					fieldLabel = '<span class="mandatory">*</span>' + label;
				}

				return textVar = new Ext.form.TextArea( {
					id :id,
					fieldLabel :fieldLabel,
					labelSeparator :':',
					allowBlank :allowBlank,
					maxLength :maxLength,
					msgTarget :'side',
					width :labelWidth,
					height :labelHeight
				});
			},

			createComboBox : function(label, labelWidth, labelHeight, id,
					store, allowBlank) {
				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',
					emptyText :'Select',
					width :labelWidth,
					height :labelHeight,
					triggerAction :'all',
					mode :'local',
					store :store,
					valueField :'key',
					displayField :'value'
				});
			},

			createListBox : function(label, labelWidth, labelHeight, id, store) {
				return textVar = {
					xtype :'multiselect',
					fieldLabel :label,
					id :id,
					width :labelWidth,
					height :labelHeight,
					allowBlank :true,
					msgTarget :'side',
					store :store,
					valueField :'key',
					displayField :'value',
					hiddenField :'displayText'
				};
			},

			createCheckBox : function(label, labelWidth, labelHeight, id) {
				return textVar = new Ext.form.Checkbox( {
					id :id,
					boxLabel :label,
					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,
					msgTarget :'side',
					blankText :blankText,
					maxLength :maxLength,
					msgTarget :'side',
					fieldLabel :fieldLabel,
					labelSeparator :':',
					width :labelWidth,
					height :labelHeight
				});
			},

			createUrlField : 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,
					vtype : 'urlOnly',
					regex :nameExp,
					blankText :blankText,
					maxLength :maxLength,
					msgTarget :'side',
					fieldLabel :fieldLabel,
					labelSeparator :':',
					width :labelWidth,
					height :labelHeight
				});
			}

		});
