function createAddress(from) {

	var saveFrom = from;
	var ac = new AjaxConfigurator();

	ubications = $('textarea[name=ubications]:visible').val();
	ubications = unescape(ubications.replace(/\n/g, '-separator-'));

	$.ajax({
	  	url: ac.url + 'crear-direccion/',
		type: 'POST',
		data: 'from=' + from + '&name=' + $('input[name=name]:visible').val() +
			'&id_loc_country=' + $('select[name=id_loc_country]:visible').val() + 
			'&id_loc_region=' + $('select[name=id_loc_region]:visible').val() + 
			'&city=' + $('input[name=city]:visible').val() + 
			'&address=' + $('input[name=address]:visible').val() + 
			'&address_number=' + $('input[name=address_number]:visible').val() + 
			'&address_floor=' + $('input[name=address_floor]:visible').val() + 
			'&postcode=' + $('input[name=postcode]:visible').val() + 
			'&ubications=' + ubications +
			'&gmaps_lat=' + $('input[name=gmaps_lat]:hidden').val() +
			'&gmaps_lng=' + $('input[name=gmaps_lng]:hidden').val() +
			'&gmaps_address=' + $('input[name=gmaps_address]:hidden').val(),
	 	cache: false,
	  	success: function(html) {
			a = eval(html);
			if(a.from == 'ad') {
				$('#address-result').html(a.list);
				$('#id_address_ubication').html(a.select);
			} else if(a.from == 'user') {
				$('#address-result').html(a.table);
			} else if(a.from == 'stuff') {
				$('#id_address_ubication').html(a.select);
			}
			$.fn.fancybox.close();
	  	}
	});
}

function modifyAddress(id) {

	var ac = new AjaxConfigurator();

	ubications = $('textarea[name=ubications]:visible').val();
	ubications = unescape(ubications.replace(/\n/g, '-separator-'));

	address = $('input[name=address]:visible').val();
	address_number = $('input[name=address_number]:visible').val();

	city = $('input[name=city]:visible').val();

	id_region = $('select[name=id_loc_region]:visible').val();
	region = $('select[name=id_loc_region]:visible option[value="' + id_region + '"]').text();

	id_country = $('select[name=id_loc_country]:visible').val();
	country = $('select[name=id_loc_country]:visible option[value="' + id_country + '"]').text();

	//alert($('input[name=gmaps_address]:hidden').val());

	$.ajax({
	  	url: ac.url + 'modificar-direccion/',
		type: 'POST',
		data: 'id=' + id + '&name=' + $('input[name=name]:visible').val() +
			'&id_loc_country=' + id_country + 
			'&id_loc_region=' + id_region + 
			'&city=' + city + 
			'&address=' + address + 
			'&address_number=' + address_number + 
			'&address_floor=' + $('input[name=address_floor]:visible').val() + 
			'&postcode=' + $('input[name=postcode]:visible').val() + 
			'&ubications=' + ubications + 
			'&gmaps_lat=' + $('input[name=gmaps_lat]:hidden').val() +
			'&gmaps_lng=' + $('input[name=gmaps_lng]:hidden').val() +
			'&gmaps_address=' + $('input[name=gmaps_address]:hidden').val(),
	  	cache: false,
	  	success: function(html){
	    	$('#address-result').html(html);
			$.fn.fancybox.close();
	  }
	});
}

function deleteAddress(id) {
	var ac = new AjaxConfigurator();
	$.ajax({
	  url: ac.url + 'eliminar-direccion/id/' + id,
	  cache: false,
	  success: function(html){
	    $('#address-result').html(html);
		$.fn.fancybox.close();
	  }
	});
}

function showDeleteAddress(id) {
	var ac = new AjaxConfigurator();
	$.ajax({
	  url: ac.url + 'mostrar-eliminar-direccion/id/' + id,
	  cache: false,
	  success: function(html){
	    $('#fancy_div').html(html);
	  }
	});
}

function showCreateAddress(from) {
	var ac = new AjaxConfigurator();
	$.ajax({
	  url: ac.url + 'mostrar-crear-direccion/from/' + from,
	  cache: false,
	  success: function(html){
	    $('#fancy_div').html(html);
	  }
	});
}

function showModifyAddress(id) {
	var ac = new AjaxConfigurator();
	$.ajax({
	  url: ac.url + 'mostrar-modificar-direccion/id/' + id,
	  cache: false,
	  success: function(html){
	    $('#fancy_div').html(html);
	  }
	});
}

/* Google maps */

function constructAddress() {

	address = $('input[name=address]:visible').val();
	address_number = $('input[name=address_number]:visible').val();
	city = $('input[name=city]:visible').val();
	postcode = $('input[name=postcode]:visible').val();
	id_region = $('select[name=id_loc_region]:visible').val();
	region = $('select[name=id_loc_region]:visible option[value="' + id_region + '"]').text();
	id_country = $('select[name=id_loc_country]:visible').val();
	country = $('select[name=id_loc_country]:visible option[value="' + id_country + '"]').text();
		
	searchAddress = '';

	if(address != '') {
		searchAddress += address + ', ';
	}

	if(address_number != '') {
		searchAddress += address_number + ', ';
	}

	if(postcode != '') {
		searchAddress += postcode + ' ';
	}

	if(city != '') {
		searchAddress += city + ', ';
	}

	if(region!= '') {
		searchAddress += region + ', ';
	}

	if(country != '') {
		searchAddress += country;
	}

	//alert(searchAddress);

	geocoder = new GClientGeocoder();

	if (geocoder) {

		geocoder.getLatLng(

			searchAddress,
			function(point) {
				if (!point) {
					//alert('getLatLng: not found');
					$('#gmaps_lat').val(0);
					$('#gmaps_lng').val(0);
				} else {
					$('#gmaps_lat').val(point.lat());
					$('#gmaps_lng').val(point.lng());
				}
			}
		);

		geocoder.getLocations(

			searchAddress,
			function(resp) {

				if(!resp) {
					//alert('getLocations: not found');
				$('#gmaps_address').val('');
				} else {
					pmObj = resp['Placemark'];
					for(elem in pmObj) {
						pmN = pmObj[elem];
						//alert(pmN['address']);
						$('#gmaps_address').val(pmN['address']);
					}
				}
			}
		);
	}
}

/* Funciones cuando creamos o modificamos los datos del usuario (donde se incluye la dirección) */

function constructUserAddress() {

	address = $('input#address').val();
	address_number = $('input#address_number').val();
	city = $('input#city').val();
	postcode = $('input#postcode').val();
	id_region = $('select#id_loc_region').val();
	region = $('select#id_loc_region option[value="' + id_region + '"]').text();
	id_country = $('select#id_loc_country').val();
	country = $('select#id_loc_country option[value="' + id_country + '"]').text();
		
	searchAddress = '';

	if(address != '') {
		searchAddress += address + ', ';
	}

	if(address_number != '') {
		searchAddress += address_number + ', ';
	}

	if(postcode != '') {
		searchAddress += postcode + ' ';
	}

	if(city != '') {
		searchAddress += city + ', ';
	}

	if(region!= '') {
		searchAddress += region + ', ';
	}

	if(country != '') {
		searchAddress += country;
	}

	//alert(searchAddress);

	geocoder = new GClientGeocoder();

	if (geocoder) {

		geocoder.getLatLng(

			searchAddress,
			function(point) {
				if (!point) {
					//alert('getLatLng: not found');
					$('#gmaps_lat').val(0);
					$('#gmaps_lng').val(0);
				} else {
					$('#gmaps_lat').val(point.lat());
					$('#gmaps_lng').val(point.lng());
				}
			}
		);

		geocoder.getLocations(

			searchAddress,
			function(resp) {

				if(!resp) {
					//alert('getLocations: not found');
				$('#gmaps_address').val('');
				} else {
					pmObj = resp['Placemark'];
					for(elem in pmObj) {
						pmN = pmObj[elem];
						//alert(pmN['address']);
						$('#gmaps_address').val(pmN['address']);
					}
				}
			}
		);
	}
}

function validateAddressForm(action, parameter) 
{

	var valid = true;

	valid = checkNotBlank('name', valid);
	valid = checkNotBlank('city', valid);
	valid = checkNotBlank('address', valid);
/*	valid = checkNotBlank('address_number', valid);
	valid = checkNotBlank('address_floor', valid);*/
	valid = checkNotBlank('postcode', valid);
	
	if(!valid) {
		$('div#error-msg').show();
	}  

	if(valid) {
		if(action == 'modify') {
			modifyAddress(parameter);
		} else {
			createAddress(parameter);
		}
	}
}

