/******************************************************************************
domutilities.js
	Copyright (C) 2007 Atlantic Database Systems, Inc. All rights reserved.
	Helper functions to manipulate the DOM
******************************************************************************/

/******************************************************************************
AddInputItem (parent, type, name, dbid, value, title, szClass, 
				size, maxLength, OnChg)
	Helper to add a data entry item.
******************************************************************************/

function AddInputItem (parent, type, name, dbid, value, title, szClass, 
						size, maxLength, OnChg)
{
	var	inp = document.createElement ("input");

	inp.type = type;
	inp.name = inp.id = name;
	inp.dbid = dbid;

	if (value)
		inp.value = value;
	if (size)
		inp.size = size;
	if (maxLength)
		inp.maxLength = maxLength;

	if (OnChg)
		inp.onchange = OnChg;
	if (title)
		inp.title = title;
	if (szClass)
		inp.className = szClass;

	parent.appendChild (inp);

	return inp;
}

/******************************************************************************
makeButton (parent, label, id, onClick)
	Create a button control.
******************************************************************************/

function makeButton (parent, label, id, onClick)
{
	if (typeof (parent) == "string")
		parent = document.getElementById (parent);

	var	but = document.createElement ("button");
	but.appendChild (document.createTextNode (label));

	if (id)
		but.id = id;
	if (onClick)
		but.onclick = onClick;

	if (parent)
		parent.appendChild (but);

	return but;
}

/******************************************************************************
EnableInputItem (inp, bEnabled)
	Helper to enable or disable an input item.
******************************************************************************/

function EnableInputItem (inp, bEnabled)
{
	if (typeof (inp) == "string")
		inp = document.getElementById (inp);

	inp.readOnly = !bEnabled;
	var	bgcolor = bEnabled ? "white" : "lightgrey";
	inp.style.backgroundColor = bgcolor;
}

/******************************************************************************
IsInputItemEnabled (inp)
	Helper to check if an input item is enabled or disabled.
******************************************************************************/

function IsInputItemEnabled (inp)
{
	if (typeof (inp) == "string")
		inp = document.getElementById (inp);

	return !inp.readOnly;
}

/******************************************************************************
GetElementText (e)
	Helper to return text.
******************************************************************************/

function GetElementText (e)
{
	if (typeof (e) == "string")
		e = document.getElementById (e);

	var	szText = "";

	if (e.firstChild) {
		szText = e.firstChild.data;
	}

	return szText;
}

/******************************************************************************
GetElementTextLast (e)
	Helper to return text.
******************************************************************************/

function GetElementTextLast (e)
{
	if (typeof (e) == "string")
		e = document.getElementById (e);

	var	szText = "";

	if (e.lastChild) {
		szText = e.lastChild.data;
	}

	return szText;
}

/******************************************************************************
SetElementText (e, text)
	Helper to set text.
******************************************************************************/

function SetElementText (e, text)
{
	if (typeof (e) == "string")
		e = document.getElementById (e);

	var	textel;

	for (textel = e.firstChild; textel && textel.nodeType != 3; // 3 = Node.TEXT_NODE
			textel = textel.nextSibling);

	if (textel) {
		textel.data = text;
	} else {
		t = document.createTextNode (text);
		e.appendChild (t);
	}
}

/******************************************************************************
SetElementHTML (e, html)
	Helper to set html.
******************************************************************************/

function SetElementHTML (e, html)
{
	if (typeof (e) == "string")
		e = document.getElementById (e);

	if (e)
		e.innerHTML = html;
}

/******************************************************************************
GetInputText (e)
	Helper to return text from an input element.
******************************************************************************/

function GetInputText (e)
{
	var	szText = "";

	if (typeof (e) == "string")
		e = document.getElementById (e);

	szText = e.value;

	return szText;
}

/******************************************************************************
getTrimmedInput (field) - Get and trim and input field.
******************************************************************************/

function getTrimmedInput (field)
{
	return Trim (GetInputText (field));
}

/******************************************************************************
SetInputText (e, text)
	Helper to set text into an input element.
******************************************************************************/

function SetInputText (e, text)
{
	if (typeof (e) == "string") {
//		SetUserMsg ("SetInputText '" + e + "'");
		e = document.getElementById (e);
	}

	e.value = text;
}

/******************************************************************************
isItemChecked (el)
	Helper to check if an item is checked.
******************************************************************************/

function isItemChecked (el)
{
	if (typeof (el) == "string")
		el = document.getElementById (el);

	return el && el.checked;
}

/******************************************************************************
SetItemChecked (el, bChecked)
	Helper to check or uncheck an item.
******************************************************************************/

function SetItemChecked (el, bChecked)
{
	if (typeof (el) == "string")
		el = document.getElementById (el);

	if (el)
		el.checked = bChecked;
}

/******************************************************************************
IsElementDisplayed (e)
	Helper to return true if the element is displayed.
******************************************************************************/

function IsElementDisplayed (e)
{
	var	bDisplayed = false;

	if (typeof (e) == "string")
		e = document.getElementById (e);

	if (e)
		bDisplayed = (e.style.display != "none");

	return bDisplayed;
}

/******************************************************************************
SetElementDisplayed (e, bDisplayed, ShowHow)
	Helper to set the display state of an element.
******************************************************************************/

function SetElementDisplayed (e, bDisplayed, ShowHow)
{
	if (typeof (e) == "string")
		e = document.getElementById (e);

	if (!ShowHow)
		ShowHow = "";

	if (e)
		e.style.display = bDisplayed ? ShowHow : "none";
}

/******************************************************************************
IsElementVisible (e)
	Helper to return true if the element is visible.
******************************************************************************/

function IsElementVisible (e)
{
	var	bVisible = false;

	if (typeof (e) == "string")
		e = document.getElementById (e);

	if (e)
		bVisible = (e.style.visibility != "hidden");

	return bVisible;
}

/******************************************************************************
SetElementVisible (e, bVisible)
	Helper to set the visibility of an element.
******************************************************************************/

function SetElementVisible (e, bVisible)
{
	if (typeof (e) == "string")
		e = document.getElementById (e);

	if (e)
		e.style.visibility = bVisible == true ? "visible" : "hidden";
}

/******************************************************************************
GetElementLoc (e)
	Helper to get the location of an element.
******************************************************************************/

function GetElementLoc (e)
{
	if (typeof (e) == "string")
		e = document.getElementById (e);

	var	x = 0;
	var	y = 0;

	for (var el = e; el; el = el.offsetParent) {
		x += el.offsetLeft;
		y += el.offsetTop;
	}

	for (var el = e.parentNode; el && el != document.body; el = el.parentNode) {
		if (el.scrollLeft) x -= el.scrollLeft;
		if (el.scrollTop) y -= el.scrollTop;
	}

	return {x: x, y: y};
}

/******************************************************************************
centerElementAbove (item, ref)
	Helper to center item over ref.
******************************************************************************/

function centerElementAbove (item, ref)
{
	if (typeof (item) == "string")
		item = document.getElementById (item);

	if (typeof (ref) == "string")
		ref = document.getElementById (ref);

	if (item && ref) {

		var	availWidth = Geometry.getViewportWidth ();
		var	availHeight = Geometry.getViewportHeight ();

		var	loc = GetElementLoc (ref);

		var	refWidth = ref.offsetWidth;
		var	itemWidth = item.offsetWidth;
		var	itemHeight = item.offsetHeight;

		var	left = (loc.x + refWidth / 2 - itemWidth / 2);

		if (left < 0)
			left = 0;
		else {
			var	shift = left + itemWidth - availWidth;
			if (shift > 0)
				left -= Math.min (left, shift);
		}

		var	top = (loc.y - itemHeight);

		if (top < 0)
			top = 0;
		else {
			var	shift = top + itemHeight - availHeight;
			if (shift > 0)
				top -= Math.min (top, shift);
		}

		item.style.left = left + "px";
		item.style.top = top + "px";
	}
}

/******************************************************************************
SetClass (elem, className)
	Helper to set the class of an element. Guess who gets this wrong.
	With thanks to http://alistapart.com/articles/jslogging (See end of article)
******************************************************************************/

function SetClass (elem, className)
{
	if (typeof (elem) == "string")
		elem = document.getElementById (elem);

	// if the node's class already exists
	// then replace its value
	if (elem.getAttributeNode ("class")) {
		for (var i = 0; i <elem.attributes.length; i++) {
			var attrName = elem.attributes[i].name.toUpperCase();
			if (attrName == 'CLASS') {
				elem.attributes[i].value = className;
			}
		}
	// otherwise create a new attribute
	} else {
		elem.setAttribute ("class", className);
	}
}

/******************************************************************************
centerElements (containerid, elids)
	Center a row of elements.
******************************************************************************/

function centerElements (containerid, elids)
{
	var	availWidth = document.getElementById (containerid).scrollWidth;

	var	nrEls = elids.length;
	var	usedWidth = 0;

	for (var i = 0; i < nrEls; i++)
		usedWidth += document.getElementById (elids [i]).scrollWidth;

	var	spacing = Math.floor ((availWidth - usedWidth) / (nrEls + 1));
	spacing = Math.max (spacing - 2, 0);
	spacing = spacing + "px";

	for (var i = 0; i < nrEls; i++)
		document.getElementById (elids [i]).style.marginLeft = spacing;
}

/******************************************************************************
setWaitCursor (bOn)
	Set the cursor to wait or auto.
******************************************************************************/

function setWaitCursor (bOn)
{
	document.body.style.cursor = bOn ? "wait" : "auto";
}

/******************************************************************************
addmsg (str, msg)
	Helper to append a message on a new line
******************************************************************************/

function addmsg (str, msg)
{
	if (str)
		str += "<br/>";

	str += msg;

	return str;
}

/******************************************************************************
BuildNodeStructure (node, msg, indent)
	Helper to build the structure of a node and its children
******************************************************************************/

function BuildNodeStructure (node, msg, indent)
{
	if (!msg)
		msg = "";

	if (!node)
		return msg;

	if (!indent)
		indent = "";

	var	newmsg = indent + "Node: type " + node.nodeType + " name " + node.nodeName + 
					" value ";
	if (node.nodeValue)
		newmsg += "'" + node.nodeValue + "'";
	else
		newmsg += node.nodeValue;

	newmsg += " children " + node.childNodes.length;

	msg = addmsg (msg, newmsg);

	try {
		for (var el = node.firstChild; el; el = el.nextSibling)
			msg = BuildNodeStructure (el, msg, indent + "&nbsp;&nbsp;");
	} catch (ex) {
	}

	return msg;
}

/******************************************************************************
SetUserMsg (msg)
	Helper to set a message to the user.
******************************************************************************/

function SetUserMsg (msg)
{
	var UserMsg = document.getElementById ("UserMsg");

	if (UserMsg) {
		if (UserMsg.firstChild)
			UserMsg.firstChild.data = msg;
		else
			UserMsg.appendChild (document.createTextNode (msg));
	}
}

/******************************************************************************
SetUserMsgHTML (msg)
	Helper to set a message to the user.
******************************************************************************/

function SetUserMsgHTML (msg)
{
	var UserMsg = document.getElementById ("UserMsg");

	if (UserMsg) {
		UserMsg.innerHTML = msg;
	}
}

/******************************************************************************
logObject - Helper to display an object contents in the console.log
******************************************************************************/

function logObject (respObj, level)
{
	if (!level)
		level = "";

	for (var item in respObj) {
		console.log (level + item + " = '" + respObj [item] + "'");
		if (typeof (respObj [item]) == "object") {
			logObject (respObj [item], level + "  ");
		}
	}
}

/******************************************************************************
MkListItem (ID, Text, Title, IsSelected, attr, attrvalue)
	Make an item for a list.
******************************************************************************/

function MkListItem (ID, Text, Title, IsSelected, attr, attrvalue)
{
	var	opt = new Option (Text, ID, false, false);
	opt.title = Title;
	opt.selected = IsSelected;
	if (attr)
		opt.setAttribute (attr, attrvalue);
	return opt;
}

if (!$.browser.msie)
	$.fn.addOption = function (opt) {return this.append (opt);}
else {
	$.fn.addOption = function (opt) {return this[0].add (opt);}
}


