/**
 * Changes Button <code>item</code> when mouse out of its area.
 * 
 * @param item the button
 */
function mouseOut(item)
{
    item.style.color = '#990000';
    item.style.backgroundColor = '#ffa72e';
}

/**
 * Changes Button <code>item</code> when mouse moves over its area.
 * 
 * @param item the button
 */
function mouseOver(item)
{
    item.style.color = '#ffffff'; 
    item.style.backgroundColor = '#ffa72e';
}

/**
 * Gets XML HTTP object depending on browser's version.
 */
function getXmlHttpObject()
{
    var xmlHttp = null;

    try
    {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    }
    catch (e)
    {
        //Internet Explorer
        try
        {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}

/**
 * Handles asynchronous HTTP response from the server.
 * If action was successful, redirects to the link provided in responseText,
 * otherwise updates web form with error messages from responseXML
 */
function handleHTTPResponse()
{
    if (httpObject.readyState == 4 && httpObject.status == 200)
    {
        if (httpObject.getResponseHeader("Content-Type") == "text/xml")
        {
            updateWebForm(httpObject.responseXML);
        }
        else
        {
            window.location = httpObject.responseText;
        }
    }
}

/**
 * Updates web form using XML with form elements and their new values to show.
 */
function updateWebForm(xmlFormElements)
{
    xmlRoot = xmlFormElements;

    if (xmlRoot.childNodes.length == 2)     /// Internet Explorer
    {                                       /// takes <xml> tag into account
        xmlRoot = xmlRoot.childNodes[1];    /// but we need real root
    }
    else                                    /// Real root matches with
    {                                       /// firstChild in Firefox and Opera
        xmlRoot = xmlRoot.firstChild;
    }

    for (var iNode = 0; iNode < xmlRoot.childNodes.length; iNode++)
    {
        var formElementNode = xmlRoot.childNodes[iNode];
        var formElementId = formElementNode.tagName;
        document.getElementById(formElementId).innerHTML =
            formElementNode.firstChild.data != null ? formElementNode.firstChild.data : '';
    }
}

/**
 * Replaces '&' and '#' characters with '_' (underline)
 *
 * @return string ready to be used as GET parameter
 */
function clear(parameterValue)
{
    parameterValue = parameterValue.replace("&", "_");
    return parameterValue.replace("#", "_");
}

/**
 * Hides HTML element if it is visible, shows it if invisible.
 */
function switchVisibility(elementId)
{
    htmlElement = document.getElementById(elementId);

	if (htmlElement.style.display == "none")
	{
	    htmlElement.style.display = "block";
	}
	else
	{
	    htmlElement.style.display = "none";
	}
}

