<!--
// funções de Javascript




//AJAX_________________________________________

function cria_ajax()
	{
	var xmlhttp;
	try
		{
		xmlhttp = new XMLHttpRequest();
		}
	catch(ee)
		{
		try
			{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}
		catch(e)
			{
			try
				{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
				}
			catch(E)
				{
				xmlhttp = false;
				}
			}
		}
	return xmlhttp;
	}




//_____________________________________________


//função para pré-carregar imagens no documento. Basta digitar a URL das imagens, separadas por vírgula
//Não importa o número de argumentos. Cada argumento é uma imagem.

function carregar()
{
var imagens=new array();

for (i=0; i<arguments.length; i++)
     {
     imagens[i]=new Image();
     imagens[i].src=arguments[i];
     }
}







function validaCPF(formulario) {
                 var cpf = document.forms[formulario].cpf.value;
                 erro = new String;
                 if (cpf.length < 11) erro = "CPF inválido!"; 
                 var nonNumbers = /\D/;
                 if (nonNumbers.test(cpf)) erro += "A verificacao de CPF suporta apenas numeros! "; 
                 if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999"){
                         erro = "CPF inválido!"; 
               }
               var a = [];
               var b = new Number;
               var c = 11;
               for (i=0; i<11; i++){
                       a[i] = cpf.charAt(i);
                       if (i < 9) b += (a[i] * --c);
               }
               if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }
               b = 0;
               c = 11;
               for (y=0; y<10; y++) b += (a[y] * c--); 
               if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }
               if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10])){
                       erro = "CPF inválido!"; 
               }
               if (erro.length > 0)
		       {
                       alert(erro);
                       document.forms[formulario].cpf.focus();
                       return false;
               	       }
		else 
		       {
        	       return true;
		       }
	}


function validaCNPJ(formulario) 
		{
		CNPJ = document.forms[formulario].cnpj.value;
		erro = new String;
		if (CNPJ.length < 18) erro = "CNPJ inválido!";
		if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-")){
			if (erro.length == 0) erro = "CNPJ inválido!";
		}
		//substituir os caracteres que nao sao numeros
		if(document.layers && parseInt(navigator.appVersion) == 4){
			x = CNPJ.substring(0,2);
			x += CNPJ.substring(3,6);
			x += CNPJ.substring(7,10);
			x += CNPJ.substring(11,15);
			x += CNPJ.substring(16,18);
			CNPJ = x;	
		} else {
			CNPJ = CNPJ.replace(".","");
			CNPJ = CNPJ.replace(".","");
			CNPJ = CNPJ.replace("-","");
			CNPJ = CNPJ.replace("/","");
		}
		var a = [];
		var b = new Number;
		var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
		for (i=0; i<12; i++){
			a[i] = CNPJ.charAt(i);
			b += a[i] * c[i+1];
		}
		if ((x = b % 11) < 2) { a[12] = 0 } else { a[12] = 11-x }
		b = 0;
		for (y=0; y<13; y++) {
			b += (a[y] * c[y]); 
		}
		if ((x = b % 11) < 2) { a[13] = 0; } else { a[13] = 11-x; }
		if ((CNPJ.charAt(12) != a[12]) || (CNPJ.charAt(13) != a[13])){
			erro = "CNPJ inválido!";
		}
		if (erro.length > 0)
			{
			alert(erro);
                        document.forms[formulario].cnpj.focus();
			return false;
			}
		else 
		       {
        	       return true;
		       }
	}


function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function






function checar(formulario)
{
for(var i=0;i<formulario.elements.length;i++)
     {
	 var campo=formulario.elements[i];
	 
	 if(campo.type=="text" || campo.type=="hidden" || campo.type=="select" || campo.type=="select-one" || campo.type=="password" || campo.type=="file" || campo.type=="textarea")
	      {
	 	  var obriga=campo.getAttribute('obrigatorio');
	 	  var nomecampoatual=campo.getAttribute('nomecampo');
		  
		  if(obriga==1 && trim(campo.value)=="")
		       {
			   alert("O campo '" + nomecampoatual + "' não pode ficar vazio.");
			   campo.focus();
			   return false;
			   break;
			   }
		  }
	 }
}



function define_pessoa(formulario)
	      {
		  //se está selecionado o contato, o campo select é obrigatório, senão, o campo text outro é obrigatório
		  if(formulario.quem_radio[0].checked==true)
		       {
			   var selecionado=formulario.quem_radio[0].value;
			   formulario.id_contato.obrigatorio=1;
			   formulario.quem.obrigatorio=0;
			   }
		  else
		       {
			   var selecionado=formulario.quem_radio[1].value;
			   formulario.id_contato.obrigatorio=0;
			   formulario.quem.obrigatorio=1;
			   }
		  }


function limita_texto(field, countfield, maxlimit) 
     {
	 if (field.value.length > maxlimit) // if too long...trim it!
	      {
		  field.value = field.value.substring(0, maxlimit);
		  }
     else 
	      {
		  countfield.innerHTML = maxlimit - field.value.length;
		  }
     }




function validarmaximo(valor, maximo)
     {
	 if(valor>maximo)
	      {
		  window.alert("O valor máximo para este campo é " + maximo);
		  return true;
		  }
	 else
	      {
		  return false;
		  }
	 }



function validarminimo(valor, minimo)
     {
	 if(trim(valor)!="" && valor<minimo)
	      {
		  window.alert("O valor mínimo para este campo é " + minimo);
		  return true;
		  }
	 else
	      {
		  return false;
		  }
	 }


function validarnumero()
{
var tecla=window.event.keyCode;
var valor=String.fromCharCode(tecla);

if( ( (parseInt(valor) !=valor) && tecla!=13 && tecla!=46  && tecla!=8 ) || (valor=="."))
	{
	event.returnValue=false;
	}
}



function validardecimal()
{
var tecla=window.event.keyCode;
var valor=String.fromCharCode(tecla);

if (parseInt(valor)!=valor && valor!="-" && valor!="." && event.keyCode!="13")
	{
	event.returnValue=false;
	}
}



function validardecimalbr()
{
var tecla=window.event.keyCode;
var valor=String.fromCharCode(tecla);

if (parseInt(valor)!=valor && valor!="-" && valor!="," && event.keyCode!="13")
	{
	event.returnValue=false;
	}
}


function pular(id_campo, id_proximo_campo)
{
var campo=document.getElementById(id_campo);
var proximo_campo=document.getElementById(id_proximo_campo);
//alert("id_campo: " + id_campo);

if(campo.type=='text')
	{
	var maximo=campo.getAttribute('maxlength');
	var tam_string=campo.value.length;
	
	if(tam_string==maximo)
	     {
		 proximo_campo.focus();
		 }	
	}
}







function nao_navega()
	{
	if(window.event.srcElement.tagName=="A" || window.event.srcElement.tagName=="IMG")
		{
		alert('Ops! \n\nEsta página é só para impressão. Não é possível navegar a partir dela.')
		}
	}





function exibe_foto(foto_url, largura, altura) 
{ 

var destino = '../ferramentas/mostrafoto.php?foto_url=' + foto_url + '&largura=' + largura + '&altura=' + altura;

var esquerda=(screen.availWidth-largura)/2;
var topo=(screen.availHeight-altura)/2;;

window.open(destino, '','toolbar=no, scrollbars=no, left=' + esquerda + ', top=' + topo + ', width=' + largura + ', height= ' + altura + ''); 
} 



function amplia_galeria(galeria_id, foto_id_escolhida) 
{ 

var destino = '../galeria_janela.php?galeria_id=' + galeria_id + '&foto_id_escolhida=' + foto_id_escolhida;

var altura=100;
var largura=100;
var esquerda=(screen.availWidth-largura)/2;
var topo=(screen.availHeight-altura)/2;;

window.open(destino, '','toolbar=no, scrollbars=yes, left=' + esquerda + ', top=' + topo + ', width=' + largura + ', height= ' + altura + ''); 
} 


function janela_impressao(arquivo) 
{ 

var destino = '../ferramentas/imprime_frames.php?arquivo=' + arquivo;

var largura=580;
var altura=400;

var esquerda=(screen.availWidth-largura)/2;
var topo=(screen.availHeight-altura)/2;;

window.open(destino, '','toolbar=no, scrollbars=yes, left=' + esquerda + ', top=' + topo + ', width=' + largura + ', height= ' + altura + ''); 
} 


function janela_envio(arquivo, usuario_id_selecionado, treino_id_selecionado) 
{ 

var destino = '../ferramentas/envia_frames.php?arquivo=' + arquivo + '&usuario_id_selecionado=' + usuario_id_selecionado + '&treino_id_selecionado=' + treino_id_selecionado;

var largura=580;
var altura=400;

var esquerda=(screen.availWidth-largura)/2;
var topo=(screen.availHeight-altura)/2;;

window.open(destino, '','toolbar=no, scrollbars=yes, left=' + esquerda + ', top=' + topo + ', width=' + largura + ', height= ' + altura + ''); 
} 


function imagem(url) 
{ 

var destino = '../ferramentas/imagem.php?url=' + url;

var largura=100;
var altura=100;
var esquerda=(screen.availWidth-largura)/2;
var topo=(screen.availHeight-altura)/2;

window.open(destino, '','toolbar=no, scrollbars=no, left=' + esquerda + ', top=' + topo + ', width=' + largura + ', height= ' + altura + ''); 
} 



function janela_entrada() 
{ 
var destino="entrada.php";

var largura=200;

var altura=200;

var esquerda=((screen.availWidth-largura)/2);

var topo=(screen.availHeight-altura)/2;

window.open(destino, '','toolbar=no, scrollbars=no, left=' + esquerda + ', top=' + topo + ', width=' + largura + ', height= ' + altura + ''); 
} 


function confirma(destino, mensagem)
{

var quero;

quero=window.confirm(mensagem);

if(quero)
     {
	 window.location.href=destino;
	 }
}


function janela_organizacao(organizacao_id) 
{ 
var destino="../detalhes_organizacao.php?organizacao_id=" + organizacao_id;

var largura=350;

var altura=250;

var esquerda=((screen.availWidth-largura)/2);

var topo=(screen.availHeight-altura)/2;

window.open(destino, '','toolbar=no, scrollbars=no, left=' + esquerda + ', top=' + topo + ', width=' + largura + ', height= ' + altura + ''); 
} 


function tecla(e)
     {
	 return unicode=e.keyCode? e.keyCode : e.charCode;
	 }


function simulashiftab(e)
	{
	if(tecla(e)==40)
		{
		simulatab(e);
		}
	
	if(!e.target && tecla(e)==38)
		{
		var campo= window.event.srcElement ? window.event.srcElement : event.target;
		var indice=campo.sourceIndex;
		var anterior=indice-2;
		document.all.item(anterior).focus();
		}
	}

function simulatab(e)
	{
	if(tecla(e)==40)
		{
		if(e.target)
			{
			return false;
			//var campo= document.getElementById(e.target.id);
			//var indice=acha_indice(campo);
			}
		else
			{
			event.keyCode = 9;
			}
		}
	return false;
	}


-->