function getInnerWinSize()
{
	return {
		x: window.innerWidth?window.innerWidth:
			(document.documentElement.clientWidth?document.documentElement.clientWidth:
				(document.body.clientWidth?document.body.clientWidth:0)),
		y: window.innerHeight?window.innerHeight:
			(document.documentElement.clientHeight?document.documentElement.clientHeight:
				(document.body.clientHeight?document.body.clientHeight:0))
	};
}

function getScrollOffset()
{
	return {
		x: document.body.scrollLeft?document.body.scrollLeft:
			(document.documentElement.scrollLeft?document.documentElement.scrollLeft:0),
		y: document.body.scrollTop?document.body.scrollTop:
			(document.documentElement.scrollTop?document.documentElement.scrollTop:0)
	};
}

function getMousePos(event)
{
	var Scroll = getScrollOffset();
	return {
		x: event.clientX?(event.clientX + Scroll.x):
			(e.pageX?e.pageX:0),
		y: event.clientY?(event.clientY + Scroll.y):
			(e.pageY?e.pageY:0)
	};
}

function drawWaitMsg(e)
{
	removeMsg();
	var msg = document.createElement('div');
	msg.className = 'label';
	msg.style.position = 'absolute';
	msg.id = 'msg';
	msg.innerHTML = 'Loading image<br>please wait...';
	var Mpos = getMousePos(e);
	msg.style.left = Mpos.x + 'px';
	msg.style.top = Mpos.y + 'px';
	document.body.appendChild(msg);
}

function remEl(id)
{
	if((EL = document.getElementById(id))) document.body.removeChild(EL);
}

function removeMsg()
{
	remEl('msg');
}

function removeImg()
{
	remEl('fullimg');
	remEl('bgbox');
	return false;
}

function drawImg()
{
	var imgH = document.createElement('div');
	imgH.id = 'fullimg';

	var WinDims = getInnerWinSize();

	var margin = 40;
	var padded_winy = WinDims.y - margin;
	var padded_winx = WinDims.x - margin;

	if(this.height > padded_winy)
	{
		var ratio = this.height / padded_winy;
		this.height = padded_winy;
		this.width /= ratio;
	}
	if(this.width > padded_winx)
	{
		var ratio = this.width / padded_winx;
		this.width = padded_winx;
		this.height /= ratio;
	}
	
	var yoffset = (padded_winy - this.height) / 2;
	var xoffset = (padded_winx - this.width) / 2;
	var Scroll = getScrollOffset();
	imgH.style.top  = ((yoffset > 0)?yoffset:0) + Scroll.y + 'px';
	imgH.style.left = ((xoffset > 0)?xoffset:0) + Scroll.x + 'px';

	imgH.appendChild(this);

	var label = document.createElement('span');
	label.innerHTML = this.name + '<br><a href="#" onclick="return removeImg();">Close</a>';
	label.className = 'label imglabel';
	imgH.appendChild(label);
	
	var dl = document.createElement('a');
	dl.target = '_blank';
	dl.href = this.src;
	dl.innerHTML = 'Download';
	dl.className = 'label download';
	if(dl.attachEvent) dl.attachEvent('onclick', removeImg);
	else dl.addEventListener('click', removeImg, false);
	imgH.appendChild(dl);

	var bgbox = document.createElement('div');
	bgbox.id = 'bgbox';
	bgbox.style.top = Scroll.y + 'px';
	document.body.appendChild(bgbox);

	document.body.appendChild(imgH);

	removeMsg();
}

function ImgFail()
{
	alert("Oops! The image failed to load.\n\nIf this error still appears after reloading the page using Ctrl + F5 it may be missing from the server");
	removeMsg();
	removeImg();
}

function FullPic(Pname, Psrc, e)
{
	removeImg();
	drawWaitMsg(e);

	var img = new Image();
	img.alt = Pname;
	img.name = Pname;
	img.title = 'Click to close';

	if(img.attachEvent){
		img.attachEvent('onclick', removeImg);
		//img.attachEvent('onload', drawImg);
		img.onload = drawImg;
		img.attachEvent('onerror', ImgFail);
	}
	else{
		img.addEventListener('click', removeImg, false);
		img.addEventListener('load', drawImg, false);
		img.addEventListener('error', ImgFail, false);
	}
	img.src = Psrc;
	return false;
}

function BSENinfo(values, e)
{
	removeMsg();
	var content = '';
	var img = '';
	var fields = new Array();
	var qs = values;

	values = values.split('');
	switch(values.length)
	{
		case 3: //511 cold
			content += '<h4><u>Cold Test Data (BS EN511)</u></h4>';
			img = 'cold';
			fields = new Array('Convective Cold','Contact Cold','Water Impermeability');
			break;
		case 4: //388 mech
			content += '<h4><u>Mechanical Data (BS EN388)</u></h4>';
			img = 'mech';
			fields = new Array('Abrasion','Cut','Tear','Puncture');
			break;
		case 6: //407 heat
			content += '<h4><u>Thermal Data (BS EN407)</u></h4>';
			img = 'heat';
			fields = new Array('Flammability','Contact Heat','Convective Heat','Radiant Heat','Small Metal Splash','Large Metal Splash');
			break;
		default: content += 'Malformed Data!';
	}
	content += '<table><tr><td><img alt="mechanical" src="images/graphics/'+img+'.png?'+qs+'"></td><td><table class="entable alncntr" cellspacing="0">';
	for(i=0;i<fields.length;i++)
	{
		content += '<tr><th>'+fields[i]+'</th><td>'+values[i]+'</td></tr>';
	}
	content += '</table></td></tr></table>X - Not Tested';

	var popup = document.createElement('div');
	popup.className = 'label';
	popup.style.position = 'absolute';
	popup.id = 'msg';
	popup.innerHTML = content+'<hr><a onclick="removeMsg();">close</a>';
	var Mpos = getMousePos(e);
	popup.style.left = (Mpos.x - 20) + 'px';
	popup.style.top = (Mpos.y - 50) + 'px';
	if(popup.attachEvent) popup.attachEvent('onclick', removeMsg);
	else popup.addEventListener('click', removeMsg, false);
	document.body.appendChild(popup);
}

function ClosePrompt(id)
{
	var spyqs = '?cl';
	var spy = new Image();
	spy.style.display = 'none';
	
	if(document.getElementById('remember_'+id) && document.getElementById('remember_'+id).checked)
	{
		var cookieName = 'remember';
		spyqs += '+rem';
		//var expires = 'expires=Fri, 17 Dec 2010 10:00:00 GMT;'
		document.cookie = cookieName+'='+id+';';//+expires;//path=/cgi-bin/';
		if(document.cookie.indexOf(id) == -1)
		{
			spyqs += '+fail';
			alert("Cookies are used to remember this setting and you appear to have them disabled");
		}
	}
	spy.src = '/images/graphics/blank.gif'+spyqs;
	document.body.appendChild(spy);
	remEl(id);
}

function PromptIdiots()
{
	var this_id = 'warn';
	if(document.cookie) if(document.cookie.indexOf(this_id) != -1) return;
	var content = '';
	var query = '';
	var warn_num = 1;

	var hreq = (window.XMLHttpRequest)?(new XMLHttpRequest()):(new ActiveXObject('Microsoft.XMLHTTP'));
	hreq.open('GET', '/cgi-bin/hdr.pl?http_user_agent', false);
	hreq.send(null);
	var user_agent = hreq.responseText;
	if(!user_agent) var user_agent = navigator.userAgent;

	var iever = user_agent.match(/msie\s*([67])\./i);
	if(iever && !user_agent.match(/Trident\/\d/i))
	{
		content += '<b>'+warn_num+'</b>) You appear to be using Internet Explorer '+iever[1]+' which is not a standards complient browser and may display this page incorrectly.<br><br>If you must use internet explorer it&#39;s a good idea to update to <a href="http://www.microsoft.com/windows/internet-explorer/default.aspx" target="_blank">version 8</a>, otherwise you&#39;ll be better off switching to something like <a href="http://www.mozilla-europe.org/en/firefox/" target="_blank">Firefox</a>, <a href="http://www.opera.com/download/" target="_blank">Opera</a>, <a href="http://www.google.com/chrome" target="_blank">Google Chrome</a> or <a href="http://www.srware.net/en/software_srware_iron_download.php" target="_blank">SRWare Iron</a> (a version of google chrome that doesn\'t track your usage).<br><br>These browsers are all much more secure than internet explorer.';
		query += 'IE'+iever[1]+'.';
		++warn_num;
	}
	if(user_agent.match(/SIMBAR/i))
	{
		if(content != '') content += '<hr>';
		content += '<b>'+warn_num+'</b>) Your computer is infected with adware called &quot;<a href="http://www.antispyware.com/glossary_details.php?ID=134230" target="_blank">SIMBAR</a>&quot;.';
		query += 'SBR.';
		++warn_num;
	}
	if(user_agent.match(/funwebproducts/i))
	{
		if(content != '') content += '<hr>';
		content += '<b>'+warn_num+'</b>) Your computer is infected with spyware/adware called <a href="http://en.wikipedia.org/wiki/MyWay_Searchbar" target="_blank">FunWebProducts</a>.<br>It spies on your browsing and will bombard you with adverts. You should remove it along with any other toolbars, screensavers, or &lsquo;smilie&rsquo; programs that you may or may not have knowingly installed.';
		query += 'FWP.';
		++warn_num;
	}
	if(user_agent.match(/Zango/i))
	{
		if(content != '') content += '<hr>';
		content += '<b>'+warn_num+'</b>) Your computer is infected with spyware called <a href="http://www.ca.com/us/securityadvisor/pest/pest.aspx?id=453096812" target="_blank">Zango Toolbar</a>.<br>It hijacks your searches, redirects the results and collects information about you.';
		query += 'ZTB.';
		++warn_num;
	}
	if(user_agent.match(/Seekmo/i))
	{
		if(content != '') content += '<hr>';
		content += '<b>'+warn_num+'</b>) Your computer is infected with adware called <a href="http://www.spywareguide.com/product_show.php?id=2368" target="_blank">Seekmo</a>.<br>It keeps track on your browsing activities and displays popup adverts.';
		query += 'STB.';
		++warn_num;
	}
	if(user_agent.match(/gamingharbor/i))
	{
		if(content != '') content += '<hr>';
		content += '<b>'+warn_num+'</b>) Your computer is infected with adware called <a href="http://www.google.com/search?q=gamingharbor" target="_blank">gamingharbor</a>.<br>It displays popus adverts on your computer and will try to install other nasty programs such as &quot;desktopsmiley&quot;';
		query += 'GHB.';
		++warn_num;
	}
	if(user_agent.match(/desktopsmiley/i))
	{
		if(content != '') content += '<hr>';
		content += '<b>'+warn_num+'</b>) Your computer is infected with adware called <a href="http://www.google.com/search?q=desktopsmiley" target="_blank">desktopsmiley</a>.<br>It displays popus adverts, hijacks your web browser, and will try to install other viruses on your computer';
		query += 'DTS.';
		++warn_num;
	}
	if(user_agent.match(/AntivirXP/i))
	{
		if(content != '') content += '<hr>';
		content += '<b>'+warn_num+'</b>) Your computer is infected with a virus called <a href="http://www.google.com/search?q=AntivirXP08" target="_blank">AntivirXP</a>.<br>It displays false warning messages about infections on your computer to con you into buying the AntivirXP "full version"';
		query += 'AVX.';
		++warn_num;
	}
	if(user_agent.match(/HotBar/i))
	{
		if(content != '') content += '<hr>';
		content += '<b>'+warn_num+'</b>) Your computer is infected with adware called <a href="http://www.google.com/search?q=HotBar" target="_blank">HotBar</a>. It spies on your browsing and displays annoying pop-up adverts. <a href="http://www.spamlaws.com/how-hotbar-works.html" target="_blank">More Info</a>';
		query += 'HTB.';
		++warn_num;
	}
	if(user_agent.match(/hotvideobar/i))
	{
		if(content != '') content += '<hr>';
		content += '<b>'+warn_num+'</b>) Your computer is infected with adware called <a href="http://www.google.com/search?q=hotvideobar+malware" target="_blank">hotvideobar</a>. It will open popup windows which display &#34;inappropriate content&#34;. This malware may have been installed without you knowing, or you may have installed something which included it like gametazar toolbar. See <a href="http://support.mozilla.com/en-US/forum/1/519581" target="_blank">here</a> and <a href="http://support.mozilla.com/nl/forum/1/587245" target="_blank">here</a> for more info.</a>';
		query += 'HVB.';
		++warn_num;
	}
	if(user_agent.match(/SpamBlockerUtility/i))
	{
		if(content != '') content += '<hr>';
		content += '<b>'+warn_num+'</b>) Your computer is infected with adware called <a href="http://www.google.com/search?q=SpamBlockerUtility+HotBar" target="_blank">SpamBlockerUtility</a> which is an adware application from HotBar. It spies on your browsing and displays annoying pop-up adverts. <a href="http://www.spamlaws.com/how-hotbar-works.html" target="_blank">More Info</a>';
		query += 'SBU.';
		++warn_num;
	}
	if(content != '')
	{
		var warn = document.createElement('div');
		warn.id = this_id;
		warn.style.position = 'absolute';
		warn.style.top = '0px';
		warn.style.left = '0px';
		warn.style.width = '450px';
		warn.style.backgroundColor = '#FFFFFF';
		warn.style.border = '3px outset #44BB44';
		warn.style.fontSize = '11pt';
		warn.style.textAlign = 'center';
		warn.innerHTML = '<div style="text-align:left;margin:3px;">'+content+'</div><hr><label for="remember_'+this_id+'"><input type="checkbox" id="remember_'+this_id+'">Don\'t tell me again</label><br><a href="javascript:ClosePrompt(\''+this_id+'\');">Close</a><img src="images/graphics/blank.gif?'+query+'" style="display:none;">';
		document.body.appendChild(warn);
	}
}

function logUserInfo()
{
	var innersz = getInnerWinSize();
	var logimg = document.createElement('img');
	logimg.style.display = 'none';
	logimg.src = '/images/graphics/blank.gif?f:'+screen.width+'x'+screen.height+',a:'+screen.availWidth+'x'+screen.availHeight+',i:'+innersz.x+'x'+innersz.y;
	document.body.appendChild(logimg);
}
