// een library met (HTML)node functies ============ // 
// (c) Leen Bessleink 2004 ======================== // 

// constructor functie nodes ======================= //
var nodes = function () {
	this.id=true;
}

nodes.prototype.childNodeBy = function (el, condition) {
	if (condition == false)
		return (false);
		
// oops , next part is not good at all.
// problems in Mozilla width typeof el==function
	if (typeof el == 'undefined' || typeof el == 'function')
		return (false);

	if (this.check_condition (el, condition))
		return (el);


	if (!el.hasChildNodes ())
		return (false);

	for (var el2 in el.childNodes)
		if (el2 != 'length') {
			var el3 = el.childNodes [el2];

			if (this.childNodeBy (el3, condition) !== false)
				return (el3);
		}

	return (false);
}


// parentNodeBy ==================================== //
// zoek een parent node die voldoet aan:
//  condition (a=b)
// voorbeeld
// (o,'nodeName=TD')
// o = het huidige object waarin men zich bevind
// zoek naar de eerste parent met de property nodeName=TD
// oftewel: zoek naar de eerste bovenliggende TD
// =================================================== //
nodes.prototype.parentNodeBy  = function (el, condition) {
	if (typeof condition != 'string')
		condition = false;

	while (true) {
		if (typeof el.parentNode == 'undefined')
			return (false);

		if (condition == false)
			return (el.parentNode);

		if (this.check_condition (el.parentNode, condition))
			return (el.parentNode);

		var el = el.parentNode;
		if (el == null)
			return (false);
	}
	return (false);
}

// check_condition ============================================ //
/*
	zinnigeParentNode = parentNodeBy ('prop!=', thisNode);
*/
// hulpfunctie ================================================ //

nodes.prototype.check_condition = function (el, condition) {
	if (typeof condition != 'string')
		return (false);

	var arr = condition.split ('=', 2);

	if (arr.length == 1)
		arr [1] = '';

	if (typeof arr.length != 'undefined' && arr.length == 2) {
		var negative = arr [0].replace (/\!$/, '');
		if (negative == arr [0]) {
			negative = false;
		} else {
			arr [0] = negative;
			negative = true;
		}
		try {
			
			if (negative) {
				if (nodes.make_string (el.getAttribute (arr [0])) != arr [1])
					return (true);

				if (nodes.make_string (el [arr [0]]) != arr [1])
					return (true);
			} else {

				if (nodes.make_string (el.getAttribute (arr [0])) == arr [1])
					return (true);

				if (nodes.make_string (el [arr [0]]) == arr [1])
					return (true);
			}
		} catch (e) {
		}
	}
	return (false);
}

nodes.prototype.make_string = function (val) {
	switch (typeof val) {
	case 'undefined':
	case 'null':
		return ('');
	case 'object':
		if (val === null)
			return ('');
	}

	return (val);
}

nodes.prototype.is_node = function (node) {
	if (node != null && typeof node != 'undefined') {
		if (typeof node.nodeType != 'undefined') {
			var nodeType = node.nodeType.toString ();

			if (nodeType != 3 && nodeType != 8) {
				return (true);
			}
		}
	}
	return (false);
}

nodes.prototype.getPreviousSibling = function (p) {
	var s = p.previousSibling;
	if (s.nodeName=='#text')
		return this.getPreviousSibling (s);
	else
		return s;
}

nodes.prototype.getNextSibling = function (p) {
	var s = p.nextSibling;
	if (s.nodeName=='#text')
		return this.getNextSibling (s);
	else
		return s;
}

/*
// een library met (HTML)node functies ============ // 
// (c) Leen Bessleink ============================= // 

// functies beschikbaar:
//parentNodeBy(OBJECT , 'property=value')


// constructor functie nodes ======================= //
var nodes = function () {
	this.id=true;
}

// parentNodeBy ==================================== //
// zoek een parent node die voldoet aan:
//  condition (a=b)
// voorbeeld
// (o,'nodeName=TD')
// o = het huidige object waarin men zich bevind
// zoek naar de eerste parent met de property nodeName=TD
// oftewel: zoek naar de eerste bovenliggende TD
// =================================================== //
nodes.prototype.parentNodeBy  = function (el, condition) {
	if (typeof condition != 'string')
		condition = false;

	while (true) {
		if (typeof el.parentNode == 'undefined')
			return (false);

		if (condition == false)
			return (el.parentNode);

		if (this.check_condition (el.parentNode, condition))
			return (el.parentNode);

		var el = el.parentNode;
	}
	return (false);
}

nodes.prototype.childNodeBy = function (el, condition) {
	if (condition == false)
		return (false);

	if (typeof el == 'undefined')
		return (false);

	if (this.check_condition (el, condition))
		return (el);

	if (!el.hasChildNodes ())
		return (false);

	for (var el2 in el.childNodes)
		if (el2 != 'length') {
			var el3 = el.childNodes [el2];

			if (this.childNodeBy (el3, condition) !== false)
				return (el3);
		}

	return (false);
}
// check_condition ============================================ //

//	zinnigeParentNode = parentNodeBy ('prop!=', thisNode);

// hulpfunctie ================================================ //

nodes.prototype.check_condition = function (el, condition) {
	if (typeof condition != 'string')
		return (false);

	var arr = condition.split ('=', 2);

	if (arr.length == 1)
		arr [1] = '';

	if (typeof arr.length != 'undefined' && arr.length == 2) {
		var negative = arr [0].replace (/\!$/, '');
		if (negative == arr [0]) {
			negative = false;
		} else {
			arr [0] = negative;
			negative = true;
		}
		try {
			if (negative) {
//				if (el.getAttribute (arr [0])+"" != arr [1])
//					return (true);
//				else
					if (el [arr [0]] != arr [1])
						return (true);
			} else {
//				if (el.getAttribute (arr [0])+"" == arr [1])
//					return (true);
//				else
					if (el [arr [0]] != arr [1])
						return (true);
			}
		} catch (e) {
		}
	}
	return (false);
}

nodes.prototype.is_node = function (node) {
	if (node != null && typeof node != 'undefined') {
		if (typeof node.nodeType != 'undefined') {
			var nodeType = node.nodeType.toString ();

			if (nodeType != 3 && nodeType != 8) {
				return (true);
			}
		}
	}
	return (false);
}

nodes.prototype.getPreviousSibling = function (p) {
	var s = p.previousSibling;
	if (s.nodeName=='#text')
		return this.getPreviousSibling (s);
	else
		return s;
}

nodes.prototype.getNextSibling = function (p) {
	var s = p.nextSibling;
	if (s.nodeName=='#text')
		return this.getNextSibling (s);
	else
		return s;
}
*/

var nodes = new nodes ();


