// JavaScript Document
var contests=["Buondi Billabong Pro","Sahara Pro Kitaizumi","Ferrolterra Pantin Classic","Gatorade Surf Classic","Zarautz Pro Surf","Oakley NB Pro","Billabong Pro Hyuga","La Caja de Canarias","No Fear Mexican Surf Fiesta","O'Neill Coldwater Classic","La Santa Pro","Maresia Surf International 2007","Onbongo Pro Surfing","Estoril Coast Pro","Xcel Pro","Reef Classic","Hawaiian Pro Haleiwa","O'Neill World Cup"];
var contestsStars=["6 Star","2 Star","4 Star","4 Star","3 Star","2 Star","2 Star","4 Star","2 Star","4 Star","6 Star PRIME","6 Star","6 Star","3 Star","4 Star","1 Star","6 Star PRIME","6 Star PRIME"];
var contestsLeft=contests.length-1;


var contestsSlots;
var indexTri;
var currentSimul;
initForecastXml(contestsLeft);

var tempResSimul=new Array();
for (var i=0;i<contestsLeft;i++) {
	tempResSimul[i]=0;
}
tempResSimul=tempResSimul.join(";");

function initTableau() {
	$('tableauResultatsHeader').innerHTML="next : <b>"+contests[contests.length-contestsLeft]+"</b>";
	var tempHTML="";
	var nbSurfers=surfers.firstChild.childNodes.length;
	tempHTML+="<table cellspacing='0' cellpadding='2'>";

	//alert(tempResSimul);
	
	for (var i=0;i<nbSurfers;i++) {
		var tempTotal=0;
		var tempResults="";
		if (surfers.firstChild.childNodes[i].getAttribute('results')) {
			
			tempResults=surfers.firstChild.childNodes[i].getAttribute('results').split(";");
			var nbResults=tempResults.length;
			tempResults=tempResults.sort(byNumber);
			tempResults=tempResults.reverse();
			
			if (nbResults>7) {
				tempResults=tempResults.slice(0,7);
			}
			
			for (var j=0;j<7;j++) {
				if (tempResults[j]) tempTotal+=parseInt(tempResults[j]);
			}
			tempResults=tempResults.join(";");	
		}
		surfers.firstChild.childNodes[i].setAttribute('totalreel',tempTotal);
		surfers.firstChild.childNodes[i].setAttribute('totalsimul',tempTotal);
		surfers.firstChild.childNodes[i].setAttribute('results',tempResults);
		surfers.firstChild.childNodes[i].setAttribute('resultssimul',tempResSimul);
		surfers.firstChild.childNodes[i].setAttribute('best7simul',tempResults);
	}
	
	//
	
	var index=msort('totalreel');
	indexTri=index.clone();
	var previousRating=1;

	if (isIe) {
		var tempHTMLtr='<tr onMouseOver="this.className=\'trHover\';" onMouseOut="this.className=\'\'">';
	} else  {
		var tempHTMLtr="<tr>";
	}
	for (var i=0;i<nbSurfers;i++) {

		tempHTML+=tempHTMLtr;
		
		if (i>0) {
			if (surfers.firstChild.childNodes[index[i]].getAttribute('totalreel')!=surfers.firstChild.childNodes[index[i-1]].getAttribute('totalreel')) {
				previousRating=(i+1);
			}
		}
		var tempName=nameOfSurfer(index[i]);
		tempHTML+="<td width='25px'>"+previousRating+"</td>";
		tempHTML+="<td width='140px'>"+tempName+"</td>";
		tempHTML+="<td width='45px'>"+surfers.firstChild.childNodes[index[i]].getAttribute('totalreel')+"</td>";
		
		var tempResults=surfers.firstChild.childNodes[index[i]].getAttribute('results').split(";");
		for (var j=0;j<7;j++) {
			if (tempResults[j]) {
				tempHTML+="<td class='cell25' title=''>"+tempResults[j]+"</td>";
			} else {
				tempHTML+="<td class='cell25' title=''>0</td>";
			}
			
		}
		
		tempHTML+="</tr>";
	}
	tempHTML+="</table>";
	$('tableauResultats').innerHTML=tempHTML;
	updateSimulation();
}
function enterSimulation() {
	var tempHTML=""
	if ($('tableauEnterSimulationHeader').innerHTML=="") {
		tempHTML+="Select a contest and enter results :<br/>";
		tempHTML+="<select id='contestForecast' onChange='drawContestSimulation(this.value)'>";
		var nbContests=contests.length;
		
		for (var i=nbContests-contestsLeft;i<nbContests;i++) {
			tempHTML+="<option value='"+i+"'>";
			tempHTML+=contests[i]+" ("+contestsStars[i]+")";
			tempHTML+="</option>";
		}
		
		tempHTML+="</select><br/>";
		tempHTML+="<a onmouseover='ieLinkOver(this)' onmouseout='ieLinkOut(this)' onClick='closeSimulation();'>close</a>";
		$('tableauEnterSimulationHeader').innerHTML=tempHTML;
		
		drawContestSimulation(0);
	}
	
	
	new Effect.Appear('tableauEnterSimulationHeader',{duration:0.3});
	new Effect.Appear('tableauEnterSimulation',{duration:0.3});
	
}
function closeSimulation() {
	new Effect.Fade('tableauEnterSimulationHeader',{duration:0.3});
	new Effect.Fade('tableauEnterSimulation',{duration:0.3});	
}
function drawContestSimulation(ncontest) {
	currentSimul=ncontest;
	//alert('drawContestSimulation'+ncontest);
	var nbSurfers=surfers.firstChild.childNodes.length;
	var tempHTML="";
	tempHTML+="<table cellspacing='0' cellpadding='2' width='100%'>";
	if (isIe) {
		var tempHTMLtr='<tr onMouseOver="this.className=\'trHover\';" onMouseOut="this.className=\'\'">';
	} else  {
		var tempHTMLtr="<tr>";
	}
	
	for (var i=0;i<nbSurfers;i++) {
		tempHTML+=tempHTMLtr;
		tempHTML+="<td id='simul_"+indexTri[i]+"_"+ncontest+"' class='fakeCursor cellsaisie' title=\"points for "+nameOfSurfer(indexTri[i])+" at "+contests[ncontest]+"\" onClick='editResult("+indexTri[i]+","+ncontest+")'>";
		
		var tempResultsSimul=surfers.firstChild.childNodes[indexTri[i]].getAttribute('resultssimul').split(";");
		//tempHTML+="0";
		tempHTML+=tempResultsSimul[ncontest];
		
		tempHTML+="</td>";
		tempHTML+="</tr>";
	}
	tempHTML+="</table>";
	
	
	$('tableauEnterSimulation').innerHTML=tempHTML;
	
}


function initForecastXml(contestsLeft) {
	//alert(nbResults);
	
	var xmlstring="";
	xmlstring+="<contests>";
	
	var nbContests=contests.length;
	
	
	/*for (var i=0;i<nbContests-contestsLeft;i++) {
		xmlstring+="<contest />";
	}*/
	for (var i=0;i<nbContests;i++) {
	//for (var i=nbContests-contestsLeft;i<nbContests;i++) {
		xmlstring+="<contest>";
		switch(contestsStars[i]) {
			case "1 Star":
				xmlstring+="<place lib='1st (winner)' val='250' totalSlots='1' pris='0' />";
				xmlstring+="<place lib='2nd (finalist)' val='219' totalSlots='1' pris='0' />";
				xmlstring+="<place lib='3rd (finalist)' val='188' totalSlots='1' pris='0' />";
				xmlstring+="<place lib='4th (finalist)' val='178' totalSlots='1' pris='0' />";
				xmlstring+="<place lib='5th (semi finals)' val='163' totalSlots='2' pris='0' />";
				xmlstring+="<place lib='7th (semi finals)' val='153' totalSlots='2' pris='0' />";
				xmlstring+="<place lib='9th (quarter finals)' val='138' totalSlots='4' pris='0' />";
				xmlstring+="<place lib='13th (quarter finals)' val='116' totalSlots='4' pris='0' />";
				xmlstring+="<place lib='17th (round of 32)' val='113' totalSlots='8' pris='0' />";
				xmlstring+="<place lib='25th (round of 32)' val='88' totalSlots='8' pris='0' />";
				xmlstring+="<place lib='33rd (round of 64)' val='84' totalSlots='16' pris='0' />";
				xmlstring+="<place lib='49th (round of 64)' val='63' totalSlots='16' pris='0' />";
				xmlstring+="<place lib='65th (round of 80)' val='58' totalSlots='8' pris='0' />";
				xmlstring+="<place lib='73rd (round of 80)' val='56' totalSlots='8' pris='0' />";

				break;			
			case "2 Star":
				xmlstring+="<place lib='1st (winner)' val='500' totalSlots='1' pris='0' />";
				xmlstring+="<place lib='2nd (finalist)' val='438' totalSlots='1' pris='0' />";
				xmlstring+="<place lib='3rd (finalist)' val='375' totalSlots='1' pris='0' />";
				xmlstring+="<place lib='4th (finalist)' val='356' totalSlots='1' pris='0' />";
				xmlstring+="<place lib='5th (semi finals)' val='325' totalSlots='2' pris='0' />";
				xmlstring+="<place lib='7th (semi finals)' val='306' totalSlots='2' pris='0' />";
				xmlstring+="<place lib='9th (quarter finals)' val='275' totalSlots='4' pris='0' />";
				xmlstring+="<place lib='13th (quarter finals)' val='231' totalSlots='4' pris='0' />";
				xmlstring+="<place lib='17th (round of 32)' val='225' totalSlots='8' pris='0' />";
				xmlstring+="<place lib='25th (round of 32)' val='175' totalSlots='8' pris='0' />";
				xmlstring+="<place lib='33rd (round of 64)' val='169' totalSlots='16' pris='0' />";
				xmlstring+="<place lib='49th (round of 64)' val='125' totalSlots='16' pris='0' />";
				xmlstring+="<place lib='65th (round of 96)' val='117' totalSlots='16' pris='0' />";
				xmlstring+="<place lib='81st (round of 96)' val='100' totalSlots='16' pris='0' />";
				xmlstring+="<place lib='97th (round of 112)' val='75' totalSlots='8' pris='0' />";
				xmlstring+="<place lib='105th (round of 112)' val='71' totalSlots='8' pris='0' />";
				break;
			case "3 Star":
				xmlstring+="<place lib='1st (winner) (temp value)' val='1500' totalSlots='1' pris='0' />";
				xmlstring+="<place lib='2nd (finalist) (temp value)' val='1313' totalSlots='1' pris='0' />";
				xmlstring+="<place lib='3rd (finalist) (temp value)' val='1125' totalSlots='1' pris='0' />";
				xmlstring+="<place lib='4th (finalist) (temp value)' val='1069' totalSlots='1' pris='0' />";
				xmlstring+="<place lib='5th (semi finals) (temp value)' val='975' totalSlots='2' pris='0' />";
				xmlstring+="<place lib='7th (semi finals) (temp value)' val='919' totalSlots='2' pris='0' />";
				xmlstring+="<place lib='9th (quarter finals) (temp value)' val='825' totalSlots='4' pris='0' />";
				xmlstring+="<place lib='13th (quarter finals) (temp value)' val='694' totalSlots='4' pris='0' />";
				xmlstring+="<place lib='17th (round of 32) (temp value)' val='675' totalSlots='8' pris='0' />";
				xmlstring+="<place lib='25th (round of 32) (temp value)' val='525' totalSlots='8' pris='0' />";
				xmlstring+="<place lib='33rd (round of 64) (temp value)' val='506' totalSlots='16' pris='0' />";
				xmlstring+="<place lib='49th (round of 64) (temp value)' val='375' totalSlots='16' pris='0' />";
				xmlstring+="<place lib='65th (round of 96) (temp value)' val='350' totalSlots='16' pris='0' />";
				xmlstring+="<place lib='81st (round of 96) (temp value)' val='300' totalSlots='16' pris='0' />";
				xmlstring+="<place lib='97th (round of 128) (temp value)' val='225' totalSlots='16' pris='0' />";
				xmlstring+="<place lib='113th (round of 128) (temp value)' val='200' totalSlots='16' pris='0' />";
				xmlstring+="<place lib='129th (round of 160) (temp value)' val='150' totalSlots='16' pris='0' />";
				xmlstring+="<place lib='149th (round of 160) (temp value)' val='75' totalSlots='16' pris='0' />";
				xmlstring+="<place lib='161st (round of 192) (temp value)' val='50' totalSlots='16' pris='0' />";
				xmlstring+="<place lib='177th (round of 192) (temp value)' val='34' totalSlots='16' pris='0' />";
				break;		
			case "4 Star":
				xmlstring+="<place lib='1st (winner)' val='1500' totalSlots='1' pris='0' />";
				xmlstring+="<place lib='2nd (finalist)' val='1313' totalSlots='1' pris='0' />";
				xmlstring+="<place lib='3rd (finalist)' val='1125' totalSlots='1' pris='0' />";
				xmlstring+="<place lib='4th (finalist)' val='1069' totalSlots='1' pris='0' />";
				xmlstring+="<place lib='5th (semi finals)' val='975' totalSlots='2' pris='0' />";
				xmlstring+="<place lib='7th (semi finals)' val='919' totalSlots='2' pris='0' />";
				xmlstring+="<place lib='9th (quarter finals)' val='825' totalSlots='4' pris='0' />";
				xmlstring+="<place lib='13th (quarter finals)' val='694' totalSlots='4' pris='0' />";
				xmlstring+="<place lib='17th (round of 32)' val='675' totalSlots='8' pris='0' />";
				xmlstring+="<place lib='25th (round of 32)' val='525' totalSlots='8' pris='0' />";
				xmlstring+="<place lib='33rd (round of 64)' val='506' totalSlots='16' pris='0' />";
				xmlstring+="<place lib='49th (round of 64)' val='375' totalSlots='16' pris='0' />";
				xmlstring+="<place lib='65th (round of 96)' val='350' totalSlots='16' pris='0' />";
				xmlstring+="<place lib='81st (round of 96)' val='300' totalSlots='16' pris='0' />";
				xmlstring+="<place lib='97th (round of 128)' val='225' totalSlots='16' pris='0' />";
				xmlstring+="<place lib='113th (round of 128)' val='200' totalSlots='16' pris='0' />";
				xmlstring+="<place lib='129th (round of 160)' val='150' totalSlots='16' pris='0' />";
				xmlstring+="<place lib='149th (round of 160)' val='75' totalSlots='16' pris='0' />";
				xmlstring+="<place lib='161st (round of 192)' val='50' totalSlots='16' pris='0' />";
				xmlstring+="<place lib='177th (round of 192)' val='34' totalSlots='16' pris='0' />";
				break;				
			case "5 Star":
				xmlstring+="<place lib='1st (winner)' val='2000' totalSlots='1' pris='0' />";
				xmlstring+="<place lib='2nd (finalist)' val='1750' totalSlots='1' pris='0' />";
				xmlstring+="<place lib='3rd (semi finals)' val='1500' totalSlots='2' pris='0' />";
				xmlstring+="<place lib='5th (quarter finals)' val='1300' totalSlots='4' pris='0' />";
				xmlstring+="<place lib='9th (round of 12)' val='1100' totalSlots='4' pris='0' />";
				xmlstring+="<place lib='13th (round of 24)' val='925' totalSlots='6' pris='0' />";
				xmlstring+="<place lib='19th (round of 24)' val='875' totalSlots='6' pris='0' />";
				xmlstring+="<place lib='25th (round of 48)' val='700' totalSlots='12' pris='0' />";
				xmlstring+="<place lib='37th (round of 48)' val='650' totalSlots='12' pris='0' />";
				xmlstring+="<place lib='49th (round of 96)' val='500' totalSlots='24' pris='0' />";
				xmlstring+="<place lib='73rd (round of 96)' val='450' totalSlots='24' pris='0' />";
				xmlstring+="<place lib='97th (round of 144)' val='300' totalSlots='24' pris='0' />";
				xmlstring+="<place lib='121st (round of 144)' val='250' totalSlots='24' pris='0' />";
				xmlstring+="<place lib='145th (round of 168)' val='100' totalSlots='12' pris='0' />";
				xmlstring+="<place lib='157th (round of 168)' val='76' totalSlots='12' pris='0' />";
				break;			
			case "5 Star PRIME":
				xmlstring+="<place lib='1st (winner)' val='2500' totalSlots='1' pris='0' />";
				xmlstring+="<place lib='2nd (finalist)' val='2188' totalSlots='1' pris='0' />";
				xmlstring+="<place lib='3rd (semi finals)' val='1875' totalSlots='2' pris='0' />";
				xmlstring+="<place lib='5th (quarter finals)' val='1625' totalSlots='4' pris='0' />";
				xmlstring+="<place lib='9th (round of 16)' val='1375' totalSlots='8' pris='0' />";
				xmlstring+="<place lib='17th (round of 24)' val='1125' totalSlots='8' pris='0' />";
				xmlstring+="<place lib='25th (round of 48)' val='875' totalSlots='12' pris='0' />";
				xmlstring+="<place lib='37th (round of 48)' val='813' totalSlots='12' pris='0' />";
				xmlstring+="<place lib='49th (round of 96)' val='625' totalSlots='24' pris='0' />";
				xmlstring+="<place lib='73rd (round of 96)' val='563' totalSlots='24' pris='0' />";
				xmlstring+="<place lib='97th (round of 144)' val='375' totalSlots='24' pris='0' />";
				xmlstring+="<place lib='121st (round of 144)' val='313' totalSlots='24' pris='0' />";
				xmlstring+="<place lib='145th (round of 192)' val='125' totalSlots='24' pris='0' />";
				xmlstring+="<place lib='169th (round of 192)' val='63' totalSlots='24' pris='0' />";
				break;			
			case "6 Star PRIME":
				xmlstring+="<place lib='1st (winner)' val='3000' totalSlots='1' pris='0' />";
				xmlstring+="<place lib='2nd (finalist)' val='2625' totalSlots='1' pris='0' />";
				xmlstring+="<place lib='3rd (semi finals)' val='2250' totalSlots='2' pris='0' />";
				xmlstring+="<place lib='5th (quarter finals)' val='1950' totalSlots='4' pris='0' />";
				xmlstring+="<place lib='9th (round of 12)' val='1650' totalSlots='4' pris='0' />";
				xmlstring+="<place lib='13th (round of 24)' val='1388' totalSlots='6' pris='0' />";
				xmlstring+="<place lib='19th (round of 24)' val='1313' totalSlots='6' pris='0' />";
				xmlstring+="<place lib='25th (round of 48)' val='1050' totalSlots='12' pris='0' />";
				xmlstring+="<place lib='37th (round of 48)' val='975' totalSlots='12' pris='0' />";
				xmlstring+="<place lib='49th (round of 96)' val='750' totalSlots='24' pris='0' />";
				xmlstring+="<place lib='73rd (round of 96)' val='675' totalSlots='24' pris='0' />";
				xmlstring+="<place lib='97th (round of 144)' val='450' totalSlots='24' pris='0' />";
				xmlstring+="<place lib='121st (round of 144)' val='375' totalSlots='24' pris='0' />";
				xmlstring+="<place lib='145th (round of 192)' val='150' totalSlots='24' pris='0' />";
				xmlstring+="<place lib='169th (round of 192)' val='75' totalSlots='24' pris='0' />";			
				break;
			default:		//default =6 stars
				xmlstring+="<place lib='1st (winner)' val='2500' totalSlots='1' pris='0' />";
				xmlstring+="<place lib='2nd (finalist)' val='2188' totalSlots='1' pris='0' />";
				xmlstring+="<place lib='3rd (semi finals)' val='1875' totalSlots='2' pris='0' />";
				xmlstring+="<place lib='5th (quarter finals)' val='1625' totalSlots='4' pris='0' />";
				xmlstring+="<place lib='9th (round of 16)' val='1375' totalSlots='8' pris='0' />";
				xmlstring+="<place lib='17th (round of 24)' val='1125' totalSlots='8' pris='0' />";
				xmlstring+="<place lib='25th (round of 48)' val='875' totalSlots='12' pris='0' />";
				xmlstring+="<place lib='37th (round of 48)' val='813' totalSlots='12' pris='0' />";
				xmlstring+="<place lib='49th (round of 96)' val='625' totalSlots='24' pris='0' />";
				xmlstring+="<place lib='73rd (round of 96)' val='563' totalSlots='24' pris='0' />";
				xmlstring+="<place lib='97th (round of 144)' val='375' totalSlots='24' pris='0' />";
				xmlstring+="<place lib='121st (round of 144)' val='313' totalSlots='24' pris='0' />";
				xmlstring+="<place lib='145th (round of 192)' val='125' totalSlots='24' pris='0' />";
				xmlstring+="<place lib='169th (round of 192)' val='63' totalSlots='24' pris='0' />";
				//xmlstring+="<place lib='no show' val='0' totalSlots='16' pris='0' />";
				break;
			
			//xmlstring+="<place lib='' val='' totalSlots='' pris='0' />";
		}
		
		
		xmlstring+="</contest>";
	}
	xmlstring+="</contests>";
	
	
	//alert(xmlstring);
	if (window.ActiveXObject) {
		contestsSlots = new ActiveXObject("Microsoft.XMLDOM");
		contestsSlots.loadXML(xmlstring);
		
	} else {
		contestsSlots = (new DOMParser()).parseFromString(xmlstring, "text/xml");
	}
	
}

function editResult(nsurfer,ncontest) {
	var tempHTML="";
	
	var tempPos=Position.cumulativeOffset($('simul_'+nsurfer+'_'+ncontest));
	
	var tempResults=surfers.firstChild.childNodes[nsurfer].getAttribute('resultssimul').split(';');
	var tempName=nameOfSurfer(nsurfer);
	
	if (tempResults[ncontest]!="0") {
		tempHTML+="Result of "+tempName+" for "+contests[ncontest]+" :<br/><br/>";
		tempHTML+="<span class='bigText'>"+tempResults[ncontest]+"</span>";
		tempHTML+="<br/><br/>";
		tempHTML+="<a onmouseover='ieLinkOver(this)' onmouseout='ieLinkOut(this)' onClick='deleteResult("+nsurfer+","+ncontest+")'>change</a> - <a onmouseover='ieLinkOver(this)' onmouseout='ieLinkOut(this)' onClick='fadeResult()'>cancel</a>";
	} else {
	
		tempHTML+="Enter the result of "+tempName+" for "+contests[ncontest]+" :<br/><br/>";
	
		tempHTML+=drawSelectContest(ncontest)+"<br/><br/>";
		
		tempHTML+="<a onmouseover='ieLinkOver(this)' onmouseout='ieLinkOut(this)' onClick='validateResult("+nsurfer+","+ncontest+")'>ok</a> - <a onmouseover='ieLinkOver(this)' onmouseout='ieLinkOut(this)' onClick='fadeResult()'>cancel</a>";
	
	}
	
	$('inputScore').innerHTML=tempHTML;
	new Effect.Appear('inputScore',{duration:0.3});
	new Effect.Move ('inputScore',{ x: tempPos[0]+25, y:tempPos[1]-100 , mode: 'absolute',duration:0.3});

}
function deleteResult(nsurfer,ncontest) {
	var tempResultsSimul=surfers.firstChild.childNodes[nsurfer].getAttribute('resultssimul').split(';');
	tempResultsSimul[ncontest]=0;
	
	var totalSimul=0;
	var tempResults=surfers.firstChild.childNodes[nsurfer].getAttribute('results').split(';');
	
	var combine=tempResults.concat(tempResultsSimul);
	combine=combine.sort(byNumber);
	combine=combine.reverse();
	
	var nbResults=combine.length;
	if (nbResults>7) {
		combine=combine.slice(0,7);
	}
	var totalSimul=0;
	
	for (var i=0;i<7;i++) {
		if (combine[i]) {
			totalSimul+=parseInt(combine[i]);
		}
	}	
	
	combine=combine.join(";");
	tempResultsSimul=tempResultsSimul.join(";");
	
	
	surfers.firstChild.childNodes[nsurfer].setAttribute('best7simul',combine);
	surfers.firstChild.childNodes[nsurfer].setAttribute('totalsimul',totalSimul);
	surfers.firstChild.childNodes[nsurfer].setAttribute('resultssimul',tempResultsSimul);
	
	var tempSlots=surfers.firstChild.childNodes[nsurfer].getAttribute('tempslots').split(';');
	var tempVal=tempSlots[ncontest];
	tempSlots[ncontest]="";
	tempSlots=tempSlots.join(";");
	surfers.firstChild.childNodes[nsurfer].setAttribute('tempslots',tempSlots);
	var tempPris=parseInt(contestsSlots.firstChild.childNodes[ncontest].childNodes[tempVal].getAttribute('pris'));
	tempPris--;
	contestsSlots.firstChild.childNodes[ncontest].childNodes[tempVal].setAttribute('pris',tempPris);
	
	
	
	$('simul_'+nsurfer+'_'+ncontest).innerHTML="0";
	updateSimulation();
	editResult(nsurfer,ncontest);
}

function fadeResult() {
	new Effect.Fade('inputScore',{duration:0.3});
}

function validateResult(nsurfer,ncontest) {
	//alert('validateResult'+ncontest+" "+$('forecastResult').value);
	
	var tempVal=$('forecastResult').value;
	var tempSlots=surfers.firstChild.childNodes[nsurfer].getAttribute('tempslots').split(';');
	tempSlots[ncontest]=tempVal;
	tempSlots=tempSlots.join(";");
	//alert(tempSlots);
	var tempPris=parseInt(contestsSlots.firstChild.childNodes[ncontest].childNodes[tempVal].getAttribute('pris'));
	tempPris++;
	$('simul_'+nsurfer+'_'+ncontest).innerHTML=contestsSlots.firstChild.childNodes[ncontest].childNodes[tempVal].getAttribute('val');
	
	contestsSlots.firstChild.childNodes[ncontest].childNodes[tempVal].setAttribute('pris',tempPris);
	
	var tempResultsSimul=surfers.firstChild.childNodes[nsurfer].getAttribute('resultssimul').split(';');
	tempResultsSimul[ncontest]=contestsSlots.firstChild.childNodes[ncontest].childNodes[tempVal].getAttribute('val');
	var tempResults=surfers.firstChild.childNodes[nsurfer].getAttribute('results').split(';');
	
	var combine=tempResults.concat(tempResultsSimul);
	combine=combine.sort(byNumber);
	combine=combine.reverse();
	
	var nbResults=combine.length;
	if (nbResults>7) {
		combine=combine.slice(0,7);
	}
	var totalSimul=0;
	
	for (var i=0;i<7;i++) {
		if (combine[i]) {
			totalSimul+=parseInt(combine[i]);
		}
	}	
	
	combine=combine.join(";");
	tempResultsSimul=tempResultsSimul.join(";");
	surfers.firstChild.childNodes[nsurfer].setAttribute('best7simul',combine);
	surfers.firstChild.childNodes[nsurfer].setAttribute('totalsimul',totalSimul);
	surfers.firstChild.childNodes[nsurfer].setAttribute('resultssimul',tempResultsSimul);
	surfers.firstChild.childNodes[nsurfer].setAttribute('tempslots',tempSlots);
	
	fadeResult();
	updateSimulation();
}

function updateSimulation() {
	var index=msort('totalsimul');
	var nbSurfers=surfers.firstChild.childNodes.length;
	
	var tempHTML="";
	var previousRating=1;
	//best7simul
	tempHTML+="<table cellspacing='0' cellpadding='2'>";
	for (var i=0;i<nbSurfers;i++) {
		var tempClass='';
		if (i>15) {
			tempClass=" class='warningText'";
		}
		
		tempHTML+="<tr"+tempClass+" title='best 7 results : "+surfers.firstChild.childNodes[index[i]].getAttribute('best7simul')+"'>";
		if (i>0) {
			if (surfers.firstChild.childNodes[index[i]].getAttribute('totalsimul')!=surfers.firstChild.childNodes[index[i-1]].getAttribute('totalsimul')) {
				previousRating=(i+1);
			}
		}
		var tempName=nameOfSurfer(index[i]);
		
		tempHTML+="<td width='25px'>"+previousRating+"</td>";
		tempHTML+="<td width='140px'>"+tempName+"</td>";
		tempHTML+="<td width='35px'>"+surfers.firstChild.childNodes[index[i]].getAttribute('totalsimul')+"</td>";
		tempHTML+="</tr>";
		
	}
	tempHTML+="</table>";
	
	$('tableauSimulation').innerHTML=tempHTML;
	
	//$('newWorldChamp').innerHTML=nameOfSurfer(index[0]);
}

function resetSimulation() {
	closeSimulation();
	var nbSurfers=surfers.firstChild.childNodes.length;
	var nbResults=surfers.firstChild.childNodes[0].getAttribute('results').split(";").length;
	//alert(nbContests);
	for (var i=0;i<nbSurfers;i++) {
		surfers.firstChild.childNodes[i].setAttribute('totalsimul',surfers.firstChild.childNodes[i].getAttribute('totalreel'));
		surfers.firstChild.childNodes[i].setAttribute('resultssimul',tempResSimul);
		surfers.firstChild.childNodes[i].setAttribute('tempslots','');
		surfers.firstChild.childNodes[i].setAttribute('best7simul',surfers.firstChild.childNodes[i].getAttribute('results'));

	}
	drawContestSimulation(currentSimul);
	initForecastXml(contestsLeft);
	updateSimulation();
}

function drawSelectContest(ncontest) {
	var tempHTML="";
	tempHTML+="<select id='forecastResult'>";
	
	var contestXml=contestsSlots.firstChild.childNodes[ncontest].childNodes;
		
	for (var i=0;i<contestXml.length;i++) {
		if (parseInt(contestXml[i].getAttribute('pris'))<parseInt(contestXml[i].getAttribute('totalSlots'))) {
			tempHTML+="<option value='"+i+"'>"+contestXml[i].getAttribute('lib')+" - "+contestXml[i].getAttribute('val')+" points</option>";
		}
	}
	
	tempHTML+="</select>";
	return tempHTML;
}

function showHelp() {
	new Effect.Appear('help',{duration:0.3});
}


function nameOfSurfer(nsurfer) {
	return 	surfers.firstChild.childNodes[nsurfer].getAttribute('prenom')+" "+surfers.firstChild.childNodes[nsurfer].getAttribute('nom');

}

function msort(mattribute) {
	var index = new Array();
	var temp = new Array();
	
	var surfersList=surfers.firstChild.childNodes;
	var nbSurfers=surfersList.length;
	
	for (var i=0;i<nbSurfers;i++) {
		
		if (index.length != 0) {
			
			var currentRes=parseInt(surfersList[i].getAttribute(mattribute));
			
			if ( currentRes<=parseInt(surfersList[findXMLindex(index[0])].getAttribute(mattribute)) ) {
				index.unshift(surfersList[i].getAttribute('n'));
			} else if ( currentRes>=parseInt(surfersList[findXMLindex(index[index.length-1])].getAttribute(mattribute)) ) {
				index.push(surfersList[i].getAttribute('n'));
			} else {

				for (var j=0;j<index.length;j++) {

					if (currentRes == parseInt(surfersList[findXMLindex(index[j])].getAttribute(mattribute))  || ( currentRes<parseInt(surfersList[findXMLindex(index[j])].getAttribute(mattribute)) && currentRes > parseInt(surfersList[findXMLindex(index[j-1])].getAttribute(mattribute)) ) ) {
						
						
						var debut = new Array;
						var fin = new Array;
						debut=index.slice(0,j);
						fin=index.slice(j);
						index = debut.concat(surfersList[i].getAttribute('n'));
						index = index.concat(fin);
						
						break;	
						
					}
				}
			}
		} else {
			index.push(0);
		}
	}
	index.reverse();
	return index;
}
function findXMLindex(nsurfer) {
	/*var nbSurfers=surfers.firstChild.childNodes.length;
	for (var i=0;i < nbSurfers; i++) {
		if (surfers.firstChild.childNodes[i].getAttribute('n') == nsurfer) {
			return i;
			break
		}
	}*/
	return nsurfer;
}
 
function byNumber(a,b) {
	return a - b;
}
function ieLinkOver(element) {
	if (isIe) {
		element.className="aHover";	
	}
}
function ieLinkOut(element) {
	if (isIe) {
		element.className="";	
	}
}