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

uscc.authentication.ProvisionUser = (function(containerSelector, options){

     var _options = $.extend({}, {}, options),
         _containerSelector = containerSelector,
         _dialog = newbay.widget.OKDialog();
         _formElements = {
            MOB: {
                    id: "msisdn-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 _provisionUser = function(){

        if(_super.validate()){

            var $containerSelector = $(_containerSelector);
            var phoneNum = $containerSelector.find("#msisdn-num").val();

            _super.setLoading(true);
            $.ajax({
                url          :   "/atpbridge?operationName=webProvision",
                type         :   "POST",
                contentType  :   "application/x-www-form-urlencoded",
                data         :   {msisdn : phoneNum},
                beforeSend   :   function(xhr){
                    xhr.setRequestHeader("X-Service-Identifier", "USC");
                },
                complete     :   function(response){
                    try{
                        var json =  $.parseJSON(response.responseText);
                        if(json.errors){
                            _super.setLoading(false);
                            _dialog.setText("An SMS has already been sent to this number. If you have forgotten or misplaced the password click on the Forgot your password link.");
                            _dialog.open();
                            _dialog.setDialogOption("title", "Number Already In Use");
                        }
                    }catch(e){
                        //do nothing
                    }
                },
                success      :   function(response){
                    hideAndClearInput();
                    _super.setLoading(false);
                    _dialog.setText("An SMS has been sent to " +  phoneNum+ ".")
                    _dialog.open();
                    _dialog.setDialogOption("title", "SMS Sent");
                },
                failure      :   function(){
                    _super.setLoading(false);
                    _dialog.setText("An error occurred when trying to complete your request. Please try again.");
                    _dialog.open();
                    _dialog.setDialogOption("title", "Error");
                }
            });
        }
    };

    var hideAndClearInput = function(){
        $('#msisdn-num').val("")
        $('#uscc-provision-box').hide('slow');
    };

    var _init = function(){
         $(_containerSelector).delegate("#uscc-provision", "click", function(event){
            _provisionUser();
         });

         $(_containerSelector).delegate("#show-provision", "click", function(event){
            $('#uscc-provision-box').show('slow');
         });
    }

    _init();

    return _self;

});
