/*
	--------------------------------------------------------------------------------------								
	Name	: RemoveSearchNode	
	
	Purpose	: This function will
			1. Remove the node with the matching nodename from Search XML
			2. Sends a Javascript RPC request to server
			3. Update the contents of SummaryList Control based on the latest search XML			
	--------------------------------------------------------------------------------------
*/

function RemoveSearchNode(nodeName)
{	
	//var lstSummary = document.all[_sSummaryListboxID];
	var lstSummary = document.getElementById("lstSummary");
	
	//	Only proceed if there are any items inside the Summary listbox
	if(lstSummary.options.length == 0)
		return;
	
	var oRootNode = SearchXml.childNodes[0];

	//	Make sure this node exists in the summary XML (which inturn means it exists in the option box)
	//	This simple check will avoid the costly iteration the follows next for those options that dont 
	//	exists in the summary box
	if(oRootNode.selectSingleNode(nodeName) != null)
	{			
		var oSummaryOptions = lstSummary.options;
		var sNodeText = oRootNode.selectSingleNode(nodeName).getAttribute("text")
			
		for(var i=0; i<oSummaryOptions.length; i++)
		{				                            //this tail end is for removing Open House nodes when OH is not selected..
			if(oSummaryOptions[i].value == nodeName || (oSummaryOptions[i].text.indexOf(sNodeText)>=0 && sNodeText.indexOf("Open Houses")>=0) )//oSummaryOptions[i].text == sNodeText)
			{
				RemoveSummaryOption(lstSummary, oRootNode, oSummaryOptions[i]);
				break;
			}
		}		
	}	
}

function CreateNode(nodeName)
{
	var oCurrNode = null;
	
	if(spm_browserType() == "ie")
		oCurrNode = SearchXml.createElement(nodeName);
	else
		oCurrNode = SearchXml.ownerDocument.createElement(nodeName);	
		
	return oCurrNode;
}

function AddParentNode(nodeName, displayName, controlType, ctrlID, category, text, value)
{
	//	Get the root XML node
	var oRootNode = SearchXml.childNodes[0];
		
	//	Get the node the corresponds to the passed-in nodename
	var oCurrNode = oRootNode.selectSingleNode(nodeName);
	
	if(oCurrNode == null)
	{
		//	Create a new node object
		oCurrNode = CreateNode(nodeName);
	
		//	Set attributes
		oCurrNode.setAttribute("displayName", displayName);
		oCurrNode.setAttribute("controlType", controlType);
		oCurrNode.setAttribute("category", category);	
		oCurrNode.setAttribute("sortValue", _nSortValue);
			
		if(ctrlID != null)
			oCurrNode.setAttribute("controlID", ctrlID);			
						
		// Append it as a child to root node								
		oRootNode.appendChild(oCurrNode);
	}	
	
	if(text != null && value != null)
	{
		oCurrNode.setAttribute("text", text);
		oCurrNode.setAttribute("value", value);
		oCurrNode.setAttribute("controlID", ctrlID);
		oCurrNode.setAttribute("displayName", displayName);			
	}
	
	return oCurrNode;
}	

function AddParentNode2(nodeName, displayName, controlType, ctrlID, category, text, value, parentID)
{
	var oParNode = AddParentNode(nodeName, displayName, controlType, ctrlID, category, text, value);
	oParNode.setAttribute("position", parentID);
	
	return oParNode;
}	

function AddChildNodes(parentNode, ctrl, donotRemove)
{
	//	Remove all the child nodes
	if(!donotRemove)
	{
		var oChildNodes = parentNode.childNodes;		
		while(oChildNodes.length > 0)
		{
			parentNode.removeChild(oChildNodes[0]);
		}
	}
	
	if(ctrl.type == "text")
	{
		ctrl.value = ctrl.value.removeSpaces();		

		if(ctrl.value.length > 0)
		{
			//	Add new child node for every value
			var aValues = ctrl.value.split(",");		
			for(var i=0; i<aValues.length; i++)
			{
				AddChildNode(parentNode, ctrl.id, aValues[i], aValues[i], i);
			}
		}				
	}
	else if(ctrl.type == "select-multiple")
	{
		//	Add new child node for every selected item
		for(var i=0; i<ctrl.options.length; i++)
		{
			if(ctrl.options[i].selected == true)
			{
				if(donotRemove && parentNode.selectSingleNode("child[@value='" + ctrl.options[i].value + "']") != null)
					continue;

				AddChildNode(parentNode, ctrl.id, ctrl.options[i].text, ctrl.options[i].value, i);
			}
		}
	}		

	//	Remove parent node from DOM tree, if there are no childs
	if(parentNode.childNodes.length == 0)
	{
		SearchXml.childNodes[0].removeChild(parentNode);
	}		
}	

function AddChildNode(parentNode, ctrlID, text, value, position, parentID)
{
	//	Create a new node object
	var oChildNode = CreateNode("child");	
	
	//	Set attributes
	oChildNode.setAttribute("controlID", ctrlID);
	oChildNode.setAttribute("position", position);		
	oChildNode.setAttribute("text", text);
	oChildNode.setAttribute("value", value);
	oChildNode.setAttribute("parentID", parentID);
	
	parentNode.appendChild(oChildNode);
	
	return oChildNode;		
}

function RemoveChildNodeByValue(parentNode, ctrlID)
{
	//	Remove child node from the current parent node
	var oChildNode = parentNode.selectSingleNode("child[@controlID = '" + ctrlID + "']");			
	if(oChildNode != null)
		parentNode.removeChild(oChildNode);	
	
	//	Remove parent from DOM tre if it doesn't contain any childs 
	RemoveNodeIfEmpty(parentNode);
}

/*
function RemoveChildNodeByPosition(parentNode, childPosition)
{
	//	Remove child node from the current parent node	
	var oChildNode = parentNode.selectSingleNode("child[" + childPosition + "]");		
	parentNode.removeChild(oChildNode);	
			
	//	Remove parent from DOM tre if it doesn't contain any childs				
	RemoveNodeIfEmpty(parentNode);		
}
*/

function RemoveChildNodeByPosition(parentNode, childPosition)
{
	//	Remove child node from the current parent node	
	var oChildNode = parentNode.selectSingleNode("child[@position='" + childPosition + "']");		
	parentNode.removeChild(oChildNode);	
			
	//	Remove parent from DOM tre if it doesn't contain any childs				
	RemoveNodeIfEmpty(parentNode);		
}	

function RemoveChildNodeByName(parentNode, childNodeName)
{
	var oChildNode = parentNode.selectSingleNode(childNodeName);
	if(oChildNode != null)
		parentNode.removeChild(oChildNode);		
}

function RemoveNodeIfEmpty(node)
{
	//	Remove node from DOM tree, if there are no childs
	if(node.childNodes.length == 0)
	{
		SearchXml.childNodes[0].removeChild(node);
	}		
}

/*
function GetChildNodeByPosition(parentNode, childPosition)
{
	return parentNode.selectSingleNode("child[" + childPosition + "]");
}
*/

function GetChildNodeByPosition(parentNode, childPosition)
{
	return parentNode.selectSingleNode("child[@position='" + childPosition + "']");
}

if( document.implementation.hasFeature("XPath", "3.0") )
{
	XMLDocument.prototype.selectNodes = function(cXPathString, xNode)
	{
		if( !xNode ) { xNode = this; } 

		var oNSResolver = this.createNSResolver(this.documentElement)
		var aItems = this.evaluate(cXPathString, xNode, oNSResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
		var aResult = [];
		for( var i = 0; i < aItems.snapshotLength; i++)
		{
			aResult[i] =  aItems.snapshotItem(i);
		}
		
		return aResult;
	}
	XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode)
	{
		if( !xNode ) { xNode = this; } 

		var xItems = this.selectNodes(cXPathString, xNode);
		if( xItems.length > 0 )
		{
			return xItems[0];
		}
		else
		{
			return null;
		}
	}

	Element.prototype.selectNodes = function(cXPathString)
	{
		if(this.ownerDocument.selectNodes)
		{
			return this.ownerDocument.selectNodes(cXPathString, this);
		}
		else{throw "For XML Elements Only";}
	}

	Element.prototype.selectSingleNode = function(cXPathString)
	{	
		if(this.ownerDocument.selectSingleNode)
		{
			return this.ownerDocument.selectSingleNode(cXPathString, this);
		}
		else{throw "For XML Elements Only";}
	}
}