var context = location.href.replace(/http:\/\/[^\/]*(\/[\w]+).*/, '$1');
if (context == location.href) {  
	context = location.href.replace(/https:\/\/[^\/]*(\/[\w]+).*/, '$1');
}
PopupLogin = function() {
    var dialog, loginBtn, passBtn, loginNd, form, topDiv, user, pass, fpBtn, frmNode;	
	var uEmail = "";


	function generateHTML() {
		topDiv = document.createElement('div');
		topDiv.id = "login_info";


		if (document.body) {
			document.body.appendChild(topDiv);
		}
	}
    return {
        init : function() {
			if(Ext.get('popup_login')){
				loginBtn = Ext.get('popup_login');
				loginBtn.on('click', this.showDialog, this);
			}
			if(Ext.get('popup_pass')){
				passBtn = Ext.get('popup_pass');
				passBtn.on('click', this.showForgotPass, this);
			}

			Ext.QuickTips.init();
		    Ext.form.Field.prototype.msgTarget = 'side';
		},

		showDialog : function() {
			generateHTML();
			
			loginNd = Ext.getDom('popup_login');
			loginNd.href = "#";
			
		
			form = new Ext.FormPanel({
				id: 'form',
				labelAlign: 'right',
				labelWidth: 75,
				width: 307,
				bodyStyle: 'background-color:transparent; padding:10px; border: 0',
				buttonAlign: 'center',
				onSubmit: Ext.emptyFn,
				submit: function() {
						this.getForm().getEl().dom.action = context + '/subscribe/login/loginauth.do';
						this.getForm().getEl().dom.submit();
						},

				items :[{
						id: 'yEmail',
						xtype: 'textfield',
						autoCreate : {tag: "input", type: "text", name: "userModel.login", size: "20", autocomplete: "off"},
						fieldLabel: 'Email or Login Id',
					//  vtype: 'email',
						blankText: 'You must provide an email address',
						width: '144px',
						maxLength: 200,
						allowBlank: false
					},{
						xtype: 'textfield',
						autoCreate : {tag: "input", type: "password", name: "userModel.password", size: "20", autocomplete: "off"},
						fieldLabel: 'Password',
	        		//  vtype: 'alphanum',
						blankText: 'You must create a password',
					//  minLength: 6,
						maxLength: 200,
						width: '144px',
						allowBlank: false
					}]
			});
			
            dialog = new Ext.Window({
				el:'login_info',
				width: 360,
				height:205,
				modal: true,
				closable:true,
				resizable:false,
				draggable:true,
				collapsible:false,
				title: 'Login',
				html: '<div id="dialogWin"></div><div id="passwordWin"></div>',
				items :[
						form
					,{
						width: 240,
						cls: 'loginLink',
						html:"<br><a href='#' id='forgot_pass' style='font:8pt Tahoma'>Forgot Password?</a>"
					},{
						width: 240,
						cls: 'loginLink',
						html:"<span style='font:8pt Tahoma'>Not a Subscriber? <a href='" + context + "/subscribe/index.jsp?src=challengesub'>Get Access Now!</a></span><br>"
				}],
				buttons: [{
                    text:'Login',
                    handler: this.formSubmit
                }]
			});
	
			dialog.on('hide', this.closeLogin, dialog);
			
			dialog.show('popup_login', function() {
//				Ext.getCmp('yEmail').focus();
//				setTimeout('form.items.items[0].focus();', 1000);
			});
		
			dialog.getEl().addKeyListener(Ext.EventObject.ENTER, this.formSubmit,dialog); 
			
			fpBtn =Ext.get('forgot_pass');
			fpBtn.on('click',this.showForgotPass);
		},
		
		formSubmit : function(){
			if(form.form.isValid())
				form.submit();
		},
		forgotSubmit : function(){
			if(form2.form.isValid()){
				form2.getForm().submit({ 
					method:'POST', 
					waitMsg:'Validating Email Address...',
					success: PopupLogin.showQuestion,
					failure: function(form2,action){Ext.MessageBox.alert('Contact Customer Service',action.result.message, PopupLogin.closeLogin); }
				});
			}
		},
		passwordSubmit : function(){
			if(form3.form.isValid()){
				form3.getForm().submit({
					waitMsg:'Validating Answer...',
					success:PopupLogin.processAnswer,
					failure: function(){Ext.MessageBox.alert('Error', '<b>Answer is incorrect!</b>', PopupLogin.closeLogin); }
				});	
			}				
		},
		closeLogin : function(){
			if(dialog.isVisible()){ dialog.hide();}
			if(Ext.getDom('login_info')){
			frmNode = Ext.getDom('login_info');
			frmNode.parentNode.removeChild(frmNode);
			}
		},
		showForgotPass : function(){
			form2 = new Ext.FormPanel({
				labelAlign: 'right',
				labelWidth: 60,
				buttonAlign: 'right',
				bodyStyle: 'background-color:transparent; padding:10px; border: 0; margin-top: 20px',
				width: 375,
				height: 133,
				url: context + '/subscribe/updateuser/updateuserinfo.do?action=doValidateEmail',
				method: 'POST',

				items :[/*{
						height: 20,
						width: 230,
				        cls: 'loginLink',
						html: '<i>Please enter your e-mail address below.</i>'
					},*/
					{
                        xtype: 'box',
						height:20,
						width: 230,
						autoEl: {tag:'div',html:'<i>Please enter your e-mail address below.</i>'}
                         },
					{
						xtype: 'textfield',
						autoCreate : {tag: "input", type: "text", name: "userModel.email", size: "20", autocomplete: "off"},
						width: '130px',
						fieldLabel: 'Email',
						vtype: 'email',
						allowBlank:false
					}]
			});

			if(!Ext.get('dialogWin')){
				generateHTML();
				dialog = new Ext.Window({
					el:'login_info',
					width: 375,
					height: 200,
					modal: true,
					closable:true,
					resizable:false,
					draggable:true,
					collapsible:false,
					title: 'Forgot Password',
					html: '<div id="passwordWin"></div>',
					items: form2,
					
					buttons: [{
						text:'Submit',
						handler: PopupLogin.forgotSubmit
					}]
				});
			}
			else{
				Ext.each(dialog.items.items, function() {
					this.destroy();
				});
//				dialog.remove(dialog.items.items[0]);
				form2.render(Ext.get('dialogWin'));
				var but = dialog.buttons;
				but[0].setText('Submit');
				but[0].setHandler(PopupLogin.forgotSubmit);
				dialog.setTitle('Forgot Your Password');
			}
			dialog.purgeListeners();
			dialog.on('hide', PopupLogin.closeLogin, dialog);
			dialog.show();
			dialog.getEl().addKeyListener(Ext.EventObject.ENTER, PopupLogin.forgotSubmit,dialog); 

		},
		showQuestion : function(resp,opt){
		
			uEmail = form2.getForm().findField('userModel.email').getValue();
			if(opt && opt.result && opt.result.success){
				
				form3 = new Ext.FormPanel({
					labelAlign: 'top',
					labelWidth: 100,
					buttonAlign: 'right',
					bodyStyle: 'background-color:transparent; padding:10px; border: 0',
					url: context + '/subscribe/updateuser/updateuserinfo.do?action=doResetPassword',
					method: 'POST',

					items :[{
							cls: 'loginLink',
							height: 50,
							html: '<i>For authentication, please provide your answer for your Secret Question.</i>'
						},{
							xtype: 'textfield',
							autoCreate : {tag: "input", type: "text", name: "userModel.passwordAnswer", size: "20", autocomplete: "off"},
							fieldLabel: opt.result.question,
							allowBlank:false
						},{
							xtype: 'hidden',
							autoCreate : {tag: "input", type: "text", name: "userModel.email"},
							value: uEmail
						}]
				});
				dialog.remove(dialog.items.items[0]);
				if(Ext.get('dialogWin')){
					frmNode = Ext.getDom('dialogWin');
					frmNode.parentNode.removeChild(frmNode);
				}
				form3.render(Ext.get('passwordWin'));
				var but = dialog.buttons;
				but[0].setHandler(PopupLogin.passwordSubmit);
				dialog.setTitle('Secret Question');
				dialog.purgeListeners();
				dialog.on('hide', PopupLogin.closeLogin, dialog);
				dialog.getEl().addKeyListener(Ext.EventObject.ENTER, PopupLogin.passwordSubmit,dialog); 
			}
			else{
				Ext.MessageBox.alert('Error', '<b>Email Address does not Exist!</b>', PopupLogin.closeLogin); 
			}
				
		},
		
		processAnswer : function(r, opt){
			if(opt && opt.result && opt.result.success){
				Ext.MessageBox.alert('Password Reset', '<b>Your password has been reset.</b><br><br><i>Please check your email to get your new password.</i>', PopupLogin.closeLogin);
			}
			else{
				Ext.MessageBox.alert('Error', '<b>Answer is incorrect!</b>', PopupLogin.closeLogin);
			}
		}
		
    };
}();
Ext.EventManager.onDocumentReady(PopupLogin.init, PopupLogin, true);
