var g_country_id = 0;
var g_province_id = 0;
var g_city = '';

var def_country_id = 0;
var def_province_id = 0;
var def_city = '';
var def_location_id = 0;


function OnSelectCountry(country_id)
{
	jQuery("#province_list_container").hide();
	jQuery('#city_list_container').hide();
	jQuery('#location_list_container').hide();
	if(jQuery("#location_result").length > 0) jQuery("#location_result").hide();	
	
	g_country_id = 0;
	g_province_id = 0;
	g_city = '';
	
	if( country_id != '' )
	{
		Ajax('locations/ajax_get_provinces/' + country_id, function(xmldom){
			g_country_id = country_id;
							
			var root = xmldom.getElementsByTagName('root')[0];
			var provinces = root.getElementsByTagName('province');
			
			var html = "";
			
			if( provinces.length )
			{
				html += "<option value=\"\">--</option>";			
			
				for( var i=0; i < provinces.length; i++ )
				{
					var province = provinces[i];
					var province_name = province.getElementsByTagName('name')[0].firstChild.nodeValue;
					var province_id = province.getElementsByTagName('id')[0].firstChild.nodeValue;
					html += "<option value=\"" + province_id + "\">" + province_name + "</option>";
				}
			}
			else
			{		
				OnSelectCity('');
			}
			
			jQuery("#province_list").html(html);
			
			if(def_province_id){
				jQuery('#province_list').val(def_province_id);
				OnSelectProvince(def_province_id);
			}
			
			if( provinces.length ) jQuery("#province_list_container").show();
		});
	}
}

function OnSelectProvince(province_id)
{
	jQuery('#city_list_container').hide();
	jQuery('#location_list_container').hide();	
	if(jQuery("#location_result").length > 0) jQuery("#location_result").hide();		
	
	g_province_id = 0;
	g_city = 0;
	
	if( province_id != '' )
	{
		//jQuery.post('locations/ajax_get_cities_by_province/' + province_id, null, function(data, response){
		
		Ajax('locations/ajax_get_cities_by_province/' + province_id, function(xmldom){
			g_province_id = province_id;
			var root = xmldom.getElementsByTagName('root')[0];
			var cities = root.getElementsByTagName('city');
		
			var html = "";			
			
			if( cities.length )
			{		
				html += "<option value=\"\">--</option>";
			
				for( var i=0; i < cities.length; i++ )
				{
					var city = cities[i];
					var city_name = city.getElementsByTagName('name')[0].firstChild.nodeValue;
					var base64_city_name = city.getElementsByTagName('base64_name')[0].firstChild.nodeValue;
					html += "<option value=\"" + base64_city_name + "\">" + city_name + "</option>";
				}
			}
			else
			{
				OnSelectCity('');
				//alert('There are no cities for the selected province');
			}
			
			jQuery("#city_list").html(html);
			
			if(def_city){
				jQuery('#city_list').val(def_city);
				OnSelectCity(def_city);
			}
			
			if( cities.length ) jQuery("#city_list_container").show();
		});
	}	
}

function OnSelectCity(city)
{
	jQuery('#location_list_container').hide();	
	if(jQuery("#location_result").length > 0) jQuery("#location_result").hide();		
	

	//jQuery.post('locations/ajax_get_locations/' + g_country_id + '/' + g_province_id + '/' + escape(city), null, function(data, response){
	var url = 'locations/ajax_get_locations/' + g_country_id + '/' + g_province_id + '/' + escape(city);
	Ajax(url, function(xmldom){
		g_city = city;

		var root = xmldom.getElementsByTagName('root')[0];
		var locations = root.getElementsByTagName('location');
	
		var html = "";			
		
		if( locations.length )
		{		
			html += "<option value=\"\">--</option>";
		
			for( var i=0; i < locations.length; i++ )
			{
				var t = null;

				var location = locations[i];

				t = location.getElementsByTagName('id')[0].firstChild;
				var location_id = ( t ? t.nodeValue : '' );

				t = location.getElementsByTagName('name')[0].firstChild;
				var location_name = ( t ? t.nodeValue : '' );

				html += "<option value=\"" + location_id + "\">" + location_name + "</option>";
			}
		}
		else
		{
			//alert('There are no locations for the selected province');
		}
		
		jQuery("#location_list").html(html);
		
		if(def_location_id){
			jQuery('#location_list').val(def_location_id);
			OnSelectLocation(def_location_id);
		}
		
		if( locations.length ) jQuery("#location_list_container").show();
	});
}

function OnSelectLocation(location_id)
{
	if(jQuery("#location_result").length > 0){
		jQuery("#location_result").hide();
	} else {
		return;
	}
	
	if( location_id != '' )
	{
		//jQuery.post('locations/ajax_get_location_by_id/' + location_id, null, function(data, response){
		Ajax('locations/ajax_get_location_by_id/' + location_id, function(xmldom){
			var root = xmldom.getElementsByTagName('root')[0];
			var locations = root.getElementsByTagName('location');
		
			if( locations.length == 1 )
			{	
				var t = null;

				var location = locations[0];
				
				t = location.getElementsByTagName('id')[0].firstChild;
				var location_id = ( t ? t.nodeValue : '' );
				
				t = location.getElementsByTagName('description')[0].firstChild;
				var location_description = ( t ? t.nodeValue : '' );

				t = location.getElementsByTagName('url')[0].firstChild;
				var location_url = ( t ? t.nodeValue : '' );

				t = location.getElementsByTagName('name')[0].firstChild;
				var location_name = ( t ? t.nodeValue : '' );

				var href = (location_url != '' ? "href=\"" + location_url + "\"" : "");

				var html = "<div class=\"pinstripes\"><div class=\"pinstripes-inner\"><p><a " + href + " target=\"_blank\">" + location_name + "</a></p>";
				html += "<div class=\"mceContentBody\">" + location_description + "</div></div></div>";
				
				if(jQuery("#location_result").length > 0){
					jQuery("#location_result").html(html);
					jQuery("#location_result").show();
				}
			}
		});
	}	
}

jQuery(document).ready(function(){
	
	if(jQuery('#country_list').length > 0){
		jQuery('#province_list_container').hide();
	} else {
		OnSelectCountry(1);
	}
	jQuery('#city_list_container').hide();
	jQuery('#location_list_container').hide();
	if(jQuery('#location_result').length > 0) jQuery("#location_result").hide();	
	
	if(jQuery('#country_list').length > 0){
		jQuery('#country_list').val("");
		jQuery('#country_list').change(function(){
			var value = jQuery(this).val();
			OnSelectCountry(value);
		});
	}
	
	jQuery('#province_list').change(function(){
		var value = jQuery(this).val();
		OnSelectProvince(value);
	});	
	
	jQuery('#city_list').change(function(){
		var value = jQuery(this).val();
		OnSelectCity(value);
	});	

	jQuery('#location_list').change(function(){
		var value = jQuery(this).val();
		OnSelectLocation(value);
	});		
});

