
function rechRapide_validate() {
	// Vérifier remplissage des champs
		var message = '';
		
		if ( document.getElementById('rechRap_typeannonce').options.selectedIndex == 0 ) {
			message += erreur_categorie_saisie + '\n';
		}
		if ( document.getElementById('rechRap_typebiens').options.selectedIndex == 0 ) {
			message += erreur_bien_saisie + '\n';
		}
		if (
			document.getElementById('rechRap_localisation').value.length == 0
			|| document.getElementById('rechRap_localisation').value == form_texte_localisation
		) {
			message += erreur_localisation_saisie + '\n';
		}
		if ( message.length > 0 ) {
			openInfoPopup(message,"","",1);
			return false;
		}
	
	// Vérifier informations carto
		// Plus de saisie qu'autorisées
			var obj = trim(document.getElementById('rechRap_localisation').value);
			var tab	= obj.split(',');
			var CPs			= "";
			var Deps		= "";
			var villes		= "";
			var nbVilles	= 0;
			var nbCPs		= 0;
			var nbDeps		= 0;
			for ( var i = 0; i < tab.length; i++ ) {
				var valeur = trim(tab[i]);
				var longeur = valeur.length;
				if ( (longeur == 5) && !isNaN(valeur) ) {
					nbCPs++;
					if ( CPs.length > 0 ) CPs += ",";
					CPs += valeur;
				} else if ( (longeur == 2) && !isNaN(valeur) ) {
					nbDeps++;
					if ( Deps.length > 0 ) Deps += ",";
					Deps += valeur;
				} else {
					nbVilles++;
					if ( villes.length > 0 ) villes += ",";
					villes += valeur;
				}
			}
			
			if (
				(nbCPs > limite_CPs)
				|| (nbDeps > limite_deps)
				|| (nbVilles > limite_villes)
			) {
				var message = '';
				if (
					(nbCPs > limite_CPs)
					|| (nbDeps > limite_deps)
				) {
					message += erreur_carto_limite_nbCPsEtDeps + '\n';
				}
				if ( nbVilles > limite_villes) {
					message += erreur_carto_limite_nbVilles + '\n';
				}
				openInfoPopup(message,"","",1);
				return false;
			}
		
	// Demander les infos géographiques
		get_localisations( ( ((Deps.length > 0) && (CPs.length > 0)) ? Deps + ',' + CPs : Deps + CPs),villes);
	
	// Vérifier les infos géographique
		if ( !valider_autorisationByPopup && document.getElementById('rechRap_localisation').value.length == 0 ) {
			message += erreur_localisation_saisie + '\n';
		}
		if ( message.length > 0 ) {
			openInfoPopup(message,"","",1);
			return false;
		}
	
	// Valider le submit du formulaire
		if ( !valider_autorisationByPopup ) {
			document.form_recherche_haut.submit();
			return true;
		} else return false;
}

function get_localisations(CPs, villes) {
	if ( CPs.length == 0 && villes.length == 0 ) return;
	
	var url_ajax = '/carto_AJAX.php?date='+ new Date().getTime() +'&cmd=get_localisations&CPs='+ CPs + '&villes='+ villes;
	var res_xml_share = AjaxXMLRequest(url_ajax);
	treatXML_rechUltraRap(res_xml_share);
}

function addLocalisations(localisations) {
	if ( localisations.length > 0 ) {
		if ( document.getElementById('rechRap_localisation').value == form_texte_localisation ) {
			document.getElementById('rechRap_localisation').value = '';
		}
		if ( document.getElementById('rechRap_localisation').value.length > 0 ) {
			document.getElementById('rechRap_localisation').value += ',';
		}
		document.getElementById('rechRap_localisation').value += localisations.replace(';', ',');
	}		
	
	if ( valider_autorisationByPopup ) {
		valider_autorisationByPopup = false;
		if (
			(document.getElementById('rechRap_localisation').value.length > 0)
			&&
			(document.getElementById('rechRap_localisation').value != form_texte_localisation)
		) {
			document.form_recherche_haut.submit();
		} else {
			document.getElementById('rechRap_localisation').value = form_texte_localisation;
			setTimeout('rechRapide_validate();', 50);
		}
	}
}

function fct_openPopup_choisirLocalisation() {
	popup_choisirLocalisation = Window('choisirLocalisation', '/carto_popup_choisirLocalisation.php?window_name=choisirLocalisation&window_height=550', '746', '550', true);
	openModal('choisirLocalisation');
}

function popup_treatXML_rechUltraRap() {
	var res_xml_share = XML_localisation;
	
	// PROPOSITIONS
	propositions	= res_xml_share.getElementsByTagName("propositions");
	rejetes			= res_xml_share.getElementsByTagName("rejetes");
	
	var tab_propositions	= new Array();
	var tab_rejetes_villes	= new Array();
	var tab_rejetes_cps		= new Array();
	var tab_rejetes_deps	= new Array();
	if ( propositions[0] ) {
		tab_propositions	= propositions[0].getElementsByTagName("proposition");
	}
	if ( rejetes[0] ) {
		tab_rejetes_villes	= rejetes[0].getElementsByTagName("ville");
		tab_rejetes_cps		= rejetes[0].getElementsByTagName("cp");
		tab_rejetes_deps	= rejetes[0].getElementsByTagName("dep");
	}
	
	if ( tab_propositions.length || tab_rejetes_villes.length || tab_rejetes_cps.length || tab_rejetes_deps.length ) {
		var txt_formulaire = '<div align="left" style="height: 431px; padding: 0; overflow-y: auto;"><div style="padding-top: 20px;">';
		
		// VILLES REJETEES
		if ( tab_rejetes_villes.length > 0 ) {
			// BLOC DE VILLES REJETEES
				// Section
				txt_formulaire += '<table cellpadding="0" callspacing="0" border="0" style="margin: 0 0 15px 16px;"><tr><td><img src="http://www.les-maisons-de-france.com/images/popups/picto-alerte.gif" alt="" /></td><td class="carto_titre3" style="padding-left: 5px;">' + erreur_carto_donnees_invalide + '</td></tr></table>\n';
				
				// Contenu : la liste des bureaux de poste retenus
				var villes	= tab_rejetes_villes;
				var nb_colonnes		= 3;
				var nb_affiches		= 0;
				var nb_a_afficher	= villes.length;
				var nb_par_colonnes	= parseInt(nb_a_afficher / nb_colonnes);
				
				if ( nb_par_colonnes * nb_colonnes < nb_a_afficher ) nb_par_colonnes++;
				
				txt_formulaire += '<table width="100%" cellspacing="0" cellspacing="0" border="0" class="carto_titre4">\n';
				txt_formulaire += '	<colgroup>\n';
				txt_formulaire += '		<col width="' + (100 / nb_colonnes) + '%">\n';
				txt_formulaire += '	</colgroup>\n';
				txt_formulaire += '	<tr>\n';
				txt_formulaire += '		<td valign="top">\n';
				txt_formulaire += '			<table cellspacing="0" cellspacing="0" border="0" class="form_text">\n';
				for ( var j = 0; j < nb_a_afficher; j++ ) {
					var ville_nom		= villes[j].getAttribute('nom');
					
					txt_formulaire += '				<tr>\n';
					//      LAISSÉ AU CAS OU PUCE
					txt_formulaire += '					<td>\n';
					txt_formulaire += '					</td>\n';
					//    / LAISSÉ AU CAS OU PUCE
					txt_formulaire += '					<td class="carto_titre4" style="padding: 8px 8px 8px 31px; width: 210px;">\n';
					txt_formulaire += '						' + ville_nom + '<br>\n';
					txt_formulaire += '					</td>\n';
					txt_formulaire += '				</tr>\n';
					
					nb_affiches++;
					
					if ( nb_affiches == nb_a_afficher ) {
						txt_formulaire += '			</table ' + nb_affiches + ' == ' + nb_a_afficher + '>\n';
						txt_formulaire += '		</td>\n';
					} else if ( nb_affiches != 0 && nb_affiches % nb_par_colonnes == 0 ) {
						txt_formulaire += '			</table>\n';
						txt_formulaire += '		</td>\n';
						if ( nb_affiches != nb_a_afficher ) {
							txt_formulaire += '		<td valign="top" style="width: 210px;">\n';
							txt_formulaire += '			<table cellspacing="0" cellspacing="0" border="0" class="form_text">\n';
						}
					}
				}
				
				txt_formulaire += '	</tr>\n';
				txt_formulaire += '</table>\n';
				
				if ( tab_rejetes_cps.length > 0 || tab_rejetes_deps.length > 0 || tab_propositions.length > 0 ) txt_formulaire += '<div style="height: 20px; overflow-y: auto;"></div>';
		}
		
		
		// CPS REJETES
		if ( tab_rejetes_cps.length > 0 ) {
			// BLOC DE CPS REJETES
				// Section
				txt_formulaire += '<table style="margin: 0 0 15px 16px;"><tr><td><img src="http://www.les-maisons-de-france.com/images/popups/picto-alerte.gif" alt="" /></td><td class="carto_titre3" style="padding-left: 5px;">' + erreur_carto_donnees_invalide + '</td></tr></table>\n';
				
				// Contenu : la liste des bureaux de poste retenus
				var cps	= tab_rejetes_cps;
				var nb_colonnes		= 3;
				var nb_affiches		= 0;
				var nb_a_afficher	= cps.length;
				var nb_par_colonnes	= parseInt(nb_a_afficher / nb_colonnes);
				
				if ( nb_par_colonnes * nb_colonnes < nb_a_afficher ) nb_par_colonnes++;
				
				txt_formulaire += '<table width="100%" cellspacing="0" cellspacing="0" border="0" class="carto_titre4">\n';
				txt_formulaire += '	<colgroup>\n';
				txt_formulaire += '		<col width="' + (100 / nb_colonnes) + '%">\n';
				txt_formulaire += '	</colgroup>\n';
				txt_formulaire += '	<tr>\n';
				txt_formulaire += '		<td valign="top">\n';
				txt_formulaire += '			<table cellspacing="0" cellspacing="0" border="0" class="form_text">\n';
				for ( var j = 0; j < nb_a_afficher; j++ ) {
					var ville_nom		= cps[j].getAttribute('codepostal');
					
					txt_formulaire += '				<tr>\n';
					//      LAISSÉ AU CAS OU PUCE
					txt_formulaire += '					<td>\n';
					txt_formulaire += '					</td>\n';
					//    / LAISSÉ AU CAS OU PUCE
					txt_formulaire += '					<td class="carto_titre4" style="padding: 8px 8px 8px 31px; width: 210px;">\n';
					txt_formulaire += '						' + ville_nom + '<br>\n';
					txt_formulaire += '					</td>\n';
					txt_formulaire += '				</tr>\n';
					
					nb_affiches++;
					
					if ( nb_affiches == nb_a_afficher ) {
						txt_formulaire += '			</table>\n';
						txt_formulaire += '		</td>\n';
					} else if ( nb_affiches != 0 && nb_affiches % nb_par_colonnes == 0 ) {
						txt_formulaire += '			</table>\n';
						txt_formulaire += '		</td>\n';
						if ( nb_affiches != nb_a_afficher ) {
							txt_formulaire += '		<td valign="top" style="width: 210px;">\n';
							txt_formulaire += '			<table cellspacing="0" cellspacing="0" border="0" class="form_text">\n';
						}
					}
				}
				
				txt_formulaire += '	</tr>\n';
				txt_formulaire += '</table>\n';
				
				if ( tab_rejetes_deps.length > 0 || tab_propositions.length > 0 ) txt_formulaire += '<div style="height: 20px; overflow-y: auto;"></div>';
		}
		
		
		// DEPS REJETES
		if ( tab_rejetes_deps.length > 0 ) {
			// BLOC DE DEPS REJETES
				// Section
				txt_formulaire += '<table style="margin: 0 0 15px 16px;"><tr><td><img src="http://www.les-maisons-de-france.com/images/popups/picto-alerte.gif" alt="" /></td><td class="carto_titre3" style="padding-left: 5px;">' + erreur_carto_donnees_invalide + '</td></tr></table>\n';
				
				// Contenu : la liste des bureaux de poste retenus
				var deps	= tab_rejetes_deps;
				var nb_colonnes		= 3;
				var nb_affiches		= 0;
				var nb_a_afficher	= deps.length;
				var nb_par_colonnes	= parseInt(nb_a_afficher / nb_colonnes);
				
				if ( nb_par_colonnes * nb_colonnes < nb_a_afficher ) nb_par_colonnes++;
				
				txt_formulaire += '<table width="100%" cellspacing="0" cellspacing="0" border="0" class="carto_titre4">\n';
				txt_formulaire += '	<colgroup>\n';
				txt_formulaire += '		<col width="' + (100 / nb_colonnes) + '%">\n';
				txt_formulaire += '	</colgroup>\n';
				txt_formulaire += '	<tr>\n';
				txt_formulaire += '		<td valign="top">\n';
				txt_formulaire += '			<table cellspacing="0" cellspacing="0" border="0" class="form_text">\n';
				for ( var j = 0; j < nb_a_afficher; j++ ) {
					var ville_nom		= deps[j].getAttribute('code');
					
					txt_formulaire += '				<tr>\n';
					//      LAISSÉ AU CAS OU PUCE
					txt_formulaire += '					<td>\n';
					txt_formulaire += '					</td>\n';
					//    / LAISSÉ AU CAS OU PUCE
					txt_formulaire += '					<td class="carto_titre4" style="padding: 8px 8px 8px 31px; width: 210px;">\n';
					txt_formulaire += '						' + ville_nom + '<br>\n';
					txt_formulaire += '					</td>\n';
					txt_formulaire += '				</tr>\n';
					
					nb_affiches++;
					
					if ( nb_affiches == nb_a_afficher ) {
						txt_formulaire += '			</table ' + nb_affiches + ' == ' + nb_a_afficher + '>\n';
						txt_formulaire += '		</td>\n';
					} else if ( nb_affiches != 0 && nb_affiches % nb_par_colonnes == 0 ) {
						txt_formulaire += '			</table>\n';
						txt_formulaire += '		</td>\n';
						if ( nb_affiches != nb_a_afficher ) {
							txt_formulaire += '		<td valign="top" style="width: 210px;">\n';
							txt_formulaire += '			<table cellspacing="0" cellspacing="0" border="0" class="form_text">\n';
						}
					}
				}
				
				txt_formulaire += '	</tr>\n';
				txt_formulaire += '</table>\n';
				
				if ( tab_propositions.length > 0 ) txt_formulaire += '<div style="height: 20px; overflow-y: auto;"></div>';
		}
		
		// PROPOSITIONS
		for (var i = 0; i < tab_propositions.length; i++ ) {
			// BLOC DE PROPOSITION
			
				// Contenu : la liste des villes retenues
				var villes	= tab_propositions[i].getElementsByTagName("ville");
				var nb_colonnes		= 3;
				var nb_affiches		= 0;
				var nb_a_afficher	= villes.length;
				var nb_par_colonnes	= parseInt(nb_a_afficher / nb_colonnes);
				
				if ( i != tab_propositions.length - 1 ) {
					txt_formulaire += '<table width="100%" border="0" cellspacing="0" cellpadding="0" class="form_section">\n';
				} else {
					txt_formulaire += '<table width="100%" border="0" cellspacing="0" cellpadding="0" class="form_section" style="margin: 0 0 15px 0;">\n';
				}
				txt_formulaire += '	<tr>\n';
				txt_formulaire += '		<td class="form_section">\n';
				txt_tmp = erreur_carto_donnees_plusieursPossibilitePourVille;
				txt_tmp = ( txt_tmp.split('{1}') ).join('<b>' + tab_propositions[i].getAttribute('nom') + '</b>');
				txt_formulaire += '			' + txt_tmp + ' :<br>\n';
				txt_formulaire += '		</td>\n';
				txt_formulaire += '	</tr>\n';
				txt_formulaire += '	<tr>\n';
				txt_formulaire += '		<td class="form_contenu">\n';
				
				if ( nb_par_colonnes * nb_colonnes < nb_a_afficher ) nb_par_colonnes++;
				txt_formulaire += '<table width="100%" border="0" cellspacing="0" cellpadding="0" class="form_text">\n';
				txt_formulaire += '	<tr>\n';
				txt_formulaire += '		<td valign="top" style="padding: 8px;">\n';
				txt_formulaire += '			<table style="border-collapse: collapse;" class="form_text">\n';
				for ( var j = 0; j < nb_a_afficher; j++ ) {
					var ville_nom		= villes[j].getAttribute('nom');
					
					txt_formulaire += '				<tr>\n';
					txt_formulaire += '					<td>\n';
					txt_formulaire += '						<input type="checkbox" id="ville_' + ville_nom + '" name="villes[]" value="' + ville_nom + '" class="inputCheckbox" style="cursor: pointer;">\n';
					txt_formulaire += '					</td>\n';
					txt_formulaire += '					<td style="width: 210px; padding-left: 8px;">\n';
					txt_formulaire += '						<label for="ville_' + ville_nom + '">' + ville_nom + '<br></label>\n';
					txt_formulaire += '					</td>\n';
					txt_formulaire += '				</tr>\n';
					
					nb_affiches++;
					
					if ( nb_affiches == nb_a_afficher ) {
						txt_formulaire += '			</table>\n';
						txt_formulaire += '		</td>\n';
					} else if ( nb_affiches != 0 && nb_affiches % nb_par_colonnes == 0 ) {
						txt_formulaire += '			</table>\n';
						txt_formulaire += '		</td>\n';
						if ( nb_affiches != nb_a_afficher ) {
							txt_formulaire += '		<td valign="top" style="padding: 8px;">\n';
							txt_formulaire += '			<table cellspacing="0" cellspacing="0" border="0" class="form_text">\n';
						}
					}
				}
				txt_formulaire += '	</tr>\n';
				txt_formulaire += '</table>\n';
				
				txt_formulaire += '		</td>\n';
				txt_formulaire += '	</tr>\n';
				txt_formulaire += '</table>\n';
				
				if ( i + 1 != tab_propositions.length ) txt_formulaire += '<div style="height: 15px; overflow-y: auto;"></div>';
		}
		
		txt_formulaire += '</div></div>';
		
		var frame;
		for ( i = 0; i < window.frames.length; i++ ) {
			if ( window.frames[i].name == 'choisirLocalisation' ) {
				frame = window.frames[i];
				i = window.frames.length;
			}
		}
		
		frame.document.getElementById('div_formulaire').innerHTML = txt_formulaire;
	}
}

var XML_localisation = '';
var valider_autorisationByPopup = false;

function treatXML_rechUltraRap(res_xml_share) {
	if (!res_xml_share) {
		openInfoPopup(err_chargement,"","",1);
		return;
	}
	res_xml_share = res_xml_share.getElementsByTagName("root")[0];
	
	// On quitte si erreurs eventuelles
	if (checkAjaxError(res_xml_share)) {
		XML_localisation = '';
		return;
	}
// alert('mark');
// return;
	
	XML_localisation = res_xml_share;
	
	document.getElementById('rechRap_localisation').value = '';
	
	// DEPS
		var deps = res_xml_share.getElementsByTagName("deps");
		if ( deps[0] ) {
			var deps = deps[0].getElementsByTagName("dep");
			
			var tab_deps = new Array();
			for (var i = 0; i < deps.length; i++ ) {
				var dep_code		= deps[i].getAttribute('code');
				
				tab_deps.push(dep_code);
			}
			
			if ( tab_deps.join(',').length > 0 ) {
				if ( document.getElementById('rechRap_localisation').value.length > 0 ) {
					document.getElementById('rechRap_localisation').value += ',';
				}
				document.getElementById('rechRap_localisation').value += tab_deps.join(',');
			}
		}
	// DEPS //
	
	// CPs
		var CPs = res_xml_share.getElementsByTagName("cps");
		if ( CPs[0] ) {
			var CPs = CPs[0].getElementsByTagName("cp");
			
			var tab_CPs = new Array();
			for (var i = 0; i < CPs.length; i++ ) {
				var CP_code		= CPs[i].getAttribute('codepostal');
				
				tab_CPs.push(CP_code);
			}
			
			
			if ( tab_CPs.join(',').length > 0 ) {
				if ( document.getElementById('rechRap_localisation').value.length > 0 ) {
					document.getElementById('rechRap_localisation').value += ',';
				}
				document.getElementById('rechRap_localisation').value += tab_CPs.join(',');
			}
		}
	// CPs //
	
	// VILLES
		var villes = res_xml_share.getElementsByTagName("villes");
		if ( villes[0] ) {
			var villes = villes[0].getElementsByTagName("ville");
			
			var tab_villes = new Array();
			for (var i = 0; i < villes.length; i++ ) {
				var ville_nom		= villes[i].getAttribute('nom');
				
				tab_villes.push(ville_nom);
			}
			
			if ( tab_villes.join(',').length > 0 ) {
				if ( document.getElementById('rechRap_localisation').value.length > 0 ) {
					document.getElementById('rechRap_localisation').value += ',';
				}
				document.getElementById('rechRap_localisation').value += tab_villes.join(',');
			}
		}
	// VILLES //
	
	// PROPOSITIONS, VILLES et CPS REJETEES
		propositions	= res_xml_share.getElementsByTagName("propositions");
		rejetes			= res_xml_share.getElementsByTagName("rejetes");
		var tab_propositions	= new Array();
		var tab_rejetes_villes	= new Array();
		var tab_rejetes_cps		= new Array();
		var tab_rejetes_deps	= new Array();
		if ( propositions[0] ) {
			tab_propositions	= propositions[0].getElementsByTagName("proposition");
		}
		if ( rejetes[0] ) {
			tab_rejetes_villes	= rejetes[0].getElementsByTagName("ville");
			tab_rejetes_cps		= rejetes[0].getElementsByTagName("cp");
			tab_rejetes_deps	= rejetes[0].getElementsByTagName("dep");
		}
		if ( tab_propositions.length || tab_rejetes_villes.length || tab_rejetes_cps.length || tab_rejetes_deps.length ) {
			valider_autorisationByPopup = true;
			fct_openPopup_choisirLocalisation();
			
			return;
		}
	// PROPOSITIONS, VILLES et CPS REJETEES //
	
	valider_autorisationByPopup = false;
}

