/**
*	Intranet scripts
**/

/** VARIABLES **/
var documentCategoriesDirectLinks = [];
var lastItemClicked=null;
var surveyEvaluationsLinks = [];

/** -------------------------- **/
if(isNS())
{
	window.captureEvents(Event.RESIZE);
}
window.onresize=resizeOuterTable;


function resizeOuterTable()
{
	
	if (isMS()) return;
	return;
	
	
	var marginH, marginB;
	marginB = 40;
	marginH = 35;
	document.getElementById("cmsOuterTable").style.width=getWindowWidth()-marginB+ "px";
	document.getElementById("cmsOuterTable").style.height=getWindowHeight()-marginH+ "px";					
}
function displayGlobalSearchOptions(ahref)
{
	var pos = getObjectPosition(ahref);
	var divGlobalSearchOptions = Obj("search_options");
	var divBg = $("search_options_bg");
	if(divGlobalSearchOptions.style.display=="none") 
	{
		Obj("search_options").style.display = "";
		var width = divGlobalSearchOptions.offsetWidth;
		ahref.innerHTML = GetString ("Intranet_SearchOptions") + " &lt;&lt;&lt;";
		divGlobalSearchOptions.style.left = divBg.style.left= pos[0]+ ahref.offsetWidth - width;
		divGlobalSearchOptions.style.top = divBg.style.top = pos[1] + 23;
		divBg.style.width = divGlobalSearchOptions.offsetWidth;
		divBg.style.height = divGlobalSearchOptions.offsetHeight;
		divBg.style.display = "";
		addEvent(document.body, "onclick", hideGlobalOptionsPanel);
		
	}
	else
	{
		divGlobalSearchOptions.style.display = divBg.style.display  = "none"
		
		ahref.innerHTML = GetString ("Intranet_SearchOptions") + " &gt;&gt;&gt;";
		removeEvent(document.body, "onclick", hideGlobalOptionsPanel);
	}
}
function hideGlobalOptionsPanel(e)
{
	if(!e)return true;
	var src = getEventSource(e);
	if(!src) return true;
	
	if(src!=$("search_options") && src!=$("linkDisplayGlobalSearchOptions") && !isChildOf(src, $("search_options")))
	{
		Obj("search_options").style.display = $("search_options_bg").style.display = "none";
		
	}
	return true;
}
var showOnce = 0;

function hightlightSearchString(searchObject, searchStrings)
{
	if(searchObject!=null && searchStrings!=null)
	{
		searchStrings = searchStrings.replace(" ",",");
		searchStrings = searchStrings.split(",");
		
		for ( var i=0; i < searchStrings.length; i++)
		{
			
			var tr = (document.createRange ? document.createRange () : document.body.createTextRange());
			if (tr.moveToElementText)
				tr.moveToElementText(searchObject);
			else
				tr.selectNode (searchObject);
				
			while(tr.findText(searchStrings[i]))
			{
				/* Exclude some elements: */
				//TH-cells
				if(tr.parentElement()!=null && 
					tr.parentElement().tagName=="TH")
				{
					tr.move("word",1);
					continue;
				}
				//Titles
				else if (tr.parentElement()!=null && 
					tr.parentElement().className=="item_category")
				{
					tr.move("word",1);
					continue;
				}
				//Download-Link
				else if (tr.parentElement()!=null && 
					tr.parentElement().innerHTML=="Download")
				{
					tr.move("word",1);
					continue;
				}
					
				showOnce++;
				if(showOnce==1)
				{
					//alert(getAttributes(tr.parentElement()));
					//alert(tr.parentElement().className);
				}
				tr.execCommand("BackColor",false, "#87cefa");
				tr.move("word",1);
			}
		}
	}
}
function showSearchResults (button, divId)
{
	if(Obj(divId)==null) return;
	if(Obj(divId).style.display=="none") //Anzeigen
	{
		button.value = "<<<";	
		button.title = GetString ("Intranet_Hide");
		Obj(divId).style.display="";
		
		//Highlighting
		var searchStrings = Obj("global_search_string").value;
		hightlightSearchString (Obj(divId), searchStrings);
		
		
	}
	else
	{
		button.value = ">>>";	
		button.title = GetString ("Intranet_Display");
		Obj(divId).style.display="none";
	}
}
function changeIntranetHospital(option)
{
	var hospital_guid = option.value;
	
	if(hospital_guid==null || hospital_guid=="") return;
	var pathName = document.location.pathname;
	if (String (pathName).toLowerCase (). endsWith ("intranetsearch.aspx"))
		pathName = "intranetNews.aspx";
		
	document.location.href = pathName +  "?change_hospital=" + hospital_guid;

}



function addID(id)
{
	var tb = document.getElementById("MyDocumentExplorer");
	if (tb)
	{
		
		if (id == 'move')
		{
			var select = document.getElementById("ddlCategories");
			tb.value += select.value + ";";
			tb.value += id;
			//alert(tb.value);
		}
		else
		{
			//if the element exists remove it
			if (tb.value.indexOf(id) >-1)
			{
				tb.value=tb.value.replace(id+';','');
			
			}
			else
			{
				tb.value += id + ';';
			}
			
			
		}
	
	}
}

function confirmSubmit(question)
{
	var agree=confirm(question);
	if (agree)
	{
		
		return true ;
	}
	else
	{
		var tb = document.getElementById("MyDocumentExplorer");
		if (tb)
		{
			tb.value = '';
		}
		checkUncheckAll('false');
		return false ;
	}
}

function checkUncheckAll(checked) 
{
if (checked == false){checked = true}else{checked = false}
var panel = document.getElementById('start_form');
for (var i = 0; i < document.getElementById('start_form').elements.length; i++) 
{
	document.getElementById('start_form').elements[i].checked = checked;
		if (checked != false)
		{
			var el = document.getElementById('start_form').elements[i];
			if (el.type == "checkbox")
			addID(el.name);
			}
		
}

return false;
}

function ResizePanel()
{
	var panelDocuments = document.getElementById('ExplorerFrame');
	if (!panelDocuments) return;
	
	var p = getObjectPosition3(panelDocuments)[1];
	var height = getClientSize()[1];
	
	panelDocuments.style.height = String(height - p) + "px";
}

function SelectFirstNode()
{
var myNode = findObj('treeview');
highlightObjLink(myNode);

}
var virtualPath = getVirtualPath();
if(!virtualPath) virtualPath = "/qm/";
function showItemPreview(ahref, item_id,idName,mode,form_guid)
{
	if (lastItemClicked!=null)
		lastItemClicked.style.fontWeight="normal";
	ahref.style.fontWeight="bold";
	lastItemClicked=ahref;
	var path = virtualPath + "view.aspx";
	if(mode==4 ||mode==10)
	{
		path = virtualPath + "intranet/documents.aspx";
	}
	else if (mode==3) //Events
	{
		path = virtualPath + "intranet/event.aspx";
	}
	else if (mode==8) //Jobs 
	{
		path = virtualPath + "intranet/job.aspx";
	}
	else if (mode == 5) // Phonelist
	{
		path = virtualPath + "phonelist_popup.aspx";
	}
	
	var win = window.open(path + "?popup=true&mode=" + mode + "&" + idName + "=" + String(item_id)+
		(form_guid?"&form_guid=" + form_guid:""),
		"PreviewItem", 
		(!form_guid?"width=600,height=500":"") + ",scrollbars=yes,resizable=yes");
	
}

function switchDocumentsMode(btn)
{
	if(btn == $("btnModeSimple"))
	{
		document.location.href="documents.aspx?mode=4";
	}
	else
	{
		document.location.href="documents.aspx?mode=10";
	}
}
function openPhotoGallery(gallery_guid, search_string)
{
	
	var win = window.open ("intranet/photogallery.aspx?gallery=" + gallery_guid + "&search_string=" + search_string, "PhotoGallery"+ gallery_guid.replace(/-/ig,""));
	win.focus();
}

function printVersionPhoneList()
{
	var str="";
	if (Obj("hidden_department")!=null && Obj("hidden_department_name")!=null)
		str+="&department=" + Obj("hidden_department").value +
			"&department_name=" + Obj("hidden_department_name").value;			
	if (Obj("hidden_kh")!=null && Obj("hidden_kh_name")!=null)
		str+="&hospital_idx=" + Obj("hidden_kh").value +
			"&hospital_name=" + Obj("hidden_kh_name").value;			
	
	//var win=window.open("intranet/phonelist.aspx?user_id=" + Obj("user_id").value + "&document=employees" + str,"PrintVersion","width=650,height=600,scrollbars=yes,toolbar=no,location=no");
	var win=window.open("intranet/phonelistPrint.aspx",
		"PhonelistPrintVersion","width=650,height=600,scrollbars=yes,resizable=yes,toolbar=yes,location=no,menubar=yes");
	win.focus();
	return false;
}


//Open object popup
function displayDocumentInfo (documentGuid)
{
	var virtualPath = getVirtualPath ();


	var win = window.open(virtualPath + "intranet/documentInfo.aspx?doc_guid=" + String(documentGuid),
		"Document",
		"width=800,height=600,scrollbars=yes,resizable=yes");
}

function highlightSelectedTopMenu (highghlightCellIndex)
{
	var tableNavigation = $("tableIntranetNavigation");
	if (!tableNavigation) return;
	
	//Get the row with highlight cells 
	var highlightRow = tableNavigation.rows[1];
	if (!highlightRow) return;
	
	var highlightCell  = highlightRow.cells [highghlightCellIndex];
	if (!highlightCell) return;
	highlightCell.className +=" selected";
}
function displayCategoryInfo (CategoryGuid)
{
	var win = window.open (getVirtualPath () + "/DocumentManagement/DocumentExplorer.aspx?compound=" + CategoryGuid, 
		"DocmentCategory", "width=1000,height=700,scrollbars=yes,resizable=yes");
	win.focus();
}


function getContentPrintversion ()
{
	var printArea = $("currentHomepageContents");
	if (!printArea) return;
	window.printPreviewObject = printArea;
	var printWindow = window.open(getVirtualPath() + "intranet/print.htm", 
			"PrintPreview", "width=800,height=600,resizable=yes,scrollbars=yes");
	printWindow.focus();
}

function getContentRtfVersion ()
{
	
	var printArea = $("currentHomepageContents");
	if (!printArea) return;
	
	
	var dispatcher = $("rtfVersionDispatcher");
	if (!dispatcher) {
		var div = document.createElement ("DIV");
		div.style.display = "none";
		div.innerHTML = "<iframe style=\"display:none\" id=\"rtfVersionDispatcher\" name=\"rtfVersion\"></iframe>";
		
		document.body.appendChild (div);
		dispatcher = $("rtfVersionDispatcher");
	}
	
	
	
	var dispatcherForm = $("rtfVersionDispatcherForm");
	if (!dispatcherForm) {
		dispatcherForm = document.createElement ("FORM");
		dispatcherForm.id  = "rtfVersionDispatcherForm";
		dispatcherForm.method = "POST";
		dispatcherForm.action ="intranet/rtfVersion.aspx";		
		dispatcherForm.style.display = "none";
		dispatcherForm.target = "rtfVersion";
		document.body.appendChild (dispatcherForm);
		var hidden = document.createElement ("INPUT");
		hidden.name = "contents";
		hidden.type = "hidden";
		dispatcherForm.appendChild  (hidden);
		
		hidden = document.createElement ("INPUT");
		hidden.name = "title";
		hidden.type = "hidden";
		dispatcherForm.appendChild  (hidden);
	}
	
	
	
	
	//Get title container
	var titles = document.getElementsByClassName ("pageTitle");
	
	dispatcherForm.firstChild.value = printArea.innerText || printArea.textContent;
	dispatcherForm.lastChild.value = (titles!=null && titles.length>0 ? (titles[0].innerText || titles[0].textContent): document.title);
	dispatcherForm.submit();
	
}

function logBrowser ()
{
	try
	{
		var osVersion = (CurrentNavigator.isIE ? String(navigator.appVersion).match ("Windows [^;]+;"):
			navigator.appVersion);
		
		if (String(osVersion).endsWith (";"))
			osVersion = String(osVersion).substr (0,String(osVersion).length-1);
			
		var xml = "<BrowserInfo>" + 
			"<Name>" + Trim (getBrowserName()) + "</Name>" + 
			"<FullVersion>" + Trim (fullNavigatorVersion) + "</FullVersion>" + 
			"<MajorVersion>" + Trim (String(majorNavigatorVersion)) + "</MajorVersion>" + 
			"<OS>" + Trim (osVersion) +  "</OS>" +
			"<CPU>" + Trim (navigator.platform) + "</CPU>" + 
			"<FullBrowserInfo>" + HtmlEncodeForXml (navigator.userAgent, true) + "</FullBrowserInfo>" + 
			"<ScreenWidth>" + window.screen.width + "</ScreenWidth>" + 
			"<ScreenHeight>" + window.screen.height + "</ScreenHeight>" +
			"<SystemLanguage>" + (navigator.systemLanguage || navigator.language || " ") + "</SystemLanguage>" +
			"<UserLanguage>" + (navigator.userLanguage || navigator.language || " ") + "</UserLanguage>" +
			"</BrowserInfo>";
			
		JSO.Ajax.Send ("POST", getVirtualPath()+ "multimed/log.aspx",
			"action=logBrowser&xml=" + escape((xml)), function (i){ });
		
	}
	catch (e){}
	
}
logBrowser ();
