var isProfilesChanged=false;

function menubelow(oDiv, oCaller)
// Toggles the display attribute of the object oDiv ad places it below the calling element
{
	var oElem=document.getElementById(oDiv);
	//oElem.style.top = (oCaller.offsetTop + oCaller.offsetHeight);
	//oElem.style.left = (oCaller.offsetLeft);
	
	var sTop = findPosY(oCaller) + oCaller.offsetHeight;
	var sLeft = findPosX(oCaller);
	
	sTop = sTop + "px";
	sLeft = sLeft + "px";
	
	oElem.style.top = sTop;
	oElem.style.left = sLeft;

	oElem.style.position = "absolute";
	if (oElem.style.display=="block") {
		oElem.style.display="none";
	} else {
		oElem.style.display="block";
	}
	
}

function docAll(sID) { 
	return document.getElementById(sID);
}

function docAll2(sID, ow) {
	if (ow==null) ow = window;
	return ow.document.getElementById(sID);
}

function expandSection(oDiv, oImage) {
	var oElem=document.getElementById(oDiv);
	if (oElem.style.display=="none") {
		oImage.src=sDesignFldr + "/toolbar_close.gif";
		oElem.style.display="block";
	} else {
		oImage.src=sDesignFldr + "/toolbar_open.gif";
		oElem.style.display="none";
	}
	resizeEditor();
}

function editItem(nObjectID, nPortalID, sObjPath) {
	var oWH=window.open("/eway/applications/portalconfig/configPages/editPage.aspx?elementid=" + nObjectID + "&pid=" + nPortalID + "&objpath=" + sObjPath,  "EditWin","scrollbars=1, status=1, resizable=1, width=600, height=400");
	oWH.focus();
}

function editArea(nObjectID, sAreaName, nPortalID, sObjPath) {
	var oWH=window.open("/eway/applications/portalconfig/configpages/editArea.aspx?oid=" + nObjectID + "&area=" + sAreaName + "&pid=" + nPortalID + "&objpath=" + sObjPath,  "EditWin","scrollbars=1, status=1, resizable=1, width=600, height=400");
	oWH.focus();
}

function launchObject(nObjectID, nPortalID, sTarget, sParams, nNewWin) {
	sURL="/eway/" + sFileName + "?oid=" + nObjectID + "&pid=" + nPortalID + "&trg=" + sTarget + "&" + sTarget + "=" + sParams;
	if (nNewWin==1) {
		openNewWin(sURL);
	} else {
		window.location.href=sURL;
	}
}


function loadAreaHTML(sArea,sHTML) {
	var oElem=document.getElementById("psp" + sArea);
	oElem.innerHTML=sHTML;
}

function launchRelPicker(nRelObjectTypeID, nRelKeywordID, sCategory, bDoSearch, bLockObjType, bLockRole) {
	var sRelParams="";
	var oNewRel=document.getElementById("efmrelations");
	var oObjectTypeID=document.getElementById("efmobjecttypeid");
	var nDoSearch=0, nLockObjType=0, nLockRole=0;

	if (nRelKeywordID==null) nRelKeywordID=0;
	if (nRelObjectTypeID==null) nRelObjectTypeID=0;
	if (bDoSearch==null) bDoSearch=false;
	if (bDoSearch==true) nDoSearch=1;
	if (bLockObjType==null) bLockObjType=false;
	if (bLockObjType==true) { nLockObjType=1; }
	if (bLockRole==null) bLockRole=false;
	if (bLockRole==true) { nLockRole=1; }
	if (sCategory==null) sCategory="";

	if (oNewRel!=null && oObjectTypeID!=null) {
		sRelParams="&relations=" + oNewRel.value + "&activenode=" + sCategory;
		sRelParams+="&lockot=" + nLockObjType + "&lockrole=" + nLockRole;
		sRelParams+="&ownerot=" + oObjectTypeID.value + "&activeot=" + nRelObjectTypeID + "&activerole=" + nRelKeywordID;

		var sReturn=openDialog(
			"/eway/library/pickerCnt.aspx?tree=relpicker&mode=1" + sRelParams,null,465,640);
		if (sReturn>"") {
			oNewRel.value=sReturn.split("%")[0];
		}
	} else {
		alert("Error: Required form field missing.");
	}
	return false;
}

function launchRelPickerCst(nObjectID, sRelObjTypes, sDefRelRoles, bLockOT, bLockRole) {
	launchRelPicker(sRelObjTypes,sDefRelRoles,"",true,bLockOT,bLockRole);
}

function modifyState(sURL,nAreaID,sTargetArea,nParamIX,sValue) {
	if (sURL==null) {
		sURL=window.location.href;
		if (sURL.indexOf("?")==-1) { sURL+="?"; }
	}
	if (sStateSeparator==null || sStateSeparator=="") {	sStateSeparator=":"; }
	sURL=setURLParam(sURL,"trg",sTargetArea);
	var sState=getURLParam(sURL,sTargetArea);
	var aState=sState.split(sStateSeparator);
	if (aState.length<nParamIX) { for (var i=aState.length;i<nParamIX;i++) { aState[i]=''; } }
	aState[nParamIX]=sValue;
	var sNewParam=aState.join(sStateSeparator);
	sURL=setURLParam(sURL,sTargetArea,sNewParam);
	return sURL;
}

function loadState(sAreaName, nTarget, sNewState, nObjListViewerObject, nCurrObjectTypeID, nLinkObjectTypeID) {
	var i=0;
	var sTmp=sAreaName, sURL;
	if (sStateSeparator==null || sStateSeparator=="") {	sStateSeparator=":"; }
	if (nCurrObjectTypeID==null) { nCurrObjectTypeID=0; } else { nCurrObjectTypeID=parseInt(nCurrObjectTypeID); }
	if (nLinkObjectTypeID==null) { nLinkObjectTypeID=0; } else { nLinkObjectTypeID=parseInt(nLinkObjectTypeID); }
	if (nObjListViewerObject==null) { nObjListViewerObject=0; } else { nObjListViewerObject=parseInt(nObjListViewerObject); }
	if (nTarget<0) {
		while (i>nTarget) {
			var oParent=document.getElementById("par" + sAreaName);
			if (oParent!=null) { sAreaName=oParent.value; } else { sAreaName=""; }
			i--;
		}
	}

	var aState=sNewState.split(sStateSeparator);
	
	var nCnt=aState.length;

	if (sAreaName.substr(0,1)=="[") { // Object is displayed via openForm, editMessage etc.
		if (nCnt>0) {
			var nObjectID=aState[1];
			var sURL='/eway/library/openForm.aspx?param1=' + nObjectID + '&param5=read';
			if (nTarget==1) {
				window.open(sURL);
			} else {
				window.navigate(sURL);
			}
		}
	} else {
		var nViewerObject=parseInt(aState[0]);
		switch (nViewerObject) {
			case -3: // Automatic viewer object
				if (nCurrObjectTypeID==nLinkObjectTypeID && !isNaN(nObjListViewerObject)) { // If objecttype in link is the same as objectlist's object type, use same vo as object list
					nViewerObject=nObjListViewerObject;
				} else { // If not, use default viewer object
					nViewerObject=-2; 
				}
				aState[0]=nViewerObject;
				break;
		}
		
		if (sAreaName.substring(0,5)=="__new") { nTarget=1; }
		if (nTarget==1) {
			sURL='/eway/default.aspx?pid=' + nPortalID + '&oid=' + nViewerObject + '&trg=__new&__new=' + aState.join(sStateSeparator);
			window.open(sURL);
		} else {
			for (i=0;i<nCnt;i++) {
				sURL=modifyState(sURL,0,sAreaName,i,aState[i]);
			}
			loadTarget(sURL);
		}
	}
}

function loadTarget(sURL) {
	if (getURLParam(sURL,"trg").substring(0,5)=="__new") {
		window.open(sURL);
	} else { 
		window.location.href=sURL;
	}
}

function loadContent(nObjectID,nPortalID,sTarget,sExtraParams) {
	var bNew=false, bTop=false, sParam, sURL;
	var sSpan="";
	var sAreaName="", sState="";
	
	if (sTarget.indexOf("__new")!=-1) { bNew=true; }
	if (sTarget.indexOf("__top")!=-1) { bTop=true; }
	if (bNew==true) { launchObject(nObjectID,nPortalID,sTarget,sExtraParams,1); }
	if (bTop==true) { launchObject(nObjectID,nPortalID,sTarget,sExtraParams,0); }

	if (nObjectID==null) nObjectID=0; 
	if (nReqObjID==null) nReqObjID=0; 
	if (nPortalID==null) nPortalID=""; 
	if (sTarget==null) sTarget=""; 
	if (sExtraParams==null) sExtraParams="";

	setURLParam(sQueryString,"trg",sTarget);
	setURLParam(sQueryString,"oid",nReqObjID);
	setURLParam(sQueryString,"pid",nPortalID);
	setURLParam(sQueryString,sTarget,sExtraParams);
	
	if (bNew==false && bTop==false) {
		window.location.href = "/eway/" + sFileName + "?" + sQueryString;
	}
}

function getURLParam(sURL,sKey,sParamDiv) {
	var ptr1, ptr2, sOut;

	if (sMainSeparator==null || sMainSeparator=="" || sMainSeparator=="&amp;") sMainSeparator="&"; 
	if (sParamDiv==null) sParamDiv=sMainSeparatorReq;

	var sFind=sMainSeparator + sKey + "=";
	ptr1 = sURL.indexOf(sFind);
	if (ptr1==-1) {
		sFind="?" + sKey + "=";
		ptr1 = sURL.indexOf(sFind);
	}

	if (ptr1!=-1) {
		ptr1+=sFind.length;
		ptr2=sURL.indexOf(sParamDiv,ptr1);
		if (ptr2==-1) {
			sOut = sURL.substr(ptr1); }
		else {
			sOut = sURL.substr(ptr1,ptr2-ptr1); }
	} else {
		sOut="";
	}
	return sOut;
}

function openNewWin(sURL,sParam1,sParam2,sParam3) {
	var sNum=Math.random()*1000;
	sNum=Math.floor(sNum);
	window.open(sURL, "win" + sNum, "width=" + sParam3 + ", height=" + sParam2 + ", status=1, scrollbars=1, menubar=1, resizable=1");
}

function setURLParam(sURL, sParamName, sNewValue) {
	var sReplace, sReplaceWith, sSep, sFind;
	if (sMainSeparator==null || sMainSeparator=="" || sMainSeparator=="&amp;") sMainSeparator="&"; 

	var bHasQm=sURL.indexOf("?")>-1; var bHasSep=sURL.indexOf(sMainSeparator)>-1;

	if (!bHasQm && !bHasSep) {
		sURL+="?" + sParamName + "=" + sNewValue;
	} else {
		var sOldParam=getURLParam(sURL,sParamName,sSep);

		sSep="?";
		sFind=sSep + sParamName + "=";
		if (sURL.indexOf(sFind)>-1) {
			sReplace=sSep + sParamName + "=" + sOldParam;
			sReplaceWith=sSep + sParamName + "=" + sNewValue;
			sURL=sURL.replace(sReplace, sReplaceWith);
		} else {
			sSep=sMainSeparator;
			sReplace=sSep + sParamName + "=" + sOldParam;
			sReplaceWith=sSep + sParamName + "=" + sNewValue;
			sFind=sSep + sParamName + "=";
			if (sURL.indexOf(sFind)>-1) {
				sURL=sURL.replace(sReplace, sReplaceWith);
			} else {
				sURL+=sReplaceWith;
			}
		}
	}
	return sURL;
}

function openDialog(sURL,DlgParam,nH,nW) {
	if (navigator.appVersion.indexOf("MSIE")!=-1) {
		return window.showModalDialog(sURL,DlgParam,"resizable:yes; help:no; status:yes; dialogHeight:" + nH + "px ;dialogWidth:" + nW + "px");	
	} else {
		window.dialogArguments = {inputparam: DlgParam};
		window.open(sURL,null,"modal=1, width=" + nW + ", height=" + nH,0);
		return window.result;
	}
}

function dlgParam() {
	this.value=null;
}

function appendChangedNode(nTopicID,sName) {
	var oElem=document.getElementById("efmnodes");
	var oChanged=document.getElementById(sName);
	if (oElem==null) { alert("Error: Required form field missing (efmnodes)"); return false; }
	if (oChanged==null) { alert("Error: Required form field missing (" + sName + ")"); return false; }

	var sChNode=document.getElementById(sName).value;
	var sChangedNodes=oElem.value;

	var sCurrNode=getDimStatus(sChangedNodes,sChNode);

	if (sCurrNode!="") {
		sChangedNodes=sChangedNodes.replace(sCurrNode,sChNode);
	} else {
		if (sChangedNodes!="") sChangedNodes+=";";
		sChangedNodes+=sChNode;
	}
	
	isProfilesChanged=true;
	oElem.value=sChangedNodes;
}

function getDimStatus(sChangedNodes,sID) {
	var aChNodes=sChangedNodes.split(";");
	var i, sNode, aNode;
	var nDimKeywordID=parseInt(sID.split(",")[3]);
	var nProfileID=parseInt(sID.split(",")[1]);

	for (i=0;i<aChNodes.length;i++) {
		sNode=aChNodes[i];
		if (sNode!=null && sNode!="") {
			aNode=sNode.split(",");
			if (nDimKeywordID==parseInt(aNode[3]) && nProfileID==parseInt(aNode[1])) return sNode;
		}
	}
	return "";
}

function hndKMapCtlDelProfile(nTopicID,nProfileID) {
	if (delProfile(nProfileID)) {
		var oTR=document.getElementById("prow_" + nTopicID + "_" + nProfileID);
		isProfilesChanged=true;

		var nIndex=oTR.rowIndex;
		var oTable=oTR.parentElement;
		oTable.deleteRow(nIndex);
	}
}

function delProfile(nID) {
	var i;
	// Read current state
	var oNodes=document.getElementById("efmnodes");
	if (oNodes!=null) {
		var sChangedNodes=oNodes.value;

		// Get all nodes in profile to be deleted
		var sDelNodes=getNodes(sChangedNodes, null, nID, null);
		var aDelNodes=sDelNodes.split(";");
		var i;
		var sDelNode;
		for (i=0;i<aDelNodes.length;i++) {
			sDelNode=aDelNodes[i];
			if (sDelNode!="") {
				// Remove the nodes
				sChangedNodes=sChangedNodes.replace(sDelNode + ";","");
				sChangedNodes=sChangedNodes.replace(sDelNode,"");
			}
		}
		// Add profile_id to string of deleted profiles
		if (sChangedNodes!="") sChangedNodes+=";";
		sChangedNodes+="-1," + nID + ",0,0";

		// Write back altered state
		document.getElementById("efmnodes").value=sChangedNodes;
		return true;
	} else {
		alert("Error: Required form field missing");
		return false;
	}
}

function getNodes(sChangedNodes,nTopicID,nProfileID,nDimKeywordID) {
	var aNodes=sChangedNodes.split(";");
	var i, nValue;
	var sNode, s="";
	var bOk;

	nTopicID=parseInt(nTopicID);
	nProfileID=parseInt(nProfileID);
	nDimKeywordID=parseInt(nDimKeywordID);

	for (i=0;i<aNodes.length;i++) {
		sNode=aNodes[i];
		if (sNode!="") {
			bOk=true;
			var aNode=sNode.split(",");
			if (!isNaN(nTopicID) && bOk==true) {
				nValue=parseInt(aNode[0]);
				if (nValue!=nTopicID) bOk=false;
			}

			if (!isNaN(nProfileID) && bOk==true) {
				nValue=parseInt(aNode[1]);
				if (nValue!=nProfileID) bOk=false;
			}

			if (!isNaN(nDimKeywordID) && bOk==true) {
				nValue=parseInt(aNode[3]);
				if (nValue!=nDimKeywordID) bOk=false;
			}

			if (bOk==true) {
				if (s!="") s+=";";
				s+=sNode;
			}
		}
	}
	return s;
}

function openOfficeFile(sURL) {

	try {
		var bOpenDav=false;
		var sUA=navigator.appVersion;
		//var bBrowserOK=(sUA.indexOf("MSIE")!=-1 && sUA.indexOf("Windows")!=-1);
        var bBrowserOK = true;
        
		if (sURL.length>0 && bBrowserOK==true) {
		    
			//var sExt=sURL.substr(sURL.length-3,sURL.length);
			var sExt=sURL.substr(sURL.lastIndexOf(".")+1,sURL.length-sURL.lastIndexOf("."))
			
			var sProt=sURL.substr(0,4);
			
			
			if (sProt!="http" && sProt!="file") {
				sURL=window.location.protocol + "//" + window.location.hostname + sURL;
				bOpenDav=true;
			}

			if (sProt=="http") { bOpenDav=true; }

			var sApp="";
			switch (sExt.toLowerCase()) {
				case "dot":
				case "doc":
				case "docx":
				case "dotx":
				case "dotm":
				case "docm":
					sApp="winword"
					break;
				case "xlt":
				case "xlw":
				case "xls":
				case "xlsx":
				case "xlsb":
				case "xlsm":
				case "xltm":
				case "xltx":
				case "xlam":
					sApp="excel"
					break;
				case "ppt":
				case "pptx":
				case "potx":
				case "pptm":
				case "potm":
				case "ppam":
				case "ppsx":
				case "ppsm":
					sApp="powerpnt"
					break;
				case "vsd":
				case "vss":
				case "vst":
				case "vdx":
				case "vsx":
				case "vtx":
					sApp = "visio"
					break;
			}

			if (bOpenDav==true && sApp!="" && chkAccessRight(sURL)) {
			    if(browserTest() == "ie"){
				    var oLauncher=new ActiveXObject("eWayCU.launcher")
				    oLauncher.Parameter=sURL;
    				
				    oLauncher.FilePath=sApp;
				    oLauncher.launch();

				    delete oLauncher;
				    oLauncher = null;
				 }else{
				    window.open(sURL);
				 }
			} else {
				window.open(sURL);
			}
		}
	}
	catch(ex) {
		alert("An error occurred: " + ex.message);
	}
}
 
function chkAccessRight(sURL) {
    if(browserTest() == "ie"){
	    var oXML=new ActiveXObject("microsoft.xmldom");
	    oXML.async=false;
	    if (oXML.load("/eway/library/docArch.aspx?cmd=GetObjectIDByURL&url=" + sURL)) {
		    var nObjectID=parseInt(oXML.selectSingleNode("//value").text);
		    if (!isNaN(nObjectID) && !nObjectID==0) {
			    oXML.load("/eway/library/docArch.aspx?cmd=chkwrtaccess&oid=" + nObjectID);
			    var oNode=oXML.selectSingleNode("//value")
			    if (oNode!=null) {
				    return oNode.text=="1";
			    }
		    } else {
			    return false;
		    }
	    }
    }else{
        var oXML = document.implementation.createDocument("","",null);
        var sXMLURL = "/eway/library/docArch.aspx?cmd=GetObjectIDByURL&url=" + sURL
        oXML.async=false;
        if(oXML.load(sXMLURL)){
	        var oNode1 = selectSingleNode("//value",oXML);
	        
	        if(! IsNull(oNode1)){
	            var nObjectID = parseInt(oNode1.firstChild.nodeValue);
		        if (!isNaN(nObjectID) && !nObjectID==0) {
		            if(oXML.load("/eway/library/docArch.aspx?cmd=chkwrtaccess&oid=" + nObjectID)){
		                var oNode2 = selectSingleNode("//value",oXML);
		                if (oNode2!=null) {
			                return oNode2.firstChild.nodeValue == "1";
		                }else{
		                    return false;
		                }
		            }else{
		                return false;
		            }
		        } else {
		            return false;
		        }
		    }else{
		        return false;
		    }
	    }
    }
}

//For mozilla
function selectNodes(sXPath,oXML) {
    var oEvaluator = new XPathEvaluator();
    var oResult = oEvaluator.evaluate(sXPath, oXML, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
    var aNodes = new Array;
    if (oResult != null) {
        var oElement = oResult.iterateNext();
        while(oElement) {
            aNodes.push(oElement);
            oElement = oResult.iterateNext();
        }
    }
    return aNodes;
}

//For mozilla
function selectSingleNode(sXPath,oXML) {
    var oEvaluator = new XPathEvaluator();
    var oResult = oEvaluator.evaluate(sXPath, oXML, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
    if (oResult != null) {
        return oResult.singleNodeValue;
    } else {
        return null;
    }              
}

//check if obj is null or undefined
function IsNull(obj){
    return ((obj == null) || (obj == undefined));
}

/*
Element.prototype.selectNodes = function (sXPath) {
    var oEvaluator = new XPathEvaluator();
    var oResult = oEvaluator.evaluate(sXPath, this, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
    var aNodes = new Array;
    if (oResult != null) {
        var oElement = oResult.iterateNext();
        while(oElement) {
            aNodes.push(oElement);
            oElement = oResult.iterateNext();
        }
    }
    return aNodes;
};

Element.prototype.selectSingleNode = function (sXPath) {
    var oEvaluator = new XPathEvaluator();
    var oResult = oEvaluator.evaluate(sXPath, this, null, 
    XPathResult.FIRST_ORDERED_NODE_TYPE, null);
    if (oResult != null) {
        return oResult.singleNodeValue;
    } else {
        return null;
    }              
}

*/


function deleteFile(nElemID, nObjectID, sAppFldName) {
	var sPrompt=aLang["confirmdeletefile"];
	if (sPrompt==null) { sPrompt="Are you sure you want to delete this file?"; }
	if (confirm(sPrompt)) {
	    var sURL="/eway/library/deleteFile.aspx?oid=" + nObjectID + "&fldname=" + sAppFldName;
	    if (window.showModalDialog!=null) {
		    window.showModalDialog(sURL,null,"dialogWidth: 300px; dialogHeight: 110px; status: no; resizable: yes; help: no;");
	    } else {
	        window.open(sURL,"DeleteFile","width=300, height=110, modal, dialog");
		}
		window.navigate(window.location.href);
	}
}

function deleteObject(sArea, nObjectID) {
	var sPrompt=aLang["confirmdeleteobject"];
	if (sPrompt==null) { sPrompt="Are you sure you want to delete this object?"; }
	if (confirm(sPrompt)) {
		if (nObjectID==null) nObjectID=document.getElementById("efmobjectid").value;

        var sURL="/eway/library/deleteObject.aspx?id=" + nObjectID;
        
        if (window.showModalDialog==null) {
	        window.open(sURL,"DeleteObject","width=300, height=110, modal, dialog");
        } else {
		    window.showModalDialog(sURL,null,"dialogWidth: 300px; dialogHeight: 110px; status: no; resizable: yes; help: no;");
        }

		refreshParent(true, nObjectID, nObjectID);
		var sURL=window.location.href;
		if (sURL.indexOf("default.aspx")!=-1) {
			sURL=modifyState(sURL,0,sArea,1,0);
			sURL=sURL.replace(nObjectID, 0);
			loadTarget(sURL);
		}
	}
}

function refreshParent(bCloseCurrent, nObjectID, nNewObjectID) {
	var oWO=top.opener;
	if (oWO==null && window.parent!=null) oWO=window.parent.opener;
	var sURL=window.location.href;
	if (sURL.indexOf("openForm.aspx")!=-1 || sURL.indexOf("editMessage.aspx")!=-1 || sURL.indexOf("default.aspx")!=-1) {
		if (oWO!=null) {
			var sOpenerURL=oWO.window.location.href;
			if (sOpenerURL.indexOf("view.aspx")!=-1) { 
				oWO.vwLoadState();
			} else {
				if (sOpenerURL.indexOf("default.aspx")!=-1 || sOpenerURL.substr(sOpenerURL.length-6,6)=="/eway/") {
					sOpenerURL=sOpenerURL.replace(nObjectID, nNewObjectID);
					oWO.loadTarget(sOpenerURL);
				}
			}
		}
		if (bCloseCurrent) top.close();
		return;
	}
}

function openTransition(nTransitionID,nObjectID) {
	openDialog("/eway/library/pickerCnt.aspx?tree=transitions&objectid=" + nObjectID + "&transitionid=" + nTransitionID,null,400,400);
}

function moveTransitionNow(nControlID, nTransitionID,nObjectID) {
	var sControlName = 'efmtrcmd' + nControlID;
	if(document.getElementById(sControlName)) {
		document.getElementById(sControlName).value="movenow," + nTransitionID + "," + nObjectID;
		if (document.getElementById(sControlName).form) doSubmit('ewaysave');
	}
}

function showProfileTransitionWarning() {
	alert(aLang['warnprtr']);
}

function pickTemplate(sTagID, sDestField, nSrcObjectTypeID, sSrcField) {
    var sURL="/eway/library/pickerCnt.aspx?tree=objpicker&lockot=1&activeot=" + nSrcObjectTypeID;

    if (window.showModalDialog==null) {
        var oArg=new Object();
        oArg.ReturnValue=null;
        oArg.CallBackFunc=pickTemplateReturn;
        oArg.ElementId=sTagID;
        oArg.DestField=sDestField;
        oArg.SrcField=sSrcField;
        
        window.dialogArguments=oArg;

        window.open(sURL,"Pick Image","modal, width=630, height=450, resizable=1");
    } else {
	    var sReturn=window.showModalDialog(sURL,null,"status:yes;center:yes;help:no;minimize:no;maximize:no;border:thin;statusbar:yes;dialogWidth:630px;dialogHeight:435px;");
	    if (sReturn!=null) {
		    var nSelObjectID=sReturn.split("%")[0];
		    var sTplName=sReturn.split("%")[1];
		    document.getElementById("h" + sTagID).value=sDestField + ";" + nSelObjectID + ";" + sSrcField;
		    document.getElementById("a" + sTagID).innerHTML=sTplName;
	    }
    }
}

function pickTemplateReturn(oArg) {
    var sTagID=oArg.ElementId;
    var nSelObjectID=oArg.ReturnValue.split("%")[0];
    var sTplName=oArg.ReturnValue.split("%")[1];

    document.getElementById("h" + sTagID).value=oArg.DestField + ";" + nSelObjectID + ";" + oArg.SrcField;
    document.getElementById("a" + sTagID).innerHTML=sTplName;
}

function pickImage(nID) {
    var oElem=document.getElementById("v" + nID);
    var oImg=document.getElementById("img" + nID);
	if (oElem!=null && oImg!=null) {
		var sURL="/eway/library/pickerCnt.aspx?" 
			+"tree=imgpicker" 

        var oArg=new Object();
        oArg.ReturnValue=null;
        if (window.showModalDialog==null) {
            var oArg=new Object();
            oArg.ReturnValue=null;
            oArg.CallBackFunc=pickImageReturn;
            oArg.ElementId=nID;
            
            window.dialogArguments=oArg;

            window.open(sURL,"Pick Image","modal, width=630, height=450, resizable=1");
        } else {
		    window.showModalDialog(sURL,oArg,"dialogWidth:630px; dialogHeight:450px; resizable: yes");
	        if (oArg.HasResult!=null) {
		        oImg.src=oArg.ReturnValue;
		        oElem.value=oImg.src;
	        }
        }

	}
}

function pickImageReturn(oArg) {
    var nID=oArg.ElementId;
    var oElem=document.getElementById("v" + nID);
    var oImg=document.getElementById("img" + nID);
    oImg.src=oArg.ReturnValue;
    oElem.value=oImg.src;
}

function loadURLInArea(sAreaName,nUpLevel,sLoadURL) {
	var i=0;
	var sTmp=sAreaName, sCurrentURL;
	while (i>=nUpLevel) {
		var oParent=document.getElementById("par" + sAreaName);
		if (oParent!=null) { sAreaName=oParent.value; } else { sAreaName=""; };
		i--;
	}
	if (sAreaName!="") {
		sCurrentURL=window.location.href;
		sCurrentURL=setURLParam(sCurrentURL,sAreaName,"-1");
		sCurrentURL=setURLParam(sCurrentURL,"+url",sLoadURL + "&+");
		window.navigate(sCurrentURL);
	}
}

function showProperties(oEvent,ActiveTab) {
	var oNodes=document.getElementById("efmnodes");
	var oRelations=document.getElementById("efmrelations");
	var oAccess=document.getElementById("efmaccess");
	var oObjectType=document.getElementById("efmobjecttypeid");
	var oObjectID=document.getElementById("efmobjectid");

	if (oNodes!=null && oObjectType!=null && oObjectID!=null && oRelations!=null) {
		var sURL="/eway/library/pickerCnt.aspx?tree=properties" 
			+"&oid=" + oObjectID.value
			+"&ot=" + oObjectType.value
			+"&nodes=" + oNodes.value 
			+"&relations=" + oRelations.value
			+"&access=" + oAccess.value
			+"&tab=" + ActiveTab
			+"&nosave=1";

        var oArg=new DialogArgument;
        oArg.CallBackFunc=showPropertiesReturn;
        openPickerDialog(sURL,oArg,480,740);
        
        if(oEvent!=null && oEvent.preventDefault!=null) oEvent.preventDefault();
        
        return false;
	} else {
		alert("Error: Required form field missing");
	}
}

function showPropertiesReturn(oArg) {
	var oNodes=document.getElementById("efmnodes");
	var oRelations=document.getElementById("efmrelations");
	var oAccess=document.getElementById("efmaccess");
    var sReturn=oArg.ReturnValue;
	oNodes.value=sReturn.split("%")[0];
	oRelations.value=sReturn.split("%")[1];
	oAccess.value=sReturn.split("%")[2];
}

function doSubmit(Command,URL) {
	var theform;
	theform = document.getElementById("frmMain");

	theform.EWAYCOMMAND.value=Command;
	if (URL!=null && URL.length>0) { theform.action=URL; }
	theform.submit();
}

function SearchFormSubmit(URL) {
	doSubmit("ewaysearch",URL);
}

    //Finds Y (vertical) position to the element "obj"
   function findPosY(obj){
       var curPos = 0;
       if(browserTest() != "firefox"){
           while(obj){
               if(obj.style.position !="absolute"){
                   curPos += obj.offsetTop;
               }
               obj =  obj.offsetParent;
           }
       }else{
           while(obj){
               if((obj.style.position !="absolute") || (obj.tagName.toLowerCase()=="table")){
                   curPos += obj.offsetTop;
               }
               obj =  obj.offsetParent;
           }
       }
       return curPos;
   }
   
   //Finds X (horisontal) position to the element "obj"
   function findPosX(obj)
   {
       var curPos = 0;
       if(browserTest() != "firefox"){
           while(obj){
               if(obj.style.position !="absolute"){
                   curPos += obj.offsetLeft;
               }
               obj =  obj.offsetParent;
           }
       }else{
           while(obj){
               if((obj.style.position !="absolute") || (obj.tagName.toLowerCase()=="table")){
                   curPos += obj.offsetLeft;
               }
               obj =  obj.offsetParent;
           }
       }
       return curPos;
   }
   
   
   
    //Checks the client browser
   //Returns lowercase text.
   //"firefox"
   //"opera"
   //"ie"
   //"?"
   function browserTest(){
 
       if(navigator.userAgent.toLowerCase().indexOf("firefox") > -1){
           return "firefox";
       }else if(navigator.userAgent.toLowerCase().indexOf("opera") > -1){
           return "opera";
       }else if(navigator.userAgent.toLowerCase().indexOf("msie") > -1){
           return "ie";
       }else{
           return "?";
       }
   }

    var iOnload_DatePicker = 0;
    
    function onload_DatePicker(){
        if(!IsNull(document.body)){
            if(!IsIE()){
                registerDatePicker();
            }
        }else{
            if(iOnload_DatePicker < 15){
                iOnload_DatePicker++;
                window.setTimeout("onload_DatePicker();",300);
            }
        }
    }
    
    function registerDatePicker(){
        var oSpanTag = null;
        var aSpanCollection = document.body.getElementsByTagName("span");
        if((!IsNull(aSpanCollection) ) && (aSpanCollection.length > 0)){
            
            for(index = 0; index < aSpanCollection.length; index ++){
                oSpanTag = aSpanCollection[index];
                if(IsIE()){
                    if(oSpanTag.getAttribute("className") == "datePicker"){
                        oSpanTag.attachEvent("onclick",onclick_DatePicker);
                    }
                }else{
                    if(oSpanTag.getAttribute("class") == "datePicker"){
                        oSpanTag.addEventListener("click",onclick_DatePicker,false);
                    }
                }
            }
        }
    }    
    
    function IsIE() {
	    return (window.navigator.appVersion.indexOf("MSIE")!=-1);
    }
    
    function onclick_DatePicker(event,element){
        
   		var oElem = null;
		var sDateTime, sPrevDate;
		var x, y;
		
		if(IsIE()){
		    oElem = window.event.srcElement.parentNode.previousSibling;

		    x = window.event.screenX;
		    y = window.event.screenY;
		}else{
		    
		    oElem = event.target.parentNode.previousSibling;
		    x = event.screenX;
		    y = event.screenY;
		}
		
		if (oElem.tagName == "INPUT" && oElem.disabled == false) {
			sPrevDate = oElem.value;
			if(IsIE()){
			    sDateTime = window.showModalDialog("/eway/library/datepicker/datepickercnt.aspx",null,"dialogLeft:" + x + ";dialogTop:" + y + ";dialogWidth:180px; dialogHeight:210px; resizable:yes; scroll:no; status:no; help:no;");
			}else{
			    var sURL = "/eway/library/datepicker/datepickercnt.aspx";
			    var Argument = new DialogArgument();
			    Argument.Parameter = oElem;
        		window.dialogArguments = Argument;
		        var oWin = window.open(sURL,sURL,"resizable=yes, modal=yes, width=180px, height=210px,top="+ y + ",left="+x,0);
		        oWin.focus();
			}
			if (sDateTime>"") { 
			    oElem.value = sDateTime; 
			}
		}
		
		if (sPrevDate != sDateTime && oElem.onchange != null) {
			oElem.onchange();
		}

    }    
    
    function setDateTime(oElem,sDateTime){
        var sPrevDate = "";
		if (sDateTime>"") { 
		    sPrevDate = oElem.value;
		    oElem.value = sDateTime; 
		}
		if (sPrevDate != sDateTime && oElem.onchange != null) {
			oElem.onchange();
		}
    }
    
    window.setTimeout("onload_DatePicker();",300);


/* Custom for Orkla */
function DoSearch(sId,sURL) {
	var txt=document.getElementById(sId);
	if (txt.value.lastIndexOf('*') != txt.value.length-1) {
		txt.value+='*';
	}
	sURL=setURLParam(sURL,"search",txt.value);
	doSubmit("ewaysearch",sURL);
}


function printPage(nPid,nObj,sTempl) {
	var nTempl=(sTempl=='') ? 12543 : (sTempl=='person' ? 6534 : sTempl);
	var oPrintWin=window.open('/eway/default.aspx?pid='+nPid+'&oid='+nTempl+'&trg=_'+nTempl+'&_'+nTempl+'=0:'+nObj, 'eWayPrint'+nObj,'width=600,height=700,status=0,scrollbars=1,menubar=0,resizable=1');
	oPrintWin.focus();
	oPrintWin.print();
}


function validateTitle() {
	var field=document.getElementById('fldTitle');
	if (field && field.value=='') {
		alert('Vennligst angi en tittel');
		field.focus();
		return false;
	}
	return true;
}

function ec_pickImage(nID) {
	var sValue;
	var oElem=docAll("ec_fld" + nID);
	var oImg=docAll("ec_img" + nID);

	if (oElem!=null && oImg!=null) {
		var sURL="/eway/library/pickerCnt.aspx?tree=imgpicker" 
			
		sValue=window.showModalDialog(sURL, null, "dialogWidth:480px; dialogHeight:470px;");
		if (sValue!=null) {
			oImg.src=sValue;
			oElem.value=sValue;
		}
	}
}

function ec_RemoveImage(nID) {
	var oElem=docAll("ec_fld" + nID);
	var oImg=docAll("ec_img" + nID);
	oElem.value = "";
	oImg.src = "";
}

function ec_ExpandCollapse(sID) {
	oElement = document.getElementById(sID);
	if (oElement){
		if (oElement.style.display == 'none') {
			oElement.style.display = 'block';
		} else {
			oElement.style.display = 'none';
		}
	}
}
