var total_busca = 0;

function ajaxLoader(_id) {
    $('#' + _id).html(ajax_loader);
}

function paginacaoRetornoAjax(_id, _origem, _total, _retornoAjax) {
    $('#' + _id).html(_retornoAjax);

    if (_origem == 'busca') {
        total_busca += eval(_total);
        $('#total').html(total_busca);
    }
}

function paginacaoAjax( _url , _pag , _start , _querystring ) {
    var _type = 'GET';
    var _pars = 'pag=' + _pag + '&start=' + _start + '' + _querystring;

    var requisicaoAjax = jQuery.ajax({
        dataType: 'html',
        type: _type,
        data: _pars,
        url: _url,
        success: function (_retornoAjax) {
            var _id = requisicaoAjax.getResponseHeader('id');
            var _origem = requisicaoAjax.getResponseHeader('origem');
            var _total = requisicaoAjax.getResponseHeader('total');
            paginacaoRetornoAjax(_id, _origem, _total, _retornoAjax);
        }
    });
}

function buscaCidade( estado , cidade ) {
    if ( ( estado != '' ) && ( estado != '0' ) ) {

        //ajaxLoader( 'carregando' );

        var _type = 'GET';
        var _url = url_do_site + 'cadastro-cidade-ajax.asp';
        var _pars = 'estado=' + estado;

        var requisicaoAjax = jQuery.ajax( {
            dataType: 'xml',
            type: _type,
            data: _pars,
            url: _url,
            success: function ( _retornoAjax ) {
                var xmlRetorno = _retornoAjax;
                var myOptions = null;
                var cd_cidade = null;
                var nm_cidade = null;

                $( '#carregando' ).html( '' );
				
				myOptions += '<option value="0">Selecione...</option>';

                $( xmlRetorno ).find( 'registro' ).each( function () {

                    cd_cidade = $( this ).find( 'cd_cidade' ).text();
                    nm_cidade = $( this ).find( 'nm_cidade' ).text();

                    myOptions += '<option value="' + cd_cidade + '" title="' + nm_cidade + '">' + nm_cidade + '</option>';
                });

                $( '#cidade' ).html( myOptions );
                $( '#estado' ).val( estado );

                if ( ( cidade != '' ) && ( cidade != '0' ) ) {
                    $( '#cidade' ).val( cidade );
                }
            }
        });
    }
}
