// JavaScript Document
$(document).ready(function() {
	var ierandom=Math.random();
	$('#categoria').change(function(){
	 	if($(this).val() == '0'){
			$('#SubCatForm').hide();
			$('#anuncioForm').hide();
	 	} else {
	 		if($(this).val() != '77'){
				getCarForm('0'); 	
				$('#anuncioForm').hide();
		 	}
		 	var id = $(this).val();
			$.ajax({
				type: "POST",
				url: "incajax_subcat.php",
				data: "idCateg="+id+"&ierandom="+ierandom,
				cache: false,
				beforeSend: function(){
					$('#idDivImgLoading').show();
				},
				success: function(msg){
					$('#idDivImgLoading').hide();
					$('#SubCatForm').html(msg);
				},
				error: function(){
				 alert('Tenemos un problema de funcionalidad con su navegador, si le es posible use otro navegador sino intentelo mas tarde. Disculpe las molestias.');
				}
			});
			$('#SubCatForm').show();
		}
	});

	$('#provincia').change(function(){
		$.ajax({
			type: "POST",
			url: "incajax_municipios.php",
			data: "idPro="+$(this).val()+"&ierandom="+ierandom,
			success: function(msg){
				$('#MunForm').html(msg);
			}
		});
		$('#MunForm').show();
	}); 
	$('#OoD').change(function(){
		$('#anuncio').attr('disabled',false);
		switch ($('#OoD').val()){
			case '0': var txt = 'Se vende '; break;
			case '1': var txt = 'Desearia comprar '; break;
		}
		if($("#subcategoria option:selected").val() == '133') {
			if($("#marca option:selected").val() != '0') { 
				var txt = txt + $("#marca option:selected").text() + ' '; 
				if($("#modelo option:selected").val() != '0') { 
					var txt = txt + $("#modelo option:selected").text() + ' '; 
				} 
			}
			if($("#combustible option:selected").val() != '0') { 
				var txt = txt + ', ' + $("#combustible option:selected").text() + ' '; 
			}
			if($("#year option:selected").val() != '0') { 
				var txt = txt + 'del año ' + $("#year option:selected").text() + ' '; 
			}
		}
		$('#anuncio').val(txt);
		//$('#anuncio').focus();

	});
	function getCarForm(id) {
		if ( id == 0 ) {
			$('#CarForm').hide();
		} else {
			$.ajax({
				type: "POST",
				url: "incajax_carform.php",
				data: "id="+id,
				success: function(msg){
					$('#CarForm').html(msg);
				}
			});
		}
	} 
	function showForm( Id, vis ) {
		if ( vis != 0 ) {
			$('#'+Id).show();
		} else {
            $('#'+Id).hide();
   		}
	}

function ismaxlength(obj){
	var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : "";
	if (obj.getAttribute && obj.value.length>mlength) {
		obj.value=obj.value.substring(0,mlength);
	}
	return mlength - obj.value.length;
}

});