function PopNews(page, name, myWidth, myHeight, x, y) {
	popWind=window.open(page,name,"width=" + myWidth + ",height=" + myHeight + ",left=" + x + ",top=" + y + ",screenX=" + x + ",screenY=" + y + ",toolbar=no,status=no,scrollbars=no,location=no,menubar=no,directories=no,resizable=no");
	if(popWind.opener == null) {
		popWind.opener = window;
	}
	else {
		popWind.focus();
	}
	window.opener = popWind;
}

function PopUp(page, myWidth, myHeight) {
	newWind=window.open(page,"popUp","width="+myWidth+",height="+myHeight+",toolbar=no,status=no,scrollbars=yes,location=no,menubar=no,directories=no,resizable=no");
	if(newWind.opener == null) {
		newWind.opener = window;
	}
	else {
		newWind.focus();
	}
}
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function showFriend(){
	try{document.getElementById("dAmigo").style.display = "block";}catch(e){}
	try{document.getElementById("escreva").style.display = "none";}catch(e){}
	try{document.getElementById("commentas").style.display = "none";}catch(e){}
}

function showWrite(){
	try{document.getElementById("dAmigo").style.display = "none";}catch(e){}
	try{document.getElementById("escreva").style.display = "block";}catch(e){}
	try{document.getElementById("commentas").style.display = "none";}catch(e){}
}

function showComment(){
	try{document.getElementById("dAmigo").style.display = "none";}catch(e){}
	try{document.getElementById("escreva").style.display = "none";}catch(e){}
	try{document.getElementById("commentas").style.display = "block";}catch(e){}
}


function checkform(obj){
	var nameRule = /^\s+|\s+$/i;
	var rules = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;

	switch(obj.name){
		case "frmAmigo":
			if (obj.nome.value.replace(nameRule, "") != "" && obj.nomeTo.value.replace(nameRule, "") !=""){
				if(rules.test(obj.emailFrom.value) && rules.test(obj.emailTo.value)){
					return true;
				}else
					alert("Um dos endereços de e-mail não é válído!");
			}else
				alert("Os campos nome, e-mail, nome do destinatário, e-mail do destinatário são obrigatórios!")
			break;
		case "frmNos":
			if (obj.titulo.value.replace(nameRule, "") != "" && obj.texto.value.replace(nameRule, "") !="" && obj.assinatura.value.replace(nameRule, "") !=""){
				if(rules.test(obj.email.value)){
					return true;
				}else
					alert("O endereço de e-mail não é válído!");
			}else
				alert("Todos os campos são obrigatórios!")
			break;
		case "frmComment":
			if (obj.titulo.value.replace(nameRule, "") != "" && obj.texto.value.replace(nameRule, "") !="" && obj.assinatura.value.replace(nameRule, "") !="" && obj.localidade.value.replace(nameRule, "") !=""){
				if(rules.test(obj.email.value)){
					obj.assinatura.disabled = false;
					return true;
				}else
					alert("O endereço de e-mail não é válído!");
			}else
				alert("Todos os campos são obrigatórios!")
			break;
	}
	return false;
}

function validarString(obj){
	var i, r, re, str, atLeast;
	var strArr = new Array();
	str = obj.q.value;
	strArr = str.split(" ");
	str = "";
	atLeast = false;
	for(i=0; i < strArr.length; i++) {
		str += strArr[i] + " ";
		if(strArr[i].length > 2) atLeast = true;
	}
	if(!atLeast) {
		alert("Para realizar uma pesquisa deve escrever pelo menos uma palavra com três caracteres!");
		return false;
	}else{
		return true;
	}
}

function hide(id){
	try{
		for(i=1;i<11;i++){
			var obj = document.getElementById("comentaTexto"+i);
			if(i == id)
				obj.style.display = "block";
			else
				obj.style.display = "none";
		}
	}catch(e){
		return;
	}
}

function go(){
	document.location.reload();
}

function MM_changeProp(objName,x,theProp,theValue) { //v6.0
  var obj = MM_findObj(objName);
  if (obj && (theProp.indexOf("style.")==-1 || obj.style)){
    if (theValue == true || theValue == false)
      eval("obj."+theProp+"="+theValue);
    else eval("obj."+theProp+"='"+theValue+"'");
  }
}
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function validarSubmit(idCanal, id) {
	var retorno = false;
	var resposta;
	frm = document.forms.frmVotacao;
	if (frm.respostas.checked)
		retorno = true;
	else {
		for (x = 0; x < frm.respostas.length; x++)
			if (frm.respostas[x].checked) {
				retorno = true;
				resposta = frm.respostas[x].value;
			}
	}
	if (retorno == false) {
		alert ("Por favor, selecione uma resposta.");
	}
	else {
		PopUp('http://www.publico.clix.pt/inquerito.asp?idCanal='+idCanal+'&id='+id+'&resposta='+resposta,545,305)
	}
}

function checkAnswer(obj){
	if (obj.respostas.checked)
		retorno = true;
	else {
		for (x = 0; x < obj.respostas.length; x++)
			if (obj.respostas[x].checked) {
				return true;
			}
	}
}


function doPostBAck(){
	MM_changeProp('confirmaInquerito','','style.display','none','DIV');
	PopNews("http://www.publico.clix.pt/inquerito.asp", "Inquerito", 545, 325);
	document.frmVotacao.Votar.disabled = true;
	document.frmVotacao.action="inquerito.asp";
	document.frmVotacao.target="Inquerito";
	document.frmVotacao.submit();
}

function doPostBAck2(id){
	MM_changeProp('confirmaInquerito'+id,'','style.display','none','DIV');
	PopNews("http://www.publico.clix.pt/inquerito.asp", "Inquerito", 545, 325);
	var obj = document.getElementById("frmVotacao"+id);
	obj.Votar.disabled = true;
	obj.action="inquerito.asp";
	obj.target="Inquerito";
	obj.submit();
}

function doPostBAckwScroll(){
	MM_changeProp('confirmaInquerito','','style.display','none','DIV');
	PopNews2("http://www.publico.clix.pt/inquerito.asp", "Inquerito", 545, 325);
	document.frmVotacao.Votar.disabled = true;
	document.frmVotacao.action="inquerito.asp";
	document.frmVotacao.target="Inquerito";
	document.frmVotacao.submit();
}

function PopNews2(page, name, myWidth, myHeight, x, y) {
	popWind=window.open(page,name,"width=" + myWidth + ",height=" + myHeight + ",left=" + x + ",top=" + y + ",screenX=" + x + ",screenY=" + y + ",toolbar=no,status=no,scrollbars=yes,location=no,menubar=no,directories=no,resizable=no");
	if(popWind.opener == null) {
		popWind.opener = window;
	}
	else {
		popWind.focus();
	}
	window.opener = popWind;
}

function aboutSite(){
	//alert("Hello!");
}

//Added by CL06
function mkGETReq(url, parameters) {
	url=url.replace("&amp;", "&");
	parameters=parameters.replace("&amp;", "&");

	http_request = false;
    if (window.XMLHttpRequest) {
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/html');
         }
    } else if (window.ActiveXObject) {
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
     }
	  if (!http_request) {
         return false;
     } 
	http_request.onreadystatechange = alertContents;
	http_request.open('GET', url, true);
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_request.setRequestHeader("Content-length", parameters.length);
	http_request.setRequestHeader("Connection", "close");
	http_request.send(parameters);
}
function alertContents() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			result = http_request.responseText;
         } else {
         }
	}
}
