// common functions for style sheets, resizing, etc

var U3Asizeg=0;
var u3Awidth=0;
var u3Aheight=0;
var U3Abrowser=-1;
var U3Aresize=1; /* -1-turned off,0-running,1-idle */
var U3Aresizec=0;

/*  setstyle - loads the appropriate style sheets depending on the screen dimensions
    the height is used to load a vert...css holding hold the font size settings and button 'box'
    the width is used to load a horz...css holding the rest of the styles with margins/padding etc
    finally inline styles are created to set the dimensions of the rest of the 'boxes' these are a
    combination of data from both width and height also the table is a third of the width of contents
    but ensure that the ratio vert... to horz.. is at least 4:3 to ensure that the header fits */
function setstyle()
{
// get the window size
	getWinHeight();
// estimate 2 extra on width and 166 on height for a 'full screen' size (not actually accurate)
	var h=U3Aheight+166;
	var w=U3Awidth+2;

/* variables for holding the widths of the boxes the formulae must be found by trial and error! */
	var menuw = new Number;
	var containw = new Number;
	var contentw = new Number;
	var headerw = new Number;
	var calcval = new Number;
	var workstr = new String;
/* get browser info to get fudge factors */
	if (U3Abrowser==-1) {
		switch (navigator.appName)
		{
		case "Microsoft Internet Explorer":
			workstr=navigator.appVersion;
			calcval=workstr.search("MSIE");
			if (calcval>-1)
			{
				workstr=workstr.substr(calcval+5,1);
			}
			if (Number(workstr)=="NaN")	
			{
				U3Abrowser=0;
			}
			else
			{
				if (Number(workstr)<8)
				{
					U3Abrowser=1; /* <=IE7 */
				}
			}
			break;
		default:
			U3Abrowser=0;
		}
	}
/* special testing */
/*	if (document.URL.search("Peter")>=0)
{
	if (parseInt(screen.height)==1024)
	{
		h=1200;
		w=1500;
	}
} */
	if (h<720 || w<650)
	{
		document.writeln("<link rel=\"stylesheet\" type=\"text/css\" href=\"css/vert600.css\"/>");
		menuw=96;
		headerw = Math.max(w-39,690);
	}
	else if (h<848 || w<1000)
	{
		w=U3Awidth;
		document.writeln("<link rel=\"stylesheet\" type=\"text/css\" href=\"css/vert768.css\"/>");
		menuw=120;
		headerw = Math.max(w-39,860);
	}
	else if (h<1032 || w<1300)
	{
		document.writeln("<link rel=\"stylesheet\" type=\"text/css\" href=\"css/vert1024.css\"/>");
		menuw=151;
		headerw = Math.max(w-44,1095);
	}
	else
	{
		document.writeln("<link rel=\"stylesheet\" type=\"text/css\" href=\"css/vert1200.css\"/>");
		menuw=192;
		headerw = Math.max(w-39,1400);
	}

	if (w<840)
	{
		document.writeln("<link rel=\"stylesheet\" type=\"text/css\" href=\"css/horz800.css\"/>");
		containw = w-35;
		contentw = w-menuw-44;
	}
	else if (w<1000)
	{
		document.writeln("<link rel=\"stylesheet\" type=\"text/css\" href=\"css/horz1024.css\"/>");
		containw = w-35;
		contentw = w-menuw-50;
	}
	else if (w<1300)
	{
		document.writeln("<link rel=\"stylesheet\" type=\"text/css\" href=\"css/horz1280.css\"/>");
		containw = w-35;
		contentw = w-menuw-50;
	}
	else
	{
		document.writeln("<link rel=\"stylesheet\" type=\"text/css\" href=\"css/horz1680.css\"/>");
		containw = w-35;
		contentw = w-menuw-50;
	}
	document.writeln("<style type=\"text/css\">");
	document.writeln("#container {width:"+containw.toString()+"px}");
	document.writeln("#header {width:"+headerw.toString()+"px}");
	document.writeln("#content {width:"+contentw.toString()+"px}");
/* set group table column width */
	calcval = Math.floor((contentw-15)/3);
	if (U3Abrowser==1)
	{
		calcval = calcval-8;
	}
	document.writeln("#grouptable col{width:"+calcval.toFixed(0)+"px}");

	document.writeln("#footer {width:"+headerw.toString()+"px}");
	document.writeln("</style>");
}

// function to get the inner size of the browser window (depends on browser used) values in global vars U3Awidth amd height
// get the inner window height depending on which browser is in use
function getWinHeight()
{
	switch (U3Asizeg)
	{
	case 0:
		if (typeof window.innerWidth!='undefined') { U3Asizeg = 1 }
		 else if (typeof document.documentElement!='undefined'
		     && typeof document.documentElement.clientWidth!=
		     'undefined' && document.documentElement.clientWidth!=0)
		{
			U3Asizeg=2;
			U3Awidth=document.documentElement.clientWidth;
			U3Aheight=document.documentElement.clientHeight;
			return 0
		}
		else
		{
			U3Asizeg=3;
			U3Awidth=getElementsByTagName('body')[0].clientWidth;
			U3Aheight=getElementsByTagName('body')[0].clientHeight;
			return 0
		}
	case 1:
		U3Awidth=window.innerWidth;
		U3Aheight=window.innerHeight;
		return 0
	case 2:
		U3Awidth=document.documentElement.clientWidth;
		U3Aheight=document.documentElement.clientHeight;
		return 0
	default:
		U3Awidth=getElementsByTagName('body')[0].clientWidth;
		U3Aheight=getElementsByTagName('body')[0].clientHeight;
		return 0
	}
}

/* putemail - inserts an anchor containing an email request parameters are:
  1 - email name
  2 - email address
  3 - text if this is "" the whole email address will be used
  4 - other parameters (eg class="xxx" may be "")  */

function putemail(name, address, text, other)
{
	document.write("<a href=\"mail");
	document.write("to:"+name+"@"+address+"\""+other+">");
	if (text.length==0)
	{
		document.write(name+"@"+address+"</a>")
	}
	else
	{
		document.write(text+"</a>")
	}
}

/* function to process a button. parameters:
 id - the id of the button
 type - type of process - 0 goto previous page, 1 - scroll to top of page, 2 'hover' (white text), 3 'unhover' (black text) */
function procbut(id,type)
{
	var h=document.getElementById("logo").height;
	switch(type)
	{
	case 0:
		history.back();
		break;
	case 1:
		window.scrollTo(0,h);
		break;
	case 2:
		document.getElementById(id).style.color="White";
		break;
	case 3: 
		document.getElementById(id).style.color="Black";
		break;
	default:
		alert("Invalid topbotton call "+type);
	}
}

// resize process start a timer if not already done if a non-null parameter call the special reload routine
function u3aresize(loadfunc)
{
// if a timer is already running or turned off
	if (U3Aresize<=0)
	{
		return 0
	}
// get the current window size
// a peculiarity of IE7 means that the width can be up to 20 larger than it should be during resize
	if (U3Abrowser==1)
	{
		var h=U3Aheight;
		var w=U3Awidth;
		getWinHeight();
		if (Math.abs(w-U3Awidth-10)<10 && h==U3Aheight)
		{
			U3Aheight=h;
			U3Awidth=w;
			return 0
		}
	}
	else
	{
		getWinHeight();
	}
// start the timer for .5 sec to call timer processor with the current window sizes
	U3Aresize=0;
	var t=setTimeout("u3atimed("+U3Awidth.toString()+","+U3Aheight.toString()+",\""+loadfunc+"\")",500)
}

// resize timeout, if the size has changed just start the timer again
function u3atimed(w,h,loadfunc)
{
// get the current window size
	getWinHeight();
// if the size is not changed reload the page
// a peculiarity of IE7 means that the width can be up to 20px larger than it should be during resize
	if (U3Abrowser==1)
	{
		if (w-20<=U3Awidth && w>U3Awidth) { U3Awidth=w }
	}
	if ((w==U3Awidth && h==U3Aheight) || U3Aresizec>10)
	{
		U3Aresize=1;
		U3Aresizec=0;
		if (loadfunc=="")
		{
			window.location.reload();
		}
		else
		{
			eval(loadfunc);	
		}
		return 0
	}
// restart the timer for .5 sec to call timer processor with the current window sizes
	U3Aresize=0;
	U3Aresizec=U3Aresizec+1;
	var t=setTimeout("u3atimed("+U3Awidth.toString()+","+U3Aheight.toString()+",\""+loadfunc+"\")",500)
}

// disable the resize processing for a while and then check size
function u3adisableresize(loadfunc)
{
	U3Aresize=-1;
	setTimeout("u3aenableresize(\""+loadfunc+"\")",1000);
}
// the "while" is up, reenable resize and check whether we need to resize  
function u3aenableresize(loadfunc)
{
	var w=U3Awidth;
	var h=U3Aheight;
	getWinHeight();
// a peculiarity of IE7 means that the width can be up to 20px larger than it should be during resize
	if (U3Abrowser==1)
	{
		if (w-20<=U3Awidth && w>U3Awidth) { U3Awidth=w }
	}
	U3Aresize=1;
	if (w!=U3Awidth || h!=U3Aheight) { u3aresize(loadfunc) }
	
}

//cookie processing routines
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
