// Javascriptfunktionen fuer Dokumentenverwaltung 
// 
// (c) 2005 by Sven Stefani, ss@stefani.biz
//
//


function areyousure(msg)
{
    if (msg == "") msg="Sind Sie sicher?";
    return confirm(msg);
}



function showlayer(layername, show)
{
	if(!document.getElementById) return;
	
	var obj = document.getElementById(layername);
	if(!obj || !obj.style) return alert('Fehler!' + layername + ' existiert nicht.');
	
	obj.style.visibility = show ? "visible" : "hidden";
} 


function login_checkselection(layername,cnt){
	if (document.flogin.f_other1.options[document.flogin.f_other1.selectedIndex].value.substr(0,1) == "-") {
		for (var i=1; i<(cnt+1); i++)
		{
			showlayer(layername+i, true); 
			showlayer(layername+i, true);
		}
	}
	else
	{
		for (i=1; i<(cnt+1); i++)
		{
			showlayer(layername+i, false); 
			showlayer(layername+i, false);
		}
	}
	
}


// removes single newlines \r, \n, or \r\n
function removesinglenewline (oldtext)
{
	// replace \n\r with #~-n-~#
	oldtext = oldtext.replace(/\r\n/g, "#~-n-~#");	

	// replace \r\n with #~-n-~#
	oldtext = oldtext.replace(/\n\r/g, "#~-n-~#");	

	// replace \r with #~-n-~#
	oldtext = oldtext.replace(/\r/g, "#~-n-~#");	

	// replace \n with #~-n-~#
	oldtext = oldtext.replace(/\n/g, "#~-n-~#");	

	// replace #~-r-~#{2,} with \n
	oldtext = oldtext.replace(/(#~-n-~#){2,}/g, "\n\n");	

	// remove single #~-n-~#
	oldtext = oldtext.replace(/(#~-n-~#)/g, " ");	

	return oldtext;
}



// the function inserttag (original name was insert) was programmed by Torsten Anacker (torsten -at- anaboe.net)
// http://aktuell.de.selfhtml.org/tippstricks/javascript/bbcode/
// modified by Sven Stefani, 16.03.2005
function inserttag(myform, aTag, eTag) {
	var input = myform;
	input.focus();
	/* für Internet Explorer */
	if(typeof document.selection != 'undefined') {
		/* Einfügen des Formatierungscodes */
		var range = document.selection.createRange();
		var insText = range.text;
		range.text = aTag + insText + eTag;
		/* Anpassen der Cursorposition */
		range = document.selection.createRange();
		if (insText.length == 0) {
			range.move('character', -eTag.length);
		} else {
			range.moveStart('character', aTag.length + insText.length + eTag.length);      
		}
		range.select();
	}
	/* für neuere auf Gecko basierende Browser */
	else if(typeof input.selectionStart != 'undefined')
	{
		/* Einfügen des Formatierungscodes */
		var start = input.selectionStart;
		var end = input.selectionEnd;
		var insText = input.value.substring(start, end);
		input.value = input.value.substr(0, start) + aTag + insText + eTag + input.value.substr(end);
		/* Anpassen der Cursorposition */
		var pos;
		if (insText.length == 0) {
			pos = start + aTag.length;
		} else {
			pos = start + aTag.length + insText.length + eTag.length;
		}
		input.selectionStart = pos;
		input.selectionEnd = pos;
	}
	/* für die übrigen Browser */
	else
	{
		/* Abfrage der Einfügeposition */
		var pos;
		var re = new RegExp('^[0-9]{0,3}$');
		while(!re.test(pos)) {
			pos = prompt("Einfügen an Position (0.." + input.value.length + "):", "0");
		}
		if(pos > input.value.length) {
			pos = input.value.length;
		}
		/* Einfügen des Formatierungscodes */
		var insText = prompt("Bitte geben Sie den zu formatierenden Text ein:");
		input.value = input.value.substr(0, pos) + aTag + insText + eTag + input.value.substr(pos);
	}
}



// cbselect(cb_name, typ)
// created by Sven Stefani, 11.04.2005
// checks, unchecks, or inverts value of checkboxes
// cb_name is the name of checkbox elements (name-attribute)
// typ is what to do: 0 deselect, 1 select, 2 invert value
function cbselect(cb_name, typ)
{
	// get all elements where name-attribute is same as cb_name
	// a_cb is an array of all elements, even if there is onle one element
	a_cb = document.getElementsByName(cb_name);
	
	if (a_cb.length == 0) alert ( 'Keine Elemente mit Attribut  name="'+cb_name+'"  gefunden');
	
	// loop over all elements
	for (var i = 0; i < a_cb.length; i++)
	{
		switch (typ)
		{
			case false:
			case "0":
				a_cb[i].checked = false;
			break;

			case true:
			case "1":
				a_cb[i].checked = true;
			break;

			case "2":
			default:
				a_cb[i].checked = !a_cb[i].checked;
			break;
		}
	}
}

function setOther56Label()
{

	rb_name = 'f_other3';
	a_rb = document.getElementsByName(rb_name)
	rb_selection = 0;
	
	if (a_rb.length == 0) alert ( 'Keine Elemente mit Attribut  name="'+rb_name+'"  gefunden');
	
	// loop over all elements
	for (var i = 0; i < a_rb.length; i++)
	{
		if (a_rb[i].checked) rb_selection = a_rb[i].value;
	}

	other5 = document.getElementById('other5label');
	other6 = document.getElementById('other6label');
	

	switch (rb_selection)
	{
		case "1":
//					document.getElementById('other5').style.visibility="visible";
//					document.getElementById('other6').style.visibility="visible";
//					document.getElementById('other6key').style.visibility="hidden";

					for (; other5.hasChildNodes(); other5.removeChild(other5.firstChild)) {}
					for (; other6.hasChildNodes(); other6.removeChild(other6.firstChild)) {}

					textnode1 = document.createTextNode("Benutzername für");
					textnode2 = document.createTextNode("Bibliothekszugang");
					newbr1 = document.createElement("br");

					textnode3 = document.createTextNode("Kennwort für");
					textnode4 = document.createTextNode("Bibliothekszugang");
					newbr2 = document.createElement("br");

					other5.appendChild(textnode1);
					other5.appendChild(newbr1);
					other5.appendChild(textnode2);

					other6.appendChild(textnode3);
					other6.appendChild(newbr2);
					other6.appendChild(textnode4);
		break;

		case "2":
//					document.getElementById('other5').style.visibility="visible";
//					document.getElementById('other6').style.visibility="visible";
//					document.getElementById('other6key').style.visibility="visible";

					for (; other5.hasChildNodes(); other5.removeChild(other5.firstChild)) {}
					for (; other6.hasChildNodes(); other6.removeChild(other6.firstChild)) {}

					textnode1 = document.createTextNode("Pfad zum Bücher-");
					textnode2 = document.createTextNode("weckerRemote-Skript");
					newbr1 = document.createElement("br");

					textnode3 = document.createTextNode("Schlüssel für eine");
					textnode4 = document.createTextNode("sichere Übertragung");
					newbr2 = document.createElement("br");

					other5.appendChild(textnode1);
					other5.appendChild(newbr1);
					other5.appendChild(textnode2);

					other6.appendChild(textnode3);
					other6.appendChild(newbr2);
					other6.appendChild(textnode4);

		break;

		case "0":
		default:

/*					for (; other5.hasChildNodes(); other5.removeChild(other5.firstChild)) {}
					for (; other6.hasChildNodes(); other6.removeChild(other6.firstChild)) {}
					document.getElementById('other5').style.visibility="hidden";
					document.getElementById('other6').style.visibility="hidden";
					document.getElementById('other6key').style.visibility="hidden";
*/
	}	
}








function plugNS()
{
	var b=1;
	var o=0;
	var p=new Array("Shockwave Flash","Shockwave for Director","RealPlayer","QuickTime","VivoActive","LiveAudio","VRML","Dynamic HTML Binding","Windows Media Services");
	var np=navigator.plugins;
	for(var x=0;x<p.length;x++)
	{
		for(var i=0;i<np.length;i++)
			if (np[i].name.indexOf(p[x])>=0) o |= b;
		b*=2;
	}
	return o;
}
	
function plugIE()
{
	if(!document.body)document.write('<body>');
	var db=document.body;
	var o=0;
	var b=1;
	var p=new Array("D27CDB6E-AE6D-11CF-96B8-444553540000","2A202491-F00D-11CF-87CC-0020AFEECF20","23064720-C4F8-11D1-994D-00C04F98BBC9","","","","90A7533D-88FE-11D0-9DBE-0000C0411FC3","9381D8F2-0288-11D0-9501-00AA00B911A5","22D6F312-B0F6-11D0-94AB-0080C74C7E95");
	db.addBehavior("#default#clientcaps");
	for(var i=0;i<p.length;i++)
	{
		if(p[i])
			if(db.isComponentInstalled("{"+p[i]+"}","componentid")) o|=b;
		b*=2;
	}
	return o;
}

function nedstatbasic(id,options)
{
	var n=navigator;
	var ver=n.appVersion;
	var d=document;
	var verIE=parseInt(ver.substring(ver.indexOf("MSIE")+5,ver.indexOf("MSIE")+6));
	if (verIE>0)
		ver=verIE;
	else 
		ver=parseInt(ver);
	var u="http://m1.nedstatbasic.net/n?id="+id;
	var r;
	if (options&1)
		r=d.referrer;
	else
		r=top.document.referrer;
	if (!(options&2))
	{
		var rc=r;
		var i=rc.indexOf('?');
		if (i>=0) 
			rc=rc.substring(0,i);
		i=rc.lastIndexOf('/');
		if (i>=0) rc=rc.substring(0,i+1);
		var l=''+d.location;
		if (l.indexOf(rc)==0)
			r='';
	}
	if (r&&(r!=d.location))
		u+="&r="+escape(r);
	if ((n.appName=="Netscape"&&ver>=3))
		u+="&p="+plugNS();
	if (verIE>=5&&n.appVersion.indexOf('Win')>=0&&n.userAgent.indexOf('Opera')<0)
		u+="&p="+plugIE();
	if (ver>=4)
	{
		var s=screen;
		var w=s.width;
		var h=s.height;
		var c=s.colorDepth;
		if (w)
			u+="&w="+w;
		if (h)
			u+="&h="+h;
		if (c)
			u+="&c="+c;
	}
	u+="&v=1";
	d.write('<a href="http://www.nedstatbasic.net/stats?'+id+'"><img src="'+u+'" width=18 height=18 alt="Nedstat Basic - Free web site statistics" style="border: 0px" /></a>');
}