// XML Helper functions
var ie = false
function NodeValue(_node)
{
	if( _node && _node.firstChild )
		return _node.firstChild.nodeValue
	return ""
}
function TagNodeValue(_doc,_tag)
{
	return NodeValue(_doc.getElementsByTagName(_tag)[0])
}
function TagNodes(_doc,_tag)
{
	return _doc.getElementsByTagName(_tag)
}

function NbChildren(_nodes)
{
	var nb=0
	var i
	for(i=0; i<_nodes.childNodes.length; i++)
		if(_nodes.childNodes[i].nodeType==1)
			nb++
	return nb
}
function Child(_node,_i)
{
	var i
	var nb=0
	for(i=0; i<_node.childNodes.length; i++)
	{
		if(_node.childNodes[i].nodeType!=1)	continue
		if(nb==_i)	return _node.childNodes[i]
		nb++
	}
	return null
}

// All Browsers xml loader
function CreateXmlDoc(_callback)
{
	if (document.implementation && document.implementation.createDocument)
	{
		xmlDoc = document.implementation.createDocument("", "", null);
		xmlDoc.readyState = 4;
		xmlDoc.onload = _callback;
	}
	else if (window.ActiveXObject)
	{
		ie = true
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.onreadystatechange = _callback;
	}
	else
	{
		alert('Your browser can\'t handle this script');
		return;
	}
	xmlDoc.async = false
	return xmlDoc
}

// Class Dictionnary
function Dictionnary()
{
	this.txts = new Array()
	this.Add = function( _key, _val )
	{
		this.txts[_key] = _val
	}

	this.Get = function( _key )
	{
		ret = this.txts[_key]
		if(!ret)
			ret = _key
		return ret
	}
	this.Load = function(_xmlDoc)
	{
		// Load xml doc
		this.m_xmlDoc		= _xmlDoc
		elems = _xmlDoc.documentElement
		for( i=0; i<NbChildren(elems); i++ )
			this.Add( Child(elems,i).getAttribute("i"), NodeValue(Child(elems,i)))
	}
}

// StatManager :: Get Statistics
function	StatLoader( set_number, cat_number)
{
	var tab = new SuperArray()
	tab.m_colTitles = new Array( " ", this.m_teamNames[0], this.m_teamNames[1])
	tab.m_lines = new Array()
	
	var sets_nodes = null
//	nodes = this.m_xmlDoc.documentElement
	sets_nodes = TagNodes(doc,"stats")[0]
	for( i=0; i<NbChildren(sets_nodes); i++ )	
	{
		if( set_number == Child(sets_nodes,i).getAttribute("id") )
			set_node = Child(sets_nodes,i);
	}
	if(!set_node)	return null

	// Parse category
	i = cat_number
	if( i>=NbChildren(set_node) ) return null
	{
		var category_node = Child(set_node,i)
		tab.m_title = this.m_categories[i]
		// Parse each stat
		for( s=0; s<NbChildren(category_node); s++ )
		{
			var stat_node = Child(category_node,s)
			var stat0 = NodeValue(Child(stat_node,0))
			var stat1 = NodeValue(Child(stat_node,1))
			tab.m_lines[s] = new Array(	this.m_dic.Get(stat_node.getAttribute("i")),stat0,stat1 )
		}
	}
	return tab;
}

// StatManager :: Get ScoreBoard
function ScoreBoard()
{
	var brd = new SuperArray()
	brd.m_separateLines		= 0
	brd.m_separateCols		= 0
	brd.m_firstColAsTitles	= 1
	brd.m_colsAsTitles		= 1
	brd.m_title = this.m_event + " - " + this.m_round + " "
				 + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
				 + this.m_date + "&nbsp;&nbsp;"

	brd.m_colTitles = new Array(" ")
	brd.m_lines = new Array( new Array( this.m_teamNames[0].bold() ),
							 new Array( this.m_teamNames[1] ) )
	if( this.m_winner==0 )	brd.m_lines[0][0] = brd.m_lines[0][0].bold();
	if( this.m_winner==1 )	brd.m_lines[1][0] = brd.m_lines[1][0].bold();
	for(i=1; i<=this.m_sets.length; i++)
	{
		brd.m_colTitles[i] = " "+i+" "
		var side0_games = this.m_sets[i-1][0]
		var side1_games = this.m_sets[i-1][1]
		brd.m_lines[0][i] = side0_games
		brd.m_lines[1][i] = side1_games
		if( side0_games>side1_games )	brd.m_lines[0][i] = brd.m_lines[0][i].bold();
		if( side0_games<side1_games )	brd.m_lines[1][i] = brd.m_lines[1][i].bold();
	}
	brd.m_colSizes = new Array(160,50,50,50)
	return brd
}

// Class StatManager
function StatManager(_xml_doc)
{
	this.m_title		= "No title"
	this.GetStats		= StatLoader
	this.GetScoreBoard	= ScoreBoard
	this.m_dic			= new Dictionnary()
	this.m_round 		= "round"
	
	// Load xml doc
	this.m_xmlDoc		= _xml_doc

	// Init
	doc = this.m_xmlDoc
	this.m_event	= TagNodeValue(doc, "event")
	this.m_round	= TagNodeValue(doc,"round")
	var date_node = TagNodes(doc,"datetime")[0]
	var date_time= new Date( parseInt(date_node.getAttribute("y")),
							parseInt(date_node.getAttribute("m"))-1,
							parseInt(date_node.getAttribute("d")),
							parseInt(date_node.getAttribute("h")),
							parseInt(date_node.getAttribute("mn")),
							parseInt(date_node.getAttribute("s")) )
	this.m_date = ""
	if( date_time.getFullYear() > 1950 )
		this.m_date = date_time.toLocaleString()
	this.m_doubles	= TagNodeValue(doc,"doubles")
	team1_node		= TagNodes(doc,"team1")[0]
	team2_node		= TagNodes(doc,"team2")[0]
	team1			= new Array( NodeValue(Child(team1_node,0)), NodeValue(Child(team1_node,1)), NodeValue(Child(team1_node,2)) )
	team2			= new Array( NodeValue(Child(team2_node,0)), NodeValue(Child(team2_node,1)), NodeValue(Child(team2_node,2)) )
	this.m_teams	= new Array( team1, team2 )

	// Friendly Team names
	team1_name = team1[1]
	team2_name = team2[1]
	if( this.m_doubles == "1" )
	{
		team1_name += " / " + team1[2]
		team2_name += " / " + team2[2]
	}
	this.m_teamNames = new Array( team1_name, team2_name )
	this.m_leadingSide = TagNodeValue( doc,"leadingside" )
	this.m_matchComplete = new Boolean(eval( TagNodeValue(doc,"matchcomplete") ))
	
	this.m_winner = 2
	this.m_winnerName = ""
	this.m_looserName = ""
	if( this.m_matchComplete == true )
	{
		this.m_winner = eval(this.m_leadingSide)
		this.m_winnerName = this.m_teamNames[this.m_winner]
		this.m_looserName = this.m_teamNames[(this.m_winner==0)?1:0]
	}
	
	this.m_scoreString = TagNodeValue(doc,"scorestring")
	this.m_resign = new Boolean(eval(TagNodeValue(doc,"resign")))
	score_node		= TagNodes(doc,"score")[0]
	this.m_sets		= new Array()
	nb_sets = 0
	// Parse each set
	for( i=0; i<NbChildren(score_node); i++ )
	{
		node = Child(score_node,i)
		if( node.nodeName == "s" )
		{
			current_set = new Array( NodeValue(Child(node,0)), NodeValue(Child(node,1)), NodeValue(Child(node,2)) )
			this.m_sets[nb_sets++] = current_set
		}
	}
	// Categories
	this.m_categories = new Array()
	set_node = Child( TagNodes(doc,"stats")[0], 0)
	for( i=0; i<NbChildren(set_node); i++ )
	{
		var category_node = Child(set_node,i)
		this.m_categories[i] = category_node.getAttribute("title")
	}
}
