/**
 * @author George
 */

var isIE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
var isIE = document.all;
var request;

function createXmlHttpRequestObject() 
{
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // this should work for all browsers except IE6 and older
  try
  {
    // try to create XMLHttpRequest object
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    // assume IE6 or older
    var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                    "MSXML2.XMLHTTP.5.0",
                                    "MSXML2.XMLHTTP.4.0",
                                    "MSXML2.XMLHTTP.3.0",
                                    "MSXML2.XMLHTTP",
                                    "Microsoft.XMLHTTP");
    // try every prog id until one works
    for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) 
    {
      try 
      { 
        // try to create XMLHttpRequest object
        xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      } 
      catch (e) {}
    }
  }
  // return the created object or display an error message
  if (!xmlHttp)
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}

function setMenuElements(){
	var leftPosition = document.getElementById("banner").offsetLeft;
	//alert(leftPosition);
	if (!isIE6) {
		document.getElementById("menuLeftEdge").style.marginLeft = leftPosition + "px";
	}
	else {
		document.getElementById("menuLeftEdge").style.marginLeft = (leftPosition - 172) + "px";
	}
	
	if (document.getElementById("login") != null) {
		document.getElementById("login").style.top = "5px";
		document.getElementById("login").style.right = (((document.body.clientWidth - document.getElementById("banner").offsetWidth) / 2) + 5) + "px";
	}
	
	if (document.getElementById('birthDay') != null) {
		dp_cal = new Epoch2('epoch_popup', 'popup', document.getElementById('birthDay'));
	}
	
	if (document.getElementById('chatContent') != null) {
		initChat();
	}
	
	inputs = document.getElementsByTagName("input");
	
	for (var i = 0; i < inputs.length; i++) {
		if (inputs[i].id.indexOf("matchDate")!=-1) {
			dp_cal = new Epoch2('epoch_popup', 'popup', inputs[i]);
		}
	}	

}

function showAddContribution() {
	if (document.getElementById("newContribution").value == "0") {
		document.getElementById("contribution").style.display = "block";
		document.getElementById("newContribution").value = "1";
	} else {
		document.getElementById("contribution").style.display = "none";
		document.getElementById("newContribution").value = "0";
	}
}

function positionContribution(element) {
	if (!isIE) {
		element.align = "right";
	}
}

function createMatchInfo(element) {
	request = createXmlHttpRequestObject();
	
	deleteChilds(document.getElementById("score1"));
	deleteChilds(document.getElementById("score2"));
	deleteChilds(document.getElementById("matchDate"));
	
	if (element.title == "Next game") {
		element.src = "images/nextGame.png";
		document.getElementById("lastGameImage").src = "images/lastGameSelected.png";
	} else {
		element.src = "images/lastGame.png";
		document.getElementById("nextGameImage").src = "images/nextGameSelected.png";
	}
	
	request.onreadystatechange = showMatchInfo;
	request.open("GET", "match/matchInfo.php?type="+element.title, true);
	request.send(null);
	
}

function showMatchInfo() {
	if (request.readyState == 4) {
		if (request.status == 200) {
			response = request.responseXML.documentElement;
			document.getElementById("team1").src= "images/teams/"+response.getElementsByTagName("firstteam")[0].firstChild.data.toString();
			document.getElementById("team2").src= "images/teams/"+response.getElementsByTagName("secondteam")[0].firstChild.data.toString();
						
			var scoretype = response.getElementsByTagName("scoretype")[0].firstChild;
			var responseScore1 = response.getElementsByTagName("firstscore")[0].firstChild;
			var responseScore2 = response.getElementsByTagName("secondscore")[0].firstChild;
			var matchDate = document.createTextNode(response.getElementsByTagName("date")[0].firstChild.data.toString());
			var summaryLink = response.getElementsByTagName("summary")[0].firstChild.data.toString();
			var previewLink = response.getElementsByTagName("preview")[0].firstChild.data.toString();
			var sheetLink = response.getElementsByTagName("sheet")[0].firstChild.data.toString();
			var minuteLink = response.getElementsByTagName("minute")[0].firstChild.data.toString();
			var videoLink = response.getElementsByTagName("video")[0].firstChild.data.toString();
			
			if (responseScore1!=null && responseScore2!=null && scoretype!=null) {
				var score1 = document.createTextNode(responseScore1.data.toString());
				var score2 = document.createTextNode(responseScore2.data.toString()+"     "+scoretype.data.toString());
				
				
				document.getElementById("score1").appendChild(score1);
				document.getElementById("score2").appendChild(score2);
			}	
			
			document.getElementById("matchDate").appendChild(matchDate);
			document.getElementById("summaryLink").href = summaryLink;
			document.getElementById("previewLink").href = previewLink;
			document.getElementById("sheetLink").href = sheetLink;
			document.getElementById("minuteLink").href = minuteLink;
			document.getElementById("videoLink").href = videoLink;
				
		}
	}
}

function deleteChilds(element) {
	if ( element.hasChildNodes() )
	{
	    while ( element.childNodes.length >= 1 )
	    {
	    	element.removeChild( element.firstChild );       
	    } 
	}

}
