jQuery(function () {

	jQuery('#tx_crigerecherche_pi2-submit').click(function () {	
		sendForm();
	});
	
	jQuery('#tx_crigerecherche_pi2-recherche').keypress(function(e) {
		if (e.which == 13) {
			sendForm();
		}
	});
	
	function sendForm () {
		var mode = jQuery('input:checked[type=radio][name=tx_crigerecherche_pi2-mode]').val();
		if(mode == 'carte') {
			$mapping = {
				mot_cle                 : jQuery('#tx_crigerecherche_pi2-recherche').val(),
				type_carte              : '',
				producteur              : '',
				thematique              : '',
				acces                   : '',
				mode_telechargement     : '',
				reference_temporelle_du : '',
				reference_temporelle_au : '',
				id_localisation         : 2,
				valeur_localisation     : '',
				act                     : 'recherche'
			};
			
			jQuery.ajax({
				url  : Geocatalogue.urlBase + 'cartotheque/ajax.php',
				data : $mapping,
				cache: false,
				async: false,
				type : 'POST',
				success : function(str) {
					var reg = new RegExp("[']+", "g");
					str = str.replace(reg, '');
					jQuery('input[name=tx_crigeatolgeosignal_pi3-hresults]').val(str);
					jQuery('#txcrigerechercheaccueil').submit();
				},
				error : function(xhr, ajaxOptions, thrownError) {
					console.log(xhr);
				}
			});
		} else if (mode == 'donnee') {
			$mapping = {
			  mot_cle                 : jQuery('#tx_crigerecherche_pi2-recherche').val(),
			  type_donnee             : '',
			  producteur              : '',
			  thematique              : '',
			  acces                   : '',
			  mode_telechargement     : '',
			  reference_temporelle_du : '',
			  reference_temporelle_au : '',
			  id_localisation         : '2',
			  valeur_localisation     : '',
			  act                     : 'recherche'
			};
				jQuery.ajax({
			  url: Geocatalogue.urlBase + 'geocatalogue/ajax.php',
			  data : $mapping,
			  cache: false,
			  async: false,
			  type : 'POST',
			  success : function(str) {
				var reg = new RegExp("[']+", "g");
					str = str.replace(reg, '');
					jQuery('input[name=tx_crigeatolgeosignal_pi1-hresults]').val(str);
					jQuery('#txcrigerechercheaccueil').submit();
				},
				error : function(xhr, ajaxOptions, thrownError) {
					console.log(xhr);
				}
			});
		}
		return false;
	}
});

