function abre_pop(url,largura,altura) {
	window.open(url, "pop", "toolbar=0,location=0,directories=0,status=0,menubar=no,scrollbars=no,resizable=no,width="+largura+",height="+altura);
}

function imprimir(){
	window.focus();
	window.print();
}

function isQtdeCaracter(iCampo, iTamanho, iNomeCampo) {
	if (iCampo.value.length > iTamanho){
		alert(iNomeCampo + ' só aceita ' + iTamanho + ' caracteres.');
		return false;
	}
	return true;
}

function isVazio(iCampo,iNomeCampo) {
	if (iCampo.value == false) {
		alert(iNomeCampo + ' é obrigatório.');
		iCampo.focus();
		return false;
	}
	return true;
}

function isChecado(iCampo,iNomeCampo) {
	var checado = false;
	for (i=0;i<iCampo.length;i++){
		if (iCampo[i].checked==true){
			checado = true;
		}
	}
	if (checado != true) {
		alert( 'Selecione o campo ' + iNomeCampo + '.');
		return false;
	} else {
		return true;
	}
}

function isCnpj(iCampo, iNomeCampo) {
	var iCnpj = null;
	iCnpj = iCampo.value;
	invalidChars = " /:,;-..";
	for (i=0; i < invalidChars.length; i++) {
		badChar = invalidChars.charAt(i);
		iCnpj = iCnpj.replace(badChar,"");
	}
	if (EhIgual(iCnpj)==true) { 
		iCampo.focus();
		alert(iNomeCampo + ' é inválido.');
		return false; 
	}
	dac1 = 0; 
	dac2 = 0; 
	dac = ""; 
	dact = ""; 
	sum = 0;
	iCampo1="";
	dac = iCnpj.substring(iCnpj.length-2, iCnpj.length);
	iCampo1 = iCnpj.substring(0,iCnpj.length-2);
	for (i=iCampo1.length-1, j=2; i >= 0 ; i--, j++) {	
		j = (j == 10) ? 2 : j; 
		sum += (iCampo1.substring(i, i+1) * j); 
	}
	dac1 = ((sum * 10) % 11); 
	dac1 = (dac1 == 10) ? 0 : dac1; 
	sum = 0;
	for (i=iCampo1.length-1, j=3; i >= 0 ; i--, j++){	
		j = (j == 10) ? 2 : j; 
		sum += (iCampo1.substring(i, i+1) * j); 
	}
	sum  += (dac1 * 2); 
	dac2 = ((sum * 10) % 11); 
	dac2 = (dac2 == 10) ? 0 : dac2; 
	dact = dac1+""+dac2;
	if (dact == dac) {
		iCampo.value = iCnpj;
		return true;
	} else {	
		iCampo.focus();
		alert(iNomeCampo + ' é inválido.');
		return false; 
	}
}

function isCpf(iCampo, iNomeCampo){
	var iCpf = null;
	iCpf = iCampo.value;
	invalidChars = " /:,;-..";
	for (i=0; i < invalidChars.length; i++) {
		badChar = invalidChars.charAt(i);
		iCpf = iCpf.replace(badChar,"");
	}
	if (EhIgual(iCpf)==true) { 
		iCampo.focus();
		alert(iNomeCampo + ' é inválido.');
		return false; 
	}
	x=0; 
	soma=0; 
	dig1=0; 
	dig2=0; 
	texto=""; 
	iCampo1="";
	len = iCpf.length; 
	x = len -1;
	for (var i=0; i <= len - 3; i++) {
		y = iCpf.substring(i,i+1); 
		soma = soma + ( y * x);
		x = x - 1; 
		texto = texto + y;
	}
	dig1 = 11 - (soma % 11);
	if (dig1 == 10) dig1=0 ; 
	if (dig1 == 11) dig1=0 ;
	iCampo1 = iCpf.substring(0,len - 2) + dig1 ;
	x = 11; soma=0;
	for (var i=0; i <= len - 2; i++) {
		soma = soma + (iCampo1.substring(i,i+1) * x); 
		x = x - 1;
	}
	dig2= 11 - (soma % 11);
	if (dig2 == 10) dig2=0; 
	if (dig2 == 11) dig2=0;
	if ((dig1 + "" + dig2) == iCpf.substring(len,len-2)) {
		iCampo.value = iCpf;
		return true;
	}
	iCampo.focus();
	alert(iNomeCampo + ' é inválido.');
	return false; 
}

function isData(iCampo, iNomeCampo){
	var barra = '/';
	var iCampo1 = iCampo.value;
	var pos1 = iCampo1.indexOf(barra);
	var pos2 = iCampo1.indexOf(barra,pos1+1)
	var dia = iCampo1.substring(0,pos1)
	var mes = iCampo1.substring(pos1+1,pos2)
	var ano = iCampo1.substring(pos2+1)
	var now = new Date();
	var MaxAno = now.getYear()+50;

	if (!(isFormatoData(iCampo))) {
		alert(iNomeCampo + ' formato inválido.');
		iCampo.focus();
		return false;
	}

	if ((eval(dia) > 31) || (eval(mes) > 12) || (eval(ano) > MaxAno) || (eval(ano) < 1900))	{
		alert(iNomeCampo + ' é inválido.');
		iCampo.focus();
		return false;
	}
	if((eval(mes) == 1) || (eval(mes) == 3) || (eval(mes) == 5) || (eval(mes) == 7) || (eval(mes) == 8) || (eval(mes) == 10) || (eval(mes) == 12))	{
		if(eval(dia) > 31)	{
			alert(iNomeCampo + ' é inválido.');
			iCampo.focus();
			return false;
		}				
	}
	
	if((eval(mes) == 4) || (eval(mes) == 6) || (eval(mes) == 9) || (eval(mes) == 11))	{
		if(eval(dia) > 30)		{
			alert(iNomeCampo + ' é inválido.');
			iCampo.focus();
			return false;
		}
	}
	
	if(eval(mes) == 2)	{
		var MaxDia = 0
		if(eval(ano) % 4 == 0)		{
			MaxDia = 29
		} else {
			MaxDia = 28
		}
		if(eval(dia) > MaxDia)		{
			alert(iNomeCampo + ' é inválido.');
			iCampo.focus();
			return false;
		}
	}
	return true;
}

function isEmail(iCampo,iNomeCampo) {
	var str = iCampo.value;
	var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
	var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
	if ( (!r1.test(str) && r2.test(str)) ) {
		return true;
	} else {
		iCampo.focus();
		alert(iNomeCampo+" inválido.");
		return false;
	}
	return true;
}

function isFormatoData(iCampo){
	var barra = '/';
	var iCampo1 = iCampo.value;
	var pos1 = iCampo1.indexOf(barra);
	var pos2 = iCampo1.indexOf(barra,pos1+1);
	if (pos1 != 2 || pos2 != 5){
		return false;
	}
	var dia = iCampo1.substring(0,pos1);
	var mes = iCampo1.substring(pos1+1,pos2);
	var ano = iCampo1.substring(pos2+1);
	if (!(isInteiro(dia) && isInteiro(mes) && isInteiro(ano))){
		return false;
	}
	return true;
}

function isIgual(iCampo1, iCampo2, iNomeCampo1, iNomeCampo2) {
	if (iCampo1.value != iCampo2.value){
		iCampo1.focus();
		alert('O campo '+iNomeCampo1+' não é igual ao campo '+iNomeCampo2);
		return false;
	}
	return true;
}

function isInteiro(iCampo){
	var i;
    for (i = 0; i < iCampo.length; i++){   
        var c = iCampo.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    return true;
}


function isNumerico(iCampo, iNomeCampo) {
	var CaractereInvalido = false;
	if (!(isInteiro(iCampo.value))){
		CaractereInvalido = true;
	}
	for (i=0; i < iCampo.value.length; i++) {
		var Caractere = iCampo.value.charAt(i);
		if(Caractere != "." && Caractere != "," && Caractere != "-"){
         	if (isNaN(parseInt(Caractere))) CaractereInvalido = true;
		}
	}
	if (!CaractereInvalido == true) {
		return true;
   	} else {
		iCampo.focus();
		alert(iNomeCampo + ' deve conter apenas números.');
		return false;
	}
}

function isSelecionado(iCampo, iNomeCampo) {
	if (iCampo.selectedIndex==0) {
		iCampo.focus();
		alert(iNomeCampo + ' não foi selecionado.');
		return false;
	} else {
		return true;
	}
}

function isTamanhoDefinido(iCampo, iMinimo, iMaximo, iNomeCampo){
	if (eval(iCampo.value.length) < iMinimo || eval(iCampo.value.length) > iMaximo){
		iCampo.focus();
		alert(iNomeCampo + ' deve ter no mínimo ' + iMinimo + ' e no máximo ' + iMaximo + ' caracteres.');
		return false;
	} else {
		return true;
	}
}

function isValor(iCampo, iNomeCampo){
	var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/; 
 	if (!(objRegExp.test(iCampo.value))) {
		alert(iNomeCampo + ' não é um valor válido.');
		return false;
	} else {
		return true;
	}

}

// Rotinas antigas
function isChecked(opcao){
	if (opcao.checked == true){
		return true;
	}
	return false;
}

function isNumber(numero)
{
   var CaractereInvalido = false;
   for (i=0; i < numero.length; i++){
      var Caractere = numero.charAt(i);
      if(Caractere != "." && Caractere != "," && Caractere != "-"){
         if (isNaN(parseInt(Caractere))) CaractereInvalido = true;
      }
   }

   return !CaractereInvalido;
}

// Verifica se todos os valores são iguais
function EhIgual(campo) { 
	var i = 0
	var j = 1
	var igual = true;
	// Todos os caracteres devem ser diferentes
	while (i < campo.length-1 && igual == true) {
		while (j < campo.length && igual == true) {
			if (campo.charAt(i) != campo.charAt(j)) {
				igual = false;
			}
			else j++;
		}
		i++;
	}
	return igual;
}
/*
Funções incluídas por Thiago França
Módulo Doação
*/
function isNum(caractere)
{
	var strValidos = "0123456789";
	if (strValidos.indexOf(caractere) == -1)
		return false;
	return true;
}
function isDtNumber(caractere)
{
	var strValidos = "0123456789/"
	if (strValidos.indexOf(caractere) == -1)
		return false;
	return true;
}
//--------------------------------------------------------------------------------------------
function ValidaTecla(campo, event)
{
	var BACKSPACE =  8;
	var key;
	var tecla;
	CheckTAB = true;
	if (navigator.appName.indexOf("Netscape") != -1)
		tecla = event.which;
	else
		tecla = event.keyCode;
	key = String.fromCharCode(tecla);
	if (tecla == 13)
		return false;
	if ( tecla == BACKSPACE )
		return true;
	if ( tecla == 0 )
		return true;
	return (isDtNumber(key));
}
function OnlyNumbers()
{
	/*
	** Se for caracter de controle retorna
	*/
	if (event.keyCode < 32)
	{
		event.returnValue = true;
		return;
	}

	/*
	** Verifica se foi digitado um número
	*/
	if ((String.fromCharCode(event.keyCode) < '0') || 
		(String.fromCharCode(event.keyCode) > '9'))
		event.returnValue = (false)
	else
		event.returnValue = (true);
}
function FormataData(campo)
{
	var Conteudo = campo.value;
	if(Conteudo.length == 8 && Conteudo.indexOf('/') < 0)
	{
		campo.value = Conteudo.substring(0,2) + '/' + Conteudo.substring(2,4) + '/' + Conteudo.substring(4,8);
	}
}
function OnlyCurrency()
{
	/*
	** Se for caracter de controle retorna
	*/
	if (event.keyCode < 32)
	{
		event.returnValue = true;
		return;
	}

	/*
	** Verifica se foi digitado um número ou vírgula ou ponto
	*/
	if ((String.fromCharCode(event.keyCode) >= '0') && (String.fromCharCode(event.keyCode) <= '9'))
		event.returnValue = (true)
	else
		if ((String.fromCharCode(event.keyCode) == ',') || (String.fromCharCode(event.keyCode) == '.'))
			event.returnValue = (true)
		else
			event.returnValue = (false);
}
function FormataCNPJ(campo)
{
	//13-Out-2005 Thiago França
	//00,000,000/0000-00
	//01 234 567 8901 23
	//123456789012345678
	var strCNPJ = campo.value;
	var strSaida = "";
	if (strCNPJ.length == 14)
	{
		for(i=0;i<14;i++)
		{
			strSaida += strCNPJ.charAt(i);
			if (i == 1 || i == 4)
			{
				strSaida += ".";
			}
			else if (i == 7)
			{
				strSaida += "/";
			}
			else if (i == 11)
			{
				strSaida += "-";
			}
		}
		campo.value = strSaida;
	}
	else
	{
		campo.value = strCNPJ;
	}
}
			
function FormataCPF(campo)
{
	//13-Out-2005 Thiago França
	//111.111.111-11
	var strCPF = campo.value;
	var strSaida = "";
	if (strCPF.length == 11)
	{
		for(i=0;i<11;i++)
		{
			strSaida += strCPF.charAt(i);
			if (i == 2 || i == 5)
			{
				strSaida += ".";
			}
			else if (i == 8)
			{
				strSaida += "-";
			}
		}
		campo.value = strSaida;
	}
	else
	{
		campo.value = strCPF;
	}
}

function RetornaNumerico(campo)
{
	// 11-Out-2005 Thiago França
	var valor = campo.value;
	var strValor = "";
	for(i=0;i<=valor.length-1;i++)
	{
		strParte = valor.charAt(i);
		if (isNum(strParte) == true)
		{
			strValor = strValor + strParte
		}
	}
	campo.value = strValor;
	campo.select();
	campo.focus();
}

function FormataValor(campo, SepMil, SepDec, e)
{
	var sep = 0;
	var key = '';
	var i = j = 0;
	var len = len2 = 0;
	var strCheck = '0123456789';
	var aux = aux2 = '';
	var whichCode = (window.Event) ? e.which : e.keyCode;
	if (whichCode == 13) return true;  // Enter
	if (whichCode == 8) return true;  // Delete (Bug fixed)
	key = String.fromCharCode(whichCode);  // Get key value from key code
	if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
	len = campo.value.length;
	for(i = 0; i < len; i++)
		if ((campo.value.charAt(i) != '0') && (campo.value.charAt(i) != SepDec)) break;
	aux = '';
	for(; i < len; i++)
		if (strCheck.indexOf(campo.value.charAt(i))!=-1) aux += campo.value.charAt(i);
	aux += key;
	len = aux.length;
	if (len == 0) campo.value = '';
	if (len == 1) campo.value = '0'+ SepDec + '0' + aux;
	if (len == 2) campo.value = '0'+ SepDec + aux;
	if (len > 2) {
	aux2 = '';
	for (j = 0, i = len - 3; i >= 0; i--)
	{
		if (j == 3)
		{
			aux2 += SepMil;
			j = 0;
		}
		aux2 += aux.charAt(i);
		j++;
	}
	campo.value = '';
	len2 = aux2.length;
	for (i = len2 - 1; i >= 0; i--)
		campo.value += aux2.charAt(i);
	campo.value += SepDec + aux.substr(len - 2, len);
	}
	return false;
}

function ValidaData(campo, NomeCampo)
{

	if (campo != "")
	{
		if(campo.charAt(2) != "/" || campo.charAt(5) != "/" || campo.length != 10)
		{
			alert(NomeCampo + ' é inválido.');
			return(false);
		}
		for(i=0;i<10;i++)
		{
			if(i != 2 && i != 5)
			{
				if(campo.charAt(i) != 0 && campo.charAt(i) != 1 && campo.charAt(i) != 2 && campo.charAt(i) != 3 && campo.charAt(i) != 4 && campo.charAt(i) != 5 && campo.charAt(i) != 6 && campo.charAt(i) != 7 && campo.charAt(i) != 8 && campo.charAt(i) != 9 || campo.charAt(i) == " ")
				{
					alert(NomeCampo + ' é inválido.');
					return(false);
				}
		    }
		}
		var Dia = 0;
		var Mes = 0;
		var Ano = 0;
		var Resto = 0;
		Dia = campo.substring(0,2);
		Mes = campo.substring(3,5);
		Ano = campo.substring(6,10);
		if (Ano < 1900)
		{
			alert(NomeCampo + ' é inválido.');
			return(false);
		}
		if ((Mes < 1) || (Mes > 12))
		{
			alert(NomeCampo + ' é inválido.');
			return(false);
		}
		if ((Dia < 1) || (Dia > 31))
		{
			alert(NomeCampo + ' é inválido.');
			return(false);
		}
		if ((Mes == "01") && (Dia <= 31))
		{
			return(true);
		}
		if (Mes == "02")
		{
			Resto = Ano % 4;
			if ((Resto == 0) && (Dia <= 29))
			{
				return(true);
			}
			if ((Resto != 0) && (Dia <= 28))
			{
				return(true);
			}
		}
		if ((Mes == "03") && (Dia <= 31))
		{
			return(true);
		}
		if ((Mes == "04") && (Dia <= 30))
		{
			return(true);
		}
		if ((Mes == "05") && (Dia <= 31))
		{
			return(true);
		}
		if ((Mes == "06") && (Dia <= 30))
		{
			return(true);
		}
		if ((Mes == "07") && (Dia <= 31))
		{
			return(true);
		}
		if ((Mes == "08") && (Dia <= 31))
		{
			return(true);
		}
		if ((Mes == "09") && (Dia <= 30))
		{
			return(true);
		}
		if ((Mes == "10") && (Dia <= 31))
		{
			return(true);
		}
		if ((Mes == "11") && (Dia <= 30))
		{
			return(true);
		}
		if ((Mes == "12") && (Dia <= 31))
		{
			return(true);
		}
		alert(NomeCampo + ' é inválido.');
		return(false);
	}
	else
	{
		alert(NomeCampo + ' é inválido.');
		return(false);
	}
}
