function llamadaSujest() {
	cadena = document.getElementById('cadena').value;
	id = document.getElementById('idsu').value;
	if (cadena != palabra) {
		if (cadena.length > 3 ) {
			palabra = cadena;
		    httpSujest.open('get', '/suggest/index/cadena/'+cadena+'/id/'+id);
		    httpSujest.onreadystatechange = handleResponseInsertSujest;
		    httpSujest.send(null);
		}
		else {
			var ni = document.getElementById('suggestlist');
			if (ni.style.display != 'none') {
				ni.style.display = 'none';
			}
		}
	}
}

function handleResponseInsertSujest() {

    if(httpSujest.readyState == 4){
        var response = httpSujest.responseText;

 	    var ni = document.getElementById('suggestlist');
	    ni.innerHTML = response;
	    ni.style.display = "";
   }
}

function handleArrowKeys(evt) 
{
	var lists = document.getElementById("suggestlist");
	var listli = lists.getElementsByTagName("li");

	if (listli.length > 0) {
		
		if (lists.style.display != 'none') {
		
		    evt = (evt) ? evt : ((window.event) ? event : null);
		    if (evt) {
		        switch (evt.keyCode) {
		            case 38: 				// up
						if (posicion >= 1)	{
							enliNext = document.getElementById("li"+posicion);
				          	enliNext.style.background = '#f8f5eb';
				          	if (posicion > 1) {
			            		posicion--;
			            	}
			            	else if (posicion == 1) {
			            		posicion = listli.length; 
			            	}
			            	enliCurrent = document.getElementById("li"+posicion);
			            	enliCurrent.style.background = '#dddddd';				          	
			          	}
		                break;
		            case 40:		        //down          
			           if (listli.length >= posicion) {
			           		if (posicion != 0) {
								enliPrev = document.getElementById("li"+posicion);
				          		enliPrev.style.background = '#f8f5eb'; 	           
				          	}
							if (listli.length > posicion) {
				            	posicion++;
			            	}
			            	else if (posicion == listli.length)  {
				            	posicion = 1;
			            	}

			            	enliCurrent = document.getElementById("li"+posicion);
			            	enliCurrent.style.background = '#dddddd';
						}
						
		                break;
		            case 13:
		            	if (posicion > 0) 
		            	{
		            		enliCurrent = document.getElementById("li"+posicion);
							document.location.href = enliCurrent.firstChild;
		            	}
		            	break; 
		         }
		         return false;
		    }
		}
	}
}

function checkBuscador()
{
	limpia = trim(document.getElementById('cadena').value, " \0\n\r\t")
	lon = limpia.length;
	if (lon > 2) {
		return true;
	}
	else {
		alert('La búsqueda tiene que tener más de 2 carácteres');
		return false; 
	}
}


function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}


function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}