function changeURL(urlField) {
	if (urlField.value != null && urlField.value.length > 0) {
		document.location.href = urlField.value;
	}
}

/*
 * Sets the focus on an input
 * Pass the form element and the index of the input field you want to set the focus on
 * Note: Start with index 0 for the first input field 
 */
function setFocus() {
	 var focusData = document.getElementById('focusData')
	 if (focusData) {
		var data = focusData.value.split(";");
		var form = document.getElementById(data[0]);
		form[data[1]].focus();
	}
}

 /*
  * Functions for determining the correct offset.
  * This is just an IE6 workaround since offsetTop and offsetLeft
  * should actually contain the values returned by this functions
  */
 
 function findPosX(obj)
 {
   var curleft = 0;
   if(obj.offsetParent)
       while(1) 
       {
         curleft += obj.offsetLeft;
         if(!obj.offsetParent)
           break;
         obj = obj.offsetParent;
       }
   else if(obj.x)
       curleft += obj.x;
   return curleft;
 }

 function findPosY(obj)
 {
   var curtop = 0;
   if(obj.offsetParent)
       while(1)
       {
         curtop += obj.offsetTop;
         if(!obj.offsetParent)
           break;
         obj = obj.offsetParent;
       }
   else if(obj.y)
       curtop += obj.y;
   return curtop;
 }

 
function openSmartEditor(url){
	alert(url);
	window.open(url,'Editor', 'width=750,height=500,menubar=no,location=no,resizable=yes,top=0,left=0');
}
 
function refreshByEditor(id, locale, userobject) {
	var url = document.location.href;
	if (userobject != null && userobject.length > 0) {
		if (url.indexOf("?")>0) {
			url+="&";
		} else {
			url+="?";
		}
		url+="userobject="+userobject;
	}
	document.location.href=url;
}

function callQuicknavLink(select){
	var value = select.options[select.selectedIndex].value;
	var data = value.split("##");
	var link = data[0];
	var target = data[1];
	
    if (target=="_top") {
        top.location.href=link;
    } else if (target=="_self") {
        self.location.href=link;
    } else if (target=="_blank") {
        window.open(link,"window"+select.selectedIndex);
    } else if (target=="_parent") {
        parent.location.href=link;
    } else { // falls target=="_notarget"
        self.location.hash = link;
    }
}

function checkForm(elem, errorMessage) {
	var form = getParentForm(elem);
	var numOfElements = form.length;
	var showError = false;
    for (var i = 0; i < numOfElements; i++) {
        currentElement = form.elements[i];
        //only check elements which have a class value 'mandatory'
        if (currentElement.className != null && (currentElement.className.indexOf("mandatory") != -1)) {
        	 //div could contain a mandatory marker as well, but actually can't be mandatory, so ignore divs
        	if (currentElement.tagName!='DIV') {
            	/*If current element is something which is 'checked' rather than having a value, determine whether one of the elements with
            	 * the same name was checked, to pass the validation.
            	 */
            	if(currentElement.type == "checkbox" || currentElement.type == "radio"){
            		if(!isSomethingChecked(currentElement.name)){
            			showError = true;
            		}
            	}else if (currentElement.value == null || currentElement.value == "") {
            		showError = true;
                }
            }
        }
    }
    if(showError){
    	alert(errorMessage);
    }else{
    	form.submit();
    }
}
/*
 * Retrieves all elements with the given name and checks whether one of them was checked
 */
function isSomethingChecked(elemName){
	var elems = document.getElementsByName(elemName);
	var elem;
	var checked = false;
	for(var i = 0; i < elems.length; i++){
		elem = elems[i];
		if (elem.checked){
				checked = true;
				break;
		}
	}
	return checked;
}

//Aufrufender Link: <a href="javascript:OpenBrWindow2(800,600,'dateiname.htm','fenstername')">Link</a>
// Mit Menubar (Drucken) und Scrollbars
 
function OpenWindow(x,y,theURL,winName) {
	var x;
	var y;
	ScreenWidth = screen.width;
	ScreenHeight = screen.height;
	xpos = (ScreenWidth/2)-(x/2);
	ypos = (ScreenHeight/2.3)-(y/2);
	features = "width=" + x + ",height=" + y + ",location=no,menubar=yes,personalbar=no,resizable=no,screenX=" + xpos + ",screenY=" + ypos + ",left=" + xpos + ",top=" + ypos + ",locationbar=no,scrollbars=yes,directories=no,statusbar=no,toolbar=no";
	window.open(theURL,winName,features);
} 

/* Due to the use of <a> tags instead of buttons we need for some forms hidden fields that contain the value previously set by the name of the button*/

function createHiddenField(formName, fieldName, value){
	$('#' + formName).append('<input type="hidden" id="' + formName + '_' + fieldName + '" name="' + fieldName + '" value="' + value + '" />');
}

function removeHiddenField(formName, fieldName){
	$('#' + formName + '_' + fieldName).remove();
}

/*
 * Iterates over all elements within the content,
 * determins the one with the biggest width and
 * sets the center div to this width
 */
function setContentWidth() {
	var content = document.getElementById("contentcolumn");
	if(content){
	    var items = content.childNodes;
	    var i = items.length;
	    var item;
	    var contentwidth = 0;
	    if(i > 0){
		    do
		    {
		      item = items[i - 1];
		      var offsetWidth = item.offsetWidth;
		      if(offsetWidth > contentwidth){
		    	  contentwidth = offsetWidth;
		      }
		    }
		    while (--i);
	    }
	    var center = document.getElementById("center");
	    var navigation = document.getElementById("navigation");
	    var newCenterWidth = navigation.offsetWidth + contentwidth + 25; //add 25px padding
	    if(center && center.offsetWidth <= newCenterWidth){
	    	center.style.width = newCenterWidth + 'px';
	    }
	}
}

/*
 * set height of element with id toResize to height of element with id heightElement only if less tha thar
 * if an additional element is given and defined its height is substracted from the new height
 */
function setElementHeight(toResize, heightElement) {
   var element1 = document.getElementById(toResize);
   var element2 = document.getElementById(heightElement);
   // both elements defined
   if ((element1 != undefined) && (element2 != undefined)) {
	    var offset = 0;
	    if (arguments.length > 2) {
	    	// additional element for offset given and defined: set offset
	    	offsetElement = document.getElementById(arguments[2]);
	    	if (offsetElement != undefined) offset = offsetElement.offsetHeight;
	    }
	    // set new height as style
	    var newElementHeight = element2.offsetHeight-offset;
		if (element1.offsetHeight < newElementHeight) {
			element1.style.height = newElementHeight + 'px';
	    }
   }
}

/*
 * Function for adding a div above each iframe found in the content.
 * Unfinished and unused currently.
 */

function addLoadAnimation(){
	var iframes = document.getElementsByTagName("iframe");
	for (var i = 0; i < iframes.length; i++) {
		//register onload
		var hideAnim = 'this.previousSibling.style.display="none";';
		iframes[i].onload = new Function('', hideAnim);
		var posX = findPosX(iframes[i]);
		var posY = findPosY(iframes[i]);
		var animDiv = document.createElement("div");
		animDiv.setAttribute("class", "animDiv");
		animDiv.style.position = 'absolute';
		animDiv.style.top = iframes[i].offsetTop; 
		animDiv.style.left = iframes[i].offsetLeft;
		var textNode = document.createTextNode("loading...");
		animDiv.appendChild(textNode);
		iframes[i].parentNode.insertBefore(animDiv, iframes[i]);
	}
}
 
 function getParentForm(elem){
		var checkElem = elem;
		while(checkElem.tagName != "FORM" && checkElem.tagName != "BODY"){
			checkElem = checkElem.parentNode;
			}
		if (checkElem.tagName == "FORM") {
				return checkElem;
			}
		else {
			return null;
			}
	}
 
 function resetForm(elem){
	 var form = getParentForm(elem);
	 form.reset();
 }
 
 /*
  * Appends a CSS class to class attribute of given element.
  * 
  * @param elem element to appends class attribute to @param className CSS
  * class to append
  */
 function cAddClass(elem, className) {
  	var elemClassName = elem.className;
  	if (elemClassName == null){
  		elem.className = className;
  		return;
  	}
  	if (elemClassName.indexOf(className) == -1) {
  		elem.className = elem.className + ' ' + className;
  	}
 }

  /*
  * Removes CSS class from class attribute of given element.
  * 
  * @param elem element to remove class attribute from @param className CSS
  * class to remove
  */
 function cRemoveClass(elem, className) {
  	var elemClassName = elem.className;
  	if (elemClassName == null){
  		return;
  	}
  	if (elemClassName.indexOf(className) != -1) {
  		elemClassName = elemClassName.replace(' ' + className, '');
  		elemClassName = elemClassName.replace(className, '');
  		elemClassName = elemClassName.replace(className + ' ', '');
  		elem.className = elemClassName;
  	}
 }
  
 function setCookie(name, wert, domain, expires, path, secure){
 	 var cook = name + "=" + unescape(wert);
 	 cook += (domain) ? "; domain=" + domain : "";
 	 cook += (expires) ? "; expires=" + new Date(expires).toGMTString() : "";
 	 cook += (path) ? "; path=" + path : "";
 	 cook += (secure) ? "; secure" : "";
 	 document.cookie = cook;
 }
  
 function getCookie(name) {
 	 var i=0;  // Suchposition im Cookie
 	 var suche = name + "=";
 	 while (i<document.cookie.length) {
 		 if (document.cookie.substring(i, i + suche.length) == suche) {
 			 var ende = document.cookie.indexOf(";", i + suche.length);
 			 ende = (ende > -1) ? ende : document.cookie.length;
 			 var cook = document.cookie.substring(i + suche.length, ende);
 			 return unescape(cook);
 		 }
 		 i++;
 	 }
 	 return "";
 }
 
 function initGalleria(containerID){
	 
	 $('#gallery_' + containerID).galleria(
		{
			clickNext : true, // helper for making the image clickable
			insert    : containerID,
			onImage   : function(image,caption,thumb) {
				// fade in the image & caption
				if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) {
					image.css('display','none').fadeIn(1000);
				}
				caption.css('display','none').fadeIn(1000);
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				// fade out inactive thumbnail
				_li.siblings().children('img.selected').fadeTo(500,0.3);
				// fade in active thumbnail
				thumb.fadeTo('fast',1).addClass('selected');
			},
			onThumb : function(thumb) { // thumbnail effects goes here
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				// if thumbnail is active, fade all the way.
				var _fadeTo = _li.is('.active') ? '1' : '0.3';
				// fade in the thumbnail when finnished loading
				thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);
				// hover effects
				thumb.hover(
					function() { thumb.fadeTo('fast',1); },
					function() { _li.not('.active').children('img').fadeTo('fast',0.3); } // don't fade out if the parent is active
				)
			}
		});
	window.setTimeout('$("#gallery_' + containerID + ' li img").first().click()', 500);
 }
