
if (($.browser.msie) && ($.browser.version < 7) ){
	$(function() {
	
	$("body").append("<div id='dialogIe'><p>Vous naviguez sur une version d'Internet Explorer qui est maintenant obsolète. </p><br/>" +
			"<p>IE6 a été développé en 2001.<br/> La version 7 et maintenant la version 8 sont disponibles et vous permettrons de naviguer de façon optimisée sur ce site. </p><br/>" +
			"<p>Merci donc de bien vouloir mettre à jour votre navigateur.</p></div>");
	

		$("#dialogIe").dialog({
			bgiframe: true,
			height: 200,
			modal: true
		});
	});
};


// Fonctions Ajax Auto Completion Cities
/*
function retrieveCities() {
	var pays = $('#projet_territoire_pays_id :selected').val();
	var departement = $('#projet_territoire_departement_id :selected').val();
	var city = $('#projet_territoire_ville').val();

	if (city.length > 0) {
		$('#cityList').show();
		$('#suggestionsCityList').html(
				"<img src='/images/sf_admin/ajax-loader.gif'>");
		$.post('/projet/city', {
			cityString : "" + city + "",
			departementString : "" + departement + "",
			paysString : "" + pays + ""
		}, function(data) {
			if (data.length > 0) {
				$('#suggestionsCityList').html(data);
			}
		});
	} else {
		$('#cityList').hide();
	}
};
*/
/*
function changeCountry() {
	var pays = $('#projet_territoire_pays_id :selected').val();
	if (pays) {
		$("div[class$='form_field_territoire']").html(
				"<img src='/images/sf_admin/ajax-loader.gif'>");
		$.post('/projet/updatecityselect', {
			paysId : "" + pays + ""
		}, function(data) {
			if (data.length > 0) {
				$("div[class$='form_field_territoire']").html(data);
			}
		});
	}
}

function changeRegion() {
	if (arguments[0] && arguments[1]) {
		var pays = arguments[0];
		var region = arguments[1];
	} else {
		var region = $('#projet_territoire_region_id :selected').val();
		var pays = $('#projet_territoire_pays_id :selected').val();
	}
	if (arguments[2] && arguments[3]) {
		var dept = arguments[2];
		var city = arguments[3];
	}

	if (region) {
		$("div[class$='form_field_territoire']").html(
				"<img src='/images/sf_admin/ajax-loader.gif'>");
		$.post('/projet/updatecityselect', {
			regionId : "" + region + "",
			paysId : "" + pays + ""
		}, function(data) {
			if (data.length > 0) {
				$("div[class$='form_field_territoire']").html(data);
				if (dept && city) {
					$('#projet_territoire_departement_id').val(dept);
					$('#projet_territoire_ville').val(city);
				}
			}
		});
	}
}

function fillCity(cityValue, dept, region, pays) {
	changeRegion(pays, region, dept, cityValue);
	$('#cityList').hide();
}

function desactivateCityLising() {
	$('#projet_territoire_pays_id').unbind('change').change( function() {
	});
	$('#projet_territoire_region_id').unbind('change').change( function() {
	});
	$('#projet_territoire_departement_id').unbind('change').change( function() {
	});
	$('#projet_territoire_ville').unbind('keyup').keyup( function() {
	});
	$('#retryMapSearch').css('display', 'block');
}

function activateCityLising() {
	$('#projet_territoire_pays_id').change( function() {
		changeCountry();
	});
	$('#projet_territoire_region_id').change( function() {
		changeRegion();
	});
	$('#projet_territoire_departement_id').change( function() {
		retrieveCities();
	});
	$('#projet_territoire_ville').keyup( function() {
		retrieveCities();
	});
	$('#cityList').hide();
}
*/


function addPlaceIntoDb(jsonValues) {

	var placeGeo = jsonValues;
	$('#addPlaceIntoDb').show();
	$('#addPlaceIntoDbContent').html("<img src='/images/sf_admin/ajax-loader.gif'>");

	if (placeGeo.Accuracy < 3) {
		alert("Les informations renvoyées sont insuffisantes pour insérer la ville dans la base de données.")
	} else {

		if (placeGeo.Accuracy > 0) {
			var paysCode = placeGeo.Country.CountryNameCode;
		}

		if (placeGeo.Country.AdministrativeArea != null) {
			var region = placeGeo.Country.AdministrativeArea.AdministrativeAreaName;
		}

		if (placeGeo.Country.AdministrativeArea.SubAdministrativeArea != null) {
			if (placeGeo.Country.AdministrativeArea.SubAdministrativeArea.SubAdministrativeAreaName != null) {
				var departement = placeGeo.Country.AdministrativeArea.SubAdministrativeArea.SubAdministrativeAreaName;
			}

			if (placeGeo.Country.AdministrativeArea.SubAdministrativeArea.Locality != null) {
				var ville = placeGeo.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName;
				if (placeGeo.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode != null) {
					var zip = placeGeo.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber;
				}

			}
		} else {
			if (placeGeo.Country.AdministrativeArea.Locality != null) {
				var ville = placeGeo.Country.AdministrativeArea.Locality.LocalityName;
				if (placeGeo.Country.AdministrativeArea.Locality.PostalCode != null) {
					var zip = placeGeo.Country.AdministrativeArea.Locality.PostalCode.PostalCodeNumber;
				}

			}
		}

		$.post('/projet/addcity', {
			paysCode : "" + paysCode + "",
			region : "" + region + "",
			departement : "" + departement + "",
			ville : "" + ville + "",
			zip : "" + zip + ""
		}, function(data) {
			if (data.length > 0) {
				$('#addPlaceIntoDbContent').html(data);
			}
		});
	}
}
function createCity(jsonValues) {

	$('#cityList').show();
	$('#suggestionsCityList').html(
			"<img src='/images/sf_admin/ajax-loader.gif'>");

	$.post('/projet/addcity', {
		paysCode : jsonValues.pays,
		region : jsonValues.region,
		departement : jsonValues.departement,
		ville : jsonValues.ville,
		zip : jsonValues.zip,
		create : true
	}, function(data) {
		if (data.length > 0) {
			$('#suggestionsCityList').html(data);
		}
	});

}

selectCity = function (value, module, inputBaseName) {
	
	var allFields = $([]);
	
	if (value.indexOf("__add__") == -1) {
		
		$('.sf_admin_form_field_departement_id').html(
				"<img src='/images/sf_admin/ajax-loader.gif'>");
		$('.sf_admin_form_field_pays_id').html(
				"<img src='/images/sf_admin/ajax-loader.gif'>");
		if ($('.sf_admin_form_field_region_id')){
			$('.sf_admin_form_field_region_id').html("<img src='/images/sf_admin/ajax-loader.gif'>");
			
			$.post('/'+module+'/selectregionfromvilleid', {
				ville_id : value
			}, function(data) {
				if(data){
					if(data.length > 0) { $('.sf_admin_form_field_region_id').html(data);
					}
				}
			});				
		}

		
		$.post('/'+module+'/selectpaysfromvilleid', {
			ville_id : value
		}, function(data) {
			if(data){
				if(data.length > 0) { $('.sf_admin_form_field_pays_id').html(data);
				}
			}
		});	

		$.post('/'+module+'/selectdepartementfromvilleid', {
			ville_id : value
		}, function(data) {
			if (data.length > 0) {
				$('.sf_admin_form_field_departement_id').html(data);
			}
		});				
		
	} else {
		$('#autocomplete_' + inputBaseName + '_ville_id').val(value.substring(7));

		var cityString = value.substring(7);
		var paysString = $('#'+inputBaseName+'_pays_id').val();
		var departementString = $('#'+inputBaseName+'_departement_id').val();
		$('#dialog_create_ville').html("<img src='/images/sf_admin/ajax-loader.gif'>");
		$.ajax({
			url: '/'+module+'/citygooglemap',
			data : "cityString="+value+"&paysString="+paysString+"&departementString="+departementString,
			success: function(data) {
			if (data.length > 0) {
				$("#dialog_create_ville").html(data);

				Inputfield = $("#inputCityGoogle");
				allFields = $([]).add(Inputfield);
				
				}
			}
		});			
		
		$("#dialog_create_ville").dialog({
			title : "City localization",
			bgiframe: true,
			autoOpen: false,
			height: 500,
			width:500,
			modal: true,
			buttons: {
				'Localize a new city': function() {
					var bValid = true;
					allFields.removeClass('ui-state-error');
					bValid = bValid && ($("#inputCityGoogle").val()!=''?true:false);
					if (bValid) {
						LoadMapSearch();
					}
				},
				Cancel: function() {
					$(this).dialog('close');
				}
			},
			close: function() {
				allFields.val('').removeClass('ui-state-error');
			}
		});
		
		$('#dialog_create_ville').dialog('open');
		
		
	}

};



function selectCountry(value, module, nameSpace) {

	$('.sf_admin_form_field_departement_id').html("<img src='/images/sf_admin/ajax-loader.gif'>");
	
	if ($("select[id$='_region_id']") .length != 0){
		$('.sf_admin_form_field_region_id').html("<img src='/images/sf_admin/ajax-loader.gif'>");

		$.post('/'+module+'/selectregionfrompaysid', {
			pays_id : value
		}, function(data) {
			if (data.length > 0) {
				$('.sf_admin_form_field_region_id').html(data);
			}
		});					
		
		$.post('/'+module+'/selectfirstregiondepartementfrompaysid', {
			pays_id : value
		}, function(data) {
			if (data.length > 0) {
				$('.sf_admin_form_field_departement_id').html(data);
			}
		});					
		
		
	} else {
		$.post('/'+module+'/selectdepartementfrompaysid', {
			pays_id : value
		}, function(data) {
			if (data.length > 0) {
				$('.sf_admin_form_field_departement_id').html(data);
			}
		});					
		
	}
	$('#autocomplete_'+nameSpace+'_ville_id').setOptions({extraParams: {'pays': value} });
	
	
}


function selectRegion(value, module) {

	$('.sf_admin_form_field_departement_id').html("<img src='/images/sf_admin/ajax-loader.gif'>");

		$.post('/'+module+'/selectdepartementfromregionid', {
			region_id : value
		}, function(data) {
			if (data.length > 0) {
				$('.sf_admin_form_field_departement_id').html(data);
			}
		});					
		
//		$('#autocomplete_adresse_ville_id').setOptions({extraParams: {'pays': value} });
	
}

