/*
htmlTableau( jat, ie, it)
htmlResultat( jat, ie, it, nc)
*/

//=========Fonctions navigation

var curU;
var curE, curT;

function Init()
{
	var i;
	var u = String( document.location).split( "#");
	var p = u[1];
	curU = u[0];

	if( p)
	{
		p = p.split( ";");

		for( i=0; p.length>i; i++)
		{
			switch( p[i].substring( 0, 1))
			{
			case 'e':	curE = p[i].substring( 1);	break;
			case 't':	curT = p[i].substring( 1);	break;
			}
		}
	}
}
Init();

//=========Fonction tableaux

function isCache( b) {
	return !b || ((b.Fla ) & 0x1); 
}

function	iCol( i) {
	var col = -1;
	for (i++; i; i >>= 1, col++);
	return col;
}
function	iHautCol( col) {
	return ( 1 << (col+1)) -2;
}
function	iBasCol( col) {
	return ( 1 << col) -1;
}
function	nInCol( col) {
	return (1 << col);
}
function	iColMaxQ( nCol, nQ) {
	return iCol( nQ -2) + nCol;
}
function	iColMinQ( nQ) {
	return iCol( nQ -2) + 1;
}
function	iBasColQ( col, nQ) {
	return ( iHautCol(col) - nInColQ(col, nQ) + 1);
}
function	nInColQ( col, nQ) {
	return nQ * nInCol( col - iColMinQ(nQ));
}
function	iBoiteMinQ( nQ) { 
	return iBasColQ( iColMinQ( nQ), nQ);
}
function	iBoiteMaxQ( nCol, nQ){ 
	return iHautCol( iColMaxQ( nCol, nQ));
}
function ADVERSAIRE1( i) { 
	return (i<<1)+2; 
}
function ADVERSAIRE2( i) { 
	return (i<<1)+1; 
}
function IMATCH( i) { 
	return (i-1)>>1; 
}
function IAUTRE( i) {
	return i&1 ? i+1 : i-1; 
}

function iColPoule( i, nCol) {
	return Math.floor( i / nCol);
}
function iRowPoule( i, nCol) {
	return i % nCol;
}

function POULE_ADVERSAIRE1( i, nCol) { 
	return (i % nCol) + (nCol * nCol); 
}
function POULE_ADVERSAIRE2( i, nCol) { 
	return Math.floor( i / nCol) + (nCol * nCol); 
}

function v( s)
{
	return s ? s : "";
}

//=========Fonction exportées

function htmlTableau( jat, ie, it)
{
	var h="";
	var e, t, l, j, c, ib, iv, nLigne, bMax, cMax, cMin, oE, oT, oB, oJ;

	ie = Number( ie);
	it = Number( it);

	oE = jat.E[ ie];

	if( 0<=it && it<oE.T.length) {
		oT = oE.T[ it];

		if( !oT.Typ || oT.Typ == 1) {

			nLigne = nInColQ( iColMaxQ( oT.Col, oT.Qua), oT.Qua) <<1;
			bMax = iBoiteMaxQ( oT.Col, oT.Qua);
			cMax = iColMaxQ( oT.Col, oT.Qua);
			cMin = iColMinQ( oT.Qua);

			h+= "<table border='0' cellspacing='0' width='100%'>";

			for( l=0; l<nLigne; l++) {

				h+= "<tr>";

				ib = bMax - (l >>1);
				iv = ADVERSAIRE1( bMax) - l;

				for( c = cMax; c >= cMin; c--) {
						
					oB = oT.B[ ib];
					oJ = (oB && oB.Jou>=0) ? jat.J[ oB.Jou] : 0;

					if( l == 0) {
						i = Math.floor(100 / (cMax - cMin +1));
						h+= "<td width='"+ i +"%' class='J";
					} else
						h+= "<td class='J";

					if( oB) {
						if( c == cMin)
							h+= iv & 1 ? "B" : "H";
						else
							h+= !(ib & 1) ? "H" : "B";
					} else 
						h+= "C";

					if( iv & 1) 
						h+= "S";

					i = 1 << ( cMax - c);
					h+= "'"+ (i>1 ? " rowspan="+ i : "") +">";

					if( oB && !isCache( oB)) {
						if( !(iv & 1)) {

							h+= htmlJoueur( jat, oB, oJ, oT, ib) + "&nbsp;";

							if( oB.QS)
								h+= "</td><td"+ (i>1 ? " rowspan="+ i : "") +" valign=bottom><b>Q"+ oB.QS +"</b>";
						} else {
							if( oB.Jou>=0 && oB.Ord && oJ.Clu)
								h+= oJ.Clu;
							h+= oB.Sco ? oB.Sco : "&nbsp;";
						}
					}

					h+= "</td>\n";

					if( iv & 1)
						break;

					iv = IMATCH( iv);
					ib = IMATCH( ib);
				}

				h+= "</tr>\n";
			}

			h+= "</table>";
		} else {
			//Poule
			h+= "<table border='1' cellspacing='0' cellpadding='5'>\n";

			bMax = oT.Col * (oT.Col +1) -1;
			cMax = oT.Col;
			cMin = 0;

			//Ligne des joueurs
			h+= "<tr><td class='PX'></td>";
			
			for( c = 0; c < oT.Col; c++) {
				ib = bMax - c;

				oB = oT.B[ ib];

				h+= "<td width='"+ Math.floor( 80 / oT.Col) +"%' align='center'>";

				if( oB && oB.Jou>=0) {
					oJ = jat.J[ oB.Jou];
					h+= oJ.Nom +"&nbsp;"+ v( oJ.Pre).substring( 0, 1) +"</td>";
				} else
					h+= "&nbsp;</td>";
			}
			h+= "</tr>\n";
			
			//La grille des matches
			for( l=oT.Col -1; l>=0; l--) {

				h+= "<tr><td>";

				//Colonne des joueurs
				ib = bMax + l - oT.Col +1;
				oB = oT.B[ ib];

				if( oB) {
					oJ = oB.Jou>=0 ? jat.J[ oB.Jou] : 0;
					h+= htmlJoueur( jat, oB, oJ);
				} else
					h+= "&nbsp;</td>";

				h+= "</td>\n";

				//Les matches
				for( c = cMax -1; c >= 0; c--) {

					ib = c * oT.Col + l;
					oB = oT.B[ ib];

					if( !oB || isCache( oB))
						h+= "\t<td class='PX'>&nbsp;</td>\n";
					else {
						h+= "\t<td align='center'>";

						oJ = oB.Jou>=0 ? jat.J[ oB.Jou] : 0;	//bug 01/10/2003
						h+= htmlJoueur( jat, oB, oJ);
						h+= "<br>";

						h+= oB.Sco ? oB.Sco : "&nbsp;";
						h+= "</td>\n";
					}
				}

				//Colonne des qualifiés
				ib = l * (oT.Col +1);
				oB = oT.B[ ib];

				if( oB && oB.QS)
					h+= "<td><b>Q"+ oB.QS +"</b></td>";

				h+= "</tr>\n";
			}
			h+= "</table>\n";
		}
	}
	return h;

	function htmlJoueur( jat, oB, oJ, oT, ib)
	{
		var h="";

//		if( oB.Ord)
//			h+= "<i><small>"+ oB.Ord +"</small></i> ";
//		else 
		if( oB.QE)
			h+= "<b>Q"+ (oB.QE!=-1?oB.QE:"") +" </b> ";
                            
		if( oB.Jou>=0) {
			if( oB.Ord || ib==0)
				h+= "<b>"+ oJ.Nom +" "+ v( oJ.Pre) +"</b> "+ v( oJ.Cls);
			else
				h+= oJ.Nom +"&nbsp;"+ String( v( oJ.Pre)).substring( 0, 1);
		}

		if( oB.TS)
			h+= " ("+ oB.TS +")";

		if( oB.Jou>=0 && String( oB.Sco).indexOf( 'VD') != -1) {
			h+= " requalifié<br>";

			breq = oT.B[ ADVERSAIRE1( ib)];
			if( breq.Jou>=0 && breq.Jou == oB.Jou)
				breq = oT.B[ ADVERSAIRE2( ib)];
			if( breq.Jou>=0) {
				jreq = jat.J[ breq.Jou];

				h+= jreq.Nom +"&nbsp;"+ v( jreq.Pre).substring( 0, 1);

				h+= " se retire";
			}
		}

		return h;
	}
}

function isMatch( t, i) {
	if( ADVERSAIRE1( i) > iBoiteMaxQ( t.Col, t.Qua) 
	||  ADVERSAIRE2( i) > iBoiteMaxQ( t.Col, t.Qua))
		return false;

	if( t.Typ == 2)	//Poule
		return iColPoule( i, t.Col) < t.Col && iColPoule( i, t.Col) < iRowPoule( i, t.Col);

	if( t.Typ == 3)	//Poule A/R
		return iColPoule( i, t.Col) < t.Col && iColPoule( i, t.Col) != iRowPoule( i, t.Col);

	b = t.B[ ADVERSAIRE1( i)];
	if( b && (b.Jou>=0 || b.QE))
		return true;

	b = t.B[ ADVERSAIRE2( i)];
	if( b && (b.Jou>=0 || b.QE)) 
		return true;

	return false;
}

function htmlResultat( jat, ie, it, nc)
{
	var h="";
	var oE, iv;

	if( !nc)
		nc = 3;	//Nb tours

	e = jat.E[ ie]; 
	t = e.T[ it]; 

	if( (t.Typ == 0 || t.Typ == 1) && t.Qua == 1) {

		h+= "<table border='0' cellpadding='5'>";

		b = t.B[ 0];
		if( b && b.Jou>=0) {
			j = jat.J[ b.Jou];

			h+= "<tr><td>Vainqueur:</td>";

			h+= "<td><b>"+ j.Nom +" "+ v( j.Pre) +"</b> "+ v( j.Cls) +" "+ v( j.Clu) +"</td><td>"+ v( b.Sco) +"</td></tr>";
		}
		cMax = iColMaxQ( t.Col, t.Qua);
		for( c=0; c<nc && c<cMax; c++) {

			s = c ? ("1/"+ (1<<c) +" finalistes: ") : "Finaliste: ";

			for( ib = iHautCol( c); ib >= iBasColQ( c, t.Qua); ib--) {

				//1/c Finaliste
				b = t.B[ ib];
				if( b && b.Jou>=0) {

					if( c < cMax) {
						b1 = t.B[ ADVERSAIRE1( ib)];
						iv = (b1 && b1.Jou == b.Jou) ? ADVERSAIRE2( ib) : ADVERSAIRE1( ib);
						b = t.B[ iv];
					}

					if( b && b.Jou>=0) {
						h+= "<tr><td>"+ s +"</td><td>";
						s = "&nbsp;";

						j = jat.J[ b.Jou];
						h+= "<b>"+ j.Nom +" "+ v( j.Pre) +"</b> "+ v( j.Cls) +" "+ v( j.Clu) +"</td></tr>";
					}
					
				} else {

					if( isMatch( t, ib)) {

						for( iv = ADVERSAIRE1( ib);; iv = IAUTRE( iv)) {

							b = t.B[ iv];

							if( b && b.Jou>=0) {
								h+= "<tr><td>"+ s +"</td><td>";
								s = "&nbsp;";

								j = jat.J[ b.Jou];
								h+= "<b>"+ j.Nom +" "+ v( j.Pre) +"</b> "+ v( j.Cls) +" "+ v( j.Clu) +"</td></tr>";
							}
							if( iv & 1)
								break;
						}
					}
				}
			}
		}

		h+= "</table>";
	}
	return h;
}

//=========Fonctions Netscape

// Netscape fix resize bug Ns4
function NS_CssFix() 
{
	if( document.NS.initWidth  != window.innerWidth ||
	    document.NS.initHeight != window.innerHeight) 
		document.location = document.location;
}

function NS_CssFixCheckIn() 
{
	if ((navigator.appName == 'Netscape') && (parseInt(navigator.appVersion) == 4)) 
	{
		if (typeof document.NS == 'undefined')
		{
			document.NS = new Object;
			document.NS.initWidth  = window.innerWidth;
			document.NS.initHeight = window.innerHeight;
		}
		window.onresize = NS_CssFix;
	}
}

NS_CssFixCheckIn();

