﻿//Version 1.0
var xmlFileName='', debug=false;
var vL='en', vDiv;
//Long Months
var nFullMonths={'en':new Array('January','February','March','April','May','June','July','August','September','October','November','December')}
//Short Months
var nMonths={'en':new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec')}
//Number Formats
var nFormats={'en':new Array(',','.',0, '$'),'cz':new Array('.',',',0, '$'),'de':new Array('.',',',0, '$'),'da':new Array('.',',',0, '$'),'es':new Array('.',',',1, ' $'),'fi':new Array(' ',',',1, ' $'),'fr':new Array(' ',',',1, ' $'),'hu':new Array(',','.',0, '$'),'it':new Array('.',',',0, '$'),'nl':new Array('.',',',0, '$ '),'no':new Array(' ',',',0, '$'),'pl':new Array('.',',',0, '$'),'pt':new Array('.',',',0, '$'),'br':new Array('.',',',0, '$'),'ro':new Array('.',',',0, '$'),'se':new Array(' ',',',0, '$'),'sl':new Array('.',',',0, '$'),'ru':new Array(' ',',',0, '$'),'ua':new Array(' ','.',1, '$'),'bg':new Array(',','.',0, '$')}
//strips out all non numerics (used before number formating)
function stripNonNumeric(str){;str += '';var rgx = /^\d|\.|-$/;var out = '';for( var i = 0; i < str.length; i++ ){;if( rgx.test( str.charAt(i) ) ){;if( !( ( str.charAt(i) == '.' && out.indexOf( '.' ) != -1 ) || ( str.charAt(i) == '-' && out.length != 0 ) ) ){;out += str.charAt(i);};};};return out;};
//formats a number for tha current vL, cBool is a boolean to say if it is a currency value
function fNumber(num, cBool){;var myNum = stripNonNumeric(num);if (isNaN(myNum)){;return num;};myNum+='';x = myNum.split('.');x1 = x[0];x2 = x.length > 1 ? nFormats[vL][1] + x[1] : '';var rgx = /(\d+)(\d{3})/;while (rgx.test(x1)){;x1 = x1.replace(rgx, '$1' + nFormats[vL][0] + '$2');};if(cBool){;return (nFormats[vL][2]==0?nFormats[vL][3]+x1+x2:x1+x2+nFormats[vL][3]);} else {;return x1 + x2;};};
//ajax call for the xml file
var xmlObj;
function loadXML(url){;
xmlObj=null;if (window.XMLHttpRequest){;
xmlObj=new XMLHttpRequest();} else if (window.ActiveXObject){;xmlObj=new ActiveXObject("Microsoft.XMLHTTP");};if (xmlObj!=null){xmlObj.onreadystatechange=read_XML;try {xmlObj.open("GET",url,true); xmlObj.send(null);} catch(err){if(debug){alert(err);};};} else {;if(debug){alert("Error occured creating XMLHttpRequest object")};};};
//readystatechange function
function read_XML(){;if (xmlObj.readyState==4) {
	if (xmlObj.status==200|xmlObj.status==0){;eval(fileLoaded());
} else {;if(debug){"fileLoaded function not found"};};};};
//listItem - to do
/*function aListItem(){;
	this.listItemText=null;
	this.listItemFields=null;
	this.listItemFieldFormat=null;
	this.cssClass='';
	this.divID='';
	this.listItemProperties;
	};*/
//Summary Object
function xmlSummary(){;
this.Headers=null;
this.Fields=null;
this.FieldFormat=null;
this.divID='';
this.tableProperties='';
this.cssClass='';};
//Table Object	
function xmlTable(){;this.Headers=null;this.Fields=null;this.FieldFormat='';this.rowName='';this.rowFilter='';this.rowFilterField='';this.altRowClass='';this.cssClass='';this.headerText='';this.headerTextFields=null;this.tableProperties='';this.divID='';this.rowFilterEmpty=''};
//Had to move this outside the function [checkStyleSheet()] for unknown reason...
var isFound=false;
//Checks for the newbox stylesheet and adds it to the page if it is needed.
function checkStyleSheet(){;var pageLinks=document.getElementsByTagName('link');for(var i=0;i<pageLinks.length;i++){;if(pageLinks[i].href.indexOf('newBox.css')>0){;isFound=true};};};if(!isFound){var nCSS=document.createElement('link');nCSS.setAttribute("href", "http://www.pokerstars.com/styles/newBox.css");nCSS.setAttribute("rel","stylesheet");nCSS.setAttribute("type","text/css");document.getElementsByTagName("head")[0].appendChild(nCSS);};
//formats the data from the xml updated attribute into a date object
function formatDate(tD, fS){;if(tD!=null){myDate=new Date(tD.substring(0,4),tD.substring(5,7),tD.substring(8,10),tD.substring(11,13),tD.substring(14,16));};if(myDate instanceof Date) {var yyyy = myDate.getFullYear();
var yy = yyyy.toString().substring(2);
var m = myDate.getMonth();
var mm = m < 10 ? "0" + m : m;
var mmm = nMonths[vL][m-1];
var mmmm = nFullMonths[vL][m-1];
var d = myDate.getDate();
var dd = d < 10 ? "0" + d : d;
var h = myDate.getHours();
var hh = h < 10 ? "0" + h : h;
var n = myDate.getMinutes();
var nn = n < 10 ? "0" + n : n;
var s = myDate.getSeconds();
var ss = s < 10 ? "0" + s : s;fS = fS.replace(/yyyy/, yyyy);fS = fS.replace(/yy/, yy);fS = fS.replace(/mmmm/, mmmm);fS = fS.replace(/mmm/, mmm);fS = fS.replace(/mm/, mm);fS = fS.replace(/m/, m);fS = fS.replace(/dd/, dd);fS = fS.replace(/d/, d);fS = fS.replace(/hh/, hh);fS = fS.replace(/h/, h);fS = fS.replace(/nn/, nn);fS = fS.replace(/n/, n);fS = fS.replace(/ss/, ss);fS = fS.replace(/s/, s);return fS;} else {;return "";};};
function formatTime(tD){;myDate=new Date(tD.substring(0,4),tD.substring(5,7),tD.substring(8,10),tD.substring(11,13),tD.substring(14,16));
var c_mins=myDate.getMinutes();c_mins+='';if(c_mins.length==1){c_mins+='0';}
var c_hours=myDate.getHours();c_hours+='';if(c_hours.length==1){c_hours='0'+c_hours;}
return c_hours+':'+c_mins;};
//Gets the value from the positon requests even if it is an arrtibute
function getThisValue(fieldName, position){;
try {
if(fieldName.indexOf('@')>0){;
return xmlObj.responseXML.getElementsByTagName(fieldName.substr(0,fieldName.indexOf('@')))[position].getAttribute(fieldName.substr(fieldName.indexOf('@')+1));} else {;
return xmlObj.responseXML.getElementsByTagName(fieldName)[position].firstChild.nodeValue;};
} catch(err) {;if(debug){alert(err);return;};};
};
//creates header text to go above the table not a commonly used feature
function makeHeader(tblObj){;if (tblObj.headerTextFields!=null){;var headerTextHTML=tblObj.headerText;for(i=0;i<tblObj.headerTextFields.length;i++){;headerTextHTML=headerTextHTML.replace('¬'+i+'¬',getThisValue(tblObj.headerTextFields[i], 0));};return headerTextHTML;};return '';};
//create table
function makeTable(tblObj){
	if(debug){if(tblObj.rowName.length==0){alert("No rowName has been specified!");return}}
	checkStyleSheet();
//create table
var theTable=makeHeader(tblObj)+'<table '+ tblObj.tableProperties;if (tblObj.cssClass!=''){;theTable+=' class="'+tblObj.cssClass+'"';};theTable+='>'
//create headerTexts
if(tblObj.Headers!=null){
theTable+='<tr>';for (i=0; i<tblObj.Headers.length;i++){;theTable+='<th>'+tblObj.Headers[i]+'</th>';};theTable+='</tr>';};
//create rows
var myRows=xmlObj.responseXML.getElementsByTagName(tblObj.rowName);
var rowsPrinted=0;
for (var x=0;x<myRows.length;x++){;
	var altClass='';
	var f=tblObj.rowFilter.length,ff=tblObj.rowFilterField.length;
	var printRow=true;
	if (f>0&&ff>0){
		printRow=false;
		if(getThisValue(tblObj.Fields[tblObj.rowFilterField], x).search(new RegExp(tblObj.rowFilter,"ig"))!=-1){printRow=true;};
	};
	if(printRow){;
	if(x%2==0){if(tblObj.altRowClass.length>0){altClass=' class="'+tblObj.altRowClass+'"'};};
	theTable+='<tr'+altClass+'>';
	for(var j=0;j<tblObj.Fields.length;j++){;rowsPrinted++;
		theTable+='<td>'+formatThis(getThisValue(tblObj.Fields[j], x), tblObj.FieldFormat[j])+'</td>';};
	theTable+='</tr>';
	};};
//close table
theTable+='</table>';
if(!rowsPrinted>0){theTable+='<p>'+tblObj.rowFilterEmpty+'</p>';}
//place in div
document.getElementById(tblObj.divID).innerHTML=theTable;
}

function makeSummary(sumObj){
	var theTable='<table '+sumObj.tableProperties; if (sumObj.cssClass!=''){;theTable+=' class="'+sumObj.cssClass+'"'}theTable+='>';
	for (i=0;i<sumObj.Headers.length;i++){;
		theTable+='<tr><th>'+sumObj.Headers[i]+'</th><td>'+formatThis(getThisValue(sumObj.Fields[i], 0), sumObj.FieldFormat[i])+'</td></tr>';
		};
	theTable+='</table>';
	document.getElementById(sumObj.divID).innerHTML=theTable;
	}
//cellformatting function	
function formatThis(a,b){
	var formatted=a;
	try{
	var c=b.split("|");
	if(c[0]=='text'){;formatted=formatted;};
	if(c[0]=='datetime'){;if(c[1]==null){;c[1]='dd/mm/yyyy';}formatted=formatDate(a, c[1]);};
	if(c[0]=='number'){formatted=fNumber(a);};
	if(c[0]=='currency'){formatted=fNumber(a, true);};
	return formatted;
	} catch(err) {return formatted;}
	return formatted;
	};
