/** Buscador inline para la administracion */
function FIND(item) {if( window.mmIsOpera ) return(document.getElementById(item));if (document.all) return(document.all[item]);if (document.getElementById) return(document.getElementById(item));return(false);}
function moveXbySlicePos (x, img) {if (!document.layers) {var onWindows = navigator.platform ? navigator.platform == "Win32" : false;var macIE45 = document.all && !onWindows && getExplorerVersion() == 4.5;var par = img;var lastOffset = 0;while(par){if( par.leftMargin && ! onWindows ) x += parseInt(par.leftMargin);if( (par.offsetLeft != lastOffset) && par.offsetLeft ) x += parseInt(par.offsetLeft);if( par.offsetLeft != 0 ) lastOffset = par.offsetLeft;par = macIE45 ? par.parentElement : par.offsetParent;}} else if (img.x) x += img.x;return x;}
function moveYbySlicePos (y, img) {if(!document.layers) {var onWindows = navigator.platform ? navigator.platform == "Win32" : false;var macIE45 = document.all && !onWindows && getExplorerVersion() == 4.5;var par = img;var lastOffset = 0;while(par){if( par.topMargin && !onWindows ) y += parseInt(par.topMargin);if( (par.offsetTop != lastOffset) && par.offsetTop ) y += parseInt(par.offsetTop);if( par.offsetTop != 0 ) lastOffset = par.offsetTop;par = macIE45 ? par.parentElement : par.offsetParent;}} else if (img.y >= 0) y += img.y;return y;}
var msgError = '';
/**
 * function sajax_init_object
 */
function sajax_init_object() {var A;try {A=new ActiveXObject("Msxml2.XMLHTTP");} catch (e) {try {A=new ActiveXObject("Microsoft.XMLHTTP");} catch (oc) {A=null;	}}if(!A && typeof XMLHttpRequest != "undefined")A = new XMLHttpRequest();if (!A)msgError += "Could not create connection object.";return A;}
sajax_request_type = "POST";

/**
 * function sajax_do_call
 */
function sajax_do_call(func_name, args) {
	var i, x, n;
	var uri;
	var post_data;

	uri = "tools/suggest.php";
	if (sajax_request_type == "GET") {
		if (uri.indexOf("?") == -1)
		uri = uri + "?rs=" + escape(func_name);
		else
		uri = uri + "&rs=" + escape(func_name);
		for (i = 0; i < args.length-1; i++)
		uri = uri + "&rsargs[]=" + escape(args[i]);
		uri = uri + "&rsrnd=" + new Date().getTime();
		post_data = null;
	} else {
		post_data = "rs=" + escape(func_name);
		for (i = 0; i < args.length-1; i++)
		post_data = post_data + "&rsargs[]=" + escape(args[i]);
	}

	x = sajax_init_object();
	if (x != null) {
		x.open(sajax_request_type, uri, true);
		if (sajax_request_type == "POST") {
			x.setRequestHeader("Method", "POST " + uri + " HTTP/1.1");
			x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		}
		x.onreadystatechange = function() {
			if (x.readyState != 4)
			return;
			msgError += "received " + x.responseText;
	
			var status;
			var data;
			status = x.responseText.charAt(0);
			data = x.responseText.substring(2);
			if (status == "-")
			alert("Error: " + data);
			else
			args[args.length-1](data);
		}
		x.send(post_data);
		msgError = func_name + " uri = " + uri + "/post = " + post_data;
		msgError += func_name + " waiting..";
	}
	delete x;
}
/**
 * function search
 */
var objsearch = null;
var idobjsearch = 0;
var obj = null;

function resaltarSearch(id) {
	try {
		
		document.getElementById("s_" + idobjsearch).className = "suggest";
		document.getElementById("ss_" + idobjsearch).className = "aproxsuggest";
		document.getElementById("s_" + id).className = "suggesthover";
		document.getElementById("ss_" + id).className = "aproxsuggesthover";
		idobjsearch = id;
		
	} catch(e) {
		
		document.getElementById("s_" + idobjsearch).className = "suggesthover";
		document.getElementById("ss_" + idobjsearch).className = "aproxsuggesthover";
		
	}
}

function suggest(name, e) {
	/** Miramos el tipo de tecla que oprimio */
	var code;
	if(typeof e == "undefined") e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
		
	if (code != 37 && code != 38 && code != 39 && code != 40 && code != 13) {
		obj = FIND(name) || document.images[name] || document.links[name] || document.anchors[name];
		idobjsearch = 0;
		
		if (obj.value != '') {
			x = moveXbySlicePos (0, obj);
			y = moveYbySlicePos (20, obj);
			objsearch.style.top = y+"px";
			objsearch.style.left = x+"px";
			x_search(obj.value, listResults);
		} else {
			objsearch.style.display = 'none';
		}
	
	}
	// onblur
	obj.onblur = function() {
		objsearch.onmouseover = function () {
			objsearch.style.display = '';
			//setTimeout ("objsearch.style.display = 'none';", 100); 
		}
		objsearch.onmouseout = function () {
			objsearch.style.display = 'none';
		}
	}
	// onkeydown
	obj.onkeydown = function (e) {
		var code;
		
		if(typeof e == "undefined") e = window.event;
		if (e.keyCode) code = e.keyCode;
		else if (e.which) code = e.which;
		
		if (objsearch.style.display == '') {
			// 38 up arrow key
			if (code == 38) {
		    	resaltarSearch(idobjsearch - 1);
		    	return false;
		    }
		    // 40 down arrow key 
		    if (code == 40) {
		    	resaltarSearch(idobjsearch + 1);
		    	return false; 
		    }
		    // 13 return key
		    if (code == 13) {
		    	document.getElementById('s_'+idobjsearch).onclick();
		    	return false;
		    }
		    // 9 tab key
		    if (code == 9) {
		    	document.getElementById('s_'+idobjsearch).onclick();
		    	return false;
		    }
   		    // 27 esc key
		    if (code == 27) {
		    	obj.onkeyup = function() {};
		    	objsearch.style.display = 'none';
		    }
		}
	}
	objsearch.onmouseover = null;
	objsearch.onmouseout = null;
	return true;		
}
/**
 * 
 */
function recuadro(id) {
	template = "<div id="+id+" style=\"width:235px;position:absolute;display:none;padding:2px;background:#fff;border:1px solid #000;overflow:auto\"></div>";
	document.write(template);
	objsearch = FIND(id);
}

// wrapper for search		
function x_search() {sajax_do_call("suggest", x_search.arguments);}
/**
 * Arma el listado y saca el ancho maximo para cuadrar el recuadro
 */
function list(arreglo) {
	var queryAux = obj.value.toLowerCase();
	var infoArr;
	var k;
	var htmlContent = '';
	var ancho = 0;
	var idconsec = 0;
	
	if (queryAux != '') {
		for(k in arreglo) {
			infoArr = arreglo[k].split("|*");
			
			htmlContent += "<a id=\"s_"+idconsec+"\" class=\"suggest\" onmouseover=\"resaltarSearch("+idconsec+");\" onclick=\"document.getElementById('"+obj.id+"').value='"+infoArr[0]+"';objsearch.style.display = 'none';\"><span id=\"ss_"+idconsec+"\" class=\"aproxsuggest\">~"+infoArr[2]+" resultados</span>"+infoArr[1]+"</a>";
			if (ancho < arreglo[k].length) {
				ancho = arreglo[k].length;
			}
			idconsec++;
		}
		if (htmlContent != '') {
			objsearch.innerHTML = htmlContent;
		} else {
			objsearch.innerHTML = "";
			objsearch.style.display = 'none';
		}
	} else {
		objsearch.innerHTML = "";
		objsearch.style.display = 'none';
	}
	return ancho;
}
/**
 * Lista los resultados
 */
function listResults(result) {
	/**
	 * Verificamos que el resultado sea vacio
	 */
	if (result == "") {
		objsearch.style.display = 'none';
		return;
	}
	
	var arreglo = result.split("|-");
	
	ancho = list(arreglo);
	objsearch.style.display = '';
	
	if (arreglo.length <= 10) {
		if(arreglo.length == 1 && ancho > 85) {
			objsearch.style.height = "40px";			
		} else {
			objsearch.style.height = (20 * arreglo.length)+"px";	
		}
	} else {
		objsearch.style.height = "200px";
	}
	
	if (objsearch.style.display == '') {
		resaltarSearch(0);
	}
}
