/*******************************************************************************************************************
* PROCURA CAMPOS OBRIGATORIOS E INFORMA-OS NA TELA
* procura_obrigatorio('campo1;campo2;campo3)
*******************************************************************************************************************/
function procura_obrigatorio(c)
{
	var campos 			= c.split(";");
	var obrigatorio =""; 
	for (i = 0; i < campos.length; i++)
	{
		obj = document.getElementById(campos[i]);
			if (obj.value =="")
			{
				obj.className = "texto_padrao_obrigatorio";
				obrigatorio="sim";
			}	
			else
			{
				obj.className = "texto_padrao";
			}	
	}
	if ( obrigatorio == "")
	{
		return true;
	}
	else
	{
		alert("Existem campos obrigatórios - favor preencher");
		return false;
	}
}

// FunÃ§Ã£o que recebe o campo e o tipo de extensÃ£o (jpg, doc, etc) e valida
function validar_extensao(campo,extensao)
{	
	var aNomeArquivo, cNomeExt;
	aNomeArquivo = campo.value.split(".");
	cNomeExt = aNomeArquivo[aNomeArquivo.length-1];
	cNomeExt = cNomeExt.toLowerCase();
	if((cNomeExt!=extensao) && (cNomeExt!=extensao))
	{
		alert('Tipo de Arquivo Inválido!\n\São aceitos somente arquivo do tipo '+extensao);
		//campo.value = "";
		return false;
	}
	else
	{
		return true;
	}	
}

// FunÃ§Ã£o que recebe o campo e o tipo de extensÃ£o (jpg, doc, etc) e valida
function validar_extensoes(campo,extensoes)
{	
	var aNomeArquivo, cNomeExt;
	aNomeArquivo = campo.value.split(".");
	cNomeExt = aNomeArquivo[aNomeArquivo.length-1];
	cNomeExt = cNomeExt.toLowerCase();
	if(extensoes.search(cNomeExt) == -1)
	{
		alert('Tipo de Arquivo Inválido!\n\São aceitos somente arquivo(s) do tipo '+extensoes);
		return false;
	}	
	else
	{
		return true;
	}
}

// FunÃ§Ã£o que cria a requisiÃ§Ã£o AJAX
function new_xmlttp()
{
	var xmlhttp = false;
	// Verifica se estamos usando IE.
	try 
	{
		// Se a versÃ£o Javascript for maio que 5.
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e)
	{
		// Se nao, entÃ£o usar o objeto activeX main antigo.
		try
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (E)
		{
			// Ou devemos estar usando um navegador diferente do IE.
			xmlhttp = false;
		}
	}
	// Se estivermos usando um navegador diferente do IE, criar uma instancia JavasScript do Objeto
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
	{
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

/* ============= TESTA CAMPO NUMERICO */
/* onkeypress="return verificanumerico(event);" */
function verificanumerico(caracter, campo) 
{
	if(window.event) 
	{ // Internet Explorer
		var tecla = event.keyCode;
	}
	else 
	{ // Firefox
		var tecla = caracter.which;
		//			  alert (tecla);
	}
	if ((tecla != 48) && (tecla != 49) && (tecla != 50) && (tecla != 51) && (tecla != 52) && (tecla != 53) && 
	(tecla != 54) && (tecla != 55) && (tecla != 56) && (tecla != 57) && (tecla != 8) && (tecla != 9) && 
	(tecla != 0) && (tecla != 46) && (tecla != 13))
	{ 
		return false;
	}
	else 
	{ 
		return true; 
	}
}

/* ============= TESTA CAMPO NUMERICO */
/* onkeypress="return verificanumerico(event);" */
function verdata(campo, caracter) 
{
	if(window.event) 
	{ // Internet Explorer
		var tecla = event.keyCode;
	}
	else 
	{ // Firefox
		var tecla = caracter.which;
	//			  alert (tecla);
	}
	if(tecla == 47) // Barra ( / )
	{
		if((campo.value.length!=2) && (campo.value.length!=5))
		{
		return false;
		}
	}
	else
	{
		if ((tecla != 48) && (tecla != 49) && (tecla != 50) && (tecla != 51) && (tecla != 52) && (tecla != 53) && 
		(tecla != 54) && (tecla != 55) && (tecla != 56) && (tecla != 57) && (tecla != 8) && (tecla != 9) && 
		(tecla != 0) && (tecla != 46) && (tecla != 13))
		{ 
			return false;
		}
		else 
		{ 
			return true; 
		}
	}			
}

// Formata o campo para Data (DD/MM/AAAA)
function formataDate(campo,caracter) 
{
	if(window.event) 
	{ // Internet Explorer
		var tecla = event.keyCode;
	}
	else 
	{ // Firefox
		var tecla = caracter.which;
		//			  alert (tecla);
	}
	if((tecla==111) || (tecla==193))  // tecla barra ( / )
	{
		if((campo.value.length!=3) && (campo.value.length!=6)) // Se nÃ£o digitou a barra na posiÃ§Ã£o correta ( 3 e 6 ), elimina
		{
			campo.value = campo.value.substring( 0, (campo.value.length-1) );
		}
	}
	else
	{
		campo.value = filtracampo(campo);
		vr = campo.value;
		tam = vr.length;
		if( (tam > 2) && (tam <= 4) ) // 12 3 atÃ© 12 34
		{
			campo.value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, tam ); 
		}
		if( (tam > 4) && (tam <= 8) ) // 12 34 5 atÃ© 12 34 5678
		{
			campo.value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 2 )  + '/' + vr.substr( 4, tam ); 
		}
	}
}

// Formata o campo para Hora (hh:mm)
function formatahora(campo,caracter) 
{
	if(window.event) 
	{ // Internet Explorer
		var tecla = event.keyCode;
	}
	else 
	{ // Firefox
		var tecla = caracter.which;
		//			  alert (tecla);
	}
	if(tecla==191)  // tecla barra ( : )
	{
		if(campo.value.length!=3) // Se nÃ£o digitou a barra na posiÃ§Ã£o correta ( 3 ), elimina
		{
			campo.value = campo.value.substring( 0, (campo.value.length-1) );
		}
	}
	else
	{
		campo.value = filtracampo(campo);
		vr = campo.value;
		tam = vr.length;
		if( (tam > 2) && (tam <= 4) ) // 12 3 atÃ© 12 34
		{
			campo.value = vr.substr( 0, 2 ) + ':' + vr.substr( 2, tam ); 
		}
	}
}

// Formata o campo para NumÃ©rico (12.345.678.901,23)
function formatavalor(campo)
{
	aux = campo.value.split(',');
	len = aux.length;
	campo.value = filtracampo(campo);
	vr = campo.value;
	tam = vr.length;
	if( tam <= 2 )
	{ 
		//campo.value = vr; 
		if(tam == 0)
		{ 
			campo.value = '0,00';
		}
		if(tam == 1)
		{ 
			campo.value = '0,0' + vr;
		}
		if(tam == 2)
		{
			campo.value = '0,' + vr;
		}
	}
	if( (tam > 2) && (tam <= 5) )
	{
		campo.value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ); 
	}
	if( (tam >= 6) && (tam <= 8) )
	{
		campo.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); 
	}
	if ( (tam >= 9) && (tam <= 11) )
	{
		campo.value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); 
	}
	if ( (tam >= 12) && (tam <= 14) )
	{
		campo.value = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); 
	}
	if ( (tam >= 15) && (tam <= 18) )
	{
		campo.value = vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam );
	} 		
	if(campo.value.length>4)
	{
		// Elimina zeros a esquerda. Ex.: 01,23 ou .4,56 ou 0.1234,56
		if(campo.value.substring(0,2)=="0.")
		{
			campo.value = campo.value.substring(2,campo.value.length);
		}
		else
		{
			if(campo.value.substring(0,1)=="0")
			{
				campo.value = campo.value.substring(1,campo.value.length);
			}
		}
	}
}

// Formata o campo para Inteiro (1234567890123)
function formatainteiro(campo)
{
	vr = campo;
	tam = vr.value.length;
	if( tam == 0 )
	{ 
		campo.value = '';
	}
	if(tam>1)
	{
		// Elimina zeros a esquerda. Ex.: 01,23 ou .4,56 ou 0.1234,56
		if(vr.value!='0')
		{
			if(vr.value.substring(0,1)=="0")
			{
				campo.value = vr.value.substring(1,tam);
			}
			else
			{
				campo.value = vr.value;
			}
		}
	}
	/*
	if(campo.value=="")
	{
		campo.value = '';
	}
	*/
}

// onkeypress="return retornainteiro(this,event);"	
function retornainteiro(campo, caracter) 
{
	if(window.event) 
	{ // Internet Explorer
		var tecla = event.keyCode;
	}
	else 
	{ // Firefox
		var tecla = caracter.which;
		//			  alert (tecla);
	}
	if ((tecla != 48) && (tecla != 49) && (tecla != 50) && (tecla != 51) && (tecla != 52) && (tecla != 53) && 
		(tecla != 54) && (tecla != 55) && (tecla != 56) && (tecla != 57) && (tecla != 8) && (tecla != 9) && 
		(tecla != 0)  && (tecla != 109))
	{ 
		return false;
	}
	else 
	{ 
		return true; 
	}
}

/* ============= VALIDAÃ‡ÃƒO DA DATA ======================= */
/* onBlur="VerificaData(this);" */
function VerificaData(digData)
{
	var bissexto = 0;
	var data = digData.value;
	var tam  = data.length;
	if (tam == 10)
	{
		var dia = data.substr(0,2)
		var mes = data.substr(3,2)
		var ano = data.substr(6,4)
		if ((ano > 1900)||(ano < 2100))
		{
			switch (mes)
			{
				case '01':
				case '03':
				case '05':
				case '07':
				case '08':
				case '10':
				case '12':
				if(dia <= 31)
				{
					return true;
				}
				break
			
				case '04':        
				case '06':
				case '09':
				case '11':
				if  (dia <= 30)
				{
					return true;
				}
				break

				case '02':
				/* Validando ano Bissexto / fevereiro / dia */
				if ((ano % 4 == 0) || (ano % 100 == 0) || (ano % 400 == 0))
				{
					bissexto = 1;
				}
				if ((bissexto == 1) && (dia <= 29))
				{
					return true;                
				}
				if ((bissexto != 1) && (dia <= 28))
				{
					return true;
				}            
				break                        
			}
		}
		alert("A Data "+data+" é inválida!");
		digData.value = "";
		digData.focus();
		return false;
	}    
}
/* =============  Valida a Hora no formato HH:MM */
/* =============  onBlur(this) */
function verifica_hora(hora)
{ 
	hrs = (hora.value.substring(0,2)); 
	min = (hora.value.substring(3,5)); 
	situacao = ""; 
	// verifica data e hora 
	if ((hrs < 00 ) || (hrs > 23) || ( min < 00) ||( min > 59))
	{ 
		situacao = "falsa"; 
	} 
	if(situacao == "falsa") 
	{ 
		alert("A Hora " + hora.value + "é inválida!"); 
		hora.value = ""; 
		//hora.focus(); 
	} 
} 

/* ============= TESTA QUALQUER CAMPO DE E-MAIL */
function testaemail(form_name)
{
	var filtro=/^.+@.+\..{2,3}$/
	if (filtro.test(form_name.email.value) == false)
	{
		alert("O email informado não é valido");
		form_name.email.focus();
		return false;
	}
	else
	{
		return true;
	}
}
	
/* ============== FORMATA CAMPOS NUMERICOS,DATAS,HORARIOS ETC. */
/* Data  OnKeyup="return formatar(this,'##/##/####',event)" */
/* Cep   OnKeyup="return formatar(this,'#####-###'),event)" */
/* Valor OnKeyup="return formatar(this,'###.##',event)" */  
function formatar(src, mask, evento) 
{
	if(window.event) 
	{ // Internet Explorer
		var tecla = event.keyCode;
	}
	else 
	{ // Firefox
		var tecla = evento.which;
	}
	var i = src.value.length;
	var saida = mask.substring(0,1);
	var texto = mask.substring(i);
	if ((tecla != 48) && (tecla != 49) && (tecla != 50) && (tecla != 51) && (tecla != 52) && (tecla != 53) && 
	(tecla != 54) && (tecla != 55) && (tecla != 56) && (tecla != 57) && (tecla != 8) && (tecla != 9) && 
	(tecla != 0) && (tecla != 46)) 
	{ 
		return false;
	}
	else 
	{
		if ((texto.substring(0,1) != saida) && (tecla != 8))
		{
			src.value += texto.substring(0,1);
		}
		return true;					
	}
}
	
// ============== LIMPA TODOS OS CARACTERES ESPECIAIS DO CAMPO SOLICITADO 
function filtracampo(campo)
{
	var s = "";
	var cp = "";
	vr = campo.value;
	tam = vr.length;
	for (i = 0; i < tam ; i++) 
	{  
		if (vr.substring(i,i + 1) != "/" && vr.substring(i,i + 1) != "-" && vr.substring(i,i + 1) != "." && 
		vr.substring(i,i + 1) != ":" && vr.substring(i,i + 1) != "," )
		{
			s = s + vr.substring(i,i + 1);
		}
	}
	campo.value = s;
	return cp = campo.value
}

// ============== ESCONDE OU MOSTRA DIV
function div_hidden (id_div,id_botao)
{
	if (document.getElementById(id_div).style.display=='none')
	{
		document.getElementById(id_div).style.display='block';
	}
	else
	{
		document.getElementById(id_div).style.display='none';
	}
	if (id_botao!="")
	{
		if (document.getElementById(id_botao).className=='botao_negativo')
		{
			document.getElementById(id_botao).className='botao_positivo';
		}
		else
		{
			document.getElementById(id_botao).className='botao_negativo';
		}
	}
}

function iframeAutoHeight(quem,heu)
{
	//by Micox - elmicox.blogspot.com - elmicox.com - webly.com.br  
	if(navigator.appName.indexOf("Internet Explorer")>-1)
	{ //ie sucks
		var func_temp = function()
		{
			var val_temp = quem.contentWindow.document.body.scrollHeight;
			quem.style.height = val_temp + "px";
			//alert(val_temp)
		}
		setTimeout(function() { func_temp() },100) //ie sucks        
	}
	else
	{
		var val = quem.contentWindow.document.body.parentNode.offsetHeight;
		val = (parseInt(val) + 15);
		quem.style.height = val + "px";
	}    
}		

// ============== TESTA CPF ===
function chkcpf(campo) 
{
	strcpf = campo.value;
	for ( i = 0; i < strcpf.length ; i++)
	{
		strcpf = strcpf.replace(".","");
		strcpf = strcpf.replace("/","");
	} 
	str_aux = "";
	for(i = 0; i <= strcpf.length - 1; i++)
	{
		if ((strcpf.charAt(i)).match(/\d/))
			str_aux += strcpf.charAt(i);
		else if (!(strcpf.charAt(i)).match(/[\.\-]/)) 
		{
			alert (" O campo CPF apresenta caracteres inválidos !!!");
			campo.value="";
			campo.focus();
			return false;
		}
	}
	if((str_aux.length > 0) && (str_aux.length < 11))
	{
		alert ("O campo CPF deve conter 11 dígitos !!!");
		campo.value="";
		campo.focus();
		return false;
	}
	soma1 = soma2 = 0;
	for (i = 0; i <= 8; i++) 
	{
		soma1 += str_aux.charAt(i) * (10-i);
		soma2 += str_aux.charAt(i) * (11-i);
	}
	d1 = ((soma1 * 10) % 11) % 10;
	d2 = (((soma2 + (d1 * 2)) * 10) % 11) % 10;
	if ((d1 != str_aux.charAt(9)) || (d2 != str_aux.charAt(10))) 
	{
		alert ("O CPF digitado é inválido !!!");
		campo.value="";
		campo.focus();
		return false;
	}
	return true;
}

// ======== FORMATAÃ‡ÃƒO PARA TELEFONE DO TIPO (01)2345-6789
function formata_fones(campo, evento) 
{
	var x;
	//campo.value = filtracampo(campo);
	vr = campo.value;
	tam = vr.length;
	for (x = 0; x < tam; x++)
	{
		//alert(" x " + x + " caracter " + vr.substring(x,x+1));
		if((x == 0) && (vr.substring(x,x+1) != "("))
		{
			campo.value = '(' + vr.substr( 0, tam ); 
		}
		if((x == 3) && (vr.substring(x,x+1) != ")"))
		{
			campo.value =  vr.substr( 0, x ) + ')' + vr.substr( x, tam ); 
		}
		if((x == 8) && (vr.substring(x,x+1) != "-"))
		{
			campo.value =  vr.substr( 0, x ) + '-' + vr.substr( x, tam ); 
		}
	}	
}
/* ============= VALIDAÃ‡ÃƒO DA DATA ======================= */
/* onBlur="VerificaData(this);" */
function valida_data(digData)
{
	var data = digData.value;
	var bissexto = 0;
	var tam  = data.length;
	if(digData.value!='')
	{
		if (tam == 10)
		{
			var dia = data.substr(0,2)
			var mes = data.substr(3,2)
			var ano = data.substr(6,4)
			if ((ano > 1900)||(ano < 2100))
			{
				switch (mes)
				{
					case '01':
					case '03':
					case '05':
					case '07':
					case '08':
					case '10':
					case '12':
					if(dia <= 31)
					{
						return true;
					}
					break
				
					case '04':        
					case '06':
					case '09':
					case '11':
					if  (dia <= 30)
					{
						return true;
					}
					break
	
					case '02':
					/* Validando ano Bissexto / fevereiro / dia */
					if ((ano % 4 == 0) || (ano % 100 == 0) || (ano % 400 == 0))
					{
						bissexto = 1;
					}
					if ((bissexto == 1) && (dia <= 29))
					{
						return true;                
					}
					if ((bissexto != 1) && (dia <= 28))
					{
						return true;
					}            
					break                        
				}
			}
			alert("A Data "+data+" é inválida!");
			digData.value = "";
			digData.focus();
			return false;
		}
	}
}
/* =============  Valida a Hora no formato HH:MM */
/* =============  onBlur(this) */
function valida_hora(hora)
{ 
	if(hora.value!='')
	{
		hrs = (hora.value.substring(0,2)); 
		min = (hora.value.substring(3,5)); 
		situacao = ""; 
		// verifica data e hora 
		if ((hrs < 00 ) || (hrs > 23) || ( min < 00) ||( min > 59))
		{ 
			situacao = "falsa"; 
		} 
		if(situacao == "falsa") 
		{ 
			alert("A Hora " + hora.value + " é inválida!"); 
			hora.value = ""; 
			//hora.focus(); 
		}
	}
} 