/* Minification failed. Returning unminified contents.
(50,25-26): run-time error JS1014: Invalid character: `
(50,26-27): run-time error JS1195: Expected expression: /
(50,59-60): run-time error JS1014: Invalid character: `
(51,16-17): run-time error JS1014: Invalid character: `
(51,48-49): run-time error JS1014: Invalid character: `
(56,19-20): run-time error JS1014: Invalid character: `
(56,20-21): run-time error JS1014: Invalid character: #
(56,22-23): run-time error JS1193: Expected ',' or ')': {
(56,61-62): run-time error JS1014: Invalid character: `
(56,62-63): run-time error JS1004: Expected ';': )
(63,1-2): run-time error JS1002: Syntax error: }
(66,5-6): run-time error JS1014: Invalid character: `
(66,6-7): run-time error JS1014: Invalid character: #
(66,8-9): run-time error JS1193: Expected ',' or ')': {
(66,43-44): run-time error JS1014: Invalid character: `
(66,44-45): run-time error JS1004: Expected ';': )
(67,1-2): run-time error JS1002: Syntax error: }
(72,10-11): run-time error JS1014: Invalid character: `
(72,36-37): run-time error JS1003: Expected ':': {
(72,40-41): run-time error JS1003: Expected ':': }
(72,51-52): run-time error JS5008: Illegal assignment: =
(72,72-73): run-time error JS1195: Expected expression: &
(72,99-100): run-time error JS1195: Expected expression: &
(72,119-120): run-time error JS1004: Expected ';': {
(72,126-127): run-time error JS1014: Invalid character: `
(72,127-128): run-time error JS1195: Expected expression: ,
(73,4-5): run-time error JS1004: Expected ';': )
(79,32-33): run-time error JS1195: Expected expression: >
(89,1-2): run-time error JS1002: Syntax error: }
(91,29-30): run-time error JS1195: Expected expression: >
(104,1-2): run-time error JS1002: Syntax error: }
(106,40-41): run-time error JS1004: Expected ';': {
(107,37-38): run-time error JS1195: Expected expression: >
(110,6-7): run-time error JS1195: Expected expression: )
(112,1-5): run-time error JS1034: Unmatched 'else'; no 'if' defined: else
(112,29-30): run-time error JS1004: Expected ';': {
(113,37-38): run-time error JS1195: Expected expression: >
(116,6-7): run-time error JS1195: Expected expression: )
(58,5-14): run-time error JS1018: 'return' statement outside of function: return ''
(61,5-25): run-time error JS1018: 'return' statement outside of function: return session.val()
 */
$required = $("input[data-val-required], textarea[data-val-required], select[data-val-required], checkbox[data-val-required],radio[data-val-required]");
$.each($required, function (b) {
    var a = $(this).attr("id");
    a = $("label[for\x3d" + a + "]");
    a.addClass("nch-required-label");
    "checkbox" == $(this).attr("type") && (a = $(this).parent(),
        $nchthischeck = $(this).attr("data-val-ischecked"),
        $nchparentlabel = a.prevAll("label").first(),
        $nchthischeck && ($nchparentlabel.parent().is("div.list")
            ? a.prevAll("label").first().addClass("nch-required-label")
            : $(this).closest("label").addClass("nch-required-label")));
    "radio" == $(this).attr("type") && (a = $(this).parent(),
        a.prevAll("label").first().addClass("nch-required-label"));
    1 === b && $(this).closest("form").append("\x3cp\x3eAll fields marked with \x3cspan style\x3d'color:red;'\x3e*\x3c/span\x3e are required\x3c/p\x3e")
});;
$(document).ajaxStart(function () {
    var activeElement = $(document.activeElement);

    if ($(activeElement).hasClass("SitecoreFormsSubmit")) {        
        $(activeElement).prop("disabled", true)
            .prop("value", "Submitting - please wait")
            .addClass("form-button-disable");
    }
});

$(document).ajaxStop(function () {
  $(".SitecoreFormsSubmit").prop("disabled", false)
    .removeClass("form-button-disable");
}); 
$(document).ajaxComplete(function (event, jqxhr, settings) {
   //only interested in forms
   if (!settings.url.includes('formbuilder')) {
    return;
  } 
  if (jqxhr.status >= 300) {    
    showErrorOnForm(settings.url);    
    logError(settings.url, jqxhr.statusText, thrownError);
  }
});
$(document).ajaxError(function (event, jqxhr, settings, thrownError) {
  //only interested in forms
  if (!settings.url.includes('formbuilder')) {
    return;
  }  
  showErrorOnForm(settings.url);
  logError(settings.url, jqxhr.statusText, thrownError);
});

const getTriggeredForm = function (url) {
  const uniqueFormUrl = `/formbuilder?${url.split("?")[1]}`;
  var form = $(`form[action='${uniqueFormUrl}']`).first();
  return form;
}
const getSessionId = function (url) {
  var form = getTriggeredForm(url);
  var session = $(`#${form.id} input[name*='.FormSessionId']`).first();
  if (!session) {
    return '';
  }
  else {
    return session.val();
  }
}
const showErrorOnForm = function (url) {  
  var form = getTriggeredForm(url)[0];
  $(`#${form.id} input.SitecoreFormsSubmit`).after("<div><p style='color: red;'>We’re sorry an error has occurred.</p></div>");
}

const logError = function (url, status, error) {
  $.ajax({
    type: "POST",
    url: `/log/form-error?formUrl=${url}&sessionId=${getSessionId(url)}&pageUrl=${window.location}&message=${status}-${error}`,
  });
};
// Handles floating input labels

var $formInputs = $('.form input, .form textarea');

const checkInputFill = $input => {
    var $label = $input.parent('label');

    if ($label.length > 0) {
        if ($input[0].value) {
            $label.addClass('is-filled-label');
        } else {
            $label.removeClass('is-filled-label');
        }
    }
};

const newFormInput = input => {
    let $input = $(input);
    $input.on('input', function () {
        let $field = $(this).closest('div');
        if ($field.length > 0) {
            if (this.value) {
                $field.addClass('field--not-empty');
            } else {
                $field.removeClass('field--not-empty');
            }
        }
        checkInputFill($input);
    });
};

if (window.getAllAngularTestabilities) {
    $.each($formInputs, (i, input) => {
        newFormInput(input);
        checkInputFill($(input));
    });
}
else if (window['angular']) {
    $.each($formInputs, (i, input) => {
        newFormInput(input);
        checkInputFill($(input));
    });
};
