// Função pop-up
function popup(url_add,width,height){
	window.open(url_add,'welcome','width='+ width +',height='+ height +',menubar=no,status=no,location=no,toolbar=no,scrollbars=auto');
}

function fechar(){
    // Função que fecha o pop-up ao clicar no botao fechar
    document.getElementById('popupfloater').style.display = 'none';
}

function abrir(){
    // Aqui definimos o tempo para fechar o pop-up automaticamente
    document.getElementById('popupfloater').style.display = 'block';
    //document.getElementById('popupfloater').style.border = 'solid 1px #ff0000';
//    setTimeout ("fechar()", 15000);
    setTimeout ("fechar()", 16000);
}

//Validação de Consulta na HOME
function ValidaFormBusca()
 {
  if (ConsisteCampoBranco(formBusca.TextoBusca.value) == true)
  {
    alert("Favor digitar um texto para busca.")
    formBusca.TextoBusca.focus();
    return false;
  }
  return true;
 }
 
//Função para validar se o campo foi preenchido 
 function ConsisteCampoBranco(str) {
	str = LimpaEspacoBrancoAll(str);
  if (str==null || str=="")
    return true;
  return false;
}


//Função para limpar espaços em Branco
function LimpaEspacoBrancoAll(str) {
  if (str!=null) {
    while (str.length > 0 &&
      "\n\r\t ".indexOf(str.charAt(str.length - 1)) != -1)
      str = str.substring(0, str.length - 1);
    while (str.length > 0 &&
      "\n\r\t ".indexOf(str.charAt(0)) != -1)
      str = str.substring(1, str.length);
  }
  return str;
}

function validateEmail(email)
        {
            if (typeof(email) != "string")
                return false;
            else if (!email.match(/^[A-Za-z0-9]+([_.-][A-Za-z0-9]+)*@[A-Za-z0-9]+([_.-][A-Za-z0-9]+)*\.[A-Za-z0-9]{2,4}$/))
                return false;
            return true;
        }

function ConsisteEmail(valor) {
	return  validateEmail(valor)
    var reg = /\w{1,}[@]\w{1,}[.]\w{1,}/
    return reg.test(valor);
}

function FranquiaCapavalida(){

  var codEmpresas = 'Start';
  var f = document.form;
 
  for(i=0;i<f.getElementsByTagName('input').length;i++){
    if(f.getElementsByTagName('input')[i].checked){
       codEmpresas += ','+f.getElementsByTagName('input')[i].value;
    }
  }
  
    if (codEmpresas!='Start'){
      window.open('../../form/contato_franquia_minisite_popup.asp?cdRA=1&Empresas='+codEmpresas,'_blank','toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=300,height=700');
      }else{ 
      alert('Você deve informar pelo menos um campo!');
   }
        
}

function FornecedorCapavalida(){

  var codEmpresas = 'Start';
  var f = document.form;
 
  for(i=0;i<f.getElementsByTagName('input').length;i++){
    if(f.getElementsByTagName('input')[i].checked){
       codEmpresas += ','+f.getElementsByTagName('input')[i].value;
    }
  }
  
    if (codEmpresas!='Start'){
      window.open('../../form/contato_fornecedor_minisite_popup.asp?codA=8&cdRA=1&Empresas='+codEmpresas,'_blank','toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=300,height=700');
      }else{ 
      alert('Você deve informar pelo menos um campo!');
   }
        
}

 function ValidaInv()
 {
  if (document.formInv.codInvest_min.value == '' && document.formInv.codInvest_max.value == '') {
    alert("Selecione pelo menos uma Faixa de Investimento.")
    return false;   
   }

  if (document.formInv.codInvest_min.value == '') {
    alert("Selecione Faixa de Investimento mínimo.")
    return false;   
   }

  if (document.formInv.codInvest_min.value != '' && document.formInv.codIvest_max.value != '')
	  if (document.formInv.codInvest_min.value > document.formInv.codInvest_max.value) {
		alert("Faixa de Investimento mínimo deve ser menor que a Faixa de Investimento máximo.")
		return false;
	   }

   return true;
 }

function mudar(divA, divB, valor, codArea, codConteudo, codCanal){
	if(valor==1){
	document.getElementById(divA).style.display="none";
	document.getElementById(divB).style.display="block";
	estrela(1,divB,codArea,codConteudo,divA,codCanal);
	}else{
	document.getElementById(divA).style.display="block";
	document.getElementById(divB).style.display="none";
	}
}
function mudarNota(divB,url,divA){
    //document.getElementById(divB).attachEvent("onclick", function() { window.open(url); }, true);
    //window.open(url);
    //alert("Obrigado pela sua participação!\nVocê atribuiu a nota: "+nota)
    alert("Obrigado pela sua participação!")
    AjaxLink(divA,url);
}

function estrela(numero,divB,codA,codC,divA,codCa){
	var starEmpty = "";
	var starFull = "";
	var aspas = '"';
	var nota = "";
	var sF;
	var sE;

	for(sF=1;sF<=numero;sF++){
	    starFull += "<img src="+ aspas +"../../img/icon_estrela_vermelha.gif"+ aspas +" onclick="+ aspas +"mudarNota('"+divB+"','../../include/votarAtualizar.asp?codA="+codA+"&codC="+codC+"&nota="+numero+"&codCanal="+codCa+"','"+divA+"');"+aspas+" alt="+ aspas +"Nota "+ numero + aspas+" />";
	    //starFull += "<img src="+ aspas +"../../img/icon_estrela_vermelha.gif"+ aspas +" onclick="+ aspas +"mudarNota('"+divB+"','../../include/votarAtualizar.asp?codA="+codA+"&codC="+codC+"&nota="+numero+"&codCanal="+codCa+"','"+divA+"',"+numero+");"+aspas+" alt="+ aspas +"Nota "+ numero + aspas+" />";
	}

	if(numero<5){
		for(sE=numero+1;sE<=5;sE++){
			starEmpty += "<img src="+ aspas +"../../img/icon_estrela_branca.gif"+ aspas +" onmouseover="+ aspas +"estrela("+ sE +",'"+ divB +"',"+codA+","+codC+",'"+divA+"',"+codCa+");"+ aspas +" alt="+ aspas +"Nota "+ numero + aspas+" />";
		}
	}

	//nota = " Dê sua nota. "+ numero;
	nota = " Dê sua nota."
	document.getElementById(divB).innerHTML = starFull + starEmpty + nota;
}

function ReplaceAll(sStr, sDe, sPara)
{
	while (sStr.indexOf(sDe) != -1)
		sStr = sStr.replace(sDe, sPara)
	return sStr;	
}

function ConsiteCGCCPF(BASE){

	BASE = BASE.replace( ".", "" );
	BASE = BASE.replace( ".", "" );
	BASE = BASE.replace( "-", "" );
	BASE = BASE.replace( "/", "" );


	if ((BASE.length < 11) && (BASE.length != 11) && (BASE.length != 14)){
		return false;
	}

	var start_format = " +-0123456789";
	var number_format = "  0123456789";
	var check_char;
	var decimal = false;
	var trailing_blank = false;
	var digits = false;
	var verify = false;

	check_char = start_format.indexOf(BASE.charAt(0))
	if (check_char == 1)
		decimal = true;
	else 
		if (check_char < 1)
			return false;

	for (var i = 1; i < BASE.length; i++){
		check_char = number_format.indexOf(BASE.charAt(i))
		if (check_char < 0)
			return false;
		else if (check_char == 1){
				if (decimal)            // Second decimal.
					return false;
				else
					decimal = true;
			}
		else if (check_char == 0){
				if (decimal || digits)  
					trailing_blank = true;
			}
		else if (trailing_blank)
			return false;
		else
			digits = true;
			//return false;
	}       


	if (BASE.length > 11) {
		if ((BASE == "00000000000000") || (BASE == "11111111111111") || (BASE == "22222222222222") || (BASE == "33333333333333") || (BASE == "44444444444444") || (BASE == "55555555555555") || (BASE == "66666666666666") || (BASE == "77777777777777") || (BASE == "88888888888888") || (BASE == "99999999999999")){
			return false;
		}

		var CGC = BASE.substring(0,12);
		var DAC = BASE.substring(12,14);
		digito = "0";
		var DF1 = 0;
		var DF2 = 0;
		var DF3 = 0;
		var DF4 = 0;
		var DF5 = 0;
		var DF6 = 0;
		var resto1 = 0;
		var resto2 = 0;
		var DIG1 = 0;
		var DIG2 = 0;
		var n1=  BASE.substring(0,1);
		var n2=  BASE.substring(1,2);
		var n3=  BASE.substring(2,3);
		var n4=  BASE.substring(3,4);
		var n5=  BASE.substring(4,5);
		var n6=  BASE.substring(5,6);
		var n7=  BASE.substring(6,7);
		var n8=  BASE.substring(7,8);
		var n9=  BASE.substring(8,9);
		var n10= BASE.substring(9,10);
		var n11= BASE.substring(10,11);
		var n12= BASE.substring(11,12);
		DF1 = eval((5 * n1) + (4 * n2) + (3 * n3) + (2 * n4) + (9 * n5) + (8 * n6) + (7 * n7) + (6 * n8) + (5 * n9) + (4 * n10) + (3 * n11) + (2 * n12));
		DF2 = eval(DF1 / 11);
		DF3 = eval(parseInt(DF2) * 11);
		resto1 = eval(DF1 - DF3);

		if ((resto1 == 0) || (resto1 == 1))	{
			DIG1 = 0}
		else {
			DIG1 = eval(11 - resto1)
		};

		DF4 = eval((6 * n1) + (5 * n2) + (4 * n3) + (3 * n4) + (2 * n5) + (9 * n6) + (8 * n7) + (7 * n8) + (6 * n9) + (5 * n10) + (4 * n11) + (3 * n12) + (2 * DIG1));
		DF5 = eval(DF4 / 11);
		DF6 = eval(parseInt(DF5) * 11);
		resto2 = eval(DF4 - DF6);
		if ((resto2 == 0) || (resto2 == 1))	{
			DIG2 = 0
		}else {
			DIG2 = eval(11 - resto2)
		};

		digito = eval((DIG1*10) + DIG2);

		if  (digito != DAC ) {
			return false
		}else{
			return true;
		}

	}else{		

		if ((BASE == "00000000000") || (BASE == "11111111111") || (BASE == "22222222222") || (BASE == "33333333333") || (BASE == "44444444444") || (BASE == "55555555555") || (BASE == "66666666666") || (BASE == "77777777777") || (BASE == "88888888888") || (BASE == "99999999999")){
			return false;
		}

		var CPF = BASE.substring(0,9);
		var DAC1 = BASE.substring(9,11);
		var controle  = 0;
		var controle1 = 0;
		var digito1 = 0;
		var inicio = 2;
		var fim = 10;
		var soma = 0;		
		for (j=1; j<3;j++){
			soma = 0;
			for (i=inicio; i<=fim;i++){	 
				soma = eval(soma + ((CPF.substring(i-1 - j,i-1-j+1)) * (fim + 1 + j - i)))
			}
			if (j == 2){ soma = eval(soma + (2 * digito1))}
				digito1 = eval((soma * 10)-((parseInt((soma * 10)/11))* 11)
			)
			if (digito1 == 10) {
				digito1 = 0
			}
			if (j == 1){
				controle =  digito1
			}else{
				controle1 =  digito1
			}			
			inicio = 3;
			fim = 11;
		}			
		digito1 = eval((controle*10) + controle1);
		if  (digito1 != DAC1 ) {
			//alert("O CNPJ/CPF não é válido.")
			return false
		}else{
			return true
		}
	}		
}