// JavaScript Document

/*
	Description: return an element using it's id as an identifier
*/
function byId(source, id){
	
	if(arguments.length == 1) {
		return document.getElementById(source);	
	}
	
	return source.getElementById(id);	
}

/*
	Description: return an array of elements using their tagname as an identifier
*/
function byTagName(source, name) {
	
	if(arguments.length == 1) {
		return document.getElementsByTagName(source);	
	}
	
	return source.getElementsByTagName(name);	
}

/*
	Description: returns an array of elements using their name as an identifier
*/
function byName(source, name) {
	
	if(arguments.length == 1) {
		return document.getElementsByName(source);	
	}
	
	return source.getElementsByName(name);	
}

/*

*/
function getChildElement(node, index) {
	if(typeof node != "object") return false;
	
	var len = node.childNodes.length;
	var iterate = 1;
	
	if(index < 1 || index > len || typeof index != "number") {
		index == 1;	
	}
	
	for(var i=0; i<len; i++) {
		var child = node.childNodes[i];
		
		if(child.nodeType == "1") {
			
			if(iterate == index) {
				return child;	
			}
			
			iterate++;
		}
	}
	return false;
}

/*
	@param inclusive - include text with style tags such as span
*/
function getElementValue(node, inclusive) {
	if(typeof node != "object") { return false; }
	
	var len = node.childNodes.length;
	var str = "";
	
	for(var i=0; i<len; i++) {
		var val = node.childNodes[i];
		
		if(val.nodeType == "3" && (val.nodeValue != null || val.nodeValue != "")) {
			str += val.nodeValue;
		} else if(inclusive && tag(val.nodeName)) {
			str += getElementValue(val, inclusive);	
		}
	}
	
	return str;
	
	function tag(tag) {
		
		if(tag == "span" || tag == "a" || tag == "label" || tag == "b" || tag == "i") {
			return true;	
		}
		
		return false;
	}
	
	
}

/*
	@param inclusive - include text with style tags such as span
*/
function getChildElementValue(node, index, inclusive) {
	return getElementValue(getChildElement(node, index), inclusive);
}

/*

*/
function getChildElementTagName(node, index) {
	var val = getChildElement(node, index).tagName;
	
	if(typeof val != "undefined" && val != false) {
		return val;
	}
	
	return false;
}
