///
/// Funções JavaScript Comuns
///

/**
 * Carregar informações de logradouro
 */
function fncComum_logradouro_info(tag)
{
  tag = $(tag);
  if (tag.val().toString().trim() == '')
    return false;

  var url = tag.attr('url');

  // Executar Ajax
  $.ajaxSetup({async: false});
  $.post(url, {cep: tag.val()}, function(data)
  {
    var info = data.split('|');
    if (info[0] == 'ERROR')
    {
      alert(info[1]);
      return false;
    } else if (info[0] != 'OK')
    {
      alert(data);
      return false;
    }

    $('#ENDERECO').val(info[1]);
    $('#LOGRADOURO').val(info[2]);
    $('#BAIRRO').val(info[3]);
    $('#MUNICIPIO').val(info[4]);
  });
}

/**
 * Tratar ceps campo
 */
function fncComum_cep_keydown(tag, ev)
{
  var res = fncFields_cep(tag, ev);
  if (res == false)
    return false;

  if ((ev.keyCode != VK_TAB) && (ev.keyCode != VK_RETURN) && (ev.altKey == false) && (ev.ctrlKey == false) && (ev.shiftKey == false))
  {
    $('#ENDERECO').val('-1');
    $('#LOGRADOURO').val('');
    $('#BAIRRO').val('');
    $('#MUNICIPIO').val('');
  }
}


/**
 * Exibir popup Correios
 */
function fncComum_popup_correio()
{
	var dlgWidth  = 800;
	var dlgHeight = 500;
  var left      = ((screen.width / 2) - (dlgWidth / 2));
  var top       = ((screen.height / 2) - (dlgHeight / 2));

	var config = 'height=' + dlgHeight + ',';
	config    += 'width=' + dlgWidth + ',';
	config    += 'menubar=no,';
	config    += 'resizable=yes,';
	config    += 'scrollbars=yes,';
	config    += 'status=yes,';
	config    += 'titlebar=no,';
	config    += 'toolbar=no,';
	config    += 'location=no,';
	config    += 'left=' + left + ',';
	config    += 'top=' + top;

  //var url = 'correios/busca_cep_java.htm';
  var url = 'http://www.buscacep.correios.com.br/servicos/dnec/menuAction.do?Metodo=menuEndereco#infoSobre';

	window.open(url, null, config);
}

/**
 * Exibir popup para alterar CEP
 *
 * @param Object tag
 */
function fncComum_alterar_cep_popup(tag)
{
	var dlgWidth  = 380;
	var dlgHeight = 370;
  var left      = ((screen.width / 2) - (dlgWidth / 2));
  var top       = ((screen.height / 2) - (dlgHeight / 2));

	var config = 'height=' + dlgHeight + ',';
	config    += 'width=' + dlgWidth + ',';
	config    += 'menubar=no,';
	config    += 'resizable=no,';
	config    += 'scrollbars=no,';
	config    += 'status=yes,';
	config    += 'titlebar=no,';
	config    += 'toolbar=no,';
	config    += 'location=no,';
	config    += 'left=' + left + ',';
	config    += 'top=' + top;

	window.open(tag, null, config);
}
