// JavaScript for Felpham Parish Council
// =====================================
// M J Knight - 17/07/2009
// =====================================
function search_setfocus() {
   window.document.searchform.q.focus();
   window.document.searchform.q.select();
}

function submit_form() {
   if (window.document.searchform.q.value=='') {
   } else {
      window.document.searchform.submit();
   }
}
function validate_date(checkDate) {
         // 1 = Invalid day
         // 2 = invalid month
         // 3 = invalid year
         // 4 = date entered < today's date
         // 5 = hyphens not used as separators
         errorCode = 0;
         days = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
         today = new Date();
         newToday = ""
         todayDay = today.getDate();
         todayMonth = today.getMonth() + 1;
         todayYear = today.getFullYear();
         startYear = todayYear;
         endYear = todayYear + 1;
         // Build newToday as today's date in yyyymmdd format
         newToday = todayYear;
         if (todayMonth < 10) {
            newToday = newToday + "0" + todayMonth;
         } else {
            newToday = newToday + "" + todayMonth;
         }
         if (todayDay < 10) {
            newToday = newToday + "0" + todayDay;
         } else {
            newToday = newToday + "" + todayDay;
         }
         newDate = checkDate.substring(6,10) + checkDate.substring(3,5) + checkDate.substring(0,2);
         newDay = parseInt(checkDate.substring(0,2),10);
         newMonth = parseInt(checkDate.substring(3,5),10);
         newYear = parseInt(checkDate.substring(6,10),10);
         hyphen1 = checkDate.substring(2,3)
         hyphen2 = checkDate.substring(5,6)
         // Check the month
         if ((newMonth < 1) || (newMonth > 12) || (isNaN(newMonth))) {
            errorCode = 2
         }
         // Check the year
         if ((newYear < startYear) || (newYear > endYear) || (isNaN(newYear))) {
            errorCode = 3
			alert(todayYear);
  		    alert(checkDate);
		    alert(newYear);
		    alert(startYear);
		    alert(endYear);
         }
         // Check the day
         if (errorCode == 0) {
            // Check for leap year
            febCheck1 = parseInt((newYear / 4),10);
       	 febCheck2 = febCheck1 * 4;
       	 if (febCheck2 == newYear) {
       	    days[1] = 29;
       	 }
            checkMonth = newMonth - 1;	
            if ((newDay < 1) || (newDay > days[checkMonth]) || (isNaN(newDay))) {
               errorCode = 1;
       	 }
         }
         // Make sure hyphens are used as separators
         if (errorCode == 0 && (hyphen1 != "-" || hyphen2 != "-")) {
            errorCode = 5;
         }
         // Set errorCode to 4 if the date entered is not greater than today's date
         if (errorCode == 0 && newDate < newToday) {
            errorCode = 4;
         }
         // Check the error code and display a suitable message if <> 0
         if (errorCode != 0) {
       	 if (errorCode == 4) {
       	    alert ("The date you enter MUST not be less than today's date");
       	 } else {
       	    alert ("Please enter a valid date in the format dd-mm-yyyy");
       	 }
            return false;
         } else {
            return true;
         }
}
function submit_search() {
         // *****************************************************
         // Validate the form and, if all okay, submit the search
         // *****************************************************
         if (window.document.eventSearch.eventStartDate.value == "" || window.document.eventSearch.eventStartDate.value.length != 10) {
            window.document.eventSearch.eventStartDate.focus();
         } else {
            if (!validate_date(window.document.eventSearch.eventStartDate.value)) {
               window.document.eventSearch.eventStartDate.focus();
            } else {
               if (window.document.eventSearch.eventEndDate.value != "" || window.document.eventSearch.eventEndDate.value.length != 0) {
                  if (!validate_date(window.document.eventSearch.eventEndDate.value)) {
                     window.document.eventSearch.eventEndDate.focus();
                  } else {
                     checkDate = window.document.eventSearch.eventStartDate.value;
                     newStartDate = checkDate.substring(6,10) + checkDate.substring(3,5) + checkDate.substring(0,2);
                     checkDate = window.document.eventSearch.eventEndDate.value;
                     newEndDate = checkDate.substring(6,10) + checkDate.substring(3,5) + checkDate.substring(0,2);
                     if (newStartDate > newEndDate) {
                        alert ("The end date, if entered, must be greater than the start date");
                        window.document.eventSearch.eventEndDate.focus();
                     } else {
                        window.document.eventSearch.submit();
                     }
                  }
               } else {
                  window.document.eventSearch.submit();
               }
            }
         }
}
function validate_time(checkTime) {
         timeHours = parseInt(checkTime.substring(0,2),10);
         timeMinutes = parseInt(checkTime.substring(3,5),10);
         timeSeparator = checkTime.substring(2,3);
         if (timeSeparator != ":") {
            alert ("Please enter a valid time in the format hh:mm");
            return false;
         } else {
            if ((timeHours < 0) || (timeHours > 23) || (isNaN(timeHours))) {
               alert ("Please enter a valid time in the format hh:mm");
               return false;
            } else {
               if ((timeMinutes < 0) || (timeMinutes > 59) || (isNaN(timeMinutes))) {
                  alert ("Please enter a valid time in the format hh:mm");
                  return false;
               } else {
                  return true;
               }
            }
         }
}
