

// Turns the input string into an XML object
function GetXmlDocument(pXml)
{
	var parser = new DOMImplementation();
	var domDoc = parser.loadXML(pXml);
	return(domDoc.getDocumentElement());
}


function PopulateControl(pControlId, pXmlPath, pXml)
{
	try
	{
		var Control = document.getElementById(pControlId);
		var XmlSearchPaths = pXmlPath.split(",");
		var i;
		var Value = "";
		var Buffer;
		for (i=0;i<XmlSearchPaths.length;i++)
		{
			Buffer = GetXmlPathValue(pXml, pXmlPath);
			if (Buffer != "")
			{
				Value = Buffer;
			}
		}
		// Do not blank it out... if the value is not overwritten, leave it as is.		
		
		if (Value != "")
		{
			PopulateControlWithValue(Control, Value);
		}
	}
	catch (exception)
	{
		alert(exception);	
	}
}

function PopulateControlWithValue(pControl, pValue)
{
	switch (pControl.tagName.toLowerCase())
	{
		case ("span"):
			pControl.innerHTML = pValue;
			break;
		case ("input"):
			pControl.value = pValue;
			break;
		case ("select"):
			pControl.value = pValue;
			break;
		case ("div"):
			pControl.innerHTML = pValue;
			break;
		default:
			alert("PopulateControlByValue:: Missing tag - " + pControl.tagName);
			break;
	}
}

function GetXmlPathValue(pInputXml, pPath)
{
	var FullPath = pPath.replace(/^\//,"");
	var PathElements = FullPath.split("/");
	
	var i;
	var OutputNode = pInputXml;
	
	
	for (i=1;i<PathElements.length;i++)
	{

		OutputNode = GetXmlNode(OutputNode, PathElements[i]);
		
	}

	if (OutputNode == null)
	{
		return("");
	}
	
	if (OutputNode.getFirstChild() == null)
	{
		return("");
	}
	
	return(OutputNode.getFirstChild().getNodeValue());			

}

// Function: GetXmlNode
// Description: Returns the first child with the corresponding tag
function GetXmlNode(pInputXml, pTag)
{				
	if (pInputXml == null)
	{
		return(null);
	}
	
	var ChildNode = pInputXml.getFirstChild();

	var found = false;
	for (;ChildNode != null; ChildNode = ChildNode.getNextSibling())
	{
		if (ChildNode.getNodeName() == pTag)
		{
			return(ChildNode);
		}
	}
	
	return(null);
}

// Function:	GetControlValue
// Args:		1 - Control's Id
function GetControlValue(pControl)
{
	var Value = "";

	if (pControl != null)
	{
		switch (pControl.tagName.toLowerCase())
		{
			case ("span"):
				Value = pControl.innerHTML;
				break;
			case ("input"):
				Value = pControl.value;
				break;
			case ("select"):
				Value = pControl.value;
				break;
			default:
				alert("GetControlValue: Missing case for tag - " + pControl.tagName);
				break;
		}
	}

	return(Value);
}


// Using this instead of the usual so that we can cater for radio buttons later.
function FindControl(pControlId)
{
	// Leave radio buttons for now.
/*	if (pControlId.search(/^RADIO_/) != -1)
	{
		var UnmaskedControlId = pControlId.replace(/^RADIO_/,"");
		var i = 0;
		var found = false;
		var Control = document.getElementById(UnmaskedControlId + "_" + i);
		while ((!found) && (Control != null))
		{
			if (Control.checked)
			{
				Value = Control.value;
				found = true;
			}
			i++;
			Control = document.getElementById(UnmaskedControlId + "_" + i);
		}
	}
	else
	{*/
/*	}*/

	return(document.getElementById(pControlId));
}


function OnEnter(e, evalCallback)
{
	var characterCode;
	if(e && e.which)
	{
		characterCode = e.which;
	}
	else
	{
		e = event;
		characterCode = e.keyCode;
	}
	if(characterCode == 13)
	{
		eval(evalCallback);
		return(false);
	}
	return(true);
}

function FormatUpDownControl(pControlId){

	var Control = FindControl(pControlId);
	if (Control != null)
	{
		var ControlValue = GetControlValue(Control);
		
		// Check for 0
		var NoNonNumerics = ControlValue.replace(/[^\d]/g,'');
		var NoZeros = NoNonNumerics.replace(/0/g,'');
		
		var ControlSpan = document.createElement("span");
		ControlSpan.innerHTML = ControlValue;
		
		Control.innerHTML='';
		
		if (NoZeros=='')
		{
			ControlSpan.className='unchanged';
			Control.appendChild(ControlSpan);
		}
		else
		{
			if (ControlValue.length>0)
			{
				if (ControlValue.match(/^-/))
				{
					ControlSpan.className='down';
					Control.appendChild(ControlSpan);
				}
				else
				{
					ControlSpan.className='up';
					Control.appendChild(ControlSpan);
				}
			}
		}
		
	}

}
