"use strict";

function isEmpty(inputStr) { 
  if (inputStr === null || inputStr === "") {
    return true;
  } 
  return false;
}
    
function validateRequired(field, alerttxt) {
  if (isEmpty(field.value)) {
    alert(alerttxt);
    return false;
  }
  return true;
}

function validateEmail(field) {
  var atPosition = field.value.indexOf("@");
  var dotPosition = field.value.lastIndexOf(".");
  if (atPosition < 1 || dotPosition - atPosition < 2) {
    alert("Email address format appears to be incorrect.");
    return false;
  } else {
    return true;
  }
}

function validate() {
  var myForm = document.getElementById("familyGroupSubmission");
  if (validateRequired(myForm.submitter_name, "Please enter your name.") === false) {
    myForm.submitter_name.focus();
    return false;
  }
  if (validateRequired(myForm.email, "Please enter your email address.") === false) {
    myForm.email.focus();
    return false;
  }
  if (validateEmail(myForm.email) === false) {
    myForm.email.focus();
    return false;
  }
  return true;
}

function currentTime() {
  var date = new Date();

  var hour = date.getHours();
  var minute = date.getMinutes();
  var second = date.getSeconds();
  
  if (hour < 10) {
    hour = "0" + hour;
  }
  if (minute < 10) {
    minute = "0" + minute;
  }
  if (second < 10) {
    second = "0" + second;
  }
  var myForm = document.getElementById("familyGroupSubmission");
  myForm.currentTime.value = hour + ":" + minute + ":" + second;
  return true;
}

function currentDate() {
  var mydate = new Date();
  var theyear = mydate.getYear();
  if (theyear < 1000) {
    theyear += 1900;
  }
  var themonth = mydate.getMonth() + 1;
  var themonthMmm = "";
  if (themonth === 1) {
    themonthMmm = "Jan";
  } else if (themonth === 2) {
    themonthMmm = "Feb";
  } else if (themonth === 3) {
    themonthMmm = "Mar";
  } else if (themonth === 4) {
    themonthMmm = "Apr";
  } else if (themonth === 5) {
    themonthMmm = "May";
  } else if (themonth === 6) {
    themonthMmm = "Jun";
  } else if (themonth === 7) {
    themonthMmm = "Jul";
  } else if (themonth === 8) {
    themonthMmm = "Aug";
  } else if (themonth === 9) {
    themonthMmm = "Sep";
  } else if (themonth === 10) {
    themonthMmm = "Oct";
  } else if (themonth === 11) {
    themonthMmm = "Nov";
  } else if (themonth === 12) {
    themonthMmm = "Dec";
  }
  if (themonth < 10) {
    themonth = "0" + themonth;
  }
  var theday = mydate.getDate();
  if (theday < 10) {
    theday = "0" + theday;
  }

//////EDIT below three variable to customize the format of the date/////
  var displayfirst = theday;
  var displaysecond = themonthMmm;
  var displaythird = theyear;
////////////////////////////////////

  var myForm = document.getElementById("familyGroupSubmission");
  myForm.currentDate.value = displayfirst + " " + displaysecond + " " + displaythird;
  return true;
}

