// chowa/pokazuje element o zadanym ID
function show_hide_element(id, display, first) {
	var obj = document.getElementById(id);
	if (obj.style.display == 'none' || (first && !obj.style.display)) {
		obj.style.display = display;
	} else {
		obj.style.display = 'none';
	}
}

// wyświetla okienko z możliwością wpisania wartości, następnie wpisuje te wartość do obiektu
function show_prompt(tag, obj, tekst, wartosc) {
	var obj = document.getElementById(obj);
	var tresc = prompt(tekst, wartosc);
	if (tresc != null && tresc != '') {
		var pos = caret(obj);
		obj.value = obj.value.substring(0, pos)+'['+tag+']'+tresc+'[/'+tag+']'+obj.value.substring(pos);
	}
}
// wyświetla dwa okienka z możliwością wpisania wartości, następnie wpisuje obie do obiektu
function show_prompt2(tag, obj, tekst1, wartosc1, tekst2, wartosc2) {
	var obj = document.getElementById(obj);
	var tresc1 = prompt(tekst1, wartosc1);
	var tresc2 = prompt(tekst2, wartosc2);
	if (tresc1 != null && tresc1 != '' && tresc2 != null && tresc2 != '') {
		var pos = caret(obj);
		obj.value = obj.value.substring(0, pos)+'['+tag+'='+tresc1+']'+tresc2+'[/'+tag+']'+obj.value.substring(pos);
	}
}

// podmienia linki klasy noweokno tak by otwieraly się w nowym oknie
function podmien_linki() {
	var obj = document.getElementsByTagName("a");
	for (var i=0; i<obj.length; i++) {
		var val = obj[i];
		if (val.className == 'noweokno') {
			val.onclick = function() {
				window.open(this.href,'_blank');
				return false;
			}
			val.onkeypress = function() {
				this.onclick();
			}
		}
	}
}

function caret(node) {
 node.focus(); 
 /* without node.focus() IE will returns -1 when focus is not on node */
 if(node.selectionStart) return node.selectionStart;
 else if(!document.selection) return 0;
 var c		= "\001";
 var sel	= document.selection.createRange();
 var dul	= sel.duplicate();
 var len	= 0;
 dul.moveToElementText(node);
 sel.text	= c;
 len		= (dul.text.indexOf(c));
 sel.moveStart('character',-1);
 sel.text	= "";
 return len;
}


// ********************** WALIDACJA FORMULARZY *************************

function val_form_komentarz() {
	var obj = document.getElementById('komentarz-tresc');
	var ok = true;
	if (obj.value.length < 5) {
		alert('Wprowadzona treść komentarza jest za krótka');
		ok = false;
	}
	return ok;
}

function val_form_watek() {
	var obj = document.getElementById('watek-tytul');
	var ok = true;
	if (obj.value.length < 1) {
		alert('Wprowadź jakiś temat wątku');
		ok = false;
	}
	return ok;
}

function val_form_post() {
	var obj = document.getElementById('post-tresc');
	var ok = true;
	if (obj.value.length < 5) {
		alert('Wprowadzona wypowiedź jest za krótka');
		ok = false;
	}
	return ok;
}
