// ***************************
// ** Librería de Funciones **
// ***************************

function dt(number) {
	window.open("dt.html?Origen="+number,"new","width=410,height=500,statusbar=no");
}

// *************************************************
// inStr 1.0: de la función inStr en Microsoft BASIC
// *************************************************
function inStr(Phrase, Word, IgnoreCase) {
	Word_Length = Word.length;
	Phrase_Length = Phrase.length;
	
	if( IgnoreCase == 1 ) {
		Word = Word.toUpperCase();
		Phrase = Phrase.toUpperCase();
	}
	
	for( i=0; i<=Phrase_Length; i++) {
		if( Phrase.substring(i,i+Word_Length) == Word ) {
			return(i);
		}
	}
	
	if( i >= Phrase_Length ) {
		return(-1);
	}
}


// ***************************************************
// cam 1.0: cambia una imagen en un documento por otra
// ***************************************************
function cam(ref,por) {
	document.images[ref].src = eval(por + ".src");
}


// ********************************************************
// getQueryString: para tomar los valores de un QueryString
// ********************************************************
Uso = location.href;
Inicio = null;

// Deteccion del QueryString
for( i=0; i<= Uso.length; i++ ) {
	if(Uso.substring(i,i+1)=="?") Inicio = i+1;
}

if( Inicio != null ) {
	Fin = Uso.length;
	QueryString = Uso.substring(Inicio, Fin);

	// Desglose del QueryString
	Pos = new Array();
	Contador = 1;
	Pos[0] = 0;
	for( i=0; i<=QueryString.length; i++) {
		if(QueryString.substring(i,i+1)=="&") {
			Pos[Contador] = i+1;
			Contador++;
		}
	}
	Elementos = Pos.length;
	
	// Asignacion de Nombres
	Nombres = new Array();
	Fin = new Array();
	for( i=0; i<=Elementos; i++) {
		for( m=Pos[i]; m<=QueryString.length; m++) {
			if( QueryString.substring(m,m+1)=="=" ) {
				Fin[i] = m;
				break;
			}
		}
		Nombres[i] = QueryString.substring(Pos[i],Fin[i]);
	}

	// Asignacion de Valores
	Valores = new Array();
	for( i=0; i<Elementos; i++) {
		for( m=Fin[i]; m<=QueryString.length; m++) {
			if( QueryString.substring(m,m+1)=="&" || QueryString.length==m) {
				End = m;
				break;
			}
		}
		Valores[i] = QueryString.substring(Fin[i]+1,End);
	}
}
				
function getQueryString(de) {
	if(Inicio != null) {
		for( i=0; i<=Elementos; i++) {
			if( Nombres[i] == de ) {
				return( Valores[i] );
				break;
			}
		}
	}
}


// ********************************************************************
// lineUpTo: Mueve elementos de una capa en coordenadas bidimensionales
// ********************************************************************
toStart = true;
Timer = 10;
oldx = 50;
oldy = 50;

function lineUpTo(x, y, endx, endy) {
	if( toStart ) {
		capa.style.top = y;
		capa.style.left = x;
		toStart = false;
	}
	
	modex = x-endx;
	modey = y-endy;
	if( modex < 0 ) { modex = -1; } else if( modex > 0) { modex = 1 } else { modex = 0 };
	if( modey < 0 ) { modey = -1; } else if( modey > 0) { modey = 1 } else { modey = 0 };
	
	if( x != endx || y != endy ) {
		switch( modex ) {
			case 1: x--; break;
			case -1: x++; break;
		}
		switch( modey ) {
			case 1: y--; break;
			case -1: y++; break;
		}
		capa.style.top = y;
		capa.style.left = x;
		vuelve = setTimeout("lineUpTo("+x+","+y+","+endx+","+endy+")", Timer);
	}
	
	oldx = x;
	oldy = y;
}


// *************************************************************
// Marquee: Crea un efecto de marquesina para plataforma cruzada
// *************************************************************
contador = 0;
function marquee(valor) {
	document.forms[0].marquesina.value = valor;
	longitud = valor.length;
	if(contador >= longitud) {
		contador = 0;
	} else if (contador <= longitud){
		valor = valor.substring(contador, longitud) + valor.substring(0,contador);
		contador++;
		vuelve = setTimeout("marquee('"+valor+"')",50);
	}
	contador = 1;
}


// ******************************************************************************************************
// Reach: mueve un objeto en una capa por la pantalla, y funciona de forma concurrente (varios al tiempo)
// ******************************************************************************************************
TimerB = 10;
objeto = new Object();
id = 0;

function reach(Capa, oldx, oldy, endx, endy, Tiempo, toStart) {
	if(id == null) { id = 1 };
	if(navigator.appName == "Netscape") {
		objeto.id = document.eval(Capa);
	} else {
		objeto.id = eval(Capa).style;
	}
	objeto.id.toStart = toStart;
	if( objeto.id.toStart ) {
		objeto.id.base = endx-oldx;
		objeto.id.altura = endy-oldy;
		objeto.id.VelocidadHorizontal = (objeto.id.base * (TimerB/Tiempo)); // * xSign; Formula de Fisica para calcular la Velocidad
		objeto.id.VelocidadVertical = (objeto.id.altura * (TimerB/Tiempo)); // * ySign; Formula de Fisica para caluclar la Velocidad
		objeto.id.x = oldx;
		objeto.id.y = oldy;
		id++;
		objeto.id.toStart = false;
	}
	
	if( objeto.id.x != endx || objeto.id.y != endy ) {
		objeto.id.x = objeto.id.x + objeto.id.VelocidadHorizontal;
		objeto.id.y = objeto.id.y + objeto.id.VelocidadVertical;
		if( objeto.id.x > endx && objeto.id.VelocidadHorizontal > 0 ) objeto.id.x = endx;
		if( objeto.id.x < endx && objeto.id.VelocidadHorizontal < 0 ) objeto.id.x = endx;
		if( objeto.id.y > endy && objeto.id.VelocidadVertical > 0 ) objeto.id.y = endy;
		if( objeto.id.y < endy && objeto.id.VelocidadVertical < 0 ) objeto.id.y = endy;
		objeto.id.left = parseInt(objeto.id.x);
		objeto.id.top = parseInt(objeto.id.y);
		toBeReached = "'"+ Capa + "'," +oldx + "," + oldy + "," + endx + "," + endy + "," + Tiempo + ","+ objeto.id.toStart;
		window.status = id;
		vuelve = setTimeout("reach("+toBeReached+")",TimerB);
	} else {
		toStart = true;
	}
}


// *********************************************************************************
// statusMarquee: crea un efecto de marquesina para la barra de estado del navegador
// *********************************************************************************
contadorb = 0;
function statusMarquee(valor) {
	window.status = valor;
	longitud = valor.length;
	if(contadorb >= longitud) {
		contadorb = 0;
	} else if (contadorb <= longitud){
		valor = valor.substring(contadorb, longitud) + valor.substring(0,contadorb);
		contadorb++;
		vuelve = setTimeout("statusMarquee('"+valor+"')",50);
	}
	contadorb = 1;
}


// **************************************************************************************************
// nutty: crea un efecto de movimiento aleatorio sobre una capa indicada dentro de un radio de acción
// **************************************************************************************************
t=0;
timerB = 100;
			
function nutty( capa, x, y, angle, radius, fuerza ) {
	if(navigator.appName == "Netscape") {
		objeto = document.eval(capa);
	} else {
		objeto = eval(capa).style;
	}
	if(t>=0) {
		x = (Math.cos(t) + radius);
		y = (Math.sin(t) + radius);
		objeto.top = y+(Math.random(20)*fuerza);
		objeto.left = x+(Math.random(20)*fuerza);
		t++;
		vuelve = setTimeout("nutty('"+capa+"',"+x+","+y+","+angle+","+radius+","+fuerza+")",100);
	}
}


// *************************************************************************************
// fadeBackground: crea un efecto "fade" desde un color a otro en el fondo del documento
// *************************************************************************************
function fadeBackground( from, tu ) {
	fromDec = eval("0x"+from);
	toDec = eval("0x"+tu);
	
	if( toDec < fromDec ) {
		document.write(" ");
		for( n = fromDec; n>=toDec; n=n-eval("0x030303") ) {
			document.bgColor = "#"+n.toString(16);
			window.status = n.toString(16);
			if(n.toString(16)==tu) {
				break;
			}
		} 
	} else {
		document.write(" ");
		for( n = fromDec; n<=toDec; n=n+eval("0x030303") ) {
			document.bgColor = "#"+n.toString(16);
			if(n.toString(16)==tu) {
				break;
			}
		} 
	}
	document.bgColor = tu;
}
