// *****************************************************************************
//
// Global functions
//
// Author: Anders Dreyer
//         Enzym ApS
//
// *****************************************************************************


//=====================================
// 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 OnCalendarSelect() {
	var ctl = window.event.srcElement;
	ctl = FindParent(ctl, "DIV");
	if (ctl!=null) {
		if (ctl.id=="calendar") {
			return false;
		}
	}
	return true;
}

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;
		}
		ctl = ctl.parentElement;
	}
	return false;
}

document.onload=new function() {
	document.attachEvent("onselectstart", OnCalendarSelect);
	document.attachEvent("oncontextmenu", OnContextMenu);
}


//=====================================
// 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 PrepPrint() {
	for (i=0; i<document.body.all.tags("A").length; i++) {
		document.body.all.tags("A")[i].href = 'JavaScript:void(0)';
		document.body.all.tags("A")[i].target = '';
	}
	for (i=0; i<document.body.all.tags("INPUT").length; i++) {
		if (document.body.all.tags("INPUT")[i].type=="submit") {
			document.body.all.tags("INPUT")[i].style.display = "none";
		}
		if (document.body.all.tags("INPUT")[i].type=="button") {
			document.body.all.tags("INPUT")[i].style.display = "none";
		}
		if (document.body.all.tags("INPUT")[i].type=="text") {
			document.body.all.tags("INPUT")[i].readOnly = "true";
		}
	}
	for (i=0; i<document.body.all.tags("TABLE").length; i++) {
		if (document.body.all.tags("TABLE")[i].className=="tbar") {
			document.body.all.tags("TABLE")[i].style.display = "none";
		}
	}
	for (i=0; i<document.body.all.tags("SELECT").length; i++) {
		document.body.all.tags("SELECT")[i].disabled = "true";
	}
	for (i=0; i<document.body.all.tags("BUTTON").length; i++) {
		document.body.all.tags("BUTTON")[i].style.display = "none";
	}
	for (i=0; i<document.body.all.length; i++) {
		document.body.all[i].setAttribute('onclick','return false');
	}
}

function MM_displayStatusMsg(msgStr) { //v1.0
	status=msgStr;
	document.MM_returnValue = true;
}

function showpopup(httpshow) {
	infoWindow = window.open(httpshow,"POPUP","scrollbars=yes,resizable=yes,width=450,height=300");
}

function ConfirmDelete(loc, indent) {
	if (confirm("Are you sure you wish to delete \""+indent+"\" ?")) {
		location.href = loc;
	}
}


//=====================================
// DOM functions
//=====================================

// Returns the ParentElement of e with the specified tagname t
function FindParent(e, t) {
	//var s = ""
	while(e!=null) {
		if (e.tagName!=t) {
			//s += e.tagName + "\n";
			e = e.parentElement;
		} else {
			return e;
			break;
		}
	}
	//alert(s);
	return e;
}

function FindParentWithID(e, d) {
	while(e.ID!=d) {
		e = e.parentElement;
	}
	return e;
}

function IsDialog() {
	return (typeof(window.dialogArguments) != "undefined")
}