//Set the maximum difference in years between todays date and year entered
var maxYearsFromNow = 50;

//If inputting a date for a previous year such as date of birth set maxYearsAgo
//to the maximum years ago that would be valid for the application
var maxYearsAgo = 0;

var validDate
var getInput;	  //This holds the input of the date textbox
	
var getSlash1;
var getSlash2;
var strDay, strMonth, strYear;
	
var leapYear;
var daysInMonth;

/*					  		   Store today's date	 			  			   */
var actualDate = new Date();

var thisDay = actualDate.getDate();
if (thisDay < 10)
{
   	 thisDay = "0" + thisDay;
}

var thisYear = actualDate.getYear();
var numMonth = actualDate.getMonth();
		 
switch (numMonth)
{
    case 0 : thisMonth = "01"; break;
    case 1 : thisMonth = "02"; break;
	case 2 : thisMonth = "03"; break;
	case 3 : thisMonth = "04"; break;
	case 4 : thisMonth = "05"; break;
	case 5 : thisMonth = "06"; break;
	case 6 : thisMonth = "07"; break;
	case 7 : thisMonth = "08"; break;
	case 8 : thisMonth = "09"; break;
	case 9 : thisMonth = "10"; break;
	case 10: thisMonth = "11"; break;
	case 11: thisMonth = "12"; break;
}
		
var thisDate = thisDay + "/" + thisMonth + "/" + thisYear;

/*				  		   	 Start of functions	 		  					   */

function isDate(obj)
{  
   		   //Date starts as true unless changed to false by failing check?
		   validDate = true;
		   
		   getInput = obj.value;
		   		   
		   if ((getInput.length) != 10)
		   {
		   	   validDate = false;		
		   }
		   
	       getSlash1 = getInput.substr(2,1);
		   getSlash2 = getInput.substr(5,1);
		   
		   if(getSlash1 != "/" || getSlash2 !="/")
		   {
		   		validDate = false;
		   }
		   
	 	   strDay = getInput.substr(0,2);
		   strMonth = getInput.substr(3,2);
		   strYear = getInput.substr(6,4);
		   
		   if (isNaN(strDay) || isNaN(strMonth) || isNaN(strYear))
		   {
		   	    validDate = false;
		   }
		   
		   /*Test whether date entered is before todays date
	 	   var maxFutureDate = (actualDate.getFullYear() + maxYearsFromNow);
		   var maxPrevDate = (actualDate.getFullYear() - maxYearsAgo);
		   		   
		   if (strYear > maxFutureDate )
		   {
		   	   validDate = false;
		   }
		   else if (strYear < maxPrevDate)		   
		   {
		   	   validDate = false;
		   }
		   
		   
		   */
		   
	 	   if (strMonth > 12 || strMonth < 1)
		   {
		   	   validDate = false;
		   }
		   
		   //Check if is a leap year
	 	   if (((strYear % 4 == 0) && strYear % 100 != 0) || strYear % 400 == 0)
		   {
  			 	 leapYear = true;
		   }
 		   else
		   {
  			 	 leapYear = false;				 
		   }
			 
	 	   //Find how many days are in the month 
		   if (strMonth == 4 || strMonth == 6 || strMonth == 9 || strMonth == 11)
		   {
			  	daysInMonth = 30;
		   }
 		   else if (strMonth == 2)
		   { 	
			 	if (leapYear == true)
			   	{
			  	  daysInMonth = 29;
			 	}
 			  	else
			 	{
			  	  daysInMonth = 28;
			 	}
		   }
 		   else
		   {
			  	daysInMonth = 31;
		   }
			 
		   //Check if it is a valid day
		   var intGetInput = strYear + strMonth + strDay
			 
		   if (strDay > daysInMonth || strDay < 1)
		   {
			  	 validDate = false;
		   }
		   
//		   Test whether date entered is before todays date
		   if (strYear < thisDate.substr(6,4))
		   {
		   	  	  validDate = false;
		   }
		   else
		   {
		        if (strMonth < thisDate.substr(3, 2))		   
		        {
		   	   	   validDate = false;
		   	    }
		   	   	else if (strMonth == thisDate.substr(3, 2))
				{
				 	if (strDay <= thisDate.substr(0, 2))
		   			{
		   	   	   	   	validDate = false
					}
		   	   	}
	   	   }
		   
		   //Final check
		   if (validDate == false && getInput != "")
		   {
		   	  	 alert(getInput + " is not a valid date for this application.\n\n" +
				 "Please enter the date in the format DD/MM/YYYY.\n" +
				 "e.g. the correct format for today's date is " + thisDate);
				 obj.value = "";
		         obj.focus();
				 return false;
		   }
		   else if(validDate == true)
		   {
			  	 var dFrom = new Date();
				 dFrom = getInput;
				 return true;
		   }
		   else
		   {
		   	   	 return false;
		   }
}

function displayToday()
{
 		 alert("Today's date is " + thisDate);
}

function compareDates(inputStart, inputEnd)
{
 	 var start = inputStart.value;
	 var end = inputEnd.value;

	 var rightOrder = true;
	 
 	 if(start !="" && end !="")
	 {
	  	   if (isDate(inputStart) && isDate(inputEnd))
		   {
	  	   	  var startYear = start.substr(6,4);
			  var startMonth = start.substr(3,2);
			  var startDay = start.substr(0,2);
			  
			  var endYear = end.substr(6,4);
			  var endMonth = end.substr(3, 2);
			  var endDay = end.substr(0, 2);
			  
			  if (startYear == endYear)
			  {
			   	     if(startMonth == endMonth)
				     {
				  	        if(startDay > endDay)
					        {
					   		      //start day is bigger than end day
					   	          rightOrder = false;
					        }
					 }
				  	 else if (startMonth > endMonth)
				 	 {
				  	   	    //start month is greater than end month
				  	 		rightOrder = false;
				 	 }
			  } 	  
			  else if (startYear > endYear)
			  {
			   	   	  	  //start year is greater than end year
			   	  		  rightOrder = false;
			  }
			  
		   }
	 }	 
	 	 
	 //Checked through the dates so...
	 if (rightOrder == false)
	 {
		   alert("Start date cannot be later than end date.");
		   inputStart.value = "";
	       inputStart.focus();
		   return false;
	 }
	 else 
	 {
		   return true;
	 }
	 
}