var http_request = false;

function showHide(idDiv)
{
	if (document.getElementById(idDiv).style.display == 'none')
	{
		document.getElementById(idDiv).style.display = 'block';
	}
	else
	{
		document.getElementById(idDiv).style.display = 'none';
	}
}

function ScreenOpacity(idMostrar)
{
	element = document.getElementById('layerDiv');
	element.style.display = "block";
	document.getElementById('layerDiv').style.height = document.body.clientHeight + 'px';
	document.getElementById('layerDiv').style.width = document.body.clientWidth + 'px';
	element = document.getElementById(idMostrar);
	element.style.display = "";

	var x = parseInt((window.screen.width / 2) - (element.offsetWidth / 2), 10);
	var y = parseInt((window.screen.height / 2) - (element.offsetHeight), 10);

	if (y < 0)
	{
		y = 10;

	}
	element.style.top = y + 'px';
	element.style.left = x + 'px';
	element.style.position = 'fixed';
	if (navigator.userAgent.indexOf('MSIE 6') >= 0)
	{
		element.style.position = 'absolute';
	}
}
function enviarRecomendacion(TxtEmailDestinatario, TxtNombreRemitente, TxtEmailRemitente, ChkCopia, OptEnviar, TxtComentarios, idTrago)
{
	var EmailDestinatario = document.getElementById(TxtEmailDestinatario).value;
	var NombreRemitente = document.getElementById(TxtNombreRemitente).value;
	var EmailRemitente = document.getElementById(TxtEmailRemitente).value;
	var ChkCopy = document.getElementById(ChkCopia).checked;
	var OptEnviarCompl = document.getElementById(OptEnviar).checked;
	var Comentarios = document.getElementById(TxtComentarios).value;
	var arrErrores = new Array('', '', '');
	var valid = true;

	if (!isEmail(EmailDestinatario))
	{
		valid = false;
		arrErrores[0] = 'Verifique el e-mail de destinatario.<br>';
	}
	if (NombreRemitente == '')
	{
		valid = false;
		arrErrores[1] = 'Verifique el nombre de remitente.<br>';
	}
	if (!isEmail(EmailRemitente))
	{
		valid = false;
		arrErrores[2] = 'Verifique el e-mail de remitente.<br>';
	}

	if (valid)
	{
		enviarMailNota(EmailDestinatario, NombreRemitente, EmailRemitente, ChkCopy, OptEnviarCompl, Comentarios, idTrago);
		ScreenOpacity('divEnviarExito');
		document.getElementById('divCerrar').style.display = 'none';
		setTimeout("showHide('divEnviarExito');showHide('divEnviar');showHide('layerDiv');",2000);
	}
	else
	{
		showHide('divErrorEnvio');
		document.getElementById('divErrorEnvio').innerHTML = arrErrores[0] + arrErrores[1] + arrErrores[2];
	}
}

function enviarMailNota(EmailDestinatario, NombreRemitente, EmailRemitente, ChkCopy, OptEnviar, Comentarios, idTrago)
{
	http_request = false;

	if (window.XMLHttpRequest)
	{ // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
			// Ver nota sobre esta linea al final
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request) {
		alert('Falla :( No es posible crear una instancia XMLHTTP');
		return false;
	}

	//http_request.onreadystatechange = envMailNota;
	
	var variablesPost = "nomRem=" + NombreRemitente + "&comentarios=" + Comentarios + "&emailRem=" + EmailRemitente + "&emailDest=" + EmailDestinatario +  "&enviar=" + OptEnviar + "&copy=" + ChkCopy;

	http_request.open("POST", "/fichatrago.php?idTrago=" + idTrago, true);
	http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	http_request.send(variablesPost);
}

function envMailNota()
{
	if (http_request.readyState == 4)
	{
		if (http_request.status == 200)
		{
			//document.getElementById('cantRecomendaciones').innerHTML = http_request.responseText;
		}
		else
		{
			alert('Hubo problemas con la petición.');
		}
	}
}
function showHideClose(idDiv)
{
	document.getElementById(idDiv).style.display = 'block';
}
function autoCierre(idDiv)
{
	setTimeout("showHide('divReportado');showHide('layerDiv');", 2000);
}
function clearText(id)
{
	document.getElementById(id).value = '';
}
function RollOver(id, src)
{
	document.getElementById(id).src = src;
}
function isEmail(s)
{
	if(s.match(/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/))
	{
		return true;
	}
	return false;
}
function openPopup(idTrago)
{
	popup = window.open('imprimir.php?idTrago=' + idTrago,'650x300','toolbar=no,status=no,scrolling=no,scrollbars=no,location=no,menubar=no,directories=no,resizable=yes,width=620,height=520');
}
function InicializarTextos(idDest, idNomRem, idRem, IdCom, IdErr)
{
	document.getElementById(idDest).value = 'E-mail (campo obligatorio)';
	document.getElementById(idNomRem).value = 'Nombre/s';
	document.getElementById(idRem).value = 'E-mail (campo obligatorio)';
	document.getElementById(IdCom).value = '';
	document.getElementById(IdErr).innerHTML = '';
}
function cambiarSolapa(idSel, idCompl)
{
	if (idSel == 'masVotados')
	{
		document.getElementById(idSel).src = '/imagenes/cajas/estadisticas/' + idSel + '_Azul.gif';
		document.getElementById(idCompl).src = '/imagenes/cajas/estadisticas/' + idCompl + '_Negro.gif';
	}
	else
	{
		document.getElementById(idSel).src = '/imagenes/cajas/estadisticas/' + idSel + '_Azul.gif';
		document.getElementById(idCompl).src = '/imagenes/cajas/estadisticas/' + idCompl + '_Negro.gif';
	}

	document.getElementById('layout_' + idSel).style.display = 'block';
	document.getElementById('layout_' + idCompl).style.display = 'none';
}