/*
    Usage: set onclick="showHide('idOfYourDiv');" on any tag
    this will now show or hide idOfYourDiv.
    
    use showHideOnLoad('idOfYourDiv', 'inloggning misslyckades') when page loads
    this will show 'idOfYourDiv' if the text 'inloggning misslyckades' exist
*/

function showHide(element)
{
    
    var e = document.getElementById(element);
    
    if (e.style.display == "none" || e.style.display == "")
    {
        e.style.display = "block";
        opacity(element, 1, 100, 500);
    }
    else
    {
        opacity(element, 100, 1, 500);
        e.style.display = "none";
    }

}
/*
    private function
*/
function checkText(e, tag, textToCheck)
{
    var childNodes = e.getElementsByTagName(tag);
    
    //Check if theres many h3 tags
    if(undefined != childNodes.length)
    {
    	for (i = 0; i < childNodes.length; i++)
    	{
    	    if (childNodes[i].innerHTML == textToCheck)
    	    {
    	        return true;
    	    }
    	    else return false;
    	}
    }
    //only one h3 tag
    else
    {
    	if (childNodes.innerHTML == textToCheck)
    	{
    	    
    	    return true;
    	}
    	return false;
    }
}

function showHideOnLoad(element, tag, text)
{
    var e = document.getElementById(element);
    if (checkText(e, tag, text))
    {
        e.style.display = "block";
    	opacity(element, 1, 100, 500);
    }
}


function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
} 
