/*
----------------------------------------------------------
	This file is included in all pages
	FileName:    generic.js
	Version:     1.00				09/01/2004
----------------------------------------------------------
*/

/* Browser detection */
var NS = (document.layers) ? 1 : 0;
var IE = (document.all) ? 1 : 0;
var NEW = (document.getElementById) ? 1 : 0;
var IE4 = (IE && !NEW) ? 1 : 0;
var IE5 = (IE && NEW) ? 1 : 0;
var MAC = (navigator.appVersion.indexOf("Mac") != -1) ? 1 : 0;
var Opera = (navigator.userAgent.indexOf("Opera") != -1) ? 1 : 0;
var Firefox = (navigator.userAgent.indexOf("Firefox") != -1) ? 1 : 0;
var NS6 = (NEW && !IE && !Opera & !Firefox) ? 1 : 0;
var NS60 = ((NS6) && (navigator.userAgent.indexOf("6\/6\.0") != -1)) ? 1 : 0;

/* Element identification. Makes allowances for earlier browser that do not support getElementById */
function getElementById(id, windowName) {
	if (IE4 || IE5) {
		return windowName ? windowName.document.all[id] : document.all[id];
	} else {
		return windowName ? windowName.document.getElementById(id) : document.getElementById(id);
	}
}

// opens a popup window based on either default or supplied arguments
var myWin, focusWin;
function openNewWindow(url, winWidth, winHeight) {
var winWidth = winWidth ? winWidth : 650;
var winHeight = winHeight ? winHeight : 570;
var parms = 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, height=' + winHeight + ',width=' + winWidth + ', left=0, top=0';
        focusWin = window.open(url, 'myWin', parms);
    if (focusWin) {
        focusWin.focus();
    }
}

/* Gets the current date and writes it to wherever the scripts is called from */
function getCurrentDate() {
	var mydate=new Date();
	var hours=mydate.getHours();
	var yr=mydate.getFullYear();
	var dy=mydate.getDay();
	var month=mydate.getMonth();
	var daym=mydate.getDate();
	if (daym<10) {
		daym="0"+daym;
	}
	var dayarray=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
	var montharray=new Array("January","February","March","April","May","June","July","August","September","October","November","December")

	document.write(dayarray[dy]+ ' ' + daym + ' ' + montharray[month] + ' ' + yr);
}

/* Shows a message in the window status bar */
function showStatus(Msg) {
	if (Msg) {
		window.status = Msg;
	} else {
		window.status = '';
	}
}

/* Hides or displays all selects on the page in order that they do not interfere with drop down menus. Changes the style. */
function hideSelects(menuItem) {
	if (getElementById(menuItem).style.visibility == 'visible') {
		getElementById('mainContent').className = "selectHide";
	} else {
		getElementById('mainContent').className = "selectVisible";
	}
}

/* Emulates the value of 'f' if the checkbox is not checked (in this case no value is sent through and we can't change 't' to 'f' */
function simulateUnchecked(fld, key) {
	if (fld.checked) {
		getElementById(key).disabled = true;
	} else {
		getElementById(key).disabled = false;
	}
}

function down(e) {
	var hideMenus = true;
	for (var i in menus) {
		if (getElementById(menus[i]).style.visibility == 'visible' && menus[i] != 'menu4') {	
			hideMenus = false;
		}
	}

	if (getClientX(e) > 300 && hideMenus) {
		if (clientArraySet == true) {
			for (var i in clientArray) {
				if (clientArray[i] != '') {	
					getElementById(clientArray[i]).style.visibility = 'hidden';
				}
			}
		}
		for (var i in menus) {
			if (getElementById(menus[i])) {
				getElementById(menus[i]).style.visibility = 'hidden';
			}
		}
	}

	/* This mirrors behaviour displayed within the calendar function that moves the calendar if the mouse 
	is down outside of either the calendar or the calendar image area. 
	The document can only respond to one mousedown event so all desired actions needs placing here */
	var calendarExists = false;
	for (i = 0; i < document.images.length; i++) {
		if (document.images[i].className == 'PopcalTrigger') {
			calendarExists = true;
		}
	}

	if (calendarExists == true) { 
		var n=!e?window.event.srcElement:e.target;
		if (!(/PopcalTrigger/i.test(n.className)||/popcal/i.test(n.name))) {
			if (window['gToday:datetime:agenda.js:gfPop:plugins_12.js']) {
				window['gToday:datetime:agenda.js:gfPop:plugins_12.js'].fHideCal();
			} else if (window['gToday:normal:agenda.js']) {
				window['gToday:normal:agenda.js'].fHideCal();
			}
		}
	}
}

/* Two functions determine the position of the mouse pointer
in relation to the browser window dependent on browser type */
function getClientX(e) {
	if (NS6 || Firefox || Opera) {
		return e.clientX;
	} else {
		return window.event.clientX;
	}
}

function getClientY(e) {
	if (NS6 || Firefox || Opera) {
		return e.clientY;
	} else {
		return window.event.clientY;
	}
}

/* two functions to ge the X and Y scroll of the page as a whole */
function GetXOffset() {
	if (NS6 || Firefox || Opera) {
		return window.pageXOffset;
	} else {
		return document.body.scrollLeft;
	}
}

function GetYOffset() {
	if (NS6 || Firefox || Opera) {
		return window.pageYOffset;
	} else {
		return document.body.scrollTop;
	}
}

/* Initialise events onload */
function init(e) {
	if (clientArraySet == true) {
		if (NS6 || Firefox || Opera) {
			document.body.addEventListener('mousedown', down, false);
		} else {
			document.onmousedown = down;
		}
	}
	captureMousePosition(e);
	if (getElementById('graphMenu')) {
			getElementById('graphMenu').style.visibility = 'visible';
	}
}

// opens a popup window based on either default or supplied arguments
var focusWin;
function openGenericWindow(url, winWidth, winHeight) {
var winWidth = winWidth ? winWidth : 650;
var winHeight = winHeight ? winHeight : 570;
var parms = 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, height=' + winHeight + ',width=' + winWidth + ', left=0, top=0';
	focusWin = window.open(url, 'myWin', parms);
    if (focusWin) {
        focusWin.focus();
    }
}

function ergoPopup(mypage,myname,w,h,pos,infocus){
if(pos=='random'){
LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;
TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
if(pos=='center'){
LeftPosition=(screen.width)?(screen.width-w)/2:100;
TopPosition=(screen.height)?(screen.height-h)/2:100;}
else if((pos!='center' && pos!='random') || pos==null){LeftPosition=100;TopPosition=100;}settings='width='+ w + ',height='+ h + ',top=' + TopPosition + ',left=' + LeftPosition + ',location=no,directories=no,menubar=yes,toolbar=no,status=yes,scrollbars=no,resizable=no,dependent=no';
lgflPopupWindow=window.open('',myname,settings);
if(infocus=='front'){lgflPopupWindow.focus();lgflPopupWindow.location=mypage;}
}

function modifyPhoneNumber() {
	var numObj = findObj('telephone');
	if(numObj) {
		var number = numObj.value;
		var startnum = number.substr(0,2);
		if(startnum == '07') {
			var newnum = '44' + number.substr(1);
		} else {
			var newnum = number;
		} 
		numObj.value = newnum;
	}
}
function showHideLayers() { //v6.0
  var i,p,v,obj,args=showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}
