//數字格式驗證
function isNum(isNum) {
    var reg = /^[0-9]*$/;
    if (!reg.test(isNum) || isNum == '') {
        return false;
    } else {
        return true;
    }
}
//手機格式檢查
function Isphone(phone) {
    var regex = /^[0-9]+$/;
    if (!regex.test(phone)) {
        return false;
    } else {
        return true;
    }
}
//E-MAIL格式檢查
function IsEmail(email) {
    var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (!regex.test(email)) {
        return false;
    } else {
        return true;
    }
}
//時間格式檢查
function isValidDate(dateString) {
    // First check for the pattern
    /*  var regex_date = /^\d{4}\/\d{1,2}\/\d{1,2}$/;
      if(!regex_date.test(dateString)){
          return false;
      }*/
    // Parse the date parts to integers
    var parts = dateString.split("-");
    var day = parseInt(parts[2], 10);
    var month = parseInt(parts[1], 10);
    var year = parseInt(parts[0], 10);
    // Check the ranges of month and year
    if ((year < 1000) || (year > 3000) || (month == 0) || (month > 12)) {
        return false;
    }
    // Set dates for each month and adjust for leap years
    var monthLength = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) {
        monthLength[1] = 29;
    }
    // Check the range of the day
    return day > 0 && day <= monthLength[month - 1];
}

//驗證重複uname及uemail->ajax
function userinboxfunction($type, $dbid, $required,$mailrequired,controlemail) {
  var $ajaxsetup = $("#userinboxtext").attr("name");
  var $ajaxsetup_arr = $ajaxsetup.split(','); //切割陣列 
  $.ajax({
    url: xoopsjsurl + '/modules/neillibrary/ajax.php',
    type: 'POST',
    data: {
      id: 184,
      type: $type,
      var1: $ajaxsetup_arr[0], //centerid
      var2: $ajaxsetup_arr[1], //sortid 
      var3: moduleid, //modules
      dbid: $dbid
    },
    success: function(response) {
      if (response) {
        $('#userinboxtext' + $type).html(response);
        $($required).show().text(jstextArr['required']);
        if ($type == '3') {
          $(controlemail).val("");
          $($mailrequired).show().text(jstextArr['required']);
        }
      } else {
        $('#userinboxtext' + $type).html("");
        $($required).hide().text(true);
        if ($type == '3') {
          $(controlemail).val($dbid);
          $($mailrequired).hide().text(true);
        }
      }
    },
    error: function() {
      console.log('ajax error!');
    }
  })
}

//自訂表單上傳檔案驗證
function uploadfileverification(boxid, fsetup, fieldArr, blockeach) {
    var form_data = new FormData(); //建構new FormData()
    for (var i = 0; i < $(boxid)[0].files.length; i++) {
        form_data.append('file[]', $(boxid)[0].files[i]);
    }
    form_data.append('id', '187');
    form_data.append('var1', fsetup);
    form_data.append('var2', fieldArr);
    form_data.append('var3', blockeach);
    var result = false; //預設值
    $.ajax({
        url: xoopsjsurl + '/modules/neillibrary/ajax.php',
        cache: false,
        contentType: false,
        processData: false,
        data: form_data, //data只能指定單一物件                 
        type: 'post',
        async: false,
        success: function(data) {
            result = data; //回傳值
        }
    });
    return result;
}

//網址驗證
function urlverify(url) {
  const regex = /^(https?|ftp):\/\/[^\s/$.?#].[^\s]*$/;
  return regex.test(url);
}

//上傳檔案類型驗證
function filetypevalidation(id, name, boxid,operate) {
  //name->upfile[]
  //boxid->#namefile
  //project->1上傳檔案驗證 / 2上傳圖檔驗證
  var form_data = new FormData(); //建構new FormData()
  for (var i = 0; i < $(boxid)[0].files.length; i++) {
    form_data.append(name, $(boxid)[0].files[i]);
  }
  form_data.append('id', id);
  form_data.append('operate', operate);
  var result = false; //預設值
  $.ajax({
    url: xoopsjsurl + '/modules/neillibrary/ajax.php',
    cache: false,
    contentType: false,
    processData: false,
    data: form_data, //data只能指定單一物件                 
    type: 'post',
    async: false,
    success: function(data) {
      result = data; //回傳值
    }
  });
  return result;
}

//輸入字數檢驗
function wordcountcheck(id, content, modulesid) {
  var result = false; //預設值
  $.ajax({
    url: xoopsjsurl + '/modules/' + modulesid + '/ajax.php',
    type: 'POST',
    async: false, //回傳值設定  
    data: {
      id: id,
      dbid: content
    },
    success: function(response) {
      result = response; //回傳值
    },
    error: function() {
      console.log('ajax error!');
    }
  })
  return result;
}