var european_union_countries = ['AT', 'BE', 'BG', 'CY', 'CZ', 'DE', 'DK', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'HU', 'IE', 'IT', 'LT', 'LU', 'LV', 'MT', 'NL', 'PL', 'PT', 'RO', 'SE', 'SI', 'SK'];

$(function(){
	
	$(".showterms").colorbox({inline:true, href:"#termbox"})
	
	$("input[type=text]").focus(function() {
        $(this).css("background-color","#FFFFFF");
    });
    $("input[type=text]").blur(function() {
        $(this).css("background-color","#F7F7F7");
    });
	
	$("#formbox_form").validate(
								{rules: 
										{vat: 	 {required: function(element) {return isEu($('#country').val());}
											     }
										}
								}
	);
	
	$('#submitnext').click(function(){
	
		if ($('#terms').length > 0){
			if ($('#terms').attr('checked')){
				return true;
			}else{
				alert('You have to accept the general terms of delivery');
				return false;
			}
		}
		
		if ($('#totaal').length > 0){
			if ($('td.standard input:checked,td.full input:checked,td.optional input:checked').length > 0){
				return true;
			}else{
				alert('You have to select at least one module');
				return false;
			}
		}
		
	});
	
	if($('#country').val()){
		isEu($('#country').val());
	}
	
	$('#submitform').click(function(){
		
		if(isEu($('#country').val())){
			validateVAT($('#vat').val());
			
			if ($('#vatchecked').val() == 'true'){
				return true;
			}else{
				return false;
			}
		}else{
			return true;
		}
	});
	
	$('#country').change(function(){
		isEu($(this).val());
	});
	
	if($('#cart').val() && $('#preview').length > 0){
			updateSettings();
	}else{
		if ($('#preview').length > 0){
			updateTotal();
		}
	}
	
	$('td.full input').click(function(){
		if ($(this).attr('checked')){
			$('td.optional input').each(function(){
				if($(this).val() > 0){
					$(this).attr('checked','checked');
					$(this).attr('disabled','disabled');
				}
			});
			$('td.standard input').attr('checked','checked').attr('disabled','disabled');
			updateTotal();
		}else{
			$('td.optional input').each(function(){
				if($(this).val() > 0){
					$(this).removeAttr('checked');
					$(this).removeAttr('disabled');
				}
			});
			updateTotal();
		}
	});
	$('td.standard input').click(function(){
		$('td.optional input').each(function(){
			if($(this).val() > 0){
				$(this).removeAttr('checked');
				$(this).removeAttr('disabled');
			}
		});
		updateTotal();
	});
	
	$('td.license input').click(function(){
		if ($(this).attr('checked')){
			$('td.text input').each(function(){
				if($(this).val() > 0){
					$(this).removeAttr('checked');
				}
			});
		}else{
			$('td.text input').each(function(){
				if($(this).val() > 0){
					$(this).attr('checked','checked');
					$(this).attr('disabled','disabled');
				}
			});
		}
		updateTotal();
	});
	
	$('td.optional input,td.shipping input,td.courier input,td.maintenance input,td.text input').click(function(){
		updateTotal();
	});
		
});

function updateTotal(){
	var total = 0;
	var count = 0;
	var subtotal = 0;
	var cart  = '';
	var total_count = $('input:checked').length;
	
	if ($('td.full input').attr("checked")){
		total = parseInt($('td.full input').val());
		cart = cart + '#code_'+$('td.full input').attr("id");
		$('td.optional input').attr('checked','checked').attr('disabled','disabled');
		$('td.text input:checked').each(function(){
			total = parseInt(total) + parseInt($(this).val());
			cart = cart + ',#code_'+$('td.text input').attr("id");
		});
		if($('td.maintenance input:checked').length > 0){
			cart = cart + ',#code_'+$('td.maintenance input:checked').attr("id");
		}
		if ($('td.license input:checked').length > 0){
			cart = cart + ',#code_'+$('td.license input').attr("id");
		}
	}else{
		$('input:checked').each(function(){
			count++;
			total = parseInt(total) + parseInt($(this).val());
			cart = cart + '#code_'+$(this).attr("id");
			if(count < total_count){
				cart = cart + ',';
			}
		});
	}
	
	if ($('td.license input').attr("checked")){
		if (!($('td.full input').attr("checked"))){
			total = total - parseInt($('td.license input').val());
		}
		subtotal = parseInt(total) * (parseInt($('td.license input').val()) / 100);
		$('#license_extra').text(subtotal).formatCurrency({symbol: ''});
		$('#license_valuta').show();
		$('#license_extra').show();
		total = parseInt(subtotal) + parseInt(total);
	}else{
		$('#license_extra').hide();
		$('#license_valuta').hide();
	}

	$('#totaal').text(total).formatCurrency({symbol: ''});
	
	$('#cart').val(cart);
	if($('#shipping').length > 0){
		$('#totaal').text(parseInt(total) + parseInt($('#shipping').val())).formatCurrency({symbol: ''});
	}
};

function updateSettings(){

	var cart_saved = $('#cart').val();
	var split	   = cart_saved.split(',');
	var count 	   = 0;
	while (count < split.length)
	{
		id = $(split[count]).parents('tr').attr('id');
		$('#'+id.substring(id.indexOf('_')+1)).attr('checked','checked');
	    if($('#'+id.substring(id.indexOf('_')+1)).parent('td').hasClass('full')){
			$('#'+id.substring(id.indexOf('_')+1)).parents('tbody').children('tr.optional').attr('checked','checked').attr('disabled','disabled');
		}
		if($('#'+id.substring(id.indexOf('_')+1)).parent('td').hasClass('license')){
			$('td.text input').removeAttr('checked');
		}
		count+=1;
	}
	updateTotal();
};

function isEu(val){
	var isEu = 0;
	for (row = 0; row < european_union_countries.length; ++row) {
		if (european_union_countries[row] == val){
			isEu++;
		}
	}
	
	if (isEu > 0){
		if(val == 'NL'){
			$('#vatrow').hide();
			return false;
		}else{
			$('#vatrow').show();
			return true;
		}
	}else
	{
		$('#vatrow').hide();
		return false;
	}
};

function validateVAT(val){
	$.getJSON('http://isvat.appspot.com/'+$('#country').val()+'/'+val+'/?callback=?', function(data){
		if (data != true){
			alert('Your VAT number cannot be validated');
			$('#vatchecked').val(data);
			$('#vat').focus();
		}else{
			$('#vatchecked').val(data);
		}
	});
};
