


$(document).ready(function() {
	if ($('select#region_select').length>0) {
		
	
	// ======================================
	// = CHANGE HERE: path for the xml file =
	// ======================================
	var xmlfile_path = site_root + "/wp-content/uploads/wwoffices/wwoffices.xml";
	// var xmlfile_path = "wwoffices.xml";

	
	// $.get(xmlfile_path, {}, function(xml) {
	$.ajax({
		type: 'GET',
		url: xmlfile_path,
		data: '',
		// success: handle_xml,
		success: function(xml) {
			try //Internet Explorer
			  {
			  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
			  xmlDoc.async="false";
			  xmlDoc.loadXML(xml);
			  }
			catch(e)
			  {
			  try //Firefox, Mozilla, Opera, etc.
			    {
			    parser=new DOMParser();
			    xmlDoc=parser.parseFromString(xml,"text/xml");
			    }
			  catch(e) {alert(e.message)}
			  }
			
			// console.log(xmlDoc);
			handle_xml(xmlDoc);
		},
		error: 	function (XMLHttpRequest, textStatus, errorThrown) {
		  alert('Error: '+ XMLHttpRequest +' ::: '+ textStatus +' ::: '+ errorThrown);
		},
	
	timeout: 5000,
	dataType: 'text'
	
	});
	}; // if
});

function handle_xml(xml) {
	// Loaded so replace "loading..." text
	$('#region_select').empty();
	// $('#region_select').append(new Option("Escolha uma Região", ""));
	$('#region_select').append("<option value=''>Escolha uma Região</option>");
	reset_country_select(true);
	reset_city_select(true);


	// Enable and populate the regions select.
	$('#region_select').attr('disabled','');
	$('region', xml).each(function () {
		var region_name = $(this).attr('name');
		// $('#region_select').append(new Option(region_name, region_name));
		$('#region_select').append("<option value='"+ region_name +"'>"+ region_name +"</option>");
	});

	// Assign change event for region select
	$('#region_select').change(function() {
		var selected_region_value = $(this).find('option:selected').attr('value');
	
		// Reset further otpions
		reset_country_select(true);
		reset_city_select(true);
		$('#wwoffices_results').empty();
	
		// If none selected stop
		if (selected_region_value == '') {
			return false;
		};
	
		// Enable and populate country select
		$('#country_select').attr('disabled','');
		$("region[name='"+ selected_region_value +"'] > country", xml).each(function () {
			var country_name = $(this).attr('name');
			// $('#country_select').append(new Option(country_name, country_name));
			$('#country_select').append("<option value='"+ country_name +"'>"+ country_name +"</option>");
		});
	});


	// Assign change event for country select
	$('#country_select').change(function () {
		var selected_country_value = $(this).find('option:selected').attr('value');
	
		// Reset further otpions
		reset_city_select(true);
		$('#wwoffices_results').empty();
	
		// If none selected stop
		if (selected_country_value == '') {
			return false;
		};
	
		// Enable and populate city select
		$('#city_select').attr('disabled','');
		$("country[name='"+ selected_country_value +"'] > city", xml).each(function () {
			var city_name = $(this).attr('name');
			// $('#city_select').append(new Option(city_name, city_name));
			$('#city_select').append("<option value='"+ city_name +"'>"+ city_name +"</option>");
		});
		// alert($('#city_select').html())
	});


	// Assign change event for city select
	// Show search results
	$('#city_select').change(function () {
		var selected_city_value = $(this).find('option:selected').attr('value');
	
		// Reset further otpions
		$('#wwoffices_results').empty();
	
		// If none selected disable further option
		if (selected_city_value == '') {
			return false;
		};
	
		// Clean and populate results div
		var html_string = "<ul>";
		$("city[name='"+ selected_city_value +"'] office", xml).each(function() {
			html_string += "<li>";
			html_string += "<p><span>Escritório:</span><span class='leftPadd-90'><strong>"+ $(this).find("office_name").text() +"</strong></span></p>";
			html_string += "<p><span>Morada:</span><span class='leftPadd-90'>"+ $(this).find("address").text() +"</span></p>";
			html_string += "<p><span>País:</span><span class='leftPadd-90'>"+ $(this).find("address_country").text() +"</span></p>";
			html_string += "<p><span>Telefone:</span><span class='leftPadd-90'>"+ $(this).find("address_phone").text() +"</span></p>";
			html_string += "<p><span>Fax:</span><span class='leftPadd-90'>"+ $(this).find("address_fax").text() +"</span></p>";
			html_string += "<p><span>&nbsp;</span><span class='leftPadd-90'>&nbsp;</span></p></li>";
		});
		html_string += "</ul>";
		$('#wwoffices_results').append(html_string);
	});



	function reset_country_select(disable) {
		$('#country_select').empty();
		// $('#country_select').append(new Option("Escolha um País", ""));
		$('#country_select').append("<option value=''>Escolha um País</option>");
		if(disable) {
			$('#country_select').attr('disabled','disabled');
		};
	};
	function reset_city_select(disable) {
		$('#city_select').empty();
		// $('#city_select').append(new Option("Escolha uma Cidade", ""));
		$('#city_select').append("<option value=''>Escolha uma Cidade</option>");
		if(disable) {
			$('#city_select').attr('disabled','disabled');
		};
	};
}
