var divScoreExact = document.getElementById('scoreExact');
var nombreMatch = 0;
var matchSelect = 0;
var selectMatch = document.getElementById('nbMatch');
var nombre = 0;
var listeScore = new Array();
var listeEquipe = new Array();
var color1 = 'none';
var color2 = '#CFB5B5';
var color3 = '#B3B3B3'; //gris 


function checkNbMatch()
{
	nombreMatch = selectMatch.value;
	promptNbScore(1);
}

function promptNbScore(nb)
{
	matchSelect = nb;
	divScoreExact.innerHTML = 'Combien de score pour le match n°' + nb + ' ?\n<select id = "selectNbMatch" onchange = "javascript:setNbMatch(this);" onkeyup = "javascript:setNbMatch(this);"><option>1</option><option>2</option><option>3</option><option>4</option><option>5</option><option>6</option></select><table><tbody id ="listeScore"></tbody></table><br /><input type = "button" onclick = "javascript:validScore(' + nb + ');" value = "Go >" />';
	setNbMatch(document.getElementById('selectNbMatch'));
}

function setNbMatch(selectNode)
{
	nombre = selectNode.options[selectNode.selectedIndex].text;
	tbodyNode = document.getElementById('listeScore');

	while(tbodyNode.hasChildNodes())
		tbodyNode.removeChild(tbodyNode.firstChild);

	trNode = document.createElement('tr');
	tdNode = document.createElement('td');
	tdNode.style.textAlign = 'center';
	tdNode.innerHTML = "";
	trNode.appendChild(tdNode);
	
	tdNode = document.createElement('td');
	tdNode.style.textAlign = 'center';
	tdNode.innerHTML = "Equipe 1";
	trNode.appendChild(tdNode);
	
	tdNode = document.createElement('td');
	tdNode.style.textAlign = 'center';
	tdNode.innerHTML = " ";
	trNode.appendChild(tdNode);
	
	tdNode = document.createElement('td');
	tdNode.style.textAlign = 'center';
	tdNode.innerHTML = "Equipe 2";
	trNode.appendChild(tdNode);
	
	tbodyNode.appendChild(trNode);
	
	trNode = document.createElement('tr');
	tdNode = document.createElement('td');
	tdNode.style.textAlign = 'center';
	tdNode.innerHTML = "Nom : ";
	trNode.appendChild(tdNode);

	tdNode = document.createElement('td');
	tdNode.style.textAlign = 'center';
	tdNode.innerHTML = "<input type = 'text' id = 'nom-equipe1' />";
	trNode.appendChild(tdNode);

	tdNode = document.createElement('td');
	tdNode.style.textAlign = 'center';
	tdNode.innerHTML = " - ";
	trNode.appendChild(tdNode);

	tdNode = document.createElement('td');
	tdNode.style.textAlign = 'center';
	tdNode.innerHTML = "<input type = 'text' id = 'nom-equipe2' />";
	trNode.appendChild(tdNode);

	tbodyNode.appendChild(trNode);
	for(i = 0; i < nombre; i++)
	{
		trNode = document.createElement('tr');
		tdNode = document.createElement('td');
		tdNode.style.textAlign = 'center';
		tdNode.innerHTML = "Score " + (i + 1) + " : ";
		trNode.appendChild(tdNode);
		
		tdNode = document.createElement('td');
		tdNode.style.textAlign = 'center';
		tdNode.innerHTML = "<input type = 'text' id = 'equipe1-" + i + "' />";
		trNode.appendChild(tdNode);
		
		tdNode = document.createElement('td');
		tdNode.style.textAlign = 'center';
		tdNode.innerHTML = " - ";
		trNode.appendChild(tdNode);
		
		tdNode = document.createElement('td');
		tdNode.style.textAlign = 'center';
		tdNode.innerHTML = "<input type = 'text' id = 'equipe2-" + i + "' />";
		trNode.appendChild(tdNode);
		
		tbodyNode.appendChild(trNode);
	}
}

function validScore(nb)
{
	valid = true;
	rez = new Array();
	for(i = 0; i < nombre; i++)
	{
		equipe1Val = document.getElementById("equipe1-" + i).value;
		equipe2Val = document.getElementById("equipe2-" + i).value;
		nomEquipe1 = document.getElementById("nom-equipe1").value;
		nomEquipe2 = document.getElementById("nom-equipe2").value;
		
		if((equipe1Val == '') || (isNaN(equipe1Val)) || (equipe1Val < 0))
		{
			valid = false;
			alert("Le score << " + equipe1Val + " >> est invalide.");
			break;
		}
		
		if((equipe2Val == '') || (isNaN(equipe2Val)) || (equipe2Val < 0))
		{
			valid = false;
			alert("Le score << " + equipe2Val + " >> est invalide.");
			break;
		}
		
		if(nomEquipe1 == '')
		{
			valid = false;
			alert("Le nom d'équipe << " + nomEquipe1 + " >> est invalide.");
			break;
		}
		
		if(nomEquipe2 == '')
		{
			valid = false;
			alert("Le nom d'équipe << " + nomEquipe2 + " >> est invalide.");
			break;
		}
		
		rez[i] = new Array(equipe1Val, equipe2Val);
	}
	
	if(valid)
	{
		listeEquipe.push(new Array(nomEquipe1, nomEquipe2));
		
		if(listeScore.length == 0)
		{
			for(i = 0; i < rez.length; i++)
				listeScore.push(new Array(rez[i]));
		}
		else
		{
			tabTmp = listeScore;
			listeScore = new Array();
			for(h = 0; h < rez.length; h++)
			{
				for(i = 0; i < tabTmp.length; i++)
				{
					tab = new Array();
					for(j = 0; j < tabTmp[i].length; j++)
					{
						tab.push(tabTmp[i][j]);
					}
					tab.push(rez[h]);
					listeScore.push(tab);
				}
			}
		}
		
		if(matchSelect != nombreMatch)
		{
			matchSelect++;
			promptNbScore(matchSelect);	
		}
		else
			afficheMatch('');
	}

}


function afficheMatch()
{
	txt = 'Voici la liste des combinaisons possibles à réaliser sur le bookmaker de votre choix : <br /><br /><table cellspacing = "0" cellpadding = "0" style="display:block; margin-bottom:10px;"><tr align = "center"><td></td>';
	
	for(i = 0; i < listeEquipe.length; i++)
		txt += "<td style = 'padding : 0px 15px 0px 5px; font-weight:bold;'>" + listeEquipe[i][0] + "</td><td>vs</td><td style = 'padding : 0px 5px 0px 15px; font-weight:bold;'>" + listeEquipe[i][1] + "</td>";
	
	txt += '<td></td></tr>';
	
	for(i = 0; i < listeScore.length; i++)
	{
		txt += "<tr align = 'center' onmouseover = 'javascript:this.style.backgroundColor = \"" + color3 + "\";' onmouseout = 'javascript:checkColor(\"" + i + "\");' onclick = 'javascript:checkColor(\"" + i + "\");' id = 'ligne-" + i + "' style = 'background-color : " + color1 + ";'><td><label for = 'combi-" + i + "'>Combinaison " + (parseInt(i) + 1) + " :&nbsp;</td>";
		tab = listeScore[i];
		for(j = 0; j < tab.length; j++)
		{
			txt += "<td><label for = 'combi-" + i + "'>[ " + tab[j][0] + '</td><td> - </td><td>' + tab[j][1] + ' ]</label></td>';		
		}
		txt += "<td><input type = 'checkbox' name = 'combi-" + i + "' id = 'combi-" + i + "' /></td></tr>";
	}
	
	txt += '</table>Il ne vous reste plus qu\'à parier sur vos combinaisons pour réaliser des gains. Nous vous recommandons <a href=\'http://adserver.itsfogo.com/click.aspx?t=i&v=1&zoneid=23833\'>Bwin</a> ou <a href=\'http://www.gambling-affiliation.com/tracking/cpc.php?idv=1508&ids=9862&idc=99\'>Betclic</a> pour effectuer vos paris.';
	document.getElementById("scoreExact").innerHTML = txt;
}



function afficheScore(txt, tab)
{
	newTab = false;
	while((!newTab) && (listeScore.length > 0))
		newTab = listeScore.shift();
	
	txtLcl = '';
	
	for(i = 0; i < tab.length; i++)
	{
		alert(i + ' | ' + txt + ' | ' + newTab);
		if(newTab)
		{
			if(txt != '')
				txtLcl += afficheScore(txt + ' / ' + tab[i][0] + ' - ' + tab[i][1], newTab);
			else
				txtLcl += afficheScore(tab[i][0] + ' - ' + tab[i][1], newTab);
		}
		else
			txtLcl += txt + ' / ' + tab[i][0] + ' - ' + tab[i][1] + "\n";
		alert(i + ' | ' + txt + ' | ' + newTab);
	}	
	
	return(txtLcl);
}

function checkColor(id)
{
	if(document.getElementById("combi-" + id).checked)
		document.getElementById("ligne-" + id).style.backgroundColor = color2;
	else
		document.getElementById("ligne-" + id).style.backgroundColor = color1;
}
