/**
 * DOM
 */
var isDOM1 = (document.getElementById) ? true : false;
var isDOM2 = (document.addEventListener && document.removeEventListener) ? true : false;

/**
 * UserAgent
 */
var theNavUA = navigator.userAgent.toLowerCase();
if (theNavUA.indexOf('windows') > -1 || theNavUA.indexOf('win') > -1) {
	var os = 'Microsoft Windows';
	if (theNavUA.indexOf('windows xp') > -1 || theNavUA.indexOf('win xp') > -1 || theNavUA.indexOf('nt 5.1') > -1) {
		var osVer = 'XP';
	} else if (theNavUA.indexOf('windows 2000') > -1 || theNavUA.indexOf('win 2000') > -1 || theNavUA.indexOf('nt 5.0') > -1) {
		var osVer = '2000';
	} else if (theNavUA.indexOf('windows me') > -1 || theNavUA.indexOf('win 9x 4.90') > -1) {
		var osVer = 'ME';
	} else if (theNavUA.indexOf('windows 98') > -1 || theNavUA.indexOf('win 98') > -1) {
		var osVer = '98';
	} else if (theNavUA.indexOf('windows 95') > -1 || theNavUA.indexOf('win 95') > -1) {
		var osVer = '95';
	} else if (theNavUA.indexOf('windows ce') > -1 || theNavUA.indexOf('win ce') > -1 || theNavUA.indexOf('windowsce') > -1 || theNavUA.indexOf('wince') > -1) {
		var osVer = 'CE';
	}
} else if (theNavUA.indexOf('linux') > -1) {
	var os = 'Linux';
} else if (theNavUA.indexOf('mac os x') > -1) {
	var os = 'Mac OS X';
} else if (theNavUA.indexOf('free bsd') > -1) {
	var os = 'Free BSD';
} else if (theNavUA.indexOf('sun os') > -1) {
	var os = 'Sun OS';
} else if (theNavUA.indexOf('irix') > -1) {
	var os = 'Irix';
} else if (theNavUA.indexOf('open bsd') > -1) {
	var os = 'Open BSD';
} else if (theNavUA.indexOf('macintosh') > -1 || theNavUA.indexOf('powerpc') > -1) {
	var os = 'Mac Classic';
} else if (theNavUA.indexOf('openvms') > -1) {
	var os = 'Open VMS';
} else if (theNavUA.indexOf('amigaos') > -1) {
	var os = 'Amiga';
} else if (theNavUA.indexOf('hurd') > -1) {
	var os = 'Hurd';
} else if (theNavUA.indexOf('hp-ux') > -1) {
	var os = 'HP-UX';
} else if (theNavUA.indexOf('unix') > -1 || theNavUA.indexOf('x11') > -1) {
	var os = 'UNIX';
} else if (theNavUA.indexOf('cygwin') > -1) {
	var os = 'Cygwin';
} else if (theNavUA.indexOf('java') > -1) {
	var os = 'Java';
} else if (theNavUA.indexOf('palmos') > -1) {
	var os = 'Palm OS';
} else if (theNavUA.indexOf('symbian') > -1) {
	var os = 'Symbian';
}

var isWin = (os == 'Microsoft Windows') ? true : false;
var isMac = (os == 'Mac OS X' || os == 'Mac Classic') ? true : false;
var isLinux = (os == 'Linux') ? true : false;
var isUNIX = (os == 'UNIX') ? true : false;

var isOp = (theNavUA.indexOf("opera") > -1) ? true : false;
var isOp5 = (theNavUA.indexOf("opera 5") > -1 || theNavUA.indexOf("opera/5") > -1) ? true : false;
var isOp6 = (theNavUA.indexOf("opera 6") > -1 || theNavUA.indexOf("opera/6") > -1) ? true : false;
var isOp7 = (theNavUA.indexOf("opera 7") > -1 || theNavUA.indexOf("opera/7") > -1) ? true : false;
var isIE = (theNavUA.indexOf("msie") > -1 && !isOp) ? true : false;
var isIE5 = (theNavUA.indexOf("msie 5") > -1 && !isOp) ? true : false;
var isIE50 = (theNavUA.indexOf("msie 5.0") > -1 && !isOp) ? true : false;
var isIE52 = (theNavUA.indexOf("msie 5.2") > -1 && !isOp) ? true : false;
var isIE6 = (theNavUA.indexOf("msie 6" && !isOp) > -1) ? true : false;
var isFFox = (theNavUA.indexOf("firefox") > -1) ? true : false;
var isNN6 = (theNavUA.indexOf("Netscape6") > -1) ? true : false;
var isNN7 = (theNavUA.indexOf("Netscape7") > -1) ? true : false;


/**
 * Multiple onload event handler
 * Mark Robinson 08 Feb 2005
 */
var lgflOnloadFunctions = new Array();
function lgflAddOnload(f) {
	if (window.onload) {
		if (window.onload != lgflOnload) {
			lgflOnloadFunctions[0] = window.onload;
			window.onload = lgflOnload;
		}
		lgflOnloadFunctions[lgflOnloadFunctions.length] = f;
	} else {
		lgflOnloadFunctions[0] = f;
		window.onload = lgflOnload;
	}
}
function lgflOnload() {
	for (var i = 0; i < lgflOnloadFunctions.length; i++) {
		eval(lgflOnloadFunctions[i])
	}
}

/**
 * Multiple onunload event handler
 * Mark Robinson 08 Feb 2005
 */
var lgflOnunloadFunctions = new Array();
function lgflAddOnunload(f) {
	if (window.onunload) {
		if (window.onunload != lgflOnunload) {
			lgflOnunloadFunctions[0] = window.onunload;
			window.onunload = lgflOnunload;
		}
		lgflOnunloadFunctions[lgflOnunloadFunctions.length] = f;
	} else {
		lgflOnunloadFunctions[0] = f;
		window.onunload = lgflOnunload;
	}
}
function lgflOnunload() {
	for (var i = 0; i < lgflOnunloadFunctions.length; i++) {
		eval(lgflOnunloadFunctions[i])
	}
}

/**
 * Find object
 */
function findObj(n, d) {
	var p, i, x;
	if (!d) {
		d = document;
	}
	if ((p = n.indexOf('?')) > 0 && parent.frames.length) {
		d = parent.frames[n.substring(p + 1)].document;
		n = n.substring(0, p);
	}
	if (!(x = d[n]) && d.all) {
		x = d.all[n];
	}
	for (i = 0; !x && i < d.forms.length; i++) {
		x = d.forms[i][n];
	}
	for (i = 0; !x && d.layers && i < d.layers.length; i++) {
		x = findObj(n, d.layers[i].document);
	}
	if (!x && d.getElementById) {
		x = d.getElementById(n);
	}
	return x;
}

/**
 * Regular expression test
 * Mark Robinson
 */
function regExpTest(pat, str) {
	var regExpSupported = false;
	if (window.RegExp) {
		var tmpStr = 'a';
		var tmpReg = new RegExp(tmpStr);
		if (tmpReg.test(tmpStr)) {
			regExpSupported = true;
		}
	}
	if (regExpSupported) {
		var ret = new RegExp(pat);
		return (!ret.test(str));
	}
}

/**
 * Read cookie
 * Mark Robinson
 */
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;
}

/**
 * Write cookie
 * Mark Robinson
 */
function writeCookie(name, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
		var expires = '; expires=' + date.toGMTString();
	}
	else {
		expires = '';
	}
	document.cookie = name + '=' + value + expires + '; path=/';
}



/**
 * Style sheet switcher functions
 * Mark Robinson
 * Needs work...
 */
function getActiveStyleSheet() {
	if (document.getElementsByTagName) {
		var i, a;
		for(i = 0; (a = document.getElementsByTagName('link')[i]); i++) {
			if(a.getAttribute('rel').indexOf('style') != -1 && a.getAttribute('title') && !a.disabled) {
				return a.getAttribute("title");
			}
		}
		return null;
	}
}
function setActiveStyleSheet(title) {
	if (document.getElementsByTagName) {
		var i, a, main;
		for(i=0; (a = document.getElementsByTagName('link')[i]); i++) {
			if(a.getAttribute('rel').indexOf('style') != -1 && a.getAttribute('title')) {
				a.disabled = true;
				if(a.getAttribute('title') == title) {
					a.disabled = false;
				}
			}
		}
	}
}
function getPreferredStyleSheet() {
	if (document.getElementsByTagName) {
		var i, a;
		for(i=0; (a = document.getElementsByTagName('link')[i]); i++) {
			if(a.getAttribute('rel').indexOf('style') != -1 && a.getAttribute('rel').indexOf('alt') == -1 && a.getAttribute('title')) {
				return a.getAttribute("title");
			}
		}
		return null;
	}
}

/**
 * Detect plugins
 * Mark Robinson
 */
function detectPlugin(pluginName, pluginMimeType, pluginActiveXName) {
	var r = new Array(false, false);
	if (!isIE) {
		if (navigator.plugins && navigator.plugins.length > 0) {
			for (i = 0; i < navigator.plugins.length; i++) {
				var x = navigator.plugins[i];
				if (x.name.indexOf(pluginName) > -1) {
					r[0] = true;
					if (x.description) {
						y = x.description;
						try {
							r[1] = y.match(/(\d+(\.?\d*)*)/)[1];
						} catch (e) { }
						try {
							r[1] = x.name.match(/(\w?\d+(\.?\d*)*)/)[1];
						} catch (e) { }
						return r;
					}
				}
			}
		} else if (navigator.mimeTypes) {
			var x = navigator.mimeTypes[pluginMimeType];
			if (x && x.enabledPlugin) {
				r[0] = true;
				return r;
			}
		}
	} else {
		for (var i = 10; i > 0; i--) {
			try {
				var x = new ActiveXObject(pluginActiveXName+'.'+i);
				r[1] = i;
				break;
			} catch (e) { }
		}
		if (r[1] > 0) {
			r[0] = true;
			return r;
		}
	}
	return false;
}

/**
 * Set external links in open in new window (standards compliant)
 * Mark Robinson
 * Makes use of "rel" attribute.
 * e.g. <a href="" title="" rel="external"></a>
 */
function setExtLinkTarget() {
	if (document.getElementsByTagName) {
		var anchors = document.getElementsByTagName('a');
		for (var i = 0; i < anchors.length; i++) {
			var anchor = anchors[i];
			if (anchor.getAttribute('href') && anchor.getAttribute('rel') == 'external') {
				anchor.target = '_blank';
				anchor.className = 'link-external';
				anchor.title = (anchor.title.indexOf('(New window)') > -1) ? anchor.title : anchor.title + ' (New window)';
			}
		}
	}
}

function setLinks() { //function taken from steven's own functions
	if (document.getElementsByTagName) {
		var anchors = document.getElementsByTagName('a');
		for (var i = 0; i < anchors.length; i++) {
			var anchor = anchors[i];
			//setExtLinkTarget(anchor);
			//setLinkTab(anchor);
			setDeleteConfirm(anchor);
		}
	}
}

function setDeleteConfirm(obj){
	var arrList = obj.className.split(' ');
	for(var i = 0; i < arrList.length; i++ ){
		if (arrList[i].toUpperCase() == 'DELETE') {
			obj.onclick = function(){
				if(confirm("Are you sure you want to delete this")){
					return true;
				}else{
					return false;
				}
			}
		}
	}
}

lgflAddOnload('setLinks()');


/**
* Wrap element using DOM compliant methods
* Mark Robinson 22 Feb 2005
*/
function lgflWrapElementById(id)
{
	if (document.getElementById)
	{
		var newParentElement = arguments[1] ? arguments[1] : 'span';
		var newParentId = arguments[2] ? arguments[2] : id+'_wrapper';
		var toBeWrapped = document.getElementById(id);
		var newparent = document.createElement(newParentElement);
		var parent = toBeWrapped.parentNode;
		parent.replaceChild(newparent, toBeWrapped);
		newparent.setAttribute('id', newParentId);
		newparent.appendChild(toBeWrapped);
		return true;
	}
	return false;
}

/**
* Set field focus
* Mark Robinson 07 Apr 2005
*/
function setFocusById(id)
{
	if (document.getElementById)
	{
		document.getElementById(id).focus();
	}
}


/**
* Load (preload-hide) stylesheet
* SHOLT
*/
function lgflPreloadHide()
{
	var tags = document.getElementsByTagName('head');
	var headtag = tags.item(0);
	newlink = document.createElement('link');
	newlink.setAttribute('id', 'preload-hide');
	newlink.setAttribute('rel', 'stylesheet');
	newlink.setAttribute('type', 'text/css');
	newlink.setAttribute('href', '/lgfl_shared/css/preload-hide.css');
	headtag.appendChild(newlink);
	return true;
}
//document.write('<link rel="stylesheet" type="text/css" href="/lgfl_shared/css/preload-hide.css" title="Normal" />');
lgflPreloadHide();

/*
* All the variables and functions have been defined,
* now lets do some work...
*/

/*
* Link targets
*/
lgflAddOnload('setExtLinkTarget()');


/*
* Style sheet selection
*/
var cookie = readCookie('lgfl_stylesheet');
var stylesheet = (cookie != 'null' ? cookie : getPreferredStyleSheet());
if (stylesheet) {
	setActiveStyleSheet(stylesheet);
}
function lgflLoadStyle() {
	var cookie = readCookie('lgfl_stylesheet');
	var title = (cookie != 'null' ? cookie : getPreferredStyleSheet());
	if (title) {
		setActiveStyleSheet(title);
	}
}
lgflAddOnload('lgflLoadStyle()');
function lgflSaveStyle() {
	var title = getActiveStyleSheet();
	if (title) {
		writeCookie('lgfl_stylesheet', title, 0);
	}
}
lgflAddOnunload('lgflSaveStyle()');








/* Still to be completed MR */

/* this function is needed to work around 
   a bug in IE related to element attributes */
function hasClass(obj) {
	var result = false;
	if (obj.getAttributeNode('class') != null) {
		result = obj.getAttributeNode('class').value;
	}
	return result;
} /* function hasClass ends */

/* this function adds 'Zebra' effect to table */
function stripeTable(id) {
	if (document.getElementById) {
		var even = false;
		var evenColor = arguments[1] ? arguments[1] : '#fff';
		var oddColor = arguments[2] ? arguments[2] : '#eee';
		var table = document.getElementById(id);
		if (!table)	{
			return;
		}
		var tbodies = table.getElementsByTagName('tbody');
		for (var h = 0; h < tbodies.length; h++) {
			var trs = tbodies[h].getElementsByTagName('tr');
			for (var i = 0; i < trs.length; i++) {
				if (!hasClass(trs[i]) && !trs[i].style.backgroundColor) {
					var tds = trs[i].getElementsByTagName('td');
					for (var j = 0; j < tds.length; j++) {
						var mytd = tds[j];
						if (!hasClass(mytd) && !mytd.style.backgroundColor) {
							mytd.style.backgroundColor = even ? evenColor : oddColor;
						}
					}
				}
				even =  !even;
			}
		}
	}
}


function lgfl_openFolder(obj){ //used by class LGfL_displayFiles 
	var parent = obj.parentNode;
	if(parent.lastChild.className=="show"){
		parent.lastChild.className="hide";
	}else{
		parent.lastChild.className="show";
	}	
	return false;
}








