/**
 * Fernand Braudel Institute intranet javascript functions
 *
 * Some functions where based on AJAX: Getting Started - 
 * http://developer.mozilla.org/en/docs/AJAX:Getting_Started 
 *
 * 2006 Gustavo R. Montesino
 * Copyright (C) 2006 Instituto Fernand Braudel de Economia Mundial
 **/

var status_timeout = false; /* Hold the timeout handle to clean the status box */

if (window.location.href.match('~gustavo'))
	baseurl = "http://servidor:8080/~gustavo/intranet";
else
	baseurl = "http://servidor:8080/intranet";

/**
 * isAjaxCapable(): Determines if the current browser can do AJAX properly
 *
 * Returns: True if AJAX is available, false otherwise
 **/
function isAjaxCapable()
{
	if (window.XMLHttpRequest)
	{
		return true;
	}
	else if (window.ActiveXObject)
	{
		var http_request = false;
		
		try
		{
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e)
			{
				return false;
			}
		}
		if (http_request)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		return false;
	}
}

/**
 * postRequest(): Request a page through post
 *
 * url: url of the page to request
 * data: data to send
 * callback: function to call on state changes
 *
 * Returns: request object on success, false otherwise
 **/
function postRequest(url, data, callback)
{
	var request = newRequest();

	if (!request)
	{
		return false;
	}

	request.open('POST', url, true);
	request.setRequestHeader("content-type", "application/x-www-form-urlencoded; charset=ISO-8859-1");
	request.send(data);
	request.onreadystatechange = callback;
	return request;
}

/**
 * getRequest(): Request a page through get
 * 
 * url: url of the page to request
 * callback: function to call on state changes
 *
 * Returns: True on success
 **/
function getRequest(url, callback)
{
	var request = newRequest();

	if (!request)
	{
		return false;
	}

	request.onreadystatechange = callback;
	request.open('GET', url, true);
	request.send(null);
	return true;
}

/**
 * newRequest(): Creates a new request object
 *
 * Returns: The newly-created object
 **/
function newRequest()
{
	http_request=false;

	if (window.XMLHttpRequest)  /* Navegadores não-MS */
	{
		http_request = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) /* Navegadores MSIE */
	{
		try
		{
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e) {}
		}
	}

	return http_request;
}

/**
 * statusmsg(): Prints a status message
 *
 * msg: Message to print
 **/
function statusmsg(msg)
{
	showmsg(msg, 'status', 'status');
}

/**
 * errormsg(): Print a error message
 *
 * msg: Message to print
 **/
function errormsg(msg)
{
	showmsg(msg, 'status', 'error');
	window.alert(msg);
}

/**
 * showmsg(): Show a message to the user.
 *
 * msg: Message to show
 * div: id of the div element to show the msg on
 * divclass: class to set the status div to
 **/
function showmsg(msg, div, divclass)
{
	statusdiv = document.getElementById(div);
	if (statusdiv)
	{
		textnode = firstTextChild(statusdiv);
		if (textnode.nodeValue)
			textnode.nodeValue = msg;
		else
		{
			textnode = document.createTextNode(msg);
			statusdiv.appendChild(textnode);
		}
		statusdiv.className = divclass;
	}
}

/**
 * status_clear_schedule(): Schedule the cleaning of the status box in 1 sec.
 **/
function status_clear_schedule()
{
	status_timeout = setTimeout("status_clear()", 1000);
}

/**
 * status_clear(): Hide the status box
 **/
function status_clear()
{	
	clearTimeout(status_timeout);
        status_timeout = false;
	
	document.getElementById("status").className = "hidden";
}

/**
 * firstTextChild(): Gets the first text child of an element
 *
 * Element: element
 *
 * Returns: First text child or false
 **/
function firstTextChild(Element)
{
	first = Element.firstChild;

	while ((first) && (first.nodeType != 3))
	{
		first = first.nextSibling;
	}

	if ((first) && (first.nodeType == 3))
		return(first);
	else
		return false;
}

/**
 * getElementText(): Returns the text being show currently in an element
 *
 * Returns: Text of the first text child element
 **/
function getElementText(Element)
{
	TextElement = firstTextChild(Element);

	if (TextElement)
		return(TextElement.nodeValue);
	else
		return("");
}

/**
 * getSelectValue(): Get the value of a select element
 *
 * Needed to work around a IE bug, which doesn't reads select.value
 * Note that reading option.value also fails in IE if the value is
 * the default one, ie, the same as option.text :(
 *
 * @select: Target select element
 *
 * Returns: the (first) selected option value
 **/
function getSelectValue(select)
{
	for (var i = 0; i < select.options.length; i++)
	{
		if (select.options.item(i).selected)
		{
			value = select.options.item(i).value;

			if (value == "")
				value = select.options.item(i).text
				
			return value;
		}
	}
}

/**
 * setSelectValue(): Sets the value of a select element
 *
 * Set the value of a select element and asserts the right option
 * is selected.
 *
 * @ select: target select
 * @ value: new value;
 *
 * Return: True on success, false if there is no option with the @value
 **/
function setSelectValue(select, value)
{
	select.value = value;

	for (var i = 0; i < select.options.length; i++)
	{
		curopt = select.options.item(i);

		if (curopt.value == value)
		{
			curopt.selected = true;
			return true;
		}
	}

	/* If we get here, no suitable option was found. Search on option texts instead */
	for (var i = 0; i < select.options.length; i++)
	{
		curopt = select.options.item(i);

		if (getElementText(curopt) == value)
		{
			curopt.selected = true;
			return true;
		}
	}

	/* The option dossn't seems to exist... */
	return false;
}

/**
 * getXmlError(): Returns the content of the first err element child of root.
 *
 * root: xml node
 *
 * returns: Contents of the first err node, or false if none is found
 **/
function getXmlError(root)
{
	err = root.getElementsByTagName("err")
	if (err.length != 0)
		return getElementText(err.item(0));
	else
		return false;
}
