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 EhIgual(campo) { // Verifica se todos os valores são iguais
	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;
}

function is150c(iCampo,iNomeCampo) {
	if (iCampo.value.length > 150){
		alert(iNomeCampo + ' só aceita 150 caracteres.');
		return false;
	}
	return true;
}

function isVazio(iCampo,iNomeCampo) {
	if (iCampo.value == false) {
		alert(iNomeCampo + ' é obrigatório.');
		iCampo.focus();
		return false;
	}
	return true;
}

function isVazio1(iCampo,iNomeCampo) {
	if (iCampo.value == false) {
		alert(iNomeCampo + ' é obrigatório.');
		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) {
	if (EhIgual(iCampo.value)==true) { 
		iCampo.focus();
		alert(iNomeCampo + ' é inválido.');
		return false; 
	}
	dac1 = 0; 
	dac2 = 0; 
	dac = ""; 
	dact = ""; 
	sum = 0;
	iCampo1="";
	dac = iCampo.value.substring(iCampo.value.length-2, iCampo.value.length);
	iCampo1 = iCampo.value.substring(0,iCampo.value.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) {
		return true;
	} else {	
		iCampo.focus();
		alert(iNomeCampo + ' é inválido.');
		return false; 
	}
}

function isCPF(iCampo, iNomeCampo){
	if (EhIgual(iCampo.value)==true) { 
		iCampo.focus();
		alert(iNomeCampo + ' é inválido.');
		return false; 
	}
	x=0; 
	soma=0; 
	dig1=0; 
	dig2=0; 
	texto=""; 
	iCampo1="";
	len = iCampo.value.length; 
	x = len -1;
	for (var i=0; i <= len - 3; i++) {
		y = iCampo.value.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 = iCampo.value.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) == iCampo.value.substring(len,len-2)) {
		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.');
		return false;
	}

	if ((eval(dia) > 31) || (eval(mes) > 12) || (eval(ano) > MaxAno) || (eval(ano) < 1900))	{
		alert(iNomeCampo + ' é inválido.');
		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.');
			return false;
		}				
	}
	
	if((eval(mes) == 4) || (eval(mes) == 6) || (eval(mes) == 9) || (eval(mes) == 11))	{
		if(eval(dia) > 30)		{
			alert(iNomeCampo + ' é inválido.');
			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.');
			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 isNumero(iCampo, iNomeCampo) {
	var CaractereInvalido = false;
	for (i=0; i < iCampo.length; i++) {
		var Caractere = iCampo.charAt(i);
		if(Caractere != "." && Caractere != "," && Caractere != "-"){
         	if (isNaN(parseInt(Caractere))) CaractereInvalido = true;
		}
	}
	if (!CaractereInvalido == true) {
		return true;
   	} else {
		iCampo.focus();
		alert(iNomeCampo + ' não é um número válido.');
		return false;
	}
}

function isSelecionado(iCampo, iNomeCampo) {
	if (iCampo.selectedIndex==0) {
		iCampo.focus();
		alert(iNomeCampo + ' não foi selecionado.');
		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;
}
