//////////////////////
function trim(str)
{
	try {
		return str.replace(/^\s+|\s+$/g,'');
	}
	catch (e) {
		return str;
	}
}
//////////////////////
function isnull(arg)
{
	arg = arg+'';
	return (arg == '' || arg == 'null' || arg == 'undefined');
}
//////////////////////
function nvl()
{
	for (var i=0; i<arguments.length; i++) {
		if (!isnull(arguments[i])) return arguments[i];
	}
	return null;
}	

///////////////////////////
// search

var search_defaultColor = "#FFCC00";

window.onload = function(){
	search_init()
	
	if(window._markerLoaded_) _markerLoaded_(window.document,3);
	
}

function search_init()
{
	
	search_colorMarker();
}	

function search_colorMarker()
{
	// get the phrase from the querystring
	var strPhrase = topmenu_getParam("searchText");
	if(strPhrase == "")
	{
		return;
	}
	
	strPhrase = strPhrase.replace(/\+/gim," ");	
	var arrPhrase	= strPhrase.split(" ");
	
	// create color array. each word will be marking in another color.
	// the proggramer can insert more color if he need.
	var arrColors = ["#FFCC00"];

	// if there are no items exit from the functions
	if (document.all.search_tdItem == null)
	{
		return;
	}
	//alert("document.all.search_tdItem.length :"+document.all.search_tdItem.length)
	if(isnull(document.all.search_tdItem.length)){
		search_loopWords(arrPhrase, arrColors, document.all.search_tdItem);
	}
	else{
		for (var i=0 ; i<document.all.search_tdItem.length ; i++)
		{	
			// get td obj to search in
			var obj = document.all.search_tdItem[i];
			if(obj == null)
			{
				return;
			}
			
			search_loopWords(arrPhrase, arrColors, obj);
		}	
	}
	
}

function search_loopWords(arrPhrase, arrColors, obj)
{
	// loop through all word and marking the word that found
	for(var i=0; i<arrPhrase.length; i++)
	{
		//if the word is empty, move next
		if (trim(arrPhrase[i]) == "")	
		{
			continue;
		}	
		
		// get current color. if there is no color, get default color.
		var strCurrColor = (i<arrColors.length?arrColors[i%arrColors.length]:search_defaultColor);
		
		// marking the word in the text
		search_markText(arrPhrase[i], obj, strCurrColor);
	}
}

function search_markText(strText, obj, strColor)
{
	
	// if there is no color, get default color
	if (strColor == "") 
	{
		strColor = search_defaultColor;
	}	
	
	var objPrev = [obj];

	// while previus level has items
	while(objPrev.length>0)
	{
		// current level items
		var objLevel = [];
		
		// loop previus level items
		for(var itemPrev in objPrev)
		{
			// get current level items
			var arrNodes = objPrev[itemPrev].childNodes;
			
			// collect text nodes
			var aLevelTextNodes = [];
			
			// loop current level items
			for(var i=0 ; i<arrNodes.length ; i++)
			{
				// get item reference
				var objItem = arrNodes.item(i);
				
				// if the td is not one that include search item move next
				
				
				// if text node
				if(objItem.nodeName == '#text')
				{
					aLevelTextNodes.push(objItem);
				}
				else
				{
					// not text node add to current level item array
					objLevel.push(objItem);
				}
			}	
			
			// loop all text nodes
			for(var iTextIndex in aLevelTextNodes)
			{
				// get item reference
				var objItem = aLevelTextNodes[iTextIndex];
				
				// search string first index
				var iFoundIndex = -1;
				
				// if text node contains search string
				if((iFoundIndex=String(objItem.nodeValue).indexOf(strText))>-1)
				{
					// loop while more instances
					while(iFoundIndex > -1)
					{
						
						// get start text node
						objItem = objItem.splitText(iFoundIndex);
						
						// create a span and insert before found text
						var oSpan =	document.createElement("SPAN");
						oSpan.style.background=strColor;
						objItem.parentNode.insertBefore(oSpan,objItem);
						
						// slice remaining text and insert to span
						var oSearchItem = objItem.splitText(strText.length);
						oSpan.appendChild(objItem);
						objItem = oSearchItem;
						
						// search on remaining text
						iFoundIndex=String(objItem.nodeValue).indexOf(strText);
					}
				}
			}
		}
		
		// set previus level item array
		objPrev = objLevel;
	}
}

var search_strLocation = "";
function search_getLocationPath()
{
	if (search_strLocation == "")
	{
		search_strLocation = document.all[search_getUniqueID() + "txtLocation"].value;
	}
	return search_strLocation;
}

var search_strSearchUniqueID = "";
function search_getUniqueID()
{
	if (search_strSearchUniqueID != "")
	{
		return search_strSearchUniqueID;
	}	
	
	var input = document.all.tags('INPUT');
	
	for (var i=0 ; i<input.length ; i++) 
	{
		for (var j in input[i])
		{
			if (input[i].id.indexOf("txtUcSearchID") != -1)
			{
				search_strSearchUniqueID = input[i].id.replace("txtUcSearchID", "");
				return search_strSearchUniqueID;
			}
		}	
	}
}

function topmenu_getParam(strParam)
{
	var url = document.URL.split('?');
	
	if (url.length != 2)
	{
		return "";
	}

	var params = url[1].split('&');

	for (var i=0 ; i<params.length ; i++)
	{
		var param = params[i].split('=');

        if (param.length != 2)
        {
			return "";
		}

		if (param[0].toLowerCase() == strParam.toLowerCase())
		{
			return param[1];
		}
	}

	return "";
}