var key;
var arrayId, idPasar;

function trim(cadena)
{
	for(i=0; i<cadena.length; )
	{
		if(cadena.charAt(i)==" ")
			cadena=cadena.substring(i+1, cadena.length);
		else
			break;
	}

	for(i=cadena.length-1; i>=0; i=cadena.length-1)
	{
		if(cadena.charAt(i)==" ")
			cadena=cadena.substring(0,i);
		else
			break;
	}
	
	return cadena;
}

function seleccionar(id)
{
	var obj = document.getElementById(id);
	document.getElementById("notaNueva").value = obj.value;
//	document.getElementById("notaNueva").value = obj.value;
	document.getElementById("notaNueva").value = trim(document.getElementById("notaNueva").value);
	document.getElementById("divCarpetasEncontradas").style.display = "none";
        document.getElementById("botonGuardar").disabled=false;
}
function cambiarFondo(id,color)
{
	var obj = document.getElementById(id);
	obj.style.backgroundColor=color;	
	obj.style.cursor = "pointer";
}

function limpiarFormulario()
{
	document.getElementById("notaNueva").value = "";
	document.getElementById("textoNotas").value = "";
	document.getElementById("titulo").value = "";
}
function fixedIE(tl, n){
	var sc='scroll'+tl, d=document, c='compatMode';
	return d[c] && d[c]=='CSS1Compat'? d.documentElement[sc]+n+'px' : d.body[sc]+n+'px';
	}

function desplegarNotas()
{
	var a,b;
	var obj = document.getElementById('notas_tarifar');
	var objNotas = document.getElementById('botonNotas');
	var docElement;
	var browseName = window.navigator.appName;
	var browserVer = parseInt(window.navigator.appVersion); 
	if ( obj.style.display == "none" )
	{
		obj.style.display = "";
		document.images["miTarifar"].src = "../../img/tabviocerrar.gif";
		
		 if (navigator.appName == 'Netscape') {
                        a = window.innerHeight - 225;
                        b = window.innerHeight - 250;
                        obj.style.top = a+"px";
                        objNotas.style.top = b+"px";

                }
		if (isIE == "6")
		{
			 a = - 250 - 25 + document.body.clientHeight;
			 b = -0 - 250 + document.body.clientHeight;	
			obj.style.setExpression("top","fixedIE('Top',"+b+")");
			objNotas.style.setExpression("top","fixedIE('Top',"+a+")");
			
			
		}
	} else {
		
		obj.style.display = "none";
		document.images["miTarifar"].src = "../../img/tabvioabrir.gif";
		if (navigator.appName == 'Netscape') {
                        a = window.innerHeight - 25;
                        objNotas.style.top = a+"px";
                }

		
		if (isIE == "6")
		{
			a = -1 - 25 + document.body.clientHeight;	
			objNotas.style.setExpression("top","fixedIE('Top',"+a+")");
		}
	}	

}
function validarTexto()
{
	
    var obj1 = document.getElementById("notaNueva");
    var obj  = document.getElementById("textoNotas");
    var obj2 = document.getElementById("titulo");

	if (obj.value == ""){
		alert ("La nota no puede quedar en blanco.");
		return false;
	}
	if (obj1.value == ""){
		alert ("Las notas deben guardarse en carpetas nuevas o existentes.");
		return false;
	}
	if (obj2.value == ""){
		alert ("La nota debe tener un titulo.");
		return false;
	}
	return true;

}

function buscarCarpetas(evento)
{
	
	document.getElementById("botonGuardar").disabled=true;
	var palabra = document.getElementById("notaNueva").value;
	var url;
 	if (evento.which == undefined){
		   key = window.event.keyCode;
	}	else {	
		   key = evento.which;	
		  		   	
	}	
	if (key != 40 && key != 38 && key != 13 ) {
		url = "../notas/buscarCarpetas.jsp" + "?palabra=" + palabra;
	} else {
			
 		url = "../notas/buscarCarpetas.jsp" + "?palabra=" + palabra+ "&busqueda=busqueda";
	}
	
	if (key != 37 && key != 39) {
		if (palabra == "" || palabra == " " || palabra == "  "){
			document.getElementById("divCarpetasEncontradas").style.display = "none";
			document.getElementById("botonGuardar").disabled=false;	
		} else {
				
			if (window.XMLHttpRequest)
			{
				req = new XMLHttpRequest();
				req.onreadystatechange = procesoBuscarCarpetas;
				req.open("GET",url,true);
				req.send(null);
			} else if (window.ActiveXObject) 
					{
					req = new ActiveXObject("Microsoft.XMLHTTP");
					if (req)
					{	
						req.onreadystatechange = procesoBuscarCarpetas;
						req.open("GET",url,true);
						req.send(null);
					}
			}
		}
	}
}
function procesoBuscarCarpetas()
{
	if (req.readyState == 4)
	{
		if (req.status == 200)
		{
				
				var patron = "NO SE ENCONTRO";
				if (req.responseText.match(patron) != null){
					document.getElementById("divCarpetasEncontradas").style.display = "none";
					document.getElementById("botonGuardar").disabled=false;
				} else {
					patron = "II";
					if (req.responseText.match(patron) == 'II')
					{
						var arrayId = req.responseText.split("II");
						bajar(arrayId);		
					} else {
						document.getElementById("divCarpetasEncontradas").style.display = "";
						document.getElementById("divCarpetasEncontradas").innerHTML = req.responseText;
						document.getElementById("botonGuardar").disabled=false;
					}		
				}	
		} else {
			document.getElementById("divCarpetasEncontradas").style.display = "none";
			document.getElementById("botonGuardar").disabled=false;
		}
	}

}		

			
function bajar(arrayId)
{
	var id = Array();
	var cont = 0;
	var aux, idaux,y;
	if (key == 40) {
		
		for(y=1;y<arrayId.length-1;y++){
				id[y -1] = arrayId[y];
			    if (document.getElementById(id[y-1]).style.backgroundColor == "" || document.getElementById(id[y-1]).style.backgroundColor == "rgb(255, 255, 255)" || document.getElementById(id[y-1]).style.backgroundColor == "#ffffff"){	
					cont = cont + 1;
				}else{
				idaux = y -1;	
				aux = id[y-1];
				}
		}
		
		if (cont == id.length){
			document.getElementById(id[0]).style.backgroundColor = '#fff7ff';
			idPasar = 	id[0];
		} else if (aux != "undefined" && aux != id[id.length-1] ){
			document.getElementById(id[idaux]).style.backgroundColor = '#ffffff';
			document.getElementById(id[idaux+1]).style.backgroundColor = '#fff7ff';
			idPasar = 	id[idaux +1];
		} else if (aux == id[id.length-1]) {
			document.getElementById(id[id.length-1]).style.backgroundColor = '#ffffff';	
			document.getElementById(id[0]).style.backgroundColor = '#fff7ff';			
			idPasar = 	id[0];
		}		
		
	}
	
	if (key == 38) {
		
		for(y=1;y<arrayId.length-1;y++){
			
			 
			 id[y -1] = arrayId[y];
			 
			if (document.getElementById(id[y-1]).style.backgroundColor == "" || document.getElementById(id[y-1]).style.backgroundColor == "rgb(255, 255, 255)" || document.getElementById(id[y-1]).style.backgroundColor == "#ffffff"){	
				cont = cont + 1;
				
			}else{
				idaux = y -1;	
				aux = id[y-1];
					
			}
		}
		
		if (cont == id.length){
			document.getElementById(id[id.length -1]).style.backgroundColor = '#fff7ff';
			idPasar = 	id[id.length -1];
		} else if (aux != id[0]){
			document.getElementById(id[idaux]).style.backgroundColor = '#ffffff';
			document.getElementById(id[idaux -1]).style.backgroundColor = '#fff7ff';
			idPasar = 	id[idaux -1];
		} else if (aux == id[0]) {
			document.getElementById(id[0]).style.backgroundColor = '#ffffff';	
			document.getElementById(id[id.length -1]).style.backgroundColor = '#fff7ff';			
			idPasar = 	id[id.length -1];
		}		
		
	}
	if (key==13){
		seleccionar("hid" + idPasar); 
		document.getElementById("botonGuardar").disabled=false;
	}
}
function mostrarCartel(msj)
{
	
	if (msj == '1')
		alert("    La nota fue agregada correctamente.");
	if (msj == '2')
		alert("No se puedo agregar la nota, intentelo nuevamente.");
	
}
