//GLOBAL VARIABLES			
	//SELECT TO LIST LINKS - (transform select box into ul list)
		var isMSIE = (navigator.userAgent.indexOf("MSIE")!=-1);
		var isMACIE = (isMSIE && navigator.userAgent.indexOf("Mac")!=-1);
		var isSAFARI = (navigator.userAgent.indexOf("Safari")!=-1);
	
		var objSelectToList;
		var objLastTd = null;
		var objSelectToListSpanPlus= null;
		
	//HOVERS AND COLOURS
		var strTImgPath = 'images/t_images/';
		var strButtonHoverBgColour = "#FF0008";
		var strButtonHoverColour = "#ffffff";
		var strPlusHover = "#2385AC";
		var strTdHover = "#2486AB";
		


		function addEvent(obj,strEventType,strFunction){//,objA,objB
		//idea from: http://ejohn.org/projects/flexible-javascript-events/
		//note: mac ie is temperamental - multiple calls to append element might ne unstable
			var arrParams = new Array();
			var intInc = 0;
			for(var i = 3; i < addEvent.arguments.length; ++i){
				//this won't work - strFunction  = strFunction.replace(/\)/,",'arguments["+i+"]')");
				//neither will this - strFunction  = strFunction.replace(/\)/,",arguments["+i+"])");										
				
				//but this will work. For some reason you can't send function.arguments to another function,
				//but if i set a variable to equal the argument then it does work
				arrParams[intInc] = addEvent.arguments[i];
				strFunction  = strFunction.replace(/\)/g,",arrParams["+intInc+"])");
				++intInc;
			}
			if(document.attachEvent){//msie
				strFunction  = strFunction.replace(/\'evt\'/,'window.event');
				obj.attachEvent("on"+strEventType,function(){eval(strFunction);},true);
			}else if(document.addEventListener){//mozilla
				strFunction  = strFunction.replace(/\'evt\'/,'evt');
				obj.addEventListener(strEventType,function(evt){eval(strFunction)},true);
			}else{
				if(isMACIE){//macie
					strFunction  = strFunction.replace(/\'evt\'/,'window.event');
				}else{//?
					strFunction  = strFunction.replace(/\'evt\'/,'evt');
				}
				eval('obj.on'+strEventType+' = ' + function(evt){eval(strFunction)});
			}
		}
		function hover(evt,isOn,obj){			
			obj.style.color = (isOn)?strPlusHover:"";
		}
		function selectThis(evt,strValue,objSelect,objTd){
			evt=(evt)?evt:window.event;
			if(objSelect){
				objSelect.value = strValue;
				
				//ACTIVATE THE SELECT BOX ONCHANGE EVENT
				var strOnChange = objSelect.getAttribute("onchange");	
				if(strOnChange){
					if(isMSIE){
						strOnChange = "" + strOnChange;
						strOnChange=strOnChange.substring(strOnChange.indexOf("{")+1,strOnChange.indexOf("}"));
						strOnChange=strOnChange.replace(/^[\s]*/,"");						
					}
					//strOnChange = strOnChange.replace(/this\.value/,"'"+objSelect.value+"'");
					//EITHER ABOVE OR BELOW
					strOnChange = strOnChange.replace(/this/,'objSelect');
					//alert("strOnChange=" + strOnChange)
					eval(strOnChange);
				}
				//HIGHLIGHT TD AND CHECKFORMS SWITCH BUTTON
				//selectItem(evt,objSelect,objTd)
				return false;
			}
		}
		function updateSpan(evt,strLiContent,objSelect,objTd){
			
			evt=(evt)?evt:window.event;
			//SET LI TAG TO UPDATE OPTION BOX WHEN CLICKED
			objSelectToListSpanPlus = null;
			var objSpans = objTd.getElementsByTagName("span");
			for(var i = 0; i < objSpans.length; ++ i){
				if(objSpans[i].className =="plus"){
					objSelectToListSpanPlus = objSpans[i];
					break;
				}
			}
			if(objSelectToListSpanPlus){
				if(isMSIE){
					strLiContent=(strLiContent!="")?strLiContent:"&nbsp;";
				}//RECALCULATE				
				objSelectToListSpanPlus.innerHTML = strLiContent;
			}
			
		}
		function goTo(strLink){
			location.href=strLink;
		}		
		function SelectToList(evt,obj,isShow){
			evt=(evt)?evt:window.event;			
		//SELECT TO LIST
			//note: mac ie is temperamental - multiple calls to append element might cause browser to crash
			if(isShow){
				if(SelectToList.arguments.length > 3){
					objTd = SelectToList.arguments[3];			
	
				//1) clear selectToListLinks from last td element before recreating
					if(objLastTd){
						var objTdDivs = objLastTd.getElementsByTagName("div");
						for(var i = 0; i < objTdDivs.length;++i){
							if(objTdDivs[i].className=="selectToListLinks"){
								//clear all appended elements from objSelectToList
								if(objSelectToList.getElementsByTagName("ul").length){
									//this seems to stop IE 5 from continuing to work after first click
									//objSelectToList.removeChild(objSelectToList.getElementsByTagName("ul")[0]);
									//objSelectToList.removeChild(objSelectToList.getElementsByTagName("div")[0]);
									
									//this works much faster
									objSelectToList.innerHTML = "";
								}
								
								//clear objSelectToList from last td
								objLastTd.removeChild(objTdDivs[i]);
								if(!isMSIE && !isSAFARI){
								//solve flicking problem for mozilla
									document.body = document.body;
								}
							}
						}
					}
				//2) copy td select box as list into objSelectToList
					var objUL = document.createElement('ul');		
					
					//get select box inside td
					var objSelect = objTd.getElementsByTagName('select')[0];
					var objOptions = objSelect.options;
					var strSelectId = objSelect.id;
					//extra data from select box options and build list from options
					
					//START FROM 1, dont include empty value
					var intStart = (objOptions[0].value=="")?1:0;
					for (var i=intStart; i<objOptions.length; i++) {						
						var strLiValue = objOptions[i].value;
						var strLiContent = objOptions[i].innerHTML;
						objLi = document.createElement('li');
						strText = "" + objOptions[i].text;
						objLi.innerHTML = strText;
						
						objLi.innerHTML = (strText=="")?'[unselect]':strText;
						addEvent(objLi,"click","selectThis('evt','"+strLiValue+"');updateSpan('evt','"+strLiContent+"')",objSelect,objTd);
						addEvent(objLi,"mouseover","hover('evt',1)",objLi);
						addEvent(objLi,"mouseout","hover('evt',0)",objLi);
						
						objUL.appendChild(objLi);
					}
					//copy ul into objSelectToList
					objSelectToList.appendChild(objUL);
	
					//create extra element for ie to clear float
					var objClear = document.createElement('div');
					objClear.setAttribute("class","endfloat");
					if(isMSIE){
						objClear.className = "endfloat";
					}
					objSelectToList.appendChild(objClear);
				
				//3) determine whether to display div above or below span
					if(isMSIE){
						var intScreenH = parseInt(document.body.clientHeight);//IF VALUE IS ZERO - set css * html body{height:100%}
						var intScreenW = parseInt(document.body.clientWidth);
					}else{
						var intScreenH = parseInt(window.innerHeight);
						var intScreenW = parseInt(window.innerWidth);
					}
					//object line height 13px plus
					var intLines = objOptions.length;//dynamic - dependent on how many in select box
					var intLineHeight = 13;
					var intObjHeight = (intLines * intLineHeight);
	
					//SAFARI treats clientY as pageY (pageY is the Y position relative to the top chrome including the height of the scroll)
					var intPosH = parseInt(evt.clientY);
					intPosH = (isSAFARI)?intPosH  - document.body.scrollTop:intPosH;
					
					var intSpace = 30;
					isDisplayAbove = ((intObjHeight + intPosH + intSpace) >= intScreenH)?1:0;
					
				//4) append div to td element adjusting margins appropriate to display above or below						
					/*
					if(isDisplayAbove){
						intObjHeight = (isMSIE)?intObjHeight + 14:intObjHeight - 8;
						if(isMSIE){
							objSelectToList.style.marginTop = ("-" + intObjHeight + "px");
						}else{
							objSelectToList.style.marginTop = ("-" + intObjHeight + "px");
						}
						if(!isMSIE && !isSAFARI){
							//solve flicking problem for mozilla after changing the dom
							document.body = document.body;
						}
						objTd.appendChild(objSelectToList);
					}else{
						if(isMSIE){
							objSelectToList.style.marginTop = "0px";
						}else{
//objSelectToList.style.marginTop = "19px";
						}
						if(!isMSIE && !isSAFARI){
							//solve flicking problem for mozilla after changing the dom
							document.body = document.body;
						}
						
						objTd.appendChild(objSelectToList);
					}
					*/
					objTd.appendChild(objSelectToList);
					
				}				
				
				//5) make objSelectToList visible
				objSelectToList.style.visibility = 'visible';
				
				//create variable to equal the last td element that was created
				//if this function is called again, the last objSelectToList must be removed before recreating
				objLastTd = objTd;
	
			}else if(!isShow){
				//cant remove element because it causes div to dissappear in firefox when rolling from span to div, even if adjacent to or on top of.
				objSelectToList.style.visibility = 'hidden';
			}
		}
		
							
		function onloadFunctions(){			
			//SELECT TO LIST
			if(!isMACIE){//too much appending causes mac ie to crash
			
				//2) create div element - objSelectToList
				//note: this needs to have been already declared as a global variable for SelectToList
				objSelectToList = document.createElement('div');
				objSelectToList.setAttribute("class","selectToListLinks");
				if(isMSIE){
					objSelectToList.className = "selectToListLinks";
				}
				
				//3) attach events to div element - objSelectToList
				addEvent(objSelectToList,"mouseover","SelectToList('evt',objSelectToList,1)");
				addEvent(objSelectToList,"mouseout","SelectToList('evt',objSelectToList,0)");
				addEvent(objSelectToList,"click","SelectToList('evt',objSelectToList,0)");
				
				//4) loop all divs
				var objAllDivs = document.getElementsByTagName("div");
				for (var i = 0; i < objAllDivs.length; ++i){
					if(objAllDivs[i].className == 'selectToList'){
						//5) add mouseover all all divs with class = selectToList in order to display - objSelectToList
						addEvent(objAllDivs[i],"click","SelectToList('evt',objSelectToList,1)",objAllDivs[i].parentNode);
						addEvent(objAllDivs[i],"mouseover","hover('evt',1)",objAllDivs[i]);
						addEvent(objAllDivs[i],"mouseout","hover('evt',0)",objAllDivs[i]);
					}
				}
			}			
		}
		onload = onloadFunctions;		
