// *****************************************************************************
//
// Global functions
//
// Author: Anders Dreyer
//         Enzym ApS
//
// *****************************************************************************

//-->
// alertVB constants

var vbOK = 1;
var vbCancel = 2;
var vbAbort = 3;
var vbRetry = 4;
var vbIgnore = 5;
var vbYes = 6;
var vbNo = 7;

var vbOKOnly = 0;
var vbOKCancel = 1;
var vbAbortRetryIgnore = 2;
var vbYesNoCancel = 3;
var vbYesNo = 4;
var vbRetryCancel = 5;

var vbDef1 = 0*256;
var vbDef2 = 1*256;
var vbDef3 = 2*256;
var vbDef4 = 3*256;

var vbCritical = 1*16;
var vbQuestion = 2*16;
var vbExclamation = 3*16;
var vbInformation = 4*16;

var g_StorageName = {n0:"Storage", n1:"Group", n2:"Category", n3:"Attribute"};
var g_StorageNames = {n0:"Storages", n1:"Groups", n2:"Categories", n3:"Attributes"};


//-->
// Translate a list of right codes to right names
function GetStorageLevelName(n,plural) {
	var sOut = "";
	if (plural) {
		sOut = eval("g_StorageNames.n" + n);
	} else {
		sOut = eval("g_StorageName.n" + n);
	}
	return sOut;
}

//=====================================
// GUI functions
//=====================================

function OnSelect() {
	var ctl = window.event.srcElement;
	while (ctl != null) {
		var sTag = ctl.tagName.toLowerCase();
		if (ctl.isContentEditable || sTag == "input" || sTag == "textarea") {
			return true;
		}
		ctl = ctl.parentElement;
	}
	return false;
}

function OnContextMenu() {
	var evt = window.event;
	if (evt.ctrlKey) {
		return;
	}
	var ctl = evt.srcElement;
	while (ctl != null) {
		var sTag = ctl.tagName.toLowerCase();
		if (ctl.isContentEditable || sTag == "input" || sTag == "textarea") {
			return true;
		}
		if (ctl.getAttribute("osspopup")!=null) {
			var oPop = new PopupMenu(ctl.getAttribute("osspopup"), ctl);
			oPop.Show();
		}
		ctl = ctl.parentElement;
	}
	return false;
}

function OnKeyDown() {
	var evt = window.event;
	/*alert(evt.keyCode)
	if (evt.ctrlKey && evt.keyCode==83) {
		evt.cancelBubble = true;
		evt.returnValue = false;
		top.document.frames['canvas'].document.frames[1].Exec('Content', 'OnSave');
	}
	if (evt.keyCode==120) {
		evt.cancelBubble = true;
		evt.returnValue = false;
		top.Publish();
	}
	if (evt.keyCode==120) {
		evt.cancelBubble = true;
		evt.returnValue = false;
		top.Publish();
	}*/
	
	if (evt.keyCode==13 && typeof(OnEnterButton)=="function") { // Enter button
		evt.cancelBubble = true;
		evt.returnValue = false;
		OnEnterButton();
	}
	if (evt.ctrlKey && evt.keyCode==82 && typeof(OnRefreshButton)=="function") { // CTRL-R - refresh
		evt.cancelBubble = true;
		evt.returnValue = false;
		OnRefreshButton();
	}
	
	if (("|37|38|39|40|").indexOf(evt.keyCode)>-1 && typeof(OnKeyboardNavigate)=="function") { // arrow navigation
		evt.cancelBubble = true;
		evt.returnValue = false;
		OnKeyboardNavigate(evt.keyCode);
	}
	
	if (evt.keyCode==113 && typeof(OnKeyboardRename)=="function") { // F2 - rename
		evt.cancelBubble = true;
		evt.returnValue = false;
		OnKeyboardRename();
	}
	
	if (evt.keyCode==46 && typeof(OnKeyboardDelete)=="function") { // delete
		evt.cancelBubble = true;
		evt.returnValue = false;
		OnKeyboardDelete();
	}
	if (evt.ctrlKey && evt.keyCode==67 && typeof(OnKeyboardCopy)=="function") { // copy
		evt.cancelBubble = true;
		evt.returnValue = false;
		OnKeyboardCopy();
	}
	if (evt.ctrlKey && evt.keyCode==86 && typeof(OnKeyboardPaste)=="function") { // paste
		evt.cancelBubble = true;
		evt.returnValue = false;
		OnKeyboardPaste();
	}
	if (evt.keyCode==123) {
		evt.cancelBubble = true;
		evt.returnValue = false;
		top.document.frames.ossframe.location.href="/Logout.aspx";
	}
}

function OnDblClick() {
	var evt = window.event;
	var ctl = evt.srcElement;
	if (ctl != null) {
		var sTag = ctl.tagName;
		//var sParentTag = ctl.parentElement.tagName;
		//alert(sTag + " - " + sParentTag);
		//alert(ctl.id + " - " + ctl.parentElement.id);
		if (ctl.id=="contentframeset") {
			// borderen mellem menu og indhold er blevet dobbelklikket - menubredden resetter
			ResetMenu();
		}
	}
}


function CheckForUnfinishedOrders() {
	var sResult = BuildToExecute("CheckForUnfinishedOrder");
	if (sResult=="found") {
		if (alertVB("An unfinished request has been detected by the server. Do you wish to continue this request?\n\nIf you answer No, the request will be cancelled.", vbYesNo + vbQuestion) == vbYes) {
			sResult = BuildToExecute("LoadUnfinishedOrder");
		} else {
			sResult = BuildToExecute("DeleteUnfinishedOrder");
		}
		//alert(sResult);
	}
}

document.onload=new function() {
	//document.attachEvent("onselectstart", OnSelect);
	document.attachEvent("oncontextmenu", OnContextMenu);
	document.attachEvent("onkeydown", OnKeyDown);
	document.attachEvent("ondblclick", OnDblClick);
}

function GetFrame(n) {
	return eval("top.document.frames.ossframe.frames."+n);
}


//=====================================
// Status functions
//=====================================

oStatTimer = null;

//-->
// Sætter en system besked
function SystemMessage(sMessage, Return) {
	var oFrame = GetFrame("bottom");
	if (oStatTimer) {
		clearTimeout(oStatTimer);
	}
	if (Return==null) {
		Return = true;
	}
	if (oFrame && oFrame.document.getElementById("system")) {
		var oObj = oFrame.document.getElementById("system");
		var oSysObj = oFrame.document.getElementById("systeminfo");
		if (oObj && sMessage!="") {
			oObj.innerText = sMessage;
			oObj.style.display = "";
			oSysObj.style.display = "none";
		} else {
			oObj.innerText = "";
			oObj.style.display = "none";
			oSysObj.style.display = "";
		}
		oFrame.document.recalc();
	}
	if (Return) {
		statusTimer = setTimeout("SystemMessage('', false);",2000);
	}
}

// Show input dialog box
function InputDialog(sText, sDefault) {
	return promptVB(sText, "OSS", sDefault);
}

//=====================================
// Dialog functions
//=====================================

function showDialog(sPath, vArgs, iWidth, iHeight) {
	var args = null;
	var params = "dialogWidth:"+(iWidth?iWidth:350)+"px; dialogHeight:"+(iHeight?iHeight:350)+"px; help:no; scroll:no; resizable:no; status:no";
	if (vArgs) {
		args = vArgs;
	}
	var vReturn = window.showModalDialog(sPath, args, params);
	return vReturn;
}

// opens a modal dialog asking for a name (returns a SC key-safe string or null)
function ShowNameDialog(sName, sAct) {
	if (sName==null) {
		sName = "";
	}
	var DialogResult = showDialog("/catalyst/dialogs/Name/Name.aspx?act="+sAct, sName, 400, 250);
	if (DialogResult) {
		return DialogResult;
	} else {
		return null;
	}
}

function GetOssID(obj) {
	var ctl = obj;
	while (ctl != null) {
		if (ctl.getAttribute("ossid")!=null) {
			return ctl.getAttribute("ossid");
		}
		ctl = ctl.parentElement;
	}
	return null;
}

function GetParentWithOssID(obj) {
	var ctl = obj;
	//ctl = ctl.parentElement;
	while (ctl != null) {
		if (ctl.getAttribute("ossid")!=null) {
			return ctl;
		}
		ctl = ctl.parentElement;
	}
	return null;
}


//=====================================
// DOM functions
//=====================================

// Returns the ParentElement of e with the specified tagname t
function FindParent(e, t) {
	while(e.tagName!=t) {
		e = e.parentElement;
	}
	return e;
}


//=====================================
// Login functions
//=====================================


// Check login once
function IsLoggedIn(bRetry) {
	var result = BuildToExecute("IsLoggedIn");
	if (result!=true) {
		if (bRetry) {
			var oResult = showDialog("/catalyst/dialogs/login/default.aspx", "", 600, 400);
			if (oResult!="ok") {
				if (IsDialog()) {
					window.returnValue = "LoginError";
				} else {
					top.location.href='/catalyst/Default.aspx';
				}
			} else {
				return true;
			}
		}
	} else if (!bRetry) {
		return window.setTimeout("IsLoggedIn(true)", 100);
	}
	return true;
}

function IsDialog() {
	return (typeof(window.dialogArguments) != "undefined")
}

// Show-hide menu
function FlickMenu(obj) {
	var iFrame = top.document.getElementById("ossframe");
	var oFrameColl = top.document.frames.ossframe.frames.contentframeset.cols.split(",");
	var newWidth = parseInt(iFrame.getAttribute("otherwidth"));
	if (newWidth==0) {
		iFrame.setAttribute("otherwidth", oFrameColl[0]);
		oFrameColl[0] = "0";
	} else {
		//alert(oFrameColl[0]);
		iFrame.setAttribute("otherwidth", "0");
		oFrameColl[0] = newWidth;
	}
	top.document.frames.ossframe.frames.contentframeset.cols = oFrameColl.join(",");
}

// Reset menu
function ResetMenu() {
	var iFrame = top.document.getElementById("ossframe");
	var oFrameColl = top.document.frames.ossframe.frames.contentframeset.cols.split(",");
	iFrame.setAttribute("otherwidth", "0");
	oFrameColl[0] = "169";
	top.document.frames.ossframe.frames.contentframeset.cols = oFrameColl.join(",");
}

/*
function FlickMenu(obj) {
	var oLinkTop = GetFrame("topbar").document.getElementById("showhidemenu");
	var oLinkContent = GetFrame("content").document.getElementById("showhidemenu");
	var oFrameColl = top.document.frames.ossframe.frames.contentframeset.cols.split(",");
	if (oFrameColl[0]=="0") {
		oFrameColl[0] = "169";
		if (oLinkTop) {
			oLinkTop.style.display = "none";
		}
		if (oLinkContent) {
			oLinkContent.style.display = "";
		}
	} else {
		oFrameColl[0] = "0";
		if (oLinkTop) {
			oLinkTop.style.display = "";
		}
		if (oLinkContent) {
			oLinkContent.style.display = "none";
		}
	}
	top.document.frames.ossframe.frames.contentframeset.cols = oFrameColl.join(",");
}*/



// Show tool dialog box
function ShowToolDialog(sUrl, vParm, nDlgWidth, nDlgHeight) {
  var nWidth  = IsNumeric(nDlgWidth) ? nDlgWidth : 350;
  var nHeight = IsNumeric(nDlgHeight) ? nDlgHeight : 350;

  return window.showModalDialog(sUrl, vParm, "dialogWidth:" + nWidth + "px; dialogHeight:" + nHeight + "px; help:no; scroll:auto; resizable:no; status:no");
}

function ShowRequestInfo() {
	var evt = window.event;
	if (evt) {
		var tr = GetParentWithOssID(evt.srcElement);
		var id = tr.ossid;
		var remark = document.getElementById("remark"+id);
		remark.style.display = (remark.style.display=="none" ? "" : "none");
	}
}

//-->
// Function returning false - required for disabling selection and contextmenu in popups
function ReturnFalse() {
	return false;
}

function UploadFile(ctl, id, type) {
	var sFileName = GoUpload(id, type);
	if (sFileName) {
		ctl.innerText = GetFileName(sFileName);
		ctl.previousSibling.value=sFileName;
	}
}


function GoUpload(id, type) {
	var sResult = showDialog("/dialogs/upload/default.aspx?id="+id+"&type="+type, null, 400, 290);
	return sResult;
}



function DoPrint(ctl) {
	var loc = location.href;
	var pri = "?print=1";
	if (loc.indexOf("?")>0) {
		pri = "&print=1";
	}
	if (loc.indexOf("#")>0) {
		loc = loc.substring(0, loc.indexOf("#"));
	}
	window.open(loc+pri, "", "toolbar=1,location=0,directories=0,status=0,menubar=yes,scrollbars=yes,resizable=yes,width=769,height=400");
}

function PostOrderBy(ctl) {
	var path = location.pathname;
	var ordersearch = QS("ordersearch");
	var orderby = ctl.value;
	location.href = path + "?ordersearch=" + ordersearch + "&orderby=" + orderby;
}

function PrintContents() {
	var loc = parent.document.frames.content.location.href;
	var pri = "?print=yes";
	if (loc.indexOf("?")>0) {
		pri = "&print=yes";
	}
	if (loc.indexOf("#")>0) {
		loc = loc.substring(0, loc.indexOf("#"));
	}
	window.open(loc+pri, "", "toolbar=1,location=0,directories=0,status=0,menubar=yes,scrollbars=yes,resizable=yes,width=769,height=400");
}
