//**** Função que permite contar a quantidade de caracteres digitada em um textarea ****
function Contar(Campo)
{
    if((140-Campo.value.length) <= 0)
    {
        Campo.value = Campo.value.substr(0,140);
    }
    //document.getElementById("Qtd").innerHTML = 200-Campo.value.length + " caracteres restantes"
    document.getElementById("Qtd").innerHTML = 140-Campo.value.length
}


function ContarCaracter(Campo)
{
    if((200-Campo.value.length) <= 0)
    {
        Campo.value = Campo.value.substr(0,200);
    }
    document.getElementById("Qtd").innerHTML = 200-Campo.value.length + " caracteres restantes"
}




//**** Função que permite apenas a digitação de números em um campo ****
function somenteNumero(e){
    var tecla=(window.event)?event.keyCode:e.which;
    
	if (tecla >95 && tecla<106)
		return true;
		    
    if((tecla > 47 && tecla < 58)) return true;
    else{
    if ((tecla != 8)&& (tecla != 16) && (tecla != 37) && (tecla != 39) && (tecla != 46) && (tecla != 35) && (tecla != 36)) return false;
    else return true;
    }
}



//**** Função para determinar o maxLength em campos textarea ****
function maxLength(textAreaField, limit) {
	var ta = document.getElementById(textAreaField);
	
	if (ta.value.length >= limit) {
		ta.value = ta.value.substring(0, limit-1);
	}
}


//**** Função para exibir comentários
function showComents(id)
{
    document.getElementById('comentarios'+id).style.display='';
}


//**** Função para exibir formulário de comentário
function showFormComent(id)
{
    document.getElementById('formComentario'+id).style.display='';
}



//**** Função para abrir janela popup
function openLink(mypage, myname, w, h, scroll)
	{
		pos_left = (screen.width - w) / 2
		pos_top = (screen.height - h) / 2
		winprops = 'height='+h+',width='+w+',scrollbars='+scroll+',left='+ pos_left +',top='+ pos_top +''
		win = window.open(mypage, myname, winprops)
		if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
	}
	
	
	
//**** Função para abrir a janela de visualização da imagem
function popAmplia(img)
{
    window.open('pop_ampliar.asp?i='+img,'','width=1,height=1,status=no,left=20,top=20');
}



function recuperarRequest(){
	var oXMLHTTP;
	
	try {
		oXMLHTTP = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			oXMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {
			oXMLHTTP = false;
		}
	}
	
	if (!oXMLHTTP && typeof XMLHttpRequest != 'undefined') {
		oXMLHTTP = new XMLHttpRequest();
	}
	
	return oXMLHTTP;
}


//**** Função para limpar o texto de um textbox
function limparTexto(obj){
	if(obj!=null)
		if((obj.value =='Separe por vírgula'))
			obj.value ='';
}


//**** Função marcarar campo data
function mascaraData(campoData)
{              
	var data = campoData.value;              
	if (data.length == 2)
	{                  
	    data = data + '/';                  
	    document.forms[0].nascimento.value = data;      
	    return true;                            
	}              
	if (data.length == 5)
	{                  
	    data = data + '/';                  
	    document.forms[0].nascimento.value = data;                  
	    return true;              
	}         
}



function showForm()
{
    if(document.getElementById('form_promo').style.display=='')
        document.getElementById('form_promo').style.display='none';
    else
        document.getElementById('form_promo').style.display='';
}



//***************************************************************
//**** Função para validar formulário
//***************************************************************
function validar(){
	//validar se o campo nome foi preenchido corretamente
	if(document.getElementById('txtNome').value =='')
	{
	    alert('Preencha corretamente o campo "Nome".');
	    document.getElementById('txtNome').focus();
	    return false;
	}
	
	//validar se o campo e-mail foi preenchido corretamente
	if( document.getElementById('txtEmail').value=="" || document.getElementById('txtEmail').value.indexOf('@')==-1 || document.getElementById('txtEmail').value.indexOf('.')==-1 )
    {
        alert('Preencha corretamente o campo "E-mail".');
        document.getElementById('txtEmail').focus();
        return false;
    }
    
    //validar se está checkado o sexo
	if((!document.form1.rdoFeminino.checked)&&(!document.form1.rdoMasculino.checked))
	{
        alert("Selecione uma opção no campo Sexo."); 
        return false;
    }
    
    //validar se o campo nascimento foi preenchido corretamente
	if(document.getElementById('txtNascimento').value =='')
	{
	    alert('Preencha corretamente o campo "Nascimento".');
		document.getElementById('txtNascimento').focus();
		return false;
	}
		
	//validar se a data de nascimento digitada é uma data válida
	else
	erro=0;
	var msgErro = 'Data de nascimento inválida.';
	hoje = new Date();
	barras = document.getElementById('txtNascimento').value.split("/");
	if (barras.length == 3){
		dia = barras[0];
		mes = barras[1];
		ano = barras[2];
		resultado = (!isNaN(dia) && (dia > 0) && (dia < 32)) && (!isNaN(mes) && (mes > 0) && (mes < 13)) && (!isNaN(ano) && (ano.length == 4) && (ano >= 1900));
		if (!resultado) {
			alert(msgErro);
			document.getElementById('txtNascimento').focus();
			return false;
		} else{
			if((mes==4 || mes==6 || mes==9 || mes==11) && dia > 30) {
				alert(msgErro);
				return false;
			} else {
				if(ano%4!=0 && mes==2 && dia>28) {
					alert(msgErro);
					return false;
				} else{
					if(ano%4==0 && mes==2 && dia>29){
						alert(msgErro);
						return false;
					}
				}
			}
		}
	} else {
		alert(msgErro);
		document.getElementById('txtNascimento').focus();
	return false;
	}
	
	
	//validar se o campo endereco foi preenchido corretamente
	if(document.getElementById('txtEndereco').value =='')
	{
	    alert('Preencha corretamente o campo "Endereço".');
	    document.getElementById('txtEndereco').focus();
	    return false;
	}
	
	//validar se o campo numero foi preenchido corretamente
	if(document.getElementById('txtNumero').value =='')
	{
	    alert('Preencha corretamente o campo "Número".');
	    document.getElementById('txtNumero').focus();
	    return false;
	}
	
	//validar se o campo bairro foi preenchido corretamente
	if(document.getElementById('txtBairro').value =='')
	{
	    alert('Preencha corretamente o campo "Bairro".');
	    document.getElementById('txtBairro').focus();
	    return false;
	}
	
	//validar se o campo cidade foi preenchido corretamente
	if(document.getElementById('txtCidade').value =='')
	{
	    alert('Preencha corretamente o campo "Cidade".');
	    document.getElementById('txtCidade').focus();
	    return false;
	}
	
	//validar se o campo cep foi preenchido corretamente
	if(document.getElementById('txtCEP').value =='')
	{
	    alert('Preencha corretamente o campo "CEP".');
	    document.getElementById('txtCEP').focus();
	    return false;
	}
	
	//validar se o campo sinopse foi preenchido corretamente
	if(document.getElementById('txtResposta').value =='')
	{
	    alert('Responda à pergunta para participar da promoção.');
	    document.getElementById('txtResposta').focus();
	    return false;
	}		
	
	
	//validar se está checkado termos de uso e politica de privacidade
	if(!document.form1.rdoAceito.checked)
	{
        alert("Você precisa ler e aceitar os Termos de Uso, Política de Privacidade e Regulamento da Promoção."); 
        return false;
    }
    
//    //validar se o campo CPF foi preenchido corretamente
//	if(document.getElementById('txtCPF').value =='')
//	{
//	    alert('Preencha corretamente o campo "CPF".');
//	    document.getElementById('txtCPF').focus();
//	    return false;
//	}
//	else
//	{
//	    var txtCPF = document.getElementById('txtCPF').value;
//        txtCPF= txtCPF.replace(".","");
//        txtCPF= txtCPF.replace(".","");
//        txtCPF= txtCPF.replace("-","");
//        
//	    x = 0;
//        soma = 0;
//        dig1 = 0;
//        dig2 = 0;
//        texto = "";
//        txtCPF1="";
//        len = txtCPF.length; x = len -1;
//        
//        for (var i=0; i <= len - 3; i++) {
//            y = txtCPF.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 ;
//        txtCPF1 = txtCPF.substring(0,len - 2) + dig1 ;
//        x = 11; soma=0;
//        for (var i=0; i <= len - 2; i++) {
//            soma = soma + (txtCPF1.substring(i,i+1) * x);
//            x = x - 1;
//        }
//        dig2= 11 - (soma % 11);
//        if (dig2 == 10) dig2=0;
//        if (dig2 == 11) dig2=0;
//        //alert ("Digito Verificador : " + dig1 + "" + dig2);
//        if ((dig1 + "" + dig2) == txtCPF.substring(len,len-2)) {
//            return true;
//        }
//        
//        document.getElementById('txtCPF').focus();
//        alert ("Número de CPF inválido!");
//        return false;
//	}
//    
    
    return true;
    
}