var words = new Array;

function dodate()
	{
	var theDate = new Date();
	document.getElementById('theDate').innerText = theDate.toLocaleString();
	}

function courseDrop(ev)
	{
	var theLst    = document.getElementById('courseList');
	var theIframe = document.getElementById('iframe');

	if (theLst)
		{
		if (theLst.style.display == 'block')
			{
			theLst.style.display = 'none';
			theIframe.style.display = 'none';
			}
		else
			{
			theLst.style.display = 'block';
			theIframe.style.left   = 200;
			theIframe.style.top    = parseInt(theLst.offsetTop);
			theIframe.style.width  = parseInt(theLst.offsetWidth);
			theIframe.style.height = parseInt(theLst.offsetHeight);
//			theIframe.style.display = 'block';
			theIframe.style.display = 'none';
			}
		}

	if (document.all)
		{ev.cancelBubble = true;}
	else
		{ev.stopPropagation();}
	}

function hideCourseDrop()
	{
	var theLst    = document.getElementById('courseList');
	var theIframe = document.getElementById('iframe');

	if (theLst)
		{
		theLst.style.display = 'none';
		theIframe.style.display = 'none';
		}
	}

function openFull(file)
	{
	myWindow = window.open(file,'testWin','fullscreen=yes');
	myWindow.focus();
	}

function openFlash(id,w,h)
	{
	myWindow = window.open('','flash'+id,'resizable=yes,toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,copyhistory=0,width=' + (w+50) + ',height=' + (h+50) + ',top=0,left=0');
	myWindow.document.write('<html>\n');
	myWindow.document.write('	<head>\n');
	myWindow.document.write('		<title>SmartScreen: Flash</title>\n');
	myWindow.document.write(unescape('%3Cscript%3E'));
	myWindow.document.write('function resizeFlash()\n');
	myWindow.document.write('	{\n');
	myWindow.document.write('	var div = document.getElementsByTagName("object")[0];\n');
	myWindow.document.write('	div.style.width  = (parseInt(document.body.clientWidth )-20) + "px";\n');
	myWindow.document.write('	div.style.height = (parseInt(document.body.clientHeight)-20) + "px";\n');
	myWindow.document.write('\n');
	myWindow.document.write('	div = document.getElementsByTagName("embed")[0];\n');
	myWindow.document.write('	div.style.width  = (parseInt(document.body.clientWidth )-20) + "px";\n');
	myWindow.document.write('	div.style.height = (parseInt(document.body.clientHeight)-20) + "px";\n');
	myWindow.document.write('	}\n');
	myWindow.document.write('\n');
	myWindow.document.write(unescape('%3C/script%3E'));
	myWindow.document.write('	</head>\n');
	myWindow.document.write('	<body onresize="resizeFlash()">\n');
	myWindow.document.write(document.getElementById('flash_' + id + '_div').innerHTML);
	myWindow.document.write('	</body>\n');
	myWindow.document.write('</html>\n');
	myWindow.document.close();
	}

function resizeFlash()
	{
	var div = document.getElementsByTagName('object')[0];
	div.style.width  = (parseInt(document.body.clientWidth )-20) + 'px';
	div.style.height = (parseInt(document.body.clientHeight)-20) + 'px';

	div = document.getElementsByTagName('embed')[0];
	div.style.width  = (parseInt(document.body.clientWidth )-20) + 'px';
	div.style.height = (parseInt(document.body.clientHeight)-20) + 'px';
	}

function wordSort(a,b)
	{
	return (b.length - a.length);
	}

function glossary()
	{
	var Div;
	if (document.getElementById('columnContents'))
		{Div = document.getElementById('columnContents');}

	if ((Div) && (window.glossaryFile))
		{
		var wordIndex = new Array();
		for (var word in words)
			{wordIndex[wordIndex.length] = word;}

		wordIndex.sort(wordSort);

		var divHTML = Div.innerHTML;
		var tempArray = new Array;
		var count = 0;
		
		var re = new RegExp('(<a.+?>.+?</a>)','ig');
		var matches = divHTML.match(re);
		if (matches != null)
			{
			for (var m=0; m<matches.length; m++)
				{
				tempArray[++count] = matches[m];
				divHTML = divHTML.replace(matches[m],':_:' + count + ':_:');
				}
			}

		// Removed by Rich, check this!	
		//re = new RegExp('(</?(img|form|input|h2|h3|h4|h5|h6).+?>)','ig');
		re = new RegExp('(<(div|form|h2|h3|h4|h5|h6)[^>]*>[^:]+</(div|form|h2|h3|h4|h5|h6)>)|<(input|image|param|embed).*>','ig');
		var matches = divHTML.match(re);
		if (matches != null)
			{
			for (var m=0; m<matches.length; m++)
				{
				tempArray[++count] = matches[m];
				divHTML = divHTML.replace(matches[m],':_:' + count + ':_:');
				}
			}

		for (var w=0; w<wordIndex.length; w++)
			{
			word = wordIndex[w];
			var letter = word.substr(0,1);
			letter = letter.toLowerCase();
			re = new RegExp('\\b('+word+')\\b','ig');
			var matches = divHTML.match(re);

			if (matches != null)
				{
				for (var m=0; m<matches.length; m++)
					{
					tempArray[++count] = '<a class="glossary" href="' + glossaryFile + '?letter=' + letter + '#' + letter + '" onmouseover="showGlossary(event,\'' + word + '\')" onmouseout="hideGlossary()">' + matches[m] + '</a>';
					divHTML = divHTML.replace(matches[m],':_:' + count + ':_:');
					}
				}
			}
			
		for (var m=1; m<=count; m++)
			{divHTML = divHTML.replace(':_:' + m + ':_:',tempArray[m]);}
            
		Div.innerHTML = divHTML;
		}
	}

function findPosX(obj)
	{
	var curleft = 0;
	if (obj.offsetParent)
		{
		while (obj.offsetParent)
			{
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
			}
		}
	else if (obj.x)
		{curleft += obj.x;}
	return curleft;
	}

function findPosY(obj)
	{
	var curtop = 0;

	if (obj.offsetParent)
		{
		while (obj.offsetParent)
			{
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
			}
		}
	else if (obj.y)
		{curtop += obj.y;}
	return curtop;
	}


function showGlossary(e,w)
	{
	var theDiv = document.getElementById('glossaryDiv');

	theDiv.innerHTML = words[w];

	if (e.srcElement != undefined)
		{
		var obj = e.srcElement;

		theDiv.style.top  = parseInt(findPosY(document.getElementById('contentArea')) + obj.offsetTop  + obj.offsetHeight+10) + 'px';
		theDiv.style.left = parseInt(findPosX(obj)+20) + 'px';
		}
	else
		{
		var obj = e.target;

		theDiv.style.top  = parseInt(findPosY(obj)+obj.offsetHeight+10) + 'px';
		theDiv.style.left = parseInt(findPosX(obj)+20) + 'px';
		}

	var theLeft = parseInt(document.getElementById('wrapper').offsetLeft);

	if (parseInt(theDiv.style.left) - theLeft > 460)
		{theDiv.style.left = (theLeft+460) + 'px';}

	theDiv.style.display = 'block';
	}

function hideGlossary()
	{
	document.getElementById('glossaryDiv').style.display = 'none';
	}

function htSearch(theForm)
	{
	var searchMode = document.getElementById('searchOptions');

	if (searchMode.selectedIndex == 2)
		{
		theForm.action = 'http://smartsearch.pncl.co.uk/cgi-bin/htsearch';
		theForm.target = '_blank';
		document.getElementById('searchConfig').value = document.getElementById('searchConfig').value;
		}
	else
		{
		theForm.action = '/search.asp';
		theForm.target = '_self';
		}
	}

function showAdvancedSearch()
	{
	document.getElementById('basicSearch').style.display = 'none';
	document.getElementById('advancedSearch').style.display = 'block';
	}

function recordDownload(filename)
	{
	if (!page_id) {page_id = 'null';}
	if (!level3 ) {level4  = 'null';}
	if (!level3 ) {level4  = 'null';}
	if (!centre ) {centre  = 'null';}
	if (!course ) {course  = 'null';}
	if (!user   ) {user    = 'null';}

	filename = filename.substr(filename.indexOf('/uploaded_files'));
	// console.log(filename);

	var url = '/cgi-bin/download.asp?page_id=' + page_id + '&level3=' + level3 + '&level4= ' + level4 + '&centre= ' + centre + '&course= ' + course + '&user= ' + user + '&filename=' + escape(filename) + '&url=' + document.location.pathname;

	if (window.XMLHttpRequest)
		{
		// code for Mozilla, etc.
		jsHTTP=new XMLHttpRequest();
		jsHTTP.open("GET",url,true);
		jsHTTP.send(null);
		}
	else if (window.ActiveXObject)
		{
		// code for IE
		jsHTTP=new ActiveXObject("Microsoft.XMLHTTP");
		if (jsHTTP)
			{
			jsHTTP.open("GET",url,true);
			jsHTTP.send();
			}
		}
	}

if (document.all)
	{document.onclick=hideCourseDrop;}
else
	{document.addEventListener('click',hideCourseDrop,false);}

window.onload = function()
	{
	if ((region != 19) && (document.getElementById('columnContents')))
		{
		var links = document.getElementById('columnContents').getElementsByTagName('A');
		for (var i=0; i<links.length; i++)
			{
			if (links[i].href.indexOf('uploaded_files') > -1)
				{
				if (document.all)
					{
					links[i].onclick=function()
						{
						recordDownload(this.href);
						return true;
						};
					}
				else
					{
					document.addEventListener('click',function()
						{
						recordDownload(this.href);
						return true;
						}
					,false);
					}
				}
			}
		}
	}
