
function formatZahl(zahl, k, fix) {

    if(!k) k = 0;

    var neu = '';

 

	var dec_point = ',';

	var thousands_sep = '.';

 

    // Runden

    var f = Math.pow(10, k);

    zahl = '' + parseInt(zahl * f + (.5 * (zahl > 0 ? 1 : -1)) ) / f ;

 

    // Komma ermittlen

    var idx = zahl.indexOf('.');

 

    // fehlende Nullen einfügen

    if(fix)    {

         zahl += (idx == -1 ? '.' : '' )

         + f.toString().substring(1);

    }

	var sign = zahl < 0;

	if(sign) zahl = zahl.substring(1);

    idx = zahl.indexOf('.');

 

	// Nachkommastellen ermittlen

    if( idx == -1) idx = zahl.length;

    else neu = dec_point + zahl.substr(idx + 1, k);

 

 

    while(idx > 0)    {

        if(idx - 3 > 0)

        neu = thousands_sep + zahl.substring( idx - 3, idx) + neu;

        else

        neu = zahl.substring(0, idx) + neu;

        idx -= 3;

    }

    return (sign ? '-' : '') + neu;

}

function getPreis(id)
       {
         $.get('db.xml', function(d){   
         $(d).find('data').each(function(){  
			var $data = $(this);  
			if ($data.find("id").text() == id)
			 {
				var price = $data.find("preis").text();
				preis = price;
				alert(preis);
			}	
         })
     })
 }  

function berechnePreis()
 {
	var total = parseFloat(model) + parseFloat(laenge) + parseFloat(gestell) + parseFloat(tischplatte) + parseFloat(rueckwand);
	for (option in zubehoer) {
		total += parseFloat(zubehoer[option]);
	}
	//Versand
	total += parseFloat(versand);
	//Aufbauservice
	//total += parseFloat(aufbauservice);
	total = total * parseInt(menge);
	total = formatZahl(total, 2, true) + " EUR" ;
	$("#total").text(total); 
 }
  
  
window.onload = function(){  berechnePreis();} 
 
 
$(document).ready(function(){
 $("#rueckwand").change( function() 
      {
		 var id = document.getElementById('rueckwand').value;
         $.get('db.xml', function(d){   
         $(d).find('data').each(function(){  
			var $data = $(this);  
			if ($data.find("id").text() == id)
			 {
				var price = $data.find("preis").text();
				rueckwand = price;
				berechnePreis();
			}	
         })
     })
})
});

$(document).ready(function(){
 $("#gestell").change( function() 
      {
		 var id = document.getElementById('gestell').value;
         $.get('db.xml', function(d){   
         $(d).find('data').each(function(){  
			var $data = $(this);  
			if ($data.find("id").text() == id)
			 {
				var price = $data.find("preis").text();
				gestell = price;
				berechnePreis();
			}	
         })
     })
})
});

$(document).ready(function(){
 $("#tischplatte").change( function() 
      {
		 var id = document.getElementById('tischplatte').value;
         $.get('db.xml', function(d){   
         $(d).find('data').each(function(){  
			var $data = $(this);  
			if ($data.find("id").text() == id)
			 {
				var price = $data.find("preis").text();
				tischplatte = price;
				berechnePreis();
			}	
         })
     })
})
});


    

$(document).ready(function(){
 $("#laenge").change( function() 
      {
		 var id = document.getElementById('laenge').value;
         $.get('db.xml', function(d){   
         $(d).find('data').each(function(){  
			var $data = $(this);  
			if ($data.find("id").text() == id)
			 {
				var price = $data.find("preis").text();
				laenge = price;
				berechnePreis();
			}	
         })
     })
})
}); 

$(document).ready(function(){
 $("#laenge").change( function() 
      {
		 var id = document.getElementById('laenge').value;
         $.get('db.xml', function(d){   
         $(d).find('data').each(function(){  
			var $data = $(this);  
			if ($data.find("id").text() == id)
			 {
				var price = $data.find("preis").text();
				laenge = price;
				berechnePreis();
			}	
         })
     })
})
}); 

$(document).ready(function(){
 $("#laenge").change( function() 
      {
		 var id = document.getElementById('laenge').value;
         $.get('db.xml', function(d){   
         $(d).find('data').each(function(){  
			var $data = $(this);  
			if ($data.find("id").text() == id)
			 {
				var price = $data.find("preis").text();
				laenge = price;
				berechnePreis();
			}	
         })
     })
})
}); 




$(document).ready(function(){
 $("#model").change( function() 
      {
		 var id = document.getElementById('model').value;
         $.get('db.xml', function(d){   
         $(d).find('data').each(function(){  
			var $data = $(this);  
			if ($data.find("id").text() == id)
			 {
				if (id == "WickeltischWandmodul")
				{
						$("#Rollen").attr("disabled", true);
						$("#Rollen").attr("checked", false);
						zubehoer["Rollen"]=0;
						$("#wickeltisch").attr("src", "images/wickeltisch_wandmodul.jpg");
				}
				else
				{
					$("#Rollen").removeAttr('disabled'); 
					$("#wickeltisch").attr("src", "images/wickeltisch_120.jpg");
				}
				var price = $data.find("preis").text();
				model = price;
				berechnePreis();
			}	
         })
     })
})
});

$(document).ready(function(){
 $(":checkbox").click( function() 
      {
		$(':checkbox[name=\'zubehoer[]\']').each(function() {
        var $checkbox = $(this);
		if ($checkbox.attr('checked') == true)
		{
		 var id = $checkbox.val();
         $.get('db.xml', function(d){   
         $(d).find('data').each(function(){  
			var $data = $(this);  
			if ($data.find("id").text() == id)
			 {
				var price = $data.find("preis").text();
				zubehoer[$checkbox.val()] = price;
				berechnePreis();
			}	
         })
     })
		}
		else
		{
			zubehoer[$checkbox.val()] = 0;
			berechnePreis();
		}
      }
    )
	})
 })



$(document).ready(function(){
$("input[name='versand']").click(function()
{
if ($("input[@name='versand']:checked").val())
	{
		 var id = $(this).val();
         $.get('db.xml', function(d){   
         $(d).find('data').each(function(){  
			var $data = $(this);  
			if ($data.find("id").text() == id)
			 {
				var price = $data.find("preis").text();
				versand = price;
				berechnePreis();
			}	
         })
})  
}; 
});
});  
   
   
$(document).ready(function(){
$("input[name='aufbauservice']").change(function()
{
if ($("input[@name='aufbauservice']:checked").val())
	{
		 var id = $(this).val();
         $.get('db.xml', function(d){   
         $(d).find('data').each(function(){  
			var $data = $(this);  
			if ($data.find("id").text() == id)
			 {
				var price = $data.find("preis").text();
				aufbauservice = price;
				berechnePreis();
			}	
         })
})  
}; 
});
});     
 

 $(document).ready(function(){
 $("#bestellen").click( function(){
		if ($("#agb").attr('checked') == true)
			document.formular.submit();
		else
			alert('Bitte akzeptieren Sie unsere AGB, um Ihre Bestellung aufzugeben.');
  
})
});

 $(document).ready(function(){
 $("#menge").keyup( function(){
		menge = $(this).val();
		if (isNaN(menge) || $(this).val() == "" || menge > 100)
		{
			menge = 1;				
			$(this).val(1);
		}
		berechnePreis();
  
})
});
