/**
 * @author pshah
 */
var forgotPassUrl = '/MedspacePortal/toSendForgottenPassword.action';
var validationMsg = "";
var pass;
var forgotEmail;
var forgotPassPanel;
var forgotPassForm;

function continueSubmit(){
    forgotPassForm = Ext.getCmp('forgotPassForm');
    forgotPassForm.getForm().submit({  
        url: forgotPassUrl,
        success: forgotSuccess,
        failure: requestFailure
    
    });
}

// redirects to the forgot password sent page
function forgotSuccess(form, action){
	s.events="event5";
	pageTracker._trackPageview('/MedspacePortal/forms/forgot-password.jsp');
	pageTracker._trackEvent('Before Login', 'Forgot Pasword Success', 'Successful Forgot Password reminder'); //forgotSuccess
	window.location.href = '/MedspacePortal/forms/password-sent.jsp';
   }

// function to handle failure return
function requestFailure(form, action){
    //Any custom message can be displyed to client if some error occurs at server side hear.
    var failureMessage = "Invalid User name or Password";
    if (action.failureType == Ext.form.Action.CONNECT_FAILURE) {
        failureMessage = 'Error while connecting to server...';
    }
    if (action.failureType == Ext.form.Action.LOAD_FAILURE) {
        failureMessage = 'Error loading data from server...';
    }
    if (action.failureType == Ext.form.Action.CLIENT_INVALID) {
        failureMessage = "Plese enter all the required fields in required format.";
    }
    if (action.failureType == Ext.form.Action.SERVER_INVALID) {
        failureMessage = action.result.errors.email;
    }
    Ext.fly('validationmsg').update('<font color=red><p>' + failureMessage + '</p></font>');
    document.getElementById('validationmsg').style.display = "block";
    Ext.getCmp('cntnuBtn').enable();
}

function continueButtonHandler(){
    var NUMERIC_REGEX = /[0-9]/;
    var LOWER_CASE_REGEX = /[a-z]/;
    var UPPER_CASE_REGEX = /[A-Z]/;
    
    forgotEmail = document.getElementById('email'); //getValue gives the default value and not the one which user entered so used this
    if (forgotEmail != undefined && forgotEmail.value.trim() === '') {
        Ext.getCmp('email').markInvalid('Email Address can not be blank');
        validationMsg = 'Email Address can not be blank';
    }
    else 
        if (!isEmail(forgotEmail.value.trim())) {
            Ext.getCmp('email').markInvalid('Please enter valid Email Address.');
            validationMsg = 'Please enter valid Email Address.';
        }
        else {
            validationMsg = "";
        }
    
    if (validationMsg != undefined && validationMsg != "") {
        return validationMsg;
    }
}

Ext.onReady(function(){
        // this is required for viewing validation msgs
        Ext.QuickTips.init();
        // turn on validation errors beside the field globally
        Ext.form.Field.prototype.msgTarget = 'side';
        forgotPassPanel = new Ext.FormPanel({
        labelWidth: 105, // 75 label settings here cascade unless overridden
        frame: false,
        bodyStyle: 'padding:10px 10px 5px ; background:transparent',
        border: false,
        bodyBorder: false,
        hideBorders: true,
        height: 170,
        width: 470,//350
        renderTo: 'forgotDiv', // to div
        id: 'forgotPassForm',
        items: [{
            id: 'nmsg',
            width: 460,//440
            autoHeight: true,
            html: '<p>We know that not being able to sign in can be frustrating, so we\'ll try to make this as quick and  easy as possible. <br/><br/><b> What\'s your Email Address? <br/><br/>'
        
        }, {
            xtype: 'textfield',
            fieldLabel: 'Email Address <br><font color="#777777">(User Name)</font>',
            labelSeparator :"", 
            name: 'email',
            width:200,
            bodyStyle: 'padding:10px 10px 15px 10px ; background:transparent',
            allowBlank: false,
            blankText: "Please enter a valid email address",
            vtype: 'email',
            id: 'email',
            listeners: {
                'specialkey': function(f, event){
                    if (event.getKey() == event.ENTER) {
                        //submitBtn.fireEvent('click', submitBtn); // This dont work even if API says that...
                        // Ext.getCmp('submitBtn').onClick(); // This should work bt dont
                        Ext.getCmp('cntnuBtn').handler.call(Ext.getCmp('cntnuBtn').scope);
                    }
                },
                render: function(comp){
    				comp.focus(false,true);
    			}
            }
        }, {
            style: 'display:none',
            id: 'validationmsg',
            width: 420,
            value: validationMsg,
            html: '<p><b>' + validationMsg
        
        }],
        buttonAlign: 'left',
        buttons: [{
            bodyStyle: 'padding:10px 10px 5px ; background:transparent',
            text: 'Continue',
            id: 'cntnuBtn',
            handler: function(){
                continueButtonHandler();
                
                if (validationMsg != "" && validationMsg != undefined) {
                    Ext.fly('validationmsg').update('<font color=red><p>' + validationMsg + '</p></font>');
                    document.getElementById('validationmsg').style.display = "block";
                }
                else {
                    document.getElementById('validationmsg').style.display = "none";
                    Ext.getCmp('cntnuBtn').disable();
                    Ext.getCmp('cntnuBtn').setText('Continue...');
                    continueSubmit();
                }
            }
        }, {
            text: 'Cancel',
            id:'cancelBtn',
            handler: function(){
                window.location.href = '/MedspacePortal/forms/login.jsp';
            }            
        }]
    });    
});
