var defis_form = function()
{
	var id, left, top, width, height, type, size, name, game, page;
	var isPlaying;
	var contained;
	var formXHR;
	
	var nbTable;
	var coms = new Array();
	var formContained;
	var table1 = null;
	var me = null;
	
	var isLoading = false;
	
	var currentPage = 1;
	var currentPageOnTable = 0;
	var currentTable = 0;
	
	var tablesDiv = null;
	
	this.start = function( node )
	{
		isPlaying = false;
		
		formXHR = initXHR();
		me = this;
		
		id = node.getAttribute('id');
		left = parseInt(node.getAttribute('left'));
		top = parseInt(node.getAttribute('top'));
		width = parseInt(node.getAttribute('width'));
		height = parseInt(node.getAttribute('height'));
		type = parseInt(node.getAttribute('type'));
		name = node.getAttribute('name');
		size = parseInt(node.getAttribute('size'));
		game = parseInt(node.getAttribute('game'));
		page = node.getAttribute('page');
		
		if( connected )
		{
			isLoading = true;
			ajaxRequest( "././php/getDefis.php", formXHR, this.loadingDone, "game=" + game + "&type=" + paramsURL[0][1] + "&gtype=" + type + "&size=" + size );
		}
		else
			makeNextObject();
	}
	
	this.loadingDone = function( result )
	{
		isLoading = false;
		if( trackSqlError( result ) )
		{
			if( result != "0" )
			{
				var lines = result.split( '&' );
				
				var nbT = parseInt(lines[0]);
				nbTable = parseInt( nbT / size );
				if( nbT > nbTable * size )
					nbTable++;
				
				for( var i = 1; i < lines.length; i++ )
				{
					var values = lines[i].split('|');
					if( values.length == 8 )
					{
						var newLine = new Array(7);
						newLine[0] = values[0];
						newLine[1] = values[1];
						newLine[2] = values[2];
						newLine[3] = values[3];
						newLine[4] = values[4];
						newLine[5] = values[5];
						newLine[6] = values[6];
						newLine[7] = values[7];
						coms.push( newLine );
					}
				}
			}
		}
		
		if( !isPlaying )
			makeNextObject();
		else
			me.fillTable();	// Refresh
	}
	
	this.fillTable = function()
	{
		if( table1 != null )
		{
			removeAllNodes( table1, true );
			table1 = null;
		}
		
		if( coms.length > 0 )
		{
			table1 = document.createElement("table");
			table1.style.position = "absolute";
			table1.style.top = "28px";
			table1.style.borderColor = "#0D5F05";
			table1.style.borderStyle = "solid";
			table1.style.borderWidth = "2px";
			table1.width = width - 30;
			
			var tbody = document.createElement('tbody');
			table1.appendChild(tbody);
			
			var row = document.createElement("tr");
			var cell1 = document.createElement("td");
			cell1.style.width = "30px";
			cell1.style.borderStyle = "solid";
			cell1.style.borderWidth = "2px";
			cell1.title = "Nombre de joueurs pour une partie";
			var celltxt1 = document.createTextNode("Joueurs");
			var cell2 = document.createElement("td");
			cell2.style.width = "50px";
			cell2.style.borderStyle = "solid";
			cell2.style.borderWidth = "2px";
			cell2.title = "Prix de la partie";
			var celltxt2 = document.createTextNode("Frais");
			var cell3 = document.createElement("td");
			cell3.style.width = "90px";
			cell3.style.borderStyle = "solid";
			cell3.style.borderWidth = "2px";
			cell3.title = "Cadeau en jeu";
			var celltxt3 = document.createTextNode("A gagner...");
			var cell4 = document.createElement("td");
			if( paramsURL[0][1] != 1 )
				cell4.style.width = "50px";
			else
				cell4.style.width = "100px";
			cell4.style.borderStyle = "solid";
			cell4.style.borderWidth = "2px";
			var celltxt4 = document.createTextNode("Actions");
						
			cell1.appendChild(celltxt1);
			row.appendChild(cell1);
			cell2.appendChild(celltxt2);
			row.appendChild(cell2);
			cell3.appendChild(celltxt3);
			row.appendChild(cell3);
			
			var cell5 = null;
			if( paramsURL[0][1] == 2 )
			{
				cell5 = document.createElement("td");
				cell5.style.width = "50px";
				cell5.style.borderStyle = "solid";
				cell5.style.borderWidth = "2px";
				var celltxt5 = document.createTextNode("Date");
				cell5.appendChild(celltxt5);
				row.appendChild(cell5);
			}
			
			cell4.appendChild(celltxt4);
			row.appendChild(cell4);
			tbody.appendChild(row);
						
			var i = 0;
			while( i < coms.length )
			{
				var curRow = document.createElement("tr");
				var curCell1 = document.createElement("td");
				curCell1.style.borderStyle = "solid";
				curCell1.style.borderWidth = "1px";
				curCell1.style.fontWeight = "normal";
				var curCell1txt1 = document.createTextNode(coms[i][6] + "/" + coms[i][1]);
				var curCell2 = document.createElement("td");
				curCell2.style.borderStyle = "solid";
				curCell2.style.borderWidth = "1px";
				curCell2.style.fontWeight = "normal";
				curCell2.style.overFlow = "hidden";
				var curCelltxt2 = null;
				if( coms[i][2] > 0 )
					curCelltxt2 = document.createTextNode(coms[i][2] + " Kdor");
				else if( coms[i][3] > 0 )
					curCelltxt2 = document.createTextNode(coms[i][3] + " Hooky");
				else
					curCelltxt2 = document.createTextNode("Gratuit");
				var curCelltxt3 = null;
				if( coms[i][4] < 2 && coms[i][5] > 0 )
				{
					if( coms[i][4] == 0 )
						curCelltxt3 = document.createTextNode(coms[i][5] + " Kdor");
					else if( coms[i][4] == 1 )
						curCelltxt3 = document.createTextNode(coms[i][5] + " Hooky");
				}
				else
				{
					if( coms[i][4] == 2 )
					{
						var kdo_txt = coms[i][5];
						if( kdo_txt.length > 50 )
							kdo_txt = kdo_txt.substr(0, 50);
						curCelltxt3 = document.createTextNode(kdo_txt);
					}
					else
						curCelltxt3 = document.createTextNode("Entrainement");
				}
				var curCell3 = document.createElement("td");
				curCell3.style.borderStyle = "solid";
				curCell3.style.borderWidth = "1px";
				curCell3.style.fontWeight = "normal";
				var curCell4 = document.createElement("td");
				curCell4.style.borderStyle = "solid";
				curCell4.style.borderWidth = "1px";
				curCell4.style.fontWeight = "normal";
				curCell4.style.textAlign = "center";
				
				if( paramsURL[0][1] == 0 )
				{
					var but1 = document.createElement("input");
					but1.type = "button";
					but1.value = "Jouer";
					but1.title = "Jouer cette partie ?";
					but1.name = coms[i][0] + "_" + game;
					but1.onclick = function()
					{
						var params = this.name.split( '_' );
						changeCurrentPage("./data/xml/defis/" + page + ".xml?idg=" + params[0] + "&idgType=" + params[1] );
					}
					curCell4.appendChild(but1);
				}
				else if( paramsURL[0][1] == 1 )
				{
					if( coms[i][1] > 2 )
					{
						var but1 = document.createElement("input");
						but1.type = "button";
						but1.value = "Rejouer";
						but1.title = "Rejouer pour améliorer votre score ?";
						but1.name = coms[i][0] + "_" + game;
						but1.onclick = function()
						{
							var params = this.name.split( '_' );
							changeCurrentPage("./data/xml/defis/" + page + ".xml?idg=" + params[0] + "&idgType=" + params[1] );
						}
						curCell4.appendChild(but1);
					}
					
					var but2 = document.createElement("input");
					but2.type = "button";
					but2.value = "Classement";
					but2.title = "Classement provisoir...";
					but2.name = coms[i][0];
					but2.onclick = function()
					{
						changeCurrentPage("./data/xml/defis/defis_result.xml?type=0&id=" + this.name );
					}
					curCell4.appendChild(but2);
				}
				else if( paramsURL[0][1] == 2 )
				{
					var but2 = document.createElement("input");
					but2.type = "button";
					but2.value = "Classement";
					but2.title = "Classement final...";
					but2.name = coms[i][0];
					but2.onclick = function()
					{
						changeCurrentPage("./data/xml/defis/defis_result.xml?type=2&id=" + this.name );
					}
					curCell4.appendChild(but2);
				}
				
				curCell1.appendChild(curCell1txt1);
				curRow.appendChild(curCell1);
				curCell2.appendChild(curCelltxt2);
				curRow.appendChild(curCell2);
				curCell3.appendChild(curCelltxt3);
				curRow.appendChild(curCell3);
				
				if( paramsURL[0][1] == 2 )
				{
					var curCell5 = document.createElement("td");
					curCell5.style.borderStyle = "solid";
					curCell5.style.borderWidth = "1px";
					curCell5.style.fontWeight = "normal";
					curCell5.title = coms[i][7];
					var curCelltxt5 = document.createTextNode(DateTimeToFrStr(coms[i][7]));
					curCell5.appendChild(curCelltxt5);
					curRow.appendChild(curCell5);
				}
				
				curRow.appendChild(curCell4);
				tbody.appendChild(curRow);
			
				i++;
			}
			
			formContained.appendChild(table1);
		}
		else
		{
			table1 = document.createElement("div");
			table1.style.left = "0px";
			table1.style.top = "28px";
			table1.style.width = width + "px";
			table1.style.textAlign = "center";
			table1.style.position = "absolute";
			var table1Txt = document.createTextNode("Pas de tournois " + name + " actuellement...");
			table1.appendChild(table1Txt);
			formContained.appendChild(table1);
		}
	}

	this.play = function( parent )
	{	
		if( isPlaying == false )
		{
			contained = document.createElement("div");

			contained.setAttribute("id", id);

			contained.style.top = top + "px";
			contained.style.left = left + "px";
			contained.style.width = width + "px";
			contained.style.height = height + "px";
			contained.style.position = "absolute";
			
			// Form contained
			formContained = document.createElement("div");
			formContained.style.left = "24px";
			formContained.style.color = "#0B0B64";
			formContained.style.position = "absolute";
			formContained.style.fontWeight = "bold";
			formContained.style.fontFamily = "times";
			
			if( connected )
			{
				var title1 = document.createElement("u");
				title1.style.left = "0px";
				title1.style.top = "0px";
				title1.style.width = width + "px";
				title1.style.textAlign = "center";
				title1.style.position = "absolute";
				var title1Txt = document.createTextNode("Tournois " + name);
				title1.appendChild(title1Txt);
				formContained.appendChild(title1);
				
				this.fillTable();
				
				if( nbTable > 1 )
				{
					currentPageOnTable = 1;
					currentTable = 0;
					me.makeNumTable(size, 10);
				}
			}
			else if( type == 0 )
			{
				formContained.style.top = "120px";
				formContained.style.width = width + "px";
				formContained.style.textAlign = "center";
				formContained.style.fontSize = "24px";
				var txtC = document.createTextNode("En vous inscrivant,");
				var brC = document.createElement("br");
				var txtC1 = document.createTextNode("vous pourrez défier d'autres joueurs...");
				var brC1 = document.createElement("br");
				var txtC2 = document.createTextNode("Et gagner encore plus !");
				
				formContained.appendChild(txtC);
				formContained.appendChild(brC);
				formContained.appendChild(txtC1);
				formContained.appendChild(brC1);
				formContained.appendChild(txtC2);
			}
			
			contained.appendChild(formContained);
			parent.appendChild(contained);
			
			if( parent == document.getElementById('map_contained') )redimMapContained( left, top, width, height );

			isPlaying = true;
		}
	}
	
	this.makeNumTable = function(rowPerPage, nbPages)
	{
		var widthRef = width;
		
		if( tablesDiv != null )
		{
			removeAllNodes( tablesDiv, true );
			tablesDiv = null;
		}
		tablesDiv = document.createElement("div");
		tablesDiv.style.position = "absolute";
		tablesDiv.style.top = height - 20 + "px";
		tablesDiv.style.textAlign = "center";
		tablesDiv.style.width = widthRef + "px";
		
		var iT = currentTable * nbPages;
		
		if( currentTable > 0 )
		{
			var curButDiv = document.createElement("b");
			curButDiv.style.cursor = "pointer";
			curButDiv.style.color = "#226622";
			curButDiv.style.padding = "2px";
			curButDiv.style.borderStyle = "solid";
			curButDiv.style.borderWidth = "1px";
			curButDiv.onclick = function()
			{
				currentPageOnTable = nbPages;
				currentTable--;
				var popI = coms.length;
				while( popI > 0 )
				{
					coms.pop();
					popI--;
				}
				if( !isLoading )
				{
					isLoading = true;
					currentPage = currentTable * nbPages + currentPageOnTable;
					var correctOffset = (currentPage - 1) * rowPerPage;
					ajaxRequest( "././php/getDefis.php", formXHR, me.loadingDone, "game=" + game + "&type=" + paramsURL[0][1] + "&gtype=" + type + "&size=" + size + "&offset=" + correctOffset );
				}
				me.makeNumTable(rowPerPage, nbPages);
			}
			var curText = document.createTextNode("< ");
			
			curButDiv.appendChild(curText);
			tablesDiv.appendChild(curButDiv);
		}
		
		currentPage = currentTable * nbPages + currentPageOnTable;
		
		var maxIT = nbPages;
		while( iT < nbTable && maxIT > 0 )
		{
			if( iT > 0 )
			{
				var curSep = document.createTextNode(" - ");
				tablesDiv.appendChild(curSep);
			}
			var curButDiv = document.createElement("b");
			if( iT == currentPage - 1 )
			{
				previousSelected = curButDiv;
				curButDiv.style.background = "#8888AA";
			}
			curButDiv.style.cursor = "pointer";
			curButDiv.style.color = "#226622";
			curButDiv.style.padding = "2px";
			curButDiv.style.borderStyle = "solid";
			curButDiv.style.borderWidth = "1px";
			curButDiv.onclick = function()
			{
				previousSelected.style.background = "";
				this.style.background = "#8888AA";
				previousSelected = this;
				var popI = coms.length;
				while( popI > 0 )
				{
					coms.pop();
					popI--;
				}
				
				var butOffset = parseInt(this.firstChild.nodeValue);
				var correctOffset = (butOffset - 1) * rowPerPage;
				if( !isLoading )
				{
					isLoading = true;
					currentPage = butOffset;
					currentPageOnTable = parseInt(currentPage / nbPages);
					ajaxRequest( "././php/getDefis.php", formXHR, me.loadingDone, "game=" + game + "&type=" + paramsURL[0][1] + "&gtype=" + type + "&size=" + size + "&offset=" + correctOffset );
				}
			}
			var curText = document.createTextNode(iT + 1);
			
			curButDiv.appendChild(curText);
			tablesDiv.appendChild(curButDiv);
			iT++;
			maxIT--;
		}
		
		if( nbTable > currentTable * nbPages + nbPages )
		{
			var curSep = document.createTextNode(" - ");
			tablesDiv.appendChild(curSep);
			
			var curButDiv = document.createElement("b");
			curButDiv.style.cursor = "pointer";
			curButDiv.style.color = "#226622";
			curButDiv.style.padding = "2px";
			curButDiv.style.borderStyle = "solid";
			curButDiv.style.borderWidth = "1px";
			curButDiv.onclick = function()
			{
				currentPageOnTable = 1;
				currentTable++;
				var popI = coms.length;
				while( popI > 0 )
				{
					coms.pop();
					popI--;
				}
				if( !isLoading )
				{
					isLoading = true;
					currentPage = currentTable * nbPages + currentPageOnTable;
					var correctOffset = (currentPage - 1) * rowPerPage;
					ajaxRequest( "././php/getDefis.php", formXHR, me.loadingDone, "game=" + game + "&type=" + paramsURL[0][1] + "&gtype=" + type + "&size=" + size + "&offset=" + correctOffset );
				}
				me.makeNumTable(rowPerPage, nbPages);
			}
			var curText = document.createTextNode(">");
			
			curButDiv.appendChild(curText);
			tablesDiv.appendChild(curButDiv);
		}
		
		formContained.appendChild(tablesDiv);
	}
	
	this.stop = function()
	{
		if( isPlaying == true )
		{
			removeAllNodes( contained, true );
			isPlaying = false;
		}
	}
}