$(function()
{
    var cache = {};

    var setProvincias = function( provincias )
    {
        var selectProvincia = $('#provincias').empty();

        for ( var i = 0; i < provincias.length; i++ ) {
            selectProvincia.append("<option value='" + provincias[ i ].id + "'>" + provincias[ i ].nombre + "</option>");
        }
    };

    $('#paises').change(function()
    {
        var idPaises = $( this ).val();

        if ( idPaises.length == 0 ) {
            return;
        }

        if ( idPaises in cache ) {
            setProvincias( cache[ idPaises ] );
        }
        else {

            jQuery.getJSON(configRoot + 'ajax.php', {q:'provincias', id:idPaises}, function( response ){
                setProvincias( cache[ idPaises ] = response );
            });

        }
    });

    $('a[href=#buscar]').click(function()
    {
        /**
        if ( lnkByTable )
        {
            var provSelect = $(this).parent().prev().find('select');
            var provValue  = provSelect.val();

            window.location.href = configRoot + 't' + getTipo + '/provincia/' + provValue + '/';
        }
        else if ( lnkRewrite )
            window.location.href = configRoot + 'index/map,provincia/id,' + $( this ).parent().prev().find('select').val() + '/';
        else
            window.location.href = configRoot + 'index.php?map=provincia&id=' + $( this ).parent().prev().find('select').val();
         */

        $(this)
        .parents('form')
        .submit();

        return false;
    });
});