/******************************
	INITIALIZATION
******************************/
function initBerichtAnzeige(hidestadt, seltyp) {
	if (hidestadt) hide(elid('div_stadt'));
	hide(elid('div_optionen')); hide(elid('bt_optionen'));
	io = elid('optionen_bullet'); io.src = 'typo3/gfx/ol/plusbullet.gif';
	if (seltyp) {
		st = elid('sel_typ');
		for (i = 0; i < st.options.length; i++) {
			if (st.options[i].value == seltyp) st.options.selectedIndex = i;
		}
		cb_selectOnly('typ[]', seltyp);
		hide(elid('div_typoptionen'));
	}
}

/******************************
	GENERAL PURPOSE FUNCTIONS
******************************/
function elid (what) { return document.getElementById(what); }
function show(el) { el.style.display = 'block'; }
function hide(el) { el.style.display = 'none'; }
function setV(el, v) { el.value = v; }
function setI(el, i) { el.innerHTML = i; }
function condSetV(el, cond) { if (el.value == cond) setV(el, el.title); }
function inpfc(el) { if (el.value == el.title) el.value = ''; }
function inpbl(el) { if (el.value == '') el.value = el.title; }
function chk(el) { return (el.value != '' && el.value != el.title); }

/******************************
	SELECT EVENT FUNCTIONS
******************************/
function evt_selectTyp() {
	st = elid('sel_typ');
	hide(elid('div_stadt'));
	if (st.selectedIndex) {
		cb_selectOnly('typ[]', st.value);
		hide(elid('div_typoptionen'));
	} else {
		cb_selectOnly('typ[]', 'ALL');
		show(elid('div_typoptionen'));
	}
	berichtAJAX_loadLand(st.value);
	show(elid('div_land'));
	hide(elid('bt_optionen'));
}

function evt_selectLand() {
	ds = elid('div_stadt');
	st = elid('sel_typ');
	sl = elid('sel_land');
	hide(elid('bt_optionen'));
	if (sl.selectedIndex) {
		berichtAJAX_loadStadt(st.value, sl.value);
		show(ds);
	} else {
		hide(ds);
	}
}

function evt_selectStadt() {
	st = elid('sel_typ'); sl = elid('sel_land'); ss = elid('sel_stadt'); show(elid('bt_optionen'));
	if (ss.value != "") berichtAJAX_loadBerichte(st.value, sl.value, ss.value, flatten('typ[]'), flatten('fach[]'));
}

function evt_selectBericht(uid) {
	dc = elid('contact_'+uid);
	if (dc.innerHTML.length > 0) dc.innerHTML = '';
	else {
		var els = document.getElementsByName('contact_div');
		for (i = 0; i < els.length; i++) if (els[i].innerHTML.length > 0) els[i].innerHTML = '';
		//FUNKTIONIERT NUR IN FIREFOX: window.scrollTo(0, elid('contact_img_'+uid).y - 50);
		berichtAJAX_loadContact(uid);
	}
}

function evt_selectMedia(uid) {
	dm = elid('media_'+uid);
	if (dm.innerHTML.length > 0) {
		if (dm.style.display == 'none') dm.style.display = 'block';
		else dm.style.display = 'none';
	} else {
		berichtAJAX_loadMedia(uid);
	}
}

function evt_submitContact(uid) {
	sm = elid('sender_mail_'+uid);
	cm = elid('content_mail_'+uid);
	if (!sm.value.match(/^[A-Za-z0-9\._-]+[@][A-Za-z0-9\._-]+[\.].[A-Za-z0-9]+$/)) {
		alert("Bitte gib eine gültige eMail-Adresse ein.");
		sm.focus();
		sm.select();
	} else if (cm.value == '') {
		alert("Bitte fülle die Mail noch mit Inhalt bevor du sie verschickst.");
		cm.focus();
		cm.select();
	} else {
		berichtAJAX_submitContact(xajax.getFormValues('contact_frm_'+uid));
	}
}

function berichte_toggleOptions() {
	dopt = elid('div_optionen');
	io = elid('optionen_bullet');
	if (dopt.style.display == 'none') {
		show(dopt);
		io.src = 'typo3/gfx/ol/minusbullet.gif';
	} else {
		hide(dopt);
		io.src = 'typo3/gfx/ol/plusbullet.gif';
	}
}

function cb_selectOnly(nm, val) {
	var els = document.getElementsByName(nm);
	for (i = 0; i < els.length; i++) {
		if (els[i].value == val || val == "ALL")
			els[i].checked = true;
		else
			els[i].checked = false;
	}
	show(elid('bt_optionen'));
}

function flatten(nm) {
	var tmp = '';
	var els = document.getElementsByName(nm);
	for (i = 0; i < els.length; i++) {
		if (els[i].checked) tmp += els[i].value+'|';
	}
	return tmp;
}
