function getEvents(day) {
  document.calendarSearch.action.value='listDate';
  document.calendarSearch.day.value=day;
  document.calendarSearch.submit();
  return;
}

function monthScroll(dir) {
  document.calendarSearch.action.value = 'scrollmonth';
  document.calendarSearch.dir.value = dir;
  document.calendarSearch.submit();
  return;
}

function monthList() {
  document.calendarSearch.action.value='monthList';
  document.calendarSearch.submit();
  return;
}

function nextEvents() {
  var offset = document.eventList.offset.value - 0;
  var maxEvents = document.eventList.n.value - 0;
  document.eventList.offset.value = offset + maxEvents;
  document.eventList.submit();
  return;
}

function prevEvents() {
  var offset = document.eventList.offset.value - 0;
  var maxEvents = document.eventList.n.value - 0;
  document.eventList.offset.value = offset - maxEvents;
  document.eventList.submit();
  return;
}

function validateDate(myStrDate, startend) {
  if(myStrDate.match(/^[0-9]{4}(\-)[0-9]{2}(\-)[0-9]{2}$/)) {
    var date_array = myStrDate.split("-");
    if(date_array[0] < 2005 || date_array[0] > 2010) { alert("The event year must be between 2005 and 2010"); return 0; }
    if(date_array[1] < 1 || date_array[1] > 12) { alert("The event month must be between 1 and 12"); return 0; }
    if(date_array[2] < 1 || date_array[2] > 31) { alert("The event day must be between 1 and 31"); return 0; }
    var month_num = date_array[1];
    var month = monthName(month_num);
    var dateObj = new Date(month + " " + date_array[2] + ", " + date_array[0]);
    if(!dateObj) {
      alert("Please use the calendar popup tool to select a valid " + startend + " date");
      return 0;
    }
    return dateObj;
  }
  alert("The event " + startend + " date must have the format YYYY-MM-DD");
  return 0;
}

function checkTime(startTime, endTime) {
  var startStr;
  var endStr;
  var startTime_array;
  var endTime_array;
  if(startTime) {
    if(endTime) {
      startStr = new String(startTime);
      endStr = new String(endTime);
      startTime_array = startStr.split(":");
      endTime_array = endStr.split(":");
      if((startTime_array[0]) > (endTime_array[0])) {
        alert("The start time may not fall before the end time");
        return 0;
      }
    }
  }
  return 1;
}

function monthName(month_num) {
    var month;
    if(month_num == 1) {month = "january";}
    else if(month_num == 2) {month = "february";}
    else if(month_num == 3 ) {month = "march";}
    else if(month_num == 4) {month = "april";}
    else if(month_num == 5) {month = "may";}
    else if(month_num == 6) {month = "june";}
    else if(month_num == 7) {month = "july";}
    else if(month_num == 8) {month = "august";}
    else if(month_num == 9) {month = "september";}
    else if(month_num == 10) {month = "october";}
    else if(month_num == 11) {month = "november";}
    else {month = "december";}
    return month;
}

function checkSearchForm() {
    var city = document.calendarSearch.city.value;
    var cityString = new String(city);
    if (checkForWhiteSpace(cityString) === true) {
        document.calendarSearch.city.value = "";
    }
    else {
        var have_city = 1;
    }
    var state = document.calendarSearch.state.value;
    if(state) {
        var have_state =1;
    }
    var zip = document.calendarSearch.zip.value;
    var zipString = new String(zip);
    if (checkForWhiteSpace(zipString) === true) {
        document.calendarSearch.zip.value = "";
    }
    else {
        if(!zipString.match(/^\d\d\d\d\d$/)) {
            if(!zipString.match(/^[a-zA-Z]\d[a-zA-Z]\s\d[a-zA-Z]\d$/)) {
                alert("Please enter a valid zip code.  US zip codes must have the form #####.  Canadian zip codes must have the form ### ###.");
                return;
            }
        }
        var have_zip = 1;
    }

    if (!((have_city && have_state) || have_zip)) {
        alert ("Please enter a city and state or a zip code");
        return;
    }

    var startDateObj = 0;
    var str = new String(document.calendarSearch.startDate.value);
    if (checkForWhiteSpace(str) === true) {
        document.calendarSearch.startDate.value = "";
    }
    else {
        startDateObj = validateDate(str, "start");
        if(startDateObj === 0) {
            return;
        }
    }
    var endDateObj;
    str = new String(document.calendarSearch.endDate.value);
    if (checkForWhiteSpace(str) === true) {
        document.calendarSearch.endDate.value = "";
    }
    else {
        endDateObj = validateDate(str, "end");
        if(endDateObj === 0) {
            return;
        }
        if(startDateObj) {
            if(startDateObj.getTime() > endDateObj.getTime()) {
                alert("The end date may not fall before the start date");
                return;
            }
        }
    }
    document.calendarSearch.submit();
    return;
}

function checkEmailForm() {
  var str = new String(document.contact.fromName.value);
  if( checkForWhiteSpace(str) ) {
      alert("Please enter your name.");
       document.contact.fromName.focus();
       return false;
   }
  var emailString = new String(document.contact.from.value);
  if(checkForWhiteSpace(emailString)) {
    alert("Please enter your email address.");
    document.contact.from.focus();
    return false;
  }
  else {
    if(!emailString.match(/^[^@]+@([-\w]+\.)+[A-Za-z]{2,4}$/)) {
      alert("Your email address does not have a valid format");
      document.contact.from.focus();      
      return false;
    }
  }
  var str = new String(document.contact.toName.value);
   if( checkForWhiteSpace(str) ) {
      alert("Please enter the recipient's name.");
       document.contact.toName.focus();
       return false;
   }
  var emailString = new String(document.contact.to.value);
  if(checkForWhiteSpace(emailString)) {
    alert("Please enter an email address for the recipient.");
    document.contact.to.focus();
    return false;
  }
  else {
    if(!emailString.match(/^[^@]+@([-\w]+\.)+[A-Za-z]{2,4}$/)) {
      alert("The recipient's email address does not have a valid format");
      document.contact.to.focus();      
      return false;
    }
  }  
 if( (document.contact.message.value.length <5) ) {
      alert("Please enter your message.");
       document.contact.message.focus();
       return false;
   }
  return true;
}  

// Check whether string s is empty.
function checkForEmpty(s) { 
  return ((s === null) || (s.length === 0)); 
}

function checkForWhiteSpace (s)  {
  var i;
  var whitespace = " \t\n\r";
  // Is s empty?
   if (checkForEmpty(s)) { return true; }
  
  for (i = 0; i < s.length; i=i+1) {
     // Check that current character isn't whitespace.
     var c = s.charAt(i);
    if (whitespace.indexOf(c) == -1) { return false; }
  }

  // All characters are whitespace.
   return true;
}
