<%#SYS_PARAM -------------------------------------------------------------------- %>
<script type="text/javascript">

this.ajaxValidateSysParam = function(data) {
  var result = '';
  var xhr = new XMLHttpRequest();
  xhr.open('POST', '<%= Rails.application.routes.url_helpers.validate_sys_param_index_path(format: :json) %>', false);
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xhr.setRequestHeader('X-CSRF-Token', document.querySelector('meta[name="csrf-token"]').content);
  try {
    xhr.send(new URLSearchParams(data).toString());
    if (xhr.status === 200) {
      var json = JSON.parse(xhr.responseText);
      if (!json[1]) {
        result = json[2];
      }
    } else {
      result = 'チェックに失敗しました。';
    }
  } catch(e) {
    result = 'チェックに失敗しました。';
  }
  return result;
};

this.validateDirectory = function(field, rules, i, options) {
  var data, result;
  data = {
    authenticity_token: document.querySelector("input[name='authenticity_token']").value,
    validate_type: 'directory',
    field_id: field.attr("id"),
    key: field.data("key"),
    value: field.val(),
    category: field.data("category")
  };
  result = ajaxValidateSysParam(data);
  if (result.length > 0) {
    return '* ' + result;
  }
};

this.validateTermUnit = function(field, rules, i, options) {
  var data, result;
  data = {
    authenticity_token: document.querySelector("input[name='authenticity_token']").value,
    validate_type: 'term_unit',
    field_id: field.attr("id"),
    key: field.data("key"),
    value: field.val(),
    category: field.data("category"),
    term: field.val(),
    unit: document.getElementById('period_unit').value
  };
  result = ajaxValidateSysParam(data);
  if (result.length > 0) {
    return '* ' + result;
  }
};

this.validatePWLengthMinMax = function(field, rules, i, options) {
  var data, result;
  data = {
    authenticity_token: document.querySelector("input[name='authenticity_token']").value,
    validate_type: 'pw_length_min_max',
    field_id: field.attr("id"),
    key: field.data("key"),
    value: field.val(),
    category: field.data("category")
  };
  result = ajaxValidateSysParam(data);
  if (result.length > 0) {
    return '* ' + result;
  }
};

this.ajaxValidateUser = function(data) {
  var result = '';
  var xhr = new XMLHttpRequest();
  xhr.open('POST', '<%= Rails.application.routes.url_helpers.validate_user_index_path(format: :json) %>', false);
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xhr.setRequestHeader('X-CSRF-Token', document.querySelector('meta[name="csrf-token"]').content);
  try {
    xhr.send(new URLSearchParams(data).toString());
    if (xhr.status === 200) {
      var json = JSON.parse(xhr.responseText);
      if (!json[1]) {
        result = json[2];
      }
    } else {
      result = 'チェックに失敗しました。';
    }
  } catch(e) {
    result = 'チェックに失敗しました。';
  }
  return result;
};

this.validateLogin = function(field, rules, i, options) {
  var data, result;
  data = {
    authenticity_token: document.querySelector("input[name='authenticity_token']").value,
    validate_type: 'login',
    field_id: field.attr("id"),
    value: field.val(),
    user_id: rules[i + 2]
  };
  result = ajaxValidateUser(data);
  if (result.length > 0) {
    return '* ' + result;
  }
};

this.emailAccountMaxSize = function(field, rules, i, options) {
  var account_max, domain_len, len, mail_domain, max;
  max = Number(rules[i + 2]);
  mail_domain = document.getElementById(rules[i + 3]).value;
  domain_len = mail_domain.length;
  account_max = max - domain_len - 1;
  len = field.val().length;
  if (len > account_max) {
    return "* " + account_max + "文字以下にしてください";
  }
};

this.fileType = function(field, rules, i, options) {
  var extensions, mimeFilter, uploadedFile;
  uploadedFile = field[0] || field;
  if (uploadedFile) {
    extensions = rules[i + 2];
    mimeFilter = new RegExp(extensions);
    if (!mimeFilter.test(uploadedFile.value.split('.').reverse()[0])) {
      return "* 画像形式が違います。";
    }
  }
};

this.fileMaxSize = function(field, rules, i, options) {
  var max, max_m, size, uploadedFile;
  uploadedFile = field[0] || field;
  if (uploadedFile && uploadedFile.files.length > 0) {
    max_m = Number(rules[i + 2]);
    max = max_m * 1024 * 1024;
    size = uploadedFile.files[0].size;
    if (size > max) {
      return "* ファイルサイズは" + max_m + "MB以下にしてください";
    }
  }
};

</script>
