var version=2409021751;
var pop_up;
var modulesLoaded=new Array;
modulesLoaded[0]='common.js v'+version;

//var debugmode=debugState();
var debuglog='';
debugPoint('module loaded','common.js v'+version);

function fSetSelector(sValue,oSelector)
{	for (var i=0;i<oSelector.options.length;i++)
	{	if (sValue==oSelector.options[i].value)
		{	oSelector.selectedIndex=i;
			return;
}	}	}

/* Calculate the actual top of the object wrt the object heirachy of visible
   elements.  Inefficient to calc both independently when we require both so 
   a future version may return an array or object */ 
function fCalcTop(oObject)
{	var oPE=oObject;
	var nTop=0;
	/* Iterate through the heirachy */
	while (oPE && (oPE.tagName!='BODY'))
	{	nTop+=oPE.offsetTop;
		oPE=oPE.offsetParent;	//NOTE: offsetParent not parentElement
	}
	return nTop;
}

/* as fCalcTop but calculate left */
function fCalcLeft(oObject)
{	var oPE=oObject;
	var nLeft=0;
	while (oPE && (oPE.tagName!='BODY'))
	{	nLeft+=oPE.offsetLeft;
		oPE=oPE.offsetParent;
	}
	return nLeft;
}

function jamFrame(thisFrameset,thisFrame)
{	var docLoc=location.href;
	if (top !=self)
	{	debugPoint('checkFrame',location.href+' loaded in frameset');
		return;
	}
	if (thisFrameset.indexOf('?')<0) thisFrameset+='?';
	else thisFrameset+='&';
	
	dbg=/-(a|l)/;
	//if (debugmode.match(dbg)) thisFrameset+='debug'+debugmode+'&';

	thisFrameset+='targetURL='+docLoc+'&targetFrame='+thisFrame;
	debugPoint('checkFrame','redirecting to '+thisFrameset);
	location=thisFrameset;
}

function setFrame(frameOverride)
{	var callingURL=location.href;
	var targetURL='';
	var targetFrame='';	

	debugPoint('setFrame',callingURL);

	if (callingURL.indexOf('?')<0) return;
	params=callingURL.substring(callingURL.indexOf('?')+1,callingURL.length);
	debugPoint('setFrame',callingURL+' received params '+params);
	
	valPairs=new Array();
	valPairs=params.split('&');
	for (var i=0;i<valPairs.length;i++)
	{	if ((valPairs[i].split('='))[0] == 'targetURL') targetURL=(valPairs[i].split('='))[1];
		if ((valPairs[i].split('='))[0] == 'targetFrame') targetFrame=(valPairs[i].split('='))[1];	
	}

	debugPoint('setFrame','targetURL='+targetURL+' targetFrame='+targetFrame);

	if (targetURL=='') return;
	otargetFrame=eval(targetFrame);
	otargetFrame.location=targetURL;	
}

function timeStr()
{	var Tnow=new Date();
	return Tnow.toLocaleString();
}

function debugPoint(funct,value)
{	logStr=timeStr()+' : '+funct+' : '+value+'\n';
	Dalert=/-a/;
	Dlog=/-l/;
	//if (debugmode.match(Dalert)) alert(logStr);
	//if (debugmode.match(Dlog)) debuglog+=logStr;
}

function debugState()
{	//initialise
	var key;
	var value;

	//find whether debug is called
	callingURL=top.location.href;
	if (callingURL.indexOf('?')<0) return '';

	//find the debug states
	params=callingURL.substring(callingURL.indexOf('?')+1,callingURL.length)
	valPairs=new Array;
	valPairs=params.split('&');
	re = /debug/i;
	for (var i=0;i<valPairs.length;i++)
	{	if (!(valPairs[i].match(re))) continue;
		valPairs[i]=valPairs[i].replace(re,'');
		return valPairs[i];
	}
	return '';	
}

function fNavVersion()															// return the current version of explorer running.  Return 0 is not MSIE
{	var sVersion=navigator.appVersion.split(';')[1];							// obtain the relevant string
	if (sVersion.search(/MSIE/i)<0) return 0;									// check is MSIE
	return parseFloat(sVersion.replace(/[^\d\.]/ig,''));						// extract the numbers and parse as a float
}

function isNS()
{	//debugPoint('isNs','checking NS compatibility');
	if (navigator.appName.indexOf('Netscape') !=-1) return true;
	return false;
}

function linkTo(page)
{	scrollbars='scrollbars=no';
	width='width=550';

	debugPoint('linkTo','about to pop up');

	if (isNS())
	{	scrollbars='scrollbars=auto';
		width='width=570';
	}
		
	if (pop_up && !pop_up.closed)
	{
		pop_up.close();
	}
	pop_up = window.open(page,'messageWindow','scrollbars=yes,status=yes,height=420,top=20,left=20,'+scrollbars+','+width);
	pop_up.focus();
}

function confirmClear(thisInput)
{	if (thisInput.value.length >0)
	{
		if (confirm('You have made changes\nPressing OK will discard these changes\n\nDo you wish to continue ?')) return true;
		else return false;
	}
	return true;
}

function loadOptions(thisScript,thisList)
{	//which state are we going for ??
	//seems that sometimes loaded and sometimes complete are exposed !!
	//loaded is set true by the returned cgi script
    	if (!((thisScript.readyState=='complete')||(thisScript.readyState=='loaded')||(loaded)) ) return;
	{	for (var i=0;i<filenames.length;i++)
		{	thisList.options[i]=new Option(filenames[i]);
			thisList.options[i].value = filenames[i];
}	}	}

function findPE(theSender,theTagName)
{	pe=theSender;
	while (pe &&(pe.tagName !=theTagName))
	{	pe=pe.parentElement;
		if (pe==top) return(null);
	}
	return (pe);
}

function register(thisModule)
{	newModule=modulesLoaded.length;
	modulesLoaded[newModule]=thisModule;
	debugPoint('module loaded',thisModule);
}

function fShowModules()
{	loadedList='Modules Loaded';
	for (var i=0;i<modulesLoaded.length;i++) loadedList+='\n'+modulesLoaded[i];
	alert(loadedList);
}

function fShowDebugLog()
{	if (debuglog.length>0) alert('Debug Log\n'+debuglog);
}

function fShow(these)
{	showThese=new Array;
	showThese=these.split(',');
	for(var i=0;i<showThese.length;i++) eval('fShow'+showThese[i]+'()');
}

//next two subs for colmnation of webpages

function fInsertColumnsTable()
{	var oNewTable=document.createElement('<table width="100%" border="0" cellpadding="0" cellspacing="0">');
	gtwpContentArea.appendChild(oNewTable);
	with (oNewTable.insertRow(-1))
	{	with (insertCell(-1))
		{	style.setAttribute('paddingRight','7px');
			style.setAttribute('textAlign','justify');
			setAttribute('width','50%');
			setAttribute('vAlign','top');
		}
		with (insertCell(-1))
		{	style.setAttribute('paddingLeft','7px');
			style.setAttribute('textAlign','justify');
			setAttribute('width','50%');
			setAttribute('vAlign','top');
	}	}
	return oNewTable;
}

function fColumnate()
{	var aParagraphs=new Array();
	for (var i=0; i<gtwpContentArea.all.tags('P').length; i++)
		aParagraphs[aParagraphs.length]=gtwpContentArea.all.tags('P')[i].innerHTML;
	if (aParagraphs.length<=1) return;		// leave things as they are in a single block
	gtwpContentArea.innerHTML='';
	var oColumnsTable=fInsertColumnsTable();

	var iParasLeft=0;
	var iParasRight=0;
	var iCachedOffsetHeight=0;
	var iCurrentCol=1;
	while (iParasLeft+iParasRight<aParagraphs.length)		// copy each of the paragraphs stored in the array
	{	iCurrentCol=iCurrentCol==0?1:0;
		iCachedOffsetHeight=oColumnsTable.offsetHeight;
		while (oColumnsTable.offsetHeight<=iCachedOffsetHeight) // the height of the table is stored.  Add a paragaph to the column,
																// if the height changes, move to the other column, otherwise the other column is longer so add more into this one
		{	if (iParasLeft+iParasRight==aParagraphs.length) break;
			var sParaText=iCurrentCol==0?aParagraphs[iParasLeft]:aParagraphs[aParagraphs.length-1-iParasRight];
			if (iCurrentCol==0)
			{	iParasLeft++;
				oColumnsTable.rows[0].cells[iCurrentCol].innerHTML+='<p>'+sParaText+'</p>';
			}
			else
			{	iParasRight++;
				with (oColumnsTable.rows[0].cells[iCurrentCol]) innerHTML='<p>'+sParaText+'</p>'+innerHTML;
			}
		}
	}

	if ((iCurrentCol==1)&&(iCachedOffsetHeight<oColumnsTable.offsetHeight))
	{	// it can only be the last paragraph which makes the right column longer than the left
		var oExtraPara=oColumnsTable.rows[0].cells[1].all.tags('P')[0];
		oColumnsTable.rows[0].cells[0].appendChild(oExtraPara.cloneNode(true));
		oExtraPara.removeNode(true);
	}
}

