
let form_confirm_user = $('#form-confirm-migrated-user');

if( form_confirm_user.length ){

  /*Form Validation*/
  form_confirm_user.validate({
    rules:{
      "password": {
        required: true,
        minlength : 6
      },
      "repeat_password": {
        required: true,
        equalTo : "[name='password']"
      },
      "country": {
        required: true
      },

    },
    errorClass: 'error-field',
    errorElement: 'p',
    errorPlacement: function( label, element ) {
      label.insertAfter( element );
    },
    ignore: ":hidden",
    submitHandler: function(form) {
      let confirm_user_data = getFormData(form);
      handleConfirmUser(form, confirm_user_data);
    }
  });

}

function handleConfirmUser(form, confirm_user_data){
  let data = handle_confirm_migrated_user_controller_object;

  data['user_data'] = confirm_user_data;

  $.ajax({
    type: 'POST',
    dataType: 'json',
    url: handle_confirm_migrated_user_controller_object.url,
    data: data,
    beforeSend: function(){
      $(form).find('.form-message').hide();
      $(form).find('.sending-message').fadeIn();
    },
    success: function(data){
      $(form).find('.form-message').hide();
      if( data.hasOwnProperty('success') ){
        $(form).find('.success-message').fadeIn();
        if( data.hasOwnProperty('redirect') ){
          window.location.href = data.redirect;
        }
      }
      if( data.hasOwnProperty('error') ){
        $(form).find('.error-message').find('p').html(data.message);
        $(form).find('.error-message').fadeIn();
      }
    },
    error: function (request, status, error) {
      console.log(status);
      console.log(error);
      console.log(request.responseText);
    }
  });
}
