// Dutch Weekly code: dd/mm/yyyy with 0 - #.000,00 - e
$(document).ready(function(){ 
	$.get("http://www.pokerstars.nl/data/leader-board/weekly.xml",{},function(xml){
	HTMLOutput = '';	
	$('date',xml).each(function(i) {
		var fromMonth = $(this).find('from_month').text();
		  if (fromMonth < 10) {
			 fromLocalMonth = '0' + fromMonth;
		  } else {
			 fromLocalMonth = fromMonth;
		  }
		var toMonth = $(this).find('to_month').text();
		  if (toMonth < 10) {
			 toLocalMonth = '0' + toMonth;
		  } else {
			 toLocalMonth = toMonth;
		  }
		var fromDay = $(this).find('from_day').text();
		  if (fromDay < 10) {
			 fromLocalDay = '0' + fromDay;
		  } else {
			 fromLocalDay = fromDay;
		  }
		var toDay = $(this).find('to_day').text();
		  if (toDay < 10) {
			 toLocalDay = '0' + toDay;
		  } else {
			 toLocalDay = toDay;
		  }  
		HTMLOutput += '<h3>Weekklassement: ';
		HTMLOutput += fromLocalDay + '/';
		HTMLOutput += fromLocalMonth + '/';
		HTMLOutput += $(this).find("from_year").text() + ' tot en met ';
		HTMLOutput += toLocalDay + '/';
		HTMLOutput += toLocalMonth + '/';
		HTMLOutput += $(this).find("to_year").text() + '.</h3>';		

	 	HTMLOutput += '<table class="table" width="60%"><tr><th>Plaats</th><th>Gebruikersnaam</th><th>Land</th><th>Punten</th></tr>';
		
		$('ranking',xml).each(function(i) {
			place = $(this).find("place").text();
			name = $(this).find("userID").text();
			country = $(this).find("country").text();
			
			// Thousands with Dots and decimals with comma - NL
			var iniPoints = $(this).find("points").text();
			var makingLocal_Points = iniPoints.replace(/\,/g, ".");
			var points = makingLocal_Points.replace(/((\d+)\.(\d{3}))\.(\d{2})/, "$1,$4"); // $1 thousands only,$2 1st decimal only, $3 the three hundreds, $4 two decimals
			
			mydata = buildTop20Table(place,name,country,points);
			HTMLOutput = HTMLOutput + mydata;
		});
		HTMLOutput += '</table>';
		HTMLOutput += '<p><span class="strong">Deze stand is inclusief alle toernooien die begonnen zijn na ';
		HTMLOutput += toLocalDay + '/';
		HTMLOutput += toLocalMonth + '/';
		HTMLOutput += $(this).find("to_year").text() + ' 23:59 ET.</span></p>';
		
		$("#writeWeekly").append(HTMLOutput);
	});
});
	
});
 
function buildTop20Table(place,name,country,points){
	// Place with e
	output = '';
	output += '<tr>';
	output += '<td>'+ place + 'e</td>';
	output += '<td>'+ name +'</td>';
	output += '<td style="text-align:center;">'+ country +'</td>';
	output += '<td>'+ points +'</td>';
	output += '</tr>';
	return output;
}
function buildHTML(theName,theWin,theLoss,theRate,theStreak,theClass){
	if (theClass == "yes") {
		theClassHTML = " class='last'";
	} 
	else
	{
		theClassHTML = "";
	}
	
	output = '';
	output += '<tr' + theClassHTML + '>';
	output += '<td>'+ theName + '</td>';
	output += '<td>'+ theWin +'</td>';
	output += '<td>'+ theLoss +'</td>';
	output += '<td>'+ theRate +'</td>';
	output += '<td>'+ theStreak +'</td>';
	output += '</tr>';
	return output;
}

