// Function to check if the form is valid
function formIsValid() {
	var returnValue = true;
	var panel = 'stap2';
	
	// Check if all the fields are entered
	$('input[type="text"]').each(function(){
		if($(this).val() == '') {
			if(returnValue == true) returnValue = false;
			$(this).addClass('formError');
		}
		else {
			$(this).removeClass('formError');
		}
	});
	
	// Check if the emailadress is correct
	if(validateEmail($('input[name="besteller-email"]')) == false) {
		$('input[name="besteller-email"]').addClass('formError');
	}
	else {
		$('input[name="besteller-email"]').removeClass('formError');
	}
	
	// Check if a breakfast has been selected
	if($('input[name^="amount"]').length == 0) {
		alert('U heeft nog geen ontbijt gekozen. Ga terug naar stap 1 en kies een ontbijt.');
		panel = 'stap1';
		returnValue = false;
	}
	
	// Scroll to the error
	if(returnValue == false) {
		$('#'+panel).trigger('click');
	}
	
	return returnValue;
} 

// Function to check if an emailadress is valid
function validateEmail(element) {
	var x = $(element).val();
	var atpos=x.indexOf("@");
	var dotpos=x.lastIndexOf(".");
	if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) {
		return false;
	}
	else {
		return true;
	}
}

// Function to calculate the totalprice
function calculateTotalprice() {
	var totalBreakfast = parseInt($('#totalBreakfast').val());
	
	// calculate the totalExtra's
	var totalExtras = 0;
	$('input[name^="extraAmount"]').each(function(){
		totalExtras += parseInt($(this).attr('rel')) * parseInt($(this).val());
	});
	
	$('#totalPrice').text(number_format((totalBreakfast + totalExtras)/100, 2, ',', '.'));
}

$(function() {
	// Show the details
	$('a[rel="detail"]').live('click', function(){
		$('#detailsContent').load($(this).attr('href'), function(response, status, xhr){
			$('#details').slideDown();	    		
		});
		return false;
	});
	
	// Close the details
	$('#closeDetail').live('click', function(){
		$('#details').slideUp();
		return false;
	});
	
	// Breakfast selected
	$('#orderLink').live('click', function(){
		// Show the selected breakfast in the form
		$('#breakfastHolder').load('./ajax/addBreakfast.php?breakfast=' + $(this).attr('rel'), function(){
			$('#breakfastHolder').load('./ajax/getBreakfastInfo.php');
		});		

		// Slide to the second step
		$('#'+$(this).attr('href')).trigger('click');
		$('#details').slideUp();
		return false;
	});
	
	// Select change the extra's
	$('input[name^="extraAmount"]').live('keyup', function(){
		calculateTotalprice();
	});
	
	// Handle the form
	$('input[type="text"]').live('change', function(){
		$('#' + $(this).attr('name')).text($(this).val());
	});
	
	$('input[name^="amount"]').live('keyup', function(){
		// Show the selected breakfast in the form
		$('#breakfastHolder').load('./ajax/addBreakfast.php?breakfast=' + $(this).attr('rel') + '&amount=' + $(this).val(), function(){
			$('#breakfastHolder').load('./ajax/getBreakfastInfo.php', function(){
				calculateTotalprice();
			});
		});	
	});
	
	// Handle the last step link
	$('#finishOrder').live('click', function(){
		// Check if the form is valid
		if(formIsValid() == true) {
			$('#'+$(this).attr('href')).trigger('click');
			calculateTotalprice();
		}
		
		return false;
	});
});
