// AJAX
var ie=(document.all) ? 1:0;
var n4=(document.layers) ? 1:0;
var n6=(document.getElementById) ? 1:0;  
var msg="";

function setHttpRequest(){
	var http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
	 http_request = new XMLHttpRequest();
	 if (http_request.overrideMimeType) {
		http_request.overrideMimeType('text/xml');
	 }
	} else if (window.ActiveXObject) { // IE
	 try {
		http_request = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
		try {
		   http_request = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {
		}
	 }
	}
	return http_request;
}

function makePostRequestTribuna(url, msg_name, parameters) {
	httpTribuna=setHttpRequest();
	if (!httpTribuna) {
	 alert('Cannot create XMLHTTP instance');
	 return false;
	}
	httpTribuna.onreadystatechange = getResponseTribuna;
	httpTribuna.open("post", url);
	httpTribuna.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;');
	httpTribuna.send(parameters);
}


function makePostRequestTool(url, parameters) {
	httpTool=setHttpRequest();
	if (!httpTool) {
	 alert('Cannot create XMLHTTP instance');
	 return false;
	}
	httpTool.onreadystatechange = getResponseTool;
	httpTool.open("post", url, true);
	httpTool.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;');
	httpTool.setRequestHeader('Content-length', parameters.length);
	httpTool.send(parameters);
}

function getResponseTribuna(){
  if (httpTribuna.readyState == 4) {
	 if (httpTribuna.status == 200) {
		result = httpTribuna.responseText;
		if(ie){    
			document.all["tri_msg"].innerHTML=result;
		}else{
			if(n4){
				document.layers["tri_msg"].innerHTML=result;
			}else{
				if(n6){      
					document.getElementById("tri_msg").innerHTML=result;
				}
			}
		}
	 }
  }
}

function getResponseTool(){
  if (httpTool.readyState == 4) {
	 if (httpTool.status == 200) {
		result = httpTool.responseText;
		//alert(result);
		window.document.location.href=result;
	 } else {
		alert('No es pot guardar.');
	 }
  }
}


//Valida un email mediante expresiones regulares
function validarEmail(valor) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(valor)){
		return true;
	} else {
		return false;
	}
}

function enviaTool(iddoc, cat, tbl, tool){
	var poststr = "iddoc="+iddoc+"&cat="+cat+"&tbl="+tbl+"&tool="+tool;
	makePostRequestTool("http://hemeroteca.e-noticies.com/proc/enviaTool.php", poststr);
}


function enviaEncuestaElemento(iddoc, idenc, idelem){
	var poststr = "iddoc="+iddoc+"&idenc="+idenc+"&idelem="+idelem;
	makePostRequestTool("http://hemeroteca.e-noticies.com/proc/enviaEncuesta.php", poststr);
}

function xat(){
	// the size of the popup window
	var width = 800;
	var height = 600;

	// the x,y position of the popup window
	// NOTE: this formula will auto-center the popup on the screen
	var y = (screen.height - height) / 2;
	var x = (screen.width - width) / 2;

	var url = 'http://www.e-noticies.com/xat/flashchat.php';
	var options = 'width=' + width + ',height=' + height + ',top=' + y + ',left=' + x + ',scrollbars=0, resizable=0';

	// open the chat window as a popup, instead of embedded in webpage
	var pop=window.open( url, 'chat', options);
	pop.focus();
}
