// ##############################################################################
// #                                                   
// # Einheiteumrechner
// #                           
// ##############################################################################

 
var UnitCalc = false;
var nOldX;
var nOldY;
var bVerschieben = false;

function startUnitCalc (strHTML)
{	
	if ( !UnitCalc ) 
	{
		UnitCalc = document.createElement('div');
		UnitCalc.setAttribute( 'id', 'UNIT_CALC');
		UnitCalc.style.position = "absolute";
		UnitCalc.style.width = "420px";
		UnitCalc.style.height = "120px";
		UnitCalc.style.top = "72px";
		UnitCalc.style.left = "564px";
		UnitCalc.style.padding = "5px";
		
		UnitCalc.style.zIndex = 100000;
		
		UnitCalc.style.border = "0px outset";
		UnitCalc.style.backgroundColor = "RGB(220,228,237)";

		document.body.appendChild( UnitCalc );

	}
	
	UnitCalc.style.visibility = 'visible';

	UnitCalc.innerHTML = strHTML;

	aMoveObj = parent.parent.document.getElementById ('UNIT_CALC_MOVE');

	if (aMoveObj && aMoveObj.addEventListener)
	{
		aMoveObj.addEventListener ("mousedown", startMoveObj, false);
	}
	else if (aMoveObj && aMoveObj.attachEvent)
	{
		aMoveObj.attachEvent ("onmousedown", startMoveObj);
	}
}


function moveObj(e)
{
	var event = e || window.event;

	var aMoveObj = parent.parent.document.getElementById ('UNIT_CALC')

	if (bVerschieben && event && aMoveObj)	{
		var nX = event.screenX;
		var nY = event.screenY;
		var nDeltaX = nX - nOldX;
		var nDeltaY = nY - nOldY;

		aMoveObj.style.left = parseInt(aMoveObj.style.left) + parseInt(nDeltaX) + "px";
		aMoveObj.style.top  = parseInt(aMoveObj.style.top) + parseInt(nDeltaY) + "px";

		nOldX = nX;
		nOldY = nY;
		return false;
	}
}


function startMoveObj(e)
{
	var event = e || window.event;

	if (event)
	{
		nOldX = event.screenX;
		nOldY = event.screenY;
	
		var aMoveObj = parent.parent.document.getElementById ('UNIT_CALC')
	
		if (aMoveObj && aMoveObj.addEventListener)
		{
			aMoveObj.addEventListener ("mousemove", moveObj, true);
			aMoveObj.addEventListener ("mouseup", stopMoveObj, true);
		}
		else if (aMoveObj && aMoveObj.attachEvent)
		{
			aMoveObj.attachEvent ("onmousemove", moveObj);
			aMoveObj.attachEvent ("onmouseup", stopMoveObj);
		}
	
		bVerschieben = true;
	}
}

function stopMoveObj(e)
{
	bVerschieben = false;
}


function ZAUC_AttrCBChanged (oAttrCB)
{	
	var strNewValue = oAttrCB.value;
	
	var aTD1 = parent.parent.document.getElementById ('UC_COMBO_1');
	var aTD2 = parent.parent.document.getElementById ('UC_COMBO_2');
	
	aTD1.innerHTML = strNewValue.replace ("UC_DB_0X", "UC_DB_01").replace ("UC_DB_0X", "UC_DB_01");
	aTD2.innerHTML = strNewValue.replace ("UC_DB_0X", "UC_DB_02").replace ("UC_DB_0X", "UC_DB_02");

	ZAUC_CalcResult ();
}

function ZAUC_ChangeCBs ()
{
	// ComboBox-Inhalt auslesen und anpassen
	var aAttrCombo = parent.parent.document.getElementById ('UNIT_CALC_ATTR_CB');
	var strNewValue = aAttrCombo.value;

	var strNewTD1 = strNewValue.replace ("UC_DB_0X", "UC_DB_01").replace ("UC_DB_0X", "UC_DB_01");
	var strNewTD2 = strNewValue.replace ("UC_DB_0X", "UC_DB_02").replace ("UC_DB_0X", "UC_DB_02");

	// Selektierte Werte mit Anzeigetext ermitteln und austauschen
	var aTD1Value = parent.parent.document.getElementById ('UC_DB_01');
	var aTD2Value = parent.parent.document.getElementById ('UC_DB_02');

	var strTD1Text = aTD1Value.options[aTD1Value.selectedIndex].innerHTML;
	var strTD2Text = aTD1Value.options[aTD2Value.selectedIndex].innerHTML;

	strReplace2From = "value=\"" + aTD1Value.value + "\">" + strTD1Text; 
	strReplace2To = "value=\"" + aTD1Value.value + "\" selected>" + strTD1Text; 

	strReplace1From = "value=\"" + aTD2Value.value + "\">" + strTD2Text; 
	strReplace1To = "value=\"" + aTD2Value.value + "\" selected>" + strTD2Text; 

	strNewTD1 = strNewTD1.replace (strReplace1From, strReplace1To);
	strNewTD2 = strNewTD2.replace (strReplace2From, strReplace2To);

	// Neue Combo-Inhalte ssetzen
	var aTD1 = parent.parent.document.getElementById ('UC_COMBO_1');
	var aTD2 = parent.parent.document.getElementById ('UC_COMBO_2');

	aTD1.innerHTML = strNewTD1;
	aTD2.innerHTML = strNewTD2;

	ZAUC_CalcResult ();
}

function ZAUC_CalcResult ()
{
	var aEdit = parent.parent.document.getElementById ('UC_EDIT_O1');

	// BTS 0008834
	var strEditValue = aEdit.value;
	strEditValue = strEditValue.replace (",", ".");
		
	var xValue =  parseFloat(strEditValue);
	var yValue;

	var aTD1Value = parent.parent.document.getElementById ('UC_DB_01');
	var aTD2Value = parent.parent.document.getElementById ('UC_DB_02');

	if (xValue && aTD1Value && aTD2Value)
	{
		// Sonderfall Fahrenheit
		if (aTD1Value.value == "ZA_GF")
		{
			yValue = (xValue - 32.0) / 1.8;
		}
		else if (aTD2Value.value == "ZA_GF")
		{
			yValue = (xValue * 1.8) + 32.0;
		}
		else
		{		
			var dFaktor1 = parseFloat(aTD1Value.value);
			var dFaktor2 = parseFloat(aTD2Value.value);
	
			yValue = xValue * dFaktor1 / dFaktor2;
		}
		
		var aEdit2 = parent.parent.document.getElementById ('UC_EDIT_O2');
	
		aEdit2.value = ZAUC_RoundNumber (yValue, 7, true);
	}
}

function ZAUC_RoundNumber (x, n, bNoFill) 
{
	if (n < 1 || n > 14) return false;
	var e = Math.pow(10, n);
	var k = (Math.round(x * e) / e).toString();
	if (bNoFill)
	{
		return k;
	}
	else
	{	
		if (k.indexOf('.') == -1) k += '.';
		k += e.toString().substring(1);
		return k.substring(0, k.indexOf('.') + n+1);
	}
}
