
function del_window_status(){
	window.status=''
	return true
}

document.onmouseover=del_window_status;
document.onmouseout=del_window_status;


function format_money(money_field){
	
	cleared_input = "";
	users_input = money_field.value;
	
	for (i = 0; i < users_input.length; i++) {
		tmp = parseInt(users_input.charAt(i));
		if (!isNaN(tmp)) 
			cleared_input += tmp;
	}

	if(cleared_input.length == 0)
		cleared_input = "0";

	result = "";
	digit_count = 0;
	for (i = cleared_input.length - 1; i > -1; i--){
		if(digit_count == 3){
			result = '.' + result;
			digit_count = 0;
		}

		result = cleared_input.charAt(i) + result;
		digit_count++;
	}

	money_field.value = result;
}

function set_wait(my_button) {
	my_button.value="bitte warten...";
	my_button.disabled=true;
	return true;
}

/* Timeticker */
var tmtime;
function startTmTime(starttime) {
    tmtime = starttime;
    window.setInterval("updateTmTime()", 1000);
}

function updateTmTime() {
    tmtime = tmtime + 1;
    
    var timeElem = document.getElementById("tmtime");
    if(!timeElem) 
		return;
    
    var seconds = tmtime % 86400;
    var minutes = Math.floor(seconds / 60);
    var hours = Math.floor(minutes / 60);
    seconds = seconds - (minutes * 60);
    minutes = minutes - (hours * 60);
    
    if(seconds < 10) 
    	seconds = "0" + seconds;
    if(minutes < 10) 
    	minutes = "0" + minutes;
    hours = hours % 24;
    if(hours < 10) 
		hours = "0" + hours;
    
    var timetext = hours + ":" + minutes + ":" + seconds;
    
    timeElem.innerHTML = timetext;
    return;
}

/*  Klappboxen */
function switch_clapbox(box_id){

	var el = document.getElementById(box_id);

	if(el.style.display == "block")
		el.style.display = "none";
	else
		el.style.display = "block"
}

/* Hilfe-Seiten */
function show_help_page(file_name){

	if(file_name == null)
		return;

	open("html/help/" + file_name, "help_window", "height=400,width=600,scrollbars=yes,resizable=yes");
}

/* Match-Control Center */
function start_match_control_center(target){

	if(target == null)
		return;

	open(target, "match_control_center", 
		"height=802,width=1030,scrollbars=yes,resizable=yes,toolbar=no,status=no");
}


/* Land- und Liga-Navigation */

function NaviConfederation(id, name, countries){
	this.id = id;
	this.name = name;
	this.countries = countries;
}

function NaviCountry(id, name, leagues){
	this.id = id;
	this.name = name;
	this.leagues = leagues;
}

function NaviLeague(id, name, link){
	this.id = id;
	this.name = name;
	this.link = link;
}

function switch_navi_element(elem_name, sw){

	var el = document.getElementById(elem_name);

	if(sw == 'on'){
		el.style.display = "block";
	}
	if(sw == 'off'){
		el.style.display = "none";
	}
}

function draw_confederation_navi_list(){

	var content ="<div class='colibox' style='width:574px;'" 
	content += 		"onmouseover=\"switch_navi_element('confederation_list', 'on')\"";
	content += 		"onmouseout=\"switch_navi_element('confederation_list', 'off')\">";

	content += "<h2 style='padding:0; margin:0;'>Konf&ouml;deration w&auml;hlen</h2>";

	content += "<table class='spTabellesysExp' border='0' cellpadding='0' cellspacing='0' id='confederation_list'>";
	
	var j = 0;
	for(var i in navi_confederations) {
		var confederation = navi_confederations[i];

		if(j == 0)
			content += "<tr>";

		content += "<td style='width:143px;'><a class='colibox' href='javascript:void(0);' onclick='draw_country_navi_list(" 
					+ confederation.id + ")'>" + confederation.name + "</a></td>";

		if(j == 3){
			content += "</tr>";
			j = -1;
		}

		j++;
	}

	content += "</table>";
	content += "</div>";

	// Confederation
	var elem = document.getElementById("confederation_data");
	elem.innerHTML = content;

	// Country-Liste ausblenden
	var elem = document.getElementById("country_data");
	elem.innerHTML = "";
	elem = document.getElementById("country_reset_data");
	elem.innerHTML = "";

	// Ligen-Liste ausblenden
	var elem = document.getElementById("league_data");
	elem.innerHTML = "";
}

function draw_country_navi_list(confederation_id, selected_country_id){

	var my_confederation = navi_confederations[confederation_id];
	var my_countries = my_confederation.countries;

	var selected_country;
	if(selected_country_id != null)
		selected_country = my_countries[selected_country_id];

	var content ="<div class='colibox' style='width:580px;'" 
	content += 		"onmouseover=\"switch_navi_element('country_list', 'on')\"";
	content += 		"onmouseout=\"switch_navi_element('country_list', 'off')\">";

	if(selected_country == null)
		content += "<h2 style='padding:0; margin:0;'>Land w&auml;hlen</h2>";
	else
		content += "<h2 style='padding:0; margin:0;'>" + selected_country.name + "</h2>";

	content += "<table class='spTabellesysExp' border='0' cellpadding='0' cellspacing='0' id='country_list'>";
	
	var j = 0;
	for(var i in my_countries) {
		var country = my_countries[i];

		if(j == 0)
			content += "<tr>";

		content += "<td style='width:143px;'><a class='colibox' href='javascript:void(0);' onclick='draw_league_navi_list(" 
					 + confederation_id + "," + country.id + ")'>" + country.name + "</a></td>";

		if(j == 3){
			content += "</tr>";
			j = -1;
		}

		j++;
	}

	content += "</table>";
	content += "</div>";

	// Confederation
	var elem = document.getElementById("confederation_data");
	elem.innerHTML = "<div class='colibox' style='width:180px;'>" + my_confederation.name + "</div>";

	// Aktivierung der Confederation-Auswahl
	elem = document.getElementById("country_data");
	elem.innerHTML = "<div class='colibox' style='width:384px;'>" 
		+ "<a class='colibox' href='javascript:void(0);' onclick='draw_confederation_navi_list()'>Konf&ouml;deration w&auml;hlen</a>" 
		+ "</div>";

	// Country-Auswahl
	var elem = document.getElementById("league_data");
	elem.innerHTML = content;

	// XXX ausblenden
	var elem = document.getElementById("country_reset_data");
	elem.innerHTML = "";

}

function draw_league_navi_list(confederation_id, country_id, selected_league_id){

	var my_confederation = navi_confederations[confederation_id];
	var my_countries = my_confederation.countries;	
	var my_country = my_countries[country_id];
	var my_leagues = my_country.leagues;

	var selected_league;
	if(selected_league_id != null)
		selected_league = my_leagues[selected_league_id];

	var content ="<div class='colibox' style='width:580px;'" 
	content += 		"onmouseover=\"switch_navi_element('league_list', 'on')\"";
	content += 		"onmouseout=\"switch_navi_element('league_list', 'off')\">";

	if(selected_league == null)
		content += "<h2 style='padding:0; margin:0;'>Liga w&auml;hlen</h2>";
	else
		content += "<h2 style='padding:0; margin:0;'>" + selected_league.name + "</h2>";

	content += "<table class='spTabellesysExp' style='display:none;' border='0' cellpadding='0' cellspacing='0' id='league_list'>";

	var j = 0;
	for(var i in my_leagues) {
		var league = my_leagues[i];

		if(selected_league != null && selected_league.id == league.id)
			continue;

		if(j == 0)
			content += "<tr>";

		content += "<td style='width:186px;'><a class='colibox' href='" 
			+ league.link + "'>" + league.name + "</a></td>";

		if(j == 2){
			content += "</tr>";
			j = -1;
		}

		j++;
	}

	content += "</table>";
	content += "</div>";

	// Ligen-Auswahl
	var elem = document.getElementById("league_data");
	elem.innerHTML = content;

	// Confederation
	var elem = document.getElementById("confederation_data");
	elem.innerHTML = "<div class='colibox' style='width:184px;'>" + my_confederation.name + "</div>";

	// Aktivierung der Country-Auswahl
	elem = document.getElementById("country_reset_data");
	elem.innerHTML = "<div class='colibox' style='width:184px;'>" 
		+ "<a class='colibox' href='javascript:void(0);' onclick='draw_country_navi_list(" + confederation_id + ")'>Land w&auml;hlen</a>" 
		+ "</div>";

	// Country
	elem = document.getElementById("country_data");
	elem.innerHTML = "<div class='colibox' style='width:184px;'>" + my_country.name + "</div>";
}


/* Verschiedenes */
function trim (mytext) {
	return mytext.replace (/^\s+/, '').replace (/\s+$/, '');
}

/* Bildertausch */
function change_menu_img(elem_name, on){
	var elem = get_element(elem_name, false);
	if(on)
		elem.src = '../img/' + elem_name + '_active.png';
	else
		elem.src = '../img/' + elem_name + '.png';
}




/////////////////////////////////////////////////
// Library
/////////////////////////////////////////////////

// Liefert Elements oder das style-Objekt des Elements mit dem angegebenen Namen
function get_element(elemname, style) {
    var Elem;

	if (document.getElementById) {
		Elem = document.getElementById(elemname);
		if(!Elem) 
			return false;

		if(style) 
			Elem = Elem.style;
    } else if(document.layers) {
        Elem = document.layers[elemname];
    } else if(document.all) {
        Elem = document.all(elemname)
        if(!Elem) 
			return false;
        if(style) 
			Elem = Elem.style;
    }

    return Elem;
}

// Liefert die Koordinaten des uebergebenen Elementes
function get_coordinates(objname) {
    var curleft = 0;
    var curtop = 0;

    var obj = document.getElementById(objname);
    if(!obj) 
		return [0,0];

    curleft = obj.offsetLeft;
    curtop = obj.offsetTop;
    if(obj.offsetParent) {
        while(obj = obj.offsetParent) {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        }
    }

    return [curleft,curtop];
}

String.prototype.trim = function() {

	// skip leading and trailing whitespace
	// and return everything in between
	var x = this;
	x = x.replace(/^\s*(.*)/, "$1");
	x = x.replace(/(.*?)\s*$/, "$1");
	return x;
}


/////////////////////////////////////////////////
// Debug 
/////////////////////////////////////////////////

var debug_mode = false;
var debug_window;

function init_debug_window(){

	if (!debug_mode)
		return;

	debug_window = open("", "debug_window", "scrollbars=yes,height=400,width=800,resizable=yes");
	debug_window.document.open();

	with(debug_window.document){
		write("<html><head></head><body>");
		write("<h1>Debuginfo</h1>");
		write("Maus: <span id='mouse_coordinates_box'>abc</span><br>");
	
		try{
			var dummy = myAthletes.length;
		} catch(e) {
			return;
		}
			
		for(var i in myAthletes){
			var athlete = myAthletes[i];
			write("athlete " + athlete.athlete_id + ": <span id='sp" + athlete.athlete_id + "'>abc</span><br>");
		}

	}
}

function debug(text){

	if(debug_window == null || debug_window.document == null)
		return;

	var mydate = new Date();
	var mytime = mydate.getMinutes() + ":" + mydate.getSeconds() + " ";

	debug_window.document.write(mytime + text);
	debug_window.document.write("<br>");
}

