var parrain_form = function()
{
	var id, left, top, width, height, back;
	var isPlaying;
	var contained;
	var formXHR;
	
	var nbTable;
	var coms = new Array();
	var bList, bBan;
	var formContained;
	var table = null;
	var me = null;
	var toChangeCell = null;
	var infoDiv = null;
	var tablesDiv = null;
	var divNoCom = null;
	var previousSelected = null;
	var bansDiv = 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'));
		back = node.getAttribute('back');
		
		if( connected )
		{
			isLoading = true;
			ajaxRequest( "././php/getFilleul.php", formXHR, this.loadingDone, "" );
		}
		else
		{
			if( back != "" )
			{
				var preload = new Image();
				preload.onload = makeNextObject;
				preload.src = back;
			}
			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 / 5 );
				if( nbT > nbTable * 5 )
					nbTable++;
				
				for( var i = 1; i < lines.length; i++ )
				{
					var values = lines[i].split('|');
					if( values.length == 4 )
					{
						var newLine = new Array(4);
						newLine[0] = values[0];
						newLine[1] = values[1];
						newLine[2] = values[2];
						newLine[3] = values[3];
						coms.push( newLine );
					}
				}
			}
		}
		
		// First time
		if( !isPlaying )
		{
			if( back != "" )
			{
				var preload = new Image();
				preload.onload = makeNextObject;
				preload.src = back;
			}
			else makeNextObject();
		}
		else
			me.fillTable();	// Refresh
	}

	this.fillTable = function()
	{
		if( table != null )
		{
			removeAllNodes( table, true );
			removeAllNodes( infoDiv, true );
			table = null;
			infoDiv = null;
		}
		
		var widthRef = width - 60;
		
		table = document.createElement("table");
		table.style.position = "absolute";
		table.style.top = "120px";
		table.style.borderColor = "#0D5F05";
		table.style.borderStyle = "solid";
		table.style.borderWidth = "2px";
		table.width = widthRef;
		
		var row = document.createElement("tr");
		var cell1 = document.createElement("td");
		cell1.style.width = "100px";
		cell1.style.borderStyle = "solid";
		cell1.style.borderWidth = "2px";
		var celltxt1 = document.createTextNode("Filleul");
		var cell2 = document.createElement("td");
		cell2.style.width = "140px";
		cell2.style.borderStyle = "solid";
		cell2.style.borderWidth = "2px";
		var celltxt2 = document.createTextNode("Date d'inscription");
		var cell3 = document.createElement("td");
		cell3.style.width = "280px";
		cell3.style.borderStyle = "solid";
		cell3.style.borderWidth = "2px";
		var celltxt3 = document.createTextNode("Etat / Kdor joués");
					
		cell1.appendChild(celltxt1);
		row.appendChild(cell1);
		cell2.appendChild(celltxt2);
		row.appendChild(cell2);
		cell3.appendChild(celltxt3);
		row.appendChild(cell3);
		table.appendChild(row);
		
		var gain = 0;
		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";
			curCell1.title = coms[i][0];
			var curCell1txt1 = document.createTextNode(coms[i][0]);
			var curCell2 = document.createElement("td");
			curCell2.style.borderStyle = "solid";
			curCell2.style.borderWidth = "1px";
			curCell2.style.fontWeight = "normal";
			curCell2.style.overFlow = "hidden";
			curCell2.title = "Inscrit depuis le : " + DateTimeToFrStr(coms[i][1]);
			var curCelltxt2 = document.createTextNode(DateTimeToFrStr(coms[i][1]));
			var curCell3 = document.createElement("td");
			curCell3.style.borderStyle = "solid";
			curCell3.style.borderWidth = "1px";
			curCell3.style.fontWeight = "normal";
			
			curCell1.appendChild(curCell1txt1);
			curRow.appendChild(curCell1);
			curCell2.appendChild(curCelltxt2);
			curRow.appendChild(curCell2);
			
			// States
			if( coms[i][2] == '0' )
			{
				var valTxt = document.createTextNode("Inscription incomplête");
				curCell3.title = "Inscrit, mais n'a pas rempli ses coordonnées...";
				curCell3.appendChild(valTxt);
			}
			else if( coms[i][2] == '1' || coms[i][2] == '2' )
			{
				gain += parseInt(coms[i][3]);
				var valTxt = document.createTextNode("Inscrit : " + coms[i][3] + " Kdor");
				curCell3.title = "Inscrit. A joué : " + coms[i][3] + " Kdor";
				curCell3.appendChild(valTxt);
			}
			else if( coms[i][2] == '3' )
			{
				var valTxt = document.createTextNode("Bloqué temporairement");
				curCell3.title = "Bloqué temporairement";
				curCell3.appendChild(valTxt);
			}
			else if( coms[i][2] == '4' )
			{
				var valTxt = document.createTextNode("Bloqué à vie");
				curCell3.title = "Bloqué à vie";
				curCell3.appendChild(valTxt);
			}
			else if( coms[i][4] == '5' )
			{
				var valTxt = document.createTextNode("Désinscrit");
				curCell3.title = "Désinscrit";
				curCell3.appendChild(valTxt);
			}
			
			curRow.appendChild(curCell3);
			table.appendChild(curRow);
		
			i++;
		}
		
		formContained.appendChild(table);
		
		infoDiv = document.createElement("div");
		infoDiv.style.position = "absolute";
		infoDiv.style.top = height - 100 + "px";
		infoDiv.style.textAlign = "center";
		infoDiv.style.width = widthRef + "px";
		var infoTxt = null;
		gain = parseInt(gain / 35);
		if( gain > 0 )
			infoTxt = document.createTextNode("Ces filleuls vous ont rapporté " + gain + " Kdor !");
		else
			infoTxt = document.createTextNode("Ces filleuls ne vous ont pour l'instant rapporté aucun kdor...");
		
		infoDiv.appendChild(infoTxt);
		formContained.appendChild(infoDiv);
	}

	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";
			contained.style.background = "url('" + back + "')";
			contained.style.backgroundRepeat = "no-repeat";
			
			// Form contained
			formContained = document.createElement("div");
			formContained.style.left = "30px";
			formContained.style.color = "#0B0B64";
			formContained.style.position = "absolute";
			formContained.style.fontWeight = "bold";
			formContained.style.fontFamily = "times";
			
			if( connected )
			{
				var widthRef = width - 80;
				var divTitle = document.createElement("div");
				divTitle.style.textAlign = "center";
				divTitle.style.top = "10px";
				divTitle.style.width = widthRef + "px";
				divTitle.style.position = "absolute";
				divTitle.style.fontSize = "24px";
				var txt1 = document.createTextNode("Parrainage");
				
				var affTxtDiv = document.createElement("div");
				affTxtDiv.style.position = "absolute";
				affTxtDiv.style.top = "42px";
				affTxtDiv.style.width = widthRef + "px";
				affTxtDiv.style.textAlign = "center";
				var affTxt = document.createTextNode("En parrainant, vous gagnez un Kdor (cumulable et illimité) à chaque fois que l'ensemble de vos filleuls enregistrés jouent 35 Kdor !");
				affTxtDiv.appendChild(affTxt);
				formContained.appendChild(affTxtDiv);
				
				bList = document.createElement("input");
				bList.style.position = "absolute";
				bList.style.top = "88px";
				bList.style.left = "60px";
				bList.style.width = "140px";
				bList.style.border = "double";
				bList.type = "button";
				bList.value = "Lister mes filleuls";
				bList.onclick = function()
				{
					if( tablesDiv != null )
						tablesDiv.style.display = "block";
					if( table != null )
						table.style.display = "block";
					if( infoDiv != null )
						infoDiv.style.display = "block";
					if( coms.length < 1 && divNoCom != null )
						divNoCom.style.display = "block";
					bansDiv.style.display = "none";
				}
				formContained.appendChild(bList);
				
				bBan = document.createElement("input");
				bBan.style.position = "absolute";
				bBan.style.top = "88px";
				bBan.style.left = "220px";
				bBan.style.width = "240px";
				bBan.style.border = "double";
				bBan.type = "button";
				bBan.value = "Mes liens et bannières de parrainage";
				bBan.onclick = function()
				{
					if( tablesDiv != null )
						tablesDiv.style.display = "none";
					if( table != null )
						table.style.display = "none";
					if( infoDiv != null )
						infoDiv.style.display = "none";
					if( divNoCom != null )
						divNoCom.style.display = "none";
					bansDiv.style.display = "block";
				}
				formContained.appendChild(bBan);
				
				bansDiv = document.createElement("div");
				bansDiv.style.display = "none";
				bansDiv.style.position = "absolute";
				bansDiv.style.top = "130px";
				bansDiv.style.width = widthRef + "px";
				
				var explainTxt = document.createTextNode("Votre lien est actif une semaine. Si un filleul potentiel ne s'inscrit que trois jours après avoir cliqué sur votre lien, vous serez tout de même son parrain.");
				var directLinkTxt = document.createTextNode("Lien direct : ");
				var directLinkDiv = document.createElement("b");
				directLinkDiv.style.background = "#C2F4F3";
				var directLinkDivTxt = document.createTextNode("http://www.hookdo.com/index.php?parrain=" + myId);
				var directLinkBr = document.createElement("br");
				var banLinkTxt = document.createTextNode("Bannière 468x60 : ");
				var banLinkDiv = document.createElement("div");
				banLinkDiv.style.background = "#C2F4F3";
				var banLinkDivTxt = document.createTextNode("<a href=\"http://www.hookdo.com/index.php?parrain=" + myId + "\"><img src=\"http://www.hookdo.com/data/images/bans/banniere.gif\" border=0 /></a>");
				var banImg = document.createElement("img");
				banImg.src = "./data/images/bans/banniere.gif";
				var imgBr = document.createElement("br");
				bansDiv.appendChild(directLinkTxt);
				directLinkDiv.appendChild(directLinkDivTxt);
				bansDiv.appendChild(directLinkDiv);
				bansDiv.appendChild(directLinkBr);
				bansDiv.appendChild(banLinkTxt);
				banLinkDiv.appendChild(banLinkDivTxt);
				bansDiv.appendChild(banLinkDiv);
				bansDiv.appendChild(banImg);
				bansDiv.appendChild(imgBr);
				bansDiv.appendChild(explainTxt);
				formContained.appendChild(bansDiv);
				
				if( coms.length > 0 )
				{
					this.fillTable();
					
					if( nbTable > 1 )
					{
						currentPageOnTable = 1;
						currentTable = 0;
						me.makeNumTable(5, 10);
					}
				}
				else
				{
					divNoCom = document.createElement("div");
					divNoCom.style.textAlign = "center";
					divNoCom.style.top = "180px";
					divNoCom.style.width = width - 80 + "px";
					divNoCom.style.position = "absolute";
					divNoCom.style.fontSize = "18px";
					var txtNoCom = document.createTextNode("Vous n'avez aucun filleul pour le moment...");
					divNoCom.appendChild(txtNoCom);
					formContained.appendChild(divNoCom);
				}
				
				divTitle.appendChild(txt1);
				formContained.appendChild(divTitle);
			}
			else
			{
				formContained.style.top = "120px";
				formContained.style.width = width - 80 + "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 suivre ici vos filleuls...");
				var brC1 = document.createElement("br");
				var txtC2 = document.createTextNode("Et inviter vos amis, ou faire de la publicité pour votre lien de parrainage !");
				
				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 - 80;
		
		if( tablesDiv != null )
		{
			removeAllNodes( tablesDiv, true );
			tablesDiv = null;
		}
		tablesDiv = document.createElement("div");
		tablesDiv.style.position = "absolute";
		tablesDiv.style.top = height - 46 + "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/getFilleul.php", formXHR, me.loadingDone, "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/getFilleul.php", formXHR, me.loadingDone, "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/getFilleul.php", formXHR, me.loadingDone, "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;
		}
	}
}