
// -- general functions --

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_showHideLayers() { //v3.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
    obj.visibility=v; }
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function begin(){
  if (navigator.appName=='Netscape'){
	  MM_showHideLayers('LayerMenu','','show');
  }
}

// -- form functions --

function valider(){
  if (document.form_nouveau.var_nom.value.length == 0){
    alert("Merci de remplir votre nom avant de valider");
    document.form_nouveau.var_nom.focus();
    return;
  }
  if (document.form_nouveau.var_email.value.length == 0){
    alert("Merci de remplir votre e-mail avant de valider");
    document.form_nouveau.var_email.focus();
    return;
  }
  if (!(is_email(document.form_nouveau.var_email.value))){
    alert("Votre e-mail est incorrect");
    document.form_nouveau.var_email.select();
    return;
  }
  if (document.form_nouveau.var_commune.value.length == 0){
    alert("Merci de remplir le nom de votre commune avant de valider");
    document.form_nouveau.var_commune.focus();
    return;
  }
  if (document.form_nouveau.var_login.value.length == 0){
    alert("Merci de remplir votre login avant de valider");
    document.form_nouveau.var_login.focus();
    return;
  }
  if (document.form_nouveau.var_password.value.length == 0){
    alert("Merci de remplir votre mot de passe avant de valider");
    document.form_nouveau.var_password.focus();
    return;
  }
  if (document.form_nouveau.var_password_controle.value.length == 0){
    alert("Merci de saisir votre mot de passe une deuxième fois avant de valider");
    document.form_nouveau.var_password_controle.focus();
    return;
  }
  if (document.form_nouveau.var_password.value != document.form_nouveau.var_password_controle.value){
  	alert("Votre mot de passe n'est pas correct");
  	document.form_nouveau.var_password.value = '';
  	document.form_nouveau.var_password_controle.value = '';
  	document.form_nouveau.var_password.focus();
    	return;
  }
  return document.form_nouveau.submit();
}

function is_email(email){
     var reg = /^[A-Za-z0-9\!\?\~\*\+\-\=\_\~]+(\.[A-Za-z0-9\!\?\~\*\+\-\=\_\~]+)*@[A-Za-z0-9\!\?\~\*\+\-\=\_\~]+((\.[A-Za-z0-9\!\?\~\*\+\-\=\_\~]+)*)$/;
     return (reg.exec(email)!=null);
}

function valider_nouveau(){
	if (document.form_login.var_login.value.length == 0){
    		alert("Merci de remplir votre login avant de valider");
    		document.form_login.var_login.focus();
    		return;
  	}
	if (document.form_login.var_email.value.length == 0){
    		alert("Merci de remplir votre e-mail avant de valider");
    		document.form_login.var_email.focus();
    		return;
  	}
  	if (!(is_email(document.form_login.var_email.value))){
    		alert("Votre e-mail est incorrect");
    		document.form_login.var_email.select();
    		return;
  	}
	return document.form_login.submit();
}

function valider_login(){
	if (document.form_login.var_login.value.length == 0){
    		alert("Merci de remplir votre login avant de valider");
    		document.form_login.var_login.focus();
    		return;
  	}
	return document.form_login.submit();
}

function valider_message(){
	if (document.form_contact.contact_nom.value.length == 0){
    		alert("Merci de remplir votre nom avant de valider");
    		document.form_contact.contact_nom.focus();
    		return;
  	}
  	if (document.form_contact.contact_telephone.value != ''){
		var reg = /^[0-9 -]{10,}$/;
		if (reg.exec(document.form_contact.contact_telephone.value)==null){
			alert("Votre numéro de téléphone est incorrect");
			document.form_contact.contact_telephone.select();
    			return;
		}
	}
  	if (document.form_contact.contact_message.value.length == 0){
    		alert("Veuillez remplir le champ 'message' avant de valider");
    		document.form_contact.contact_message.focus();
    		return;
  	}
  	return document.form_contact.submit();
}

function selection_multiple(){
	document.form_carto.r_commune_selection.value = '';
	for(i=0; i<document.form_carto.r_commune.length; i++){
		if (document.form_carto.r_commune.options[i].selected){
			document.form_carto.r_commune_selection.value += document.form_carto.r_commune.options[i].value +'#';
		}
	}
	return document.form_carto.submit();
}

function impression_carto(id){
	var config = 'impression=oui';
	if (document.form_carto_res.res_carte.checked){
		config += '&carte=oui';
	}
	if (document.form_carto_res.res_donnees.checked){
		config += '&donnees=oui';
	}
	if (document.form_carto_res.res_imp.value != ''){
		var reg = /^[0-9]+$/;
		if (reg.exec(document.form_carto_res.res_imp.value)!=null){
			config += '&nombre=' + document.form_carto_res.res_imp.value;
		}
	}
	config += '&id='+id;
	window.open("impression.php?"+config, "imprimer", option03);
	document.form_carto_res.carto_stats_imp.value = 'oui';
	return document.form_carto_res.submit();
}

function impression_liens(donnees){
	var config = donnees;
	if (config == "page=organismes"){
		if (document.form_organismes.org_imp_nombre.value != ''){
			var reg = /^[0-9]+$/;
			if (reg.exec(document.form_organismes.org_imp_nombre.value)!=null){
				config += '&nombre=' + document.form_organismes.org_imp_nombre.value;
			}
		}
		if (document.form_organismes.org_imp_nom.options.selectedIndex > 0){
			indexS = document.form_organismes.org_imp_nom.options.selectedIndex;
			config += '&nom=' + document.form_organismes.org_imp_nom.options[indexS].value;
		}
		if (document.form_organismes.org_ordre[0].checked) config += '&ordre=nom_organisme';
		if (document.form_organismes.org_ordre[1].checked) config += '&ordre=commune';
		if (document.form_organismes.org_ordre[2].checked) config += '&ordre=id_groupe';
		config += '&organisme_query=' + document.form_organismes.organisme_query.value;
		document.form_organismes.org_stats_imp.value = 'oui';
		
		window.open("impression_donnees.php?"+config, "imprimer", option03);
		document.form_organismes.organisme_recherche.value='oui';
		return document.form_organismes.submit();
	}
	else if (config == "page=ressources"){
		config += document.form_ressources.donnee_impressions.value;
		document.form_ressources.res_stats_imp.value = 'oui';
		window.open("impression_donnees.php?"+config, "imprimer", option03);
		document.form_ressources.ressource_recherche.value='oui';
		return document.form_ressources.submit();
	}
	else {
		document.form_liens.liens_stats_imp.value = 'oui';
		window.open("impression_donnees.php?"+config, "imprimer", option03);
		document.form_liens.cartographie_liens.value='oui';
		return document.form_liens.submit();
	}
}

function affichage_donnees(donnees){
	var config = donnees;
	window.open("impression_donnees.php?"+config, "savoir", option04);
}

function affichage_donnees_carto(donnees){
	var config = donnees;
	window.open("affichage_frame.php?"+config, "savoir", option04);
}

// open functions

var option01 ="height=470, width=310, left=0, top=0, menubar=0, scrollbars=0, status=0, titlebar=0, toolbar=0, resizable=no, location=no, directories=no, ontop=true";
var option02 ="height=550, width=550, left=0, top=0, menubar=0, scrollbars=0, status=0, titlebar=0, toolbar=0, resizable=no, location=no, directories=no, ontop=true";
var option03 ="height=550, width=665, left=0, top=0, menubar=1, scrollbars=1, status=1, titlebar=1, toolbar=1, resizable=yes";
var option04 ="height=550, width=665, left=0, top=0, menubar=0, scrollbars=1, status=0, titlebar=0, toolbar=0, resizable=no, location=no, directories=no, ontop=true";
var option05 ="height=130, width=645, left=0, top=0, menubar=0, scrollbars=0, status=0, titlebar=0, toolbar=0, resizable=no, location=no, directories=no, ontop=true";


function ouvrir(page,taille){
	window.open(page, 'savoir',taille,true);
}

function ouvrirAide(page,taille){
	window.open(page, 'aide',taille,true);
}

function ouvrirInfo(page,taille){
	window.open(page, 'information',taille,true);
}

function ouvrirImp(page,taille){
	window.open(page, 'impression',taille,true);
}

function ouvrirLegende(page,taille){
	window.open(page, 'legende',taille,true);
}

// Mise à jour

function miseJour(){
	Today = new Date();
	journee = Today.getUTCDay();
	jour = Today.getUTCDate();
	if (jour < 15){
		jour = "01";
	}else {
		jour = "15";
	}
	mois = Today.getMonth();
	annee = Today.getUTCFullYear();
	switch (mois){
		case 0:
			mois = "janvier";
			break;
		case 1:
			mois = "février";
			break;
		case 2:
			mois = "mars";
			break;
		case 3:
			mois = "avril";
			break;
		case 4:
			mois = "mai";
			break;
		case 5:
			mois = "juin";
			break;
		case 6:
			mois = "juillet";
			break;
		case 7:
			mois = "août";
			break;
		case 8:
			mois = "septembre";
			break;
		case 9:
			mois = "octobre";
			break;
		case 10:
			mois = "novembre";
			break;
		case 11:
			mois = "décembre";
			break;
		default :
			mois = "janvier";
	}
	document.write("&nbsp;"+jour+"&nbsp;"+mois+"&nbsp;"+annee+"<br>");
}