/* Wandelt das Dezimalkomma in einen Dezimalpunkt um */
    function InZahl (Wert)
    {
        var PosPunkt = Wert.indexOf(".",0);
        var PosKomma = Wert.indexOf(",",0);
        if (PosKomma < 0) PosKomma = Wert.length;
		
        // Dezimalpunkte zur Tausendergruppierung entfernen
        while ((0 <= PosPunkt) && (PosPunkt < PosKomma))
        {
            Wert = Wert.substring(0, PosPunkt) + Wert.substring(PosPunkt + 1, Wert.length);
            PosPunkt = Wert.indexOf(".",0);
            PosKomma--;
        }
										    
        // Enthaelt die Variable 'Wert' ein Komma ?
        PosKomma = Wert.indexOf(",",0);
        if (PosKomma >= 0)
        {
	    Wert = Wert.substring(0, PosKomma) + "." + Wert.substring(PosKomma + 1, Wert.length);
	}

	// Sonstige Zeichen entfernen
	//Wert = Wert.replace(/[^0-9|\.]/g, "");

	Wert2 = "";
	for(i=0; i<Wert.length; i++)
	{
	    if((Wert.substr(i,1) >= "0" && Wert.substr(i,1) <= "9") || Wert.substr(i,1) == '.')
	    {
		Wert2 += Wert.substr(i, 1);
	    }
	}
	return parseFloat(Wert2);
}

function updSum(id, anzahl)
{
    if(anzahl)
    {
	preis_excl = InZahl(document.getElementById(id+'_excl').innerHTML);
	preis_incl = InZahl(document.getElementById(id+'_incl').innerHTML);

	preis_excl = Number(preis_excl * anzahl).toFixed(2);
	preis_incl = Number(preis_incl * anzahl).toFixed(2);
	
	preis_excl = preis_excl.replace(/\./, ",");
	preis_incl = preis_incl.replace(/\./, ",");
	
	preis_excl += '&nbsp;&euro;';
	preis_incl += '&nbsp;&euro;';
    }
    else
    {
	preis_excl = '';
	preis_incl = '';
    }
    
    document.getElementById(id+'_sum_excl').innerHTML = preis_excl;
    document.getElementById(id+'_sum_incl').innerHTML = preis_incl;
}

function updCalc(id, mode)
{
    if(id.indexOf('subprod') < 0)
    {
	// Hauptprodukt aktualisieren
	anzahl = InZahl(document.getElementById(id+'_anz').value);
	updSum(id, anzahl);

	// Unterprodukte aktualisieren	
	l=1;
	while((el = document.getElementById(id+'_subid'+l)))
	{
	    real_id = 'subprod'+el.value;
    
    	    if((el = document.getElementById('subprod'+el.value+'_check'))) check = el.checked;
    	    else check = false;

    	    if(anzahl != '' && (el = document.getElementById(real_id+'_defanz')))
	    {
		defanz = InZahl(el.value);
	
		if(check && (el = document.getElementById(real_id+'_anz')))
		{
		    if(anzahl && defanz) el.value = anzahl * defanz;
		    else el.value = '';
		}
	    }
	    
	    if(check) updSum(real_id, anzahl * defanz);
	    l++;
	}

    }
    else	// nur Unterprodukt aktualisieren
    {
	// Hole Anzahl des Produktes, nicht des Subprodukts!
	parentId = 'prod' + GetParentProd(id) + '_anz';
	anzahl = InZahl(document.getElementById(parentId).value);
	
        defanz = InZahl(document.getElementById(id+'_defanz').value);
	if((el = document.getElementById(id+'_check'))) check = el.checked;
        else check = false;

	if(mode == 1)
	{
    	    if(check && (anzahl * defanz > 0)) document.getElementById(id+'_anz').value = (anzahl * defanz);
    	    else document.getElementById(id+'_anz').value = '';
	}
	
	if(check) updSum(id, anzahl * defanz);
	else updSum(id, 0);
    }
}

function updCheck(id, parent_id)
{
    check = document.getElementById(id+'_check').checked;

    if(check)
    {
	anzahl = InZahl(document.getElementById(parent_id+'_anz').value);
	defanz = InZahl(document.getElementById(id+'_defanz').value);
    }
    else
    {
	anzahl = defanz = 0;
    }
    
    if(anzahl * defanz > 0) document.getElementById(id+'_anz').value = (anzahl * defanz);
    else document.getElementById(id+'_anz').value = '';
    
    updSum(id, anzahl * defanz);
}

function StartUp()
{
    el = document.getElementById('prod1_anz');
    if(el) el.focus();
    
    i=1;
    while( (el = document.getElementById('prod'+i+'_anz')) )
    {
	updCalc('prod'+i);
	i++;
    }
    
    SetupMenu();
    //SetupMenu(100);
//    setTimeout("helper()", 500);
//    sfHover();
}
function helper() {
    window.resizeBy(0, 50);
//    window.resizeBy(0, +1);
}
function SubmitCart()
{
    if((el = document.getElementById('cartForm')))
    {
	el.action = "shopping_cart.php";
	el.target = "_self";
	el.submit();
    }
}

function PrintCart()
{
    if((el = document.getElementById('cartForm')))
    {
	el.action = "print_panel.php";
	el.target = "_blank";
	el.submit();
    }
}

function SubmitList()
{
    if((el = document.getElementById('cartForm')))
    {
	el.action = "shopping_list.php";
	el.target = "_self";
	el.submit();
    }
}

function open_popup(link)
{
    myWindow = window.open(link, "", "location=no, scrollbars=yes, toolbar=no, directories=no, status=yes, menubar=no, copyhistory=no, width=900, resizable=yes");
    myWindow.focus();
}

function GetParentProd(subid)
{
    var subnr = subid.replace('subprod', '');
    var elements = document.getElementsByTagName('input');
    
    for(i=0; i<elements.length; i++)
    {
	if(elements[i].value == subnr)
	{
	    parentId = elements[i].id;
	    parentId = parentId.replace('prod', '');
	    parentId = parentId.substr(0, parentId.indexOf('_subid'));
	    return parentId;
	}
    }
    return '';
}
