// *****************************************************************
//  Copyright (c) 2004 ORISA Software GmbH All Rights Reserved.
// ORISA Software GmbH
// www.orisa.de
// *****************************************************************

// open new browser window
// *****************************************************************
var extWindows = new Array();

function openWindow(url, wName, settings)
{
	// init
	wWidth = settings.width? settings.width: 100;
	wHeight = settings.height? settings.height: 100;
	wStatus = settings.status? 1: 0;
	wResizable = settings.resize? 1: 0;
	wMenu = settings.menu? 1: false;
	wScroll = settings.scroll? 1: 0;
	wTool = settings.tool? 1: 0;

	// append time parameter
	if(url)
	{
		if(url.indexOf("?") > -1)
			url += "&RT=";
		else
			url += "?RT=";
		url += new Date().getTime();
	}
	var top = 0;
	if(screen.availHeight)
		top = (screen.availHeight - wHeight)/2;
	var left = 0;
	if(screen.availWidth)
		left = (screen.availWidth - wWidth)/2;

	// open window
	var win = null;
		try
		{
			win = window.open(url+="&WWIDTH="+wWidth+"&WHEIGHT="+wHeight, wName, 'toolbar=' + wTool + ',location=0,directories=0,status=' + wStatus + ',menubar=' + wMenu + ',scrollbars=' + wScroll + ',resizable=' + wResizable + ',left=' + left + ',top=' + top + ',width=' + wWidth + ',height=' + wHeight);
			win.focus();
		}
		catch(ex)
		{
			; //alert("openWindow() failed!");;
		}
	return win;
}
	
function resetAttribute(id)
{
	if(id)
		document.forms["sessionData"].RESET.value=id; 
	else
		document.forms["sessionData"].RESET.value="-1"; 
	submitForm();
}

// ******************************************
function submitForm()
{
	showGlasspane();
	var fData = document.forms["sessionData"];
	var wTree = topFrame.getFrameObj("tree");
	fData.SCROLL.value = wTree.document.body.scrollTop;
	fData.RT.value = new Date().getTime();
	fData.submit();
}

// ******************************************
function paramToString(pArr)
{
	var rV = "?";

	for(var i in pArr)
	{
		if(i == "cG")
			continue;
		if(rV.length > 1)
			rV += "&";
		rV += i;
		rV += "=";
		
		rV += pArr[i];
	}

	rV += "&RT=" + new Date().getTime();
	
	return rV;
}

// update container frames
// ******************************************
function handleAttribute(param)
{
	var fObj = null;
	var fV = document.forms["sessionData"].elements;
	var params = paramToString(param);
	// expand group status -> navigateTop/ container
	var expandGroup = (fV["GID"].value!="");

	// all frames loaded ?
	if(topFrame.getFrameObj("views")!=null  && topFrame.getFrameObj("container")!=null  && topFrame.getFrameObj("navigateTop")!=null  && topFrame.getFrameObj("navigateBottom")!=null  && topFrame.getFrameObj("status")!=null) 
	{
		// update view-tab 
		topFrame.getFrameObj("views").updateView(param["nV"],param["cG"],param["MV"],(param["status"]==1? fV["CONF"].value==1?2:1:0));

		// update status-tab
		topFrame.getFrameObj("status").updateView(param["sb_left"], ((param["MV"]=="1" || param["MV"]==1)?param["sb_right"]:""), param["status"]);

		// update navigate frames
		if(fV["BACK"] != param["bId"] || fV["NEXT"] != param["nId"])
		{
			topFrame.getFrameObj("navigateTop").updateView(param["bId"],param["nId"],param["MV"],param["nV"],expandGroup);
			topFrame.getFrameObj("navigateBottom").updateView(param["bId"],param["nId"],param["MV"],param["nV"]);
		}

		if(param["nV"] == VIEW_OVERVIEW)
		{
			// hide tree
			switchTree(false);
			// change to overview
			topFrame.setUrl("container", "overview" + fV["PQ"].value + ".jsp" + params);
		}
		else
		{
			// show tree
			switchTree(true);
			// change to configuration
			topFrame.setUrl("container", "attribute.jsp" + params + "&EXPGROUP=" + expandGroup);
		}
		fV["VIEW"].value = param["nV"];
		
		// 20060203; tsc; #35530; if for any reason the view changed, update buttons (different buttons for config view and overview)
		if (topFrame.getFrameObj("head").currentView!=param["nV"])
			topFrame.getFrameObj("head").updateView(param["nV"]);
		
		topFrame.setUrl("discount", "discount.jsp?RT=" + new Date().getTime());
		
		/* attribut (if attribute id was changed or the frame is empty)
		if(fV["ID"] != param["ID"])
		{
			//topFrame.setUrl("attribute", "attribute.jsp" + params); // + "&W=" + topFrame.getFrameHeight("attribute"));
			topFrame.setUrl("container", "attribute.jsp" + params); // + "&W=" + topFrame.getFrameHeight("attribute"));
		}*/

		// values
		//topFrame.setUrl("values","values.jsp" + params);
		// customizedpages?
		/*if("_ui1" == cCO.ui)
		{
			topFrame.setUrl("tkCode", "customizedpages/tkCode.jsp?RT=" + new Date().getTime());
		}*/

		// update form
		fV["ID"].value = param["ID"];
		fV["BACK"].value = param["bId"];
		fV["NEXT"].value = param["nId"];
		fV["CONF"].value = param["status"];
		fV["VIEW"].value = param["nV"];
		fV["GROUP"].value = param["cG"];
		
		// reset values
		fV["IDVAL"].value = "";
		fV["COUNT"].value = "";
		fV["NEXTATT"].value = "";
		fV["NEXTIDX"].value = "0"; // default value
		fV["EVALNEXT"].value = "";
		fV["SAVE"].value = 0;
		fV["RESET"].value = 0;
		
		hideGlasspane();
	}
	else
	{
		setTimeout("handleAttribute({ID:'" + param["ID"] + "',bId:'" + param["bId"] + "',nId:'" + param["nId"] + "',status:'" + param["status"] + "',NEXTIDX:'" + param["NEXTIDX"] + "',nV:'" + param["nV"] + "',scroll:'" + param["scroll"] + "',cG:'" + param["cG"] + "',MV:'" + param["MV"] + "'})",100);
	}
}


// change view between configuration view und overview
// ******************************************
function changeView()
{
	var fV = document.forms["sessionData"].elements;

	if(fV["VIEW"].value == VIEW_CONFIGURATION)
	{
		// change to overview
		fV["VIEW"].value = VIEW_OVERVIEW;

		switchTree(false);
		
		topFrame.setUrl("container", "overview" + fV["PQ"].value + ".jsp?CONF=" + fV["CONF"].value + "&RT=" + new Date().getTime());
		topFrame.getFrameObj("views").updateView(fV["VIEW"],fV["GROUP"].value);
		topFrame.getFrameObj("navigateTop").updateView(null,null);
		topFrame.getFrameObj("navigateBottom").updateView(null,null);		
	}
	else
	{
		switchTree(true);
		
		// forward change to tree
		fV["VIEW"].value = VIEW_CONFIGURATION;		
		submitForm();
	}
	//20050822 slz; button-frame has to be reloaded
	topFrame.getFrameObj("head").updateView(fV["VIEW"].value);
}

function switchTree(visible)
{
	var fApp = topFrame.getFrameObj("application");
	if (fApp)
	{
		if (visible && fApp.leftPane.style.visibility == "hidden")
		{
			fApp.leftPane.style.visibility = "";
			fApp.splitter.style.visibility = "";
			fApp.rightPane.style.width = fApp.innerWidth - (fApp.leftPane.offsetWidth + fApp.splitter.offsetWidth);
			fApp.rightPane.style.left = fApp.leftPane.offsetWidth + fApp.splitter.offsetWidth;
		}
		else
		if (!visible && fApp.leftPane.style.visibility != "hidden")
		{
			fApp.rightPane.style.width = fApp.rightPane.offsetWidth + fApp.leftPane.offsetWidth + fApp.splitter.offsetWidth;
			fApp.rightPane.style.left = 0;
			fApp.leftPane.style.visibility = "hidden";
			fApp.splitter.style.visibility = "hidden";
		}
	}
}

// navigate 
// ******************************************
function changeAttribute(goBack)
{
	var fV = document.forms["sessionData"].elements;
	// view?
	if(goBack)
		fV["ID"].value = fV["BACK"].value;
	else
		fV["ID"].value = fV["NEXT"].value;
	submitForm();
}

// 
// *****************************************************************
function handleAction(cName, optParam)
{
	switch (cName)
	{
		case "B_PRINT":
			showWindow('printPage.jsp?RT=' + new Date().getTime(),cName,{width:800, height:600, resize:true, menu:true, scroll:true});
			break; 
		case "B_MAIL":
			showWindow('mailframe.jsp?BODY=modul&RT=' + new Date().getTime(), cName, {width:500, height:500});
			break; 
		case "B_OFFER":
			if (optParam == OFFER_FORMAT_WORD)
				setUrl("dyn", "loadActiveX.jsp?RT=" + new Date().getTime()); 
			else if (optParam == OFFER_FORMAT_PDF)
				//setUrl("dyn", "generatePDF.jsp?RT=" + new Date().getTime());				
				showWindow('generatePDF.jsp?RT=' + new Date().getTime(), 'B_OFFER', {width:820, height:620, resize:true});
			break; 
		case "B_UPLOAD": 
			showWindow('upload.jsp?RT=' + new Date().getTime(), cName, {width:400, height:120, resize: false});			
			break; 
		case "B_ADDRESS": 
			showWindow('addressForm.jsp?RT=' + new Date().getTime(), cName, {width:400, height:360, resize: false});			
			break; 
		case "B_TEST_COMPARE": 
			showWindow('testCompare.jsp?RT=' + new Date().getTime(), cName, {width:640, height:480, resize: true, menu:true, scroll:true});
			break; 
		case "B_SAVE":
			alert(functionTxt["B_SAVE"]);
			// 20051212; tsc; save using jsp; see #33547
			setUrl("dyn", 'save.jsp?RT=' + new Date().getTime());
			break; 
		case "B_INFO":
			showWindow('fs_userinfo.html?RT=' + new Date().getTime(), cName, {width:400, height:590, resize: false});
			break; 
		case "B_NEW":
			restart(-1,"RESET=TRUE");
			break;
		case "B_DOCUMENT": 
			showWindow(escape(optParam), cName, {width:800, height:600, resize: true, menu:true, scroll:true});			
			break; 
		case "B_DOCUMENT_LIST": 
			showWindow('documents.jsp?'+((optParam!=null && typeof(optParam)!="undefined")?'CID='+escape(optParam)+"&":'')+'RT=' + new Date().getTime(), cName, {width:200, height:15, resize: false, scroll:true});
			break; 
		case "B_CHARACTERISTICS": 
			showWindow('characteristics.jsp?'+((optParam!=null && typeof(optParam)!="undefined")?'CID='+escape(optParam)+"&":'')+'RT=' + new Date().getTime(), cName, {width:250, height:50, resize: false, scroll:true});
			break; 
		case "B_ALTERNATIVES": 
			showWindow('alternatives.jsp?'+((optParam!=null && typeof(optParam)!="undefined")?'CID='+escape(optParam)+"&":'')+'RT=' + new Date().getTime(), cName, {width:450, height:50, resize: false, scroll:true});
			break; 
		case "CONFLICT": 
			showWindow('conflicts.jsp?'+((optParam!=null && typeof(optParam)!="undefined")?'CID='+escape(optParam)+"&":'')+'RT=' + new Date().getTime(), cName, {width:500, height:150, resize: false, scroll:true});
			break; 
		case "SHOW_LOAD_ERRORS": 
			showWindow('loadErrors.jsp?'+((optParam!=null && typeof(optParam)!="undefined")?'CID='+escape(optParam)+"&":'')+'RT=' + new Date().getTime(), cName, {width:500, height:150, resize: false, scroll:true});
			break; 
		case "SET_BY_RULE": 
			showWindow('setByRule.jsp?'+((optParam!=null && typeof(optParam)!="undefined")?'CID='+escape(optParam)+"&":'')+'RT=' + new Date().getTime(), cName, {width:500, height:150, resize: false, scroll:true});
			break; 
		// case "B_OFFER_SHORT"?
	}
	return false; 
}
function showWindow(url, wKey, settings)
{
	switch(wKey)
	{
		case "CONFLICT":
		case "SHOW_LOAD_ERRORS":
		case "SET_BY_RULE":
		case "B_ALTERNATIVES": 
		case "B_CHARACTERISTICS": 
		case "B_DOCUMENT_LIST":
		case "B_UPLOAD":
		case "B_ADDRESS":
		case "B_MAIL":
			showGlasspane();

			var cWidth = settings.width? settings.width: 100;
			var cHeight = settings.height? settings.height: 100;
			var wInfo = getFrameInfo(self);
			var cTop = (wInfo.offsetHeight - cHeight)/2;
			var cLeft = (wInfo.offsetWidth - cWidth)/2;
			
			var obj = document.getElementById("f_dyn");
			with (obj.style)
			{
				top = cTop;
				left = cLeft;
				zIndex = 30001;
				width = cWidth + "px";
				height = cHeight + "px";
				borderTopColor = "#89A0C6";
				borderLeftColor = "#89A0C6";
				display = "block";
			}
			obj.src = url;
			break;
		case "B_TEST_COMPARE":
		case "B_DOCUMENT":
		case "B_PRINT":
		case "B_INFO":
		case "B_OFFER":
		case "ADDWIN":
			/* "ADDWIN" is used by user defined links! */
			if(wKey == "ADDWIN" && settings)
			{
				if(settings.height)
					settings.height += 20;
				if(settings.width)
					settings.width += 20;
			}
			// real window
			var w = extWindows[wKey];	
			if (w && !w.closed)
				w.close();	
			extWindows[wKey] = openWindow(url, wKey, settings);
			break;
	}
}

function hideWindow()
{
	var obj = document.getElementById("f_dyn");
	obj.src = "empty.html";
	obj.style.display = "none";
	hideGlasspane();
}

// 
// *****************************************************************
function removeWindows()
{
	var w = null;
	for(var i in extWindows)
	{
		w = extWindows[i];

		if (w && !w.closed)
			w.close();
	}
}
// *****************************************************************
function refresh() 
{
	// cause view refresh
	topFrame.document.forms["sessionData"].elements["VIEW"].value = VIEW_CONFIGURATION;

	// switch to overview
	topFrame.setUrl("tree", "tree.jsp?VIEW=" + VIEW_OVERVIEW + "&RT="+ new Date().getTime());
}

// add/ update field value in specified form
// *****************************************************************
function addEntryToForm(id, value)
{
	var form = document.forms["sessionData"];
	var entry = form.elements[id];
	if(typeof entry == "undefined" && value != null)
	{
		entry = document.createElement('INPUT');
		entry.type = "hidden";
		entry.name = id;
		form.appendChild(entry);
		
		// workaround: nach appendChild wird das entry nicht gefunden mit form.entryName
		form.elements[id] = entry;
	}
	if(entry)
		entry.value = value;
}

/* ************************************************************ */
	//  click tree
	// ******************************************
	function notifyView(cId)
	{
		if(cId)
		{
			// register only
			topFrame.document.forms["sessionData"].elements["TREE_ID"].value = cId;
		}
	}
	
	function checkView(cId)
	{
		if(cId)
		{
			// update + submit form
			topFrame.document.forms["sessionData"].elements["ID"].value = cId;
			topFrame.document.forms["sessionData"].elements["VIEW"].value = VIEW_CONFIGURATION;
			topFrame.submitForm();
		}
	}
	
	// toggle group in tree
	// ******************************************
	function toggle(id)
	{
		var trContent = document.getElementById("c_" + id);
		var tButton = document.getElementById("t_" + id);
		if (trContent!=null)
		{
			if (trContent.className)
			{
				// open group
				trContent.className = "";
				tButton.src=imgT.src;
				if(ttCloseGroup)
					tButton.title=ttCloseGroup;
				topFrame.addEntryToForm("HIDE_" + id,"")
			}
			else
			{
				trContent.className = "noContent";
				tButton.src=imgTC.src;
				if(ttOpenGroup)
					tButton.title=ttOpenGroup;
				topFrame.addEntryToForm("HIDE_" + id,"true");
			}
		}
	}

	function resizeTreeWidth()
	{
		var fWidth = getFrameInfo(self).offsetWidth;
		if(fWidth > 80)
		{
			var wTitle = (fWidth - 65) + "px";
			var wContainer = (fWidth - 20) + "px";
			var divs = document.getElementsByTagName("DIV");
			for (i=0; i<divs.length; i++)
			{
				cDiv = divs[i];
				if(cDiv.className=="groupTitle")
					cDiv.style.width=wTitle;
				else if(cDiv.className=="group_container")
					cDiv.style.width=wContainer;
			}
		}
	}

	//
	function expandGroupAttribute(gId, cId)
	{
		if(gId == null)
			// reset
			topFrame.addEntryToForm("GID","");
		else
		{
			topFrame.addEntryToForm("GID", gId);
			topFrame.addEntryToForm("ID", cId);
		}
		submitForm();
	}
	//
	function getAbsoluteOffset(win, element, jPos, withParentFrames)
	{
		// offset inside the frame
		while (element)
		{
			jPos.top  += element.offsetTop;
			jPos.left += element.offsetLeft + 1;
			element = element.offsetParent;
		}
		if(withParentFrames)
		{
			// offset caused by parent frames
			element = win;
			while(element && element.name)
			{
				os = element.parent.document.getElementById(element.name);
				if(os)
				{
					jPos.top += os.offsetTop;
					jPos.left += os.offsetLeft + 1;
				}
				jPos.top   -= element.document.body.scrollTop;
				jPos.left  -= element.document.body.scrollLeft;
				if(element.name != top.name)
					element = element.parent;
				else
					element = null;
			}
		}
		return jPos;
	}
	
	// scrollable table:
	function resizeContainer(dWidth, dHeight, dPadding)
	{
		var obj = document.getElementById("scrollContainer");
		var fInfo = getFrameInfo(self);
		if(obj)
		{
			if(dHeight)
			{
				// 20050830; tsc; "-3" = fix for broken vertical scrollbar in result list when resizing
				maxH= fInfo.offsetHeight-dHeight - 3;
				cH = document.getElementById("tableContent").offsetHeight;
				if(maxH > 0)
					obj.style.height=((cH+5)<maxH?cH+5:maxH)+ "px";
			}
			if(dWidth && dWidth < fInfo.offsetWidth)
				obj.style.width=(fInfo.offsetWidth-dWidth)+ "px";
			obj.style.overflow="auto";

			var tbl = document.getElementById("tableContent");
			var tblHead = document.getElementById("tableHeader");
			if (tbl && tblHead)
			{
				if (obj.offsetHeight<obj.scrollHeight)
				{
					tblHead.rows[0].cells[tblHead.rows[0].cells.length-1].style.paddingRight = "24px";
					if (tblHead.rows.length>1)
						tblHead.rows[1].cells[tblHead.rows[1].cells.length-1].style.paddingRight = "24px";
				}
				else
				{
					tblHead.rows[0].cells[tblHead.rows[0].cells.length-1].style.paddingRight = (dPadding+2)+"px";
					if (tblHead.rows.length>1)
						tblHead.rows[1].cells[tblHead.rows[1].cells.length-1].style.paddingRight = (dPadding+2)+"px";
				}
			}
			//setTimeout("adjustHeadWidth(" + dPadding + ");",4);
		}
	}

	function adjustHeadWidth(dPadding)
	{
		var tbl = document.getElementById("tableContent");
		var cells = tbl.rows[0].cells;
		for (var i = 0; i < cells.length; i++)
		{
			if(document.getElementById("hd" + i) != null)
				document.getElementById("hd" + i).style.width = cells[i].offsetWidth-dPadding;
		}
	}
	// end scrollable table
