// Fokus auf erstes Formular-Element setzen (nicht hidden)
function setFormFocus()
{
	window.focus();
	
	var i=0;
	if (window.document.forms[0])
		for (i=0; i<=10; i++)
		{
			if (window.document.forms[0].elements[i] && window.document.forms[0].elements[i].type!='hidden'
			   && window.document.forms[0].elements[i].type!='submit' && window.document.forms[0].elements[i].type!='button')
			{
				window.document.forms[0].elements[i].focus();
				window.document.forms[0].elements[i].select();
				return;	
			}
		}
}

// URL aufrufen 
function url(myurl)
{
	window.location.href = myurl;
}


function open_window(windowstring,openFrom)
{
	param = 'width=650,height=500,resizable=yes,scrollbars=yes,left=50,top=50';
	if (openFrom)
		openFrom.window.open(windowstring,'',param);
	else
		window.open(windowstring,'',param);
}

function comfirm_del(url)
{
    if (confirm("Wirklich löschen?")) location.href=url;
}

function write_message(uid)
{
	opener.location.href='in.php?th=message_write&id='+uid;
	opener.window.focus();
}

// smilie popup 
function smilies() {
	
	window.open('tools/smilies.php','','width=300,height=400,resizable=yes,scrollbars=yes,left=50,top=50'); 
}

// bb-codes info 
function bbcodes() {
	
	window.open('tools/bbcodes.php','','width=300,height=350,resizable=yes,scrollbars=yes,left=50,top=50'); 
}


//alle (de)markieren
var checkflag = "false";
          
function check(field) {
  if (checkflag == "false") {
    for (i = 0; i < field.length; i++) {
      field[i].checked = true;
    }
    checkflag = "true";
  }
  else {
    for (i = 0; i < field.length; i++) {
      field[i].checked = false; 
    }
    checkflag = "false";
  }
}

//In die Textbox "Text" einen String einfuegen
function add_code(codestring) {
    opener.document.getElementById('insert_text').value += " " + codestring + " ";
    
    /*	
    var myText = opener.document.getElementById('insert_text');
    var chaineAj = codestring;

    //IE support
    if (myText.selection) {
        //myText.focus();
        sel = opener.document.selection.createRange();
        sel.text = chaineAj+' ';
        //document.sqlform.insert.focus();
    }
    //MOZILLA/NETSCAPE support
    else if (opener.document.form1.text.selectionStart || opener.document.form1.text.selectionStart == "0") {
        var startPos = opener.document.form1.text.selectionStart;
        var endPos = opener.document.form1.text.selectionEnd;
        var chaineSql = opener.document.form1.text.value;

        myText.value = chaineSql.substring(0, startPos) + chaineAj + chaineSql.substring(endPos, chaineSql.length);
    } else {
        myText.value += chaineAj;
    }
    */
    
}

//Mathematische Hilfsfunktionen
function Mod(a, b) {
    return a - Math.floor(a / b) * b;
}

function Div(a, b) {
    return Math.floor(a / b);
}

//Zeichen, die man noch schreiben darf (z.B beim Pinnwandzettel)
function LettersLeft(ErlaubteAnzahl)
{

	Laenge=document.form1.text.value.length;
	if(Laenge>ErlaubteAnzahl)
	{
		document.form1.text.value=document.form1.text.value.substring(0,ErlaubteAnzahl);
		RestlicheZeichen=0;
	}
	else
	{
		RestlicheZeichen=ErlaubteAnzahl-Laenge;
	}
	document.form1.Laenge.value=RestlicheZeichen;
}

//Ticker Argumente: text, start (normalerweise: 0), div-id, Anzahl Zeichen die angezeigt werden sollen
function newsticker(newsticker_text, newsticker_start, object_id, show_letters)
{
	if (show_letters > newsticker_text.length)
		show_letters =  newsticker_text.length;
		
	if (Math.round(newsticker_start)+Math.round(show_letters) <= newsticker_text.length)
		document.getElementById(object_id).innerHTML = newsticker_text.substring(newsticker_start,Math.round(newsticker_start)+Math.round(show_letters));
	else
		document.getElementById(object_id).innerHTML = newsticker_text.substring(newsticker_start) +' '+ newsticker_text.substring(0,Math.round(show_letters) - (Math.round(newsticker_text.length)-Math.round(newsticker_start))-1);
	
	newsticker_start++;
	if(newsticker_start>=newsticker_text.length)
		newsticker_start=0;
	
	setTimeout("newsticker('"+newsticker_text+"','"+newsticker_start+"', '"+object_id+"','"+show_letters+"')", 300);
}

//Counter, der Angegebenen Timestamp im Sekundentakt runter zaehlt
//Parameter: Unix-TS zum runterzaehlen, Objekt (DIV)-ID, startText (steht direkt vor dem Zeittext)
function writeCounter(timestamp, object_id, startText)
{
	if (!startText) startText = '';
	var timestring 	= '';
	var tstemp 		= timestamp;
	var inttemp		= 0;
	
	if (timestamp <= 0) 
	{
		timestring = ' ';
		
	} else {
		//Tage, Stunden, Minuten berechnen
		inttemp = Div(timestamp,86400);
		tstemp = tstemp-(inttemp * 86400);
		if (inttemp > 0)
			timestring += inttemp+' Tag(e) ';
		
		inttemp = Div(tstemp,3600);
		tstemp = tstemp-(inttemp * 3600);
		if (inttemp > 0)
			timestring += inttemp+' Stunde(n) ';
		
		inttemp = Div(tstemp,60);
		tstemp = tstemp-(inttemp * 60);
		if (inttemp > 0)
			timestring += inttemp+' Minute(n) ';
		
		timestring += tstemp+' Sekunde(n) ';
		
		timestring = startText+timestring;
	
	}

	//In das Div ausgeben
	document.getElementById(object_id).innerHTML = timestring;
	
	timestamp -= 1;
	//Nach einer Sekunde wieder aufrufen
	setTimeout("writeCounter("+timestamp+",'"+object_id+"','"+startText+"')", 1000);
}

//Zeigt oder versteckt ein Element mit der ID
function ShowHideElement(id) {
	obj = document.getElementsByTagName("div");
	if (obj[id].style.visibility == 'visible'){
		obj[id].style.visibility = 'hidden';
		obj[id].style.display = "none";
	}
	else {
		obj[id].style.visibility = 'visible';
		obj[id].style.display = "block";
	}
}
