var valida = '';

function onPesquisa() {
    if (query.value == '') {
        alert('Favor informar o texto da pesquisa!');
        query.focus();
    } else {
        self.location = pagina.value + '&query=' + query.value;
    }
    return false;
}

// Inicio Menu
function scrollOver() {
	scroller.scrollAmount='0';
	scroller.style.cursor='default';
}

function scrollOut() {
	scroller.scrollAmount='3';
}
// Fim Menu

function ChecaExtensaoArquivo(arquivo) {
	if (arquivo != '') {
		var extensoesOk = ",.bmp,.gif,.jpg,.png,.wmf,";
		var extensao    = "," + arquivo.substr( arquivo.length - 4 ).toLowerCase() + ",";
		if( extensoesOk.indexOf( extensao ) == -1 ){
			alert( arquivo + "\n\nNão possui uma extensão válida!" );
			return false;
		}
	}
	return true;
}

function checkdate(objName)
{	var datefield = objName;
	if (datefield.value == "")
		return true;

	if (chkdate(objName) == false) 
	{	//datefield.select();
		alert("Data inválida");
		datefield.value = ""
		datefield.focus();
		return false;
	}
	else
	{	//alert("Data Válida");
		return true;
   	}
}

function chkdate(objName) 
{	var strDate;
	var strDay;
	var strMonth;
	var strYear;
	var intday;
	var intMonth;
	var intYear;
	var datefield = objName;
	strDate = datefield.value;
	//concatenar os campos e verificar quantidades
	// considerando um campo só com quantidade certa.
	strDay = strDate.substr(0, 2);
	strMonth = strDate.substr(3, 2);
	strYear = strDate.substr(6, 4);
	strBarra1 = strDate.substr(2, 1);
	strBarra2 = strDate.substr(5, 1);
   	
	// Mostra os valores das variaveis
	//alert("strDay: " + strDay + "\nstrBarra1: " + strBarra1 + "\nstrMonth: " + strMonth + "\nstrBarra2: " + strBarra2 + "\nstrYear: " + strYear);

	intday = parseInt(strDay, 10);
	intday1 = parseInt(strDate.substr(0, 1), 10);
	intday2 = parseInt(strDate.substr(1, 1), 10);
	if ((isNaN(intday1)) || (isNaN(intday2)))
	{	//alert("Erro no dia");
		return false;
	}
	intMonth = parseInt(strMonth, 10);
	intMonth1 = parseInt(strDate.substr(3, 1), 10);
	intMonth2 = parseInt(strDate.substr(4, 1), 10);
	if ((isNaN(intMonth1)) || (isNaN(intMonth2)))
	{	//alert("Erro no mês");
		return false;
	}
	
	intYear = parseInt(strYear, 10);
	intYear1 = parseInt(strDate.substr(6, 1), 10);
	intYear2 = parseInt(strDate.substr(7, 1), 10);
	intYear3 = parseInt(strDate.substr(8, 1), 10);
	intYear4 = parseInt(strDate.substr(9, 1), 10);
	if ((isNaN(intYear1)) || (isNaN(intYear2)) || (isNaN(intYear3)) || (isNaN(intYear4)) || (intYear <= 1900))
	{	//alert("Erro no ano");
		return false;
	}
	
	if ((strBarra1 != "/") ||(strBarra2 != "/"))
	{	//alert("Erro na Barra / ");
		return false;
	}
	
	if (intMonth > 12 || intMonth < 1)
	{	return false;
	}
	if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1))
	{	return false;
	}
	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1))
	{	return false;
	}
	if (intMonth == 2)
	{	if (intday < 1)
		{	return false;
		}
		if (LeapYear(intYear) == true)
		{	if (intday > 29)
			{	return false;
			}
		}
		else
		{	if (intday > 28)
			{	return false;
			}
		}
	}
	return true;
}

function LeapYear(intYear)
{	if (intYear % 100 == 0)
	{	if (intYear % 400 == 0)
		{	return true;
		}
	}
	else
	{	if ((intYear % 4) == 0)
		{ 	return true;
		}
	}
	return false;
}

function Formata_Data(objeto,teclapres) {
	var tecla = teclapres.keyCode;
	vr = objeto.value;
	s="";
	for (x=0; x < vr.length; x++)
	 	if (vr.charCodeAt(x) >= 48 && vr.charCodeAt(x)<=57)
			s= s + vr.charAt(x);
	vr = s;
	tam = vr.length + 1;
	if ( tecla != 9 && tecla != 8 && tecla != 46 && tecla != 37 && tecla != 39 && tecla != 35 && tecla != 36){
		if ( tam < 2 )
			objeto.value = vr.substr(0,2);
		if ( tam > 2 && tam < 4 )
			objeto.value = vr.substr(0,2) + '/' + vr.substr(2,2);
		if ( tam > 4 )
			objeto.value = vr.substr(0,2) + '/' + vr.substr(2,2) + '/' + vr.substr(4,4); 
	}
	if (tam > 5 && (tecla == 8 || tecla == 46)) 
			objeto.value = vr.substr(0,2) + '/' + vr.substr(2,2) + '/' + vr.substr(4,4); 
	if ((tam > 2 && tam < 6) && (tecla == 8 || tecla == 46)) 
			objeto.value = vr.substr(0,2) + '/' + vr.substr(2,2);
	if (tam < 4 && (tecla == 8 || tecla == 46)) 
			objeto.value = vr.substr(0,2);
}

function Valida_Email(objeto) {
	if ((objeto.value.indexOf("@")==-1 || objeto.value.indexOf(".")==-1) && objeto.value!="")
		{ objeto.select();
		  alert("E-mail inválido");
		  objeto.value = "";
		  return false;
		}
	return true;
}

function Valida_Telefone(objeto) {
	str=objeto.value;
	tel="";

	if (str == "")
		return true
		
	if ((str.length<12 || str.length>13 || str.substring(0,1)!="(" || str.substring(3,4)!=")") && (str.indexOf("-")!=7 || str.indexOf("-")!=8)) {
		objeto.select();
		alert("Telefone inválido. Formato: (12)3456-7890");
    	objeto.value = "";
		objeto.focus();
		return false;
	}
	for (var i = 0; i < str.length; i++)
		if (str.substring(i,i+1) >= "0" && str.substring(i,i+1) <= "9")
		  	tel+=str.substring(i,i+1);	
	if (isNaN(tel)) {
		objeto.select();
		alert("Telefone inválido. Formato: (12)3456-7890");
		objeto.value = "";
		objeto.focus();
		return false;		
	}
	return true;
}

function Formata_Telefone(objeto,teclapres) {
	var tecla = teclapres.keyCode;
	vr = objeto.value;	
	s="" 
	for (x=0; x < vr.length; x++)
	 	if (vr.charCodeAt(x) >= 48 && vr.charCodeAt(x)<=57)
			s= s + vr.charAt(x)
	vr = s
	tam = vr.length + 1;
	if ( tecla != 9 && tecla != 8 && tecla != 46 && tecla != 37 && tecla != 39 && tecla != 35 && tecla != 36) {
	  if ( tam == 1 )
		objeto.value = vr.substr(0,1);
	  if ( tam > 1 && tam < 3 )
		objeto.value = '(' + vr;
	  if ( tam >=3 && tam < 7 )
		objeto.value = '(' + vr.substr(0,2) +  ')' + vr.substr(2,4);
	  if ( tam > 6 && tam < 11 )
		objeto.value = '(' + vr.substr(0,2) +  ')' + vr.substr(2,3) + '-' + vr.substr(5,4);
	  if ( tam > 10 )
		objeto.value = '(' + vr.substr(0,2) +  ')' + vr.substr(2,4) + '-' + vr.substr(6,4);
	}
	if (tam > 10 && (tecla == 8 || tecla == 46))
		objeto.value = '(' + vr.substr(0,2) +  ')' + vr.substr(2,4) + '-' + vr.substr(6,4);	  
	if ((tam > 6 && tam < 11) && (tecla == 8 || tecla == 46))
		objeto.value = '(' + vr.substr(0,2) +  ')' + vr.substr(2,3) + '-' + vr.substr(5,4);
	if ((tam > 3 && tam < 7) && (tecla == 8 || tecla == 46))
		objeto.value = '(' + vr.substr(0,2) +  ')' + vr.substr(2,4);
	if ((tam > 1 && tam < 3) && (tecla == 8 || tecla == 46))
		objeto.value = '(' + vr;
	if (tam == 1 && (tecla == 8 || tecla == 46))
		objeto.value = vr;
}

function Valida_Url(objeto) {
	if (objeto.value.substr(0,7).toLowerCase() == "http://")
		objeto.value = objeto.value.substr(7).toLowerCase();
	return true;
}

function enumero(field)
{	
 var valid = "0123456789"
 var ok = "yes";
 var temp;

 var msg = "";

 for (var i=0; i<field.value.length; i++)
 {
 	temp = "" + field.value.substring(i, i+1);
	if (valid.indexOf(temp) == "-1") ok = "no";
	
	msg += i + " - Temp: " + temp + " Ok: " + ok + "\n"
 }
 //alert(msg);
 if (ok == "no")
 	{
	alert("Neste campo só pode ter número inteiro!");
	field.value = "";
	field.focus();
    return false;
	}
 else
 	return true;
}

//*******************************************************************
// Valida Campo de CNPJ
//Passa o objeto Text
function Valida_CNPJ(objName)
{	var datefield = objName;
	if (datefield.value == "")
		return true;

	if (CalculaCNPJ(objName.value) == false) 
	{	datefield.select();
		alert("CNPJ inválido!!!");
		datefield.value = ""
		datefield.focus();
		return false;
	}
	else
	{	
		return true;
   	}
}

function CalculaCNPJ(RecebeCNPJ)
{
var soma
var resultado1
var resultado2

s="" 

for (x=0; x < RecebeCNPJ.length; x++)
 	if (RecebeCNPJ.charCodeAt(x) >= 48 && RecebeCNPJ.charCodeAt(x)<=57)
		s= s + RecebeCNPJ.charAt(x)

RecebeCNPJ = s

if (RecebeCNPJ.length != 14 || RecebeCNPJ == "00000000000000")
	{
	return false
	}
else
	{
	soma = RecebeCNPJ.charAt(0) * 5 + RecebeCNPJ.charAt(1) * 4 + RecebeCNPJ.charAt(2) * 3 + RecebeCNPJ.charAt(3) * 2 + RecebeCNPJ.charAt(4) * 9 + RecebeCNPJ.charAt(5) * 8 + RecebeCNPJ.charAt(6) * 7 + RecebeCNPJ.charAt(7) * 6 + RecebeCNPJ.charAt(8) * 5 + RecebeCNPJ.charAt(9) * 4 + RecebeCNPJ.charAt(10) * 3 + RecebeCNPJ.charAt(11) * 2

	soma = soma -(11 * (parseInt(soma / 11)))

	if (soma == 0 || soma == 1)
		resultado1 = 0
	else
		resultado1 = 11 - soma

	if (resultado1 == RecebeCNPJ.charAt(12))
		{
		soma = RecebeCNPJ.charAt(0) * 6 + RecebeCNPJ.charAt(1) * 5 + RecebeCNPJ.charAt(2) * 4 + RecebeCNPJ.charAt(3) * 3 + RecebeCNPJ.charAt(4) * 2 + RecebeCNPJ.charAt(5) * 9 + RecebeCNPJ.charAt(6) * 8 + RecebeCNPJ.charAt(7) * 7 + RecebeCNPJ.charAt(8) * 6 + RecebeCNPJ.charAt(9) * 5 + RecebeCNPJ.charAt(10) * 4 + RecebeCNPJ.charAt(11) * 3 + RecebeCNPJ.charAt(12) * 2

		soma = soma - (11 * (parseInt(soma / 11)))
		
		if (soma == 0 || soma == 1)
			resultado2 = 0
		else
			resultado2 = 11 - soma

		if (resultado2 == RecebeCNPJ.charAt(13))
			return true
		else
			return false
		}
	else
		return false
	}
}

function Formata_Cnpj(objeto,teclapres) {
	var tecla = teclapres.keyCode;
	vr = objeto.value;	
	s="" 
	for (x=0; x < vr.length; x++)
	 	if (vr.charCodeAt(x) >= 48 && vr.charCodeAt(x)<=57)
			s= s + vr.charAt(x)
	vr = s
	tam = vr.length + 1;
	if ( tecla != 9 && tecla != 8 && tecla != 46 && tecla != 37 && tecla != 39 && tecla != 35 && tecla != 36) {
	  if (tam < 3)
		objeto.value = vr.substr(0,2);
	  if (tam > 2 && tam < 6)
		objeto.value = vr.substr(0,2) +  '.' + vr.substr(2,3);
	  if (tam > 5 && tam < 9)
		objeto.value = vr.substr(0,2) +  '.' + vr.substr(2,3) + '.' + vr.substr(5,3);
	  if (tam > 8 && tam < 13)
		objeto.value = vr.substr(0,2) +  '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,4);
	  if (tam > 12)
		objeto.value = vr.substr(0,2) +  '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,4) + '-' + vr.substr(12,2);
	}
	if (tam > 12 && (tecla == 8 || tecla == 46))
		objeto.value = vr.substr(0,2) +  '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,4) + '-' + vr.substr(12,2);
	if ((tam > 8 && tam < 13) && (tecla == 8 || tecla == 46))
		objeto.value = vr.substr(0,2) +  '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,4);
	if ((tam > 5 && tam < 9) && (tecla == 8 || tecla == 46))
		objeto.value = vr.substr(0,2) +  '.' + vr.substr(2,3) + '.' + vr.substr(5,3);
	if ((tam > 2 && tam < 6) && (tecla == 8 || tecla == 46))
		objeto.value = vr.substr(0,2) +  '.' + vr.substr(2,3)
	if ((tam > 1 && tam < 3) && (tecla == 8 || tecla == 46))
		objeto.value = vr.substr(0,2)
}

function Valida_CPF(objName)
{	var datefield = objName;
	if (datefield.value == "")
		return true;

	if (checaCPF(objName.value) == false) 
	{	datefield.select();
		alert("CPF inválido!!!");
		datefield.value = ""
		datefield.focus();
		return false;
	}
	else
	{	//alert("Data Válida");
		return true;
   	}
}

function checaCPF (CPF) {
	s="" 
	for (x=0; x < CPF.length; x++)
	 	if (CPF.charCodeAt(x) >= 48 && CPF.charCodeAt(x)<=57)
			s= s + CPF.charAt(x)
	CPF = s

	if (CPF.length != 11 || CPF == "00000000000" || CPF == "11111111111" ||
		CPF == "22222222222" ||	CPF == "33333333333" || CPF == "44444444444" ||
		CPF == "55555555555" || CPF == "66666666666" || CPF == "77777777777" ||
		CPF == "88888888888" || CPF == "99999999999")
		return false;
	soma = 0;
	for (i=0; i < 9; i ++)
		soma += parseInt(CPF.charAt(i)) * (10 - i);
	resto = 11 - (soma % 11);
	if (resto == 10 || resto == 11)
		resto = 0;
	if (resto != parseInt(CPF.charAt(9)))
		return false;
	soma = 0;
	for (i = 0; i < 10; i ++)
		soma += parseInt(CPF.charAt(i)) * (11 - i);
	resto = 11 - (soma % 11);
	if (resto == 10 || resto == 11)
		resto = 0;
	if (resto != parseInt(CPF.charAt(10)))
		return false;
	return true;
}

function Formata_Cpf(objeto,teclapres) {
	var tecla = teclapres.keyCode;
	vr = objeto.value;	
	s="" 
	for (x=0; x < vr.length; x++)
	 	if (vr.charCodeAt(x) >= 48 && vr.charCodeAt(x)<=57)
			s= s + vr.charAt(x)
	vr = s
	tam = vr.length + 1;
	if ( tecla != 9 && tecla != 8 && tecla != 46 && tecla != 37 && tecla != 39 && tecla != 35 && tecla != 36) {
	  if ( tam < 10 )
		objeto.value = vr.substr(0,9);
	  if ( tam > 9 )
		objeto.value = vr.substr(0,9) +  '-' + vr.substr(9,2);
	}
	if (tam >= 11 && (tecla == 8 || tecla == 46)) 
		objeto.value = vr.substr(0,9) +  '-' + vr.substr(9,2);  
    if ( tam <= 10 && (tecla == 8 || tecla == 46))
		objeto.value = vr.substr(0,tam)
}

function Formata_DataHora(objeto,teclapres) {
	var tecla = teclapres.keyCode;
	vr = objeto.value;
	s="" 
	for (x=0; x < vr.length; x++)
	 	if (vr.charCodeAt(x) >= 48 && vr.charCodeAt(x)<=57)
			s= s + vr.charAt(x)
	vr = s
	tam = vr.length + 1;
	if ( tecla != 9 && tecla != 8 && tecla != 46 && tecla != 37 && tecla != 39 && tecla != 35 && tecla != 36){
		if ( tam < 2 )
			objeto.value = vr.substr(0,2)
		if ( tam > 2 && tam < 4 )
			objeto.value = vr.substr(0,2) + '/' + vr.substr(2,2);
		if ( tam > 4 && tam < 8)
			objeto.value = vr.substr(0,2) + '/' + vr.substr(2,2) + '/' + vr.substr(4,4); 
		if ( tam > 8 && tam < 10)
			objeto.value = vr.substr(0,2) + '/' + vr.substr(2,2) + '/' + vr.substr(4,4) + ' ' + vr.substr(8,2); 
		if ( tam > 10 )
			objeto.value = vr.substr(0,2) + '/' + vr.substr(2,2) + '/' + vr.substr(4,4) + ' ' + vr.substr(8,2) + ':' + vr.substr(10,2); 
	}
	if (tam > 10 && (tecla == 8 || tecla == 46)) 
			objeto.value = vr.substr(0,2) + '/' + vr.substr(2,2) + '/' + vr.substr(4,4) + ' ' + vr.substr(8,2) + ':' + vr.substr(10,2); 
	if ((tam > 8 && tam < 11) && (tecla == 8 || tecla == 46)) 
			objeto.value = vr.substr(0,2) + '/' + vr.substr(2,2) + '/' + vr.substr(4,4) + ' ' + vr.substr(8,2); 
	if ((tam > 4 && tam < 9) && (tecla == 8 || tecla == 46)) 
			objeto.value = vr.substr(0,2) + '/' + vr.substr(2,2) + '/' + vr.substr(4,4); 
	if ((tam > 2 && tam < 5) && (tecla == 8 || tecla == 46)) 
			objeto.value = vr.substr(0,2) + '/' + vr.substr(2,2);
	if (tam < 3 && (tecla == 8 || tecla == 46)) 
			objeto.value = vr.substr(0,2)
}

function Valida_DataHora(objName) {
	var ValorOriginal = objName.value;
	objName.value = ValorOriginal.substring(0,10);
	if (checkdate(objName)) {
		objName.value = ValorOriginal.substring(11,16);
		if (Valida_Hora(objName)) {
			objName.value = ValorOriginal;
		}
	}
}

function Valida_Hora(campo) 
{	
    if (campo.value.length == 0)
	{	return true;
	}
	else if ((campo.value.length >= 1) && (campo.value.length < 5))
	{	campo.select();
		alert("Hora Inválida!");
		campo.value = "";
		campo.focus();
		return false;
	}
	else if (campo.value.length = 5)
	{	if (isNaN(campo.value.substring(0,2)) || (campo.value.substring(0,2) > 23) || (campo.value.substring(0,2) < 0 ))
		{	campo.select();
			alert("Hora Inválida!");
			campo.value = "";
			campo.focus();
			return false;
		}
		else if (isNaN(campo.value.substring(3,5)) || (campo.value.substring(3,5) > 59) || (campo.value.substring(3,5) < 0 ))
		{	campo.select();
			alert("Hora Inválida!");
			campo.value = "";
			campo.focus();
			return false;
		}
		else if (campo.value.substring(2,3) != ":")
		{	campo.select();
			alert("Hora Inválida!");
			campo.value = "";
			campo.focus();
			return false;
		}
		else
		{	return true;
		}
	}
}

function Formata_Cep(objeto,teclapres) {
	var tecla = teclapres.keyCode;
	vr = objeto.value;	
	s="" 
	for (x=0; x < vr.length; x++)
	 	if (vr.charCodeAt(x) >= 48 && vr.charCodeAt(x)<=57)
			s= s + vr.charAt(x)
	vr = s
	tam = vr.length + 1;
	if ( tecla != 9 && tecla != 8 && tecla != 46 && tecla != 37 && tecla != 39 && tecla != 35 && tecla != 36) {
	  if ( tam < 2 )
		objeto.value = vr.substr(0,2);
	  if (tam > 2 && tam < 5)
		objeto.value = vr.substr(0,2) +  '.' + vr.substr(2,3);
	  if (tam > 5)
		objeto.value = vr.substr(0,2) +  '.' + vr.substr(2,3) + '-' + vr.substr(5,3);
	}
	if ((tam > 5 && tam < 9) && (tecla == 8 || tecla == 46))
		objeto.value = vr.substr(0,2) +  '.' + vr.substr(2,3) + '-' + vr.substr(5,3);
	if ((tam > 2 && tam < 6) && (tecla == 8 || tecla == 46))
		objeto.value = vr.substr(0,2) +  '.' + vr.substr(2,3)
	if ((tam > 1 && tam < 3) && (tecla == 8 || tecla == 46))
		objeto.value = vr.substr(0,2)
}

