//Global variables
var tempDay, tempMonth, tempYear;


function checkDate(form) {
 
    var startDateStr = form.dayField.value;
    //var endDateStr = document.view.enddate.value;
  
    //validate that date is correct format
    if (!validateDate(startDateStr, form)) {
        return false;
    }
  
    /* Split the string at every space and put the values into an array so,
    using the previous example, the first element in the array is "Wed", the
    second element is "Jan", the third element is "1", etc. */
    var startDate_array = startDateStr.split( '/' );
    //var endDate_array = endDateStr.split( '/' );


	var startDateMonth = parseInt(startDate_array[0],10);
	var startDateDay = parseInt(startDate_array[1],10);
	var startDateYear = parseInt(startDate_array[2],10);
	//var endDateMonth = parseInt(endDate_array[0],10);
	//var endDateDay = parseInt(endDate_array[1],10);
	//var endDateYear = parseInt(endDate_array[2],10);
 
    var tmpDate = new Date();
    //get today's date at midnight to compare to the entered string
    var todayDate = new Date(tmpDate.getFullYear(), tmpDate.getMonth(), tmpDate.getDate());
    var todayPlusTenDays = new Date(tmpDate.getFullYear(), tmpDate.getMonth(), tmpDate.getDate());
    //find date ten days from now since scheduling is only available until then
    todayPlusTenDays = todayPlusTenDays.setDate(todayPlusTenDays.getDate()+9);
    
    var startDateStr = new Date(startDateStr);
    todayPlusTenDays = new Date(todayPlusTenDays);
    
    //do not allow user to pick date either before today or within the ten day range   
    if ((startDateStr > todayPlusTenDays) || (startDateStr < todayDate)) {
        var tmpMonth = todayDate.getMonth() + 1
        var alertString = "Starting date must be between " + tmpMonth + "/" + todayDate.getDate() + "/" + todayDate.getFullYear();
        tmpMonth = todayPlusTenDays.getMonth() + 1
        alertString += " and " + tmpMonth + "/" + todayPlusTenDays.getDate() + "/" + todayPlusTenDays.getFullYear();
        alert(alertString + ".");
        form.dayField.focus();
        return false;
    }

    //needs to be true
    return true;
} 


function validateDate(startDateStr, form) {
	//Date format checker - make sure date is in correct format before submitting
	var dateFormat = /^\d{1,2}\/\d{1,2}\/\d{2,4}/;
	
	if(!dateFormat.test(startDateStr))
	{
		alert("Please check the formatting of the transit trip day you entered and resubmit.");
		form.dayField.focus();
		return false;
	}

	return true;
}