function valida_valor(tipo_validacao,valor)
{
   switch(tipo_validacao)
   {
       case 'obrigatorio':
           if(valor!=undefined && valor!='' && valor != null)
           {
               return true;
           }
       break;
       case 'numerico':
           if(!(isNaN(valor)))
           {
               return true;
           }
       break;
       case 'cpf':
       break;
       case 'cnpj':
       break;
       case 'email':
           return /^\w+([\.-\_]?\w+)*@\w+([\.-\_]?\w+)*(\.\w{2,3})+$/.test(endereco);
       break;
       case 'numero_residencial':
           /*if(/^\d{1,3}*-\d{1,4}/.teste(valor) || / /
           caracs_antes_traco =
           for(var i=0;i<valor.length;i++)
           {
                         }*/
       break;
       case 'data':
           //RECEBE A DATA NO FORMATADO DD/MM/AAAA
           var dias_fevereiro = new Array(29,28);
           var dia_meses = new Array(31,dias_fevereiro,31,30,31,30,30,31,30,31,30,31);
           var dia = valor.substr(0,2);
           dia = parseInt(dia,10);
           var mes = valor.substr(3,2);
           mes = parseInt(mes,10);
           var ano = valor.substr(6,4);
           ano = parseInt(ano);
           if(dia>0 && dia<=31 && mes>0 && mes<=12 && ano>0 && ano<=9999)
           {
               mes--;
               if(mes==1)
               {
                   var ano_nao_bissexto = ano%4;
                   if(dia>dia_meses[mes][ano_nao_bissexto])
                   {
                       return false;
                   }
               }
               else
               {
                   if(dia>dia_meses[mes])
                   {
                       return false;
                   }
               }
               return true;
           }
           else
           {
               return false;
           }
       break;
       default:
           alert('Tipo de validação inválida -> '+tipo_validacao);
       break;
   }
   return false;
} 
function fonte()
{
	if (event.button == 2 || event.button == 3)
	   {
		  alert("Opção inválida!");  
	   }
}

function tecla()
{
	if (event.keyCode == 116)
	   {
		   event.keyCode = 0;
		   event.returnValue=false;
		   document.onKeyDown=tecla;
	   }
}

//Função para carregar tela de login
//function Sistemas(url1){window.open(url1, "_blank", "height = 540, width = 780, menubar ='no', status='no'");}
function Sistemas(url1){window.open(url1, "_blank", "height = 980, width = 1024, menubar ='no', status='no',scrollbars = yes");}
function SistemasAjuda(url1){window.open(url1, "_blank", "height = 140, width = 380, menubar ='no', status='no'");}

function printit(){window.print() ;} 



function delimita_tamanho(input,evento,tamanho)
{
	if (input.value.length == tamanho) 
	   {
		    alert('Número de caracteres excedeu o limite'); 
			return false;
	   }
}


function ajusta_campo(input,evento,tipo)
  {	  
	var BACKSPACE=  8; 
    var ESPACO=  32; 
    var DEL=  46; 
    var FRENTE=  39; 
    var TRAS=  37; 
    var key; 
    var tecla; 
	var tamanho;
	var strValidos;
	var temp;
	
	tecla= (evento.keyCode ? evento.keyCode: evento.which ? evento.which : evento.charCode)
    if (( tecla == BACKSPACE )||(tecla == DEL)||(tecla == FRENTE)||(tecla == TRAS)) {return true;}
    if ( tecla == 13 ){return false;}
    if ((tecla<48)||(tecla>57)){return false;}
    key = String.fromCharCode(tecla); 
    input.value = input.value+key;	
	temp="";
	
		if (tipo == 'data')
		   {
			   tamanho = 10;
			   strValidos = "0123456789";
		   }
		if (tipo == 'li_sql')
		   {
			   tamanho = 19;
			   strValidos = "0123456789";
		   }
		if (tipo == 'sus')
		   {
			   tamanho = 19;
			   strValidos = "0123456789";
		   }	
		if (tipo == 'ctps')
		   {
			   tamanho = 19;
			   strValidos = "0123456789.";
		   }			   
		if (tipo == 'tre')
		   {
			   tamanho = 22;
			   strValidos = "0123456789.";
		   }			   
		if (tipo == 'rg')
		   {
			   tamanho = 13;
			   strValidos = "0123456789xX";
			   if (input.value.length < 10)
				  {
						if (input.value.length == 9){input.value = '0'+input.value;}
						if (input.value.length == 8){input.value = '00'+input.value;}
						if (input.value.length == 7){input.value = '000'+input.value;}
						if (input.value.length == 6){input.value = '0000'+input.value;}
						if (input.value.length == 5){input.value = '00000'+input.value;}
						if (input.value.length == 4){input.value = '000000'+input.value;}
						if (input.value.length == 3){input.value = '0000000'+input.value;}
						if (input.value.length == 2){input.value = '00000000'+input.value;}
						if (input.value.length == 1){input.value = '000000000'+input.value;}	
						if (input.value.length == 0){input.value = '0000000000'+input.value;}					
		
				 }			   
		   }
		if (tipo == 'numero')
		   {
			   tamanho = 8;
			   strValidos = "0123456789";
		   }
		if (tipo == 'telefone')
		   {
			   tamanho = 9;
			   strValidos = "0123456789";
		   }
		if (tipo == 'cep')
		   {
			   tamanho = 9;
			   strValidos = "0123456789";
		   }			   
		if (tipo == 'cpf')
		   {
			   tamanho = 14;
			   strValidos = "0123456789";
			   if (input.value.length < 11)
				  {
						if (input.value.length == 10){input.value = '0'+input.value;}				
						if (input.value.length == 9){input.value = '00'+input.value;}
						if (input.value.length == 8){input.value = '000'+input.value;}
						if (input.value.length == 7){input.value = '0000'+input.value;}
						if (input.value.length == 6){input.value = '00000'+input.value;}
						if (input.value.length == 5){input.value = '000000'+input.value;}
						if (input.value.length == 4){input.value = '0000000'+input.value;}
						if (input.value.length == 3){input.value = '00000000'+input.value;}
						if (input.value.length == 2){input.value = '000000000'+input.value;}
						if (input.value.length == 1){input.value = '0000000000'+input.value;}	
						if (input.value.length == 0){input.value = '00000000000'+input.value;}							
				 }				   
		   }		
		if (tipo == 'cnpj')
		   {
			   tamanho = 18;
			   strValidos = "0123456789";
			   if (input.value.length < 14)
				  {
						if (input.value.length == 13){input.value = '0'+input.value;}	
						if (input.value.length == 12){input.value = '00'+input.value;}	
						if (input.value.length == 11){input.value = '000'+input.value;}							
						if (input.value.length == 10){input.value = '0000'+input.value;}				
						if (input.value.length == 9){input.value = '00000'+input.value;}
						if (input.value.length == 8){input.value = '000000'+input.value;}
						if (input.value.length == 7){input.value = '0000000'+input.value;}
						if (input.value.length == 6){input.value = '00000000'+input.value;}
						if (input.value.length == 5){input.value = '000000000'+input.value;}
						if (input.value.length == 4){input.value = '0000000000'+input.value;}
						if (input.value.length == 3){input.value = '00000000000'+input.value;}
						if (input.value.length == 2){input.value = '000000000000'+input.value;}
						if (input.value.length == 1){input.value = '0000000000000'+input.value;}	
						if (input.value.length == 0){input.value = '00000000000000'+input.value;}							
				 }					   
		   }				   
	
	
	         for (var i = 0; i<input.value.length;i++ )
             {
				if (tipo == 'data')
				   {
					   if (temp.length==2) temp=temp+"/";
					   if (temp.length==5) temp=temp+"/";
				   }
				if (tipo == 'rg')
				   {
					   if (temp.length==3) temp=temp+".";
					   if (temp.length==7) temp=temp+".";
					   if (temp.length==11) temp=temp+"-";	
				   }
				if (tipo == 'numero')
				   {
               		   if (temp.length==2) temp=temp+"-";
				   }
				if (tipo == 'telefone')
				   {
               		   if (temp.length==4) temp=temp+"-"; 
				   }
				if (tipo == 'cep')
				   {
               		   if (temp.length==5) temp=temp+"-";
				   }		
				if (tipo == 'cpf')
				   {
					   if (temp.length==3) temp=temp+".";
					   if (temp.length==7) temp=temp+".";
					   if (temp.length==11) temp=temp+"-";
				   }	
				if (tipo == 'li_sql')
				   {
					   if (temp.length==3) temp=temp+".";
					   if (temp.length==7) temp=temp+".";
					   if (temp.length==11) temp=temp+".";					   
					   if (temp.length==15) temp=temp+".";
				   }	
				if (tipo == 'sus')
				   {
					   if (temp.length==3) temp=temp+".";
					   if (temp.length==7) temp=temp+".";
					   if (temp.length==11) temp=temp+".";					   
					   if (temp.length==15) temp=temp+".";
				   }					   
				if (tipo == 'cnpj')
				   {
					   if (temp.length==2) temp=temp+".";
					   if (temp.length==6) temp=temp+".";
					   if (temp.length==10) temp=temp+"/";
					   if (temp.length==15) temp=temp+"-";
				   }				
						if ( strValidos.indexOf( input.value.substr(i,1) ) != -1 ){temp=temp+input.value.substr(i,1);}  
			 }
         
		 input.value = temp.substr(0,tamanho);
         return false;  
  }


// FUNÇÃO DE VALIDAÇÃO DO FORMULÁRIO

function TRAMA_findObj(n, d) { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=TRAMA_findObj(n,d.layers[i].document); return x;
}

function TRAMA_validateForm() 
  { 
     var a,h,j,t,teste,i,p,q,campo_nome,campo_descricao,campo_verificacao,num,min,max,formulario_erros='',args=TRAMA_validateForm.arguments;
     for (i=0; i<(args.length-2); i+=3)
	     {
			campo_descricao		=args[i+1];
			campo_verificacao	=args[i+2];
			campo_conteudo		=TRAMA_findObj(args[i]);
			
			if (campo_conteudo) 
			   {
					campo_nome=campo_conteudo.name;
					if ((campo_conteudo=campo_conteudo.value)!="") 
					   {
							if (campo_verificacao == 'email') // Checa se o campo eh um email
								{ 
								arroba=campo_conteudo.indexOf('@');
								ponto=campo_conteudo.indexOf('.');
								if (arroba<1 || arroba==(campo_conteudo.length-1) || ponto<1 || ponto==(campo_conteudo.length-1)) formulario_erros+='›› '+campo_descricao+' precisa conter um e-mail válido.\n';
								} 
						else if (campo_verificacao == 'url') // Checa se o campo eh um url
							    { 
									if (campo_conteudo != "http://") // Se o campo estiver diferente do valor padrão...
										{
										protocolo=campo_conteudo.indexOf('tp://');
										ponto=campo_conteudo.indexOf('.');
										if (protocolo<1 || protocolo==(campo_conteudo.length-1) || ponto<1 || ponto==(campo_conteudo.length-1)) formulario_erros+='›› '+campo_descricao+' precisa conter um URL válido.\n';
										}
							    } 
						else if (campo_verificacao == 'cpf') 
							{ 
							 if (campo_conteudo == '111.111.111-11' || campo_conteudo == '222.222.222-22' || campo_conteudo == '333.333.333-33' || campo_conteudo == '444.444.444-44' || campo_conteudo == '555.555.555-55' || campo_conteudo == '666.666.666-66' || campo_conteudo == '777.777.777-77' || campo_conteudo== '888.888.888-88' || campo_conteudo == '999.999.999-99' || campo_conteudo == '000.000.000-00')
								{ formulario_erros+='›› '+campo_descricao+' Inválido.\n'; }
							else
								{
									  var c1 = campo_conteudo.substr(0,3);
									  var c2 = campo_conteudo.substr(4,3);
									  var c3 = campo_conteudo.substr(8,3);
									  var c = c1+''+c2+''+c3;
									  var dv = campo_conteudo.substr(12,2);
									  var d1 = 0; 
									  for (t = 0; t < 9; t++) 
										  {d1 += c.charAt(t)*(10-t);}
										  if (d1 == 0)
											 {
												 //formulario_erros+='›› '+campo_descricao+' Inválido.\n'; 
											 }				
											 
									d1 = 11 - (d1 % 11);
									if (d1 > 9) d1 = 0; 
									if (dv.charAt(0) != d1) 
									   {
											//formulario_erros+='›› '+campo_descricao+' Inválido.\n'; 
									   }
									d1 *= 2;
									for (j = 0; j < 9; j++) {d1 += c.charAt(j)*(11-j);}
									d1 = 11 - (d1 % 11);
										if (d1 > 9) d1 = 0;
										if (dv.charAt(1) != d1) 
										   {
											formulario_erros+='›› '+campo_descricao+' Inválido.\n'; 
										  }								  
								} 	
							}
						else if (campo_verificacao == 'cnpj') 
							{ 
							
									var c1 = campo_conteudo.substr(0,2); 
									var c2 = campo_conteudo.substr(3,3); 
									var c3 = campo_conteudo.substr(7,3); 
									var c4 = campo_conteudo.substr(11,4); 
									var c = c1+''+c2+''+c3+''+c4;
									var dv = campo_conteudo.substr(16,2);
									var d1 = 0;
			  
									for (a = 0; a <12; a++)
										{d1 += c.charAt(11-a)*(2+(a % 8)); }
										if (d1 == 0){formulario_erros+='›› '+campo_descricao+' Inválido.\n'; }
										d1 = 11 - (d1 % 11);
										if (d1 > 9) d1 = 0;
										if (dv.charAt(0) != d1){formulario_erros+='›› '+campo_descricao+' Inválido.\n';}
									   d1 *= 2;
									   for (h = 0; h < 12; h++){ d1 += c.charAt(11-h)*(2+((h+1) % 8)); }
									   d1 = 11 - (d1 % 11);
									   if (d1 > 9) 
									   d1 = 0;
									   if (dv.charAt(1) != d1)
										  {
											//formulario_erros+='›› '+campo_descricao+' Inválido.\n'; 
										  }
							
							}
				
		else if (campo_verificacao == 'negativo') // Checa se o campo eh um numero
				{ 
				num = parseFloat(campo_conteudo);
				if ( num < 0 ) formulario_erros+='›› '+campo_descricao+' não permite valores negativos.\n';
				}	
				
		else if (campo_verificacao == 'telefone') // Checa se o campo eh um numero
				{ 
				 if (campo_conteudo.length < 9){formulario_erros+='›› '+campo_descricao+' incompleto.\n';}					    
				}					
				
		else if (campo_verificacao == 'data') 
				{ 
				   	   if (campo_conteudo.length == 10)
					      {					   
							   barras = campo_conteudo.split("/");                       
							   dia = barras[0];
							   mes = barras[1];
							   ano = barras[2];		
							   hoje = new Date();
							   anoAtual = hoje.getFullYear();
							   
							   if (dia.length < 2){formulario_erros+='›› '+campo_descricao+' - dia precisa conter 2 digitos.\n';}			
							   if (dia > 31){formulario_erros+='›› '+campo_descricao+' - dia deve ser menor ou igual a 31.\n';}	
							   if (dia < 0){formulario_erros+='›› '+campo_descricao+' - dia deve ser maior que 0.\n';}		
							   
							   if (mes.length < 2){formulario_erros+='›› '+campo_descricao+' - mês precisa conter 2 digitos.\n';}		
							   if (mes > 12){formulario_erros+='›› '+campo_descricao+' - mês deve ser menor ou igual a 12.\n';}	
							   if (mes < 0){formulario_erros+='›› '+campo_descricao+' - mês deve ser maior que 0.\n';}			
												   
							   if (ano.length < 4){formulario_erros+='›› '+campo_descricao+' - ano precisa conter 4 digitos.\n';}				   
							   if (ano > anoAtual){formulario_erros+='›› '+campo_descricao+' - ano deve ser menor ou igual ao ano atual.\n';}
							   if (ano < 1900){formulario_erros+='›› '+campo_descricao+' - ano deve ser maior que 1900.\n';}
						  }
  					  else
					      {formulario_erros+='›› '+campo_descricao+' Inválida.\n';}
				}					
				
		else if (campo_verificacao == 'numero') // Checa se o campo eh um numero
				{ 
				num = parseFloat(campo_conteudo);
				if ( parseFloat(campo_conteudo)!=''+num) formulario_erros+='›› '+campo_descricao+' precisa conter um número.\n';
				}											
			} 			
    else if (campo_verificacao == 'obrigatorio')
			{
			formulario_erros += '›› '+campo_descricao+' é um campo obrigatório e está em branco.\n';
			}
// testar data maior
    else if (campo_verificacao == 'datamaior')
			{
			$datamaior = exlplode(":",$campo_conteudo.length);
			if (($datamaior[0]) >= ($datamaior[1])){formulario_erros += '›› '+campo_descricao+' Data inicio nao pode ser maior\n';
}
		}	

// ------------
		}
	} 
  if (formulario_erros) alert('Operação cancelada \n\n'+formulario_erros);
  document.TRAMA_returnValue = (formulario_erros == '');
}
