// gather browser information and send to the counter php page
// by calling gather_stats(false) in the body of the page
// (boolean value should be true if debugging)

// pathPrefix provides a way of adjusting for placing in the
// folder system Ñ it should adjust to mjewing.co.uk/php/

function gather_stats(bDisplay, pathPrefix) {
	var ci_screenwidth = screen.width;		
	var ci_referrer_url = "" + document.referrer;
	// note: these must be called after the BODY tag
	var ci_windowWidth = window.innerWidth;
	if (isNaN(ci_windowWidth)) ci_windowWidth = document.body.clientWidth; // for IE
	var ci_windowHeight = window.innerHeight;
	// note, in non-IE browsers, this next method of getting the height
	// gets the height of the whole, scrollable document, not just the window:
	if (isNaN(ci_windowHeight)) ci_windowHeight = document.body.clientHeight; // for IE

	if (bDisplay) {
		display_stat("Screen width", ci_screenwidth);
		display_stat("Referrer", ci_referrer_url);
		display_stat("Window width", ci_windowWidth);
		display_stat("Window height", ci_windowHeight);
	}
	
	// prepare details for URL transmission
	var ci_detailsStr = pathPrefix + "browser_stats.php?";
	ci_detailsStr = add_url_data(ci_detailsStr, "scWid", ci_screenwidth);
	ci_detailsStr = add_url_data(ci_detailsStr, "&ref", ci_referrer_url);
	ci_detailsStr = add_url_data(ci_detailsStr, "&wnWidth", ci_windowWidth);
	ci_detailsStr = add_url_data(ci_detailsStr, "&wnHeight", ci_windowHeight);
	
	// log or display the details:
	if (bDisplay) {
		ci_detailsStr = add_url_data(ci_detailsStr, "&disp", "y");
		document.writeln("<p><a href='" + ci_detailsStr + "'>Click to log values</a></p>");
	} else {
		log_details(ci_detailsStr);
	}
}

function display_stat(statName, statValue) {
	// display a stat:
	document.writeln("<b>" + statName + ":</b> " + statValue + "<br>");
}

function log_details(encDetailsStr) {
	// send the details to the server
	php_call = new Image();
	php_call.src = encDetailsStr;
}

function add_url_data(urlStr, dataName, dataValue) {
	// adds a data value to the provided URL:
	retStr = urlStr + dataName + "=" + escape(dataValue);
	return retStr;
}

