// Funções gerais
function toggleMe(a){
    var e=document.getElementById(a);
    if(!e) {
        return true;
    }
    if((e.style.display=="none")||(e.style.display=="")){
        e.style.display="block"
    }
    else{
        e.style.display="none"
    }
    return true;
}

function ValidacaoEmail(value) {
    return (value == "" || value.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) != -1);
}


function TiraValue(){
    if(document.busca.q.value == 'O que você procura'){
        document.busca.q.value="";
    }
}

function ColocaValue(){
    if(document.busca.q.value == ''){
        document.busca.q.value="O que você procura";
    }
}

/*
Funções para chamar AJAX
*****************************************************************************/

function ajaxEnviaNews(idobj, endereco){
    ajaxHTML(idobj, endereco);
}

/*
Funções da ENQUETE
*****************************************************************************/

function toggleMeEnquete(a,b){
    var e=document.getElementById(a);
    var f=document.getElementById(b);
    if((!e)||(!f)) {
        return true;
    }
    if(e.style.display=="none"){
        e.style.display="block"
        f.style.display="none"
    }
    else{
        e.style.display="none"
        f.style.display="block"
    }
    return true;
    document.geElementById("verificaVoto").innerHTML="";
}

function envia_enquete(codenquete){
    form 			= document.forms[1];
    totRespostas 	= document.enquete.radiobutton.length;
    valorChecado	= false;
    for(i=0; i<totRespostas; i++){
        if(document.enquete.elements["radiobutton"][i].checked){
            valorChecado = true;
            break;
        }
    }//for
    if(!valorChecado){
        alert("Por favor, selecione uma das opções de resposta.");
    }
    else{
        cod = acharadio();

        ajaxHTML('opcEnquete', 'ajaxGravaEnquete.php?codresposta=' + cod + '&codenquete=' + codenquete + '&' + Math.ceil(Math.random()*1000));
		
        for(i=0; i<totRespostas; i++){
            document.enquete.elements["radiobutton"][i].checked = false;
        }//for

        ajaxHTMLNoLoading('displayEnqueteResultado', 'ajaxResultadoEnquete.php?codenquete=' + codenquete + '&' + Math.ceil(Math.random()*1000));
    }
}

function envia_enquete_captcha(codenquete){
    totRespostas 	= document.enquete.radiobutton.length;
    var response_captcha = document.getElementById('recaptcha_response_field').value;
    var challenge_captcha = document.getElementById('recaptcha_challenge_field').value;
    cod = acharadio();
    ajaxHTML('opcEnquete', 'ajaxGravaEnquete.php?codresposta=' + cod + '&codenquete=' + codenquete + '&recaptcha_response_field=' + response_captcha + '&recaptcha_challenge_field=' + challenge_captcha + '&' + Math.ceil(Math.random()*1000));
    for(i=0; i<totRespostas; i++){
        document.enquete.elements["radiobutton"][i].checked = false;
    }//for
    if(document.getElementById('enquete-int-box')){
        ajaxHTMLNoLoading('enquete-int-box', 'ajaxResultadoEnquete.php?codenquete=' + codenquete + '&' + Math.ceil(Math.random()*1000));
    }else{
        ajaxHTMLNoLoading('displayEnqueteResultado', 'ajaxResultadoEnquete.php?codenquete=' + codenquete + '&' + Math.ceil(Math.random()*1000));
    }
    if(document.getElementById('contCaptcha')){
        document.getElementById('contCaptcha').style.display='none';
    }
}

function valida_enquete_captcha(codenquete){
    form 			= document.forms[1];
    totRespostas 	= document.enquete.radiobutton.length;
    valorChecado	= false;
    for(i=0; i<totRespostas; i++){
        if(document.enquete.elements["radiobutton"][i].checked){
            valorChecado = true;
            break;
        }
    }//for
    if(!valorChecado){
        alert("Por favor, selecione uma das opções de resposta.");
        return false;
    }
    else{
        document.getElementById('contCaptcha').style.display='block';
    }
}

function valida_enquete_captcha_enquete(codenquete){
    form 			= document.forms[1];
    totRespostas 	= document.enquete.radiobutton.length;
    valorChecado	= false;
    for(i=0; i<totRespostas; i++){
        if(document.enquete.elements["radiobutton"][i].checked){
            valorChecado = true;
            break;
        }
    }//for
    if(!valorChecado){
        alert("Por favor, selecione uma das opções de resposta.");
        return false;
    }
    else{
        document.getElementById('contCaptcha').style.display='block';
        document.getElementById('btn-enviar').style.display='none';
    }
}

function acharadio(){
    for(i=0;i<7;i++){
        if(document.enquete.radiobutton[i]){
            if(document.enquete.radiobutton[i].checked){
                return document.enquete.radiobutton[i].value;
            }
        }
    }
}

/*
Funções do COMENTÁRIO
*****************************************************************************/

function validaFormComentario() {
    var desNome = document.getElementById('desNome');
    var emlEmail = document.getElementById('emlEmail');
    var texComentario = document.getElementById('texComentario');

    var filter=/^[\w-]+(\.[\w-]+)*@(([A-Za-z\d][A-Za-z\d-]{0,61}[A-Za-z\d]\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
	
    if(desNome.value == '') {
        alert('Por favor, preencha corretamente: Nome.');
        desNome.focus();
        return false;
    }
	
    if (!filter.test(emlEmail.value)) {
        alert('Por favor, preencha corretamente: E-mail.');
        emlEmail.focus();
        return false;
    }

    if(texComentario.value == '') {
        alert('Por favor, preencha corretamente: Comentário.');
        texComentario.focus();
        return false;
    }
	
    if( typeof( document.getElementById("recaptcha_response_field") ) != 'undefined' ){
        if(document.getElementById("recaptcha_response_field").value == "")	{
            alert("Por favor digite o código.");
            document.getElementById("recaptcha_response_field").focus();
            return false;
        }
    }

    return true;

}

/*
Funções do ENVIE AMIGO
*****************************************************************************/

function displayEnvieAmigo() { 
    document.getElementById('yestv-exclusivo-conteudo').style.display = 'none';
    document.getElementById('yestv-exclusivo-envie-amigo').style.display = 'block';
}

function validaEnvieAmigo(){
    if(document.frEnvieAmigo.seunome.value==""){
        alert("Por favor, preencha o campo: 'Seu Nome'.");
        document.frEnvieAmigo.seunome.focus();
        return false;
    }
    if(document.frEnvieAmigo.seuemail.value==""){
        alert("Por favor, preencha o campo: 'Seu e-mail'.");
        document.frEnvieAmigo.seuemail.focus();
        return false;
    }
    if(ValidacaoEmail(document.frEnvieAmigo.seuemail.value)==""){
        alert("Por favor, preencha o campo: 'Seu e-mail' corretamente.");
        document.frEnvieAmigo.seuemail.focus();
        return false;
    }
    if(document.frEnvieAmigo.destinatario.value==""){
        alert("Por favor, preencha o campo: 'Destinatário'.");
        document.frEnvieAmigo.destinatario.focus();
        return false;
    }
    if(document.frEnvieAmigo.emaildestinatario.value==""){
        alert("Por favor, preencha o campo: 'E-mail'.");
        document.frEnvieAmigo.emaildestinatario.focus();
        return false;
    }
    if(ValidacaoEmail(document.frEnvieAmigo.emaildestinatario.value)==""){
        alert("Por favor, preencha o campo: 'E-mail' corretamente.");
        document.frEnvieAmigo.emaildestinatario.focus();
        return false;
    }
    var url = 'ajaxEnvieAmigo.php?seunome='+document.frEnvieAmigo.seunome.value+
    '&seuemail='+document.frEnvieAmigo.seuemail.value+
    '&destinatario='+document.frEnvieAmigo.destinatario.value+
    '&emaildestinatario='+document.frEnvieAmigo.emaildestinatario.value+
    '&endereco_pag='+document.frEnvieAmigo.endereco_pag.value;

    ajaxEnviaNews('yestv-exclusivo-envie-amigo', url);
}

function validaEnvieAmigoGaleria(){
    if(document.frEnvieAmigo.seunome.value==""){
        alert("Por favor, preencha o campo: 'Seu Nome'.");
        document.frEnvieAmigo.seunome.focus();
        return false;
    }
    if(document.frEnvieAmigo.seuemail.value==""){
        alert("Por favor, preencha o campo: 'Seu e-mail'.");
        document.frEnvieAmigo.seuemail.focus();
        return false;
    }
    if(ValidacaoEmail(document.frEnvieAmigo.seuemail.value)==""){
        alert("Por favor, preencha o campo: 'Seu e-mail' corretamente.");
        document.frEnvieAmigo.seuemail.focus();
        return false;
    }
    if(document.frEnvieAmigo.destinatario.value==""){
        alert("Por favor, preencha o campo: 'Destinatário'.");
        document.frEnvieAmigo.destinatario.focus();
        return false;
    }
    if(document.frEnvieAmigo.emaildestinatario.value==""){
        alert("Por favor, preencha o campo: 'E-mail'.");
        document.frEnvieAmigo.emaildestinatario.focus();
        return false;
    }
    if(ValidacaoEmail(document.frEnvieAmigo.emaildestinatario.value)==""){
        alert("Por favor, preencha o campo: 'E-mail' corretamente.");
        document.frEnvieAmigo.emaildestinatario.focus();
        return false;
    }
    var url = 'ajaxEnvieAmigoGaleria.php?seunome='+document.frEnvieAmigo.seunome.value+
    '&seuemail='+document.frEnvieAmigo.seuemail.value+
    '&destinatario='+document.frEnvieAmigo.destinatario.value+
    '&emaildestinatario='+document.frEnvieAmigo.emaildestinatario.value+
    '&endereco_pag='+document.frEnvieAmigo.endereco_pag.value;

    ajaxEnviaNews('yestv-exclusivo-envie-amigo', url);
}
function banner() {
    document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="700" height="300">');
    document.write('<param name="movie" value="swf/banner.swf">');
    document.write('<param name="quality" value="high" />');
    document.write('<param name="wmode" value="transparent" />');
    document.write('<embed src="swf/banner.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="700" height="300" wmode="transparent">');
    document.write('</embed>');
    document.write('</object>');
}

function justinbutton() {
    document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="173" height="95">');
    document.write('<param name="movie" value="swf/justinbutton.swf">');
    document.write('<param name="quality" value="high" />');
    document.write('<param name="wmode" value="transparent" />');
    document.write('<embed src="swf/justinbutton.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="173" height="95" wmode="transparent">');
    document.write('</embed>');
    document.write('</object>');
}

//Tenta criar o objeto xmlHTTP
try{
    xmlhttp = new XMLHttpRequest();
}catch(ee){
    try{
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }catch(e){
        try{
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }catch(E){
            xmlhttp = false;
        }
    }
}

//Fila de conexões
fila=[]
ifila=0

//Carrega via XMLHTTP a url recebida e coloca seu valor
//no objeto com o id recebido
function ajaxHTML(id,url){
    //Carregando...
    document.getElementById(id).innerHTML="<span id='msg_ajax'>Enviando... <br /> </span>"
    //Adiciona à fila
    fila[fila.length]=[id,url]
    //Se não há conexões pendentes, executa
    if((ifila+1)==fila.length)ajaxRun()
}

function ajaxHTMLNoLoading(id,url){
    //Carregando...
    document.getElementById(id).innerHTML = "";
    //Adiciona à fila
    fila[fila.length]=[id,url]
    //Se não há conexões pendentes, executa
    if((ifila+1)==fila.length)ajaxRun()
}

function ajaxHTMLCarregando(id,url){
    //Carregando...
    document.getElementById(id).innerHTML = "<span id='msg_ajax'>Carregando... <br /> </span>";
    //Adiciona à fila
    fila[fila.length]=[id,url]
    //Se não há conexões pendentes, executa
    if((ifila+1)==fila.length)ajaxRun()
}

//Executa a próxima conexão da fila
function ajaxRun(){
    //Abre a conexão
    xmlhttp.open("GET",fila[ifila][1],true);
    //Função para tratamento do retorno
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4){
            //Mostra o HTML recebido
            retorno=unescape(xmlhttp.responseText.replace(/\+/g," "))
            document.getElementById(fila[ifila][0]).innerHTML=retorno
            //Roda o próximo
            ifila++
            if(ifila<fila.length)setTimeout("ajaxRun()",20)
        }
    }
    //Executa
    xmlhttp.send(null)
}


