// JavaScript Document
$( function() {
    $("#cidade").change( function(){
        url = ( $(this).val() > 0 ) ? '&cidade=' + $(this).val() : '';
        if ( $("#categoria").val() > 0 ) location.href = '?categoria=' + $("#categoria").val() + url;
        //location.href = '?';
    });
});

function CarregaCategoria( id ) {
	if ( id > 0 ) location.href = '?categoria='+id;
}

function FiltraBairro( categoria, bairro ) {
	p_bairro = ( bairro > 0 ) ? '&bairro=' + bairro : '';
	if ( categoria > 0 ) location.href = '?categoria=' + categoria + p_bairro;
}

function AbreOrcamento( categoria, bairro, letra )
{
	url = 'guiaautomotivo_orcamento.php?categoria=' + categoria + '&bairro=' + bairro + '&letra=' + letra;
	AbreJanela('sdf_guiaorcamento', url, 430, 480, 10, 10, true, false);
}

function AbreEmail( id, categoria, bairro, letra )
{
	url = 'guiaautomotivo_orcamento.php?categoria=' + categoria + '&bairro=' + bairro + '&cd=' + id + '&letra=' + letra;
	AbreJanela('sdf_guiaorcamento', url, 430, 520, 10, 10, true, false);
}

function AbreLocalizacao( id )
{
	url = 'guiaautomotivo_localizacaogm.php?cd=' + id;
	AbreJanela('sdf_guialocalizacao', url, 600, 420, 10, 10, true, false);
}

function AbreFoto( id )
{
	url = 'guiaautomotivo_fotoloja.php?cd=' + id + '&seq=1';
	AbreJanela('sdf_guiafoto', url, 640, 480, 10, 10, true, false);
}



function AlimentaComboMarcaModelo ( id, tipo, categoria, vlrDefault, combovlrDefault2, vlrDefault2 ) {

	// Recupera o objeto formulario
	formoferta = document.getElementById( 'frmorcamento' );

	// Desativa os dois combos
	formoferta.modelo.disabled = true;

	// Limpa combo de modelo
	formoferta.modelo.options.length = 0;
	formoferta.modelo.options[0] = new Option( 'Selecione o modelo', '0');

	txtCombo2 = '';
	id2 = '';

	if ( id == 'marca' ) {
		// Valor do filtro (igual ao parametro tipo neste caso)
		vlr = tipo;
		txtCombo  = 'Selecione a marca';
		txtCombo2 = 'Selecione o modelo';
		id2 = 'modelo';
	} else if ( id == 'modelo' ) {
		// Valor do filtro (valor da marca)
		vlr = formoferta.marca.value;
		txtCombo = 'Selecione o modelo';
	}

	// recupera valores de filtro
	cidade = 0;

	carregaCombosTipoMarcaModelo ( id, tipo, categoria, vlr, vlrDefault, txtCombo, id2, txtCombo2, vlrDefault2, '', '', 0 );
}




function validaOrcamento() {
	//frm = document.getElementById('frmorcamento');
	erro = '';

	if ( $("#nome").val() == '' ) erro += 'Nome\n';
	if ( !ValidaEmail($("#email").val()) ) erro += 'E-mail\n';
	if ( $("#telefone").val().length != 14 ) erro += 'Telefone\n';
	if ( $("#tipo").val()>0 && $("#marca").val()<=0 ) erro += 'Marca\n';
	if ( $("#tipo").val()>0 && $("#modelo").val()<=0 ) erro += 'Modelo\n';
	if ( $("#modelo").val()>0 && $("#ano").val()<=0 ) erro += 'Ano-modelo\n';
	if ( $("#mensagem").val().length < 5 ) erro += 'Mensagem\n';

	if ( erro != '' ) {
	   alert('Verifique os seguintes campos:\n'+erro);
	   return false;
	} else {
	   return true;
	}
}



function VerTelefone( id, telefone )
{
	$("#telefone_"+id+" a:first ").after(" <span>" + telefone + "</span>");
	$("#telefone_"+id+" a:first ").remove();
	$.get('/ajax/guia_vertelefone.php',{cd:id});
}


$( function() {
	$("#lnkexibirletras").click( function(){
		$("#navegaletras").slideToggle("slow");
	});

    $("#uf").change(function(){
        $(this).jqCidade();
    });
});


