var atPopupWindow = null;
var IsPortalPage = false;
var isIE = window.ActiveXObject ? true : false;
var AJAXLoading = '<img src="ilib/wait.gif">&nbsp;Loading...';


(function($)
{
	$at.im = {

		init: function(options)
		{
			if (typeof options === 'number')
				options = {standbyInterval: options * 1000};
			$at.im.options = $.extend({standbyInterval: 0}, options);

			if ($at.im.options.standbyInterval > 0)
			{
				$(window).load(function()
				{
					$at.im.queueRefresh(500);
				});
			}
		},
		queueRefresh: function(timeout)
		{
			window.setTimeout(function()
			{
				$.post('ajax.aspx?bo=PageMgrLib.IMQueue', '', function(result, status, xhr)
				{
					if (result && result.StatusCode == '200')
					{
						$.each(result.Actions, $at.im.executeAction);
					}
					$at.im.queueRefresh($at.im.options.standbyInterval);
				}, 'json');
			}, timeout);
		},
		openWindow: function(fn, args)
		{
			var url = $at.page.getSitePath() + 'ajax.aspx?bo=PageMgrLib.IMQueue&fn=' + fn;
			if (args)
				url += '&' + args;
			var msgWin = open(url, 'im' + Math.round(Math.random()*100000),
					'toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=no,scrollbars=no,width=600,height=500');
			msgWin.opener = top;
			msgWin.focus();
		},
		executeAction: function(i, action)
		{
			var IMJoin = function(args)
			{
				$at.im.openWindow('join', args);
			};
			eval(action);
		},
		start: function(userId)
		{
			$at.im.openWindow('start', 'toUserId=' + userId);
		}
	};
})(jQuery);

var IMInit = $at.im.init;
var StartIM = $at.im.start;


function showhide(option){
if (document.getElementById){
	//debugger;
	var val = option.value;
	var current = option.current;
	var usr = document.getElementById('user');
	var all = document.getElementById('searchall');
	var web = document.getElementById('searchweb');
	var usecase = document.getElementById('usecase');
	var QTextUsr = document.getElementById('input_search_bar_user');
	var QTextUseCase = document.getElementById('input_search_bar_usecase');
	var QTextAll = document.getElementById('input_search_bar_all');

	try
		{
			var QTextWeb = document.getElementById('cse-search-box').q ; 		
		}
	catch(e)
		{}
				

	var QText = "";
	res = document.getElementById('as_input_search_bar_user');
	
	//disable old
	switch(current) {
		case "searchall":
		  QText = QTextAll.value;
		  all.style.display = "none"; 
		  break;
		case "user":
		  QText = QTextUsr.value;
		  usr.style.display = "none"; 
		  break;
		case "usecase":
		  QText = QTextUseCase.value;
		  usecase.style.display = "none"; 
		  break;	
		case "searchweb":
		  QText = QTextWeb.value;
		  web.style.display = "none";
		  break;

	}

	//enabled new	
	switch(val) {
		case "searchall":
		  QTextAll.value = QText;
		  all.style.display = "inline"; 
		  break;
		case "user":
		  QTextUsr.value = QText;
		  usr.style.display = "inline";
		  break;
		case "usecase":
		  QTextUseCase.value = QText;
		  usecase.style.display = "inline";
		  break;
		case "searchweb":
		  web.style.display = "inline";
		  if(QText != "") document.getElementById('cse-search-box').q.style.background = '#ffffff';
		  QTextWeb.value = QText;
		  
	}; 

	option.current = val;
	if (res != null) {
	    res.style.display = "none";
	}
	return(true);
} 
} 

function searchUser(frm) {
	frm.action = frm.action + '&p=' + frm['input_search_bar_user'].value + '*';
	frm['QText'].value = frm['input_search_bar_user'].value + '*';
	return true;

}


function HelpWindow(file,window) {
	msgWindow=open(file,window,'toolbar=no,location=no,directories=no,status=yes,menubar=no,resizable=yes,scrollbars=yes,width=950,height=700');
	if (msgWindow.opener == null) msgWindow.opener = self;
	msgWindow.focus();
}
function openFavorites(el) {
	var param="toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes,scrollbars=yes,width=380,height=700";
	param+=",left="+(screen.width-390)+",top=2";

	var w=open(HomeURL + '?bo=atFavorites.Tile&fn=list','favorites',param);
	w.opener=top;
	w.focus();
}
function goToURL(url) {
	if(url!=null && url.length) {
		url = ReplaceString(url,"&amp;","&");
		var w=top;
		if(parent.frames["main"]!=null) w=parent.frames["main"];
		w.location=url;
	}
}
function ReplaceString(strText,find,repl) {
	var tindex = strText.indexOf(find);
	while (tindex != -1){
		strText = strText.replace(find,repl);
		tindex = strText.indexOf(find);
	}
	return( strText );
}
function imgChange(ImageName, ImagePath) {
	document.images[ImageName].src=ImagePath;
}
function mailWindow(file, window) {
    if ((navigator.appName.toLowerCase()=="netscape") & (navigator.appVersion.substring(0,1)=="4")) {
        msgWindow=open(file,window,'resizable=yes,width=730,height=450');
        if (msgWindow.opener == null) msgWindow.opener = self;
    }
    else {
        msgWindow=open(file,window,'resizable=yes,width=600,height=450');
        if (msgWindow.opener == null) msgWindow.opener = self;
    }
	atPopupWindow = msgWindow;
}
function newWindow(file, window) {
    if ((navigator.appName.toLowerCase()=="netscape") & (navigator.appVersion.substring(0,1)=="4")) {
        msgWindow=open(file,window,'resizable=yes,width=730,height=450');
        if (msgWindow.opener == null) msgWindow.opener = self;
    }
    else {
        msgWindow=open(file,window,'resizable=yes,width=600,height=450');
        if (msgWindow.opener == null) msgWindow.opener = self;
    }
	atPopupWindow = msgWindow;
}
function newWindowOpt(file, window, opt) {
    if ((navigator.appName.toLowerCase()=="netscape") & (navigator.appVersion.substring(0,1)=="4")) {
        msgWindow=open(file,window,opt);
        if (msgWindow.opener == null) msgWindow.opener = self;
    }
    else {
        msgWindow=open(file,window,opt);
        if (msgWindow.opener == null) msgWindow.opener = self;
    }
	atPopupWindow = msgWindow;
}
function newBrowser(file, window) {
    if ((navigator.appName.toLowerCase()=="netscape") & (navigator.appVersion.substring(0,1)=="4")) {
        brsWindow=open(file,window,'resizable=yes,width=730,height=450,status=yes,toolbar=yes,location=yes,menubar=yes,directories=yes');
        if (brsWindow.opener == null) brsWindow.opener = self;
    }
    else {
        brsWindow=open(file,window,'resizable=yes,width=600,height=450');
        if (brsWindow.opener == null) brsWindow.opener = self;
    }
}
function sendToFriendFolder(c,p,s) {
	if(c==null) c=0; //CatId //required
	if(p==null) p=0;
	if(s==null) s=0;
	//var w=open(HomeURL + '?bo=atFavorites.Tile&fn=list','favorites',param);
	var link=HomeURL + "?bo=b6portal.wo&fn=SendToFriend&mode=0&c="+c;
	if(p) link+="&p="+p; //page //optional
	if(s) link+="&s="+s; //sort //optional
	newWindowOpt(link,"SendToFriend","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=600,height=450");
}
function sendToFriendTopic(t) {
	if(t==null) t=0; //TopicId //required
	var link=HomeURL + "?bo=b6portal.wo&fn=SendToFriend&mode=1&t="+t;
	newWindowOpt(link,"SendToFriend","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=600,height=450");
}
function MsgBox(txt) {
	alert (txt);
}
function check_box(box_obj) {
	box_obj.checked = true;
}
function StatusSet(txt)
{
    window.status = txt;  
}
function StatusClear()
{
    window.status = "";
}
function openWindow(file,target,options) {
	var msgWindow=open(file,target,options);
	if(msgWindow!=null) {
		if(msgWindow.opener==null) msgWindow.opener=self;
		msgWindow.focus();
	}
}
function moc(ParentID, RefID) {
	document.getElementById('page_menu_tab_' + ParentID).attributes['onclick'].nodeValue='';
	if (RefID.indexOf("?bo=") == -1) {
		location=HomeURL + '?bo=b6portal.wo&fn=Dir&c=' + RefID;
	} else {
		location=RefID;
	}

}

function atNavPaneRefreshCats(CatId)
{
	if (parent.atFrames)
	{
		if (parent.atFrames.NavPane)
		{
			var oNavPane = parent.atFrames.NavPane.NavPane1;
			var oNavItem = parent.atFrames.NavPane.atFindNavItem(/CatMenu\.aspx/i);
			var LastCat = parent.atFrames.RecentCat;
			if (oNavItem)
			{
				if (! oNavItem.active)
				{
					try
					{
						oNavItem.setactive();
						oNavItem.setiframe();
					}
					catch(e)
					{}
				}
				if (!CatId || (CatId != LastCat))
				{
					var oIframe = oNavPane.document.getElementById("_navpane_area");
					if (oIframe && oNavItem.iframe)
					{
						var IframeURL = oNavItem.url;
						IframeURL = IframeURL.replace(/\?c=\d*/, "");
						if (CatId)
						{
							IframeURL += "?c=" + CatId + "#c" + CatId;
						}
						//alert("Refresh Category [" + CatId + "]" + IframeURL);
						oNavItem.iframe.src = IframeURL;
					}
				}
			}
		}
	}
}

function atNavPaneRefreshTrans(TransId)
{
	if (parent.atFrames)
	{ 
		if (parent.atFrames.NavPane)
		{ 
			var oNavPane = parent.atFrames.NavPane.NavPane1;
			var oNavItem = parent.atFrames.NavPane.atFindNavItem(/myDbs\.Tile/i);
			if (oNavItem)
			{ 
				// Simulate mouse click to activate pane
				parent.atFrames.NavPane.atNavItemDirect(oNavItem, 'DOWN');
				//return;
			}
			
			var oIframe = oNavPane.document.getElementById("_navpane_area");
			if (oIframe && oNavItem)
			{ 
				if (oNavItem.iframe)
				{ 
					oIframe.src = oIframe.src;
				}
			}
		}
	}
}

function CheckFrameset(frameset_url)
{
	if (!parent.frames[0])
	{
		if (self.opener)
		{
			if (self.opener.atPopupWindow == self)
			{
				//self.opener.atPopupWindow = null;
				return;
			}
		}
		//alert("Reloading frameset " + frameset_url);
		top.location = frameset_url;
	}
}

function ReloadPage()
{
	top.location.reload();
}

var atLinkCaption;
var atLinkId;

function atInsertTopicCatLink(FieldName) {	
	//atLinkId = b6fm[FieldName];
	atLinkId = document.forms['b6fm'][FieldName];
	atLinkCaption = document.getElementById(FieldName + 'ZZZCaptionValue');
	var w=open("FrameExec.asp?bo=b6portal.wo&fn=LookupTopic&Editor=2", "lookup", "toolbar=no,status=yes,directories=no,menubar=no,resizable=yes,scrollbars=yes,scrollable=yes");
	w.focus();
}

function atInsertTopicCatLinkRef(Id,Caption) {
	atLinkId.value = Id;
	atLinkCaption.innerHTML = Caption;	
}



function getKeyCode(e)
{
 if (window.event)
    return window.event.keyCode;
 else if (e)
    return e.which;
 else
    return null;
}


function FormatNumber(hf,f,p) {	

	if(f.value != "")
		f.value = DecimalToNumber(f.value,p,hf);	
	else
		hf.value = "";

	//alert(hf.value);
}

function DocumentDirectEdit(sWebDavPath)
{
	try
	{
		eval(unescape("%45%64%69%74%44%6F%63%75%6D%65%6E%74%42%75%74%74%6F%6E%20%3D%20%6E%65%77%20%41%63%74%69%76%65%58%4F%62%6A%65%63%74%28%22%53%68%61%72%65%50%6F%69%6E%74%2E%4F%70%65%6E%44%6F%63%75%6D%65%6E%74%73%2E%31%22%29%3B"));
		if (EditDocumentButton)	
		   {
			  EditDocumentButton.EditDocument(sWebDavPath);	
			}	
	}
	catch (e)
	{
		alert('Direct Edit mode could not be started. MS Office 2003 is required to use Direct Edit.')
	}
}



var ie	= document.all
var ns6	= document.getElementById&&!document.all

var isMenu 	= false ;

var menuSelObj = null ;
var overpopupmenu = false;

function mouseSelect(e)
{
	var obj = ns6 ? e.target.parentNode : event.srcElement.parentElement;

	if( isMenu )
	{
		if( overpopupmenu == false )
		{
			isMenu = false ;
			overpopupmenu = false;
			document.getElementById('menudiv').style.display = "none" ;
			return true ;
		}
		return true ;
	}
	//return false;
	return true;
}

// POP UP MENU
function	ItemSelMenu(e)
{
	var	obj = ns6 ? e.target.parentNode : event.srcElement.parentElement;	

      menuSelObj = obj ;
      
	if (ns6)
	{
		document.getElementById('menudiv').style.left = e.clientX+document.body.scrollLeft;
		document.getElementById('menudiv').style.top = e.clientY+document.body.scrollTop;
	} else
	{
		document.getElementById('menudiv').style.pixelLeft = event.clientX+document.body.scrollLeft;
		document.getElementById('menudiv').style.pixelTop = event.clientY+document.body.scrollTop;
	}
	document.getElementById('menudiv').style.display = "";
	
	isMenu = true;
	return false ;
}


//IFrame Resizer


function resizeCaller() {
    var dyniframe=new Array();
    for (i=0; i<iframeids.length; i++){
        if (document.getElementById)
            resizePortletIframe(iframeids[i]);
    }
}

function resizePortletIframe(frameid){
    var currentfr=document.getElementById(frameid + "wait");
    if(currentfr!=null) currentfr.style.display="none";
    currentfr=document.getElementById(frameid);
    
    if (currentfr && !window.opera) {
            currentfr.style.display="block"
	    var height = 0;            
            try {        
                    if (currentfr.contentDocument && currentfr.contentDocument.body.offsetHeight) { //ns6 syntax
			var height = currentfr.contentWindow.innerHeight + currentfr.contentWindow.scrollMaxY; 
			//currentfr.height = currentfr.contentDocument.body.offsetHeight > 700 ? 700 :  currentfr.contentDocument.body.offsetHeight ; 
                    } else if (currentfr.Document && currentfr.Document.body.scrollHeight) { //ie5+ syntax
                        if(null == currentfr.Document.getElementById("webReportFrame")) {
				height = currentfr.document.body.scrollHeight > 700 ? 700 : currentfr.document.body.scrollHeight ;
			} else {
				height = currentfr.Document.getElementById("webReportFrame").scrollHeight + 24 + 32; //todo: detect toolbar, requires report viewer change
				//debugger;
			}
		    }

		    currentfr.height = height;	
		    currentfr.style.visibility = 'visible';
		    //alert(height);
            } catch (ex) {
                if (!document.all) 
                    document.getElementById(frameid).parentNode.innerHTML =  "Error: " + ex ;
                else                    
                    document.getElementById(frameid).parentNode.innerHTML =  ex.name + ": " + ex.message;
            }
    
            if (currentfr.addEventListener)
                currentfr.addEventListener("load", readjustIframe, false)
            else if (currentfr.attachEvent){
                currentfr.detachEvent("onload", readjustIframe) // Bug fix line
                currentfr.attachEvent("onload", readjustIframe)
            }
        }
}

function readjustIframe(loadevt) {
    var crossevt=(window.event)? event : loadevt
    var iframeroot=(crossevt.currentTarget)? crossevt.currentTarget : crossevt.srcElement
    if (iframeroot)
        resizePortletIframe(iframeroot.id);
}

function loadintoIframe(iframeid, url){
    if (document.getElementById)
        document.getElementById(iframeid).src=url
}

var iframeids;

function newCalWindow(file) {
        var window = "Calendar0";
        msgWindow = open(file, window, "resizable=yes,scrollbars=yes,width=750,height=550");
        if (msgWindow.opener == null) msgWindow.opener = self;
    }

function HideShow(id) {
	var o = document.getElementById(id);
	if (o)
	{
		if (o.style.display != "block")
			o.style.display = "block";
		else
			o.style.display = "none";
	}
}
function trimEncodedHash(sURL){
	if(sURL.length > 4)
	{
		if (sURL.substr(sURL.length-3, 3)=='%23')
		{
			return sURL.substr(0, sURL.length-3);
		}
		else
		{
			return sURL;
		}
	}
	else
	{
		return sURL;
	}
}
function toggle_directories(id, PageItemId)
{
	
	dicon = document.getElementById("toglleicon_" + id + "_" + PageItemId);
	dsubcats = document.getElementById("idCatsAfterFirstLevel_" + id + "_" + PageItemId);

	if (dicon)
	{
		if (dicon.src.indexOf("ilib/col_middle.gif")>0)
		{
			dicon.src = "ilib/exp_middle.gif";
		}
		else
		{
			dicon.src = "ilib/col_middle.gif";
		}
	}
	if (dsubcats)
	{
		if (dsubcats.style.display != "none")
		{
			dsubcats.style.display = "none";
		}
		else
		{
			dsubcats.style.display = "block";
		}
	}
}
function toggle_customprops()
	{
		mtvs = document.getElementsByTagName("tr")
		mticon = document.getElementById("mt_icon");
		if (mticon)
		{
			if (mticon.src.indexOf("ilib/left_col.gif")>0)
			{
				mticon.src = "ilib/left_exp.gif";
			}
			else
			{
				mticon.src = "ilib/left_col.gif";
			}
		}
		if (mtvs)
		{
			for (var i=0; i < mtvs.length; i++)
			{
				mtv=mtvs[i];
				if (mtv.className == "cl___topic_meta_value")
				{
					if (mtv.style.display != "none")
					{
						mtv.style.display = "none";
					}
					else
					{
						mtv.style.display = "block";
					}
				}
			}
		}
	};
function toggle_customprops1()
	{
		mticon = document.getElementById("mt_icon");
		if (mticon)
		{
			if (mticon.src.indexOf("ilib/left_col.gif")>0)
			{
				mticon.src = "ilib/left_exp.gif";
				$('.cl___topic_meta_value').show();
			}
			else
			{
				mticon.src = "ilib/left_col.gif";
				$('.cl___topic_meta_value').hide();
			}
		}
	};
	
var InitJQuery = new Array(1);
function AddInitJQuery(fn) {
    var c = {};
    c.name = fn;
    var i = InitJQuery.length ;
    InitJQuery[i] = fn;
    //InitJQuery.add(fn);
};

jQuery(function() {
    for(var i = 1, len = InitJQuery.length; i < len; i++){
        eval(InitJQuery[i]+'();');
    }
    
});


/**** Org.Network Portlet *****/


				function changeManTab(tab,pid)
				{
					dr = document.getElementById("dataDirectReports" + pid);
					sm = document.getElementById("dataSameManager" + pid);
					rc = document.getElementById("dataReportsToChain" + pid);
					cn = document.getElementById("dataConnections" + pid);
					cni = document.getElementById("dataIndirectConnections" + pid);
					tdr = document.getElementById("tdDirectReports" + pid);
					tsm = document.getElementById("tdSameManager" + pid);
					trc = document.getElementById("tdReportToChain" + pid);
					tcn = document.getElementById("tdConnections" + pid);
					tcni = document.getElementById("tdIndirectConnections" + pid);

					if (tab ==1)
					{
						dr.style.display = "block";
						sm.style.display = "none";
						rc.style.display = "none";
						cn.style.display = "none";
						cni.style.display = "none";

						tdr.className= "selected";
						tsm.className= "normal";
						trc.className= "normal";
						tcn.className= "normal";
						tcni.className= "normal";
						
						tdr.focus();

					}
					if (tab ==2)
					{
						dr.style.display = "none";
						sm.style.display = "block";
						rc.style.display = "none";
						cn.style.display = "none";
						cni.style.display = "none";
						
						
						tdr.className= "normal";
						tsm.className= "selected";
						trc.className= "normal";
						tcn.className= "normal";
						tcni.className= "normal";

						tsm.focus();

					}
					if (tab ==3)
					{
						dr.style.display = "none";
						sm.style.display = "none";
						rc.style.display = "block";
						cn.style.display = "none";
						cni.style.display = "none";
						
						tdr.className= "normal";
						tsm.className= "normal";
						trc.className= "selected";
						tcn.className= "normal";
						tcni.className= "normal";

						trc.focus();
					}
					if (tab ==4)
					{
						dr.style.display = "none";
						sm.style.display = "none";
						rc.style.display = "none";
						cn.style.display = "block";
						cni.style.display = "none";
						
						tdr.className= "normal";
						tsm.className= "normal";
						trc.className= "normal";
						tcn.className= "selected";
						tcni.className= "normal";

						tcn.focus();
					}
					if (tab ==5)
					{
						dr.style.display = "none";
						sm.style.display = "none";
						rc.style.display = "none";
						cn.style.display = "none";
						cni.style.display = "block";
						
						tdr.className= "normal";
						tsm.className= "normal";
						trc.className= "normal";
						tcn.className= "normal";
						tcni.className= "selected";

						tcni.focus();
					}
			
				}


// Setup cluetips for user info popups

(function($)
{
	var setupCluetips = function(page, container)
	{
		$at.require(['jquery.hoverIntent', 'jquery.cluetip']);
		try //temporary workaround because of cluetip error
		{
			$('a.UserInfo-hover-Popup', container).cluetip(
			{
				width: 280,
				showTitle: false,
				cluetipClass: 'clueTip',
				mouseOutClose: false,
				hoverIntent: {
					sensitivity: 3,
					interval: 400,
					timeout: 0
				}
			});
		}
		catch(e){}

		// Fix IE z-index bug
		$('div.PageSubTabs span.SubTabItem').each(function(i){
			this.style.zIndex = 9999 - i;
			if ($.browser.msie) this.style.zoom = 1;
		});
	};
	$(setupCluetips);
	if ($at.page.addEventHandler)
		$at.page.addEventHandler('load', setupCluetips);

	$at.page.topUserSearch = function(id, options)
	{
		var opts = $.extend(
		{
			script: 'ajax.aspx?bo=b6User.User&fn=usersearch&json=1&searchfields=wluserDisplayname,phone,wluseremail&infofields=wluseremail,phone&',
			varname: 'input',
			json: true,
			shownoresults: true,
			maxresults: 10,
			showmaxresults: true,
			cache: false,
			minchars: 2,
			timeout: 100000,
			dely: 150,
			callback: function (obj) {
				window.location=obj.url;
			}
		}, options);

		$(function()
		{
			new bsn.AutoSuggest(id, opts);
		});
	};


	$at.page.topUseCaseSearch = function(id, options)
	{
		var opts = $.extend(
		{
			script: 'ajax.asp?bo=b6User.User&fn=usecasesearch&json=1&',
			varname: 'input',
			json: true,
			shownoresults: true,
			maxresults: 10,
			showmaxresults: true,
			cache: false,
			minchars: 2,
			timeout: 100000,
			dely: 150,
			hideimage: true,
			callback: function (obj) {
				window.location=obj.url;
			}
		}, options);

		$(function()
		{
			new bsn.AutoSuggest(id, opts);
		});
	};

})(jQuery);

// JQueryMainGridTabs ***********
function addJQueryTab(url, caption) {
	var newIndDyn = $("#JQueryMainGridTabs").tabs("length")
	JQueryMainGridTabsCount = JQueryMainGridTabsCount + 1;
	var newIndMax = JQueryMainGridTabsCount;
   $("#JQueryMainGridTabs").tabs("add","#JQueryMainGridTab-" + newIndMax, caption);

   var newTabHTML =  "<iframe id='JQueryMainGridFrameTab-" + newIndMax + 
	   "' marginwidth='0' marginheight='0'frameborder=0 scrolling='no' height='800px' width='100%' src='" + url + "'></iframe>";
   $("#JQueryMainGridTab-" + newIndMax).append(newTabHTML);	 
   $("#JQueryMainGridTabs").tabs("select", newIndDyn);
}

function removeJQueryTab() {
   indx =$("#JQueryMainGridTabs").tabs('option', 'selected');
   $("#JQueryMainGridTabs").tabs("remove", indx);
   if (($("#JQueryMainGridTabs").tabs("length"))>0) $("#JQueryMainGridTabs").tabs("select",0);
}
//******************************
