$(document).ready(function()
{
	

	$('#vehtypex option:first').attr('selected','selected');

	$('#vehtypex').change(function()
	{
		mask();
		$.post('include/getLocation.php', {vtype: $(this).val()},
            function(data){
					locations=data.locations;
					var select1 = $('#pu_location');
					var options1 = select1.attr('options');
					$('option', select1).remove();
					
					var select2 = $('#rt_location');
					var options2 = select2.attr('options');
					$('option', select2).remove();
					
					$.each(data.locations, function(index, array) {
						options1[options1.length] = new Option(array['location_name'],array['id']);
					});

					$.each(data.locations, function(index, array) {
						options2[options2.length] = new Option(array['location_name'],array['id']);
					});
					
					$('#pu_month').val(data.pu_month);
					$('#pu_day').val(data.pu_day);
					$('#pu_year').val(data.pu_year);
					$('#pu_hour').val(data.pu_hour);
					$('#pu_minute').val(data.pu_minute);

					$('#rt_month').val(data.rt_month);
					$('#rt_day').val(data.rt_day);
					$('#rt_year').val(data.rt_year);
					$('#rt_hour').val(data.rt_hour);
					$('#rt_minute').val(data.rt_minute);

					$('#strt').val(data.strt);
					$('#pu_date_display').html(data.pickupdate);
					$('#rt_date_display').html(data.dropoffdate);

					$( "#datepicker" ).datepicker( "setDate" , '' );
					$( "#datepicker2" ).datepicker( "setDate" , '' );
					$('#pickuptime').find('option').remove();
					$('#dropofftime').find('option').remove();
					$('#pickuptime').append($('<option></option').val('').html('Select a time'));
					$('#dropofftime').append($('<option></option').val('').html('Select a time'));
					unmask();
					
            }, 'json');
	}).error(function(xhr, ajaxOptions, thrownError) {  alert(xhr.status); alert(thrownError); });

	$('#openhours_pickup').load('include/getopenhours.php?lid='+$("#pu_location").val());
	$('#openhours_dropoff').load('include/getopenhours.php?lid='+$("#pu_location").val());
	$('#pu_location').change(function()
	{
		mask();
		$('#openhours_pickup').load('include/getopenhours.php?lid='+this.value);
		$('#openhours_dropoff').load('include/getopenhours.php?lid='+this.value);
		$('#rt_location').attr('selectedIndex', this.selectedIndex);

		$( "#datepicker" ).datepicker( "setDate" , '' );
		$( "#datepicker2" ).datepicker( "setDate" , '' );
		$('#pickuptime').find('option').remove();
		$('#dropofftime').find('option').remove();
		$('#pickuptime').append($('<option></option').val('').html('Select a time'));
  	    $('#dropofftime').append($('<option></option').val('').html('Select a time'));

		unmask();
	}
	);

	$('#rt_location').change(function()
	{
		mask();
		$('#openhours_dropoff').load('include/getopenhours.php?lid='+this.value);
		idata= new Object();
		idata.pu_location=$('#pu_location').val();
		idata.rt_location=$('#rt_location').val();
		$.get('include/onewaycheck.php',idata, function(data) {
		  
		  if(data==0)
				{
					unmask();
					alert('One way rentals are not possible between locations with different suffixes (1), (2), (3) etc.  Please select a drop off location which has the same suffix as the Pick Up location.');
					document.frm.rt_location.selectedIndex=document.frm.pu_location.selectedIndex;
				}
				else
				{
				  unmask();
				  alert('As the pick Up and Drop Off locations are different this is a ONE WAY journey.  One way rentals must be agreed in advance with the locations you have requested.  If available it attracts an additional charge to the vehicle cost.  Depending on the Pick Up and Return locations these charges can range from &pound; 50 to £70 for Cars and £150 to £200 for Vans.  For all possible options and precise costing please contact our offices.');
				}
		});

	}
	);

	$('#frmbttn').click(function()
	{
		mask();
		idata= new Object();

		idata.pu_location=$('#pu_location').val();
		idata.pu_day=$('#pu_day').val();
		idata.pu_month=$('#pu_month').val();
		idata.pu_year=$('#pu_year').val().substr(2,2);
		idata.pu_hour=$('#pu_hour').val();
		idata.pu_minute=$('#pu_minute').val();

		idata.rt_location=$('#rt_location').val();
		idata.rt_day=$('#rt_day').val();
		idata.rt_month=$('#rt_month').val();
		idata.rt_year=$('#rt_year').val().substr(2,2);
		idata.rt_hour=$('#rt_hour').val();
		idata.rt_minute=$('#rt_minute').val();
	
		//idata=$(this).serialize();
		
		$.post("include/validatefrm.php", idata,
		   function(data){
			 
			if(data=='1')
				$('#frm').submit();
			else
			{alert(data);	}
				
		   });
		unmask();
		return false;
		
	});	
});

function mask()
{
	$("#mask").css({ opacity: 0.5 });
	$("#mask").width($("#booking").width());
	$("#mask").height($("#booking").height());
	$("#mask").css({ display:'block' });
	//h=parseInt($("#booking").height())/2+'px';
	$("#mask").css({ paddingTop:'120px' });
}
function unmask()
{
	$("#mask").css({ display:'none' });
}
function displayPUCalendarWindow(url, width, height) {
		/*var pu_supplier = stringSplit ( document.resform1.pu_location.options[document.resform1.pu_location.selectedIndex].value, ':' );
		url = url + "&pu_location=" + pu_supplier[1];

		if(document.resform1.pu_location.options[document.resform1.pu_location.selectedIndex].value==-1)
		{
		  alert('Please select pickup location');
		  return;
		}*/
		LeftPosition = (screen.availWidth) ? (screen.availWidth-width)/2 : 0;
		TopPosition = (screen.availHeight) ? (screen.availHeight-height)/2 : 0;
		//url = url + "&pu_location=" +11;
		
		var Win1 = window.open(url,"Vanhire",'width=' + width + ',height=' + height + ',resizable=no,scrollbars=yes,menubar=no,status=no,left=' + LeftPosition + ',top=' +TopPosition );
	}
	function displayRTCalendarWindow(url, width, height) {
		/*var rt_supplier = stringSplit ( document.resform1.pu_location.options[document.resform1.pu_location.selectedIndex].value, ':' );
		url = url + "&pl=" + document.resform1.rt_location.options[document.resform1.rt_location.selectedIndex].value*/
		
		LeftPosition = (screen.availWidth) ? (screen.availWidth-width)/2 : 0;
		TopPosition = (screen.availHeight) ? (screen.availHeight-height)/2 : 0;
		
		var p_month = document.frm.pu_month.value;
		var p_year = document.frm.pu_year.value;
		var p_day = document.frm.pu_day.value;
		
		url = url + "&p_day=" + p_day + "&p_month=" + p_month + "&p_year=" + p_year;
		var Win2 = window.open(url,"Vanhire",'width=' + width + ',height=' + height + ',resizable=no,scrollbars=yes,menubar=no,status=no,left=' + LeftPosition + ',top=' +TopPosition );

		
		
	}


function submitme()
{
x=document.getElementsByName('cars');
        
        theone=false;
        //if(x['length']!=0)
		if(document.resform1.cars.value!='-1')
        {
            xx='stopsell_'+document.resform1.cars.value;
           
            theone=true        ;
            //document.resform1.cars.value=document.resform1.v[i].value;
            if(document.getElementById(xx).value=='1')
                {
                  alert('At this location this vehicle is either in very short supply or SOLD OUT.  Call our office on  (UK) 8704-99-77-11  for other options.Or  select another location.');
                  return false;
                }
           
            
        }

		

        if(theone==false)
        {
           alert('Please select a  vehicle. ');
           return false;
        }
}

function process(val)
{
 document.resform1.t.value=val;
}


$(document).ready(function(){
	
	$('[id^="yellowbttn2_"]').click(function(event){
		currentid=$(this).attr('id').replace('yellowbttn2_','');
		car=$('#cars').val(); 
		if(car==currentid)
		{
			$('#yellowbttn_'+currentid).removeClass('greenbttn');$('#yellowbttn_'+currentid).addClass('yellowbttn');
			$('#cars').val('-1');
		}
		else
		{
			$('div[id^="acceptdiv_"]').hide();
			if($('#acceptdiv_'+currentid).length)
			{
				

				$('#acceptdiv_'+currentid).show();

			}
			else
			{
				if(car!=-1)
				{
					$('#yellowbttn_'+car).removeClass('greenbttn').addClass('yellowbttn');
				}
				$('#yellowbttn_'+currentid).removeClass('yellowbttn');$('#yellowbttn_'+currentid).addClass('greenbttn')
				$('#cars').val(currentid);
			}
		}
		event.preventDefault();
		return;
		
	});

	$('.accept').click(function(event){
		car=$('#cars').val();
		event.preventDefault();

		currentid=$(this).parent().attr('id').replace('acceptdiv_','');
		if(car!=currentid)
		{
			if(car!=-1)
			{
				$('#yellowbttn_'+car).removeClass('greenbttn').addClass('yellowbttn');
				//$('#yellowbttn_'+car+' a').html('<span>Select</span>');
			}
			$('#yellowbttn_'+currentid).removeClass('yellowbttn');$('#yellowbttn_'+currentid).addClass('greenbttn')
			//$(this).html('<span>Selected</span>');
			$('#cars').val(currentid);
		}
		else
		{
			$('#yellowbttn_'+currentid).removeClass('greenbttn');
			$('#yellowbttn_'+currentid).addClass('yellowbttn');
			$('#cars').val('-1');
			//$(this).html('<span>Select</span>');

		}
		$(this).parent().hide();

	});

	$('.return').click(function(event){
		event.preventDefault();
		$(this).parent().hide();
	});

		if($( "#datepicker" ).length > 0)
	{
		$( "#datepicker" ).datepicker({
				showOn: "button",
				buttonImage: "images/cal.gif",
				buttonImageOnly: true,
				numberOfMonths: 2,
				showButtonPanel: false,
				
				dateFormat:'dd/mm/yy',		
				
				onSelect: function(dateText, inst) { 
					//alert('gettime.php?pl='+$('#pu_location').val()+'&date='+dateText+'&t=d');
					mask();
					$.getJSON('gettime.php?pl='+$('#pu_location').val()+'&date='+dateText+'&t=p', function(data) {
					  
					  p_minute='';
					  p_hour='';
					  p_month='';
					  p_day='';
					  p_year='';
					 

					 $( "#datepicker2" ).datepicker( "setDate" , dateText );
					 $( "#datepicker2").datepicker( "option", "minDate", dateText );
					 $('#pickuptime').find('option').remove();
					 $('#dropofftime').find('option').remove();
					
					  if(data['msg']=='success')
						{
						  
						  
						  $('#pickuptime').append($('<option></option').val('').html('Select a time'));
						  $('#dropofftime').append($('<option></option').val('').html('Select a time'));

						  for(x in data['openhours'])
							{
								
								$('#pickuptime').append($('<option></option').val(x).html(data['openhours'][x]));
								$('#dropofftime').append($('<option></option').val(x).html(data['openhours'][x]));
							}
							min=data['minmin'];

							$date=dateText.split('/');

							p_day=parseInt($date[0],10);
							p_month=parseInt($date[1],10);
							p_year=parseInt($date[2],10);
							p_minute='';
							p_hour='';

						}
						else
						{
							$('#pickuptime').append($('<option></option').val('').html(data['openhours']));
							$('#dropofftime').append($('<option></option').val('').html(data['openhours']));
						}
						document.frm.pu_minute.value =p_minute;
						document.frm.pu_hour.value = p_hour;
						document.frm.pu_day.value = p_day;
						document.frm.pu_month.value = p_month;
						document.frm.pu_year.value = p_year;

						document.frm.rt_minute.value = p_minute;
						document.frm.rt_hour.value = p_hour;
						document.frm.rt_day.value = p_day;
						document.frm.rt_month.value = p_month;
						document.frm.rt_year.value = p_year;
						unmask();
					});
				},

				beforeShow: function(input, inst) { 
					if($(this).datepicker('isDisabled'))
					{
					}
				}
				
			});

			
			$( "#datepicker2" ).datepicker({
				showOn: "button",
				buttonImage: "images/cal.gif",
				buttonImageOnly: true,
				numberOfMonths: 2,
				showButtonPanel: false,
				minDate: +2 ,
				dateFormat:'dd/mm/yy',		
				
				onSelect: function(dateText, inst) { 
					mask();
					$.getJSON('gettime.php?pl='+$('#rt_location').val()+'&date='+dateText+'&t=d', function(data) {
					  
					  p_minute='';
					  p_hour='';
					  p_month='';
					  p_day='';
					  p_year='';
					   $('#dropofftime').find('option').remove();
					  if(data['msg']=='success')
						{
						 
						  $('#dropofftime').append($('<option></option').val('').html('Select a time'));

						  for(x in data['openhours'])
							{
								
								$('#dropofftime').append($('<option></option').val(x).html(data['openhours'][x]));
							}
						  
							min=data['minmin'];
							
							$date=dateText.split('/');
							p_day=parseInt($date[0],10);
							p_month=parseInt($date[1],10);
							p_year=parseInt($date[2],10);
							p_minute='';
							p_hour='';
						}
						else
						{
							
							$('#dropofftime').append($('<option></option').val('').html(data['openhours']));
						}
						document.frm.rt_minute.value = p_minute;
						document.frm.rt_hour.value = p_hour;
						document.frm.rt_day.value = p_day;
						document.frm.rt_month.value = p_month;
						document.frm.rt_year.value = p_year;
						unmask();
					});
				},

				beforeShow: function(input, inst) { 
					if($(this).datepicker('isDisabled'))
					{
					}
				}
				
			});
			$("#datepicker" ).datepicker( "setDate" , '' );
			$("#datepicker2" ).datepicker( "setDate" , '' );

			cal="+"+$('#calender_start_date').val();
			
			$( "#datepicker").datepicker( "option", "minDate", cal );
			$( "#datepicker2").datepicker( "option", "minDate", cal );

			$('#pickuptime').change(function(){
				
				v=$(this).val();
				if(v!='')
				{
					p_minute=v%60;
					p_hour=parseInt(v/60);
					document.frm.pu_minute.value = p_minute;
					document.frm.pu_hour.value = p_hour;
				}
			});
		
			$('#dropofftime').change(function(){
				
				v=$(this).val();
				if(v!='')
				{
					p_minute=v%60;
					p_hour=parseInt(v/60);
					document.frm.rt_minute.value = p_minute;
					document.frm.rt_hour.value = p_hour;
				}
			});

			$('#calpickup').click(function(e){
				$("#datepicker" ).datepicker( "show");
				e.preventDefault();
			});

			$('#caldropoff').click(function(e){
				$("#datepicker2" ).datepicker( "show");
				e.preventDefault();
				
			});
	}

});


function previewdetail(x){
pl=document.resform1.pu_location.value;
rt=document.resform1.rt_location.value;
noday=document.resform1.days.value;

pickupmonth=document.resform1.pu_month.value;
pickupyear=document.resform1.pu_year.value;
pickupday=document.resform1.pu_day.value;

pickuphour=document.resform1.pu_hour.value;
pickupminute=document.resform1.pu_minute.value;

pickupdatecheck=pickupyear+'-'+pickupmonth+'-'+pickupday+'-'+pickuphour+'-'+pickupminute;

dropoffmonth=document.resform1.rt_month.value;
dropoffyear=document.resform1.rt_year.value;
dropoffday=document.resform1.rt_day.value;

dropoffhour=document.resform1.rt_hour.value;
dropoffminute=document.resform1.rt_minute.value;

dropoffdatecheck=dropoffyear+'-'+dropoffmonth+'-'+dropoffday+'-'+dropoffhour+'-'+dropoffminute;

			
			var dsocleft=0;		
			var dsoctop=0;	
			

			 $('#popupdiv').html('<div class="border_preview" style="width:400px;"><table border="0"><tr><td  align="right" colspan="3"><div id="loader_container"><div id="loader_preview"><div align="center">Loading...</div></div></div> </td></tr>	</table></div>');
			
			 
			 $('#popupdiv').show();
			 $('#popupdiv').load("include/vehiledetail.php?id="+x+"&day="+noday+"&pd="+pickupdatecheck+"&dd="+dropoffdatecheck+"&pl="+pl);

}
