/****************************/
/* Common utility functions */
/****************************/

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

/* Find the position of a DOM Element */

function findPosition(obj) {
	var curleft = 0;
    var curtop = 0;
    if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft, curtop];
}

/* Get the scroll offset of the window */

function getScrollXY() {
    var scrOfX = 0, scrOfY = 0;
    if( typeof( window.pageYOffset ) == 'number' ) {
        //Netscape compliant
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        //DOM compliant
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }
    return [ scrOfX, scrOfY ];
}

function cancelBubbling(e) {
  if (!e) {
    var e = window.event
  }
  e.cancelBubble = true
  if (e.stopPropagation) {
    e.stopPropagation()

  }
}

/************************/
/* Global DOM Functions */
/************************/

/* Search for parents node by tagname */
function findParentNodeByTagName(obj, tagName) {

	do {
		obj = obj.parentNode;
	}  while (obj.nodeName != tagName && obj.nodeName != 'BODY')

	return (obj)
}

/* Search parents for element with specific classname */
function findParentNodeByClassName(obj, className) {

	do {
		obj = obj.parentNode;
	}  while ((obj.className.indexOf(className) == -1) && obj.nodeName != 'BODY')

	return (obj)
}

/* Search children for element(s) with specific classname */
function getElementsByClassName(obj, className, looseSearch) {
    /*
    obj = the object to search from;
    classname = the name of the class to search for;
    looseSearch = (default:true), if true then use indexOf ; if false then strict compare;
    */

    var list = new Array();
    var childrenList = obj.getElementsByTagName('DIV');
    var searchCriterium;

    if(!looseSearch)
        searchCriterium = "childrenList[i].className == className";
    else
        searchCriterium = "childrenList[i].className.indexOf(className) != -1";

    for(var i=0; i < childrenList.length; i++) {
        if(eval(searchCriterium)) {
            list.push(childrenList[i]);
            }
    }
    return (list);
}

/************************/
/* More specific Functions */
/************************/

function mailAuthor(obj) {
	var form = obj.parentNode;
	var msg = "";

	if (form.Leserbrief.value == '' && !form.Leserbrief.value) {
		msg = msg + "<li>Das Feld 'Leserbrief' muss ausgefüllt werden.</li>";
	}
	if (form.Name.value == '' && !form.Name.value) {
		msg = msg + "<li>Das Feld 'Ihre Name' muss ausgefüllt werden.</li>";
	}
	if (form.Absender.value == '' && !form.Absender.value) {
		msg = msg + "<li>Das Feld 'E-Mail Absender' muss ausgefüllt werden.</li>";
	}
	if (form.Website.value == '' && !form.Code.value) {
		msg = msg + "<li>Das Feld 'Code' muss ausgefüllt werden.</li>";
	}
	if (msg != '') {
		var errdiv = document.getElementById("formErrors");
		var ul = document.createElement("ul");
		ul.innerHTML = msg;
		if(errdiv.firstChild) {errdiv.removeChild(errdiv.firstChild)}	// Clean the error div
		errdiv.appendChild(ul);
		alert('false')
		return false;
	}
	form.submit();
	return false;
}

function switchTab (theObj) {
    /* first check if object is already active */
    if(findParentNodeByTagName(theObj, 'LI').className.indexOf('active') != -1) {
        return false;
    }

    /* root element of the tabs container */
    var tabsContainerObj = findParentNodeByClassName(theObj, 'tabbedPanes');
    /* root element of the tabs group */
    var tabsObj = findParentNodeByClassName(theObj, 'tabs');

    /* list of tab buttons */
    var tabItems = findParentNodeByClassName(theObj, 'tabs').getElementsByTagName('A');
    /* list of tab panes */
    var paneList = getElementsByClassName(tabsContainerObj, 'pane', true);

    /* find out the order number of the selected tab */
    var position;

    for (var i=0; i < tabItems.length; i++ ) {
        if (tabItems[i] == theObj) {
            position = i;
            findParentNodeByTagName(tabItems[i], 'LI').className = 'active';
            //theObj.parentNode.className = 'active';
            //alert(theObj.nodeName + "==");
        }
        else {
            findParentNodeByTagName(tabItems[i], 'LI').className = '';
            //alert(theObj.nodeName);
        }
    }

    for (var i=0; i < paneList.length; i++ ) {
        if (i == position) {
            paneList[i].className += ' active';
        }
        else {
            paneList[i].className = paneList[i].className.replace(' active','');
        }
    }
}



