/**
 * getParentNode - Return the parent node of a certain type
 */
function getParentNode(node, parentType)
{
	var oParent = false;
	parentType = parentType.toUpperCase();
	while (node.parentNode && !oParent)
	{
		node = node.parentNode;
		if (node.nodeName.toUpperCase() == parentType)
		{
			oParent = node;
			break;
		};
	};
	
	return oParent;
};


/**
 * autoTab() - Tab from one field to another
 *
 * @argument {Object} formID - ID of the form to search for fields in
 * @argument {String} fieldID1 --- fieldID(N) - Pass field IDs in order to tab
 * @return {Boolean} - true on success. false on any failure
 */
function autoTab(formID)
{
	var oForm = document.getElementById(formID) || false;
	if (!oForm) { oForm = document.forms[document.forms.length - 1] || false; };
	if (!oForm) { alert('autoTab - no form'); return false; };

	var oNode = false;
	
	for (var i = 1; i < arguments.length; i++)
	{
		oNode = eval('oForm.' + arguments[i]) || false;
		if (!oNode) { continue; };		
		
		if ((i + 1) < arguments.length)
		{
			oNode.onkeyup = function()
			{
				var e = (arguments.length > 0) ? arguments[0] : window.event;
				var sKeyCode = (e.which) ? e.which : e.keyCode;								
				var isGoodKeyCode = false;	// will be good if it is numberic or a letter
				// 0-9
				if (!isGoodKeyCode && (sKeyCode >= 48 && sKeyCode <= 57)) { isGoodKeyCode = true; };
				// numPad 0-9
				if (!isGoodKeyCode && (sKeyCode >= 96 && sKeyCode <= 105)) { isGoodKeyCode = true; };
				// a-z
				if (!isGoodKeyCode && (sKeyCode >= 65 && sKeyCode <= 90)) { isGoodKeyCode = true; };
				
				if (!isGoodKeyCode) { return; };											
				
				// keycodes 48-90 are for 0-9 and a-z
				if (isGoodKeyCode && (this.value.length == this.maxLength))
				{
					// get next sibling input element
					nextNode = this.nextSibling;
					var tmpCount = 0;
					while (nextNode.nodeName != 'INPUT' && tmpCount < 10)
					{
						tmpCount++;
						nextNode = nextNode.nextSibling;
					};
					if (nextNode.nodeName == 'INPUT')
					{
						try {
							nextNode.select()
						} catch(e) { nextNode.focus(); };
					};
				};
			};
		};
		
		if (oNode.value == oNode.defaultValue)
		{
			
		};
	};
	
	return true;
};


function selectAll(elName)
{
	var doSelect = (arguments.length > 1) ? arguments[1] : true;	// Select or de-select
	var aNodes = document.getElementsByTagName('INPUT');
	var thisNode;
	for (var i = 0; i < aNodes.length; i++)
	{
		thisNode = aNodes[i];
		if (thisNode.name == elName)
		{
			thisNode.checked = doSelect;
			if (thisNode.onclick)
			{
				thisNode.onclick();
			};
		};
	};
	
	return true;
};

var win;
var img;
function viewImage(sURL)
{
	img = new Image();
	img.src = sURL;
	
	
	var opts = 'location=0,menubar=0,status=0,resizable=1,scrollbars=1';
	
	var width = 500;
	var height = 500;
	var winName = sURL.split('/');
	winName = 'viewImage' + winName[winName.length - 1];
	winName = winName.replace(/./g, '');
	
	opts += ',width=' + width;
	opts += ',height=' + height;

	window.open(gbl_appConfig.rootURL + 'xhr/viewImage/?img=' + sURL, winName, opts);
		
	return false;	// Make sure link does not proceed to open.
};


function showHide(elID)
{
	var oIcon = (arguments.length > 1) ? arguments[1] : false;
	var o = document.getElementById(elID) || false;
	if (!o) { return false; };
	var doHide = (o.style.display != 'none');
	
	o.style.display = (doHide) ? 'none' : '';	
	if (oIcon)
	{
		if (oIcon.nodeName != 'IMG')
		{
			var newIcon = false;
			if (typeof oIcon.childNodes != 'undefined')
			{
				for (var i = 0; i < oIcon.childNodes.length; i++)
				{
					newIcon = oIcon.childNodes[i];
					if (newIcon.nodeName == 'IMG')
					{
						break;
					};
				};
			};
			if (newIcon.nodeName == 'IMG') 
			{
				oIcon = newIcon;
				if (doHide)
				{
					oIcon.src = oIcon.src.replace('_contract', '_expand');
				}
				else
				{
					oIcon.src = oIcon.src.replace('_expand', '_contract');
				};
			};
			
		};
	};
};

