// JavaScript Document

var numero = 0;

// Funciones comunes
c= function (tag) { // Crea un elemento
   return document.createElement(tag);
}
d = function (id) { // Retorna un elemento en base al id
   return document.getElementById(id);
}
e = function (evt) { // Retorna el evento
   return (!evt) ? event : evt;
}
f = function (evt) { // Retorna el objeto que genera el evento
   return evt.srcElement ?  evt.srcElement : evt.target;
}

addField = function (divi,tipo) {
	var div=divi;
	var tipo=tipo;
	//alert(div+" "+tipo);
	
	container = d(div);
   
   
   if(tipo==1){	
   //alert(tipo);
   span = c('SPAN');
   //span.className = 'enlace';
   span.id = 'enlace' + (++numero);

   titulo =c('INPUT');
   //titulo.innerHTML ='titulo del link: ';
   titulo.name = 'titulos[]';
   titulo.type = 'text';
   titulo.setAttribute('class','tdtxtform');
   
   codigo =c('INPUT');
   //codigo.innerHTML ='Código SNIES: ';
   codigo.name = 'codigos[]';
   codigo.type = 'text';
   codigo.setAttribute('class','tdtxtform'); 
   
   lin =c('INPUT');
	   //lin.innerHTML ='Link: ';
	   lin.name = 'links[]';
	   lin.type = 'text';
	   lin.setAttribute('class','tdtxtform');
	   
   span.appendChild(titulo);
   span.appendChild(codigo); 	   
   span.appendChild(lin); 	   
   }else{
   
   span = c('SPAN');   
   span.id = 'file' + (++numero);   
   
   titulo =c('INPUT');
   //titulo.innerHTML ='titulo del link: ';
   titulo.name = 'titulosa[]';
   titulo.type = 'text';
   titulo.setAttribute('class','tdtxtform');
   
   codigo =c('INPUT');
   //codigo.innerHTML ='Código SNIES: ';
   codigo.name = 'codigosa[]';
   codigo.type = 'text';
   codigo.setAttribute('class','tdtxtform');
   
   field = c('INPUT');   
   field.name = 'archivo[]';
	   //field.id = 'img[]';
   field.type = 'file';
   field.setAttribute('size','10');
   
   span.appendChild(titulo);
   span.appendChild(codigo); 	   
   span.appendChild(field);	   
   }
     
   
   a=c('A');
   a.name = span.id;
   a.href = '#';
   a.onclick = removeField;
   a.innerHTML = 'Quitar';
   
   
   span.appendChild(a);
   container.appendChild(span);
}
removeField = function (evt) {
   lnk = f(e(evt));
   span = d(lnk.name);
   span.parentNode.removeChild(span);
}

lista_destacados = function()
{
	var lista = document.getElementById("nivel");
	lista.disabled = !lista.disabled;
}

/////////////////////////////////////////////////////
//Autor: David Cerón
//Agregar los tags meta
function agregarMeta(descripcion,clave)
{
	var etiquetas;
	var cabeza = document.getElementsByTagName("head")[0];
	var tag = document.createElement("meta");

	//Eliminar los meta anteriores
	etiquetas  = document.getElementsByName("keywords");
	for(i=0; i<etiquetas.length; i++)
		cabeza.removeChild(etiquetas[i]);

	etiquetas  = document.getElementsByName("description");
	for(i=0; i<etiquetas.length; i++)
		cabeza.removeChild(etiquetas[i]);

	//Palabras claves
	tag.name = "keywords";
	tag.content = clave;
	cabeza.appendChild(tag);

	//Descripcion
	tag = document.createElement("meta");
	tag.name = "description";
	tag.content = descripcion;
	cabeza.appendChild(tag);
}

/////////////////////////////////////////////////////////////////
//Codificación y decodificación UTF-8
//http://www.webtoolkit.info/javascript-utf8.html
function utf8Encode(string)
{
	string = string.replace(/\r\n/g,"\n");
	var utftext = "";

	for (var n = 0; n < string.length; n++) {

		var c = string.charCodeAt(n);

		if (c < 128) {
			utftext += String.fromCharCode(c);
		}
		else if((c > 127) && (c < 2048)) {
			utftext += String.fromCharCode((c >> 6) | 192);
			utftext += String.fromCharCode((c & 63) | 128);
		}
		else {
			utftext += String.fromCharCode((c >> 12) | 224);
			utftext += String.fromCharCode(((c >> 6) & 63) | 128);
			utftext += String.fromCharCode((c & 63) | 128);
		}

	}

	return utftext;
}

function utf8Decode(utftext)
{
	var string = "";
	var i = 0;
	var c = c1 = c2 = 0;

	while ( i < utftext.length ) {

		c = utftext.charCodeAt(i);

		if (c < 128) {
			string += String.fromCharCode(c);
			i++;
		}
		else if((c > 191) && (c < 224)) {
			c2 = utftext.charCodeAt(i+1);
			string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
			i += 2;
		}
		else {
			c2 = utftext.charCodeAt(i+1);
			c3 = utftext.charCodeAt(i+2);
			string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
			i += 3;
		}

	}

	return string;
}

//Simplemente oculta el llamado AJAX
function cargarDIV(capa, pagina)
{
	new Ajax.Updater(capa,pagina,{asynchronous:true,evalScripts:true});
}

///////////////////////////////
//Clase manejadora de Banners
function Banner(id_banner)
{
	this.banners = new Array();
	this.vinculos = new Array();
	this.titulo = new Array();

	this.imagen = document.getElementById(id_banner);
	this.vinculo = this.imagen.parentNode;
	this.ban_pos = 0;
}

Banner.prototype.correr = function()
{
	this.imagen.src = this.banners[this.ban_pos];
	this.vinculo.href = this.vinculos[this.ban_pos];
	this.vinculo.title =  this.titulo[this.ban_pos];

	this.ban_pos++;
	if (this.ban_pos >= this.banners.length)
	{
		this.ban_pos = 0;
	}
}

Banner.prototype.programar = function(variable,tiempo)
{
	this.correr();
	setInterval(variable +".correr()",tiempo);
}

//Funciones para agregar o eliminar eventos
//tomadas de http://www.quirksmode.org/js/eventSimple.html

function addEventSimple(obj,evt,fn)
{
	if (obj.addEventListener)
		obj.addEventListener(evt,fn,false);
	else if (obj.attachEvent)
		obj.attachEvent('on'+evt,fn);
}

function removeEventSimple(obj,evt,fn)
{
	if (obj.removeEventListener)
		obj.removeEventListener(evt,fn,false);
	else if (obj.detachEvent)
		obj.detachEvent('on'+evt,fn);
}


//Función para borrar un archivo adjunto en noticas, agenda o comunicados de prensa
eliminarAdjunto = function (id_archivo, modulo)
{
	var etiqueta = document.getElementById("arc_"+id_archivo);
	var frame = document.getElementById("ifrm");

	if (window.confirm("¿Desea eliminar el archivo?"))
	{
		frame.src = "agendapedagogica/eliminarAdjunto.php?archivo="+id_archivo+"&modulo="+modulo;
	}

	etiqueta.parentNode.removeChild(etiqueta);
}

//Función especifica para borrar un archivo en Notas comunicantes
function eliminarArchivo(archivo)
{
	var fila = archivo.parentNode.parentNode;
	var frame = document.getElementById("ifrm");
	var imagen = document.getElementsByName("imagen[]");
	var con=0;

	if (window.confirm("¿Desea eliminar el archivo?"))
	{
		while (con < imagen.length)
		{
			if (imagen[con].parentNode == archivo.parentNode)
			{
				frame.src = "agendapedagogica/eliminarArchivoNota.php?archivo="+imagen[con].value;
				break;
			}
			con++;
		}

		fila.parentNode.removeChild(fila);
	}
}
