// Haleakala Sunrise Forecast .com functions

function check_forecast_input(f) {

  if (selectIsEmpty(f.date_year)) {
    alert("The date's year needs to be selected.");
    f.date_year.focus();
    return false;
  }

  if (selectIsEmpty(f.date_month)) {
    alert("The date's month needs to be selected.");
    f.date_month.focus();
    return false;
  }

  if (selectIsEmpty(f.date_date)) {
    alert("The date's date needs to be selected.");
    f.date_date.focus();
    return false;
  }

  if (selectIsEmpty(f.am_pm)) {
    alert("The am/pm field needs to be selected.");
    f.am_pm.focus();
    return false;
  }

  if (selectIsEmpty(f.rating)) {
    alert("The rating needs to be selected.");
    f.rating.focus();
    return false;
  }

  if (! isDigits(f.wind_mph)) {
    alert("The wind speed needs to be an integer.");
    f.wind_mph.focus();
    return false;
  }

  if (! isDigits(f.wind_chill_f)) {
    alert("The wind chill factor needs to be an integer.");
    f.wind_chill_f.focus();
    return false;
  }

  if (! isDigits(f.temperature_f)) {
    alert("The temperature needs to be an integer.");
    f.temperature_f.focus();
    return false;
  }

  return true;
}

function check_observation_input(f) {

  if (selectIsEmpty(f.date_year)) {
    alert("The date's year needs to be selected.");
    f.date_year.focus();
    return false;
  }

  if (selectIsEmpty(f.date_month)) {
    alert("The date's month needs to be selected.");
    f.date_month.focus();
    return false;
  }

  if (selectIsEmpty(f.date_date)) {
    alert("The date's date needs to be selected.");
    f.date_date.focus();
    return false;
  }

  if (selectIsEmpty(f.rating)) {
    alert("The rating needs to be selected.");
    f.rating.focus();
    return false;
  }

  return true;
}

function check_registration_input(f) { // TODO

  if (isNotEmail(f.email)) {
    alert('Please enter your Email Address.');
    f.email.focus();
    return false;
  }

  if (!/^\d{5,}$/.test(f.code.value)) {
    alert('Please enter a call-in code of at least 5 digits.');
    f.password.focus();
    return false;
  }

  if (isEmpty(f.first_name)) {
    alert('Please enter your First Name.');
    f.first_name.focus();
    return false;
  }

  if (isEmpty(f.last_name)) {
    alert('Please enter your Last Name.');
    f.last_name.focus();
    return false;
  }

  // init dates
  var today     = new Date();
  var startDate = new Date(f.service_start_date_year.value, f.service_start_date_month.value - 1, f.service_start_date_date.value,  0, 0, 0);
  var endDate   = new Date(f.service_end_date_year.value,   f.service_end_date_month.value - 1,   f.service_end_date_date.value,    0, 0, 0);

  // start date not in the past
  var yesterday = new Date(today.getYear(), today.getMonth(), today.getDate(), 0, 0, 0);
  yesterday.setDate(today.getDate()-1);
  if (yesterday >= startDate) {
    alert('Please enter a valid Start Date (today or a future date).');
    f.service_start_date_year.focus();
    return false;
  }

  // start date before end date
  if (endDate < startDate) {
    alert('Please make sure that the Start Date is before the End Date');
    f.service_start_date_year.focus();
    return false;
  }

  // end date not more than 30 days last start date
  var thirtyDaysAfterStartDate = new Date(f.service_start_date_year.value, f.service_start_date_month.value - 1, f.service_start_date_date.value, 0, 0, 0);
  thirtyDaysAfterStartDate.setDate(thirtyDaysAfterStartDate.getDate() + 30);
  if (endDate > thirtyDaysAfterStartDate) {
    alert('Please enter an End Date that is within 30 days of the Start Date.');
    f.service_end_date_year.focus();
    return false;
  }

  return true;
}

function check_profile_input(f) {
  return true;
}

function check_profile_password_input(f) {
  if (!/\S/.test(f.old_pw.value)) {
    alert("Please enter your old password.");
    f.old_pw.focus();
    return false;
  }

  if (f.new_pw1.value != f.new_pw2.value) {
    alert("The new passwords do not match.");
    f.new_pw1.focus();
    return false;
  }

  return true;
}

function check_password_reminder_input(f) {
  if (isNotEmail(f.email)) {
    alert('Please enter your email address.');
    return false;
  }

  return true;
}

function check_observation_input(f) {
  if (selectIsEmpty(f.date_year)) {
    alert('Please select a year.');
    return false;
  }

  if (selectIsEmpty(f.date_month)) {
    alert('Please select a month.');
    return false;
  }

  if (selectIsEmpty(f.date_date)) {
    alert('Please select a date.');
    return false;
  }

  if (selectIsEmpty(f.rating)) {
    alert('Please select a rating.');
    return false;
  }

  return true;
}

function onPhoneKeyUp(phone_element) {
  if (phone_element.value.replace(/\D/, '').length >= 10) {
    phone_element.form.phone_number_test.disabled = false;
  }
  else {
    phone_element.form.phone_number_test.disabled = true;
  }
}

function doPhoneNumberTest(phone_number) {
  var all_numbers = phone_number.replace(/\D/, '');
  if (all_numbers.length < 10) {
    alert('It looks like the number you entered is too short.\nTen digits are required.');
    return;
  }

  $.ajax({
    url: '/test-phone-number.php',
    type: 'POST',
    data: { 'phone_number': all_numbers },
    error: function() { alert('There was an error. Please try again later.'); },
    success: function(json) {
      var data;
      try {
        data = eval(json);
      }
      catch (e) {
        alert('There was an error.');
        return;
      }

      if (data[0]) { // success
        alert(data[1]);
      }
      else { // failure, but alert the message just the same
        alert(data[1]);
      }
    }
  });

  alert("I'm trying to call your phone now.\nThis will take just a moment...");
}
