
// Delete the spaces at the begining and at the end of a string
function trim(myString) 
{ 
	return myString.replace(/^\s+/g,'').replace(/\s+$/g,'');
} 

// Init the page
function initPage()
{
	if($('query') != null)
	{
		// Give the focus to the query field
		$('query').focus();
	}
}

// Is executed when the user press a key and when the query field get focus
function newQuery(event)
{
	// Codes for the arrow keys
	var UP = Event.KEY_UP;
	var DOWN = Event.KEY_DOWN;
	var ENTER = Event.KEY_ENTER;
	// Code of the pressed key
	var key = event.keyCode;
	// Event.isLeftClick(event)
	// If the enter key is pressed
	if(key == ENTER)
	{
		//sendSearchForm();
		hideResults();
		return;
	}
	// If an arrow key is pressed
	if((key==DOWN || key==UP) && queryResultCount > 0)
	{
		// Increment the div id
		if(key==DOWN && queryResultId<queryResultCount)
		{
			// Reset the old div CSS class
			if(queryResultId >= 1)
			{
				setInactiveResults();
			}
			queryResultId++;
		}
		// Decrement the div id
		if(key==UP && queryResultId>1)
		{
			// Reset the old div CSS class
			if(queryResultId <= queryResultCount)
			{
				setInactiveResults();
			}
			queryResultId--;
		}
		// Change the div CSS class
		setActiveResult(queryResultId);
		//$("query").value = getNodeContent($("result_"+queryResultId));
	}
	else
	{
		// Throw the ajax request
		loadQueryResults();
	}
}

// Set a line active in the query result list
function setActiveResult(id)
{
	$("result_"+id).className = "activeQueryResult";
	queryResultId = id;
	$("query").value = getNodeContent($("result_"+queryResultId));
}

// Set a line active in the query result list
function setInactiveResults()
{
	var root = $("queryResults").childNodes;
	// For each child nodes
	for(var i=0;i<root.length;i++)
	{
		root[i].className = "queryResult";
	}
}

// Hide the resuls box
function hideResults()
{
	if($("queryResults") != null)
	{
		$("queryResults").style.display = 'none';
	}
}


// Clear the printed category
function clearCategory()
{
	$("printCategory").update("&nbsp;");
}

// Set the current category
function setCategory(category,percent)
{
	$("printCategory").update(category+" ("+percent+"%)");
}

// Load a category
function loadCategory(category)
{
	alert('Prêt à charger la catégorie : '+category+'.');
}

// Javascript vars
var queryResultId = 0;
var queryResultCount = 0;

// HTML entities transformation
function urlencode(str) {
	// Vars                  
	var histogram = {}, histogram_r = {}, code = 0, tmp_arr = [];
	var ret = str.toString();
	var replacer = function(search, replace, str) {
    	var tmp_arr = [];
    	tmp_arr = str.split(search);
    	return tmp_arr.join(replace);
	};
	// The histogram is identical to the one in urldecode.
	histogram['!']   = '%21';
	histogram['%20'] = '+';
	// Begin with encodeURIComponent, which most resembles PHP's encoding functions
	ret = encodeURIComponent(ret);
	for(search in histogram) {
    	replace = histogram[search];
    	ret = replacer(search, replace, ret) // Custom replace. No regexing
	}
	// Uppercase for full PHP compatibility
	return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
    	return "%"+m2.toUpperCase();
	});
	return ret;
}

// Get a DOM node content
function getNodeContent(node)
{
	if(navigator.appName=="Microsoft Internet Explorer")
	{
		return node.text;
	}
	else
	{
		return node.textContent;
	}
}

// Hide the adding form
function hide_add_form()
{
	$("add_form").style.display = "none";
}