///////////////////////////////////////////////////////////////////////////////////////////////////
//	DLib General JavaScript functions - copyright davidviner.com 2006-2009 (except where stated)
//
//	04.03.2009	5.5.9	DJV		Slight rearrangement to Ajax code.
//
///////////////////////////////////////////////////////////////////////////////////////////////////

//C////////////////////////////////////////////////////////////////////////////////////////////////
// Utility functions.
// @DLibUtilities
///////////////////////////////////////////////////////////////////////////////////////////////////

DLibUtilities = function ()
{
	var	ta = new Object;

	var _private =
	{
		///////////////////////////////////////////////////////////////////////////////////////////
		// From http://www.softcomplex.com/docs/get_window_size_and_scrollbar_position.html
		///////////////////////////////////////////////////////////////////////////////////////////

		filterResults : function (n_win, n_docel, n_body)
		{
			var n_result = n_win ? n_win : 0;

			if (n_docel && (!n_result || (n_result > n_docel)))
			{
				n_result = n_docel;
			}

			return (n_body && (!n_result || (n_result > n_body)) ? n_body : n_result);
		}
	}

	var _public =
	{
		//F////////////////////////////////////////////////////////////////////////////////////////
		// Find the x/Y position of an object (from: http://www.quirksmode.org/js/findpos.html).
		// This doesn't take into account scrolling - add the values from scrollOffsets.
		// @findPos [int]	The x/y position.
		// @obj		Object	The object to check.
		///////////////////////////////////////////////////////////////////////////////////////////

		findPos : function (obj)
		{
			var	left = 0;
			var top = 0;

			if (obj.offsetParent)
			{
				left = obj.offsetLeft;
				top = obj.offsetTop;

				// WARNING: this loop will fail if it encounters a "position: relative" box

				while (obj = obj.offsetParent)
				{
					left += obj.offsetLeft;
					top += obj.offsetTop;
				}
			}

			return [left, top];
		},

		//F////////////////////////////////////////////////////////////////////////////////////////
		// Find the X/Y scroll positions of the browser window. From
		// http://www.softcomplex.com/docs/get_window_size_and_scrollbar_position.html.
		// @scrollOffset	[int]	X/Y offset (if any).
		///////////////////////////////////////////////////////////////////////////////////////////

		scrollOffset : function ()
		{
			scX = _private.filterResults (window.pageXOffset ? window.pageXOffset : 0,
				document.documentElement ? document.documentElement.scrollLeft : 0,
				document.body ? document.body.scrollLeft : 0);

			scY = _private.filterResults (window.pageYOffset ? window.pageYOffset : 0,
				document.documentElement ? document.documentElement.scrollTop : 0,
				document.body ? document.body.scrollTop : 0);

			return [scX, scY];
		},

		//F////////////////////////////////////////////////////////////////////////////////////////
		// Get the current browser window size. From
		// www.howtocreate.co.uk/tutorials/javascript/browserwindow
		// @getWindowSize	[int]	Array of width/height.
		///////////////////////////////////////////////////////////////////////////////////////////

		getWindowSize : function ()
		{
			var w = 0, h = 0;

			if (typeof (window.innerWidth) == 'number')
			{
				// Standard

				w = window.innerWidth;
				h = window.innerHeight;
			}
			else
			if (document.documentElement && (document.documentElement.clientWidth ||
				document.documentElement.clientHeight))
			{
				// IE 6+ in 'standards compliant mode'

				w = document.documentElement.clientWidth;
				h = document.documentElement.clientHeight;
			}
			else
			if (document.body && (document.body.clientWidth || document.body.clientHeight))
			{
				// IE 4 compatible

				w = document.body.clientWidth;
				h = document.body.clientHeight;
			}

			return [w, h];
		},

		///////////////////////////////////////////////////////////////////////////////////////////
		// Used by SEF_TABLE entry in dbscreens.php.
		///////////////////////////////////////////////////////////////////////////////////////////

		selectMove : function (fn, md)
		{
			var selList = document.getElementById (fn + "_sel");
			var avList = document.getElementById (fn + "_av");
			var mainList = document.getElementById (fn);
			var from = avList;
			var to = selList;

			if (md == 1)
			{
				from = selList;
				to = avList;
			}

			if (from.length > 0)
			{
				var idx = from.selectedIndex;

				if (idx > -1)
				{
					var txt = from.options [idx].text;
					var val = from.options [idx].value;
					var newOpt = new Option (txt, val);
					var done = false;

					for (i = 0; i < to.length; i++)
						if (txt < to.options [i].text)
						{
							to.options.add (newOpt, i);
							done = true;
							break;
						}

					if (!done)
						to.options.add (newOpt, to.length);

					from.options [idx] = null;
				}

				var sList = "^";

				if (selList.length > 0)
				{
					for (i = 0; i < selList.length; i++)
					{
						sList += selList.options [i].value + "^";
					}
				}

				if (sList == "^") sList = "";

				mainList.value = sList;
			}
		},

		///////////////////////////////////////////////////////////////////////////////////////////
		// Initialise the taField values.
		///////////////////////////////////////////////////////////////////////////////////////////

		initTA : function (taFlds, minRows, maxRows)
		{
			ta.flds = taFlds;
			ta.minRows = minRows;
			ta.maxRows = maxRows;
		},

		///////////////////////////////////////////////////////////////////////////////////////////
		// Handle a taField vertical size.
		///////////////////////////////////////////////////////////////////////////////////////////

		expandTA : function (fld)
		{
			for (i = 0; i < ta.flds.length; i++)
			{
				document.getElementById (ta.flds [i]).rows =
					(fld == ta.flds [i] ? ta.maxRows : ta.minRows);
			}
		},

		///////////////////////////////////////////////////////////////////////////////////////////
		// Toggle the visibility of the image change fields (SEF_IMAGE).
		///////////////////////////////////////////////////////////////////////////////////////////

		imgChgFlds : function (fld)
		{
			var f = document.getElementById ("ichg" + fld).style;
			f.display = (f.display == "none" ? "inline" : "none");
		},

		//F////////////////////////////////////////////////////////////////////////////////////////
		// Initialise an AJAX connection.
		// @initAjax	object	The ajax object.
		// @url			string	The URL of the ajax handler.
		// @resp		object	The function object	that will handle the ajax response.
		///////////////////////////////////////////////////////////////////////////////////////////

		initAjax : function (url, resp)
		{
			var xmlHttp;

			xmlHttp = (window.XMLHttpRequest ? new XMLHttpRequest () :
				new ActiveXObject ("MSXML2.XMLHTTP"));

			xmlHttp.open ("GET", url, true);
			xmlHttp.onreadystatechange = resp;
			xmlHttp.send (null);

			return xmlHttp;
		},

		//F////////////////////////////////////////////////////////////////////////////////////////
		// Return true if there is a valid response from the specified AJAX connection.
		// @checkAjax	boolean	True if valid.
		// @ajax		object	The ajax object to be checked.
		///////////////////////////////////////////////////////////////////////////////////////////

		checkAjax : function (ajax)
		{
			return (ajax != null && ajax.readyState == 4 && ajax.status == 200);
		}
	};

	return _public;
} ();

///////////////////////////////////////////////////////////////////////////////////////////////////


