$(document).ready(function() { 
	
	// DATEPICKER
	$( "#date_from" ).datepicker();
	$( "#date_to" ).datepicker();
	

	// DEFAULT DATE
	var startDate = new Date();
	startDate.setDate(startDate.getDate()+1); // day of tomorrow
	var startMonth = startDate.getMonth()+1;
	var startDay = startDate.getDate();
	var startYear = startDate.getFullYear();
	var formatStartDate = startMonth+"/"+startDay+"/"+startYear;
	
	var endDate = new Date();
	endDate.setDate(startDate.getDate()+1); // day after tomorrow
	var endMonth = endDate.getMonth()+1;
	var endDay = endDate.getDate();
	var endYear = endDate.getFullYear();
	var formatEndDate = endMonth+"/"+endDay+"/"+endYear;
	
	$('#date_from').val(formatStartDate);
	$('#date_to').val(formatEndDate);
	
	
	// SELECT FIELDS
	var activityOptionsContent = $('#activityNameId').html();
	
	$('#typeId').change(function () {
		
		var value = $(this).val();
		var classItem = 'activityType'+value;
		var reg = new RegExp("[ ]+", "g");
		
		// If select all, show everything
		if (value == "-1"){
			$("#activityNameId").html(activityOptionsContent);
		// else, show the all tour option + specified options
		}else{
			$("#activityNameId").html(activityOptionsContent);
			$("#activityNameId").find('option').each( function () { 
																
				var arrClass = $(this).attr("class").split(reg);
				if ( arrClass[0] != classItem && arrClass[1] != classItem && $(this).attr('class') != "all"){
					$(this).remove();	
				}
			});
		}
		
		// Change the value of the activityNameId
		$("#activityNameId").val("-1");
		
	});
	
	
	$('#date_from').change(function () {
		
		// Get the time of the from and to date
		var date_from_time =  stringToTime( $('#date_from').val() );
		var date_to_time =  stringToTime( $('#date_to').val() );
		
		// check if the from_date is superior to to_date
		if (date_from_time > date_to_time){
			// Change to_date to be 1 day after from_date
			var tempEndDate = new Date();
			//tempEndDate.setTime(date_from_time + 24 * 3600 * 1000); // day after date_from_time
			tempEndDate.setTime(date_from_time);
			var tempEndMonth = tempEndDate.getMonth()+1;
			var tempEndDay = tempEndDate.getDate();
			var tempEndYear = tempEndDate.getFullYear();
			var formatTempEndDate = tempEndMonth+"/"+tempEndDay+"/"+tempEndYear;
			
			$('#date_to').val(formatTempEndDate);
		}
	});
	
	
	$('#date_to').change(function () {
		
		// Get the time of the from and to date
		var date_from_time =  stringToTime( $('#date_from').val() );
		var date_to_time =  stringToTime( $('#date_to').val() );
		
		// check if the from_date is superior to to_date
		if (date_to_time < date_from_time){
			// Change to_date to be 1 day after from_date
			var tempEndDate = new Date();
			//tempEndDate.setTime(date_from_time + 24 * 3600 * 1000); // day after date_from_time
			tempEndDate.setTime(date_to_time);
			var tempEndMonth = tempEndDate.getMonth()+1;
			var tempEndDay = tempEndDate.getDate();
			var tempEndYear = tempEndDate.getFullYear();
			var formatTempEndDate = tempEndMonth+"/"+tempEndDay+"/"+tempEndYear;
			
			$('#date_from').val(formatTempEndDate);
		}
	});
	
	
});



function stringToTime (string){
	var datePieces = string.split("/");
	var month = parseFloat (datePieces[0]) - 1;
	var day = parseFloat (datePieces[1]);
	var year = parseFloat (datePieces[2]);
	date = new Date(year,month,day);
	return date.getTime();
}

