// AJAX USANDO GET

function criaObjeto(){
    var obj;
	try{		
		// XmlHttpRequest para Firefox,	Opera, Safari e derivados.		
		obj = new XMLHttpRequest();	
	}	catch (e){		
		// XmlHttpRequest para Internet Explorer.		
		try{			
		// Internet Explorer 6.0+
			obj = new ActiveXObject("Msxml2.XMLHTTP");
		}catch (e){
			// Internet Explorer.			
			obj = new ActiveXObject("Microsoft.XMLHTTP");		
		}	
	}	
	return obj;
}

var http = criaObjeto();
var nomeDiv;

function enviaRequisicao(campo,acao,mensagem){
	if (mensagem != undefined){
		document.getElementById(campo).innerHTML = mensagem;
	}
	nomeDiv = campo;
    http.open('get',acao);
    http.onreadystatechange = processaResposta;
    http.send(null);
}

function processaResposta(){
    if(http.readyState == 4){
        if(http.status == 200){
            var resposta = http.responseText;
            document.getElementById(nomeDiv).innerHTML = resposta;
            if (resposta.indexOf('cadastrouEmail')>0){
            	document.getElementById(nomeDiv).innerHTML = "<fieldset><legend>Newsletter</legend><input type=\"text\" id=\"nome\" value=\"Nome\" onfocus=\"if(this.value == 'Nome') this.value = ''\" onblur=\"if(this.value == '') this.value = 'Nome'\" /><input type=\"text\" id=\"email\" value=\"E-mail\" onfocus=\"if(this.value == 'E-mail') this.value = ''\" onblur=\"if(this.value == '') this.value = 'E-mail'\" /><input type=\"button\" value=\"\" class=\"btn-ok\"  onclick=\"validaNewsletter()\"/></fieldset>";
            	alert("Obrigado por se cadastrar em nossa newsletter.");
            }            
            if (resposta.indexOf('erroCadastroEmail')>0){
            	document.getElementById(nomeDiv).innerHTML = "<fieldset><legend>Newsletter</legend><input type=\"text\" id=\"nome\" value=\"Nome\" onfocus=\"if(this.value == 'Nome') this.value = ''\" onblur=\"if(this.value == '') this.value = 'Nome'\" /><input type=\"text\" id=\"email\" value=\"E-mail\" onfocus=\"if(this.value == 'E-mail') this.value = ''\" onblur=\"if(this.value == '') this.value = 'E-mail'\" /><input type=\"button\" value=\"\" class=\"btn-ok\"  onclick=\"validaNewsletter()\"/></fieldset>";            	
            	alert("E-mail inválido ou já cadastrado.Tente novamente.");
            } 
            if (resposta.indexOf('chamaGaleria') >0){
            	$('#photos').galleryView({
            			panel_width: 536,
            			panel_height: 401,
            			frame_width: 67,
            			frame_height: 67
                  });
            }
            
            if (resposta.indexOf('carregando_ajax') >0){
            	
					// Fancybox - Start
					$('.lista-gal-fotos a , #lista-galeria .foto-capa, #lista-galeria h3 a, #lista-galeria .link-fotos,#not-int a').fancybox({
						'overlayColor': '#032305',
						'titlePosition':'over'
					});
					$('.link-videos,.title-video,.foto-capa-video').fancybox({

						'autoScale': false,
						'type' : 'iframe' 
					});
	
					// Fancybox - End
					
					// Compartilhar - Start
					var share = $('.share-content');
					share.hide();
					$('.link-share').click(function(){
						$(this).siblings(share).fadeIn(400);
							return false;
						});
						$('.close-share').click(function(){
							$(this).parent().fadeOut(400);
						});
					// Compartilhar - End.
					
						
            }
            
        }else{
            document.getElementById(nomeDiv).innerHTML = "Erro.";
        }
    }
}

function enviaRequisicao2(campo,acao,mensagem){
	http2 = criaObjeto();
	if (mensagem != undefined){
		document.getElementById(campo).innerHTML = mensagem;
	}
	nomeDiv2 = campo;
    http2.open('get',acao);
    http2.onreadystatechange = processaResposta2;
    http2.send(null);
}

function processaResposta2(){
    if(http2.readyState == 4){
        if(http2.status == 200){
            var resposta = http2.responseText;
            if (resposta.indexOf('carregouCategoria')>0){
            	clearTimeout(ID);
            }
            document.getElementById(nomeDiv2).innerHTML = resposta;
        }else{
            document.getElementById(nomeDiv2).innerHTML = "PÃ¡gina não encontrada.";
        }
    }
}

function enviaRequisicao3(campo,acao,mensagem){
	http3 = criaObjeto();
	if (mensagem != undefined){
		document.getElementById(campo).innerHTML = mensagem;
	}
	nomeDiv3 = campo;
    http3.open('get',acao);
    http3.onreadystatechange = processaResposta3;
    http3.send(null);
}

function processaResposta3(){
    if(http3.readyState == 4){
        if(http3.status == 200){
            var resposta = http3.responseText;
            document.getElementById(nomeDiv3).innerHTML = resposta;
            if (resposta.indexOf('existeErro')>0){
				inicio = resposta.indexOf('*')+1;
				fim = resposta.indexOf('**');
            	texto = resposta.substring(inicio,fim);            	
            	alert(texto);
            }  
            if (resposta.indexOf('cadastrouEmail')>0){
            	alert("Cadastro realizado com sucesso.");
            	document.getElementById('emailNews').value = "";
            }            
            if (resposta.indexOf('erroCadastroEmail')>0){
            	alert("E-mail já existente.");
            	document.getElementById('emailNews').value = "";
            }
            if (resposta.indexOf('exibirRetornoEnvioAmigo')>0){
				inicio = resposta.indexOf('*')+1;
				fim = resposta.indexOf('**');
            	divSelecionado = resposta.substring(inicio,fim);            	
            	chamaRetornoEnvioAmigo(divSelecionado);
            }            
            if (resposta.indexOf('exibirRetornoContato')>0){
           		alert("Contato realizado com sucesso.");
           		paginaInterna('contato.php');
            }  
            if (resposta.indexOf('carregaFonte')>0){
	           Cufon('h2');
            }
            if (resposta.indexOf('exibirFiliese')>0){
               alert("Solicitação enviada com sucesso.")
	           paginaInterna('filiese.php');
            }
        }else{
            document.getElementById(nomeDiv3).innerHTML = "Página não encontrada.";
        }
    }
}

function enviaRequisicao4(campo,acao){
	http4 = criaObjeto();
	if (mensagem != undefined){
		document.getElementById(campo).innerHTML = mensagem;
	}	
	nomeDiv4 = campo;
    http4.open('get',acao);
    http4.onreadystatechange = processaResposta4;
    http4.send(null);
}

function processaResposta4(){
    if(http4.readyState == 4){
        if(http4.status == 200){
            var resposta = http4.responseText;
            document.getElementById(nomeDiv4).innerHTML = resposta;
        }
    }
}


function enviaRequisicao5(campo,acao,mensagem){
	if (mensagem != undefined){
		document.getElementById(campo).innerHTML = mensagem;
	}
	http5 = criaObjeto();
	nomeDiv5 = campo;
    http5.open('get',acao);
    http5.onreadystatechange = processaResposta5;
    http5.send(null);
}

function processaResposta5(){
    if(http5.readyState == 4){
        if(http5.status == 200){
            var resposta = http5.responseText;
            document.getElementById(nomeDiv5).innerHTML = resposta;
            if (resposta.indexOf('cadastrouEmail')>0){
            	alert("Obrigado por se cadastrar em nossa newsletter.");
            	document.getElementById('emailNews').value = "";
            	document.getElementById('meioPaginaHidden').innerHTML = "";
            }            
            if (resposta.indexOf('erroCadastroEmail')>0){
            	alert("E-mail invÃ¡lido.Tente novamente.");
            	document.getElementById('emailNews').value = "";
            	document.getElementById('meioPaginaHidden').innerHTML = "";
            }            
        }else{
            document.getElementById(nomeDiv5).innerHTML = "Erro.";
        }
    }
}

function enviaRequisicao6(campo,acao,mensagem){
	if (mensagem != undefined){
		document.getElementById(campo).innerHTML = mensagem;
	}
	http6 = criaObjeto();
	nomeDiv6 = campo;
    http6.open('get',acao);
    http6.onreadystatechange = processaResposta6;
    http6.send(null);
}

function processaResposta6(){
    if(http6.readyState == 4){
        if(http6.status == 200){
            var resposta = http6.responseText;
            document.getElementById(nomeDiv6).innerHTML = resposta;
        }else{
            document.getElementById(nomeDiv6).innerHTML = "Erro.";
        }
    }
}
//Executa a função objetoXML()
var xmlhttp = criaObjeto(); 


//Função que envia o formulÃ¡rio
function enviarForm(url, campos, destino, mensagem) {
	//Atribui à variÃ¡vel 'elemento' o elemento que irÃ¡ receber a pÃ¡gina postada
	elemento = document.getElementById(destino); 

	//Se o objeto de 'xmlhttp' não estiver true
	if (!xmlhttp) {
		//Insere no 'elemento' o texto atribuído
		elemento.innerHTML = 'Impossível iniciar o objeto XMLHttpRequest.'; 
		return;
	} else { 
		if (mensagem != undefined){
			//Insere no 'elemento' o texto atribuído
			elemento.innerHTML = mensagem; 
		}
	}
	//Abre a pÃ¡gina que receberÃ¡ os campos do formulÃ¡rio
	xmlhttp.open('POST',url,true);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1");
	xmlhttp.send(campos); 	
	xmlhttp.onreadystatechange = processaRespostaPost;
}

function processaRespostaPost() {
	//Se a requisição estiver completada
	if (xmlhttp.readyState == 4 || xmlhttp.readyState == 0) { 
		//Se o status da requisição estiver OK
		if (xmlhttp.status == 200) {
			resposta = xmlhttp.responseText;
            elemento.innerHTML = resposta; 
            if (resposta.indexOf('apagaEnquete')>0){
            	document.getElementById('enquete').innerHTML ='';
            }
			if (resposta.indexOf('existeErro')>0){
				inicio = resposta.indexOf('*')+1;
				fim = resposta.indexOf('**');
				alert(resposta.substring(inicio,fim));
				abrePaginaContato();
            } 
            if (resposta.indexOf('redircionaContato')>0){
				alert('E-mail enviado com sucesso.');
				abrePaginaContato();
            }            
		} else { 
			//Insere no 'elemento' o texto atribuído
			elemento.innerHMTL = 'PÃ¡gina não encontrada!'; 
		}
	}
}



