var amounts = new Object();
var bmpages = new Object();
amounts[selectedid] = new Object();
function showRoomDetails(pos)
{
	var obj=document.getElementById('room-details-'+pos);
	obj.style.visibility='visible';
	obj=document.getElementById('room-details-main');
	if (obj!=null)
	{
	obj.style.visibility='hidden';
	}
	obj=document.getElementById('room-hover-image-'+pos);
	if (obj!=null)
	{
	obj.style.display='block';
	}

}

function showLayer(id)
{
	var obj=document.getElementById(id);
		
	if (obj!=null)
	{
		obj.style.visibility='visible';
		obj.style.display='block';
	}	

}

function hideRoomDetails(pos)
{
	var obj=document.getElementById('room-details-'+pos);
	obj.style.visibility='hidden';
	obj=document.getElementById('room-details-main');
	if (obj!=null)
	{
	obj.style.visibility='visible';
	}
	obj=document.getElementById('room-hover-image-'+pos);
	if (obj!=null)
	{
	obj.style.display='none';
	}

}

function hideLayer(id)
{
	var obj=document.getElementById(id);

	if (obj!=null)
	{
		obj.style.visibility='hidden';
		obj.style.display='none';
	}
}

function checkValue(obj)
{
	
}

function makeAmountsPostable()
{
	budget = document.getElementById("constant-budget").value;
	list = "";
	for (a in amounts)
	{
//alert(a.toString());
		for (b in amounts[a])
		{
			//alert(b.toString());
			for (c in amounts[a][b])
			{	
				if (list!='') list += ":";
				list += "item[uri("+a+"),domain("+b+"),position("+c+")";
				list += ",amount("+amounts[a][b][c]["amount"]+")";
				list += ",price("+amounts[a][b][c]["price"]+")";
				list += "]";
				//alert(c.toString());
				for (d in amounts[a][b][c])
				{
					//alert(d.toString()+","+amounts[a][b][c][d]);
				}
			}
		}
	}
	sendlist = "calculator[budget("+budget+")].items("+list+")";

	calcnsp = sendlist;
	//document.getElementById("output-new").value = calcnsp;
	updateLinks();
}

function addPageToBankettmappe(uri)
{
	obj = document.getElementById("mbm-link");
	col = obj.style.color;
	obj.style.color = "#aaaadd";
	if (!bmpages[uri])
	{
		//alert("uri:"+uri.toString());
		bmpages[uri] = uri;								
	}
	list = "";
	for (a in bmpages)
	{
		
		if (list!='') list += ":";
		list += "item[uri("+a+")]";
		//alert(c.toString());
			
	}
	sendlist = "bankettmappe.items("+list+")";

	bmnsp = sendlist;
	var nspele = new NSPNamespaceElement();

	nspele.setNSPserialized(bmnsp);

	bmxml = nspele.getXElement().xml;

//alert(bmnsp);
	//document.getElementById("output-new").value = calcnsp;
	updateLinks();
	obj.color = col;
}

function removePageFromBankettmappe(uri)
{
	if (bmpages[uri])
	{
		//alert("uri:"+uri.toString());
		delete bmpages[uri];
										
	}
	list = "";
	for (a in bmpages)
	{
		
		if (list!='') list += ":";
		list += "item[uri("+a+")]";
		//alert(c.toString());
			
	}
	sendlist = "bankettmappe.items("+list+")";

	bmnsp = sendlist;
	var nspele = new NSPNamespaceElement();
	nspele.setNSPserialized(bmnsp);
	bmxml = nspele.getXElement().xml;
//alert(bmnsp);
	//document.getElementById("output-new").value = calcnsp;
	updateLinks();
}

function setAmounts()
{
	lnsp = calcnsp;
//alert(lnsp);
	var nspele = new NSPNamespaceElement();
	nspele.setNSPserialized(lnsp);
	itemsnsp = nspele.selectSingleNSP("items");
	itemarr = new Array(0);
	if (itemsnsp != null) itemarr = itemsnsp.NSPList.getAsArray();
//alert(itemarr.length);
	for (index=0;index<itemarr.length;index++)
	{
		latb = itemarr[index].getAttributeElement();
		if (latb != null)
		{
			luri = "";
			ldomain = "";
			lposition = "";
			lprice = "";
			lamount = "";
			latb2 = latb.selectSingleNSP("uri");
			if (latb2 != null) luri = latb2.getValue();
			latb2 = latb.selectSingleNSP("domain");
			if (latb2 != null) ldomain = latb2.getValue();
			latb2 = latb.selectSingleNSP("position");
			if (latb2 != null) lposition = latb2.getValue();
			latb2 = latb.selectSingleNSP("price");
			if (latb2 != null) lprice = latb2.getValue();
			latb2 = latb.selectSingleNSP("amount");
			if (latb2 != null) lamount = latb2.getValue();	
			updateAmounts(luri,ldomain,lposition,lamount,lprice);
		}
	}
	
}

function setBankettmappe()
{
	lnsp = bmnsp;
//alert(lnsp);
	var nspele = new NSPNamespaceElement();
	nspele.setNSPserialized(lnsp);
	itemsnsp = nspele.selectSingleNSP("items");
	itemarr = new Array(0);
	if (itemsnsp != null) itemarr = itemsnsp.NSPList.getAsArray();
//alert(itemarr.length);
	for (index=0;index<itemarr.length;index++)
	{
		latb = itemarr[index].getAttributeElement();
		if (latb != null)
		{
			luri = "";			
			latb2 = latb.selectSingleNSP("uri");
			if (latb2 != null) luri = latb2.getValue();
			if (luri!='')
			if (!bmpages[luri])
			{
				//alert("uri:"+uri.toString());
				bmpages[luri] = luri;								
			}
			
		}
	}
	
}

function setCurrentPageCalculator()
{
	lnsp = calcnsp;
	//document.getElementById("output-new").value = calcnsp;
//alert(lnsp);
	var nspele = new NSPNamespaceElement();
	nspele.setNSPserialized(lnsp);
	//document.getElementById("output-new").value = nspele.getNSPserialized()
	latb = nspele.getAttributeElement();
//alert(latb.getName());
//alert(latb.getValue());
	if (latb!=null) latb = latb.selectSingleNSP('budget');
//alert(latb.getName());
//alert(latb.getValue());
	budget = latb.getValue();
	if (sellang == 'de') budget = budget.replace('.',',');
	if (latb!=null) document.getElementById("constant-budget").value = budget;
	
	for (b in amounts[selectedid])
	{

		for (c in amounts[selectedid][b])
		{	
			nsppos =  new NSPNamespaceElement();
		
			nsppos.setNSPserialized(c);
			
			itemobj = null;
			if (nsppos.getParameter()!="")
			{
				itemobj = document.getElementById("item-"+nsppos.getName()+"-"+nsppos.getParameter());
			}else itemobj = document.getElementById("item-"+nsppos.getName());
			amount = amounts[selectedid][b][c]["amount"];
			if (sellang == 'de') amount = amount.replace('.',',');
			if (itemobj!=null) itemobj.value = amount;
			
		}
	}
	updateCalculator();
}

function updateCalculator()
{
	budget = document.getElementById("constant-budget").value;
	if (sellang == 'de') budget = budget.replace(',','.');
	sumpage = sumCurrentPage();
	sumequip = sumEquipments();
	sumprov = sumProvisionFee();
	sumfood = sumFood();
	sumbev = sumBeverage();
	sumpac = sumPackage();
	sumb = sumBusiness();
	sumroom = sumRentRate();
	sumcost = sumequip+sumfood+sumbev+sumpac+sumprov+sumb;
	restbudget = budget-(sumequip+sumfood+sumbev+sumpac+sumb+sumprov);
//alert(sumpage);
	if (sellang == 'de')
	{
		sumpage = sumpage.toString().replace('.',',');
		sumprov = sumprov.toString().replace('.',',');
		sumequip = sumequip.toString().replace('.',',');
		sumfood = sumfood.toString().replace('.',',');
		sumbev = sumbev.toString().replace('.',',');
		sumpac = sumpac.toString().replace('.',',');
		sumb = sumb.toString().replace('.',',');
		sumcost = sumcost.toString().replace('.',',');
		sumroom = sumroom.toString().replace('.',',');
		restbudget = restbudget.toString().replace('.',',');

	}
	document.getElementById("sum-costs-current-page").value = sumpage;
	document.getElementById("costs-room-provision-fee").value = sumprov;
	document.getElementById("costs-equipment-domain").value = sumequip;
	document.getElementById("costs-food-domain").value = sumfood;
	document.getElementById("costs-beverage-domain").value = sumbev;
	document.getElementById("costs-package-domain").value = sumpac;
	document.getElementById("costs-business-center-domain").value = sumb;	
	document.getElementById("costs-room-domain").value = sumroom;
	document.getElementById("sum-costs").value = sumcost;
	
	document.getElementById("rest-budget").value = restbudget;
	
}

function sumCurrentPage()
{
	sumitem = 0;
	//if (selectedid == 'costs-room-domain') return 0;
	//alert(selectedid);
	for (b in amounts[selectedid])
	{

		for (c in amounts[selectedid][b])
		{	
			if (c!="rent-rate")
			{
				if (!isNaN(amounts[selectedid][b][c]["price"]) && !isNaN(amounts[selectedid][b][c]["amount"]))
				sumitem = sumitem + amounts[selectedid][b][c]["amount"] * amounts[selectedid][b][c]["price"];
			}
			
		}
	}
	return sumitem;
}

function sumProvisionFee()
{
	sumitem = 0;
	
	if (sumPackage()==0)
	for (a in amounts)
	{
		//alert(a);
		if (amounts[a]["room-domain"] != null)
		{
			if (amounts[a]["room-domain"]["provision-fee"]!=null)
			{
				if (!isNaN(amounts[a]["room-domain"]["provision-fee"]["price"]) && !isNaN(amounts[a]["room-domain"]["provision-fee"]["amount"]))
				sumitem = sumitem + amounts[a]["room-domain"]["provision-fee"]["amount"] * amounts[a]["room-domain"]["provision-fee"]["price"];
			}
		}
	} else sumitem = 0;
	
	return sumitem;
}

function sumRentRate()
{
	sumitem = 0;
	
	//if (sumPackage()==0)
	for (a in amounts)
	{
		//alert(a);
		if (amounts[a]["room-domain"] != null)
		{			
			
			if (amounts[a]["room-domain"]["rent-rate"]!=null)
			{
				if (!isNaN(amounts[a]["room-domain"]["rent-rate"]["price"]) && !isNaN(amounts[a]["room-domain"]["rent-rate"]["amount"]))
				sumitem = sumitem + amounts[a]["room-domain"]["rent-rate"]["amount"] * amounts[a]["room-domain"]["rent-rate"]["price"];
			}
		}
	} //else sumitem = 0;
	return sumitem;
}

function sumFood()
{
	sumitem = 0;
	for (a in amounts)
	{
		//alert(a);
		if (amounts[a]["food-domain"] != null)
		{
			for (c in amounts[a]["food-domain"])
			{	
				//alert(c);
				if (!isNaN(amounts[a]["food-domain"][c]["price"]) && !isNaN(amounts[a]["food-domain"][c]["amount"]))
				sumitem = sumitem + amounts[a]["food-domain"][c]["amount"] * amounts[a]["food-domain"][c]["price"];
				
			}
		}
	}
	return sumitem;
}

function sumBeverage()
{
	sumitem = 0;
	for (a in amounts)
	{
		if (amounts[a]["beverage-domain"] != null)
		{
			for (c in amounts[a]["beverage-domain"])
			{	
				if (!isNaN(amounts[a]["beverage-domain"][c]["price"]) && !isNaN(amounts[a]["beverage-domain"][c]["amount"]))
				sumitem = sumitem + amounts[a]["beverage-domain"][c]["amount"] * amounts[a]["beverage-domain"][c]["price"];
				
			}
		}
	}
	return sumitem;
}

function sumBusiness ()
{
        sumitem = 0;
        for (a in amounts)
        {
                if (amounts[a]["business-center-domain"] != null)
                {
                        for (c in amounts[a]["business-center-domain"])
                        {
                                if (!isNaN(amounts[a]["business-center-domain"][c]["price"]) && !isNaN(amounts[a]["business-center-domain"][c]["amount"]))
                                sumitem = sumitem + amounts[a]["business-center-domain"][c]["amount"] * amounts[a]["business-center-domain"][c]["price"];

                        }
                }
        }
        return sumitem;
}

function sumEquipments()
{
	sumitem = 0;
	for (a in amounts)
	{
		if (amounts[a]["equipment-domain"] != null)
		{
			for (c in amounts[a]["equipment-domain"])
			{	
				if (!isNaN(amounts[a]["equipment-domain"][c]["price"]) && !isNaN(amounts[a]["equipment-domain"][c]["amount"]))
				sumitem = sumitem + amounts[a]["equipment-domain"][c]["amount"] * amounts[a]["equipment-domain"][c]["price"];
				
			}
		}
	}
	return sumitem;
}

function sumPackage()
{
	sumitem = 0;
	for (a in amounts)
	{
		if (amounts[a]["package-domain"] != null)
		{
			for (c in amounts[a]["package-domain"])
			{	
				if (!isNaN(amounts[a]["package-domain"][c]["price"]) && !isNaN(amounts[a]["package-domain"][c]["amount"]))
				sumitem = sumitem + amounts[a]["package-domain"][c]["amount"] * amounts[a]["package-domain"][c]["price"];
				
			}
		}
	}
	return sumitem;
}

function updateBudget()
{
	var budget = document.getElementById("constant-budget").value;
	if (sellang == 'de') budget = budget.replace(',','.');
	if (calcnsp == 'null') calcnsp = "calculator[budget("+budget+")]";
	lnsp = calcnsp;
	//alert(lnsp);
	var nspele = new NSPNamespaceElement();
	nspele.setNSPserialized(lnsp);
	
	latb = nspele.getAttributeElement();

	if (latb!=null) latb = latb.selectSingleNSP('budget');

	if (latb!=null) latb.setValue(budget);
	
	calcnsp = nspele.getNSPserialized();
	
	updateLinks();
	
}

function updateAmounts(uri,domain,position,amount,price)
{
	if (!amounts[uri])
	{
//alert("uri:"+uri.toString());
		amounts[uri] = new Object();								
	}
	
	if (!amounts[uri][domain])
	{
//alert("domain:"+domain.toString());
		amounts[uri][domain] = new Object();								
	}
	
	if (!amounts[uri][domain][position])
	{
//alert("position:"+position.toString());
		amounts[uri][domain][position] = new Object();
	}
	
	if (sellang == 'de') amount = amount.replace(',','.');
	amounts[uri][domain][position]["amount"] = amount;
//alert("amount:"+amount.toString());
	if (mainlang == 'de') price = price.replace(',','.');
	amounts[uri][domain][position]["price"] = price;
//alert("price:"+price.toString());
	
	
}

function updateLinks()
{
	links = document.getElementsByTagName("a");
	for (index=0;index< links.length;index++)
	{
		if (links[index].href.indexOf("?values")!=-1)
		{
			links[index].href=links[index].href.substring(0,links[index].href.indexOf("?values"))+"?";
		}
		
		if (links[index].href.indexOf("&values")!=-1)
		{
			links[index].href=links[index].href.substring(0,links[index].href.indexOf("&values"))+"&";
		}
		
		if (links[index].href.indexOf("?")==-1)
		{
			links[index].href=links[index].href+"?";
		}else if(links[index].href.indexOf("?values")==-1 && links[index].href.indexOf("&values")==-1)
		{
			links[index].href=links[index].href+"&";
		}
		
		links[index].href=links[index].href+"values[calculator-nsp]="+escape(calcnsp)+"&values[bankettmappe-nsp]="+escape(bmnsp)+"&xmlvalues[banquetmap-pages-xml]="+bmxml;
		//alert(links[index].href=links[index].href);
	}
}

function initPage()
{
	nspele = new NSPNamespaceElement();
	nspele.setNSPserialized(bmnsp);
	bmxml = nspele.getXElement().xml;
	setBankettmappe();
	setAmounts();
	setCurrentPageCalculator();
	updateLinks();
}


