/*
	adds mouseover to li tag in top menu
*/

function startList() {
	if (document.getElementById) {
		navRoot = document.getElementById("dd");
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName == "LI") {
				node.onmouseover = function() {
				this.className = "over";
				}
			node.onmouseout=function() {
				this.className = "";
				}
			}
		}
	}
	
	// call functiom for setting left teasers position
	left_teasers_position();
	// call functiom for selecting
	changeSize();
	// call rolover functiom
	MM_preloadImages('fileadmin/images/button_login_ro.gif','fileadmin/images/eng_flag_ro.gif','fileadmin/images/ger_flag_ro.gif')
}

/*
	sets yellow stroke width and visibility
*/

function changeSize() {	
	if (document.getElementById) {
		var m_menu=document.getElementById('main_menu');
		var imgs = m_menu.getElementsByTagName('img');
		var menuStatus='go';
		var j=0;
		var r=3; // margin
		var w=0; // width
		var b=2; // left and right borders of image
		l_menu_sirina = 210 // left menu width without border
		while (j<imgs.length-1){
			imgs[j].parentNode.parentNode.style.zIndex = 400-j*10;
			j++;
		}
		var i=0;
		while (menuStatus=='go') {
			w += imgs[i].width; 
			w += r;
			w += b;
			if (imgs[i].id=='main_active') {
				if (document.getElementById('yellow_stroke') != null) {
					document.getElementById('yellow_stroke').style.display='block';
				}
				menuStatus='inMenu';
			} else if (i>=imgs.length-1) {	
				menuStatus='outOfMenu';
			} else {
				i++;
			}
		}
		if (document.getElementById('menu_left') != null) {
			var l_menu=document.getElementById('menu_left');
			var l_menu_links = l_menu.getElementsByTagName('a');
			var first = l_menu_links[0];
			if (first != null && (first.className=='active' || first.className=='open') && menuStatus == "inMenu")  {
				first.className += ' no_top_border';
			}
			if (menuStatus == "outOfMenu" && document.getElementById('sidebar') != null){
				document.getElementById('sidebar').style.marginTop = "-2px";
			}
		} else if (menuStatus == "outOfMenu" && document.getElementById('sidebar') != null){
			document.getElementById('sidebar').style.marginTop = "0px";
		}
		w -= r; // minus last margin
		w -=2;  // minus 2px from borders
		if (w<l_menu_sirina) { 
			w=l_menu_sirina; 
		}
		document.getElementById('yellow_stroke_spacer').width=w;
	}
}

/*
	sets position of left teasers based on left menu heigth
*/

function left_teasers_position() {	
	var menurelative = false;
	if (document.getElementById) {
		if (document.getElementById('menu_left') != null) {
			var l_menu=document.getElementById('menu_left');
			var l_menu_lis = l_menu.getElementsByTagName('li');
			
			if((l_menu_lis.length > 10) && document.getElementById('left_teasers_holder') != null)
			{
				document.getElementById('left_teasers_holder').className = 'left_teasers_relative';
				menurelative = true;
			}
			
		} else if (document.getElementById('left_teasers_holder') != null) {
		
			document.getElementById('left_teasers_holder').className = 'left_teasers_relative';
			menurelative = true;
		}
		if (document.getElementById('sidebar') != null) {
			if (menurelative) {
				
				var iSidebarH = document.getElementById('sidebar').offsetHeight;
				var iContentH = document.getElementById('content_holder').offsetHeight;
				if (iContentH < iSidebarH) {
					document.getElementById('content_holder').style.height = iSidebarH-24+"px";
				}
				
			} else {
				var iTeasersH = document.getElementById('left_teasers_holder').offsetHeight;
				
				var iSidebarH = document.getElementById('sidebar').offsetHeight;
				var iContentH = document.getElementById('content_holder').offsetHeight;
				
				if (iContentH < (iSidebarH + iTeasersH)) {
					//alert('a');
					document.getElementById('content_holder').style.height = (iSidebarH+iTeasersH+50)+"px";
				} else if (iTeasersH>30) {
					//alert('b');
					document.getElementById('content_holder').style.height = (iContentH+20)+"px";
				}
			}
		}
		
	}
}

window.onload=startList;

/*
	standorte functions
*/
function standorte_locationSelected(obj, prefixId, url) {
	location.href=url+"&"+prefixId+"[value]="+obj.options[obj.selectedIndex].value;
}

function standorte_postcodeSelected(obj, prefixId, url) {
	location.href=url+"&"+prefixId+"[value]="+obj.options[obj.selectedIndex].value;
}
/*
	media library functions
*/
function media_changeCat(obj, prefixId, url) {
	location.href = url+"&"+prefixId+"[cat]="+obj.options[obj.selectedIndex].value;
}

function media_popup(img, original, x, y) {
	//window.open('/fileadmin/php/showpic.php?img='+img,'popup', 'height='+(h+50)+',width='+(w+20)+',top=10,left=10,status=yes,toolbar=no,menubar=no,location=no,resizable=yes');
	window.open('/fileadmin/php/showpic.php?img='+img+'&original='+original, 'popup', 'height='+y+', width='+x+', top=10, left=10, status=yes, toolbar=no, menubar=no, resizable=yes');
}
/*
	training program functions
*/
function course_selected(obj, prefixId, url) {
	location.href = url+"&"+prefixId+"[course]="+obj.options[obj.selectedIndex].value;
}

function course_selectLocTime(obj) {
	var dstreet = document.getElementById("dstreet");
	var dzip = document.getElementById("dzip");
	var dcity = document.getElementById("dcity");
	var val = obj.options[obj.selectedIndex].value;
	vals = val.split("###");
	dzip.value = vals[0];
	dcity.value = vals[1];
	dstreet.value = vals[2];
}


/*
	send email
*/
function isPPC() {
	if (navigator.appVersion.indexOf("PPC") != -1) return true;
	else return false;
}
/*
	send page to friend
*/
function sendPage(pageTitle){
	if (pageTitle == null) {
		pageTitle = "Koetter website";
	}

	//var webloc = window.location;
	var webloc = 'http://www.koetter.de/';
/*	
	
	var apo = webloc.indexOf("&");
	while (-1<apo) {
	   alert('found');
	   webloc = webloc.substr(0,apo)+'#'+webloc.substr(apo+1,255);
	   apo = webloc.indexOf("&");
	}
	alert('webloc:'+webloc+"\n"+'location:'+window.location);
*/	
	
	if(isPPC()) {
		this.location.href = "mailto:\?subject\=" + document.title + "?body="+pageTitle+": " + webloc;
	}else { 
		this.location.href = "mailto:\?body=Koetter website: " + webloc;
	}
}


/*
	rolover funcion
*/

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
/*
 added from randomly generated script file
 */
var browserName = navigator.appName;
var browserVer = parseInt(navigator.appVersion);
var version = "";
var msie4 = (browserName == "Microsoft Internet Explorer" && browserVer >= 4);
if ((browserName == "Netscape" && browserVer >= 3) || msie4 || browserName=="Konqueror" || browserName=="Opera") {version = "n3";} else {version = "n2";}
// Blurring links:
function blurLink(theObject)	{	//
	if (msie4)	{theObject.blur();}
}

/*
 added from t3lib/validate
 */
/***************************************************************
*
*  Universal validate-form
*
* $Id: jsfunc.validateform.js,v 1.6 2005/12/19 20:29:26 kurfuerst Exp $
*
*
*
*  Copyright notice
*
*  (c) 1998-2003 Kasper Skaarhoj
*  All rights reserved
*
*  This script is part of the TYPO3 t3lib/ library provided by
*  Kasper Skaarhoj <kasper@typo3.com> together with TYPO3
*
*  Released under GNU/GPL (see license file in tslib/)
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
*  This copyright notice MUST APPEAR in all copies of this script
***************************************************************/


function validateForm(theFormname,theFieldlist,goodMess,badMess,emailMess)	{
	var formObject = document[theFormname];
	if (!formObject)	{
		formObject = document.getElementById(theFormname);
	}
	if (formObject && theFieldlist)	{
		var index=1;
		var theField = split(theFieldlist, ",", index);
		var msg="";
		var theEreg = '';
		var theEregMsg = '';
		var specialMode = '';

		while (theField) {
			theEreg = '';
			specialMode = '';

				// Check special modes:
			if (theField == '_EREG')	{	// EREG mode: _EREG,[error msg],[JS ereg],[fieldname],[field Label]
				specialMode = theField;

				index++;
				theEregMsg = unescape(split(theFieldlist, ",", index));
				index++;
				theEreg = unescape(split(theFieldlist, ",", index));
			} else if (theField == '_EMAIL')	{
				specialMode = theField;
			}

				// Get real field name if special mode has been set:
			if (specialMode)	{
				index++;
				theField = split(theFieldlist, ",", index);
			}

			index++;
			theLabel = unescape(split(theFieldlist, ",", index));
			theField = unescape(theField);
			if (formObject[theField])	{
				var fObj = formObject[theField];
				var type=fObj.type;
				if (!fObj.type)	{
					type="radio";
				}
				var value="";
				switch(type)	{
					case "text":
					case "textarea":
						value = fObj.value;
					break;
					case "select-one":
						if (fObj.selectedIndex>=0)	{
							value = fObj.options[fObj.selectedIndex].value;
						}
					break;
					case "select-multiple":
						var l=fObj.length;
						for (a=0;a<l;a++)	{
							if (fObj.options[a].selected)	{
								 value+= fObj.options[a].value;
							}
						}
					break;
					case "radio":
						var len=fObj.length;
						if (len)	{
							for (a=0;a<len;a++)	{
								if (fObj[a].checked)	{
									value = fObj[a].value;
								}
							}
						} else {
							if (fObj.checked)	{
								value = fObj.value;
							}
						}
					break;
					default:
						value=1;
				}

				switch(specialMode)	{
					case "_EMAIL":
						var theRegEx_notValid = new RegExp("(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)", "gi");
						var theRegEx_isValid = new RegExp("^.+\@[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})$","");
						if (!theRegEx_isValid.test(value))	{	// This part was supposed to be a part of the condition: " || theRegEx_notValid.test(value)" - but I couldn't make it work (Mozilla Firefox, linux) - Anyone knows why?
							msg+="\n"+theLabel+' ('+(emailMess ? unescape(emailMess) : 'Email address not valid!')+')';
						}
					break;
					case "_EREG":
						var theRegEx_isValid = new RegExp(theEreg,"");
						if (!theRegEx_isValid.test(value))	{
							msg+="\n"+theLabel+' ('+theEregMsg+')';
						}
					break;
					default:
						if (!value)	{
							msg+="\n"+theLabel;
						}
				}
			}
			index++;
			theField = split(theFieldlist, ",", index);
		}
		if (msg)	{
			var theBadMess = unescape(badMess);
			if (!theBadMess)	{
				theBadMess = "Diese Felder sind Pflichtfelder:";
			}
			theBadMess+="\n";
			alert(theBadMess+msg);
			return false;
		} else {
			var theGoodMess = unescape(goodMess);
			if (theGoodMess)	{
				alert(theGoodMess);
			}
			return true;
		}
	}
}
function split(theStr1, delim, index) {
	var theStr = ''+theStr1;
	var lengthOfDelim = delim.length;
	sPos = -lengthOfDelim;
	if (index<1) {index=1;}
	for (a=1; a<index; a++)	{
		sPos = theStr.indexOf(delim, sPos+lengthOfDelim);
		if (sPos==-1)	{return null;}
	}
	ePos = theStr.indexOf(delim, sPos+lengthOfDelim);
	if(ePos == -1)	{ePos = theStr.length;}
	return (theStr.substring(sPos+lengthOfDelim,ePos));
}
