// Impede clique direito do mouse
//document.oncontextmenu = new Function("return false")
// Esconde os links da barra de status
function hidestatus(){
	window.status='..:: Electronics ::..';
	return true
}
document.onmouseover = hidestatus;
document.onmouseout  = hidestatus;
//funcao para utilizar ajax
function ajaxInit() {
var 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;
			}
		}
	}
	return xmlhttp;
}
pro_codigo = "";
function viewcomments(prjcode, showall) {
	ajax = ajaxInit();
	if (ajax) {
		ajax.onreadystatechange = writecomments;		
		pro_codigo = prjcode;
		url = 'functions.php?mode=comments&prj='+prjcode+'&showall='+showall;
		ajax.open('GET',url,true);
		ajax.send(null);
	}
}
function writecomments() {
	if (ajax.readyState == 1) {
		document.getElementById("prjComments").innerHTML = "<img src=\"images/progresso.gif\" border=\"0\"> Carregando . . .";
	}
	else if (ajax.readyState == 4) {
		if (ajax.status == 200) {
			var x = 0;
			var toeval = ajax.responseText;
			if (toeval == '')
				toeval = "{'comentarios':[{'datahora':'-1','descricao':'N&atilde;o existem coment&aacute;rios neste projeto.'}]}";
			var json = eval("("+toeval+")");
			var str = "";
			if (json.comentarios[0].datahora == -1)
				str = json.comentarios[x].descricao;
			else
				for (x = 0; x <= json.comentarios.length-1; x++) {
					if (str == "")
						str = "		<tr><td><font size=\"1\">" + json.comentarios[x].datahora + "</font><br>" + json.comentarios[x].descricao + "</td></tr>";
					else
						str = str + "\n		<tr><td><font size=\"1\">" + json.comentarios[x].datahora + "</font><br>" + json.comentarios[x].descricao + "</td></tr>";
				}
			if ((str.search("<tr>") > 0) && (json.comentarios.length-1 > 0))
				str = "<table width=\"100%\" border=\"1\" cellpadding=\"2\" cellspacing=\"0\" bordercolor=\"#006699\" bordercolorlight=\"#0099CC\" bordercolordark=\"#003366\" hspace=\"0\" vspace=\"0\">"+str+"</table>";
			document.getElementById("prjComments").innerHTML = str; //Opera, etc
			if (json.comentarios.length-1 <= 10) {
				document.getElementById("allComments").href = "javascript: viewcomments("+pro_codigo+", 1)"; //Opera, etc
				document.getElementById("allComments").innerHTML = "[Ver todos]"; //Opera, etc
			}
			else
			{
				document.getElementById("allComments").href = "javascript: viewcomments("+pro_codigo+", 0)"; //Opera, etc
				document.getElementById("allComments").innerHTML = "[10 primeiros]"; //Opera, etc
			}
		}
	}
}
// Abre o resistor numa nova janela
function resistor() {
	window.open("resistor.php","janela","toolbar=no,location=no,directories=no,status=no,menubars=no,scrollbars=no,resizable=no,width=430,height=320");
}
// Abre o cálculo de potência de fontes PC
function calculofonte() {
	window.open('ads/calculopotencia.html','calculopotencia','toolbar=no,location=no,directories=no,status=no,menubars=no,scrollbars=yes,resizable=yes,width=830,height=600');
}
// Abre o resistor numa nova janela
function verimagem(imagem) {
	cimagem = "imagem.php?img="+imagem;
	window.open(cimagem,"janela","toolbar=no,location=no,directories=no,status=no,menubars=no,scrollbars=yes,resizable=no,width=750,height=550");
}
// reloads the window if Nav4 resized
function MM_reloadPage(init) {
	if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
		document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
	else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH)
		location.reload();
}
MM_reloadPage(true);
// função para validar campos de email
function validaemail(form,campo){
	if (document.forms[form].elements[campo].value=="") {
		alert("O campo "+campo+" é obrigatório")
		return false
	}
	else
	{
		email = document.forms[form].elements[campo].value;
		invalid = "Endereço de email inválido. Você precisa digitar seu email corretamente.";
		invalid = invalid + "\nEx: contato@electronics.com.br.";

		if (email.indexOf("@")<1 || email == "contato@electronics.com.br" || email == "") {
			alert(invalid);
			return false;
		}
		else {
			if (campo=="forgot") {
				alert("Um email será encaminhado com uma nova senha temporária!");
			}
			return true;
		}
	}
}
// valida somente um campo texto != de vazio
function validacampo(form,campo) {
	if (document.forms[form].elements[campo].value=="") {
		alert("O campo "+campo+" não pode ser vazio");
		return false;
	}
}
// validar determinado form (Não consegui fazer uma superfunção)
function validar(form) {
	if (form=="formmail") {
		if (document.forms[form].nome.value=="") {
			alert("O campo 'Nome' é obrigatório");
			return false;
		}
		if (document.forms[form].email.value=="") {
			alert("O campo 'Email' é obrigatório");
			return false;
		}
		if (document.forms[form].mensagem.value=="") {
			alert("O campo 'Mensagem' é obrigatório");
			return false;
		}
		email = document.forms[form].email.value;
		invalid = "Endereço de email inválido. Você precisa digitar seu email corretamente.";
		invalid = invalid + "\nEx: contato@electronics.com.br.";  
		if (email.indexOf("@")<1 || email == "contato@electronics.com.br" || email == "") {
			alert(invalid);
			return false;
		}
	}
}
// abre iframe comentário
function comentario(iframeid,permissao) {
	objIFRAME = document.getElementById(iframeid);
	if (permissao==true) {
		objIFRAME.style.display="block";
	} else {
		objIFRAME.style.display="none";
	}
}
// confirma campos da pesquisa
function checkpoll(form,campo,nopcoes,cmsg) {
  for (var i=0;i<nopcoes+1;i++) {
    if (document.forms[form].elements[i].name==campo) {
	  if (document.forms[form].elements[i].checked) {
	    return true;
		break;
	  }
	}
  }
  alert(cmsg);
  return false;
}
// Imprime a página navega para mesma ou próxima
function imprimir(pagina)
{
  window.print();
//  window.navigate(pagina);
}

function visible(idlinha,imgref) {
	if (document.getElementById(idlinha).style.display == "none")
	{
		document.getElementById(idlinha).style.display = "";
		document.getElementById(imgref).src = "images/btnup.gif";
	}
	else
	{
		document.getElementById(idlinha).style.display = "none";
		document.getElementById(imgref).src = "images/btndown.gif";
	}
}
function redirect(url) {
  location.href = 'redirect.php?link='+url;
}
function selectVote(valor) {
	switch (parseInt(valor)) {
		case 0:
		{
			document.getElementById("imgvote1").src = "images/novote.gif";
			document.getElementById("imgvote2").src = "images/novote.gif";
			document.getElementById("imgvote3").src = "images/novote.gif";
			document.getElementById("imgvote4").src = "images/novote.gif";
			document.getElementById("imgvote5").src = "images/novote.gif";
			break;
		}
		case 1:
		{
			document.getElementById("imgvote1").src = "images/vote.gif";
			document.getElementById("imgvote2").src = "images/novote.gif";
			document.getElementById("imgvote3").src = "images/novote.gif";
			document.getElementById("imgvote4").src = "images/novote.gif";
			document.getElementById("imgvote5").src = "images/novote.gif";
			break;
		}
		case 2:
		{
			document.getElementById("imgvote1").src = "images/vote.gif";
			document.getElementById("imgvote2").src = "images/vote.gif";
			document.getElementById("imgvote3").src = "images/novote.gif";
			document.getElementById("imgvote4").src = "images/novote.gif";
			document.getElementById("imgvote5").src = "images/novote.gif";
			break;
		}
		case 3:
		{
			document.getElementById("imgvote1").src = "images/vote.gif";
			document.getElementById("imgvote2").src = "images/vote.gif";
			document.getElementById("imgvote3").src = "images/vote.gif";
			document.getElementById("imgvote4").src = "images/novote.gif";
			document.getElementById("imgvote5").src = "images/novote.gif";
			break;
		}
		case 4:
		{
			document.getElementById("imgvote1").src = "images/vote.gif";
			document.getElementById("imgvote2").src = "images/vote.gif";
			document.getElementById("imgvote3").src = "images/vote.gif";
			document.getElementById("imgvote4").src = "images/vote.gif";
			document.getElementById("imgvote5").src = "images/novote.gif";
			break;
		}
		case 5:
		{
			document.getElementById("imgvote1").src = "images/vote.gif";
			document.getElementById("imgvote2").src = "images/vote.gif";
			document.getElementById("imgvote3").src = "images/vote.gif";
			document.getElementById("imgvote4").src = "images/vote.gif";
			document.getElementById("imgvote5").src = "images/vote.gif";
			break;
		}
	}
}
function setVote(valor, projeto, uid) {
	ajax = ajaxInit();
	if (ajax) {
		ajax.onreadystatechange = writeVotes;
		url = 'functions.php?mode=vote&prj='+projeto+"&valor="+valor+"&uid="+uid;
		ajax.open('GET',url,true);
		ajax.send(null);
	}	
}
function writeVotes() {
	if (ajax.readyState == 1) {
		document.getElementById("imgvote1").src = "images/progresso.gif";
		document.getElementById("imgvote2").style.display = "none";
		document.getElementById("imgvote3").style.display = "none";
		document.getElementById("imgvote4").style.display = "none";
		document.getElementById("imgvote5").style.display = "none";
	}
	else if (ajax.readyState == 4) {
		if (ajax.status == 200) {
			var x = 0;
			var toeval = ajax.responseText;
			if (toeval == '')
				toeval = "{'votos':[{'qtde':'0','media':'0'}]}";
			var json = eval("("+toeval+")");
			document.getElementById("imgvote1").onclick = "";
			document.getElementById("imgvote2").onclick = "";
			document.getElementById("imgvote3").onclick = "";
			document.getElementById("imgvote4").onclick = "";
			document.getElementById("imgvote5").onclick = "";
			document.getElementById("imgvote1").onmouseover = "";
			document.getElementById("imgvote2").onmouseover = "";
			document.getElementById("imgvote3").onmouseover = "";
			document.getElementById("imgvote4").onmouseover = "";
			document.getElementById("imgvote5").onmouseover = "";
			document.getElementById("imgvote1").onmouseout = "";
			document.getElementById("imgvote2").onmouseout = "";
			document.getElementById("imgvote3").onmouseout = "";
			document.getElementById("imgvote4").onmouseout = "";
			document.getElementById("imgvote5").onmouseout = "";
			document.getElementById("imgvote2").style.display = "";
			document.getElementById("imgvote3").style.display = "";
			document.getElementById("imgvote4").style.display = "";
			document.getElementById("imgvote5").style.display = "";
			if (parseInt(json.votos[0].qtde) < 2)
				document.getElementById("txtVotos").innerHTML = "("+json.votos[0].qtde+" voto)";
			else
				document.getElementById("txtVotos").innerHTML = "("+json.votos[0].qtde+" votos)";
			selectVote(json.votos[0].media);
		}
	}
}