// Netscape Resize Fix (wg. der Style Sheets)

function MM_reloadPage(init){  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);


// Weitere Funktionen
// DHTML for Image
n=(document.layers)?true:false;

function imgDiv(name,src){
  if(n) document.layers['mainpic'].document.images[name].src = src;
  else document.images[name].src = src;
}
// end DHTML for Image

// SUCHE
function extSearchWin(url){
	title = 'globalSearch';
	sizeH = 780;
	sizeV = 550;
	extWinHandle = top.open(url,title,'toolbar=yes,directories=no,status=yes,scrollbars=yes,menubar=yes,resizable=yes,screenY=10,alwaysRaised=yes,width='+sizeH+',height='+sizeV);
	if(navigator.appVersion.indexOf("MSIE 3",0)==-1) id = setTimeout('extWinHandle.focus()',1000);
}

// test valid Email
function isEmail(str){
    var atLoc,dotLoc,len,i,ch;
    atLoc = str.indexOf('@');
    dotLoc = str.lastIndexOf('.');
    len = str.length;  
    //- @ vorhanden
    //- mindestens 1 buchstabe vor dem @
    if (atLoc < 1) return false;

    //- punkt hinter dem @ 
    //- mindestens 1 buchstabe zwischen @ und .
    if (dotLoc - atLoc < 2) return false;

    //- mindestens 1 buchstabe nach dem .
    if (dotLoc >= len-1) return false;

    return true;
}

// Test String Chars/Length
function testString(string, check, lang){
	if (lang)
		if ((string.length)<lang)
			return false;
	for (i=0; i<string.length; i++){
		if (check.indexOf(string.charAt(i))==-1)
			return false;
	}
	return true;
}

// Kategoriesuche, level=1 oder 2
function searchCategories(level, param){
    var form = document.CatSearch;
	if (form.elements["CatLevel"+level+"ID"].selectedIndex!=0 && form.elements["CatLevel"+level+"ID"].options[form.elements["CatLevel"+level+"ID"].selectedIndex].defaultSelected == false) 
	{
        if (param) {
			trenner = "?";
			if ( form.action.indexOf("?")>= 0 ) trenner = "&";
            form.action=form.action + trenner + "CatLevel" + level + "ID=" + form.elements["CatLevel"+level+"ID"].options[form.elements["CatLevel"+level+"ID"].selectedIndex].value;
        }
        form.method="POST";
		form.submit();
	}
}

// Selectboxen MenuID-Jump
function jumptoMenuID(selectbox,languageID){
	var form 			= selectbox.form;
	var selectedValue	= selectbox.options[selectbox.options.selectedIndex].value;
	if	(selectedValue == "") {
		form.reset();
		selectbox.blur();
		return;
	} else {
		top.location.href = "../../menu/"+selectedValue+"_l"+languageID+"/index.html";
		selectbox.blur();
	}
}

//new function to allow authors to determine window size and name
function openMagazineWindow(x,y,theURL,winName){
	xmin = Math.min(x,screen.availWidth-10);
	ymin = Math.min(y,screen.availHeight-36);
	xpos = (screen.availWidth-(xmin+10))/2;
	ypos = (screen.availHeight-(ymin+36))/2;
	properties = "width=" + xmin + ",height=" + ymin + ",location=no,menubar=no,personalbar=no,resizable=yes,screenX=" + xpos + ",screenY=" + ypos + ",left=" + xpos + ",top=" + ypos + ",locationbar=no,scrollbars=yes,directories=no,statusbar=no,toolbar=no";
	window.open(theURL,winName,properties).focus();
}

function showAdressTable(divTag){
	var div = document.getElementById(divTag);
	if(document.handelsweltenBestellung.receivadress.checked==true){
		div.style.display="inline";
	}
	else {
		div.style.display = "none";
	}       	
}

function MM_showHideLayers(){
	var i,p,v,obj,args=MM_showHideLayers.arguments;
	for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
	if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
		obj.visibility=v; }
}

function MM_findObj(n, d){
	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 openCenteredWindow(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=yes,screenX=" + xpos + ",screenY=" + ypos + ",left=" + xpos + ",top=" + ypos + ",locationbar=no,scrollbars=yes,directories=no,statusbar=no,toolbar=no";
	window.open(theURL,winName,features);
} 

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();
    }
}

/*
 * 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;
}

$(document).ready(function(){
	//add readspeaker link
	var innerHTML = $('#readspeakerLink').html();
	$('#readspeakerBox').prepend(innerHTML);
});

/* Glossar functionality requires jQuery */
$(document).ready(function(){
	if (window.location.href.indexOf("print") == -1) {
		$('div.glossarContent').hide();
		$('div.glossarExpander').show().css("display","inline-block");
		$('div.glossarContent.preopened').show();
		$('div.glossarExpander.preopened').hide();
		$('div.glossarMinimizer.preopened').show().css("display","inline-block");
	}

	$('div.glossarExpander').click(function() {
		$(this).parent().next('div.glossarContent').fadeIn("normal");
		$(this).hide();
		$(this).next('div.glossarMinimizer').show().css("display","inline-block");
	});

	$('div.glossarMinimizer').click(function() {
		$(this).parent().next('div.glossarContent').hide();
		$(this).hide();
		$(this).prev('div.glossarExpander').show().css("display","inline-block");
	});
});
