$(document).ready(function(){
var n = 1; //Ändrad från 1
/*$('.potatoes:first select, .potatoes:first input').each(function(){
	var classtitle = $(this).attr('class');
	$(this).attr('name', classtitle+'['+n+']');
});*/
$('#add').click(function(){
	n++;
	/*$('.potatoes:last').clone(true).insertAfter('.potatoes:last');
	$('.potatoes:last').attr('id', n);
	$('.potatoes:last select:not(.type,.quantity) option').remove();
	$('.potatoes:last select, .potatoes:last input').each(function(){
		var classtitle = $(this).attr('class');
		$(this).attr('name', classtitle+'['+n+']');
	});*/
	/* Lagts till */$('#p'+n).css({
		'display':'block'
	});
	if(n>1){
		$('#remove').removeAttr('disabled');
	}else{
		$('#remove').attr('disabled', 'disabled');
	}
	/* Lagts till */if(n<10){
		$('#add').removeAttr('disabled');
	}else{
		$('#add').attr('disabled', 'disabled');
	}
	return false;
});
$('#remove').click(function(){
	//$('.potatoes').last().remove();
	/* Lagts till */$('#p'+n).css({
		'display':'none'
	});
	$('#p'+n).find('select').not('.type,.quantity').children().remove();
	$('#p'+n).find('.quantity').val('0')
	n--;
	
	if(n>1){
		$('#remove').removeAttr('disabled');
	}else{
		$('#remove').attr('disabled', 'disabled');
	}
	/* Lagts till */if(n<10){
		$('#add').removeAttr('disabled');
	}else{
		$('#add').attr('disabled', 'disabled');
	}
	return false;
});
$('select.type').click(getCountry);
$('select.country').change(getClass);
$('select.class').change(getFraction);	
$('select.fraction').change(getPackage);
function getCountry(){
	var object = $(this);
	var type_val = $(this).val();
	$(this).parent().siblings().children('.class').children().remove();
	$(this).parent().siblings().children('.fraction').children().remove();
	$(this).parent().siblings().children('.package').children().remove();
	$.post('../bestallsida/populate.php', {type : type_val}, function populateCountry(countryResponse){
		var id = object.parents('.potatoes').attr('id');
		$('#'+id).find('.country').html(countryResponse);
	});
}
function getClass(){
	var object = $(this);
	var country_val = $(this).val();
	var type_val = $(this).parent().siblings().children('.type').val();	
	$(this).parent().siblings().children('.fraction').children().remove();
	$(this).parent().siblings().children('.package').children().remove();
	$.post('../bestallsida/populate.php', {type : type_val, country : country_val}, function populateClass(classResponse){
		var id = object.parents('.potatoes').attr('id');
		$('#'+id).find('.class').html(classResponse);
	});
}
function getFraction(){
	var object = $(this);
	var class_val = $(this).val();
	var country_val = $(this).parent().siblings().children('.country').val();	
	var type_val = $(this).parent().siblings().children('.type').val();	
	$(this).parent().siblings().children('.package').children().remove();
	$.post('../bestallsida/populate.php', {selclass : class_val, type : type_val, country : country_val}, function populateFraction(fractionResponse){
		var id = object.parents('.potatoes').attr('id');
		$('#'+id).find('select.fraction').html(fractionResponse);
	});	
}
function getPackage(){
	var object = $(this);
	var fraction_val = $(this).val();
	var country_val = $(this).parent().siblings().children('.country').val();
	var class_val = $(this).parent().siblings().children('.class').val();
	var type_val = $(this).parent().siblings().children('.type').val();
	$.post('../bestallsida/populate.php', {fraction : fraction_val, selclass : class_val, type : type_val, country : country_val}, function populatePackage(packageResponse){
		var id = object.parents('.potatoes').attr('id');
		$('#'+id).find('select.package').html(packageResponse);
	});
}
$('#submit').click(validate);
function validate(){
	if($('.potatoes select.type').val()==''){alert('Var god och fyll i en hel rad för att kunna fortsätta.');return false;}
	if($('.potatoes select.country').val()==''){alert('Var god och fyll i en hel rad för att kunna fortsätta.');return false;}
	if($('.potatoes select.class').val()==''){alert('Var god och fyll i en hel rad för att kunna fortsätta.');return false;}
	if($('.potatoes select.fraction').val()==''){alert('Var god och fyll i en hel rad för att kunna fortsätta.');return false;}
	if($('.potatoes select.package').val()==''){alert('Var god och fyll i en hel rad för att kunna fortsätta.');return false;}
	/*if($('#buyer').val()==''){$('#buyer').focus();  $('#buyer').addClass('error'); return false }else{ $('#buyer').removeClass('error'); }
	if($('#deliveryaddress').val()==''){$('#deliveryaddress').focus();  $('#deliveryaddress').addClass('error'); return false }else{ $('#deliveryaddress').removeClass('error'); }
	if($('#invoiceaddress').val()==''){$('#invoiceaddress').focus(); $('#invoiceaddress').addClass('error');return false }else{ $('#invoiceaddress').removeClass('error'); }*/
	if($('#buyer').val()!='' && $('#deliveryaddress').val()!='' || $('#invoiceaddress').val()!=''){
		$('*').removeClass('error');
	}else{
		$('#buyer').focus();
		$('#buyer, #deliveryaddress').addClass('error'); 
		return false
	}
}
$('#mail').click(function(){
	var message = $('div.mail').html();
	$.post('mail.php',{message: message}, function succsess(){
		alert('Tack för din beställning vi kommer skicka en beställningsbekräftelse till din så snart vi motagit beställningen.');
	});
});
$('#print').click(function(){
	window.print();
});
});
