function daysInFebruary (year)
			{return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );}

 			function validate_form ( )
			{
				valid = true;
//check branch is selected		
			var branch = document.dates.branch.value;
			if (branch == 0){
			alert ('Please choose which branch you wish to rent from');
			valid = false;
			return valid;
			}	
		
		
//set current date and then split it into an array
				x = new Date();
    		var year = x.getFullYear();
				var month = x.getMonth()+1;
				var day = x.getDate();
				var today = (day + "/" + month + "/" + year);
				var todayParts = today.split("/");
				var maxYear = (todayParts[2]);
	
//split collect and return dates into arrays
				var collectday = document.dates.collect_date.value;
				var returnday = document.dates.return_date.value;
				var collectdayParts = collectday.split("/");
				var returndayParts = returnday.split("/");
				var collectMonth=collectdayParts[1];
				var collectDay=collectdayParts[0];
				var collectYear=collectdayParts[2];
				var returnDay=returndayParts[0];
				var returnMonth=returndayParts[1];
				var returnYear=returndayParts[2];
				
if (collectYear.length == 2){var collectYear = 20+collectYear;}				
if (returnYear.length == 2){var returnYear = 20+returnYear;}
		
				var objRegExp = /^\d{1,2}(\/)\d{1,2}\1\d{2,4}$/;
 
		
//check to see if in correct format
  			if(!objRegExp.test(document.dates.return_date.value)) /*|| (!objRegExp.test(document.dates.collect_date.value))*/  
				{
	 			 			alert("Please use dd/mm/yyyy format for dates");
							valid=false;
							return valid; //doesn't match pattern, bad date
				}
				if (returnMonth == 9 || returnMonth == 4 || returnMonth == 6 || returnMonth == 11)
				{
				 	 		var daysinMonth = 30;
				}
				if (returnMonth == 2)
				{
				 	 		var daysinMonth = 29;
				}
				else
				{
				 			var daysinMonth = 31;
				}
		
				if (collectDay<1 || collectDay>31 || (collectMonth==2 && collectDay>daysInFebruary(collectYear)) || collectDay > daysinMonth[collectMonth])
			 	{
	 						alert("Please enter a valid collect day")
							valid = false;
							return valid;
				}
				if (returnDay<1 || returnDay>31 || (returnMonth==2 && returnDay>daysInFebruary(returnYear)) || returnDay > daysinMonth[returnMonth])
			 	{
	 						alert("Please enter a valid return day")
							valid = false;
							return valid;
				}
				if (collectYear.length !== 4 || collectYear==0){
					 		alert("Please enter a valid 4 digit year before end of year")
							valid = false;
							return valid;
				}
				if (collectMonth.length<1 || collectMonth<1 || collectMonth>12 || returnMonth.length<1 || returnMonth<1 || returnMonth>12)
				{
		 		 	 		alert("Please enter a valid month (dd/mm/yyyy)")
							valid = false;
							return valid;
				}		
				if ((Number(todayParts[2]) > Number(collectYear)) ||
					 	 (Number(todayParts[2]) == Number(collectYear) &&
						 ((Number(todayParts[1]) > Number(collectdayParts[1])) ||
						 ((Number(todayParts[1]) == Number(collectdayParts[1])) &&
						 (Number(todayParts[0]) > Number(collectdayParts[0])) ))))
				{
				 				alert ( "Your collect date is before today!" );
								valid = false;
								return valid;
				}
				if ((Number(todayParts[2]) > Number(returnYear)) ||
					 	 (Number(todayParts[2]) == Number(returnYear) &&
						 ((Number(todayParts[1]) > Number(returndayParts[1])) ||
						 ((Number(todayParts[1]) == Number(returndayParts[1])) &&
						 (Number(todayParts[0]) > Number(returndayParts[0])) ))))
				{
				 				alert ( "Your return date is before today!" );
								valid = false;
								return valid;
				}
        if ((Number(collectYear) > Number(returnYear)) ||
				 	 (Number(collectYear) == Number(returnYear) &&
					 ((Number(collectMonth) > Number(returnMonth)) ||
					 ((Number(collectMonth) == Number(returnMonth)) &&
					 (Number(collectDay) > Number(returnDay)) )))){
				 				alert ( "Your return date must be after your collect date" );
								valid = false;
								return valid;
				}
				if (collectday == returnday)
				{
				 				alert ( "Your booking must be for one day or more.");
								valid = false;
								return valid;
				}
				if ( document.dates.collect_date.value == "" )
        {
                alert ( "Please enter a collect date." );
                valid = false;
								return valid;
        }
        if ( document.dates.return_date.value == "" )
        {
                alert ( "Please enter a return date" );
                valid = false;
								return valid;
        }
				
var collectdate=new Date();
collectdate.setFullYear(collectYear,collectMonth-1,collectDay);

var returndate=new Date();
returndate.setFullYear(returnYear,returnMonth-1,returnDay);

var collectdayofweek = collectdate.getDay();
var returndayofweek = returndate.getDay();

				
if (collectdayofweek == 0 || returndayofweek == 0)
{
alert("Sorry we do not offer collection or return on a Sunday");
							valid = false;
							return valid;
}				
			return valid;
			}