function init() {
	//Liens externes
	if (document.getElementsByTagName) {
		var anchors = document.getElementsByTagName("a");
		for (var i=0; i<anchors.length; i++) {
			var anchor = anchors[i];
			if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
				anchor.target = '_blank';
			}
		}
	}
}
window.onload = init;

function recherche() {
	var codepostal = document.getElementById('codepostal').value.replace(/^\s+/, '').replace(/\s+$/, '');
	var nom = document.getElementById('nom').value.replace(/^\s+/, '').replace(/\s+$/, '');
	if (codepostal.length > 0) {
		return rechercheCodePostal();
	} else if (nom.length > 0) {
		return rechercheNom();
	} else {
		alert('Veuillez entrer un critère de recherche.');
		return false;
	}
}

function rechercheNom() {
	document.getElementById('nom').value = document.getElementById('nom').value.replace(/^\s+/, '').replace(/\s+$/, '');
	if (document.getElementById('nom').value.length > 0) {
		return true;
	} else {
		alert('Le nom entré n\'est pas valide');
		return false;
	}
}

function rechercheCodePostal() {
	document.getElementById('codepostal').value = document.getElementById('codepostal').value.replace(/\s+/g, '');
	if (document.getElementById('codepostal').value.match(/^[a-z]\d[a-z]\s?\d[a-z]\d$/i)) {
		//document.location.href = 'repertoire.php?codepostal='+document.getElementById('codepostal').value.replace(/\s/, '');
		return true;
	} else {
		alert('Le code postal entré semble invalide.');
		return false;
	}
}

function rechercheVille() {
	var page = 'repertoire.php';
	if (document.location.href.match(/repertoiremtl\.php/)) {
		page = 'repertoiremtl.php';
	}
	if (document.getElementById('ville').value.match(/^\d+$/)) {
		document.location.href = page+'?ville='+document.getElementById('ville').value;
	} else {
		document.location.href = page;
	}
}

function listeDocuments() {
	var url = 'extranet.php';
	if (document.location.href.match(/\/admin\//)) {
		url = 'documents.php';
	}
	if (document.getElementById('categorie').value.match(/^\d+$/)) {
		document.location.href = url+'?categorie='+document.getElementById('categorie').value;
	} else {
		document.location.href = url;
	}
}