var CR_URL_DIR = "/";

$().ready(function()
{
    var LANG_CODE = $('#l').val();
    
	// Set autocomplete for pickup
	$("#pickup").autocomplete("/get_locations.php", {
		width: 300,
		cacheLength: 1,
		max: 20,
		minChar: 2,
		extraParams:
		{
		  language_code: function() { return LANG_CODE; }
		}
	});

	// What to do when we get results for pickup's autocomplete 
	$("#pickup").result(function(event, data, formatted) {
		if (data)
		{
			$("#ploc_id").val(data[1]);
			$("#country_code").val(data[2]);
			$("#pickup").val(data[3]);

			$("#dloc_id").val(data[1]);
			$("#dropoff").val(data[3]);
		}
	});

	// Set autocomplete for dropoff
	$("#dropoff").autocomplete("/get_locations.php", {
		width: 300,
		cacheLength: 1,
		max: 20,
		minChar: 3,
		extraParams:
		{
		  country_code: function()
		  {
		    return $('#country_code').val();
		  },
		  
		  language_code: function() { return LANG_CODE; }
		}
	});

	// What to do when we get results for dropoff's autocomplete 
	$("#dropoff").result(function(event, data, formatted) {
		if (data)
		{
			$("#dloc_id").val(data[1]);
			$("#dropoff").val(data[3]);
		}
	});
	
    $.datepicker.setDefaults($.datepicker.regional[LANG_CODE]);
 
	
	// Set calendar to date inputs
    $('#pDate').datepicker({ minDate: 0, dateFormat: 'dd/mm/yy', firstDay: 1 });
    $('#dDate').datepicker({ minDate: 0, dateFormat: 'dd/mm/yy', firstDay: 1 });

	// When a pickup date is selected make the minDate for dropoff this date
    $('#pDate').change(function () { 
      var date_tmp = $('#pDate').val().split("/");
	  var date_help = $('#dDate').val();
      $('#dDate').datepicker('option', { minDate: new Date(date_tmp[2], date_tmp[1]-1, date_tmp[0]) }); 
	  $('#dDate').val(date_help);
    });

	// When a dropoff date is selected make the maxDate for pickup this date
    $('#dDate').change(function () { 
      var date_tmp = $('#dDate').val().split("/");
	  var date_help = $('#pDate').val();
      $('#pDate').datepicker('option', { maxDate: new Date(date_tmp[2], date_tmp[1]-1, date_tmp[0]) }); 
	  $('#pDate').val(date_help);
    });
    
    $(".infoHover").tooltip({
        showBody: " - ",
        extraClass: 'tooltip'
    });  


});


function filterClass()
{
  document.getElementById('search_form_el').submit();
}

function validate_CR_form()
{
  var error = "";
  if(document.getElementById("pickup").value == "" || document.getElementById("ploc_id").value == "")
    error += ERR_form_no_pick_up_loc+"\n";

  if(document.getElementById("dropoff").value == "" || document.getElementById("dloc_id").value == "")
    error += ERR_form_no_drop_off_loc+"\n";

  if (document.getElementById("pDate").value.split("/").length != 3)
    error += ERR_form_no_pick_up_date+"\n";
  
  if (document.getElementById("dDate").value.split("/").length != 3)
    error += ERR_form_no_drop_off_date+"\n";

  if(document.getElementById("age").value == "")
    error += ERR_form_no_drivers_age+"\n";

  else if(document.getElementById("age").value < 18)
    error += ERR_form_drivers_age_to_low+"\n";

  if(error)
  {
    alert(error);
    return false;
  }


  return true;
}

//$().ready();

//----------------------------------------------------
/*		function CR_search()
		{
			$.ajax({
				type: "POST",
				url: "/CR_Foreign/CR_search.php",
				data: CR_URL_address,
				success: function(msg){
					//$("#CR_results_elem").html(msg);
					if(msg.indexOf("@@@@@@@##done##@@@@@@@") == -1)
					{
          				setTimeout("CR_search()", 1000);
					}
				}
			});
		}
	


		function CR_search()
		{
			$.ajax({
				type: "POST",
				url: "/get_results.php",
				data: CR_URL_address,
				success: function(msg){
					$("#CR_results_elem").html(msg);
					if(msg.indexOf("@@@@@@@##done##@@@@@@@") == -1)
					{
          				setTimeout("CR_search()", 1000);
					}
				}
			});
		}


 */



