// Author: Luís Fernando V. Viégas

function openAjax() {
var ajax;
try{
    ajax = new XMLHttpRequest();
	}catch(ee){
		try{
			ajax = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				ajax = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(E){
				ajax = false;
			}
		}
	}
return ajax;
}

function fbusca(){
	var pchave=document.getElementById('busca').value;
	if(pchave.length<4 || pchave=="Busca rápida"){
		alert('Você deve digitar no mínimo 3 letras para poder utilizar a busca.');
		return false;
	}else{
		return true;
	}
}

function mailing(){
	var nome=document.getElementById('nome').value;
	var email=document.getElementById('email').value;
	
	var re = /^[^@]+@[^@]+.[a-z]{2,}$/i;

	if(nome!="" && email!="" && nome!="Nome" && email!="E-mail" && email.search(re) > -1){
		var url='ajax/mailing.php?nome='+nome+'&email='+email;
		var ajax=openAjax();
		ajax.open("GET", url, true);
		ajax.send(null);
		ajax.onreadystatechange = function() {
											if (ajax.readyState == 4) {
												if (ajax.status == 200) {
													if(ajax.responseText==1){
														alert('Cadastro já existente');	
													}else{
														alert('Cadastrado com sucesso!');	
													}
														document.getElementById('nome').value='Nome';
														document.getElementById('email').value='E-mail';
												}else{
													alert("Erro:\n" + ajax.statusText);
												}
											 }
											}
	}else{
		alert('Preencha corretamente os campos Nome e Email');	
	}
}

function include(id,arquivo,destino){
	var url='ajax/'+arquivo+'.php?id='+id;
	var ajax=openAjax();
	ajax.open("GET", url, true);
	ajax.send(null);
	ajax.onreadystatechange = function() {
										if (ajax.readyState == 4) {
											if (ajax.status == 200) {
												document.getElementById(destino).innerHTML=ajax.responseText;
											}else{
												alert("Erro:\n" + ajax.statusText);
											}
										 }else{
											document.getElementById(destino).innerHTML='<img src=\"imgs/load.gif\" alt=\"Carregando\" />'; 
										 }
										}
}

function votar(){
	var opcao_id=document.getElementById('opcao_id').value;
	var nome=document.getElementById('enquete_nome').value;
	var email=document.getElementById('enquete_email').value;
	var comentario=document.getElementById('enquete_comentario').value;

	if(comentario!="" && comentario!="Comentário" && nome!="" && nome.toLowerCase()!="nome" && email!="" && email.toLowerCase()!="e-mail"){
		var re = /^[^@]+@[^@]+.[a-z]{2,}$/i;		
		if(email.search(re) != -1){
			var url='enquete.php?act=votar&opcao_id='+opcao_id+'&nome='+escape(nome)+'&email='+escape(email)+'&comentario='+escape(comentario);
			window.open(url, "enquete", "height = 350, width = 370, scrollbars=yes");
		}else{
			alert('E-mail inválido');	
		}
	}else if(comentario!="" && comentario!="Comentário" && (nome=="" || email=="" || nome.toLowerCase()=="nome" || email.toLowerCase()=="e-mail")){
		if(confirm('Seu comentário será ignorado se os campos Nome e Email não forem preenchidos')){
			var url='enquete.php?act=votar&opcao_id='+opcao_id+'&nome='+escape(nome)+'&email='+escape(email);
			window.open(url, "enquete", "height = 350, width = 370, scrollbars=yes");
		}
	}else{
		var url='enquete.php?act=votar&opcao_id='+opcao_id+'&nome='+escape(nome)+'&email='+escape(email);
		window.open(url, "enquete", "height = 350, width = 370, scrollbars=yes");

	}
}

// Pop Up Automática para imagens

PositionX = 100;
PositionY = 100;

defaultWidth  = 500;
defaultHeight = 500;

var AutoClose = true;

if (parseInt(navigator.appVersion.charAt(0))>=4){
var isNN=(navigator.appName=="Netscape")?1:0;
var isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}
var optNN='scrollbars=no,width='+defaultWidth+',height='+defaultHeight+',left='+PositionX+',top='+PositionY;
var optIE='scrollbars=no,width=150,height=100,left='+PositionX+',top='+PositionY;
function amplia(imageURL,legenda){
if (isNN){imgWin=window.open('about:blank','',optNN);}
if (isIE){imgWin=window.open('about:blank','',optIE);}
with (imgWin.document){
writeln('<html><head><title>Carregando...</title><style>body{margin:0px;}</style>');writeln('<sc'+'ript>');
writeln('var isNN,isIE;');writeln('if (parseInt(navigator.appVersion.charAt(0))>=4){');
writeln('isNN=(navigator.appName=="Netscape")?1:0;');writeln('isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}');
writeln('function reSizeToImage(){');writeln('if (isIE){');writeln('window.resizeTo(100,100);');
writeln('width=112-(document.body.clientWidth-document.images[0].width);');
writeln('height=100-(document.body.clientHeight-document.images[0].height);');
writeln('window.resizeTo(width,height);}');writeln('if (isNN){');       
writeln('window.innerWidth=document.images["George"].width;');writeln('window.innerHeight=document.images["George"].height;}}');
writeln('function doTitle(){document.title="'+legenda+'";}');writeln('</sc'+'ript>');
if (!AutoClose) writeln('</head><body bgcolor=FFFFFF scroll="no" onload="reSizeToImage();doTitle();self.focus()">')
else writeln('</head><body bgcolor=FFFFFF scroll="no" onload="reSizeToImage();doTitle();self.focus()" onblur="self.close()">');
writeln('<img name="George" src='+imageURL+' style="display:block"></body></html>');
close();		
}}