﻿/* ************************************************************************ */
/* This JavaScript file contains  a collection of functions for working     */
/* with the DOM.                                                            */
/* Assembled/written by David Haglund (haagel.se)                           */
/*     (though some of the functions are originally developed by others)    */
/* Last update to this file: 2009-07-31                                     */
/* ************************************************************************ */




/* Add an event handler to an element.                                      */
/* element: The DOM element to attach the handler to (element-object).	    */
/* eventType: Type of event (string) (i.e. "load", "mouseover").			*/
/* func: The function that should handle the event (function reference).	*/
/* useCapture: ??? (bool).													*/
/* Example: addEvent(window, 'load', initiateMyLogic, false);               */
function addEvent(element, eventType, func, useCapture) 
{
    if (element.addEventListener) 
    {
        //W3C DOM.
        element.addEventListener(eventType, func, useCapture);
    }
    else if (element.attachEvent) 
    {
        //IE.
        element.attachEvent('on' + eventType, func);
    }
    else 
    {
        //Old browsers.
        element['on' + eventType] = func;
    }
}




/* Remove an event handler from an element.                                 */
/* element: The DOM element that has an event handler (element-object).	    */
/* eventType: Type of event (string) (i.e. "load", "mouseover").			*/
/* func: The function that is the event handler (function reference).		*/
/* useCapture: ??? (bool).													*/
/* Example: removeEvent(window, 'load', initiateMyLogic, false);            */
function removeEvent(element, eventType, func, useCapture) 
{
    if (element.removeEventListener) 
    {
        //W3C DOM.
        element.removeEventListener(eventType, func, useCapture);
    }
    else if (element.detachEvent) 
    {
        //IE.
        element.detachEvent('on' + eventType, func);
    }
    else 
    {
        //Old browsers -> Hack solution (redirect to a function that does nothing).
        element['on' + eventType] = dummyFunction;
    }
}




/* A function that does nothing. */
function dummyFunction(e) 
{
    //DO NOTHING!
}




/* Add a class (className:string) to an HTML element.                       */
/* Does not affect classes that that the element already have.              */
function addClass(element, className)
{
	if(!element.className)    //If the element doesn't have a class applied...
	{
	    element.className = className;
	}
	else
	{
	    if (element.className.indexOf(className) < 0) //If the element doesn't already have the class...
	    {
	        //Append the class.
	        var newClassName = element.className;
	        newClassName += ' ' + className;
	        element.className = newClassName;
	    }
	}
}




/* Remove a class (className:string) from an HTML element.                  */
/* Does not affect  other classes that that the element might have.         */
/* If the element doesn't have the class, nothing happens.                  */
function removeClass(element, className)
{
	var currentClassName = element.className;
	var newClassName;
	var position = currentClassName.indexOf(className);
	
	if(position != -1)    //It the element has the class...
	{
		if(currentClassName == className)    //If the element ONLY has this class...
		{
			newClassName = '';
		}
		else
		{
			//The element has several classes applied.
			
			if(position == 0)    //If the class to remove is written first in the list...
			{
				//Remove the class including a white space after.
				newClassName = currentClassName.replace(className+' ', '');
			}
			else
			{
				//Remove the class including a white space before.
				newClassName = currentClassName.replace(' '+className, '');
			}
		}
		
		element.className = newClassName;
	}
}


function toggleClass(element, className) 
{
    if (!element.className)    //If the element doesn't have a class applied...
    {
        addClass(element, className);
    }
    else 
    {
        if (element.className.indexOf(className) < 0) //If the element doesn't already have the class...
        {
            addClass(element, className);
        }
        else 
        {
            removeClass(element, className);
        }
    }
}




/* Returns a reference to the element which generated an event.             */
/* e: A reference to the event object that is supplied by some browsers.    */
/* If no element could be found, null is returned.						    */
function getTargetElement(e) 
{
    var element;

    if (window.event && window.event.srcElement) 
    {
        //IE
        element = window.event.srcElement;   
    }
    else if (e && e.target) 
    {
        //W3C
        element = e.target;   
    }
    else 
    {
        //Could not get the element.
        element = null;
    }
    
    return element;
}




/* This function takes a DOM element and an element type (target:string).   */
/* It climbs up the DOM tree from the element and upwards until it finds    */
/* an element with the requested element type. This element is returned.    */
/* If the root of the DOM tree ('html') is found and the requested element  */
/* type still haven't been found, null is returned.                         */
function ascendDOM(element, target) 
{
    target = target.toLowerCase();
    while (element.nodeName.toLowerCase() != target && element.nodeName.toLowerCase() != 'html') 
    {
        element = element.parentNode;
    }
    return (element.nodeName.toLowerCase()) == 'html' ? null : element;
}




/* Returns an array with elements that has a specific class.                */
/* searchClass: The name of the class (string)                              */
/* node: The parent node - only childnodes is searched (element-object)     */
/*       (If node is set to null, document will be used)                    */
/* tag: The name of the tags to search (string)                             */
/*      (If tag is null, all tags will be searched)                         */
function getElementsByClass(searchClass, node, tag) 
{
    var classElements = new Array();
    
    if (node == null) 
    {
        node = document;
    }
    if (tag == null) 
    {
        tag = '*';
    }
    
    var els = node.getElementsByTagName(tag);
    var elsLen = els.length;
    var pattern = new RegExp('(^|\\\\s)' + searchClass + '(\\\\s|$)');
    
    for (i = 0, j = 0; i < elsLen; i++) 
    {
        if (pattern.test(els[i].className)) 
        {
            classElements[j] = els[i];
            j++;
        }
    }
    
    return classElements;
}
//NOTE: This doesn't seem to work very well if the element has several classes...



/* Returns a reference to the element which generated an event.             */
/* Prevent the default action that the browser has for some events.         */
/* Example: follow links when they are clicked                              */
function preventDefaultAction(e) 
{
    if (window.event)    
    {
        //IE
        window.event.returnValue = false;
    }
    if (e && e.preventDefault)    
    {
        //W3C
        e.preventDefault();
    }
}




/* Removes all childnodes of the specified element/node.                    */
function removeAllChildren(element) 
{
    while (element.childNodes.length > 0) 
    {
        element.removeChild(element.firstChild);
    }
}




/* Check whether the specified DOM element has a specific class.            */
/* element: The DOM element to check.                                       */
/* className: The className (string) to look for.                           */
/* Returns true if the element has the class, otherwise false.              */
function hasClass(element, className) 
{
    var elementHasClass = false;
    var allClasses = element.className;

    if (allClasses == className) 
    {
        //The specified class is the only class the element has ("myClass").
        elementHasClass = true;
    }
    else if (allClasses.substr(0, className.length + 1) == className + ' ') 
    {
        //The specified class is the first class in a list of classes that the element has ("myClass ..."). 
        elementHasClass = true;
    }
    else if (allClasses.substr(allClasses.length - (className.length + 1), className.length + 1) == ' ' + className) 
    {
        //The specified class is the last class in a list of classes that the element has ("... myClass").
        elementHasClass = true;
    }
    else if (allClasses.indexOf(' ' + className + ' ', 0) >= 0) 
    {
        //The specified class is in the middle of a list of classes that the element has ("... myClass ...").
        elementHasClass = true;
    }
    else 
    {
        //The element does NOT have the class.
        elementHasClass = false;
    }

    return elementHasClass;
}




//Flytta
function isInteger(text)
{
    var validChars = '0123456789';

    var isInteger = true;

    var startIndex = 0;
    if (text.charAt(0) == '-') 
    {
        //The first character might be a minus sign.
        startIndex = 1;
    }

    for (i = startIndex; i < text.length; i++)
    {
        var charToTest = text.charAt(i);
        if (validChars.indexOf(charToTest, 0) == -1)
        {
            isInteger = false;
            break;
        }
    }
    
    return isInteger;
}
