$(document).ready(function(){
	
	var langCode = location.pathname.substr(1, 2);
	function populate(url, id){
		$('#' + id).after(' <img src="/img/ajax-loader.gif" id="spinner" />');
		$.getJSON(url, function(data){
			if(id == 'countryId'){
				clear('regionId');
				clear('townId');
				clear('subTownId');
			} else if(id == 'regionId'){
				clear('townId');
				clear('subTownId');
			} else if(id == 'townId'){
				clear('subTownId');
			}
			document.getElementById(id).options.length = 0;
                        
                        var keys = [];
                        $.each(data , function(key, value){
                                if(key!=0)
                                        keys.push(value);
                        });
                        
                        //flip the name/value pairs
                        var value;
                        var datar = {};
                        for(value in data) {
                               datar[data[value]] = value;
                        }
                        var aa = keys.sort();
                        
                        document.getElementById(id).options[0] = new Option(data[0], 0);
                        
			var i = 1;
			$.each(aa, function(key, value){
				document.getElementById(id).options[i++] = new Option(value, datar[value]);
			});
			if(document.getElementById(id).options.length > 1){
				document.getElementById(id).style.display = 'inline';
				$('#regionId, #townId, #subTownId, #street1').each(function(key, el){
					$(el).parents('tr').show();
				});
			} else {
				document.getElementById(id).style.display = 'none';
				$('#regionId, #townId, #subTownId, #street1').each(function(key, el){
					$(el).parents('tr').hide();
				});
			}
			$('#spinner').remove();
		});
	}
	function clear(id){
		document.getElementById(id).options.length = 0;
		document.getElementById(id).style.display = 'none';
	}
	$('#countryId').change(function(){
                if(escape($(this).val()) != 0){
                        populate("/" + langCode + "/default/locations/regions/countryId/" + escape($(this).val()), 'regionId');
                } else {
                        clear('regionId');
                        clear('townId');
                        clear('subTownId');
                }
	});
	$('#regionId').change(function(){
                if(escape($(this).val()) != 0){
                        populate("/" + langCode + "/default/locations/towns/regionId/" + escape($(this).val()), 'townId');
                } else {
                        clear('townId');
                        clear('subTownId');
                }
	});
	$('#townId').change(function(){
                if(escape($(this).val()) != 0){
                        populate("/" + langCode + "/default/locations/subtowns/townId/" + escape($(this).val()), 'subTownId');
                } else {
                        clear('subTownId');
                }
	});
	if($('#countryId').val() && !$('#regionId').val()){
		$('#countryId').change();
	}	
	
	function setExemptFromVat(){
		// auto set 'exempt from VAT' to on for Schengen countries
		var euCountries = [];
		euCountries['Austria'] = 7;
		euCountries['Czech Republic'] = 8;
		euCountries['Denmark'] = 9;
		euCountries['Estonia'] = 10;
		euCountries['Finland'] = 11;
		euCountries['France'] = 2;
		euCountries['Germany'] = 12;
		euCountries['Greece'] = 13;
		euCountries['Hungary'] = 14;
		euCountries['Iceland'] = 15;
		euCountries['Italy'] = 16;
		euCountries['Ireland'] = 4;
		euCountries['Latvia'] = 29;
		euCountries['Lithuania'] = 30;
		euCountries['Luxembourg'] = 17;
		euCountries['Malta'] = 36;
		euCountries['Netherlands'] = 3;
		euCountries['Norway'] = 18;
		euCountries['Poland'] = 19;
		euCountries['Portugal'] = 20;
		euCountries['Slovakia'] = 23;
		euCountries['Slovenia'] = 41;
		euCountries['Spain'] = 24;
		euCountries['Sweden'] = 25;
		euCountries['Switzerland'] = 26;
		var found = false;
		for(var country in euCountries){
			if($('#countryId').val() == euCountries[country]){
				found = true;
				break;
			}
		}
		// console.log('is eu country: ' + found);
		// console.log('vat: ' + $('#vatnumber').val());
		if(found && $('#vatnumber').val()){
			$('#isexemptfromvat').attr('checked', true);
		} else {
			$('#isexemptfromvat').removeAttr('checked');
		}
	}

	if($('#isexemptfromvat').length){
		setExemptFromVat();
		$('#countryId, #vatnumber').change(function(){
			setExemptFromVat();
		});
	}
	
});
