var oFrame;
var resultsHTML ="";
var tmr=0;
var d=new Date();
var forceOverCSS=0;
var nextTable="";
var lastTournament="";

// JSON cross-domain communications
// Constructor -- pass a REST request URL to the constructor

function JSONscriptRequest(fullUrl) {
    this.fullUrl = fullUrl; 
    this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
    this.headLoc = document.getElementsByTagName("head").item(0);
    this.scriptId = 'JscriptId' + JSONscriptRequest.scriptCounter++;
}

JSONscriptRequest.scriptCounter = 1;
JSONscriptRequest.prototype.buildScriptTag = function () {
    this.scriptObj = document.createElement("script");
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("charset", "utf-8");
    this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
    this.scriptObj.setAttribute("id", this.scriptId);
}
 
JSONscriptRequest.prototype.removeScriptTag = function () {
    this.headLoc.removeChild(this.scriptObj);  
}

JSONscriptRequest.prototype.addScriptTag = function () {
    this.headLoc.appendChild(this.scriptObj);
}


function drawFrame(s,o,k){
	// s is the ID
	// if you want to you can pass in "o" which is the name of the div to load data into
	if(!o){document.writeln('<div name="results_main" id="results_main"></div>');}
	fillFrame(s,o,k);
}

function fillFrame(s,o,k){
	// k can contain a string of ids and divs to load tables into 
	// (so you can draw a page full of tables if you like)
	// but k must have pairs of values - e.g. "tournament,div,tournament,div" etc
	if(k){nextTable=k;}
	if(!o){o="results_main";}
	oFrame=document.getElementById(o);
	if(oFrame!=null){
		loadLeagueTable(s);
	}
}

function loadLeagueTable(s){
	loadData('http://www.scoreresults.co.uk/widget_leaguetable_jsn.asp?tournamentid='+s); 
}

function drawProfile(s){
	document.writeln('<div name="results_main" id="results_main"></div>');
	oFrame=document.getElementById("results_main");
	loadPlayer(s);
}

// Define the callback function
function writeData(jsonData) {     
	var s=jsonData;
	bObj.removeScriptTag();

	// check to see if any/all of the classes have already been defined.
	// if not, use js replace to turn class='' into style=''
	var x=verifyStyle(".results_playerlink");
	//if(x!=true||forceOverCSS==1){s=s.replace(/class='results_playerlink'/gi, "style='color:black;text-decoration:none;'");}
	if(x!=true||forceOverCSS==1){s=s.replace(/class='results_playerlink'/gi, "style='color:black;font-weight:normal;'");}

	var x=verifyStyle(".results_historylink");
	//if(x!=true||forceOverCSS==1){s=s.replace(/class='results_historylink'/gi, "style='color:black;text-decoration:none;'");}
	if(x!=true||forceOverCSS==1){s=s.replace(/class='results_historylink'/gi, "style='color:black;font-weight:normal;'");}

	var x=verifyStyle(".results_line1");
	if(x!=true||forceOverCSS==1){s=s.replace(/class='results_line1'/gi, "style='background-color:#F0F0F0;'");}
	var x=verifyStyle(".results_line2");
	if(x!=true||forceOverCSS==1){s=s.replace(/class='results_line2'/gi, "style='background-color:#E9E9E9;'");}
	var x=verifyStyle(".results_table");
	if(x!=true||forceOverCSS==1){s=s.replace(/class='results_table'/gi, "style='border:1px solid black; background-color:#FFFFFF;width:600px;'");}
	var x=verifyStyle(".results_header");
	if(x!=true||forceOverCSS==1){s=s.replace(/class='results_header'/gi, "style='background-color:black;color:white;text-align:center;'");}
	var x=verifyStyle(".results_subheader");
	if(x!=true||forceOverCSS==1){s=s.replace(/class='results_subheader'/gi, "style='background-color:silver;color:black;'");}
	var x=verifyStyle(".results_played");
	if(x!=true||forceOverCSS==1){s=s.replace(/class='results_played'/gi, "style='text-align:right;width:30px;padding-right:8px;'");}
	var x=verifyStyle(".results_player");
	if(x!=true||forceOverCSS==1){s=s.replace(/class='results_player'/gi, "style='text-align:left;color:black;'");}

	var x=verifyStyle(".results_profile");
	if(x!=true||forceOverCSS==1){s=s.replace(/class='results_profile'/gi, "style='border:1px solid black; background-color:#FFFFFF;width:600px;'");}
	var x=verifyStyle(".results_profileheader");
	if(x!=true||forceOverCSS==1){s=s.replace(/class='results_profileheader'/gi, "style='background-color:black;color:white;text-align:center;'");}
	var x=verifyStyle(".results_profilesummary_table");
	if(x!=true||forceOverCSS==1){s=s.replace(/class='results_profilesummary_table'/gi, "style='border:1px solid black; background-color:#FFFFFF;width:350px;'");}
	var x=verifyStyle(".results_profilesummary_header");
	if(x!=true||forceOverCSS==1){s=s.replace(/class='results_profilesummary_header'/gi, "style='background-color:black;color:white;text-align:center;'");}
	var x=verifyStyle(".results_profilesummary_subheader");
	if(x!=true||forceOverCSS==1){s=s.replace(/class='results_profilesummary_subheader'/gi, "style='background-color:silver;color:black;text-align:right;'");}
	var x=verifyStyle(".results_profilesummary");
	if(x!=true||forceOverCSS==1){s=s.replace(/class='results_profilesummary'/gi, "style='text-align:right;'");}
	var x=verifyStyle(".results_profilesummary_value");
	if(x!=true||forceOverCSS==1){s=s.replace(/class='results_profilesummary_value'/gi, "style='background-color:#F0F0F0;text-align:right;'");}

	var x=verifyStyle(".results_history_table");
	if(x!=true||forceOverCSS==1){s=s.replace(/class='results_history_table'/gi, "style='border:1px solid black; background-color:#FFFFFF;width:600px;'");}
	var x=verifyStyle(".results_history_header");
	if(x!=true||forceOverCSS==1){s=s.replace(/class='results_history_header'/gi, "style='background-color:black;color:white;text-align:center;'");}

	d=new Date();
	var r=d.getTime();
	if(tmr==0){tmr=r;}
	r=r-tmr;
	while(r<500){
		d=new Date();
		r=d.getTime();
		r=r-tmr;
	}
	oFrame.innerHTML=s;

	if (nextTable.length>0){
		var i=nextTable.indexOf(",");
		var s=nextTable.substr(0,i);
		nextTable=nextTable.substr(i+1,nextTable.length);
		i=nextTable.indexOf(",");
		if(i<0){i=nextTable.length;}
		var t=nextTable.substr(0,i);
		if(s!="" && t!=""){
			nextTable=nextTable.substr(i+1,nextTable.length);
			fillFrame(s,t,nextTable);
		}else{
			nextTable="";
		}
	}
}

function showLoading(){
	var s='<table width="500"><tr><td width="0" height="200"><img src="images/blank.gif" height="200" width="0"></td><td align="center"><table><tr><td align="right"><img src="http://www.scoreresults.co.uk/images/loading.gif" height="32" width="32"></td><td>Loading. Please wait...</td></tr></table></td></tr></table>';
	oFrame.innerHTML=s;
}

function loadData(req){
	if(oFrame){showLoading();}
	d=new Date();
	tmr=d.getTime();
	// Create a new request object
	bObj = new JSONscriptRequest(req); 
	// Build the dynamic script tag
	bObj.buildScriptTag(); 
	// Add the script tag to the page
	bObj.addScriptTag();
}

function verifyStyle(selector) {
    var rules;
    var haveRule = false;

    if (typeof document.styleSheets != "undefined") {   //is this supported
        var cssSheets = document.styleSheets;

        outerloop:
        for (var i = 0; i < cssSheets.length; i++) {

             //using IE or FireFox/Standards Compliant
            rules =  (typeof cssSheets[i].cssRules != "undefined") ? cssSheets[i].cssRules : cssSheets[i].rules;

             for (var j = 0; j < rules.length; j++) {
                 if (rules[j].selectorText == selector) {
                         haveRule = true;
                        break outerloop;
				 }
            }//innerloop

        }//outer loop
	}
    return haveRule;
}


function loadPlayer(s,a){
	var o=a;
	while(o && o.tagName.toUpperCase()!="DIV"){
		o=o.parentNode;
	}
	if(o){oFrame=document.getElementById(o.id);}
	loadData('http://www.scoreresults.co.uk/widget_playerprofile_jsn.asp?id='+s); 
}

function loadTournament(s){
	loadData('http://www.scoreresults.co.uk/widget_leaguetable_jsn.asp?tournamentid='+s);
}

function showMessage(){
	var o=document.getElementById("messagetd");
	o.style.display="block";
}

function sendMessage(){
	var msg=document.getElementById("txtmsg").value;
	var o=document.getElementById("messagetd");
	o.innerHTML="<table width='100%'><tr><td align='center'>Sending message. Please wait.....</td></tr></table><br>";
	alert(msg);
	
}
