document.charset="utf-8";

document.onmousemove = updateLeadingPosition;

function updateLeadingPosition(e) {
    var y = document.all ? event.clientY + document.documentElement.scrollTop : e.pageY ;
    var x = document.all ? event.clientX + document.documentElement.scrollLeft : e.pageX;
    var loading = document.getElementById("loading");
    loading.style.position = "absolute";
    loading.style.top = y + 20 + "px";
    loading.style.left = x + 20 + "px";
    loading.style.zIndex = 200;
}

function getHTTPObject(){
	if (window.ActiveXObject) 
			return new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest) 
		return new XMLHttpRequest();
	else {
		return null;
	}
}

function ajaxRequest(callable, query) {
	showLoading();
	httpObject = getHTTPObject();
	if (httpObject != null) {
		httpObject.onreadystatechange = function() {
			if(httpObject.readyState == 4) {
				callable();
			}
		};
		httpObject.open("GET", query, true);
		httpObject.send(null);
		
	} else {
		document.getElementById("messageText").innerHTML = "Ihr Browser unterst&uuml;tzt leider kein Ajax!";
		document.getElementById("message").setAttribute('class', 'errorMessage');
	}
}

function showLoading() {
	document.getElementById("loading").style.display = "block";
}

function stopLoading() {
	document.getElementById("loading").style.display = "none";
}

function encode_utf8(s) {
	return unescape(encodeURIComponent(s));
}

function ajaxPostRequest(callable, query, postString) {
	showLoading();
	httpObject = getHTTPObject();
	if (httpObject != null) {
		httpObject.onreadystatechange = function() {
			if(httpObject.readyState == 4) {
				callable();
			}
		};
		httpObject.open("POST", query, true);
		httpObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		httpObject.setRequestHeader("Content-length", postString.length);
		httpObject.setRequestHeader("Connection", "close");
		httpObject.send(encode_utf8(postString));
	} else {
		document.getElementById("messageText").innerHTML = "Ihr Browser unterst&uuml;tzt leider kein Ajax!";
		document.getElementById("message").setAttribute('class', 'errorMessage');
	}
}

function getReport(resultId) {
	ajaxRequest(delayOverlay, "/bericht/" + resultId);
}

function getFileUploadFormReport() {
  id = document.getElementById('reportId').value;
  ajaxPostRequest(delayOverlay, "/fileupload/form", "reportId=" + id);
}

function getFileUploadForm() {
  id = document.getElementById('eventId').value;
  ajaxPostRequest(delayOverlay, "/fileupload/form", "eventId=" + id);
}

function getPhoto() {
	ajaxRequest(delayOverlay, "/galerie");
}

function getProfile(playerId) {
	ajaxRequest(delayOverlay, "/spieler/" + playerId);
}

function delayOverlay() {
	setTimeout("setOverlay()", 200);
}

function setOverlay() {
	stopLoading();	
	document.getElementById("overlayContent").innerHTML = httpObject.responseText;
	showOverlay();
}

function getSeasons() {
	var teamCell = document.getElementById("team");
	document.getElementById("season").innerHTML = '<select disabled="true"><option value="0">Keine Einträge</option></select>';
	document.getElementById("result").innerHTML = '<select disabled="true"><option value="0">Keine Einträge</option></select>';
	document.getElementById("createButton").setAttribute('disabled', 'true');	
	document.getElementById("createButton").setAttribute('value', 'Spielbericht erstellen');
	document.getElementById("deleteButton").setAttribute('disabled', 'true');
	var teamSelect = teamCell.getElementsByTagName("select")[0];
	var team = teamSelect.options[teamSelect.selectedIndex].value;
	if(team != 0) {
		ajaxRequest(setSeasons, "/api/seasons/" + team);
	}
}

function setSeasons() {
	stopLoading();	
	var seasonCell = document.getElementById("season");
	var xmlDoc = httpObject.responseXML;
	if(xmlDoc.getElementsByTagName("seasons").length > 0) {
		var seasons = xmlDoc.getElementsByTagName("season");
		var select = document.createElement("select");
		select.setAttribute("onchange", "getResultsForReportCreation();");
		select.setAttribute("name", "season");
		var option = document.createElement("option");
		option.appendChild(document.createTextNode("Bitte wählen"));
		select.appendChild(option);
		for(var i = 0; i < seasons.length; i++) {
			var season = seasons[i];
			var seasonId = season.getAttribute("id");
			var year = season.getElementsByTagName("year")[0].firstChild.nodeValue;
			var league = season.getElementsByTagName("league")[0].firstChild.nodeValue;
			option = document.createElement("option");
			option.setAttribute("value", seasonId);
			option.appendChild(document.createTextNode(year + " - " + league));
			select.appendChild(option);
		}
		seasonCell.innerHTML = "";
		seasonCell.appendChild(select);
	} else {
		seasonCell.innerHTML = "";
		seasonCell.appendChild(document.createTextNode("Es sind leider keine Saisons verfügbar!"));
	}
}

function getEvents() {
	var yearCell = document.getElementById("year");
	document.getElementById("event").innerHTML = '<select disabled="true"><option value="0">Keine Einträge</option></select>';
	document.getElementById("createButton").removeAttribute('disabled');	
	document.getElementById("createButton").setAttribute('value', 'Ereignis erstellen');
	document.getElementById("deleteButton").setAttribute('disabled', 'true');
	var yearSelect = yearCell.getElementsByTagName("select")[0];
	var year = yearSelect.options[yearSelect.selectedIndex].value;
	if(year != 0) {
    document.getElementById('year2').setAttribute('value', year);
		ajaxRequest(setEvents, "/api/events/" + year);
	} else {
    document.getElementById('year2').setAttribute('value', '');
    CKEDITOR.instances.body.setData('');
		document.getElementById("title").value = "";
		document.getElementById("date").value = "";
    document.getElementById("createButton").setAttribute('disabled', 'true');
  }
}

function setEvents() {
	stopLoading();	
	var eventCell = document.getElementById("event");
	var xmlDoc = httpObject.responseXML;
	if(xmlDoc.getElementsByTagName("events").length > 0) {
		var events = xmlDoc.getElementsByTagName("event");
		var select = document.createElement("select");
		select.setAttribute("onchange", "getEventContent();");
		select.setAttribute("name", "event");
		var option = document.createElement("option");
		option.appendChild(document.createTextNode("Neues Ereignis... "));
    option.setAttribute('value', '0');
		select.appendChild(option);
		for(var i = 0; i < events.length; i++) {
			var event = events[i];
			var eventId = event.getAttribute("id");
			var title = event.getElementsByTagName("title")[0].firstChild.nodeValue;
			var date = event.getElementsByTagName("date")[0].firstChild.nodeValue;
			option = document.createElement("option");
			option.setAttribute("value", eventId);
			option.appendChild(document.createTextNode(date + " - " + title));
			select.appendChild(option);
		}
		eventCell.innerHTML = "";
		eventCell.appendChild(select);
	} else {
		eventCell.innerHTML = "";
		eventCell.appendChild(document.createTextNode("Es sind leider keine Saisons verfügbar!"));
	}
}

function getResultsForReportCreation() {
	var seasonCell = document.getElementById("season");
	document.getElementById("result").innerHTML = '<select disabled="true"><option value="0">Keine Einträge</option></select>';
	document.getElementById("createButton").setAttribute('disabled', 'true');
	document.getElementById("deleteButton").setAttribute('disabled', 'true');
	document.getElementById("createButton").setAttribute('value', 'Spielbericht erstellen');	
	var seasonSelect = seasonCell.getElementsByTagName("select")[0];
	var season = seasonSelect.options[seasonSelect.selectedIndex].value;
	if(season != 0) {
		ajaxRequest(setResults, "/api/results/" + season);
	}
}

function setResults() {
	stopLoading();	
	var resultCell = document.getElementById("result");
	var xmlDoc = httpObject.responseXML;
	if(xmlDoc.getElementsByTagName("results").length > 0) {
		var results = xmlDoc.getElementsByTagName("result");
		var select = document.createElement("select");
		select.setAttribute("onchange", "getReportContent();");
		select.setAttribute("name", "result");
		var option = document.createElement("option");
		option.setAttribute("value", "0");
		option.appendChild(document.createTextNode("Bitte wählen"));
		select.appendChild(option);
		for(var i = 0; i < results.length; i++) {
			var result = results[i];
			var resultId = result.getAttribute("id");
			var date = result.getElementsByTagName("date")[0].firstChild.nodeValue;
			var homeTeam = result.getElementsByTagName("homeTeam")[0].firstChild.nodeValue;
			var guestTeam = result.getElementsByTagName("guestTeam")[0].firstChild.nodeValue;
			var goals = result.getElementsByTagName("goals")[0].firstChild.nodeValue;
			var report = result.getElementsByTagName("report")[0].firstChild.nodeValue;
			option = document.createElement("option");
			if(report == "yes") {
				option.setAttribute("class", "reportInUse");
			}
			option.setAttribute("value", resultId);
			option.appendChild(document.createTextNode("[" + date + "]" + " " + homeTeam + " - " + guestTeam + " " + goals));
			select.appendChild(option);
		}
		resultCell.innerHTML = '';
		resultCell.appendChild(select);
	} else {
		resultCell.innerHTML = '<select disabled="true"><option value="0">Keine Einträge</option></select>';
		resultCell.appendChild(document.createTextNode("Es sind leider keine Ergebnisse verfügbar!"));
	}
}

function getReportContent() {
	var resultCell = document.getElementById("result");
	var resultSelect = resultCell.getElementsByTagName("select")[0];
	var result = resultSelect.options[resultSelect.selectedIndex].value;
	if(result != 0) {
		ajaxRequest(setReportContent, "/api/report/" + result);
	} else {
		document.getElementById("body").value = "";
		document.getElementById("players").value = "";
	}
}

function getEventContent() {
	var eventCell = document.getElementById("event");
	var eventSelect = eventCell.getElementsByTagName("select")[0];
	var event = eventSelect.options[eventSelect.selectedIndex].value;
	if(event != 0) {
		ajaxRequest(setEventContent, "/api/event/" + event);
	} else {
    CKEDITOR.instances.body.setData('');
		document.getElementById("title").value = "";
		document.getElementById("date").value = "";
	  document.getElementById("deleteButton").setAttribute('disabled', 'true');
	}
}

function setEventContent() {
	stopLoading();
	document.getElementById("createButton").removeAttribute('disabled');
	response = httpObject.responseText;
	if(response.length > 0) {
		// Spielbericht vorhanden 
		document.getElementById("createButton").setAttribute('value', 'Ereignis aktualisieren');
		document.getElementById("deleteButton").removeAttribute('disabled');
		title = response.substring(response.indexOf('<title>') + 7, response.indexOf('</title>'));
		date = response.substring(response.indexOf('<date>') + 6, response.indexOf('</date>'));
		content = response.substring(response.indexOf('<content') + 9, response.indexOf('</content>'));
		CKEDITOR.instances.body.setData(content);
		document.getElementById("title").value = title;
		document.getElementById("date").value = date;	
    document.getElementById("eventId").value = id;
	} else {
		// kein Spielbericht vorhanden
		document.getElementById("createButton").setAttribute('value', 'Spielbericht erstellen');
		document.getElementById("deleteButton").setAttribute('disabled', 'true');
		CKEDITOR.instances.body.setData('');
		document.getElementById("title").value = '';
		document.getElementById("date").value = '';	
	}
}

function getContents() {
	var areaCell = document.getElementById("area");
	var areaSelect = areaCell.getElementsByTagName("select")[0];
	var area = areaSelect.options[areaSelect.selectedIndex].value;
	if(area != 0) {
		ajaxRequest(setContents, "/api/content/" + area);
	} else {
		document.getElementById('createButton').setAttribute('disabled', 'true');
		CKEDITOR.instances.text.setData('');
	}
}

function setContents() {
	stopLoading();
	document.getElementById('createButton').removeAttribute('disabled');
	response = httpObject.responseText;
	stopLoading();
	area = response.substring(response.indexOf('<content>') + 9, response.indexOf('</content>'));
	CKEDITOR.instances.text.setData(area);
}

function setReportContent() {
	stopLoading();
	document.getElementById("createButton").removeAttribute('disabled');
	response = httpObject.responseText;
	if(response.length > 0) {
		// Spielbericht vorhanden 
		document.getElementById("createButton").setAttribute('value', 'Spielbericht aktualisieren');
		document.getElementById("deleteButton").removeAttribute('disabled');
		id = response.substring(response.indexOf('<id>') + 4, response.indexOf('</id>'));
		body = response.substring(response.indexOf('<body>') + 6, response.indexOf('</body>'));
		players = response.substring(response.indexOf('<players>') + 9, response.indexOf('</players>'));
		CKEDITOR.instances.body.setData(body);
		document.getElementById("players").value = players;	
    document.getElementById("reportId").value = id;
	} else {
		// kein Spielbericht vorhanden
		document.getElementById("createButton").setAttribute('value', 'Spielbericht erstellen');
		document.getElementById("deleteButton").setAttribute('disabled', 'true');
		CKEDITOR.instances.body.setData('');
		document.getElementById("players").value = "";
	}
}

