var recharges_form = function()
{
	var id, left, top, width, height, back;
	var isPlaying;
	var contained;
	var formXHR;
	
	var nbTable;
	var coms = new Array();
	var formContained;
	var table = null;
	var me = null;
	var toChangeCell = null;
	var previousSelected = 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/getRecharges.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 / 10 );
				if( nbT > nbTable * 10 )
					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 );
			table = null;
		}
		
		var widthRef = width - 60;
		
		table = document.createElement("table");
		table.style.position = "absolute";
		table.style.top = "40px";
		table.style.borderColor = "#0D5F05";
		table.style.borderStyle = "solid";
		table.style.borderWidth = "2px";
		table.width = widthRef;
		
		var tbody = document.createElement('tbody');
		table.appendChild(tbody);
		
		var row = document.createElement("tr");
		var cell1 = document.createElement("td");
		cell1.style.width = "80px";
		cell1.style.borderStyle = "solid";
		cell1.style.borderWidth = "2px";
		var celltxt1 = document.createTextNode("Date");
		var cell2 = document.createElement("td");
		cell2.style.width = "160px";
		cell2.style.borderStyle = "solid";
		cell2.style.borderWidth = "2px";
		var celltxt2 = document.createTextNode("Type");
		var cell3 = document.createElement("td");
		cell3.style.width = "80px";
		cell3.style.borderStyle = "solid";
		cell3.style.borderWidth = "2px";
		var celltxt3 = document.createTextNode("Kdor");
		var cell4 = document.createElement("td");
		cell4.style.width = "100px";
		cell4.style.borderStyle = "solid";
		cell4.style.borderWidth = "2px";
		var celltxt4 = document.createTextNode("Code");
					
		cell1.appendChild(celltxt1);
		row.appendChild(cell1);
		cell2.appendChild(celltxt2);
		row.appendChild(cell2);
		cell3.appendChild(celltxt3);
		row.appendChild(cell3);
		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(DateTimeToFrStr(coms[i][0]));
			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][1] == 1 )
				curCelltxt2 = document.createTextNode("Appel Allopass");
			else if( coms[i][1] == 2 )
				curCelltxt2 = document.createTextNode("Sms Allopass");
			else if( coms[i][1] == 3 )
				curCelltxt2 = document.createTextNode("Appel Rentabiliweb");
			else if( coms[i][1] == 4 )
				curCelltxt2 = document.createTextNode("Sms Rentabiliweb");
			else if( coms[i][1] == 5 )
				curCelltxt2 = document.createTextNode("Neosurf Rentabiliweb");
			else if( coms[i][1] == 6 )
				curCelltxt2 = document.createTextNode("Carte bancaire Rentabiliweb");
			else if( coms[i][1] == 7 )
				curCelltxt2 = document.createTextNode("Sms wipub");
			else if( coms[i][1] == 8 )
				curCelltxt2 = document.createTextNode("Code offert");
			else
				curCelltxt2 = document.createTextNode("Type manquant");
			var curCelltxt3 = document.createTextNode(coms[i][2]);
			var curCell3 = document.createElement("td");
			curCell3.style.borderStyle = "solid";
			curCell3.style.borderWidth = "1px";
			curCell3.style.fontWeight = "normal";
			var curCelltxt4 = document.createTextNode(coms[i][3]);
			var curCell4 = document.createElement("td");
			curCell4.style.borderStyle = "solid";
			curCell4.style.borderWidth = "1px";
			curCell4.style.fontWeight = "normal";
			
			curCell1.appendChild(curCell1txt1);
			curRow.appendChild(curCell1);
			curCell2.appendChild(curCelltxt2);
			curRow.appendChild(curCell2);
			curCell3.appendChild(curCelltxt3);
			curRow.appendChild(curCell3);
			curCell4.appendChild(curCelltxt4);
			curRow.appendChild(curCell4);
			tbody.appendChild(curRow);
		
			i++;
		}
		
		formContained.appendChild(table);
	}

	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("Recharges");
				
				if( coms.length > 0 )
				{
					this.fillTable();
					
					if( nbTable > 1 )
					{
						currentPageOnTable = 1;
						currentTable = 0;
						me.makeNumTable(10, 10);
					}
				}
				else
				{
					var 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 effectué aucune recharge 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 recharges...");
				var brC1 = document.createElement("br");
				var txtC2 = document.createTextNode("Et ainsi gérer vos dépenses...");
				
				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/getRecharges.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/getRecharges.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/getRecharges.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;
		}
	}
}