// - - - - - - AJAX Parameter - - - - - - - - - - 
// var betDetailServiceURL='/cgi-bin/betdetails.pl';  (defined in HTML/JSP Page)
// var BETDETAIL_TIMEOUT=5000; // BETDETAIL_TIMEOUT between Requests 5000 ==  5 Sec (defined in HTML/JSP Page)
// var  BETDETAIL_ERRORTIMEOUT=60000; (defined in HTML/JSP Page)

var betDetailsReq=null;
var autorestart=0; // If set to 1 cycling restarts with j_sequence=0 after last sequence
var j_sequence=0;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

// - - - - - -  Input Parameter - - - - - - - - - - 
var j_betid=0;			// the event we want details from score24
var j_hometeam='';	// the name of the home team
var j_guestteam='';	// the name of the guest team
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

// - - - - - -  Globale Variables - - - - - - - - -
var result="&nbsp;";		// the result of the game, shown in the table header
var tableLines = new Array();		// An Array of "<ROW>-Objects"  used in processRequest() and drawBetDetailTable()

var Actions = new Array();		// Associative Array of actions odered by "type" attribute, 
				// only needed output should differ <*action>'s  node-value
				// i.e. show red card image instead of string "red"

Actions["410"]='<img src="../images/yellowCard.gif"';
Actions["415"]='<img src="../images/yellowRedCard.gif"';
Actions["420"]='<img src="../images/redCard.gif"';
//Actions["410"]='<div class="yellow"></div>';
//Actions["415"]='<div class="yellow1"></div><div class="red1"></div>'; 
//Actions["420"]='<div class=\"red\"></div>';
Actions["430"]='<img src="../images/substitution.gif"';

var rowClasses = new Array("dark","light");	// differnet line colors as defined in score24.css


function initBetDetailsAjax() {
	if (BETDETAIL_TIMEOUT!=-1) {
		var url = betDetailServiceURL+"&j_betid="+j_betid+"&j_sequence="+j_sequence+"&ms="+(new Date().getTime());
		// alert(url);
		initBetDetailsAjaxRequest(url);
		// set callback function    
		betDetailsReq.onreadystatechange = processBetDetailsRequest;    
		betDetailsReq.send(null);
	}
}

function startBetDetails(betid,hometeam,guestteam) {
	//alert('betid: '+betid);
	j_betid=betid;
	if (guestteam == '' || guestteam == null) {		// TopWette has just one string seperated by ':'
		var teams=hometeam.split(':');
		if (teams.length == 2) {
			j_hometeam = teams[0];
			j_guestteam = teams[1];
		} else {
			j_hometeam = hometeam;
			j_guestteam = '';
		}
	} else {
		j_hometeam = hometeam;
		j_guestteam = guestteam;
	}
	betDetailsReq=null
	j_sequence=0;
	result="&nbsp;";
	drawBetDetailTable();
	initBetDetailsAjax();
}


function initBetDetailsAjaxRequest(url) {    
	if (window.XMLHttpRequest) {        
		betDetailsReq = new XMLHttpRequest();    
	} else if (window.ActiveXObject) {        
		isIE = true;        
		betDetailsReq = new ActiveXObject("Microsoft.XMLHTTP");    
	}    
	betDetailsReq.open("GET", url, true);
}


// function processBetDetailsRequest() 
//
// processes a XML Result of this structure:
// 
// <!ELEMENT event (result?, row+)>
// <!ATTLIST event
//	id CDATA #REQUIRED
// >
// <!ELEMENT guestaction (#PCDATA)>
// <!ATTLIST guestaction
//	type CDATA #REQUIRED
// >
// <!ELEMENT guestname (#PCDATA)>
// <!ELEMENT homeaction (#PCDATA)>
// <!ATTLIST homeaction
//	type CDATA #REQUIRED
// >
// <!ELEMENT homename (#PCDATA)>
// <!ELEMENT result (#PCDATA)>
// <!ELEMENT row (homename?, homeaction?, guestname?, guestaction?)>
// <!ATTLIST row
//	time CDATA #REQUIRED
// >

function processBetDetailsRequest() {
	if (betDetailsReq.readyState == 4) {     
		if (betDetailsReq.status == 200) {
			var rowNum=0;			// set rowNum to 0 - will be raised for every <row> Tag found, used to change colors of lines
			tableLines = new Array();
			j_sequence++; 
			var eventArray = betDetailsReq.responseXML.getElementsByTagName('event'); 	// Get the <event> Tag
			for (var i=0; i<eventArray.length; i++) {
				var nodes = eventArray[i].firstChild;
				while (nodes != null) {					// Loop over all child nodes of <event>
					if (nodes.nodeType != 3) {				// Skip Text Nodes (CR, Tabs, Spaces....) between Tags 
						switch(nodes.nodeName) {
							case "result":	result = nodes.firstChild.nodeValue;  // set global variable result, used by drawBetDetailTable()
									break;
							case "row":	tableLines[rowNum] = new Object(); 	// create new "<RAW>-Object"
													// and fill with time and default values
									var timeValue = nodes.getAttribute('time');
									if(timeValue != null && timeValue != "")
										timeValue = timeValue +"\'";
									tableLines[rowNum]["time"]= timeValue;
									tableLines[rowNum]["homename"]="&nbsp;";
									tableLines[rowNum]["homeaction"]="&nbsp;";								
									tableLines[rowNum]["guestname"]="&nbsp;";
									tableLines[rowNum]["guestaction"]="&nbsp;";								
									rowNodes=nodes.firstChild;
									while (rowNodes != null) {	// Loop over all childs of <row>
										if (rowNodes.nodeType != 3) {			// Skip Text Nodes (CR, Tabs, Spaces....) between Tags 
											switch(rowNodes.nodeName) {
												case "homename" : 	if(tableLines[rowNum]["homename"]!=null && tableLines[rowNum]["homename"]!="&nbsp;"){
																rowNum ++;
																tableLines[rowNum] = new Object();
																tableLines[rowNum]["time"]= timeValue;
																tableLines[rowNum]["homename"]="&nbsp;";
																tableLines[rowNum]["homeaction"]="&nbsp;";								
																tableLines[rowNum]["guestname"]="&nbsp;";
																tableLines[rowNum]["guestaction"]="&nbsp;";
																
															}
															tableLines[rowNum]["homename"]=rowNodes.firstChild.nodeValue;
															break;
												case "homeaction":	var actionType=rowNodes.getAttribute('type');
															if((actionType!=null) && (Actions[actionType]!="") && (Actions[actionType]!=null)) {
																tableLines[rowNum]["homeaction"]=Actions[actionType];
															} else {
																tableLines[rowNum]["homeaction"]='';
																if(rowNodes.firstChild) {
																	tableLines[rowNum]["homeaction"]=rowNodes.firstChild.nodeValue;
																	tableLines[rowNum]["homename"] = '<table><tr><td>'+tableLines[rowNum]["homename"]+'</td><td style="padding-left: 5px;"><img src="../images/goal.gif"></td></tr></table>';
																}
															}
															break;
												case "guestname":	if(tableLines[rowNum]["guestname"]!=null && tableLines[rowNum]["guestname"]!="&nbsp;"){
																rowNum ++;
																tableLines[rowNum] = new Object();
																tableLines[rowNum]["time"]= timeValue;
																tableLines[rowNum]["homename"]="&nbsp;";
																tableLines[rowNum]["homeaction"]="&nbsp;";								
																tableLines[rowNum]["guestname"]="&nbsp;";
																tableLines[rowNum]["guestaction"]="&nbsp;";
																
															}
															tableLines[rowNum]["guestname"]=rowNodes.firstChild.nodeValue;
															break;
												case "guestaction":	var actionType=rowNodes.getAttribute('type');
															if((actionType!=null) && (Actions[actionType]!="") && (Actions[actionType]!=null)) {
																tableLines[rowNum]["guestaction"]=Actions[actionType];
															} else {
																tableLines[rowNum]["guestaction"]='';
																if(rowNodes.firstChild) {										
																	tableLines[rowNum]["guestaction"]=rowNodes.firstChild.nodeValue;
																	tableLines[rowNum]["guestname"] = '<table><tr><td>'+tableLines[rowNum]["guestname"]+'</td><td style="padding-left: 5px;"><img src="../images/goal.gif"></td></tr></table>';

																}
															}
															break;
												default: 			
															break;
											}
										}
									rowNodes = rowNodes.nextSibling;	// Get next child of <row>
									} // end of while(rowNodes != null)
									rowNum++; 	// raise nuber of found <row> tags
									break;
							default:
								break;
						} // end of switch(nodes.nodeName)
					} // end of if(nodes.nodeType!=3) 
					nodes = nodes.nextSibling;
				} // end of while(nodes != null) 
			} // end of for(var i=0;i<eventArray.length;i++)

		drawBetDetailTable();				// We got all <result> and <row>s, so lets draw the table...
		setTimeout('initBetDetailsAjax()', BETDETAIL_TIMEOUT);	// do the next poll in 5 seconds        

		} else {		// end of if (betDetailsReq.status == 200) 

			if ((betDetailsReq.status == 404) && (autorestart==1)) {
				j_sequence=0;
				initBetDetailsAjax(); // restart immediately  
			} else {
				setTimeout('initBetDetailsAjax()', BETDETAIL_ERRORTIMEOUT);	// do the next poll in 60 seconds        
			}

		}
	} // end of if (betDetailsReq.readyState == 4)
} 

function drawBetDetailTable() {
	var tableData =	'<table width="100%" border="0" cellpadding="0" cellspacing="1" style="border: solid 1px #ffffff;">'+
			'<tr height="36"><td class="normalTextWhiteBold" align="center" bgcolor="#377601">'+result+'</td>'+
			'<td class="normalTextWhiteBold" align="center" bgcolor="#377601">'+j_hometeam+'</td>'+
			'<td class="normalTextWhiteBold" align="center" bgcolor="#377601">'+j_guestteam+'</td>';
	for (var i=0; i<tableLines.length; i++) {
		tableData += '<tr height="36" class="bgtable_'+(i%2)+'">';
		tableData += '<td width="53" class="normalTextBlackBold" align="center">'+tableLines[i]["time"]+'</td>';
		tableData += '<td width="192"><table><tr><td width="62" align="center" valign="middle" class="normalTextBlackBold">'+tableLines[i]["homeaction"]+'</td><td valign="middle" class="normalTextBlackBold">'+tableLines[i]["homename"].replace(/\n/g,"<br>")+'</td></tr></table></td>';
		tableData += '<td width="192"><table><tr><td width="62" align="center" valign="middle" class="normalTextBlackBold">'+tableLines[i]["guestaction"]+'</td><td valign="middle" class="normalTextBlackBold">'+tableLines[i]["guestname"].replace(/\n/g,"<br>")+'</td></tr></table></td>';
		tableData += '</tr>';
	}
	tableData += '</table>';
	var xmlData_div=window.document.getElementById('xmlData');
	xmlData_div.innerHTML=tableData;
}

