jQuery.ajaxSetup({type: "POST"});

function clearSelectedFields() {		
	jQuery('#city_name').attr("value","");	
	jQuery('#city_name').flushCache();
}


jQuery(document).ready(function() {

	jQuery("#city_name").focus(
			function() { 
				//alert('hi');
				
				areaUID = jQuery("#countrySelect option:selected").val();
				if(!areaUID){				
				jQuery("#countrySelect").focus();
				jQuery("#countryTxt").css({ color: "red"});
				alert("Выберите страну");
				//jQuery(this).attr("disabled","disabled")				
				}
				//alert(areaUID);
			} 
	);
	
	jQuery("#countrySelect").change( 
		function() { 
			
			var currentCountry = jQuery("#countrySelect option:selected").val();			
			if(currentCountry == 'RU' || currentCountry == 'UA' || currentCountry == 'BY' || currentCountry == 'KZ') {				
				//jQuery("#region_id").removeAttr("disabled");
				jQuery('#regionSel').html('<img src="images/loading_photo.gif" />');
				jQuery.post(
					'ajax_get_category.php',
					{from:'jQuery', action:'getRegion', text:'Выберите регион', languages_id:3, country:currentCountry, getCity:false},
					function(data) {
						jQuery('#regionSel').html(data);
						}
					);
					
				/*jQuery('#regionWrap').css({ display: ""});
				jQuery('#regionWrapS').html('');
				
					jQuery.post(
					'ajax_get_category.php',
					{from:'jQuery', action:'getRegion', text:'Выберите регион', languages_id:3, country:currentCountry},
					function(data) {
						jQuery('#regionSel').html(data);
						}
					);
					
					if(jQuery("#region_id_js")) {
						jQuery("#region_id_js").attr("value",'0');
						jQuery("#city_id_js").attr("value",'0');
					}			*/
			}
			else{				
				jQuery("#region_id").attr("disabled",'disabled');				
				/*if(jQuery("#region_id_js")) {
						jQuery("#region_id_js").attr("value",'0');				
						jQuery("#city_id_js").attr("value",'0');
				}*/
			}
			
			/*jQuery("#countryTxt").css({ color: ""});
			clearSelectedFields();
			if(jQuery("#countrySelect option:selected").val())
			jQuery("#city_name").focus();*/
			
			//alert(jQuery("#countrySelect option:selected").val());
			
		}
	);
		
	jQuery("#city_name").autocomplete("jquery.php",
	{			
		minChars: 3,
		width: 220,
		matchContains: true,
		autoFill: false,
		
		cacheLength:150,		
				
		matchSubset:true,		
		mustMatch:true,
		
		max:20,
		
		extraParams: {
				action: 'autocomplitePlaces' , 				
				country_id: function() { return jQuery("#countrySelect option:selected").val(); }

		},

		formatItem: function(row, i, max) {			
			return i + "/" + max  + ": " + row[0];
		},

		formatMatch: function(row, i, max) {
			return row[1];
		},

		formatResult: function(row) {
			return row[0];
		}
	}

	);	
	
	jQuery("#submit_cars_btn").click( function(event){
		
		if(jQuery('#categories_id').attr("value") == '0') {
			alert('Выберите марку');
		}
		/*else if(jQuery('#sub1cat').children().attr("value")==-1){
			alert('Выберите модель');			
		}*/
		else{
			jQuery('#search_cars_form').submit();
		}		
	});
	
	/*
	
	//jQuery("#staff_uid_select").
	jQuery("#search_cars_form").bind("submit", function(event) {	
	
		//jQuery('#places_uid').attr("value");
		//alert(jQuery('#categories_id').attr("value"));
		
		//return false;
		
		//if(jQuery('#categories_id').attr("value") == '0') {
		//alert('Выберите марку');		
		//event.stopPropagation();
		//return false;
		//}
		
	});
	*/
});