/** Misc **/
/* -------------------------------------------------------------------------------------- */
function initAjax(){
	try{
		if( window.XMLHttpRequest ){
			globReq = new XMLHttpRequest();
		}
		else if( window.ActiveXObject ){
			globReq = new ActiveXObject( "Microsoft.XMLHTTP" );
		}
		else{
			// no Ajax;
			alert( "Ajax wird nicht unterstuetzt" );
		}
	}
	catch( e ){
		alert( "Exception: "+e );
	}
}
/* -------------------------------------------------------------------------------------- */

/** Menu **/
/* -------------------------------------------------------------------------------------- */
function removeAllChildren( id ){
	numChildren = document.getElementById( id ).childNodes.length;
	for(i=0; i<numChildren; i++){
		firstChild = document.getElementById( id ).firstChild;
		document.getElementById( id ).removeChild(firstChild);
	}
}

function addMenuItem( name, no, pageNr ){
	switch( navigator.appName ){
	case "Microsoft Internet Explorer":
		if( no == pageNr ){
			document.getElementById('menu').innerHTML += "<a href=\"index.php?page="+no+"\" class=\"menuitemred\">-"+name+"-</a>";
		}
		else{
			document.getElementById('menu').innerHTML += "<a href=\"index.php?page="+no+"\" class=\"menuitem\">-"+name+"-</a>";
		}
	break;
	default:
		newLink = document.createElement("a");
		newLink.setAttribute("href", "index.php?page="+no );
		if( no == pageNr ){
			newLink.setAttribute("class", "submenured");
		}
		else{
			newLink.setAttribute("class", "submenu");
		}
		newLinkText = document.createTextNode( '-'+name+'-' );
		newLink.appendChild(newLinkText);
		document.getElementById('menu').appendChild(newLink);
	break;
	}
}

function clickOnMenu( supermenu_item, pageNr ){
	removeAllChildren( 'menu' );
	for( i=0; i<topic.length; i++){
		if( topic[i]["topic"] == supermenu_item ){
			addMenuItem( topic[i]["name"], topic[i]["no"], pageNr );
		}
	}
}

function showSubMenu( id ){
	var elem = document.getElementById( id );
	for( var i=0; i<11; i++){
		if( document.getElementById( 'submenu'+i ) ){
			document.getElementById( 'submenu'+i ).style.display='none';
		}
	}
	if( elem ){
		elem.style.display = 'block';
	}
}
/* -------------------------------------------------------------------------------------- */

/** News **/
/* -------------------------------------------------------------------------------------- */
var globReq; 
var globNewsTagNr;
var globId;

function myAjaxCallNews( newsId, newsTagNr ){
	if( !globReq ){
		initAjax();
	}
	url = 'ajax.news.php?id='+newsId;
	globReq.open( "GET", url, true );
	globId = 'news_'+newsTagNr;
	globNewsTagNr = newsTagNr;
	globReq.onreadystatechange = myCallBackNews;
	globReq.send( null );
}

function myCallBackNews(){
	if( 4 == globReq.readyState ){
		if( 200 != globReq.status ){
			alert( "Fehler " + globReq.status + ": " + globReq.statusText );
		}
		else{
			xmlElement = globReq.responseXML.documentElement;
			currentNode     = xmlElement.getElementsByTagName("newstext")[0];
			currentPic      = xmlElement.getElementsByTagName("picture")[0];
			currentPicText  = xmlElement.getElementsByTagName("picturetext")[0];
			currentPicPos   = xmlElement.getElementsByTagName("picposition")[0];
			targetNode = document.getElementById( globId );
			if( currentPic && currentPic.hasChildNodes() ){
				divimg = document.createElement("div");
				if( currentPicPos.hasChildNodes() && currentPicPos.firstChild.data==1 ){
					divimg.setAttribute( "class", "news-pic-right");
				}
				else if( currentPicPos.hasChildNodes() && currentPicPos.firstChild.data==0 ){
					divimg.setAttribute( "class", "news-pic-left");
				}
				else{
					divimg.setAttribute( "class", "news-pic-large");
				}
				br = document.createElement("br");
				if( currentPicPos.hasChildNodes() && currentPicPos.firstChild.data==2 ){ 
					br.setAttribute("clear", "all");
				}
				pictext = document.createTextNode( currentPicText.firstChild.data );
				img = document.createElement("img");
				img.setAttribute( "src", "imgcm/"+currentPic.firstChild.data );
				img.setAttribute( "border", "1" );
				divimg.appendChild( img );
				divimg.appendChild( br );
				divimg.appendChild( pictext );
				targetNode.appendChild( divimg );
			}
			
			currentChild = currentNode.firstChild
			if( currentChild!=null ){
				do{
					if( currentChild.data ){
						newText = document.createTextNode( currentChild.data );
						targetNode.appendChild( newText );
					}
					else if( currentChild.nodeName=="br" ){
						newBr = document.createElement("br");
						targetNode.appendChild( newBr );
					}
					if( currentChild.hasChildNodes() ){
						if( currentChild.nodeName=="b" ){
							newSubB    = document.createElement("b");
							newSubText = document.createTextNode( currentChild.firstChild.data );
							newSubB.appendChild( newSubText );
							targetNode.appendChild( newSubB );
						}
					}
				}while( currentChild != currentNode.lastChild && (currentChild = currentChild.nextSibling) );
			}
			linkNode = document.getElementById( "newsLink_"+globNewsTagNr );
			while( linkNode.hasChildNodes() ){
				dummyNode = linkNode.firstChild;
				linkNode.removeChild( dummyNode );
			}
			newsBeginNode = document.getElementById( "beginNews_"+globNewsTagNr );
			newsBeginNode.setAttribute( "class", "news-begintext-open" );
		}
	}
}
/* -------------------------------------------------------------------------------------- */

/** Team **/
/* -------------------------------------------------------------------------------------- */
var globMatchDay;

function changePlayerPic( num ){
	document.getElementById("playerphoto").setAttribute( "src", playerpic[num]['file'] );
	document.getElementById("playerphoto").setAttribute( "border", 1 );
	document.getElementById("pname").firstChild.replaceData( 0, document.getElementById("pname").firstChild.length, unescape(playerpic[num]['name']) );
}

function setMatchDay( mDay, team ){
	cnt = 0;
	myAjaxCallTable( mDay, team );
	for( i=0; i<games.length; i++){
		if( games[i]["matchday"] == mDay ){
			document.getElementById('matchday').firstChild.nodeValue              = mDay+'. Spieltag';
			document.getElementById('match_results').firstChild.nodeValue         = 'Spielergebnisse '+games[i]["matchday"]+'. Spieltag';
			document.getElementById('date-' + cnt).firstChild.nodeValue           = games[i]["date"];
			document.getElementById('home-' + cnt).firstChild.nodeValue           = games[i]["home"];
			document.getElementById('guest-' + cnt).firstChild.nodeValue          = games[i]["guest"];
			document.getElementById('goals-' + cnt).firstChild.nodeValue          = games[i]["goal_home"]+':'+games[i]["goal_guest"];
			document.getElementById('goals_halftime-' + cnt).firstChild.nodeValue = '('+games[i]["goal_home_halftime"]+':'+games[i]["goal_guest_halftime"]+')';
			cnt++;
		}
	}
}

function myAjaxCallTable( matchday, team ){
	if( !globReq ){
		initAjax();
	}
	globMatchDay = matchday;
	url = 'ajax.bundesliga.php?matchday='+matchday+'&team='+team;
	globReq.open( "GET", url, true );
	globReq.onreadystatechange = myCallBackTable;
	globReq.send( null );
}

function myCallBackTable(){
	if( 4 == globReq.readyState ){
		if( 200 != globReq.status ){
			alert( "Fehler " + globReq.status + ": " + globReq.statusText );
		}
		else{
			xmlElement = globReq.responseXML.documentElement;
			for(i=0; i<xmlElement.getElementsByTagName("team").length; i++){
				currentTeamSuperNode  = xmlElement.getElementsByTagName("team")[i];
				currentTeamNode       = currentTeamSuperNode.getElementsByTagName("name")[0];
				currentGoal1Node      = currentTeamSuperNode.getElementsByTagName("goal1")[0];
				currentGoal2Node      = currentTeamSuperNode.getElementsByTagName("goal2")[0];
				currentNumOfGamesNode = currentTeamSuperNode.getElementsByTagName("numofgames")[0];
				currentDiffNode       = currentTeamSuperNode.getElementsByTagName("diff")[0];
				currentWinNode        = currentTeamSuperNode.getElementsByTagName("win")[0];
				currentTieNode        = currentTeamSuperNode.getElementsByTagName("tie")[0];
				currentLooseNode      = currentTeamSuperNode.getElementsByTagName("loose")[0];
				currentPointsNode     = currentTeamSuperNode.getElementsByTagName("points")[0];
				//alert(i);
				document.getElementById('team-'+i).firstChild.nodeValue       =  currentTeamNode.firstChild.data;
				document.getElementById('goal1-'+i).firstChild.nodeValue      =  currentGoal1Node.firstChild.data;
				document.getElementById('goal2-'+i).firstChild.nodeValue      =  currentGoal2Node.firstChild.data;
				//document.getElementById('numofgames-'+i).firstChild.nodeValue =  currentNumOfGamesNode.firstChild.data;
				//document.getElementById('diff-'+i).firstChild.nodeValue       =  currentDiffNode.firstChild.data;
				document.getElementById('win-'+i).firstChild.nodeValue        =  currentWinNode.firstChild.data;
				document.getElementById('tie-'+i).firstChild.nodeValue        =  currentTieNode.firstChild.data;
				document.getElementById('loose-'+i).firstChild.nodeValue      =  currentLooseNode.firstChild.data;
				document.getElementById('points-'+i).firstChild.nodeValue     =  currentPointsNode.firstChild.data;
			}
		}
	}
}
/* -------------------------------------------------------------------------------------- */

/** Results Archive **/
/* -------------------------------------------------------------------------------------- */
var globResArchId;

function myAjaxCallResultsArchive( resultsArchiveId ){
	if( !globReq ){
		initAjax();
	}
	url = 'ajax.resultsarchive.php?id='+resultsArchiveId;
	globResArchId = resultsArchiveId;
	globReq.open( "GET", url, true );
	globReq.onreadystatechange = myCallBackResultsArchive;
	globReq.send( null );
}

function myCallBackResultsArchive(){
	if( 4 == globReq.readyState ){
		if( 200 != globReq.status ){
			alert( "Fehler " + globReq.status + ": " + globReq.statusText );
		}
		else{
			xmlElement = globReq.responseXML.documentElement;
			/*
			 *  table
			 *    \----> tr td img img td text text
			 *    \----> text
			 */
			tableNode = document.createElement("table");
			tableNode.setAttribute("width", "100%");
			tableNode.setAttribute("align", "center");
			tableNode.setAttribute("border", "0");

			pic1Node = xmlElement.getElementsByTagName( "pic1" );
			pic2Node = xmlElement.getElementsByTagName( "pic2" );
			if( pic1Node[0].hasChildNodes() ){
				trChild1Node = document.createElement("tr");
				trChild2Node = document.createElement("tr");
				if( pic2Node[0].hasChildNodes() ){

					tdChild11Node = document.createElement("td");
					tdChild11Node.setAttribute("width","330");
					tdChild11Node.setAttribute("align","center");
					imgPic1Node = document.createElement("img");
					imgPic1Node.setAttribute("src", "imgcm/"+pic1Node[0].firstChild.data);
					imgPic1Node.setAttribute("border", "1");
					tdChild11Node.appendChild( imgPic1Node );

					tdSeparator1Node = document.createElement("td");
					tdSeparator1Node.setAttribute("width", 30);
					tdSeparator2Node = document.createElement("td");
					tdSeparator2Node.setAttribute("width", 30);

					tdChild12Node = document.createElement("td");
					tdChild12Node.setAttribute("width","330");
					tdChild12Node.setAttribute("align","center");
					imgPic2Node = document.createElement("img");
					imgPic2Node.setAttribute("src", "imgcm/"+pic2Node[0].firstChild.data);
					imgPic2Node.setAttribute("border", "1");
					tdChild12Node.appendChild( imgPic2Node );

					trChild1Node.appendChild( tdChild11Node );
					trChild1Node.appendChild( tdSeparator1Node );
					trChild1Node.appendChild( tdChild12Node );


					tdChild21Node = document.createElement("td");
					tdChild21Node.setAttribute("widht", "330");
					tdChild21Node.setAttribute("class", "results-pictex");
					text1Node = document.createTextNode( xmlElement.getElementsByTagName("pictext1")[0].firstChild.data );
					tdChild21Node.appendChild( text1Node );

					tdChild22Node = document.createElement("td");
					tdChild22Node.setAttribute("widht", "330");
					tdChild22Node.setAttribute("class", "results-pictex");
					text2Node = document.createTextNode( xmlElement.getElementsByTagName("pictext2")[0].firstChild.data );
					tdChild22Node.appendChild( text2Node );

					trChild2Node.appendChild( tdChild21Node );
					trChild2Node.appendChild( tdSeparator2Node );
					trChild2Node.appendChild( tdChild22Node );



				}//! if( pic2Node[0].hasChildNodes() )
				else{
					tdChild11Node = document.createElement("td");
					tdChild11Node.setAttribute("width","330");
					tdChild11Node.setAttribute("align","center");
					imgPic1Node = document.createElement("img");
					imgPic1Node.setAttribute("src", "imgcm/"+pic1Node[0].firstChild.data);
					imgPic1Node.setAttribute("border", "1");
					tdChild11Node.appendChild( imgPic1Node );

					trChild1Node.appendChild( tdChild11Node );

					tdChild21Node = document.createElement("td");
					tdChild21Node.setAttribute("widht", "330");
					tdChild21Node.setAttribute("class", "results-pictex");
					text1Node = document.createTextNode( xmlElement.getElementsByTagName("pictext1")[0].firstChild.data );
					tdChild21Node.appendChild( text1Node );

					trChild2Node.appendChild( tdChild21Node );
				}//! if( pic2Node[0].hasChildNodes() ) else

				tableNode.appendChild( trChild1Node );
				tableNode.appendChild( trChild2Node );
			}//! if( pic1Node[0].hasChildNodes() )

			trChildTextNode = document.createElement("tr");
			tdChildTextNode = document.createElement("td");
			tdChildTextNode.setAttribute("colspan",3);
			tdChildTextNode.setAttribute("class","results-text");
			textNode = document.createTextNode( xmlElement.getElementsByTagName("text")[0].firstChild.data );
			tdChildTextNode.appendChild(document.createElement("br"));
			tdChildTextNode.appendChild(textNode);
			tdChildTextNode.appendChild(document.createElement("br"));
			tdChildTextNode.appendChild(document.createElement("br"));
			tdChildTextNode.appendChild(document.createElement("br"));
			tdChildTextNode.appendChild(document.createElement("br"));
			trChildTextNode.appendChild(tdChildTextNode);
			tableNode.appendChild( trChildTextNode );

			document.getElementById( globResArchId ).removeChild(document.getElementById( globResArchId ).firstChild);
			document.getElementById( globResArchId ).appendChild( tableNode );
			document.getElementById( globResArchId+"-more" ).removeChild( document.getElementById( globResArchId+"-more" ).firstChild );
		}
	}
}
/* -------------------------------------------------------------------------------------- */

/** Partner **/
/* -------------------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------------------- */

