function toggleTeam (t) {
  if (t.length > 1) {
    $("#team").css("display","inline");
    $("#lbTeam").css("display","inline");
  } else {
    $("#team").css("display","none");
    $("#lbTeam").css("display","none");
  }
}

function newAbt(s) {
var j=0;
for (j=0;j<orgIDs.length;j++) {
	if (orgIDs[j][0]==s) break;
}
//alert("in newAbt s="+s+" j="+j);
$("#team").empty();
//alert("teams[s].length="+teams[s].length+" orgIDs[s].length="+orgIDs[s].length);
for (var i=0;i<teams[j].length;i++) {
	var opt = '<option';
	if (orgIDs[j][i] == preOrgID) opt = opt + ' selected';
  opt = opt+' value="' + orgIDs[j][i] + '">' + teams[j][i] + '</option>';
	$("#team").append(opt);
}
toggleTeam(orgIDs[j]);
}			

function isInOrg(o,a) {
	for (var i=0;i<o.length;i++) {
		if (o[i] == a) return true;
	}
	return false;
}

function initAbt(a) {
//alert("in initAbt a="+a);
var sel=0;
for (var i=0;i<abts.length;i++) {
	var opt = '<option';

	if (isInOrg(orgIDs[i], a)) {
		opt = opt + ' selected';
		newAbt(orgIDs[i][0]);
		sel=i;
	}

  opt = opt+' value="' + orgIDs[i][0] + '">' + abts[i] + '</option>';
	$("#abt").append(opt);
}
toggleTeam(orgIDs[sel]);
preOrgID = -1;
}

function setOrt(o,plz) {
	var id = "#"+o;
	var ort = " ";
	switch (plz) {
		case "21445": ort = "Wulfsen"; break;
		case "21441": ort = "Garstedt"; break;
		case "21442": ort = "Toppenstedt"; break;
		case "21444": ort = "Vierhöfen"; break;
		case "21423": ort = "Winsen (Luhe)"; break;
		case "21435": ort = "Stelle"; break;
		case "21376": ort = "Salzhausen"; break;
	}
	$(id).attr("value",ort);
//alert("in setOrt o="+o+" plz="+plz+" id="+id+" ort="+ort);
}

function testDate (dat,em) {
  //alert ("em="+em);
  $("#"+em).text("");
  var dd;
  var mm;
  var yyyy;
  if (dat.match(/\d+\.\d+\.\d+/)) {
    var dmy = dat.split(".");
//    alert("dmy="+dmy.length+"/"+dmy);
    var d = parseInt(dmy[0],10);
    if (d < 1 || d > 31) {$("#"+em).text("Tag zwischen 1 und 31 eingeben!"); return dat;}
    if (d < 10) dd = "0"+d; else dd=d;
    var m = parseInt(dmy[1],10);
    if (m < 1 || m > 12) {$("#"+em).text("Monat zwischen 1 und 12 eingeben!"); return dat;}
    if (m<10) mm = "0"+m; else mm=m;
    var yy = parseInt(dmy[2],10);
    if (yy < 100) yy += 2000;
//    alert (d+"/"+m+"/"+yy);
    var dx = new Date(yy,m-1,d);
    if (dx.getDate() != d) {$("#"+em).text("Datum ungültig!"); return dat;}
    return dd+"."+mm+"."+yy;
  } else {$("#"+em).text("Bitte Datum in der Form t.m.j eingeben!"); return dat;}
}

function testTime (d,em) {
  $("#"+em).text("");
  var hh;
  var mm;
  if (d.match(/\d+:\d+/)) {
    var hm = d.split(":");
    var h = parseInt(hm[0],10);
    if (h < 0 || h > 23) {$("#"+em).text("Stunde zwischen 0 und 23 eingeben!"); return d;}
    if (h < 10) hh = "0"+h; else hh=h;
    var m = parseInt(hm[1],10);
    //alert("hm="+hm+" / m="+m);
    if (m < 0 || m > 59) {$("#"+em).text("Minuten zwischen 0 und 59 eingeben!"); return d;}
    if (m<10) mm = "0"+m; else mm=m;
    return hh+":"+mm;
  } else $("#"+em).text("Bitte Zeit in der Form hh:mm eingeben!");
}

function testTimeGT (d1,d2,em) { // prüft, ob Datum korrekt und größer d2
  $("#"+em).text("");
  var z1 = d1.split(":");
  var z2 = d2.split(":");
  if (z1[0] > z2[0] || z1[0] == z2[0] && z1[1] > z2[1]) $("#"+em).text("Zeit muss größer sein!");
}

function testErgebnis (erg,em) {
  //alert ("em="+em);
  $("#"+em).text("");
  var auetal;
  var gegner;
  if (erg=="" || erg.match(/\d+\:\d+/)) {
    return erg;
  } else {$("#"+em).text("Bitte Ergebnis in der Form Auetal : Gegner eingeben!"); return dat;}
}

var ispfut=-1;
var anzSpF=0;
var actSpF;
var nextSpF;
var aktivSpF;
var actTopF=50;
var actHeightF=0;
var speedF=2555;
var slideSpeedF=40;
var timeoutF=actTopF*slideSpeedF+1000;

var isperg=-1;
var anzSpE=0;
var actSpE;
var nextSpE;
var aktivSpE;
var actTopE=80;
var actHeightE=0;
var speedE=3000;
var slideSpeedE=10;
var timeoutE=actTopE*slideSpeedE+1700;
var doSlideErg=true;

function initSpielFut() {
  ispfut=0;
  isperg=0;
  nichtsAnzeigen=true;
  //alert("initSpielFut anzSpF="+anzSpF);
  if (!($("div#events").length))
  $("div#ergebnis").css("border-top-width","0");
  else nichtsAnzeigen=false;
  
  if (anzSpE==0) {
    $("div#ergebnis").css("display","none");
    $("div#spiel").css("border-top-width","0");
  }
  else {
    actSpE="#spielErg0";
    nichtsAnzeigen=false;
    if (anzSpE > 1) window.setTimeout("nextSpielErg()", 2500);
    //alert("initSpielFut actSpE="+actSpE);
    $(actSpE).css("height","76px");
  }
  
  if (anzSpF==0) {
    $("div#spiel").css("display","none");
  }
  else {
    actSpF="#spielFut0";
    nichtsAnzeigen=false;
    if (anzSpF>1) window.setTimeout("nextSpielFut()", 2000);
    $(actSpF).css("height","46px");
  }
  if (nichtsAnzeigen) $("div#eventCol").css("border-width","0");
}

function nextSpielFut() {
  actTopF=50;
  actHeightF=0;
  ispfut++;
  if (ispfut==anzSpF) ispfut=0;
  nextSpF="#spielFut"+ispfut;
  aktivSpF = setInterval("slideSpielFut('"+actSpF+"','"+nextSpF+"')",slideSpeedF);
  window.setTimeout("nextSpielFut()", timeoutF);
  actSpF=nextSpF;
}
function slideSpielFut(a,b) {
  actTopF-=1;
  actHeightF+=1;

  $(b).css("top",parseInt(actTopF)+"px");
  $(b).css("height",parseInt(actHeightF)+"px");
  $(a).css("height",parseInt(actTopF)+"px");
  if (actTopF <= 0) clearInterval(aktivSpF);
}


function nextSpielErg() {
  if (doSlideErg) {
    actTopE=50;
    actHeightE=0;
    isperg++;
    if (isperg==anzSpE) isperg=0;
    nextSpE="#spielErg"+isperg;
    aktivSpE = setInterval("slideSpielErg('"+actSpE+"','"+nextSpE+"')",slideSpeedE);
    window.setTimeout("nextSpielErg()", timeoutE);
    actSpE=nextSpE;
  }
}
function slideSpielErg(a,b) {
  actTopE-=1;
  actHeightE+=1;

  $(b).css("top",parseInt(actTopE)+"px");
  $(b).css("height",parseInt(actHeightE)+"px");
  $(a).css("height",parseInt(actTopE)+"px");
  if (actTopE <= 0) clearInterval(aktivSpE);
}

function stopSlideErg() {
/*************
  clearInterval(aktivSpE);
  doSlideErg=false;
***************/
}
function startSlideErg() {
/*************
  doSlideErg=true;
  nextSpielErg();
***************/
}
//alert("xx.js included");