//Função para abrir pop-up
function abrir(pagina,largura,altura, noscroll) {
	if(!noscroll){
		scrollbars = 'no';
	} else {
		scrollbars = 'yes';
	}
	config="toolbar=no,location=no,width="+largura+",height="+altura+",status=no,menubar=no,scrollbars=" + scrollbars + ",resizable=no,top=" + ((screen.height - altura) / 2) + ",left=" + ((screen.width - largura) /2);
	pop=window.open(pagina,"newwindow",config);
}


//Função para que o campo aceite somente números inteiros
function checkIt(evt) {
	evt = (evt) ? evt : window.event
	var charCode = (evt.which) ? evt.which : evt.keyCode
	if (charCode > 31 && (charCode < 48 || charCode > 57)) {
		status = "Este campo aceita somente número."
		return false
	}
	status = ""
	return true
}


//Função para que o campo aceite somente números inteiros e vírgula
function CheckNum(){
	if ((event.keyCode < 48) || (event.keyCode > 57)) {
		if ((event.keyCode != 44) && (event.keyCode != 45)) {
		return false;
		}
	}
return true;
}

//Função para que o campo aceite somente números, letras sem acento e espaço;
function CheckNumChar(){
	if ((event.keyCode < 48) || (event.keyCode > 57)){
		if ((event.keyCode < 97) || (event.keyCode > 122)) {
			if (event.keyCode != 32){
				status = "Este campo aceita somente números e letras minúsculas.";
				return false;
			}
		}
	}
	return true;
}

//Função para que o campo aceite somente números, letras e caracteres de arquivo;

function CheckFilename(){
	if ((event.keyCode < 46) || (event.keyCode > 58)){
		if ((event.keyCode < 97) || (event.keyCode > 122)) {
			if (event.keyCode != 95){
				status = "Este caracter não é válido para nome de arquivos.";
				return false;
			}
		}
	}
	return true;
}


//Função que faz uma máscara para telefone
function formataTel(campo) {
	var valor
	valor = campo.value.replace('(','').replace('-','').replace(')','');
	if(valor.length == 1){
		campo.value = "(" + valor;
	}else if(valor.length > 6){
		campo.value = "(" + valor.substr(0,2) + ")" + valor.substr(2,4) + "-" + valor.substr(6,4) ;
	}else if(valor.length == 6){
		campo.value = "(" + valor.substr(0,2) + ")" + valor.substr(2,4) ;
	}else if(valor.length > 2 ){
		campo.value = "(" + valor.substr(0,2) + ")" + valor.substr(2,valor.length-2);
	}
}
function formataTel7(campo) {
	var valor

	valor = campo.value.replace('(','').replace('-','').replace(')','');
	if(valor.length == 9){
		campo.value = "(" + valor.substr(0,2) + ")" + valor.substr(2,3) + "-" + valor.substr(5,valor.length-5) ;
	}
}


//Função que faz uma máscara para data
function formataData(campo) {
	var valor
	valor = campo.value.replace('/','').replace('/','');
	if(valor.length > 4){
		campo.value = valor.substr(0,2) + "/" + valor.substr(2,2) + "/" + valor.substr(4,4) ;
	}else if(valor.length > 2 ){
		campo.value = valor.substr(0,2) + "/" + valor.substr(2,2);
	}
}
function formataDataMesAno(campo) {
	var valor
	valor = campo.value.replace('/','');
	if(valor.length > 2){
		campo.value = valor.substr(0,2) + "/" + valor.substr(2,4);
	}
}

//Função que faz uma máscara para cep
function formataCEP(campo) {
	var valor
	valor = campo.value.replace('-','');
	 if(valor.length >= 5){
		campo.value = valor.substr(0,5) + "-" + valor.substr(5,3) ;

	}else{
		campo.value = valor;
	}
}


//Validação de data
function validDate(d) {
	var reData = /(\d{2})\/(\d{2})\/(\d{4})/;
	var datePart = d.match(reData); // datePartconterá [0]=dia, [1]=mes e [2]=ano

	if(datePart == null){
	//alert("Data Invalida");
	return false;
	}

	// passando as partes da data para variáveis mais "amigáveis":
	var dd = datePart[1], mm = datePart[2], yy = datePart[3];
	//alert(dd); // 1o. grupo //alert(mm); // 2o. grupo //alert(yy); // 3o. grupo

	//condições de datas inválidas:
	// dia menor que 1 ou maior que 31
	if (dd<1) { return false;}

	// mes fora dos limites
	if (mm<1 || mm>12) { return false;}

	// meses 4,6,9,11 nao possuem mais de 30 dias
	if ((dd > 30) && (mm == 4)) {return false;}
	if ((dd > 30) && (mm == 6)) {return false;}
	if ((dd > 30) && (mm == 9)) {return false;}
	if ((dd > 30) && (mm == 11)) {return false;}

	if ((dd > 31) && (mm == 1)) {return false;}
	if ((dd > 31) && (mm == 3)) {return false;}
	if ((dd > 31) && (mm == 5)) {return false;}
	if ((dd > 31) && (mm == 7)) {return false;}
	if ((dd > 31) && (mm == 8)) {return false;}
	if ((dd > 31) && (mm == 10)) {return false;}
	if ((dd > 31) && (mm == 12)) {return false;}

	// fevereiro não tem mais de 28 dias, exceto em ano bissexto
	if ((dd > 29) && (mm == 2) && (anobissexto(yy) )) {return false;}
	if ((dd > 28) && (mm == 2) && (!anobissexto(yy) )) {return false;}

// se passou pelos testes acima, então a data é considerada válida.
return true;
}

function validDateMesAno(d) {
	mm = d.substring(0,2);
	aaaa = d.substring(3);

	// mes fora dos limites
	if (mm<1 || mm>12) { return true;}

	// mes fora dos limites
	if (aaaa<1900 || aaaa>2100) { return true;}

// se passou pelos testes acima, então a data é considerada válida.
return false;
}

// retorna verdadeiro se for ano bissexto ou falso se não for bissexto
function anobissexto(yy){
	return (yy % 4 == 0 &&(yy % 100 != 0 || yy % 400 == 0));
}

//VERIFICA O CPF, INCLUSIVE NUMEROS IGUAIS
/*function checkCPF(whatForm, whatInput, errorMessage){
  var tmpCPF = whatForm.elements[whatInput];
  var numcpf = tmpCPF.value;*/
function checkCPF(whatInput,errorMessage){
  var tmpCPF = whatInput;
  var numcpf = tmpCPF.value;

  x = 0;
  soma = 0;
  dig1 = 0;
  dig2 = 0;
  texto = "";
  numcpf1 = "";
  len = numcpf.length;
  x = len -1;
  for (var i=0; i <= len - 3; i++) {
    y = numcpf.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 ;
    numcpf1 = numcpf.substring(0,len - 2) + dig1 ;
    x = 11; soma=0;
  for (var i=0; i <= len - 2; i++) {
    soma = soma + (numcpf1.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) == numcpf.substring(len,len-2)) {
    if(numcpf % 11111111111 == 0){
      //showWarning(errorMessage, tmpCPF);
      tmpCPF.focus();
      return false;
    }
    return true;
  }
  //showWarning(errorMessage, tmpCPF);
  tmpCPF.focus();
  return false;
}

function checkCNPJ(whatInput,errorMessage){
  var tmpCNPJ = whatInput;
  CNPJ = tmpCNPJ.value;

  CNPJ = LIMP(CNPJ);
  if(isNUMB(CNPJ) != 1){
   // showWarning(errorMessage, tmpCNPJ);
    tmpCNPJ.focus();
    return false;
  }
  else{
    if(CNPJ == 0){
      //showWarning(errorMessage, tmpCNPJ);
      tmpCNPJ.focus();
      return false;
    }
    else{
      g = CNPJ.length - 2;
      if (RealTestaCNPJ(CNPJ,g) == 1){
        g = CNPJ.length - 1;
        if(RealTestaCNPJ(CNPJ,g) == 1){
          return true;
        }
        else{
          return true;
        }
      }
      else{
		//showWarning(errorMessage, tmpCNPJ);
        tmpCNPJ.focus();
        return false;
      }
    }
  }
}

//DEPENDENCIA PARA O CNPJ
function RealTestaCNPJ(CNPJ,g){
  var VerCNPJ=0;
  var ind=2;
  var tam;
  for(f = g; f > 0; f--){
    VerCNPJ+=parseInt(CNPJ.charAt(f-1))*ind;
    if(ind>8){
      ind=2;
    }
    else{
      ind++;
    }
  }
  VerCNPJ%=11;
  if(VerCNPJ==0 || VerCNPJ==1){
    VerCNPJ=0;
  }
  else{
    VerCNPJ=11-VerCNPJ;
  }
  if(VerCNPJ!=parseInt(CNPJ.charAt(g))){
    return false;
  }
  else{
    return true;
  }
}

//DEPENDENCIA PARA O CNPJ
function LIMP(c){
  while((cx=c.indexOf("-"))!=-1){
    c = c.substring(0,cx)+c.substring(cx+1);
  }
  while((cx=c.indexOf("/"))!=-1){
    c = c.substring(0,cx)+c.substring(cx+1);
  }
  while((cx=c.indexOf(","))!=-1){
    c = c.substring(0,cx)+c.substring(cx+1);
  }
  while((cx=c.indexOf("."))!=-1){
    c = c.substring(0,cx)+c.substring(cx+1);
  }
  while((cx=c.indexOf("("))!=-1){
    c = c.substring(0,cx)+c.substring(cx+1);
  }
  while((cx=c.indexOf(")"))!=-1){
    c = c.substring(0,cx)+c.substring(cx+1);
  }
  while((cx=c.indexOf(" "))!=-1){
    c = c.substring(0,cx)+c.substring(cx+1);
  }
  return(c);
}

//DEPENDENCIA PARA O CNPJ
function isNUMB(c){
  if((cx=c.indexOf(","))!=-1){
    c = c.substring(0,cx)+"."+c.substring(cx+1);
  }
  if((parseFloat(c) / c != 1)){
    if(parseFloat(c) * c == 0){
      return(1);
    }
    else{
      return(0);
    }
  }
  else{
    return(1);
  }
}

//Cria mascara para os campos
function criamascara(_RefObjeto, _Modelo, num){

         var valorAtual = _RefObjeto.value;

         //alert(valorAtual);

         var valorNumerico = '';

         var nIndexModelo = 0;

         var nIndexString = 0;

         var valorFinal = '';

         var adicionarValor = true;

         //num é um parâmetro para saber se a máscara é apenas numérica ou não

         if (num!=undefined && num!=0){num=1;}else{num=0;}

         // limpa a string valor atual para verificar

         // se todos os caracteres são números

         for (i=0;i<_Modelo.length;i++){

             if (_Modelo.substr(i,1) != '#'){

                valorAtual = valorAtual.replace(_Modelo.substr(i,1),'');

              }
         }

         // verifica se todos os caracteres são números

         for (i=0;i<valorAtual.length;i++){

             if(num==0){

                        if (!isNaN(parseFloat(valorAtual.substr(i,1)))){

                                 valorNumerico = valorNumerico + valorAtual.substr(i,1);

                         }

             }

             else{

                        valorNumerico = valorNumerico + valorAtual.substr(i,1);

             }

          }

          // aplica a máscara ao campo informado usando

          // o modelo de máscara informado no script

          for (i=0;i<_Modelo.length;i++){


              if (_Modelo.substr(i,1) == '#'){

                 if (valorNumerico.substr(nIndexModelo,1) != ''){

                    valorFinal = valorFinal + valorNumerico.substr(nIndexModelo,1);

                    nIndexModelo++;nIndexString++;

                 } else {

                    adicionarValor = false;

                 }
              } else {

                if (adicionarValor && valorNumerico.substr(nIndexModelo,1) != ''){

                   valorFinal = valorFinal + _Modelo.substr(nIndexString,1)

                              nIndexString++;
                 }
               }

           }

//alert(valorFinal)

_RefObjeto.value = valorFinal

}

function verifica_campos_faleconosco(f) {

	if (document.form.txt_nome.value == ""){
		alert("Preencha o campo 'Nome'.");
		document.form.txt_nome.focus();
	return false;
	}

	if (document.form.txt_nome.value.length < 3){
		alert("O campo 'Nome' não está preenchido corretamente!");
		document.form.txt_nome.focus();
	return false;
	}
	
	if (document.form.txt_email.value < 1){
		alert("Preencha o campo 'E-Mail'.");
		document.form.txt_email.focus();
	return false;
	}

	var  exp = /^([0-9,a-z,A-Z]+)([.,_]([0-9,a-z,A-Z]+))*[@]([0-9,a-z,A-Z]+)([.,_,-]([0-9,a-z,A-Z]+))*[.]([0-9,a-z,A-Z]){2}([0-9,a-z,A-Z])?$/;
	if (!exp.test(document.form.txt_email.value)) {
		alert("E-mail inválido!");
		document.form.txt_email.focus();
	return false;
	}

	if (document.form.txt_telefone.value == ""){
		alert("Preencha o campo 'Telefone'.");
		document.form.txt_telefone.focus();
	return false;
	}
	
	if (document.form.txt_mensagem.value == ""){
		alert("Preencha o campo 'Mensagem'.");
		document.form.txt_mensagem.focus();
	return false;
	}

	if (document.form.txt_mensagem.value.length < 3){
		alert("O campo 'Mensagem' não está preenchido corretamente!");
		document.form.txt_mensagem.focus();
	return false;
	}	

return true;
}

function verifica_campos_casamento(f) {

	if (document.form.preNomeConvite.value == ""){
		alert("Preencha o campo 'Nome que consta no convite'.");
		document.form.preNomeConvite.focus();
	return false;
	}

	if (document.form.preNomeConvite.value.length < 5){
		alert("O campo 'Nome que consta no convite' não está preenchido corretamente!");
		document.form.preNomeConvite.focus();
	return false;
	}
	
	if (document.form.preTelefone.value == ""){
		alert("Preencha o campo 'Telefone'.");
		document.form.preTelefone.focus();
	return false;
	}	
	if (document.form.preTelefone.value.length < 13){
		alert("O campo 'Telefone' não está preenchido corretamente!");
		document.form.preTelefone.focus();
	return false;
	}	
	if (document.form.preEmail.value < 1){
		alert("Preencha o campo 'E-Mail'.");
		document.form.preEmail.focus();
	return false;
	}

	var  exp = /^([0-9,a-z,A-Z]+)([.,_]([0-9,a-z,A-Z]+))*[@]([0-9,a-z,A-Z]+)([.,_,-]([0-9,a-z,A-Z]+))*[.]([0-9,a-z,A-Z]){2}([0-9,a-z,A-Z])?$/;
	if (!exp.test(document.form.preEmail.value)) {
		alert("E-mail inválido!");
		document.form.preEmail.focus();
	return false;
	}

	if (document.form.preNome.value == ""){
		alert("Preencha o campo 'Nome Completo (confirmante)'.");
		document.form.preNome.focus();
	return false;
	}

	if (document.form.preNome.value.length < 5){
		alert("O campo 'Nome Completo (confirmante)' não está preenchido corretamente!");
		document.form.preNome.focus();
	return false;
	}
	
	if (document.form.preQtdAdultos.value == ""){
		alert("Preencha o campo 'Adultos'.");
		document.form.preQtdAdultos.focus();
	return false;
	}	
	if (document.form.preQtdCriancas.value == ""){
		alert("Preencha o campo 'Crianças'.");
		document.form.preQtdCriancas.focus();
	return false;
	}	
return true;
}

