/*******************************

 DHTML.js, Version 3.9, 22-Jul-2009
 Copyright Northgate Information Solutions UK Limited, 2002-2009

 *******************************/

/********************
 DHTML class
 
 Constructor:
	DHTML()
 Class methods:
	getBrowserDetails()
	getClientWidth(win)
	getClientHeight(win)
	getScrollLeft(win)
	getScrollTop(win)
	getParent(element)
	getOffsetLeft(obj)
	getOffsetTop(obj)
	inTable(obj)
	getComputedStyle(doc, element, style)
	compareVersions(version1, version2)
	processDocumentTags(func, n, args)
	getFrameDimensions(win)
 ********************/
 
// *** Constructor for DHTML class ***
function DHTML() {
	return null;
}

// *** DHTML class methods ***
DHTML.getBrowserDetails = function dhtmlGetBrowserDetails() {
	var result;
	
	/* Regular expressions that match the userAgent properties
	   for different browsers. */
	var reNavProps = [
			// Opera.
			{ userAgent: /Opera/i,
			  name: "Opera",
			  minSupportedVersion: DHTML.NOTALLOWED,
			  minAllowedVersion: DHTML.NOTALLOWED,
			  id: "" },
			// Internet Explorer.
			// Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; InfoPath.1)
			{ userAgent: /; MSIE ([^;]+)[^\)]+\)/i,
			  name: "Internet Explorer",
			  minSupportedVersion: "6.0",
			  minAllowedVersion: "6.0",
			  id: "IE" },
			// Netscape.
			{ userAgent: /rv:[\d\.]+\) Gecko\/\d{8} Netscape.?\/([\d\.]+)/i,
			  name: "Netscape",
			  minSupportedVersion: DHTML.NOTALLOWED,
			  minAllowedVersion: "7.0",
			  id: "FF" },
			// Mozilla.
			{ userAgent: /Mozilla\/[\d\.]+ \(.+; rv:([\d\.]+)\) Gecko\/\d{8}$/i,
			  name: "Mozilla",
			  minSupportedVersion: DHTML.NOTALLOWED,
			  minAllowedVersion: "1.4",
			  id: "FF" },
			// Mozilla Firefox.
			// Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Gecko/20040803 Firefox/1.0
			// Mozilla/5.0 (X11; U; Linux i686; en-GB; rv:1.7.5) Gecko/20041215 Firefox/1.0 Red Hat/1.0-12.EL4
			{ userAgent: /Firefox\/([\d\.]+)/i,
			  name: "MozillaFirefox",
			  minSupportedVersion: "2.0",
			  minAllowedVersion: "1.0",
			  id: "FF" },
			// Safari.
			// Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.19 (KHTML, like Gecko) Version/3.1.2 Safari/525.21
			// Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10_5_5; en-us) AppleWebKit/525.27.1 (KHTML, like Gecko) Version/3.2.1 Safari/525.27.1
//			{ userAgent: /Windows.+Version\/([\d\.]+)\sSafari/i,
			{ userAgent: /Version\/([\d\.]+)\sSafari/i,
			  name: "Safari",
			  minSupportedVersion: DHTML.NOTALLOWED,
			  minAllowedVersion: "3.1",
			  id: "FF" },
			// Chrome.
			// Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.19 (KHTML, like Gecko) Chrome/0.3.154.9 Safari/525.19
			{ userAgent: /Windows.+Chrome\/([\d\.]+)/i,
			  name: "Chrome",
			  minSupportedVersion: DHTML.NOTALLOWED,
			  minAllowedVersion: "0.3",
			  id: "FF" }
		];

    var ua = navigator.userAgent;
    var pf = navigator.platform;
    
	// Assume an unsupported browser until proved otherwise.
	DHTML.browserType = null;
	DHTML.supportLevel = DHTML.NOTALLOWED;

    for (var i = 0; i < reNavProps.length; i++) {
		result = reNavProps[i].userAgent.exec(ua);
		if (result != null) {
			DHTML.browserVersion = result[1];
			if (reNavProps[i].minAllowedVersion != DHTML.NOTALLOWED) {
				/* If the browser is at least the minimum fully
				   supported version... */
				if (reNavProps[i].minSupportedVersion != DHTML.NOTALLOWED
						&& DHTML.compareVersions(DHTML.browserVersion,
							reNavProps[i].minSupportedVersion) != -1) {
					DHTML.supportLevel = DHTML.FULLSUPPORT;
				}
				// If the browser is at least the minimum allowed version...
				else if (DHTML.compareVersions(DHTML.browserVersion,
						reNavProps[i].minAllowedVersion) != -1) {
					DHTML.supportLevel = DHTML.NOSUPPORT;
				}
				DHTML.browserType = reNavProps[i].id;
			}
			DHTML.browserDetails = reNavProps[i];
			break;
		}
    }
    
    // IE is only fully supported on Windows.
    if (DHTML.browserType == "IE" && pf.search(/Win32/i) == -1) {
		DHTML.supportLevel = DHTML.NOSUPPORT;
    }
}

DHTML.getClientWidth = function dhtmlGetClientWidth(win) {
	return(win.document.body.clientWidth);
}

DHTML.getClientHeight = function dhtmlGetClientHeight(win) {
	return(win.document.body.clientHeight);
}

DHTML.getScrollLeft = function dhtmlGetScrollLeft(win) {
	if (win.document.all)
		return(win.document.documentElement.scrollLeft);
	else
		return(win.pageXOffset);
}

DHTML.getScrollTop = function dhtmlGetScrollTop(win) {
	if (win.document.all)
		return(win.document.documentElement.scrollTop);
	else
		return(win.pageYOffset);
}

	// Get a reference to an element's parent.
DHTML.getParent = function dhtmlGetParent(element) {
	if (document.getElementById)
		return(element.parentNode);
	else
		return(element.parentElement);
}

DHTML.getOffsetLeft = function dhtmlGetOffsetLeft(obj) {
	if (DHTML.browserType == "IE"
			&& DHTML.browserVersion < 5.5
			&& DHTML.inTable(obj)) {
		var os = 0;
		for (var el = obj; el.tagName != "BODY"; el = el.parentNode)
			os += el.offsetLeft;
		return(os);
	}
	else
		return(obj.offsetLeft + obj.offsetParent.offsetLeft);
}

DHTML.getOffsetTop = function dhtmlGetOffsetTop(obj) {
	if (DHTML.browserType == "IE"
			&& DHTML.browserVersion < 5.5
			&& DHTML.inTable(obj)) {
		var os = 0;
		for (var el = obj; el.tagName != "BODY"; el = el.parentNode)
			os += el.offsetTop;
		return(os);
	}
	else
		return(obj.offsetTop + obj.offsetParent.offsetTop);
}

DHTML.inTable = function dhtmlInTable(obj) {
	// If the clicked node is in a table...
	for (var el = obj; el.tagName != "BODY"; el = el.parentNode)
		if (el.tagName == "TD" || el.tagName == "TH")
			return(true);
	return(false);
}

DHTML.getComputedStyle = function dhtmlGetComputedStyle(doc, element, style) {
	/* Parameters:
		doc - a reference to the document containing element.
		element - a reference to the element for which the style is required.
		style - a string containing the name of the required style. */
	
	if (typeof style != "string")
		return null;
		
	if (DHTML.browserType == "IE")
		return eval("element.currentStyle." + style);
	else if (DHTML.browserType == "FF")
		return eval("doc.defaultView.getComputedStyle(element, null)." + style);
	else
		return null;
}

DHTML.isWordTopic = function dhtmlIsWordTopic(doc) {
	var metaTags = doc.getElementsByTagName("meta");
	for (var i = 0; i < metaTags.length; i++) {
		// <meta name=ProgId content=Word.Document>
		if (metaTags[i].name == "ProgId" && metaTags[i].content == "Word.Document")
			return true;
	}	
	return false;
}

DHTML.compareVersions = function dhtmlCompareVersions(version1, version2) {
	/* Compares two version numbers, each of which is a string in the
	   format: major.minor{.build}. Returns -1 if version1 is less than
	   version2; 1 if version2 is less than version1; and 0 if the two
	   are equal.
	   
	   If either is not in the correct format, the function returns null.
	*/
	
	var v1;
	var v2;
	
	var result = 0;
	var ver1 = version1.toString().match(/^(\d+)\.(\d+)(?:\.(\d+))?(?:\.(\d+))?$/);
	if (ver1 == null)
		return(null);
	var ver2 = version2.toString().match(/^(\d+)\.(\d+)(?:\.(\d+))?(?:\.(\d+))?$/);
	if (ver2 == null)
		return(null);
	for (var i = 1; i <= 3; i++) {
		v1 = parseInt(ver1[i]);
		v1 = (isNaN(v1) ? 0 : v1);
		v2 = parseInt(ver2[i]);
		v2 = (isNaN(v2) ? 0 : v2);
			
		if (v1 < v2)
			return(-1);
		if (v1 > v2)
			return(1);
	}
	return(0);
}

DHTML.processDocumentTags = function dhtmlProcessDocumentTags(func, n, args)
{
	/*************************
	 Uses a supplied function to process a document node and then
	 recursively processes each of that node's children.
	 func is the function to call.
	 n is the node to process.
	 args is an array of arguments to be passed to func.
	 results can be returned by changing the array contents.
	**************************/
	
	// Call the function.
	func(n, args);
	
	// Get the children of the node.
	var children = n.childNodes;
	// Process each child...
	for (var i = 0; i < children.length; i++)
		DHTML.processDocumentTags(func, children[i], args);
}

DHTML.getFrameDimensions = function dhtmlGetFrameDimensions(win) {
	var result = {
		width: 0,
		height: 0
	}
	var frame = document.getElementById(win.name);
	if (frame.scrollHeight) {
		result.width = frame.scrollWidth;
		result.height = frame.scrollHeight;
	}
	return result;
}

// *** DHTML class variables ***
DHTML.browserVersion = null;
DHTML.browserType = null;
DHTML.supportLevel = null;
DHTML.browserDetails = null;

// *** DHTML constants ***
DHTML.FULLSUPPORT = 1;
DHTML.NOSUPPORT = 0;
DHTML.NOTALLOWED = -1;

