function show_searchloader(){
	$('#list_xajax_content').html('<div class="loader">Suche...<br><img src="/img_08/m03loader.gif"></div>');
}
function folgen_suche_datum(index, format) {
	var jahr, monat, tag;

	jahr = $('#jahr').val();
	monat = $('#monat').val();
	tag = $('#tag').val();
	
	if (isNaN(tag) == true || isNaN(monat) == true || isNaN(jahr) == true) {
		alert("Bitte füllen Sie alle Datumsfelder aus!");
	} else {
		show_searchloader();
		xajax_format_search("datum", 0, format, jahr, monat, tag);
	}
}

function folgen_suche_nummer(index, format) {
	var episode;
	episode = $('#folgen_suche_nummer').val();

	if (isNaN(episode) == true || episode == "") {
		alert("Das Folgennummer-Feld darf nur Ziffern enthalten!");
	} else {
		show_searchloader();
		xajax_format_search("folge", 0, format, episode, "", "");
	}
}

function folgen_suche_wort(index, format) {
	var word;
	word = $('#folgen_suche_wort').val();
	if (word.length < 3) {
		alert("Das Suchwort muss mindestens 3 Zeichen lang sein!");
		return false
	}

	if (isAlphaNum(word) == false) {
		alert("Das Stichwort-Feld darf nur Buchstaben und Ziffern enthalten!");
		return false
	} else {
		$('#suggestion').hide();
		show_searchloader();
		xajax_format_search("wort", index, format, word, "", "");
	}
}

function isAlphaNum(str) {
	if (str.match(/^[a-zA-Z0-9äöüÜÄÖÜß ]+$/)) {
		return true;
	}
	return false
}

var box_id = 0;
var last_search = 0;
var format = "home";

function folgen_suche_suggestion(e, format_name) {
	format = format_name;
	key_event = e || window.event;

	// down 40
	if (key_event.keyCode == 40) {
		// alert(40);
		if (box_id == return_count) {
			select_word_box();
		} else if (box_id >= 0 || box_id < return_count) {
			box_id = box_id + 1;
			select_word_box();
		} else {
			select_word_box();
		}

		return false;
	}
	// up 38
	if (key_event.keyCode == 38) {
		// alert(38);
		if (box_id == 1) {
			select_word_box();
		} else if (box_id > 1 && box_id <= return_count) {
			box_id = box_id - 1;
			select_word_box();
		} else {
			select_word_box();
		}
		return false;
	}

	if (key_event.keyCode == 13) {
		if (box_id > 1 && box_id <= 5) {
			click_word_box(box_id);

		}
		return false;
	}
	lookup_suggestion();
}

var lookup_timeout;
var return_count;
var search_str_length;
function lookup_suggestion() {
	searchstr_len = document.getElementById('folgen_suche_wort').value.length;
	words = document.getElementById('folgen_suche_wort').value.split(" ");
	word_count = words.length - 1;

	if (return_count <= 1
			&& document.getElementById('folgen_suche_wort').value.length >= search_str_length) {
		if (document.getElementById('word_1').innerHTML.lastIndexOf(document
				.getElementById('folgen_suche_wort').value) >= 0) {
			return false;
		}
		if (return_count == 0
				&& document.getElementById('folgen_suche_wort').value.length > 3) {
			return false;
		}

	} else {
		search_str_length = document.getElementById('folgen_suche_wort').value.length;
	}

	if (words[word_count].length >= 3 && searchstr_len <= 50) {
		var jetzt = new Date();
		diff = jetzt.getTime() - last_search;
		if (diff >= 2000 || last_search == 0) {
			xajax_suggetion_search(
					document.getElementById('folgen_suche_wort').value, format);
			clearTimeout(lookup_timeout);
			last_search = jetzt.getTime();
		} else {
			lookup_timeout = setTimeout("lookup_suggestion(format)", 1000);
		}
	}
	if (document.getElementById('folgen_suche_wort').value < 3) {
		document.getElementById('suggestion').style.visibility = "hidden";
		box_id = 0;
		clear_highlight();
	}

}

function select_word_box() {
	if (document.getElementById('suggestion').style.visibility == "visible"
			&& box_id > 0) {
		highlite_word_box(box_id);
		copy_word_box_text(box_id);
	}
}
function highlite_word_box(id) {
	if (document.getElementById('suggestion').style.visibility == "visible") {
		clear_highlight();
		document.getElementById('word_' + id).setAttribute("class", "highlite");
		document.getElementById('word_' + id).setAttribute("className",
				"highlite");
	}

}

function clear_highlight() {
	document.getElementById('word_1').setAttribute("class", "normal");
	document.getElementById('word_1').setAttribute("className", "normal");
	document.getElementById('word_2').setAttribute("class", "normal");
	document.getElementById('word_2').setAttribute("className", "normal");
	document.getElementById('word_3').setAttribute("class", "normal");
	document.getElementById('word_3').setAttribute("className", "normal");
	document.getElementById('word_4').setAttribute("class", "normal");
	document.getElementById('word_4').setAttribute("className", "normal");
	document.getElementById('word_5').setAttribute("class", "normal");
	document.getElementById('word_5').setAttribute("className", "normal");

}

function click_word_box(id) {
	copy_word_box_text(id);
	document.getElementById('suggestion').style.visibility = "hidden";
	box_id = 0;
	clear_highlight();
	folgen_suche_wort(0, format);

}

function copy_word_box_text(id) {
	document.getElementById('folgen_suche_wort').value = document
			.getElementById('word_' + id).innerHTML;
	document.getElementById('folgen_suche_wort').focus();
}

function update_suggestion_search(string) {
	if (string.length > 1) {
		string = string.split("|");
		return_count = string.length - 1;
		for ( var i = 1; i <= return_count; i++) {
			document.getElementById('word_' + i).innerHTML = string[i];
		}
		document.getElementById('suggestion').style.height = return_count * 13;
		xy = findPos(document.getElementById('folgen_suche_wort'));
		document.getElementById('suggestion').style.left = xy[0] - 8;
		document.getElementById('suggestion').style.top = xy[1] - 77;
		document.getElementById('suggestion').style.visibility = "visible";
	} else {
		return_count = 0;
		document.getElementById('suggestion').style.visibility = "hidden";
	}

}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [ curleft, curtop ];
}
