<!--
//-----------------------------------------------------------------------------
// Javascript tools section
//-----------------------------------------------------------------------------
// functions
//-----------------------------------------------------------------------------

var languageFlagsVisible=true;

// ----------------------------------------------------------------------------
/**
 * @function maximizePopup(targetWindow)
 * @author B.Schreiber
 * @brief Sets given targetWindow's size to available width and height.
 * @param targetWindow : The target window object to resize.
 * 
 * @return /
 *
 * Set given targetWindw's size to available screen'width and height.
 * Also it is positioned to the top left corner of the screen.
 * 
 **/
// ----------------------------------------------------------------------------
function maximizePopup(targetWindow)
{
	if (targetWindow)
	{
		targetWindow.resizeTo(screen.availWidth,screen.availHeight);
		// ie needs this twice over timeout... (weird!)
		if (isIE()) targetWindow.setTimeout("maximizePopup(this)",200);
		targetWindow.moveTo(0,0);
	}
}
// ----------------------------------------------------------------------------
/**
 * @function isIE(targetUrl)
 * @author B.Schreiber
 * @brief Test for IE.
 * 
 * @return boolean
 *
 * Test for IE.
 * 
 **/
// ----------------------------------------------------------------------------
function isIE()
{
	return (navigator.appName.search(/Explorer/)!=-1);
}
// ----------------------------------------------------------------------------
/**
 * @function jsRedirect(targetUrl)
 * @author B.Schreiber
 * @brief Javascript redirect code, that redirects to given url.
 * 
 * @return /
 *
 * Uses document.location.href to redirect over javascript to another source.
 * 
 **/
// ----------------------------------------------------------------------------
function jsRedirect(targetUrl)
{
	if (targetUrl)
	{
		document.location.href=targetUrl;
	}
}

// ------------------------------------------------------------------------------------------
/**
 * @function safemail(name,domain,display)
 * @author M.Schlegel
 * @brief safe mail script
 * @param name    : mail prefix (before @)
 * @param domain  : mail postfix (after @)
 * @param display : The link display.
 * 
 * @return nothing. writes directly to document.
 *
 * Crypting mail script.
 **/
// ----------------------------------------------------------------------------
function safemail(name, domain, display) 
{
	displayed=(typeof(display)=="undefined") ? name+"@"+domain : display
	document.write('<a href=mailto:' + name + '@' + domain + '><font color=BLACK>' + displayed + '</font></a>');
};
// ------------------------------------------------------------------------------------------
/**
 * @function setUrlParam(_url_,paramname,paramvalue)
 * @author B.Schreiber
 * @brief parse param string of given url and set's given params value there.
 * @param _url_      : The url to use to set the new param into.
 * @param paramname  : The new parameter's name
 * @param paramvalue : The new parameter's value.
 * 
 * @return url param string with the new parameter
 *
 * Takes the given docuementURL (inclusive params!) and adds or replaces given
 * parameter in param-string.
 * The result is returned by this function.
 * Most common calling is: setUrlParam(document.URL,'newparam','12345');
 **/
// ----------------------------------------------------------------------------
function setUrlParam(_url_,paramname,paramvalue)
{
	var result='';
	var found=false;
	// split url at ?
	urlparts=_url_.split("?");
	params=urlparts[1].split("&");
	for (i=0; i<params.length;i++)
	{
		pnv=params[i].split("=");
		pn=pnv[0]; pv=pnv[1];
		if (pn==paramname)
		{
			pv=paramvalue;
			params[i]=pn+"="+pv;
			found=true;
			break;
		}
	}
	if (!found) params.push(paramname+"="+paramvalue);
	
	result=urlparts[0]+"?"+params.join("&");
	
	return result;
}
// ----------------------------------------------------------------------------
/**
 * @function getUrlParam(_url_,paramname,defaultVal)
 * @author B.Schreiber
 * @brief Get a param out of given _url_ param-string.
 * @param _url_      : The url to parse for param
 * @param paramname  : The parameter's name
 * @param defaultVal : The value to return if param is not present.
 * 
 * @return param's value if present or given defaultVal if not present.
 *
 * Parse given _url_ and return the value of the parameter with given paramname.
 * If the param is not within param-string then the given defaultVal is returned.
 **/
// ----------------------------------------------------------------------------
function getUrlParam(_url_,paramname,defaultVal)
{
	var result=defaultVal;
	urlparts=_url_.split("?");
	params=urlparts[1].split("&");
  	for (i=0; i<params.length;i++)
  	{
  		pnv=params[i].split("=");
  		pn=pnv[0]; pv=pnv[1];
  		if (pn==paramname)
  		{
  			result=pv;
  			break;
  		}
  	}
  	
  	return result;
}

// shorter way to get elementById for explorer compatibility.
function getElem(elemId)
{
	return document.getElementById(elemId);
}

function getKeyCode(event) 
{
   return (event.keyCode?event.keyCode:event.which?event.which:event.charCode);
}

function inRange(val,a,b)
{
	if ( (val>=a) && (val<=b)) return true;
	return false;
}

function decode_utf8(utftext) 
{
	var plaintext = ""; var i=0; var c=c1=c2=0;
    	// while-Schleife, weil einige Zeichen uebersprungen werden
    	while(i<utftext.length)
    	    {
    	    c = utftext.charCodeAt(i);
    	    if (c<128) {
    	        plaintext += String.fromCharCode(c);
    	        i++;}
    	    else if((c>191) && (c<224)) {
    	        c2 = utftext.charCodeAt(i+1);
    	        plaintext += String.fromCharCode(((c&31)<<6) | (c2&63));
    	        i+=2;}
    	    else {
    	        c2 = utftext.charCodeAt(i+1); c3 = utftext.charCodeAt(i+2);
    	        plaintext += String.fromCharCode(((c&15)<<12) | ((c2&63)<<6) | (c3&63));
    	        i+=3;}
    	    }
    	return plaintext;
}
function encode_utf8(rohtext) {                                         
	// dient der Normalisierung des Zeilenumbruchs          //-->
	rohtext = rohtext.replace(/\r\n/g,"\n");                
	var utftext = "";                                       
	for(var n=0; n<rohtext.length; n++)                     
	    {                                                   
	    // ermitteln des Unicodes des  aktuellen Zeichens   
	    var c=rohtext.charCodeAt(n);                        
	    // alle Zeichen von 0-127 => 1byte                  
	    if (c<128)                                          
	        utftext += String.fromCharCode(c);              
	    // alle Zeichen von 127 bis 2047 => 2byte           
	    else if((c>127) && (c<2048)) {                      
	        utftext += String.fromCharCode((c>>6)|192);     
	        utftext += String.fromCharCode((c&63)|128);}    
	    // alle Zeichen von 2048 bis 66536 => 3byte         
	    else {                                              
	        utftext += String.fromCharCode((c>>12)|224);    
	        utftext += String.fromCharCode(((c>>6)&63)|128);
	        utftext += String.fromCharCode((c&63)|128);}    
	    }                                                   
	return utftext;                                         
}

function changePageLanguage(toLanguage)
{
	var cid,pg,lnk;
	lnk='index.php?lang='+toLanguage;
	cid=getElem('carid');
	pg=getElem('pg');
	
	if (cid) lnk=lnk+'&carid='+cid.value;
	if (pg) lnk=lnk+'&pg='+pg.value;
	if (window.onChangeLanguage!=undefined) onChangeLanguage(toLanguage,lnk);
	jsRedirect(lnk);
}                                                        
function validatePage(language)
{
	var pgNr,tit;
	// correct pg...
	pgNr=getElem('pgNr');
	tit=getElem('title');
	if (!pgNr) alert('Missing inputfield pgNr!');
	if (!tit)  alert('Missing inputfield title!');
	getElem('pg').value=pgNr.value;
	window.document.title+=' - '+tit.value;
	checkTimeout();
}

function checkTimeout()
{
	var to;
	to=getElem('timeout');
	if (!to) alert('Missing inputfield timeout!');
	// test timeout
	if (to.value==1)
	{
		document.location.href='index.php?op=dsess';
	}	
}
function disableTextSelection()
{
	if (isIE())
	{
		document.onselectstart=function disabletext(e){return (top.inputfield_focused?true:false);};
	}
	else
	{
		document.onmousedown=function disabletext(e){return (top.inputfield_focused?true:false);};
		document.onclick=function disabletext(e){return true;};
	};
	// text inputs and textarea field-patch.
	$('input[type="text"],textarea').click( function(){this.focus(); top.inputfield_focused=true;} );
	$('input[type="text"],textarea').keydown( function(){top.inputfield_focused=true;} );
	$('input[type="text"],textarea').blur( function(){top.inputfield_focused=false;} );
}
function getCaretPos (oField) 
{
     // Initialize
     var iCaretPos = 0;

     // IE Support
     if (document.selection) 
     { 

       // Set focus on the element
       oField.focus();
  
       // To get cursor position, get empty selection range
       var oSel = document.selection.createRange();
  
       // Move selection start to 0 position
       oSel.moveStart('character', -oField.value.length);
  
       // The caret position is selection length
       iCaretPos = oSel.text.length;
     }

     // Firefox support
     else if (oField.selectionStart || oField.selectionStart == '0')
       iCaretPos = oField.selectionStart;

     // Return results
     return (iCaretPos);
}
function showPageLoadingInfo()
{
	languageFlagsVisible=$('#languageflags').is(':visible');
	$('#languageflags').hide();
	$('#pageloading').show();
        $('#gallery').hide();
}
function hidePageLoadingInfo()
{
	if (languageFlagsVisible) $('#languageflags').show();
	$('#pageloading').hide();
}
 function setCaretPos (oField, iCaretPos) 
 {

     // IE Support
     if (document.selection) 
     { 

       // Set focus on the element
       oField.select();
       oField.focus();
  
       // Create empty selection range
       var oSel = document.selection.createRange();
  
       // Move selection start and end to 0 position
       oSel.moveStart('character', -oField.value.length);
  
       // Move selection start and end to desired position
       oSel.moveStart('character', iCaretPos);
       oSel.moveEnd('character', 0);
       oSel.select();
     }

     // Firefox support
     else if (oField.selectionStart || oField.selectionStart == '0') 
     {
       oField.selectionStart = iCaretPos;
       oField.selectionEnd = iCaretPos;
       oField.focus();
     }
}
function setCaretPosToEnd(oField)
{
	if (oField)
	{
		setCaretPos(oField,oField.value.length);
	};
}

function ScrollToElement(theElement){

  var selectedPosX = 0;
  var selectedPosY = 0;
              
  while(theElement != null){
    selectedPosX += theElement.offsetLeft;
    selectedPosY += theElement.offsetTop;
    theElement = theElement.offsetParent;
  }
                        		      
 window.scrollTo(selectedPosX,selectedPosY);

}
function saveScrollCoordinates() 
{
  	$('#scrollx').val((document.all)?document.body.scrollLeft:window.pageXOffset);
  	$('#scrolly').val((document.all)?document.body.scrollTop:window.pageYOffset);
} 
function initScrollCoordinates() 
{
	$('#scrollx').val(0);
	$('#scrolly').val(0);
} 

function scrollToCoordinates() 
{
  window.scrollTo($('#scrollx').val(), $('#scrolly').val());
} 

function correctIFrameHeight(iframeId,notParentFrameLimited)
{
	var h,iframe,iframes,myiframe,i;

	try
	{
		if (top.parent.iFrameHeight!=undefined) 
		{
			// joomla's iFrameHeight Function... if available...
			top.parent.iFrameHeight();
		}
		else
		{
			if (!iframeId)
			{
				iframes=top.parent.document.getElementsByTagName('iframe');
				for (i=0; i<iframes.length;i++)
				{
					myiframe=iframes[i];
					if (myiframe.contentWindow.document.location.href==document.location.href) break;
				};
			};
			
			iframe=top.parent.document.getElementById(iframeId);
			if (!iframe) iframe=myiframe;
			if (!document.all)
			{
				if (iframe)
				{
					h=iframe.contentDocument.height;
					if (!notParentFrameLimited) h=parseInt(iframe.style.height)+10;
					iframe.style.height = h + 'px';
				};
			}
			else if (document.all)
			{
				if(iframe)
				{
					h=iframe.contentWindow.document.body.scrollHeight;
					if (!notParentFrameLimited) h=parseInt(iframe.style.height)+10;
					iframe.style.height = h +'px';
				};
			};
		}
	}
	catch(err)
	{
		// iframe height not possible, or not allowed...
		// do nothing.
	};
}

function getWindowWidth(win)
{
    if (win == undefined)
        win = window;
    if (win.innerWidth) {
        return win.innerWidth;
    } else {
        if (win.document.documentElement 
                && win.document.documentElement.clientWidth) 
        {
            return win.document.documentElement.clientWidth;
        }
        return win.document.body.offsetWidth;
    }
}

function getWindowHeight(win)
{
    if (win == undefined)
        win = window;
    if (win.innerHeight) {
        return win.innerHeight;
    } else {
        if (win.document.documentElement 
                && win.document.documentElement.clientHeight) 
        {
            return win.document.documentElement.clientHeight;
        }
        return win.document.body.offsetHeight;
    }
}

// implizites und explizites checkbox-handling.
function excludeCheck(forElem,chkboxid_arr)
{
	for (i=0; i<chkboxid_arr.length; i++)
	{
		if (forElem.checked) cb_check(chkboxid_arr,false);
	};
}
function implyCheck(forElem,chkboxid_arr)
{
	for (i=0; i<chkboxid_arr.length; i++)
	{
		if (forElem.checked) cb_check(chkboxid_arr,forElem.checked);
	};
}
function implyUncheck(forElem,chkboxid_arr)
{
	for (i=0; i<chkboxid_arr.length; i++)
	{
		if (!forElem.checked) cb_check(chkboxid_arr,false);
	};
}
function cb_check(chkboxid_arr,state)
{
	s=true;
	if (state!=undefined) s=state;
	for(i=0; i<chkboxid_arr.length;i++)
	{
		chkbox=getElem(chkboxid_arr[i]);
		if (chkbox) chkbox.checked=s;
	};
}

function linkToHome()
{
	if (isIE())
	{
		window.location.replace("about:home");
	}
	else
	{
		home();
	}
}

