﻿// JScript File
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
function searchbox_clickSearchImage(clientID, nFolderID, strLang, obj)
{
	// get search pharse
	var strSearchText = document.getElementById(clientID + "searchInput").value;
	
	// check for not empty pharse
	
	if (searchbox_isEmpty(strSearchText) == true)
	{
		alert(arrMessagesSearchBox['pharse_empty']);
		return;
	}
	
	// check for legal pharse
	if (searchbox_validPharse(strSearchText) == false)
	{
		alert(arrMessagesSearchBox['pharse_error']);
		return;
	}
	
	// get path
	var strLocationPath = searchbox_getLocationPath(clientID)		
	
	// params
	var params = "?SearchText=" + escape(strSearchText);
	
	
	//////////////////////////
	// NOT GENERIC SEARCH - START
	//////////////////////////
	
	
	
	
	//////////////////////////
	// NOT GENERIC SEARCH - END
	//////////////////////////
	
	//event.returnValue = false;
	//alert(strLocationPath + params);
	window.location = strLocationPath + params;
	
}

function searchbox_overSearchImage(obj)
{
	obj.src = searchbox_On.src;
	obj.style.cursor = 'hand';
}

function searchbox_outSearchImage(obj)
{
	obj.src = searchbox_Off.src;
}

function searchbox_isEmpty(strValue)
{

	// if the field value is empty, return true (error)
	if (strValue == "")
	{
		return true;
	}
	
	return false;	
		
}

var strNotLegalChars = "!@#$%^&~\?;:";
function searchbox_validPharse(strSearchText)
{
	for (var i=0 ; i<strSearchText.length ; i++)
	{
		if (strNotLegalChars.indexOf(strSearchText.charAt(i)) > -1)
		{
			return false;
		}
	}
	
	return true;
}

var searchbox_strLocation = "";
function searchbox_getLocationPath(clientID)
{
	if (searchbox_strLocation == "")
	{
		searchbox_strLocation = document.getElementById(clientID + "txtLocation").value;
		//var arrLocation = String(document.links[0]).split('Templates');
		//searchbox_strLocation = arrLocation[0];
	}
	return searchbox_strLocation;
}

function searchbox_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 Selectedchange(objValue)
{ 
    var arrMain =objValue.split(";");
    
    if (arrMain.length==3)
    {
        var arrExternal_Link = arrMain[0].split("=");
        var arrKs_link = arrMain[1].split("=");
        var arrNew_Window = arrMain[2].split("=");
        
        if (arrExternal_Link[1] != "")
        {
            if (arrNew_Window[1] == 1)
            {                
                window.open(arrExternal_Link[1],"_blank");
            }
            else
            {
              window.location = arrExternal_Link[1];            
            }
        }
        else if(arrKs_link[1] != "")
        {
            if (arrNew_Window[1] == 1)
            {
                window.open(arrKs_link[1],"_blank");
            }
            else
            {
              window.location = arrKs_link[1];            
            }
        }
    }
}
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
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 (document.all.search_tdItem == null)
	{
		return;
	}
	
	///fixed
    var CheckNum = parseInt(document.all.search_tdItem.length);
    if(isNaN(CheckNum))
    {
        var obj = document.all.search_tdItem;
        if(obj == null)
        {
           return;
        }

        search_loopWords(arrPhrase, arrColors, obj);
    }
	///end fixed

	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);
	}	
	
	// marking the title that describe the count of the total results
	var obj = document.all[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.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 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";
}



