// Quelle: http://www.devblog.de
// "Shoutbox als AJAX-Anwendung"
// Code darf frei verwendet werden

// globale Instanz von XMLHttpRequest
var xmlHttp = false;

// XMLHttpRequest-Instanz erstellen
// ... für Internet Explorer
try {
    xmlHttp  = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
    try {
        xmlHttp  = new ActiveXObject("Microsoft.XMLHTTP");
    } catch(e) {
        xmlHttp  = false;
    }
}
// ... für Mozilla, Opera und Safari
if (!xmlHttp  && typeof XMLHttpRequest != 'undefined') {
    xmlHttp = new XMLHttpRequest();
}

// aktuelle Daten laden
loadData();

//Zeitangabe für das Interval in Millisekunden
var reloadtime = 5000;

//Prüfen ob URI Parameter gesetzt sind
//Wird ermittelt, dass sich der User in media_movies befindet -> reloadtime im interval hochsetzen
if(window.location.search != ""){
	//URI Parameter auslesen
	var strUrlParams = window.location.search;
	//Splitten nach jedem &
	var arrUrlParams = strUrlParams.split("&");
	
	//Übereinstimmung prüfen und für den movie-bereich den sb_reload hochsetzen
	if(arrUrlParams[0] + "&" + arrUrlParams[1] == "?rub=media&sub=movies"){
		//Einmaliges laden, damit der aktuelle content noch angezeigt wird
		loadData();
		//sb_reload interval erhöhen
		reloadtime = 600000;
	}
}

// Interval setzen um neue Daten anzufordern
setInterval("loadData()", reloadtime);

function loadData()
{
	if (xmlHttp) {
		xmlHttp.open('GET', 'inc/sb_getdata.php', true);
		xmlHttp.onreadystatechange = function () {
			if (xmlHttp.readyState == 4) {
				document.getElementById("asb_content").innerHTML = xmlHttp.responseText;
			}
		}
		xmlHttp.send(null);
	}
} //end function loadData

/* Durch saveData2 abgelöst!
function saveData()
{
	if (xmlHttp) {
		xmlHttp.open('POST', 'inc/sb_setdata.php');
		xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlHttp.send('sbauthor='+document.frmshoutbox.sbauthor.value+'&sbtopic='+document.frmshoutbox.sbtopic.value+'&sbsec_code='+document.frmshoutbox.sbsec_code.value);
	}
	
	//Shoutbox-Formularfelder leeren und Focus auf 'sbtopic' setzen
	document.frmshoutbox.sbauthor.value = '';
	document.frmshoutbox.sbsec_code.value = '';
	document.frmshoutbox.sbtopic.value = '';
	document.frmshoutbox.sbtopic.focus();
	//Shoutbox reload
	loadData();
} //end function saveData
*/

/* Funktion wie saveData, allerdings andere Umsetzung */
function saveData2(){
	var myAjax = new Ajax.Request(
				'inc/sb_setdata.php',
				{
					method: 'post',
					parameters: 'sbauthor='+document.frmshoutbox.sbauthor.value+'&sbtopic='+document.frmshoutbox.sbtopic.value+'&sbsec_code='+document.frmshoutbox.sbsec_code.value,
					onComplete: loadData
				}
	);
	
	//Shoutbox-Formularfelder leeren und Focus auf 'sbtopic' setzen
	document.frmshoutbox.sbauthor.value = '';
	document.frmshoutbox.sbsec_code.value = '';
	document.frmshoutbox.sbtopic.value = '';
	document.frmshoutbox.sbtopic.focus();
}