//menu 
var mnuSpan; 
var ServiceLoaded = false;
var mono_htmlEditor_Global;
var mono_htmlEditor_flag = "reset";
var mono_htmlEditor_DoubleClick = false;
var mozilla=document.getElementById && !document.all;
var ie=document.all;

function enableServiceCall() {
	ServiceLoaded = true;
}

function doOnClickBody() {
  document.getElementById("hdnScrollTop").value = document.getElementById(ContainerPanelClientID).scrollTop;
}
 
function doOnLoadBody() {
  document.getElementById(ContainerPanelClientID).scrollTop = document.getElementById("hdnScrollTop").value;
}

function WSInit() {
	mnuSpan = document.getElementById("SFMenuDIV");
	if (mnuSpan != null) {
		mnuSpan.style.display = "none";
	}
	var tbl = document.getElementById("DeskTopTableCell_tblMain");
	if (tbl != null) {
		tbl.border = "0";
	}
	if (WebServiceLocation != '') {
		PortalWebService.onserviceavailable = enableServiceCall;
		PortalWebService.useService(WebServiceLocation,"PortalData");
	}
	
	if (adminRole != 'User')
	{
		document.body.oncontextmenu = showMenu;
		document.body.attachEvent("onclick", hideMenu);
		
	}
	initializeCfgMenu();
	if (window.mono_htmlEditor_isActive != null) {
			document.onactivate = mono_htmlEditor_activate;
	}
	if (!mozilla)
	{
		document.body.setActive();
	}

	CustomStartUp();
	doOnLoadBody();
}

function showPopup (newURL, width, height) {
  var newFeatures = 'width=' + width + ', height=' + height + ',channelmode=0,dependent=0,directories=0,fullscreen=0,location=0,menubar=0,resizable=0,scrollbars=0,status=0,titlebar=0,toolbar=0'
  var popupWindow = window.open(newURL, '_blank', newFeatures);
}

function mono_htmlEditor_activate(sender){

	if (sender!=null) {
		mono_htmlEditor_DoubleClick = true;
		sender.setActive();
		setTelerikLink(sender.getAttribute('id'));
	}
	elem = document.activeElement;
	if (mono_htmlEditor_DoubleClick==true && elem!=null && elem.compactEditor==null && (elem.tagName=='HtmlEditor' || elem.htmlEditor!=null || (elem.containerPanel!=null) && mono_htmlEditor_Global.visible=="true")) {
		if (mono_htmlEditor_Global==null) if (elem.tagName=='HtmlEditor') mono_htmlEditor_Global=elem; else mono_htmlEditor_Global = elem.htmlEditor;
		mono_htmlEditor_Global.visible="true";
		mono_htmlEditor_Global.htmlHolder.contentEditable="true";
		if (mono_htmlEditor_flag == "reset") {
			mono_htmlEditor_flag = "set";
			if ((event.clientY - mono_htmlEditor_Global.parentElement.offsetHeight) < 0) {
				mono_htmlEditor_Global.parentElement.parentElement.style.setExpression("top", "document.getElementById('ContainerPanel').clientHeight - mono_htmlEditor_Global.parentElement.offsetHeight");
			} 
			else {
				mono_htmlEditor_Global.parentElement.parentElement.style.setExpression("top", "0");
			}
		}
	}
	else {
		if (mono_htmlEditor_Global!=null) {
			mono_htmlEditor_Global.visible="false";
			mono_htmlEditor_Global.htmlHolder.contentEditable="false";
			mono_htmlEditor_flag = "reset";
		}
		mono_htmlEditor_DoubleClick = false;
	}
}

function setTelerikLink(DivID) {
	var link;
	var telerikedit;
	//alert(DivID);
	telerikedit = window.open('telerikedit.aspx?ElementID=' + DivID,'telerikedit','location=1, menubar=1, resizable=1, scrollbars=1, width=900, height=700');
	telerikedit.moveTo(50,50);
	//link = document.getElementById('teleriklink');
	//link.setAttribute('href', 'telerikedit.aspx?ElementID=' + DivID);
}

function SaveModuleState(mp, state) {
	var ret;
	if (!ServiceLoaded) {
		alert ("Web service not loaded - you may not have the administrative rights to this module");
		document.location.href=document.location.href;
		return true;	
	}
	if (state == 'deleted') {
		ret = confirm("Are you sure?")
		if (ret == false)	{
			document.location.href=document.location.href;
			return true;
		}
	}
	
	var unameText = uname;
	var co = PortalWebService.createCallOptions();
	co.funcName = "SaveModuleState";
	co.async = false;
	var oResult = PortalWebService.PortalData.callService(co, mp, getQueryVariable('tabid'), unameText, state);
	document.location.href=document.location.href;
	return true;
}

function maximizeDIV (divID)
{
	
	var el = document.getElementById(divID);
	var iHeight = document.body.clientHeight;
	var iWidth = document.body.clientWidth;
	var oldt = el.style.top;
	var oldl = el.style.left;
	el.style.border = 'dashed 2px LightSteelBlue';
	
	el.style.position = 'absolute';
	el.style.zIndex = "500000";
	
	el.style.top = 0;
	el.style.left = 0;
	var i;
	var ec = document.getElementById("EditorContainer" + divID);

	if (ec!=null) {
		ec.style.width = "100%";
		ec.style.height = "100%"
		ec.style.overflow = "visible";
		el.style.posHeight = ec.style.posHeight ;
		el.style.posWidth = ec.style.posWidth;
		el.style.overflow = "visible";
	}

	
	
	return;
	if ((el.offsetHeight < document.body.clientHeight) || (el.offsetWidth < document.body.clientWidth)) {
		// Notice we set both the style and clip sub-objects. The clip
		// object is used in Netscape, the style object is used in IE.
		// By setting both directly we do not need to distinguish between
		// browsers.
		if (el.offsetHeight < iHeight) 
			el.style.pixelHeight = el.offsetHeight + 100;
		else
			el.style.pixelHeight = document.body.clientHeight;
		if (el.offsetWidth < iWidth) 
			el.style.pixelWidth = el.offsetWidth + 100;
		else
			el.style.pixelWidth = document.body.clientWidth;
		}
		if ((el.offsetHeight >= document.body.clientHeight) && (el.offsetWidth >= document.body.clientWidth)) {
			el.style.border = 'solid 0px black';
			el.style.pixelWidth = document.body.clientWidth - 30;
			clearTimeout(tout);
		} 
		else
		{
			tout = setTimeout('maximizeDIV(' + divID + ')', 1);
		}
}



function showMenu () {
//  if ((window.event.button == 2)) // && (mp))
//  {
	
	if (cfgMenuDiv.style.display == "inline") {hideMenu(); return false;}
	
	oldsp_hideAllMenus();
    mnuSpan.style.position = "absolute";
    mnuSpan.style.display = "inline";
    mnuSpan.style.posTop = event.clientY + document.body.scrollTop;   
    mnuSpan.style.posLeft = event.clientX + document.body.scrollLeft;
    if ((window.document.body.clientWidth - window.event.clientX) < mnuSpan.clientWidth) {
		mnuSpan.style.left = window.document.body.clientWidth - mnuSpan.clientWidth + document.body.scrollLeft;
	}
    if ((window.document.body.clientHeight - window.event.clientY) < mnuSpan.clientHeight) {
		mnuSpan.style.top = window.document.body.clientHeight - mnuSpan.clientHeight + document.body.scrollTop;
	}
	
    return false;
//  }
}

function hideMenu () {
	oldsp_hideAllMenus();
	mnuSpan.style.display = "none";
}

var cfgMenuDiv;
var i=0;

function initializeCfgMenu() {
	cfgMenuDiv = document.createElement("div");
	cfgMenuDiv.style.position = "absolute";
	cfgMenuDiv.style.zIndex = "300";
	cfgMenuDiv.style.display = "none";
	cfgMenuDiv.className = "sftbMenuDiv";
	window.document.body.appendChild(cfgMenuDiv);
	if (window.document.attachEvent) 
	{
		window.document.attachEvent("onmousedown",sfCfgMenu_mouseDown);
	}
	
	var tempStr = "";
	var menuItems = new Array (1);
	var menuAction = new Array (1);
	menuItems[0] = "Configure this item";
	menuAction[0] = "config";
	
	tempStr = "<DIV width=100%><TABLE cellspacing=0 cellpadding=0 width=100%><TBODY>";
	for (var i=0; i<menuItems.length; i++) {
		tempStr = tempStr + "<TR><TD id='sfConfigMenuItem" + i + "' action='" + menuAction[i] + "' onmouseover='this.className=\"sftbMenuItem_mOver\"' onmouseout='this.className=\"\"'><SPAN>" + menuItems[i] + "</SPAN></TD></TR>"
	}
	tempStr = tempStr + "</TBODY></TABLE></DIV>";
	cfgMenuDiv.innerHTML = tempStr;
			
	if (cfgMenuDiv.children != null)
	{
		for (var i=0; i< cfgMenuDiv.children[0].children[0].rows.length; i++) {
			cfgMenuDiv.children[0].children[0].rows[i].cells[0].attachEvent("onmousedown",sfCfgMenuAction);
		}
	}
}

function sfCfgMenu_mouseDown() {
	cfgMenuDiv.style.left = window.event.clientX + window.document.body.scrollLeft;
	if ((window.document.body.clientHeight - window.event.clientY) < cfgMenuDiv.clientHeight) {
		cfgMenuDiv.style.top = window.document.body.clientHeight - cfgMenuDiv.clientHeight + window.document.body.scrollTop;
	}
	else {
		cfgMenuDiv.style.top = window.event.clientY + window.document.body.scrollTop;
	}
	cfgMenuDiv.style.display = "none";
}

var configID;
var clientSender;
function ShowConfigMenu() {
	configID = event.srcElement.configID;
	if (configID == null) {
		configID = event.srcElement.parentElement.configID;
	}
	clientSender = event.srcElement.sender;
	if (clientSender == null) {
		clientSender = event.srcElement.parentElement.sender;
	}
	cfgMenuDiv.style.display = "inline";

}

function sfCfgMenuAction() {
	cfgMenuDiv.style.display = "none";
	event.cancelBubble = true;
	var menuItem = event.srcElement;
	if (menuItem.tagName == "SPAN") menuItem = event.srcElement.parentElement;

	switch(menuItem.action)
		{
		case "config":
			//
			//alert(configID);
			window.location.href = "http://" + window.location.hostname + window.location.pathname + "?tabid=11&tabindex=13&configID=" + configID + "&sender=" + clientSender;
			break;
		}
}

function printWindow(){
browserVersion = parseInt(navigator.appVersion)
if (browserVersion >= 4) window.print()
}

function prepareForPrint(htmlHolder)
{
	if (htmlHolder==null) return false;
	var doc = window.document.createDocumentFragment();
	var docBody = doc.createElement("BODY");
	doc.appendChild(docBody);
	docBody.innerHTML = htmlHolder.innerHTML;
	return doc;
}

function printModule(htmlHolderID)
{
	var htmlHolder = document.getElementById(htmlHolderID);
	var doc = prepareForPrint(htmlHolder);
	doc.execCommand("Print",true);
}

function mailpage()
{
  mail_str = "mailto:?subject= Recommending Web page: " + document.title;
  mail_str += "&body= I recommend this page -- " + document.title;
  mail_str += ". You should check this out at, " + location.href; 
  location.href = mail_str;
}

function CheckForUsername() {
	if (uname == ''){
		//alert ("You must first log in with your username and password!");
		window.location = (myAccountPage);
		return false;
	}
	else
	{
		return true;
	}
}

/*
function keydown() {
	alert(event.keyCode);
	//F12
	if (event.keyCode == 123) {
		window.open("/admin/SearchDB.aspx", "_search", "width=400");
	}
}

document.attachEvent("onkeydown", keydown);
*/