function recalculatePrice()
{
	// set up the data string
	var data = 'action=calculateprice';
	
	// add data from the selects
	data += '&aid=' + $('#aid option:selected').attr('value');
	data += '&lid=' + $('#lid option:selected').attr('value');

	// make the ajax call
	$.ajax({
		method: "get", url: "ajax.php", data: data,
		success: function(html){ $('#price').html(html); } // write result into #price
	});
}

$(document).ready(function()
{ 
	var ajaxOrderFormOptions = { 
	beforeSend: function(){ return validateFormOnSubmit(document.getElementById('order_form')); },
	success: function(html){
	 $('#order_form').hide();
	 $('#order_form_results').html(html);
	
	 $('#close_form_popup-text').click(function(){
	  $('#form_popup').fadeOut(500); 
	  $("#order_form").resetForm();
	 });
	},
	clearForm: true
	};
	$('#order_form').ajaxForm(ajaxOrderFormOptions); 
	
	
	var ajaxRecommendFormOptions = { 
	beforeSend: function(){ return validateRecommendForm(document.getElementById('recommend_form')); },
	success: function(html){
	 $('#recommend_form').hide();
	 $('#recommend_form_results').html(html);
	
	 $('#close_tellafriend_popup-text').click(function(){
	  $('#tellafriend_popup').fadeOut(500); 
	  $("#recommend_form").resetForm();
	 });
	},
	clearForm: true
	};
	$('#recommend_form').ajaxForm(ajaxRecommendFormOptions); 
	

	$('#land').change(function()
	{ 
	// fetch the article price
		var land = $("#land option:selected").attr('value');
		$("#lid").find("option[value="+land+"]").attr("selected","selected");
		
		var aid = $("#aid option:selected").attr('value');
		 $.ajax({
		  method: "get", url: "ajax.php", data: "action=shipmentprice&aid="+aid+"&lid="+land,
		  success: function(html){ $("#shipmentprice").html(html); } // write the price into html
		 });

		// recalculate total price
		recalculatePrice(); 
	});
	
	
	$('#title').change(function()
	{ 
		// fetch the article price
		var title = $("#title option:selected").attr('value');
		
		if (title == "Herr" || title == "Frau" || title == "Mr" || title == "Ms"){ 
			$("#firmenname").fadeTo("slow", 0.33);
			$("#UIDNR").fadeTo("slow", 0.33);
			$("#firmenname-text").fadeTo("slow", 0.33);
			$("#UIDNR-text").fadeTo("slow", 0.33); // ausblenden firma und UID-Nr
		}
		else if (title == "Firma" || title == "Company"){ 
			$("#firmenname").fadeTo("slow", 1);
			$("#UIDNR").fadeTo("slow", 1);
			$("#firmenname-text").fadeTo("slow", 1); 
			$("#UIDNR-text").fadeTo("slow", 1);  // ausblenden firma und UID-Nr
		}
	});
	
	$('#aid').change(function()
	{ 
		// fetch the article price
		var aid = $("#aid option:selected").attr('value');
		var lid = $("#lid option:selected").attr('value');
		
		$.ajax({
			method: "get", url: "ajax.php", data: "action=articleprice&aid="+aid,
			success: function(html){ 
			$("#articleprice").html(html);} // write the price into html
		});
		 $.ajax({
		  method: "get", url: "ajax.php", data: "action=shipmentprice&aid="+aid+"&lid="+lid,
		  success: function(html){ $("#shipmentprice").html(html); } // write the price into html
		 });

		// recalculate total price
		recalculatePrice(); 
	});

	$('#lid').change(function()
	{ 
	 // fetch the shipment price
	 var aid = $("#aid option:selected").attr('value');
	 var lid = $("#lid option:selected").attr('value');
	 $("#land").find("option[value="+lid+"]").attr("selected","selected");
	
	 $.ajax({
	  method: "get", url: "ajax.php", data: "action=shipmentprice&aid="+aid+"&lid="+lid,
	  success: function(html){ $("#shipmentprice").html(html); } // write the price into html
	 });
	
	 // recalculate total price
	 recalculatePrice();
	});
		
	
	
	
	$('#pop_me_up').click(function(){ 
		$('#order_form').show();
		$('#order_form_results').html('');
		$('#form_popup').fadeIn(500);
		$('#tellafriend_popup').fadeOut(500);
		$('#agbg_popup').fadeOut(500);
		return false; });
	$('#btn-jetztbestellen').click(function(){ 
		$('#order_form').show();
		$('#order_form_results').html('');
		$('#form_popup').fadeIn(500);
		$('#tellafriend_popup').fadeOut(500);
		$('#agbg_popup').fadeOut(500);
		return false; });
	
	$('#btn-weiterempfehlen').click(function(){
		$('#recommend_form').show();
		$('#recommend_form_results').html('');
		$('#tellafriend_popup').fadeIn(500); 
		$('#form_popup').fadeOut(500);
		$('#agbg_popup').fadeOut(500);
		 return false;
		 });																		   
	
	$('#btn-agb').click(function(){ 
		$('#agbg_popup').fadeIn(500); 
		$('#form_popup').fadeOut(500);
		$('#tellafriend_popup').fadeOut(500);
		return false; });
	$('#btn-agb-text').click(function(){ $('#agbg_popup').fadeIn(500); return false; });
	
	$('#close_form_popup').click(function(){ 
		$('#form_popup').fadeOut(500); 
		$("#order_form").resetForm();
		$("#shipmentprice").html("--,--");
		$("#price").html("--,--");
		$("#articleprice").html("89,00");
		});
	$('#close_form_popup-text').click(function(){ 
		$('#form_popup').fadeOut(500);  
		$("#order_form").resetForm();
		$("#shipmentprice").html("--,--");
		$("#price").html("--,--");
		$("#articleprice").html("89,00");
		});
	$('#close_tellafriend_popup').click(function(){ $('#tellafriend_popup').fadeOut(500);  $("#recommend_form").resetForm(); });
	$('#close_tellafriend_popup-text').click(function(){ $('#tellafriend_popup').fadeOut(500);  $("#recommend_form").resetForm(); });
	$('#close_agbg_window').click(function(){ $('#agbg_popup').fadeOut(500); return false; });
	$('#close_agbg_window-text').click(function(){ $('#agbg_popup').fadeOut(500); return false; });
});

