window.onload				= init;

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)
{
	var redirectUrl	= getHostAddress() + "/TemplateControls/SendToAFriend/SendToAFriendTemplatePopup.aspx";
	nPopupWinTop = screen.height/2 - nPopupWinHeight/2;
	nPopupWinLeft = screen.width/2 - nPopupWinWidth/2;
	
	var arrLocation = String(document.links[0]).split('Templates');
	var currLocation	=	window.location.href;
	redirectUrl	=	redirectUrl + "?linkURL=" + currLocation;
	window.open(redirectUrl, 'sendToFriend','top=' + nPopupWinTop + ',left=' + nPopupWinLeft + ',width=' + nPopupWinWidth + ',height=' + nPopupWinHeight + ',scrollbars=' + bPopupWinScrollbars + ',toolbar=' + bPopupWinToolbar);
}

//''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
function getHostAddress()
{
	var	urlPrefix		= getUrlPrefix();
	lastSlashPos		= urlPrefix.lastIndexOf('/');
	hostAddress			= urlPrefix.substring(0,lastSlashPos);
	
	return hostAddress;
}

function getUrlPrefix()
{
	// cut all the url file & query suffix
	var	urlPath			= location.href.substring(0,location.href.indexOf('.aspx'));
	var lastSlashPos	= urlPath.lastIndexOf('/');
	var urlPrefix		= urlPath.substring(0,lastSlashPos);	
	
	return urlPrefix;
}

function getQueryFields()
{
	var newQuery	=	"";
	
	// get the relevant section from the url
	var	urlPrefix	= getUrlPrefix();
	var queryFields = urlPrefix.substring(urlPrefix.lastIndexOf('/')+1,urlPrefix.length);	
	var fieldsArr	= new Array();
	fieldsArr		= queryFields.split("-");
	
	if (fieldsArr.length > 2)
	{
		newQuery	=	"FolderID=" + fieldsArr[0] + "&docID=" + fieldsArr[1] + "&lang=" + fieldsArr[2];				
	}
	else if (fieldsArr.length == 2)
	{
		newQuery	=	"FolderID=" + fieldsArr[0] + "&lang=" + fieldsArr[1];
	}
	
	return newQuery;
	
}

function OnPoint(obj)
{
	if(obj.sel != 1) obj.src = obj.on_src
}
function OffPoint(obj)
{
	if(obj.sel != 1) obj.src = obj.off_src
}

function ShowPointInfo(ind,len,imglen)
{
	ShowParentDiv(ind);
	var obj = document.getElementById("ucMap_MapPointImage_"+ind);
	if(obj && obj.sel == 1) return

	for(var i = 0; i < imglen; i++)
	{
		var obj = document.getElementById("ucMap_MapPointImage_"+i);
		if(obj)
		{
			if(i==ind)
			{
				obj.src = obj.on_src
				obj.sel = 1
			}
			else
			{
				obj.src = obj.off_src
				obj.sel = 0
			}
		}
	}
	
	function ShowHideInfo(ind,id)
	{
		
		var obj1 = document.getElementById(id);

		if(obj1)
		{
			if(obj1.MapPoint==String(ind))
			{
				obj1.style.display='inline'
			}
			else
			{
				obj1.style.display='none'
			}
		}
		
		var objMap=document.getElementById("ImageMapText");
		objMap.style.display='none';
	}
	
	for(var i = 0; i < len; i++)
	{
		ShowHideInfo(ind,"ucMap_ImagePointInfoList__ctl"+i+"_tblImagePointInfoItem")
		ShowHideInfo(ind,"ucMap_ImagePointInfoList__ctl"+i+"_labLinkedDocs")
	}
}

function ShowParentDiv(ind)
{
	var myDiv = 'divParentHide';
	var divCollection = window.document.getElementsByTagName("DIV");
	for(var i=0; i<divCollection.length; i++)
	{
		if(myDiv == divCollection[i].myParamHide)
		{
			divCollection[i].style.display = 'none';
		}
	}

	myDiv = 'divParent'+ind;
	var divCollection = window.document.getElementsByTagName("DIV");
	for(var i=0; i<divCollection.length; i++)
	{
		if(myDiv == divCollection[i].myParam)
		{
			divCollection[i].style.display = 'block';
			break;
		}
	}
}
