function checkVariants(selector){
	//var childDivs = selector.nextUntil('.topvariant', '.childVariantDiv');
	var rel = selector.attr('rel');
	var sel = 'div[rel="' + rel +'"]';
	var childDivs = $(sel);
	var value = selector.val();
	
	childDivs.css('display','none');
	childDivs.each(function() {
		if($(this).attr('id') == ('childVariant_' + value) ){
			$(this).css('display','block');
		}
	});
}

jQuery(document).ready(function(){
	$('#country,#eutaxid').change(function() {
		var country =  $('#country option:selected');
		var taxId =  $('#eutaxid').val();
		var euMember = country.attr("rel");
		if(euMember == 1 && taxId.length == 0 && country.attr('value') != 54){
			$('#tax-warning').css('display','block');
		}else{
			$('#tax-warning').css('display','none');
		}
	});
	
	//check variants
	$('.topvariant').change(function() {
		checkVariants($(this));
	});
	$('.topvariant').each(function() {
		checkVariants($(this));
	});
	
	//check form submits
	$('.itemform').submit(function() {
		$('.subvariant').each(function() {
			console.log('Checking ' + $(this).parent().attr('id'));
			if($(this).parent().css('display') == 'none'){
				console.log('Removing ' + $(this).parent().attr('id'));
				$(this).remove();
			}
		})
	});
});
