window.onload				= init;


function siteSearch()
{
	var strPhrase = document.getElementById("searchText").value;
	alert("search for :"+strPhrase);
}

function init()
{
	
	search_init();
	
	if(	init.active)	return;
		init.active	=	true;
	
	try
	{
		if(window.initKSForm) initKSForm();	

		if(window._markerLoaded_) _markerLoaded_(window.document,3);
		
		setfocus();
		done();
	}catch(e){}
}

//////////////////////
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;
}	

// <name> = 'faq' or 'tip', determines the dimension of the popup
function openForm(path, name)
{  
	var width = 400;
	var height = 260;
	var	dialogTop0	= parseInt(self.dialogTop,10);
	var dialogLeft0	= parseInt(self.dialogLeft,10);
	
	var	dialogTop	= self.event.clientY + dialogTop0 + 50;
	var dialogLeft	= self.event.clientX + dialogLeft0;
	
	switch (name.toLowerCase())
	{
		case 'faq':
			width = 400;
			height = 300;
			break;
		case 'tip':
			width = 400;
			height = 260;
			break;
		case 'friend':
			width = 400;
			height = 350;
			break;
		default:
			width = 400;
			height = 260;
	}
	window.open(path, "KSForm", "width="+width+",height="+height);
}

var search_defaultColor = "#FFCC00";

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," ");	
	strPhrase = strPhrase.replace(/\%20/gim," ");	
	var arrPhrase	= strPhrase
	
	
	// if there are no items exit from the functions
	if (document.all.search_content == null)
	{
		return;
	}
	//alert("document.all.search_tdItem.length :"+document.all.search_tdItem.length)
	if(isnull(document.all.search_content.length)){
		search_loopWords(arrPhrase, document.all.search_content);
	}
	else{
		for (var i=0 ; i<document.all.search_content.length ; i++)
		{	
			// get td obj to search in
			var obj = document.all.search_content[i];
			if(obj == null)
			{
				return;
			}
			
			search_loopWords(arrPhrase, obj);
		}	
	}
	
}

function search_loopWords(arrPhrase, obj)
{
	search_markText(arrPhrase, obj, search_defaultColor);
}

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;
	}
}
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 "";
}

function send_sendToAFriend(nPopupWinWidth, nPopupWinHeight, bPopupWinScrollbars, bPopupWinToolbar, nFolderID)
{
	// language
	var strLang = "HE";
	
	try
	{
		var arrLocation = String(location.href).split('/');
		var strParams = arrLocation[arrLocation.length-2];
		var arrParams = strParams.split('-');
		
		if (arrParams.length > 1)
		{
			strLang = arrParams[arrParams.length-1].toUpperCase();
		}
		else
		{
			var arrLocation = String(location.href).split('?');
	
			if (arrLocation.length == 2)
			{
				var arrParams = arrLocation[1].split('&');

				for (var i=0 ; i<arrParams.length ; i++)
				{
					var param = arrParams[i].split('=');

					if (param.length == 2)
					{
						if (param[0].toLowerCase() == "lang")
						{
							strLang = param[1].toUpperCase();
						}
					}
				}
			}
		}	
	}
	catch (e){}
	
	// folderid
	try
	{
		if (isNaN(nFolderID))
		{
			nFolderID = 0;
		}
	}
	catch (e)
	{
		nFolderID = 0;
	}
		
	nPopupWinTop = screen.height/2 - nPopupWinHeight/2;
	nPopupWinLeft = screen.width/2 - nPopupWinWidth/2;
	
	var arrLocation = String(document.links[0]).split('Templates');
	window.open(arrLocation[0] + 'TemplateControls/SendToAFriend/SendToAFriendTemplatePopup.aspx?folderid=' + nFolderID + '&lang=' + strLang, 'sendToFrient','top=' + nPopupWinTop + ',left=' + nPopupWinLeft + ',width=' + nPopupWinWidth + ',height=' + nPopupWinHeight + ',scrollbars=' + bPopupWinScrollbars + ',toolbar=' + bPopupWinToolbar);
}

function createFlashObj(objectString)
{
	document.write(objectString);
}
