$(document).ready(function(){
  
  $.getJSON(
  	'http://' + location.host + '/cascadeSelectPlace.php?BRED=' + Math.random(),
  	{'region': 0},
  	function(data, status){                             
  	  $('#sStartR, #sEndR').fillSelect(data).attr('disabled','');
  	  
  	  if ( !$.cookie('sStartR') ) $('#sStartR option:contains("Москва")').attr('selected', 'selected');
  	  else $('#sStartR option:contains("' + $.cookie('sStartR') + '")').attr('selected', 'selected');
  	  
  	  if ( !$.cookie('sEndR') ) $('#sEndR option:contains("Москва")').attr('selected', 'selected');
  	  else $('#sEndR option:contains("' + $.cookie('sEndR') + '")').attr('selected', 'selected');
  	  
  	  $('#sStartR, #sEndR').change();
    }
  );
  
  $('#sStartR, #sEndR').change(function(){
  	$(this).attr('id').substring(0, ($(this).attr('id').length - 1)) 
  	var Place  = $('#' + $(this).attr('id').substring(0, ($(this).attr('id').length - 1)) + 'P');
  	if ($(this).val().length == 0 || $(this).val() == 0){
  		Place.attr('disabled','disabled');
      Place.clearSelect();
  	}
  	else{
  		$.getJSON(
      	'http://' + location.host + '/cascadeSelectPlace.php?BRED=' + Math.random(),
      	{'region': $(this).val()},
      	function(data, status){                             
      	  Place.fillSelect(data).attr('disabled','');
      	  if ( $.cookie(Place.attr('id')) ) $('#' + Place.attr('id') + ' option:contains("' + $.cookie(Place.attr('id')) + '")').attr('selected', 'selected');
        }
      );
  	}
  });
  
  $('#sStartP').change(function(){
    if ($('#sStartP option:selected').val() != '' && $('#sStartP option:selected').val() != 0)
      $('#sEndP option:contains("' + $('#sStartP option:selected').text() + '")').attr('selected', 'selected');
  });
  
  $(function() {
		$("#EditDataPlace").dialog("destroy");
		$("#EditDataPlace").dialog({
			autoOpen: false,
			height: 200,
			width: 700,
			modal: true,
			show: 'blind',
			hide: 'blind',
			buttons: {
				'Сохранить': function() {
				  $("#frDataPlaces").submit();
					$(this).dialog('close');
				},
				'Отмена': function() {
					$(this).dialog('close');
				}
			}
		});
		
		$('#DataUpdate').click(function() {
		  $('#EditDataPlace').dialog('open');
    });
    
    $('.CarsDoOrder').click(function() {
      $(document).ClaerCookie();
      $.cookie("nCarId", $(this).attr('CarId'), { path: '/'});
		  $('#EditDataPlace').dialog('open');
    });
    
	});
	
});