var uscc = uscc || {};
uscc.authentication = uscc.authentication || {};

/**
 * @class USCC Login From
 *
 *
 * @events onBeforeSubmit.LoginForm
 * @depends newbay.pva.widget.Form
 * @extends newbay.pva.widget.Form
*/
uscc.authentication.ForgottenPword = (function(containerSelector, options){

    var _options = $.extend({}, {}, options),
        _containerSelector = containerSelector,
        _dialog = newbay.widget.OKDialog();
        _formElements = {
            NKN: {
                    id: "uscc-mob-num",
                    validationType:"mandatory_number",
                    localizedError:"Please enter a valid Phone number"
            }
        },
        _super = new newbay.pva.widget.Form ({containerSelector: containerSelector, formElements: _formElements});

    //extend the Form Widget
    var _self = $.extend({}, _super, {});


    var _resendPassword = function(){
        if(_super.validate()){
            var $containerSelector = $(_containerSelector);

            _super.setLoading(true);
            mobile_num = $containerSelector.find("#uscc-mob-num").val(),

            //this will call the atp login service
            $.ajax({
                   url          :   "/atpbridge?operationName=forgotPassword",
                   type         :   "POST",
                   contentType  :   "application/x-www-form-urlencoded",
                   data         :   {msisdn : mobile_num},
                   beforeSend   :   function(xhr){
                        xhr.setRequestHeader("X-Service-Identifier", "USC");
                   },
                   complete     :   function(response){
                       try{
                            var responseStr = $.parseJSON(response.responseText);
                            if(response.status === 401){
                                _dialog.setText("Phone number not found.");
                                _dialog.open();
                                _dialog.setDialogOption("title", "Invalid User");
                                _self.setLoading(false);
                            }
                       }catch(e){
                           //do nothing.
                       }
                   },
                   success      :   function(response){
                       _dialog.setText("A new password has been sent to your phone");
                       _dialog.open();
                       _dialog.setDialogOption("title", "Password Resent");
                       _dialog.subscribe("okclick", function(){
                           location.href = newbay.authentication.systemSettings.contextWebApp+"/intro";
                           //http://localhost/tlc/web/ext/fp/register/undefined/web/intro?dev=true;
                       });
                   },
                   failure      :   function(response){
                       _dialog.setText("An Error occurred while sending the request.");
                       _dialog.open();
                       _dialog.setDialogOption("title", "Error");
                       _self.setLoading(false);
                   }
            });
        }
    };


     var _init = function () {
        $(_containerSelector).delegate ("#uscc-resend", "click", function (event) {
            event.preventDefault();
            _resendPassword ();
         });
         $(_containerSelector).delegate ("#uscc-mob-num", "keyup", function (event) {
             if (event.keyCode==13) {
                _resendPassword ();
             }
         });
     };

    _init ();

    return _self;
});
