

var search_defaultColor = "yellow";
function search_init()
{

    search_colorMarker();
	
}	
function search_colorMarker()
{
	//# get the phrase from the querystring
    var strPhrase = search_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 = ["#ffff99","#66ccff","#ffcc00","#99ff00","#ff6666"];

	//# if there are no items exit from the functions
	if ($get("search_tdItem") == null)
	{
		return;
	}
	
	///fixed
    var CheckNum = parseInt($get("search_tdItem").length);
    if(isNaN(CheckNum))
    {
        var obj = $get("search_tdItem");
        if(obj == null)
        {
           return;
        }

        search_loopWords(arrPhrase, arrColors, obj);
    }
	///end fixed

    for (var i = 0; i < $get("search_tdItem").length ; i++)
	{	
		//# get td obj to search in
		var obj = $get("search_tdItem")[i];
		if (obj == null)
		{
			return;
		}
		
		search_loopWords(arrPhrase, arrColors, obj);
	}	
	
	//# marking the title that describe the count of the total results
	var obj = $get(search_getUniqueID() + "tdResults");
	
	if(obj == null)
	{
		return;
	}
	
	var strRes = obj.innerHTML;
	var arrRes = strRes.split('"');
	
	if (arrRes.length > 1)
	{
		// put just the words
		obj.innerHTML = arrRes[1];
		
		// marking the words
		search_loopWords(arrPhrase, arrColors, obj);
		
		// add the string that remove before
		obj.innerHTML = arrRes[0] + ' "' + obj.innerHTML + '" ';
	}	
}

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 (search_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_strSearchUniqueID = "";
function search_getUniqueID()
{
	if (search_strSearchUniqueID != "")
	{
		return search_strSearchUniqueID;
	}

	var input = document.getElementsByName('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 search_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 "";
}

function search_trim(str)
{
	try {
		return str.replace(/^\s+|\s+$/g,'');
	}
	catch (e) {
		return str;
	}
}

function search_mouseoverItem(obj)
{
	obj.style.cursor = 'hand';
	obj.className = "search_item_on";
}

function search_mouseoutItem(obj)
{
	obj.className = "search_item_off";
}



