// validation for past dates and pickup and return comparision

function check_dates()
{
    myDate = new Date();
	mm=myDate.getMonth()+1;
  
  if(document.getElementById("city").disabled==false)
	{
      if(document.getElementById("city").value=="0")
		{	     
		 document.getElementById("div_error").innerHTML="Select City";
		 return false;
	    }
		else
		{
		document.getElementById("div_error").innerHTML="";
		}  
    } 
  
  
  if(document.getElementById("region").value=="0")
	{
     document.getElementById("div_error").innerHTML="Select Neighborhood";
	 return false;
    }
	else
	{
	 document.getElementById("div_error").innerHTML="";
	}

  if(document.getElementById("datum1").value=="")
	{
     document.getElementById("div_error").innerHTML="Select Pickup date";
	 return false;
    }
	else
	{
	 document.getElementById("div_error").innerHTML="";
	}

	if(document.getElementById("datum2").value=="")
	{
     document.getElementById("div_error").innerHTML="Select Return date";
	 return false;
    }
	else
	{
	 document.getElementById("div_error").innerHTML="";
	}

	var today = mm+"/"+myDate.getDate()+"/"+myDate.getFullYear();
	
	var SDate,EDate;
	SDate=document.getElementById("datum1").value;
    EDate=document.getElementById("datum2").value;

	
	var arr1 = SDate.split("/");
	var start_m= arr1[0];
	var start_d = arr1[1];
	var start_y = arr1[2];

	
	var arr = EDate.split("/");
	var end_m= arr[0];
	var end_d = arr[1];
	var end_y = arr[2];

	var endDate = new Date(EDate);
	var startDate= new Date(SDate);
	var endTime=new Date(EDate+" "+document.getElementById('listreturntime').value);
	var startTime=new Date(SDate+" "+document.getElementById('listpickuptime').value);
	
	/*if(SDate != '' && SDate<today)
	{
	 document.getElementById("div_error").innerHTML="Invalid Pickup Date";
	}
	if(EDate != '' && EDate<today)
	{
	 document.getElementById("div_error").innerHTML="Invalid Return Date";
	}*/

	var currentdate = new Date();
	var currmm=currentdate.getMonth()+1;
	var currday=currentdate.getDate();
	var curryear=currentdate.getFullYear();	

	if (SDate != "")
	{	
	
	
		
	if((start_y==curryear)&&(start_m==currmm)&&(start_d<currday))
	{
	document.getElementById("div_error").innerHTML="Pickup date should not be in the past";
	return false;
	}
	if((start_y<curryear)&&(start_m==currmm)&&((start_d<=currday)||(start_d>=currday)))
	{
	document.getElementById("div_error").innerHTML="Pickup date should not be in the past";
	return false;
	}
	if((start_y<curryear)&&((start_m<=currmm)||(start_m>=currmm))&&((start_d<=currday)||(start_d>=currday)))
	{
	document.getElementById("div_error").innerHTML="Pickup date should not be in the past";
	return false;
	}	if((start_y==curryear)&&(start_m<currmm)&&((start_d<=currday)||(start_d>=currday)))
	{
	document.getElementById("div_error").innerHTML="Pickup date should not be in the past";
	return false;
	}
	 if((startTime.getTime()-currentdate.getTime())<1000*60*60*24){
	document.getElementById("div_error").innerHTML="Sorry, Spagg requires a minimum of 24 hours advance notice to reserve a car.";
	return false;
		
	}
	}

	if (EDate != "")
	{	
	
	if((end_y==curryear)&&(end_m==currmm)&&(end_d<currday))
	{
	document.getElementById("div_error").innerHTML="Return date should not be in the past";
	return false;
	}
	if((end_y<curryear)&&(end_m==currmm)&&((end_d<=currday)||(end_d>=currday)))
	{
	document.getElementById("div_error").innerHTML="Return date should not be in the past";
	return false;
	}
	if((end_y<curryear)&&((end_m<=currmm)||(end_m>=currmm))&&((end_d<=currday)||(end_d>=currday)))
	{
	document.getElementById("div_error").innerHTML="Return date should not be in the past";
	return false;
	}	if((end_y==curryear)&&(end_m<currmm)&&((end_d<=currday)||(end_d>=currday)))
	{
	document.getElementById("div_error").innerHTML="Return date should not be in the past";
	return false;
	}
	 
	}

   
    //Return date should be greter tahn pickupdate

	if(SDate != '' && EDate != '' && startDate > endDate)
	{
	document.getElementById("div_error").innerHTML="Return date should be after the Pickup date";
	return false;
	}
	if((endTime.getTime()-startTime.getTime())<1000*60*60*48){
	document.getElementById("div_error").innerHTML="Sorry, Spagg requires a minimum of 2 full days of rental.";
	return false;
	}
	
	
	return true;
}