var xmlHttp1;

function reloadUserStat() {
  var url = "server/benutzerstat_.inc.php";
  xmlHttp1 = GetXmlHttpObject(stateChangedUserStat);
  xmlHttp1.open("GET", url , true);
  xmlHttp1.send(null);
}

function stateChangedUserStat() {
  if (xmlHttp1.readyState == 4 || xmlHttp1.readyState == "complete") {
	document.getElementById("userstat").innerHTML = xmlHttp1.responseText;
  }
}


var xmlHttp2;

function reloadWhoIsOnline() {
  var url = "server/whoisonlinebox_.inc.php";
  xmlHttp2 = GetXmlHttpObject(stateChangedWhoIsOnline);
  xmlHttp2.open("GET", url , true);
  xmlHttp2.send(null);
}

function stateChangedWhoIsOnline() {
  if (xmlHttp2.readyState == 4 || xmlHttp2.readyState == "complete") {
	document.getElementById("whoisonline").innerHTML = xmlHttp2.responseText;
  }
}


var xmlHttp3;

function reloadShoutbox() {
  var url = "server/shoutbox_.inc.php";
  xmlHttp3 = GetXmlHttpObject(stateChangedShoutbox);
  xmlHttp3.open("GET", url , true);
  xmlHttp3.send(null);
}

function stateChangedShoutbox() {
  if (xmlHttp3.readyState == 4 || xmlHttp3.readyState == "complete") {
	document.getElementById("shoutbox").innerHTML = xmlHttp3.responseText;
  }
}


var xmlHttp4;

function loadGame() {
  var url = "server/spielen_.inc.php";
  xmlHttp4 = GetXmlHttpObject(stateChangedGame);
  xmlHttp4.open("GET", url , true);
  xmlHttp4.send(null);
}

function stateChangedGame() {
  if (xmlHttp4.readyState == 4 || xmlHttp4.readyState == "complete") {
	document.getElementById("game").innerHTML = xmlHttp4.responseText;

	setInterval('reloadTime()', 1000);
	//setInterval('reloadHits()', 1000);
	//setInterval('reloadClicks()', 1000);
  }
}


var xmlHttp5;
var imgtoturn;

function turnImage(img) {
  var url = "server/turn_.inc.php?img="+img;
  imgtoturn = img;
  xmlHttp5 = GetXmlHttpObject(stateChangedTurn);
  xmlHttp5.open("GET", url , true);
  xmlHttp5.send(null);
}

function stateChangedTurn() {
  var resp;

  if (xmlHttp5.readyState == 4 || xmlHttp5.readyState == "complete") {
	resp = xmlHttp5.responseText;
	if (resp == "fertig") {
	  document.location = "index.php?page=wettbewerb&wettbewerb=spielen";
	}
	document.getElementById("img"+imgtoturn).innerHTML = resp;
	reloadHits();
	reloadClicks();
  }
}

function startTurnBack(img1, img2) {
  setTimeout("turnBack('"+img1+"', '"+img2+"')", 800);
}

function turnBack(img1, img2) {
  document.getElementById("img"+img1).innerHTML = "<img src=\"images/wettbewerb/rahmen.jpg\" border=\"0\" onclick=\"turnImage('" + img1 + "')\" />";
  document.getElementById("img"+img2).innerHTML = "<img src=\"images/wettbewerb/rahmen.jpg\" border=\"0\" onclick=\"turnImage('" + img2 + "')\" />";
}



var xmlHttp6;

function reloadTime() {
  var url = "server/timeleft_.inc.php";
  xmlHttp6 = GetXmlHttpObject(stateTimeLeft);
  xmlHttp6.open("GET", url , true);
  xmlHttp6.send(null);
}

function stateTimeLeft() {
  if (xmlHttp6.readyState == 4 || xmlHttp6.readyState == "complete") {
	document.getElementById("timeleft").innerHTML = xmlHttp6.responseText;
  }
}



var xmlHttp7;

function reloadHits() {
  var url = "server/numhits_.inc.php";
  xmlHttp7 = GetXmlHttpObject(stateHits);
  xmlHttp7.open("GET", url , true);
  xmlHttp7.send(null);
}

function stateHits() {
  if (xmlHttp7.readyState == 4 || xmlHttp7.readyState == "complete") {
	document.getElementById("numhits").innerHTML = xmlHttp7.responseText;
  }
}



var xmlHttp8;

function reloadClicks() {
  var url = "server/numclicks_.inc.php";
  xmlHttp8 = GetXmlHttpObject(stateClicks);
  xmlHttp8.open("GET", url , true);
  xmlHttp8.send(null);
}

function stateClicks() {
  if (xmlHttp8.readyState == 4 || xmlHttp8.readyState == "complete") {
	document.getElementById("numclicks").innerHTML = xmlHttp8.responseText;
  }
}

/* ********** */

function GetXmlHttpObject(handler) {
  var objXmlHttp = null;

  if (navigator.userAgent.indexOf("Opera") >= 0) {
	//alert("This example doesn't work in Opera");
	return;
  }

  if (navigator.userAgent.indexOf("MSIE") >= 0) {
	var strName = "Msxml2.XMLHTTP";
	if (navigator.appVersion.indexOf("MSIE 5.5") >= 0) {
	  strName = "Microsoft.XMLHTTP";
	}
	try {
	  objXmlHttp = new ActiveXObject(strName);
	  objXmlHttp.onreadystatechange = handler;
	  return objXmlHttp;
	}
	catch(e) {
	  //alert("Error. Scripting for ActiveX might be disabled");
	  return;
	}
  }

  if (navigator.userAgent.indexOf("Mozilla") >= 0) {
	objXmlHttp = new XMLHttpRequest();
	objXmlHttp.onload = handler;
	objXmlHttp.onerror = handler;
	return objXmlHttp;
  }
}


/* end of ajax */


/**
 * Sets/unsets the pointer in browse mode
 *
 * @param   object   the table row
 * @param   object   the color to use for this row
 *
 * @return  boolean  whether pointer is set or not
 */
function setPointer(theRow, thePointerColor, theWeight)
{
    if (thePointerColor == '' || typeof(theRow.style) == 'undefined') {
        return false;
    }

    if (typeof(document.getElementsByTagName) != 'undefined') {
        var theCells = theRow.getElementsByTagName('td');
    }
    else if (typeof(theRow.cells) != 'undefined') {
        var theCells = theRow.cells;
    }
    else {
        return false;
    }

    var rowCellsCnt  = theCells.length;
    for (var c = 0; c < rowCellsCnt; c++) {
	    theCells[c].style.color = thePointerColor;
		theCells[c].style.fontWeight = theWeight;
    }

    return true;
}


/**
 * Sets/unsets the pointer in browse mode
 *
 * @param   object   the table row
 * @param   object   the color to use for this row
 *
 * @return  boolean  whether pointer is set or not
 */
function setPointerBG(theRow, thePointerColor)
{
    if (thePointerColor == '' || typeof(theRow.style) == 'undefined') {
        return false;
    }

    if (typeof(document.getElementsByTagName) != 'undefined') {
        var theCells = theRow.getElementsByTagName('td');
    }
    else if (typeof(theRow.cells) != 'undefined') {
        var theCells = theRow.cells;
    }
    else {
        return false;
    }

    var rowCellsCnt  = theCells.length;
    for (var c = 0; c < rowCellsCnt; c++) {
        theCells[c].style.backgroundColor = thePointerColor;
        //theCells[c].style.color = thePointerColor;
    }

    return true;
}


function toggle(foo) {
  if (document.getElementById(foo).style.display == "none") {
	show(foo, 1);
        //setCookie(foo, "o");
  }
  else {
        if (document.getElementById(foo).style.display == "block") {
          hide(foo, 1);
        }
        else {
          show(foo, 1);
        }
  }
}


function togglePlan(foo, num) {
  if (document.getElementById("team"+foo).style.display == "none") {
	for (i = 0; i < num; i++) {
	  hide("team"+i, 1);
	}
	show("team"+foo, 1);
	//setCookie(foo, "o");
  }
  else {
	if (document.getElementById("team"+foo).style.display == "block") {
	  hide("team"+foo, 1);
	}
	else {
	  for (i = 0; i < num; i++) {
		hide("team"+i, 1);
	  }
	  show("team"+foo, 1);
	  //setCookie(foo, "o");
	}
  }
}


function show(foo, f) {
  document.getElementById(foo).style.display = "block";
  //if (f) { setCookie(foo, "o"); }
}


function hide(foo, f) {
  document.getElementById(foo).style.display = "none";
  //if (f) { setCookie(foo, "c"); }
}


function openMenu(foo) {
  if (foo == "") {
  }
  else {
        toggle(foo);
  }
}


function confirmSponsorDelete(sponsor, url) {
  if (confirm("Sponsor '"+sponsor+"' wirklich löschen?")) {
	document.location = url;
  }
}


function showBig(img, week) {
  document.getElementById("bild"+week).src = "images/wettbewerb/bild"+img+".jpg";
}


function setDateInto(field, delay) {
  datetime = getCurrentDatePlus(delay);

  curval = document.getElementById(field).value;

  if (curval == datetime) {
    emptyField(field);
  }
  else {
    document.getElementById(field).value = datetime;
  }
}


function getCurrentDatePlus(delay) {
  var datetime;
  var someDate=new Date();
  someDate.setDate(someDate.getDate()+delay)

  datetime = addLeadingZero(someDate.getDate()) + ".";
  datetime += addLeadingZero(someDate.getMonth() + 1) + ".";
  datetime += someDate.getFullYear();

  return datetime;
}


function emptyField(field) {
  document.getElementById(field).value = "";
}


function addLeadingZero(num) {
  var number = "" + num;

  if (number.length == 1) {
    number = "0" + number;
  }

  return number;
}

function calcSize(sms, anz, max) {
  if (sms.value.length > max) {
	// if too long...trim it!
	sms.value = sms.value.substring(0, max);
  }
  else {
	// otherwise, update 'characters left' counter
	anz.value = max - sms.value.length;
  }
}

