// JavaScript Document

function fnc_limpa_select( p_select ) {
  for( var i = ( p_select.options.length - 1 ); i >= 0; i-- ) p_select.options[ i ] = null;
   p_select.selectedIndex = -1;
}

function fnc_cria_option( p_select, p_valor, p_texto, p_selected ) {			
  if( p_select != null && p_select.options != null )
  p_select.options[ p_select.options.length ] = new Option( p_texto, p_valor, false, p_selected );
}

function pega_estados_ajax(select_destino, estado_id){
   if (typeof estado_id == 'undefined')
   		estado_id = '';
   estado_session = estado_id;
   if (document.getElementById(select_destino)){
	   elemento_form = document.getElementById(select_destino);
	   fnc_cria_option( elemento_form, '', 'carregando...', true );
   }
   param = 'estado_id='+estado_id;
   http( 'POST' , '/_ajax/_cfc/gerais.cfc?method=pega_estados' , pega_estados_ajax_retorno , param ); 
}

function pega_estados_ajax_retorno(obj){ 

  if (elemento_form){
	 fnc_limpa_select(elemento_form);
	 fnc_cria_option( elemento_form, '', '-- Selecione --', true );
	 
	 for( var i = 0; i < obj.length; i++ ) {
		valor = obj[i].estado_id.toString();
		label = obj[i].estado;
		if (estado_session && estado_session != '' && valor == estado_session){
			selected = true;
		} else { selected = false; }
		fnc_cria_option( elemento_form, valor, label, selected );
	 }
	  
	  /* habilita o elemento */
	  elemento_form.disabled = false;
	  /* -- */
  }
	  
}

function pega_municipios_ajax(estado_id, select_destino, municipio_id){
   municipio_session = municipio_id;
   if (document.getElementById(select_destino)){
	   elemento_form = document.getElementById(select_destino);
	   fnc_cria_option( elemento_form, '', 'carregando...', true );
   }
   param = 'estado_id='+estado_id;
   http( 'POST' , '/_ajax/_cfc/gerais.cfc?method=pega_municipios' , pega_municipios_ajax_retorno , param ); 
}

function pega_municipios_ajax_retorno(obj){ 

  if (elemento_form){
	 fnc_limpa_select(elemento_form);
	 fnc_cria_option( elemento_form, '', '-- Selecione --', true );
	 
	 for( var i = 0; i < obj.length; i++ ) {
		valor = obj[i].municipio_id.toString();
		label = obj[i].municipio;
		if (municipio_session && municipio_session != '' && valor == municipio_session){
			selected = true;
		} else { selected = false; }
		fnc_cria_option( elemento_form, valor, label, selected );
	 }
	  
	  /* habilita o elemento */
	  elemento_form.disabled = false;
	  /* -- */
  }
	  
}

function fnc_carrega_entidades_ajax(select_destino){
	if (document.getElementById('modalidade_id')){
		var modalidade_id = document.getElementById('modalidade_id').value;
	} else { var modalidade_id  = ''; }
	if (document.getElementById('estado_id')){
		var estado_id 	  = document.getElementById('estado_id').value;
	} else { var estado_id  = ''; }
	if (document.getElementById('municipio_id')){
		var municipio_id  = document.getElementById('municipio_id').value;
	} else { var municipio_id  = ''; }
    
    if (document.getElementById(select_destino)){
	    elemento_form = document.getElementById(select_destino);
	    fnc_cria_option( elemento_form, '', 'carregando...', true );				
    }
    param = 'modalidade_id='+modalidade_id+'&estado_id='+estado_id+'&municipio_id='+municipio_id;
    http( 'POST' , '/_ajax/_cfc/gerais.cfc?method=pega_entidades' , fnc_carrega_entidades_ajax_retorno , param ); 

}

function fnc_carrega_entidades_ajax_retorno(obj){ 
  if (elemento_form){
	 if (obj.length > 0){
		 fnc_limpa_select(elemento_form);
		 fnc_cria_option( elemento_form, '', '-- Selecione --', true );
	 
		 for( var i = 0; i < obj.length; i++ ) {
			valor = obj[i].cod_pessoa.toString();
			label = obj[i].razao_social;
			fnc_cria_option( elemento_form, valor, label, false );
		 }
	  
	 } else {
		 fnc_limpa_select(elemento_form);
		 fnc_cria_option( elemento_form, '', '-- Nenhum Anunciante Localizado --', true );
	   }
	 /* habilita o elemento */
	 elemento_form.disabled = false;
	 /* -- */
  }
}

function abre_link_ajax(link_id){
	if(link_id == null || link_id == ""){
		alert('É necessário escolher um Link!');
		return false;
	}//fim if
   param = 'link_id='+link_id;
   http( 'POST' , '/_ajax/_cfc/gerais.cfc?method=pega_links' , pega_links_ajax_retorno , param ); 
}//fim abre_link

function pega_links_ajax_retorno(obj){ 
	 if(obj != null && obj.length){
		url = obj[0].link;
		target = obj[0].target;
		if(target == null || target == "_self"){
			window.location.href = url;
		}else{
			window.open(url,'popup');	
		}//fim if else
	}//fim if
}//fim pega_links_ajax_retorno