//	funcion encargada de crear el objeto AJAX
function http_request() {
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

//	agrega documento nuevo a imprimir y devuelve el total de documentos en cola
function subtitulo(id) {
	
	_resumen = http_request();
	_URL = "subtitulo.php?";
	if (id != "") {
		_URL+= "id=" + id + "&";
	}
	_resumen.open("GET",_URL,true);
	_resumen.onreadystatechange = function() {
		if (_resumen.readyState == 1) {
			cargando("noticiaEntera" + id);
		}
		if (_resumen.readyState == 4) {
			if (_resumen.status == 200) {				
				var _XML = _resumen.responseXML;
				var _id = _XML.getElementsByTagName('id').item(0).firstChild.data;
				var _titulo = document.getElementById("titulo" + _id).innerHTML;
				var _fecha = _XML.getElementsByTagName('fecha').item(0).firstChild.data;
				var _subtitulo = _XML.getElementsByTagName('subtitulo').item(0).firstChild.data;			
				var _imagen = _XML.getElementsByTagName('imagen').item(0).firstChild.data;
				var _imagenGrande = _XML.getElementsByTagName('imagenGrande').item(0).firstChild.data;
				var _texto = "";
				var docs = pDocs.split(",");
				var b_pDoc = false;

				document.getElementById("noticiaSumario" + _id).style.display = 'none';
				document.getElementById("noticiaEntera" + _id).style.display = '';
				
				for (var c = 0; c < nDocs || _id == docs[c]; c++) {
					if (_id == docs[c]) {
						b_pDoc = true;
					}
				}				
				
				_texto+= "	<div id=\"gnoticia" + _id + "\" class=\"titulo\">";
				_texto+= "		<a href=\"javascript:ocultarNoticia(" + _id + ");\">" + _titulo + "</a>";					
				_texto+= "	</div>";
				if (_imagen != "**nulo" && _imagenGrande == 0) {
					_texto+= "	<div>";
					_texto+= "		<img id=\"imagen" + _id + "\" src=\"/images/" + _imagen + "\" align=\"right\" class=\"marcoImagen\">";
					_texto+= "	</div>";
				}
				_texto+= "	<div class=\"fecha\">";
				_texto+= "		Art&iacute;culo&nbsp;publicado&nbsp;el&nbsp;" + _fecha + "&nbsp;&nbsp;";
				_texto+= "		<a href=\"javascript:aImprimir(" + _id + ");\" id=\"imprimir" + _id + "\" style=\"display:\">";
				if (!b_pDoc) {
					_texto+= "		<img valign=\"bottom\" border=\"0\" src=\"images/printplus.gif\" alt=\"Añadir art&iacute;culos a la cola de impresi&oacute;n\">";
				}
				_texto+= "		&nbsp;&nbsp;";
				_texto+= "		</a>";
				_texto+= "		<a href=\"#\" onclick=\"void(0);\">";
				_texto+= "		<img valign=\"bottom\" border=\"0\" src=\"/images/print.gif\" alt=\"Imprimir solo este art&iacute;culo\">";
				_texto+= "		</a>";
				_texto+= "	</div>";
				if (_subtitulo != "**nulo") {
					_texto+= "	<div id=\"subtitulo" + _id + "\" class=\"subtitulo\">" + _subtitulo + "</div>";
				}
				if (_imagen != "**nulo" && _imagenGrande == 1) {
					_texto+= "	<div>";
					_texto+= "	<center>";
					_texto+= "		<img id=\"imagenXL" + _id + "\" src=\"/images/" + _imagen + "\" class=\"marcoImagen\">";
					_texto+= "	</center>";
					_texto+= "	</div>";
				}
				_texto+= "	<div id=\"_texto" + _id + "\" class=\"a10\">";
				_texto+= "		<a href=\"?noticia=" + _id + "\">Leer art&iacute;culo completo</a>";
				_texto+= "	</div>";
				
				document.getElementById("noticiaEntera" + _id).innerHTML = _texto;
			}
		}
	}
	_resumen.send(null);
	return
	
}

//	agrega documento nuevo a imprimir y devuelve el total de documentos en cola
function aImprimir(id) {
	
	_aImprimir = http_request();
	_URL_ = "sesion.php?";
	_aImprimir.open("GET",_URL_ + "pDocs=" + id,true);
	_aImprimir.onreadystatechange = function() {
		if (_aImprimir.readyState == 1) {
			cargando('dek');
		}
		if (_aImprimir.readyState == 4) {
			if (_aImprimir.status == 200) {
				descargando('dek');				
				//	tambien muestra el div que contiene el número de artículos que se van a imprimir y el boton para imprimirlos
				document.getElementById('aImprimir').style.display = '';
				document.getElementById('imprimir' + id).style.display = 'none';
				
				inicializarWEB();
			}
		}
	}
	_aImprimir.send(null);
	return
	
}

//	agrega documento nuevo a imprimir y devuelve el total de documentos en cola
function sumario(id) {
	
	_aImprimir = http_request();
	_URL_ = "sesion.php?";
	_aImprimir.open("GET",_URL_ + "pDocs=" + id,true);
	_aImprimir.onreadystatechange = function() {
		if (_aImprimir.readyState == 1) {
			cargando('dek');
		}
		if (_aImprimir.readyState == 4) {
			if (_aImprimir.status == 200) {
				descargando('dek');				
				//	tambien muestra el div que contiene el número de artículos que se van a imprimir y el boton para imprimirlos
				document.getElementById('aImprimir').style.display = '';
				document.getElementById('imprimir' + id).style.display = 'none';
				
				inicializarWEB();
			}
		}
	}
	_aImprimir.send(null);
	return
	
}

//	inicializa las variables de sesión
function inicializarWEB() {
	
	_inicializar = http_request();
	_URL_ = "sesion.php?pDocs=0&nDocs=0";
	_inicializar.open("GET", _URL_, true);
	_inicializar.onreadystatechange = function() {
		if (_inicializar.readyState == 1) {
			//cargando("dek");
		}
		if (_inicializar.readyState == 4) {
			if (_inicializar.status == 200) {
				//descargando('dek');
				var _XML = _inicializar.responseXML;
				//	leo el XML que devuelve "sesion.php" guardo en las variables 
				//	"_pDocs" y "_nDocs" el contenido de las variables de sesión
				pDocs = _XML.getElementsByTagName('pDocs').item(0).firstChild.data;
				nDocs = _XML.getElementsByTagName('nDocs').item(0).firstChild.data;
				
				if (nDocs > 0) {
					//	muestro el letrero con los archivos a imprimir
					document.getElementById('aImprimir').style.display = '';
					if (nDocs > 1) {
						document.getElementById('articulosAImprimir').innerHTML = nDocs + ' art&iacute;culos a imprimir';
					} else {
						document.getElementById('articulosAImprimir').innerHTML = nDocs + ' art&iacute;culo a imprimir';
					}
				}				
			}
		}
	}
	_inicializar.send(null);
	return
	
}

function ocultarNoticia(id) {
	
	document.getElementById("noticiaSumario" + id).style.display = "";
	document.getElementById("noticiaEntera" + id).style.display = "none";
	
}

function cargando(div) {
	document.getElementById(div).style.display = "";
	document.getElementById(div).innerHTML = "<img src=\"../images/loading.gif\">&nbsp;cargando ...&nbsp;";	
}

function descargando(div) {
	document.getElementById(div).style.display = "none";
}

function editarCampo(div) {
	
	HTML = document.getElementById(div).innerHTML;
	HTML = HTML.replace(/<p>/gi, "");
	HTML = HTML.replace(/<\/p>/gi, "\n\n");
	document.getElementById(div).style.display = "none";
	document.getElementById("e" + div).style.display = "";
	document.getElementById("f" + div).value = HTML;
	document.getElementById("f" + div).focus();
	document.getElementById("f" + div).select();
	
}

function ocultaForm(div) {
	textoFormateado = "<p>";
	textoFormateado+= document.getElementById("f" + div).value.replace(/[\n]+/g, "</p><p>");
	textoFormateado+= "</p>";
	document.getElementById(div).innerHTML = textoFormateado;
	document.getElementById("e" + div).style.display = "none";
	document.getElementById(div).style.display = "";
}

function ocultaTitulo(div) {
	document.getElementById(div).innerHTML = document.getElementById("f" + div).value;
	document.getElementById("e" + div).style.display = "none";
	document.getElementById(div).style.display = "";
}

function reloj() {
	
	var momentoActual = new Date();
	var dia = momentoActual.getDate();
	var mes = momentoActual.getMonth();
	var ano = momentoActual.getFullYear();
	var hora = momentoActual.getHours();
	var minuto = momentoActual.getMinutes();
	var segundo = momentoActual.getSeconds();

    str_segundo = new String (segundo)
    if (str_segundo.length == 1)
       segundo = "0" + segundo

    str_minuto = new String (minuto)
    if (str_minuto.length == 1)
       minuto = "0" + minuto

    str_hora = new String (hora)
    if (str_hora.length == 1)
       hora = "0" + hora	
	
	var horaImprimible = ano + "-" + mes + "-" + dia + " " + hora + ":" + minuto + ":" + segundo;
	
	document.getElementById("fecha").innerHTML = horaImprimible;

	setTimeout("reloj()", 1000);
	
}

function ampliarImagen(imagen) {
	var img = "<img id=\"imagen\" src=\"../images/xl_" + imagen + "\" class=\"marcoImagen\">";
	document.getElementById("fullTabla").innerHTML = img;
	document.getElementById("full").style.display = "";
}

var menuEmpresasSelec = "";

function menuEmpresas (itemMenuEmp) {
	document.getElementById('bancos').style.display = "none";
	document.getElementById('ahorro').style.display = "none";
	document.getElementById('rurales').style.display = "none";
	document.getElementById('seguros').style.display = "none";
	document.getElementById('despachos').style.display = "none";
//	document.getElementById('telemarketing').style.display = "none";
	if (menuEmpresasSelec == "" || menuEmpresasSelec != itemMenuEmp) {
		document.getElementById(itemMenuEmp).style.display = "";
		menuEmpresasSelec = itemMenuEmp;
	} else {
		menuEmpresasSelec = "";
	}
}

var menuDerechaSelec = "";

function menuDerecha (itemMenuDer) {
	document.getElementById('menuTexLeg').style.display = "none";
	document.getElementById('menuOcio').style.display = "none";
	document.getElementById('menuHerrSind').style.display = "none";
	document.getElementById('menuHumor').style.display = "none";
	document.getElementById('menuNovedades').style.display = "none";
//	document.getElementById('menuComunicados').style.display = "none";
	document.getElementById('menuPlantillas').style.display = "none";
//	document.getElementById('menuRevistas').style.display = "none";
	if (menuDerechaSelec == "" || menuDerechaSelec != itemMenuDer) {
		document.getElementById(itemMenuDer).style.display = "";
		menuDerechaSelec = itemMenuDer;
	} else {
		menuDerechaSelec = "";
	}
}

function imagen (imagen, origen) {
	html = "<a href=\"javascript: ocultaDiv('fotero');\"><img src=\"" + imagen + "\"></a><br>";
	html+= "&nbsp;&nbsp;&nbsp;<a href=\"http://www." + origen + "\">" + origen + "</a>";
	
	document.getElementById('fotero').style.display = "";
	document.getElementById('fotero').innerHTML = html;
}

function ocultaDiv (capa) {
	document.getElementById(capa).style.display = "none";
}

function muestraDiv (capa) {
	document.getElementById(capa).style.display = "";
}

function handleEnter (field, event, formulario) {
    var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
    if (keyCode == 13) {
        document.getElementById(formulario).submit();
        return false;
    }
    else
    return true;
} 

function fechaHoy (div) {
	var momentoActual = new Date();
	var dia = momentoActual.getDate();
	var mes = momentoActual.getMonth() + 1;
//	if (mes < 10)
//		mes = "0" + mes;
	var ano = momentoActual.getFullYear();

	var horaImprimible = ano + "-" + mes + "-" + dia + " 07:00:00";

	document.getElementById(div).value = horaImprimible;
}

function fecha () {
	var mydate = new Date()
	var year = mydate.getYear()
	if (year < 1000)
	year+= 1900
	var day = mydate.getDay()
	var month = mydate.getMonth()
	var daym = mydate.getDate()
	if (daym < 10)
	daym = "0"+daym
	var dayarray = new Array("Domingo","Lunes","Martes","Miercoles","Jueves","Viernes","Sabado")
	var montharray = new Array("Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre")
	alert ("<small><font color='000000' face='Arial'>"+dayarray[day]+" "+daym+" de "+montharray[month]+" de "+year+"</font></small>")
}

function cargaAnadir () {
	fechaHoy('fecha');
	document.anadir.titulo.focus();
}

function menuAuth (aptdo) {
	if (aptdo == 'delegadosAuth') {
		muestraDiv('delegadosAuth'); 
		ocultaDiv('afiliadosAuth'); 
		document.getElementById('delegadosAuth').focus();
		document.getElementById('afiliadosAuth').value = '';
	} else {
		muestraDiv('afiliadosAuth'); 
		ocultaDiv('delegadosAuth'); 
		document.getElementById('afiliadosAuth').focus();
		document.getElementById('delegadosAuth').value = '';
	}
}

function inicializaMenuAuth() {
	if (autenticado == 1) {
		ocultaDiv('afiliados'); 
		ocultaDiv('delegados');
		muestraDiv('salirAut');
	}
}
