//Procedimientos de control general de Webs
//©2010, zanoni
//THE MIRACLE
//*******************************************************************************
/*TIPOS, CLASES Y PROTOTIPOS*/
//Prototipos******************************************************************************

function Init()
{
var a,b,c,n,ref,ref2;
b= this.items.length;
c= this.detail.length;
for (a=0;a<b;a++)
	{
	ref= this.name + "_registro_" + (a+1).toString();
	this.items[a]= document.getElementById(ref);
	this.items[a].style.cursor= "pointer";
	};
for (a=0;a<c;a++)
	{
	ref= this.name + "_campo_" + (a+1).toString();
	ref2= this.name + "_etiqueta_" + (a+1).toString();
	this.detail[a]= document.getElementById(ref);
	this.detail[a].style.visibility= "hidden";
	this.label[a]= document.getElementById(ref2);
	this.label[a].style.visibility= "hidden";
	};
	
this.cursorMenos.style.cursor= "pointer";
this.cursorMenos.title= "Retrocede en la lista hacia atrás";
this.cursorMas.style.cursor= "pointer";
this.cursorMas.title= "Avanza la lista hacia adelante";
return;	
}

function InitOpciones(tabla)
{
var a,b,c,n,ref;
c= tabla.length;
n= tabla[0].length-1;
this.opciones= tabla;
this.itemsOpciones= new Array(n);
for (a=0;a<n;a++)
	{
	ref= this.name + "_opciones_" + (a+1).toString();
	this.itemsOpciones[a]= document.getElementById(ref);
	this.itemsOpciones[a].style.cursor= "pointer";
	this.itemsOpciones[a].style.visibility= "hidden";
	};
		
return;	
}

function SetOpciones()
{
var a,b,registro;	
b= this.itemsOpciones.length;
registro= this.reg;
for (a=0;a<b;a++)
	{
	if (this.opciones[registro-1][a+1]=="0")
		this.itemsOpciones[a].style.visibility= "hidden"
	else
		this.itemsOpciones[a].style.visibility= "visible";
	};

return;	
}

function opcionClick(n)
{
var registro,opcion,temp,ventana1;
registro= this.reg;
opcion= n;
temp= this.opciones[registro-1][opcion];
if (temp=="0")
	return;
switch (opcion)
	{
	case 1:
		ventana1= window.open(temp,"SOLICITUDES","height=768,width=1024");
		ventana1.focus();
		break;	
	case 2:	
		ventana2= window.open(temp,"DESCARGAS");
		ventana2.focus();
		break;	
	};


return;	
}

function mOver(n)
{
var activo= this.inicio+n-1;
var a,b;
b= this.campos;
if (activo>this.registros)
	return;
if (activo==this.reg)
	return;
this.items[n-1].className= this.over;
return;	
}

function mOut(n)
{
var activo= this.inicio+n-1;
var a,b;
b= this.campos;
if (activo>this.registros)
	return;
if (activo==this.reg)
	return;
this.items[n-1].className= this.off;
return;	
}

function mClick(n)
{
var activo= this.inicio+n-1;
var a,b;
b= this.campos;
if (activo>this.registros)
	return;
if (activo==this.reg)
	return;
if ((this.reg>=this.inicio)&&(this.reg<=(this.inicio+this.items.length-1)))
	this.items[this.reg-this.inicio].className= this.off;
this.items[n-1].className= this.on;
this.reg= activo;

for (a=0;a<b;a++)
	{
	this.detail[a].style.visibility= "visible";
	this.label[a].style.visibility= "visible";
	if (navigator.appName=="Netscape")
		this.detail[a].textContent= this.lista[this.reg-1][a]
	else
		this.detail[a].innerText= this.lista[this.reg-1][a];
	};

if (this.opciones==null)
	return
else
	this.SetOpciones();	
return;	
}

function View()
{
var a,b,c,d,n,m,vacio;
a= this.inicio;
b= this.registros;
c= this.items.length;
d= this.campos;
m= this.clave;
vacio= " ";
for (n=0;n<c;n++)
	{
		if (a==this.reg)
			this.items[n].className= this.on
		else
			this.items[n].className= this.off;
		
		if (a>b)
			{
			if (navigator.appName=="Netscape")
				this.items[n].textContent= vacio
			else
				this.items[n].innerText= vacio;
			this.items[n].style.visibility= "hidden";
			}
		else
			{
			if (navigator.appName=="Netscape")
				this.items[n].textContent= this.lista[a-1][m-1]
			else
				this.items[n].innerText= this.lista[a-1][m-1];
			this.items[n].style.visibility= "visible";
			};

	a= a+1;	
	};
		
if ((this.inicio+c-1)>=b)
	this.cursorMas.style.visibility= "hidden"
else
	this.cursorMas.style.visibility= "visible";
if (this.inicio==1)
	this.cursorMenos.style.visibility= "hidden"
else
	this.cursorMenos.style.visibility= "visible";

if (this.leyenda!=null)
	{
	if (this.inicio+c-1>b)
		m= b
	else
		m= this.inicio+c-1;
	if (navigator.appName=="Netscape")
		this.leyenda.textContent= b.toString() + " elementos > " + (this.inicio).toString() + "-" + m.toString()
	else
		this.leyenda.innerText= b.toString() + " elementos > " + (this.inicio).toString() + "-" + m.toString();
		
	};
return;	
}

function Next()
{
if ((this.inicio + this.items.length)<=this.registros)
	this.inicio= this.inicio + this.items.length
else
	return;

this.View();	
return;	
}

function Prev()
{
if ((this.inicio - this.items.length)>=1)
	this.inicio= this.inicio - this.items.length
else
	return;
	
this.View();
return;
}

function InitMenu()
{
var a,b,c,n,ref;
b= this.items.length;
for (a=0;a<b;a++)
	{
	ref= this.name + "_" + (a+1).toString();
	this.items[a]= document.getElementById(ref);
	this.items[a].style.cursor= "pointer";
	};
return;	
}

function menuOver(n)
{
var activo= n;	
if (activo==this.opcion)
	return;
this.items[n-1].className= this.over;		
return;	
}

function menuOut(n)
{
var activo= n;
if (activo==this.opcion)
	return;
this.items[n-1].className= this.off;		
return;
}

function menuClick(n)
{
var activo= n;
var a,b;
if (activo==this.opcion)
	return;
if (this.opcion!=null)
	this.items[this.opcion-1].className= this.off;

this.items[n-1].className= this.on;
this.opcion= activo;	
return;	
}

function menuView()
{
var a,b,c,d,n,m,vacio;
c= this.items.length;
for (n=0;n<c;n++)
	{
	if ((n+1)==this.opcion)
		this.items[n].className= this.on
	else
		this.items[n].className= this.off;
	};	
return;	
}

function menuWeb(pag,marco)						//navega a la pag. web especificada en ventana principal o en marco especificado
{
if (marco==null)
	window.location.href= pag
else
	document.getElementById(marco).src= pag;
return;	
}

function menuObjeto(off,on)						//conmuta la visibilidad de los objetos (off=lo oculta) (on=lo hace visible)
{
if (off!=null)
	document.getElementById(off).style.visibility= "hidden";
if (on!=null)
	document.getElementById(on).style.visibility= "visible";
return;	
}

function listaObjeto(off,on)
{
if (off!=null)
	document.getElementById(off).style.visibility= "hidden";
if (on!=null)
	document.getElementById(on).style.visibility= "visible";
return;	
}

function calInit()
{
var a,b,c;
//this.fecha.setMonth(5);
this.dia= this.fecha.getDate();
this.ano= this.fecha.getFullYear();
this.mes= this.fecha.getMonth() + 1;
this.semana= this.fecha.getDay();
if (this.semana==0)
	this.semana= 7;
switch (this.mes)
	{
	case 1:
		this.mesLong= "enero";
		break;
	case 2:
		this.mesLong= "febrero";
		break;
	case 3:
		this.mesLong= "marzo";
		break;
	case 4:
		this.mesLong= "abril";
		break;
	case 5:
		this.mesLong= "mayo";
		break;
	case 6:
		this.mesLong= "junio";
		break;
	case 7:
		this.mesLong= "julio";
		break;
	case 8:
		this.mesLong= "agosto";
		break;
	case 9:
		this.mesLong= "septiembre";
		break;
	case 10:
		this.mesLong= "octubre";
		break;
	case 11:
		this.mesLong= "noviembre";
		break;
	case 12:
		this.mesLong= "diciembre";		
	};
switch (this.semana)
	{
	case 1:
		this.semanaLong= "lunes";
		break;
	case 2:
		this.semanaLong= "martes";
		break;
	case 3:
		this.semanaLong= "miércoles";
		break;
	case 4:
		this.semanaLong= "jueves";
		break;
	case 5:
		this.semanaLong= "viernes";
		break;
	case 6:
		this.semanaLong= "sábado";
		break;
	case 7:
		this.semanaLong= "domingo";	
	};
for (a=29;a<=31;a++)
	{
	this.fecha.setDate(a);
	b= this.fecha.getMonth() + 1;
	if (b!=this.mes)
		break;
	};
this.diaultimo= a - 1;
this.fecha.setDate(1);
this.fecha.setMonth(this.mes-1);
this.dia1= this.fecha.getDay();
if (this.dia1==0)
	this.dia1= 7;
return;	
}

//****************************************************************************************
//CLASES ESPECIFICAS
//Clase para control de menus
function Menu(nombre,boton_off,boton_over,boton_on,opciones,selec)
{
this.name= nombre;
this.off= boton_off;
this.over= boton_over;
this.on= boton_on;
this.items= new Array(opciones);
this.opcion= selec;
this.Init= InitMenu;
this.mOver= menuOver;
this.mOut= menuOut;
this.mClick= menuClick;
this.View= menuView;
this.Web= menuWeb;
this.Objeto= menuObjeto;
}

//Clase para control de lista
function Lista(nombre,boton_off,boton_over,boton_on,menos,mas,tabla,rango,campoclave,selec,leyenda)
{
this.name= nombre;										//nombre del objeto(instancia)
this.off= boton_off;
this.over= boton_over;
this.on= boton_on;
this.items= new Array(rango);							//matriz de objetos visuales del indice (campo clave)
this.detail= new Array(tabla[0].length);				//matriz de objetos visuales de detalle (campos detalle)
this.label= new Array(tabla[0].length);
this.opciones= null;										//matriz de opciones
this.itemsOpciones= null;								//matriz de objetos visuales de las opciones
this.cursorMenos= document.getElementById(menos);		//cursor retroceder un rango
this.cursorMas= document.getElementById(mas);			//cursor avanzar un rango
this.lista= tabla;				//tabla de datos
this.registros= tabla.length;	//numero de registros
this.campos= tabla[0].length;	//numero de campos
this.inicio= 1;					//registro inicial visible
this.reg= selec;				//registro activo seleccionado
this.clave= campoclave;			//indice del campo clave (1 a N)
this.Init= Init;				//inicializar elementos visuales
this.InitOpciones= InitOpciones;	//inicializar opciones
this.SetOpciones= SetOpciones;
this.mOver= mOver;
this.mOut= mOut;
this.mClick= mClick;
this.View= View;
this.Next= Next;
this.Prev= Prev;
this.opcionClick= opcionClick; 
this.leyenda= document.getElementById(leyenda);			//objeto visual que muestra la leyenda: numero de items y rango actual - Si es NULL, no hay leyenda
this.Objeto= listaObjeto;
}

//Clase para control de calendarios
function Calendario(nombre,fondo,celdas,selec)
{
this.name= nombre;										
this.off= fondo;										//estilo del fondo del calendario
this.cell= celdas;										//estilo de las celdas
this.selec= selec;										//dia seleccionado
this.fecha= new Date();
this.items= new Array(6);
this.Init= calInit;
this.dia;												//dia de hoy
this.semana;										//dia de semana de hoy (1:lunes 7:domingo)
this.mes;											//mes de hoy (1-12)
this.ano;											//año de hoy
this.mesLong;										//mes, nombre literal
this.semanaLong;									//dia de semana, nombre literal
this.dia1;											//dia de semana (1-7) del primer dia del mes
this.diaultimo;										//ultimo dia del mes (28,29,30,31)
}


//***********************************************************************************************
//CARGA DE TABLAS DE DATOS EXTERNAS EN XML
function CargaDatosXML(archivo,base,ref)
{
var navegador= navigator.appName;
if (navegador=="Microsoft Internet Explorer")
	{
	var xmldoc= document.getElementById(ref).XMLDocument;	
	}
else	
	if (window.XMLHttpRequest)
		{
		var xdoc= new XMLHttpRequest();
		xdoc.open("GET",archivo,false);
		xdoc.send("");
		var xmldoc= xdoc.responseXML;
		}
	else
		{
		window.alert("Este navegador es obsoleto y no puede obtener fuentes de datos dinamicas. Actualícelo, por favor.");
		return;
		};
	
var database= xmldoc.getElementsByTagName(base);
var nreg= database.length;		//numero de registros
var nfield= 0;					//numero de campos
var x= database[0].firstChild;
while(x!=null)
	{
	if(x.nodeType==1)
		nfield= nfield+1;
	x= x.nextSibling;
	};

var tabla= new Array(nreg);
var a,b;
for(a=0;a<nreg;a++)
	{
	x= database[a].firstChild;
	tabla[a]= new Array(nfield);
	b= 0;
	while(x!=null)
		{
		if(x.nodeType==1)
			{
			tabla[a][b]= x.firstChild.data;
			b= b+1;
			};
		x= x.nextSibling;
		};
	};

return tabla;
};



function ExtenderCapa(caparef,capa)
{
var navegador= navigator.appName;
var ref= document.getElementById(caparef);
var subcapa= document.getElementById(capa);
var ext;
if ((navegador=="Microsoft Internet Explorer")||(navegador=="Opera"))
	ext= parseInt(subcapa.currentStyle.top,10) + subcapa.clientHeight;
if (navegador=="Netscape")
	ext= parseInt(window.getComputedStyle(subcapa,"").getPropertyValue("top"),10) + subcapa.clientHeight;
if (ext>ref.clientHeight)
	ref.style.height= ext.toString() + "px";
return(ext);	
};

function InitPag()
{
//inicializacion de la pag web
var navegador= navigator.appName;
var ventana;
var extension;
if (navegador=="Microsoft Internet Explorer")
	ventana= document.body.parentNode.clientHeight;
if ((navegador=="Opera")||(navegador=="Netscape"))
	ventana= window.innerHeight-1;
extension= document.getElementById("banner").clientHeight + document.getElementById("main").clientHeight + document.getElementById("foot").clientHeight;
if (extension<ventana)
	document.getElementById("main").style.height= (ventana - document.getElementById("banner").clientHeight - document.getElementById("foot").clientHeight).toString() + "px";

var pos= document.getElementById("banner").clientHeight + document.getElementById("main").clientHeight;
document.getElementById("foot").style.top= pos.toString() + "px";

var ancho= window.screen.width;
var x;
if (ancho>1024)
	{
	x= (ancho/2)-512;
	document.getElementById("banner").style.left= x.toString() + "px";
	document.getElementById("foot").style.left= x.toString() + "px";
	document.getElementById("main").style.left= x.toString() + "px";
	};
document.getElementById("banner").style.visibility= "visible";
document.getElementById("foot").style.visibility= "visible";
document.getElementById("main").style.visibility= "visible";
return;	
};



