function blocoBusca(a) {
	if (a == 'abre') {
		bcWidth01 = document.getElementById('mnAp01').offsetWidth;
		
		bcWidth02 = bcWidth01 - 26;
		
		//alert(bcWidth01);
		
		document.getElementById('mnAp02').style.width = bcWidth02 + 'px';
		
		document.getElementById('mnAp01').style.top = '27' + 'px'
		
		document.getElementById('mnAp02').style.display = 'block';
		
		document.getElementById('bcIp01').style.width = bcWidth02 - 95 + 'px'
		
		bcOpen = 1;
	} else if (a == 'fecha'){
		
		document.getElementById('mnAp02').style.display = 'none';
		
		document.getElementById('mnAp01').style.top = '70' + 'px'
		
	}
}

function replaceAll(string, token, newtoken) {
	while (string.indexOf(token) != -1) {
 		string = string.replace(token, newtoken);
	}
	return string;
}

function setMaxLength() {
	var x = document.getElementsByTagName('textarea');
	var counter = document.createElement('div');
	counter.className = 'counter';
	for (var i=0;i<x.length;i++) {
		if (x[i].getAttribute('maxlength')) {
			var counterClone = counter.cloneNode(true);
			counterClone.relatedElement = x[i];
			counterClone.innerHTML = '<span>0</span>/'+x[i].getAttribute('maxlength');
			x[i].parentNode.insertBefore(counterClone,x[i].nextSibling);
			x[i].relatedElement = counterClone.getElementsByTagName('span')[0];

			x[i].onkeyup = x[i].onchange = checkMaxLength;
			x[i].onkeyup();
		}
	}
}

function checkMaxLength() {
	var maxLength = this.getAttribute('maxlength');
	var currentLength = this.value.length;
	if (currentLength > maxLength)
		this.relatedElement.className = 'toomuch';
	else
		this.relatedElement.className = '';
	this.relatedElement.firstChild.nodeValue = currentLength;
	// not innerHTML
}


function removePergunta(indice){
	
	var atual_div = document.getElementById("div_pergunta_"+indice);
	var texto_atual = document.getElementById("texto_"+indice);
	atual_div.style.display = 'none';
	texto_atual.value = '';
	organizaLinhasCadastroperguntas();
}

function adicionaPergunta(indice){
	var proxima_div = document.getElementById("div_pergunta_"+(indice+1));
	proxima_div.style.display = 'block';
	organizaLinhasCadastroperguntas();
}



function organizaLinhasCadastroperguntas(){
	var divs = document.getElementsByTagName('div');
	var divs_perguntas = Array();
	for(i=0; i< divs.length; i++){
		div = divs[i];
		if(div.id.indexOf('div_pergunta_')>=0 && div.style.display == 'block'){
			divs_perguntas.push(div);	
		}		
	} 
	
	for(k=0; k< divs_perguntas.length; k++){
		var div = divs_perguntas[k];
		div.className = "ln10 " + (k%2==0?'lnBg':'');
		
		
		var showExcluir =  (k==0 && divs_perguntas.length==1)?false:true;
		var showAdicionar = (k==divs_perguntas.length-1 && k<9)?true:false;
		
		getElementByIdInsideObj(div,'excluir').style.display = (showExcluir?'inline':'none');
		getElementByIdInsideObj(div,'barra').style.display = (showExcluir&&showAdicionar?'inline':'none');
		getElementByIdInsideObj(div,'adicionar').style.display = (showAdicionar?'inline':'none');	
			
	}
		
}

function getElementByIdInsideObj(obj,idToFind){
	var content = obj.getElementsByTagName('*');
	for(y = 0; y<content.length; y++){
		if(content[y].id == idToFind){
			return content[y];
		}
	}
	return null;
}


function confirmResetForm(fm){
	//alert(document.form.fm.action);
	//document.form.fm.reset();
	
	var formularios = document.getElementsByTagName("form");
	
	for(i = 0;i<formularios.length; i++ ){
		if(formularios[i].name == fm){
			formularios[i].reset();
		}
	} 
}


function filtraResultados(tipo_id){
	var data = tipo_id.split("#");
	if (data.length > 0) {
		var tipo = data[0];
		var id = data[1];
		window.location.href = site_url+'/resultado/index/'+id+'/'+tipo;
	} 	
}

function mostraCampoExteriorAcademico(indiceLooop){
	document.getElementById("divInputPais_"+indiceLooop).style.display = "block";
	document.getElementById("divInputUf_"+indiceLooop).style.display = "none";
}
function mostraCampoUfAcademico(indiceLooop){
	document.getElementById("divInputPais_"+indiceLooop).style.display = "none";
	document.getElementById("divInputUf_"+indiceLooop).style.display = "block";
}

function trocaBloco(a,b) {
	document.getElementById(a).style.display = 'none';
	document.getElementById(b).style.display = 'block';
}

function habilitaInputSenha(link_id){
	var iptSenha = document.getElementById('senha'); 
	iptSenha.className = 'ip02';
	iptSenha.readOnly  = false;
	document.getElementById(link_id).style.display = 'none';
	
}

function  dataAindaTrabalhando(idInput){
	document.getElementById(idInput).value = "00/00/000";
}

function undoAll(){

	var confirmado = confirm('Deseja desfazer todas as alteraÃ§Ãµes?');
	
	if(!confirmado)
		return false;


	var inputs = document.getElementsByName("id[]");
	//alert(inputs.length);
	for(k=0;k<inputs.length;k++){
		try{
			document.getElementById("trTagImg_"+k).style.display = "table-row";
		}catch(ee){
			//alert('asdasd '+ee);
		}					
	}
	return true;
}

function desfazerAlteracoes(){
	if (confirm('Deseja realmente limpar o todo conteúdo da capa?')) {
		document.cadastraCapaSite.reset();
	}
	
}
function limpaItemHome(list_id,todos){
	
	
	var confirmado = confirm('Deseja realmente limpar este item?');
	
	if (confirmado) {
	
	
		var campos = Array('id', //'posicao',
		'referencia', 'titulo', 'img', 'resumo', 'link');
		
		
		
		for (i = 0; i < campos.length; i++) {
			var inputs = document.getElementsByName(campos[i] + "[]");
			for (k = 0; k < inputs.length; k++) {
			
				if (campos[i] == 'id') {
					//alert("ID VALUE: "+inputs[list_id].value );
					if (inputs[list_id].value != '') {
					
					/*if(todos==false){
					 document.getElementById("trTagImg_"+list_id).style.display = "none";
					 }else{
					 document.getElementById("trTagImg_"+k).style.display = "none";
					 }*/
					}
				}
				
				if (campos[i] != 'id') {
					if (todos == false) {
						inputs[list_id].value = '';
					}
					else {
						inputs[k].value = '';
					}
				}
				
				
			}
		}
	}
}



function abrePopParticipantes(programa_id){
	//alert(programa_id);
	//window.
	//opener.document.location = sURL; 
	
	newwindow=window.open(site_url+'/participante/participante/listaInscricao/'+programa_id,'participantes','height=300,width=500');
	if (window.focus) {newwindow.focus()}
	return false;
}

function  mudaParticipanteInscricao(id,nome){
	opener.focus()
	
	var form = opener.document.cadastroInscricao;
	var trNome = opener.document.getElementById('nomeParticipante');
	form.participante_id.value = id;
	//alert(form.participante_id);
	trNome.innerHTML = nome;
	self.close();
}


function enderecoExterior(btn_id, id,textoLocal){
	var btn = document.getElementById(btn_id+id);

	if(btn.innerHTML==textoLocal+' no exterior'){
		document.getElementById("trCep_uf_exterior_"+id).style.display = 'block';
		document.getElementById("trCidade_"+id).style.display = 'none';
		document.getElementById("trCEP_"+id).style.display = 'none';
		
		document.getElementById("exterior_"+id).value = '1';

		btn.innerHTML = textoLocal+' no Brasil';
	}else{
		document.getElementById("trCep_uf_exterior_"+id).style.display = 'none';
		document.getElementById("trCidade_"+id).style.display = 'block';
		document.getElementById("trCEP_"+id).style.display = 'block';
		document.getElementById("exterior_"+id).value = '0';
		btn.innerHTML = textoLocal+' no exterior'; 
	}
}
function exibeDadosGraduacao(drop,tbPrefix,id){
	if(drop.selectedIndex>0  &&  drop.selectedIndex<6){
		mostraOcultaDiv(tbPrefix+'_'+id,'block');
 	}else{
		mostraOcultaDiv(tbPrefix+'_'+id,'none');
	}
}

function exibeDadosArtigo(drop,tbPrefix,id) {
	if(drop.selectedIndex==2){
		mostraOcultaDiv(tbPrefix+'_'+id,'block');
 	}else{
		mostraOcultaDiv(tbPrefix+'_'+id,'none');
	}
}

function addTbDados(idTb,tbPrefix,texto){
	
	var link = document.getElementById('linkAdd_'+idTb);
	var tbAtiva = document.getElementById('tb_'+idTb+'_ativa');
	//alert('tb_'+idTb+'_ativa'+" -- TB "+idTb+' EstÃ¡ ativa?:'+tbAtiva.value);
	if(tbAtiva.value=='false'){
		
		link.innerHTML = 'remover '+texto;
		tbAtiva.value = 'true';
		//mostraOcultaTabela(tbPrefix+'_'+idTb,'table');	
		//alert('aaa');
		try{
			mostraOcultaDiv(tbPrefix+'_'+(idTb),'block');
			mostraOcultaDiv('divAdd_'+(idTb+1),'block');
			
		}catch(e){
			//alert(e)
		};
	}else{
		link.innerHTML = 'adicionar '+texto;
		tbAtiva.value = 'false';
		mostraOcultaTabela(tbPrefix+'_'+idTb,'none');
		try{
			//alert('idTb que vai inciar o loop '+idTb);
			for(i=idTb+1; i<7; i++){
				mostraOcultaDiv('divAdd_'+(i),'none');
				document.getElementById('tb_'+i+'_ativa').value = 'false';
				mostraOcultaTabela(tbPrefix+'_'+i,'none');	
				document.getElementById('linkAdd_'+i).innerHTML  ='adicionar '+texto;			
			}
		}catch(e){
			//alert(e +'-ERROR****----'+ i)
		};
	}
}

function mostraOcultaDiv(id,status){
    var div = document.getElementById(id);
	if (!status) {
    	div.style.display = div.style.display == 'none' ? 'block' : 'none';
	}else{
		div.style.display  = status;
	}
}

function mostraOcultaTabela(id, status){
	var tabela = document.getElementById(id);
	if (!status) {
		tabela.style.display = tabela.style.display == 'none' ? 'table' : 'none';
	}else{
		tabela.style.display  = status;
	}
}

function noenter(event){
    var tecla = event.keyCode;
    /*if ((tecla == 13)) {
        
		if(document.forms[0]){
			// tentativa de dar 'TAB' automaticamente
			// favor completar assim que possï¿½vel
		}
		
		return false;
    }*/
    return tecla;
}

/* pega a string e retorna a extensï¿½o (p/ validar formato de arquivo)*/
function getStrExtension(str){
    return str.substring(str.lastIndexOf('.'), str.length);	
}

slOpen = 0;

function slOpener(a,b) {
	if (slOpen == 0) {
		slWidth01 = document.getElementById(a).offsetWidth;
		//alert(slWidth01);
		slWidth02 = slWidth01 - 2;
		document.getElementById(b).style.width = slWidth02 + 'px';
		document.getElementById(b).style.display = 'block';
		slOpen = 1;
	} else {
		document.getElementById(b).style.display = 'none';
		slOpen = 0;
	}
}



/* VALIDA INPUTS TIPO TEXTO E DROPDOWN*/
function validaForm_teste(formIdent) 
{ 
	var eregFn = /\{fn:(.+?)\}/; 
  var arrayForId = new Array();
  var form, elements, i, elm; 
  form = document.getElementById(formIdent) 
    ? document.getElementById(formIdent) 
    : document.forms[formIdent]; 

	if (document.getElementsByTagName)
	{
		
		elements = form.getElementsByTagName('input');
		
		for( i=0, elm; elm=elements.item(i++); )
		{
			
			if (elm.getAttribute('type') == "text")
			{
				
				var obrigatorio = elm.className.indexOf('obrigatorio')>=0?true:false; 
				var valida = false;
				var valido = true;
				
				if(obrigatorio==true || elm.value.length > 0 ){
					valida = true;
				}
				
				if(valida){
					
					var ereRs = eregFn.exec(elm.className);
					if(ereRs!=null){
						fnrs = ereRs[1];
						valido = eval(fnrs)(elm.value);
					}else{
						valido =  (elm.value.length>=0)?true:false;
					}
				}
				//alert(elm.name);
				var xpto = new Array();
				xpto[0] = elm.id;
				arrayForId[xpto[0]] = valido;				
			}
		}
		elements = form.getElementsByTagName('select');
		for( i=0, elm; elm=elements.item(i++); )
		{
			var obrigatorio = elm.className.indexOf('obrigatorio')>=0?true:false; 
			var valida = false;
			var valido = true;
			
			
			if(obrigatorio==true || elm.value.length > 0 ){
				valida = true;
			}
			
			var ereRs = eregFn.exec(elm.className);
			if(ereRs!=null){
				fnrs = ereRs[1];
				valido = eval(fnrs)(elm.value);
			}else{
				valido =  (elm.value.length>=0)?true:false;
			}
			
				var xpto = new Array();
				xpto[0] = elm.id;
				arrayForId[xpto[0]] = valido;				
	
		}
		
		elements = form.getElementsByTagName('label');
		for( i=0, elm; elm=elements.item(i++); )
		{
			//  se nÃ£o ta no array da erro fiel el dropdown maluko !
			// vira false LOL
			
			var valido = arrayForId[elm.getAttribute('for')];
			//alert("---> "+  elm.for + 'VALIDO :??'+valido);
			if(valido){
				unsetErrorClass(elm);
			}else{
				setErrorClass(elm);
			}
		}
		//var alels = document.getElementsByTagName('label');
		//alert(elements.length + " ---- "+ alels.length);
		
		//alert(forIdInvalido);
		
		//for(i=0; i<arrayForId.length; i++){
			//alert(arrayForId[i]);
		//}
	}


	// Actually looking through more elements here
	// but the result is the same.
	else
	{
		elements = form.elements;
		for( i=0, elm; elm=elements[i++]; )
		{
			if (elm.type == "text")
			{
				elm.value ='';
			}
		}
	}
}
	
function addEvent(elm, strEvent, fnHandler)
{
	return ( elm.addEventListener
	? elm.addEventListener( strEvent, fnHandler, false)
	: elm.attachEvent( 'on'+strEvent, fnHandler)
	);
}