// Validate the Add or Update News Item form.

function validate_newsitem_form( ) {
  valid = true;
  // Check the Publish Date field.
  if ( ! document.NewsItemForm.PublishDate.value ) {
    alert ( "'Publish Date' must be entered.");
    valid = false;
  } else {
    if (! isValidDate(document.NewsItemForm.PublishDate.value)) {
      alert ("'Publish Date' is not in the correct format (MM/DD/YYYY), or is less than 01/01/2009 or greater than 12/31/2100");
      valid = false;
    }
  } // End of: 'if ( document.NewsItemForm.PublishDate.value == '' ) {'  
  
  // Check the Expiry Date field.
  if (document.NewsItemForm.ExpiryDate) {
    if (! isValidDate(document.NewsItemForm.ExpiryDate.value)) {
      alert ("'Expiry Date' is not in the correct format (MM/DD/YYYY), or is less than 01/01/2009 or greater than 12/31/2100");
      valid = false;
    }
  } // End of: 'if (document.NewsItemForm.ExpiryDate) {'
  
  return valid;
}


// Original:  Sandeep V. Tamhankar (stamhankar@hotmail.com)
// This script and many more are available free online at
// The JavaScript Source!! http://javascript.internet.com

function isValidDate(dateStr) {
  // Checks for the following valid date formats:
  // MM/DD/YYYY
  // Also separates date into month, day, and year variables

  //var datePat = /^(\d{1,2})(\/)(\d{1,2})\2(\d{2}|\d{4})$/;

  // To require a 4 digit year entry, use this line instead:
  var datePat = /^(\d{1,2})(\/)(\d{1,2})\2(\d{4})$/;

  var matchArray = dateStr.match(datePat); // is the format ok?
  if (matchArray == null) {
    return false;
  }
  month = matchArray[1]; // parse date into variables
  day = matchArray[3];
  year = matchArray[4];
  if (month < 1 || month > 12) { // check month range
    return false;
  }
  if (day < 1 || day > 31) {
    return false;
  }
  if ((month==4 || month==6 || month==9 || month==11) && day==31) {
    return false
  }
  if (month == 2) { // check for february 29th
    var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
    if (day>29 || (day==29 && !isleap)) {
      return false;
    }
  }
  if ((year < 2009) || (year > 2100)) {
    return false;
  }
  return true;  // date is valid
}

