// ajax functions for the top 16 stats per country
var req;
var reqtrend;
var trendboxheight 	= 150;
var trendboxwidth 	= 300;
function top16handletop16(){
	top16cleartable();
}

function top16cleartable(){
	var lFactorySign	= $('#factorysign').val();
	var lOnlyBeer		= $('#onlybeer').val();
	var lInsidePicture	= $('#insidepicture').val();
	var lCountry 		= $('#country').val();
	var lYear 			= $('#statsyear').val();
	var url;

	// clear the array but save the first row
	var icounter;
	var irows	= 20;
	icounter	= 0;
	var sname;

	if ($('#top20nodata')){
		$('#top20nodata').attr('innerHTML', '');
	}
	for(icounter=1; icounter<irows+1;icounter++){
		sname = "#top20a" + icounter;
		if ($(sname)){
			$(sname).attr('innerHTML','');
			$(sname).css('backgroundColor','');
		}
		sname = "#top20b" + icounter;
		if ($(sname)){
			$(sname).css('backgroundColor','');
		}
		sname = "#top20ref" + icounter;
		if ($(sname)){
			$(sname).attr('innerHTML','');
			$(sname).attr('href','');
		}
		sname = "#top20bar" + icounter;
		if ($(sname)){
			$(sname).css('width','0px');
		}

		sname = "#top20c" + icounter;
		if ($(sname)){
			$(sname).css('backgroundColor','');
		}
		
		sname = "#top20d" + icounter;
		if ($(sname)){
			$(sname).attr('innerHTML','');
			$(sname).css('backgroundColor','');
		}
	}


	if (lCountry > 0) {
		url = 'cci_stats_xml.php';
		url = url + '?country=' + lCountry;
		url = url + '&onlybeer=' + lOnlyBeer;
		url = url + '&insidepicture=' + lInsidePicture;
		url = url + '&factorysign=' + lFactorySign;
		url = url + '&year=' + lYear;
		Top16ExecAjax(url);
	}
}

function top16settable(){

	// clear the array but save the first row
	var icounter;
	var sbackcolor;

	// ok, let's evaluate the xml
	var oNodes;
	var userid;
	var fullname;
	var totalcrowns;
	var maxcrowns = 1;
	var maxwidth = 250;
	var sname;
	var lheight = 0;

	if (req.readyState == 4) { // Complete
		if (req.status == 200) { // OK response
			xml =  req.responseXML;
			if(xml!=null) {
				oNodes = xml.getElementsByTagName("entity");
				sText = "";
				if(oNodes==null){
					alert('Houston we have a problem');
				} else {
					if (oNodes.length>0) {
						icounter = 0;
						for (icounter=0; icounter<oNodes.length; icounter++ )
						   {
							fullname 	= oNodes[icounter].getAttribute('name');
							totalcrowns	= oNodes[icounter].getAttribute('total');
							userid		= oNodes[icounter].getAttribute('userid');
							if (icounter%2 > 0){
								sbackcolor = "ffffff";
							} else {
								sbackcolor = "efefef";
							}
							if (icounter == 0){
								maxcrowns = totalcrowns;
							}

							sname = "#top20a" + (icounter + 1);
							if ($(sname)){
								$(sname).attr('innerHTML', icounter + 1);
								$(sname).css('backgroundColor', sbackcolor);
							}
							sname = "#top20b" + (icounter + 1);
							if ($(sname)){
								$(sname).css('backgroundColor',sbackcolor);
							}

							sname = "#top20ref" + (icounter + 1);
							if ($(sname)){
								$(sname).attr('innerHTML',fullname);
								$(sname).attr('href','cci_members_maintain.php?ID=' + userid);
							}
							sname = "#top20c" + (icounter + 1);
							if ($(sname)){
								$(sname).css('backgroundColor',sbackcolor);
							}

							sname = "#top20d" + (icounter + 1);
							if ($(sname)){
								$(sname).attr('innerHTML', totalcrowns);
								$(sname).css('backgroundColor',sbackcolor);
							}
							sname = "#top20bar" + (icounter + 1);
							if ($(sname)){
								$(sname).css('width', Math.round((totalcrowns/maxcrowns)*maxwidth) + "px");
							}
						}
					} else {
						sname = "#top20nodata";
						$(sname).attr('innerHTML', labels[2]);
					}
				}
			}
		}
	}
}

function Top16ExecAjax(url){
	if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
		req.onreadystatechange = top16settable;
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = top16settable;
			req.open("GET", url, true);
            req.send();
        }
    }
}

function showTrend(user,country){
	var url				= '/phplib/cci_stats_per_country_trend.php?';
	var yposition		= 0;
	var xposition		= 0;
	var windowheight	= 0;
	var windowidth		= 0;
	trendboxheight 		= 150;
	trendboxwidth		= 300;

	if (country == 0){
		url = '/phplib/cci_stats_per_year_trend.php?';
	}

	if (country == -1){
		url = '/phplib/cci_stats_per_continent_trend.php?';
		trendboxheight 	= 180;
		trendboxwidth	= 640;
	}
	if (navigator.appName.indexOf("Microsoft")!=-1) {
		windowheight = document.body.offsetHeight;
		windowidth = document.body.offsetWidth;
	} else {
		windowheight = window.innerHeight;
		windowidth = window.innerWidth;
	}

	if (oMouseY + (trendboxheight + 30) > windowheight){
		yposition = oMouseY - (trendboxheight + 50);
	} else {
		yposition = oMouseY + 20;
	}

	if (trendboxwidth > 620) {
		xposition = (windowidth - trendboxwidth)/2;
	} else {
		xposition = oMouseX - (trendboxwidth + 20);
	}

	url += 'user='+user;
	url += '&';
	url += 'country='+country;
	Tip(labels[3], WIDTH, trendboxwidth, HEIGHT, trendboxheight, STICKY, true,  FADEOUT, 0,FADEIN, 0, CENTERWINDOW, true, TITLE, labels[2], CLICKCLOSE, true, SHADOW, true);
	//Tip(labels[3], LEFT, true, DELAY, 0, WIDTH, trendboxwidth, STICKY, true,  HEIGHT, trendboxheight, FADEIN, 0, TITLE, labels[2], SHADOW, true, LEFT, true,CENTERWINDOW, true );
	TrendExecAjax(url);
}

function TrendExecAjax(url){
	if (window.XMLHttpRequest) {
		reqtrend = new XMLHttpRequest();
		reqtrend.onreadystatechange = ShowTrend;
		reqtrend.open("GET", url, true);
		reqtrend.send(null);
	// branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
		reqtrend = new ActiveXObject("Microsoft.XMLHTTP");
		if (reqtrend) {
			reqtrend.onreadystatechange = ShowTrend;
			reqtrend.open("GET", url, true);
			reqtrend.send();
		}
	}
}

function ShowTrend(){
	var yposition = 0;
	var xposition = 0;
	var windowheight = 0;
	var windowidth = 0;

	if (navigator.appName.indexOf("Microsoft")!=-1) {
		windowheight = document.body.offsetHeight;
		windowidth = document.body.offsetWidth;
	} else {
		windowheight = window.innerHeight;
		windowidth = window.innerWidth;
	}

	if (oMouseY + (trendboxheight + 30) > windowheight){
		yposition = oMouseY - (trendboxheight + 50);
	} else {
		yposition = oMouseY + 20;
	}

	if (trendboxwidth > 620) {
		xposition = (windowidth - trendboxwidth)/2;
	} else {
		xposition = oMouseX - (trendboxwidth + 20);
	}

	if (reqtrend.readyState == 4) { // Complete
		if (reqtrend.status == 200) { // OK response
			xml =  reqtrend.responseText;
			if(xml!=null) {
				xml = xml.replace(/^\s+|\s+$/g, '') ;
//				UnTip();

				if (xml != ''){
					Tip(xml, WIDTH, trendboxwidth, HEIGHT, trendboxheight, FADEIN, 0, STICKY, true, CENTERWINDOW, true, CENTERALWAYS, true, TITLE, labels[2], CLICKCLOSE, true, SHADOW, true);
				} else {
					Tip(labels[4], WIDTH, trendboxwidth, HEIGHT, trendboxheight, FADEIN, 0, STICKY, true, CENTERWINDOW, true, CENTERALWAYS, true, TITLE, labels[2], CLICKCLOSE, true, SHADOW, true);
				}
			}
		}
	}
}