function PreLoad_Img()
{	// Chargement des images
	var swapIm = new Array();
	var Im = new Array("nord.gif","nord_on.gif","ne.gif","ne_on.gif","est.gif","est_on.gif","se.gif",
	"se_on.gif","sud.gif","sud_on.gif","so.gif","so_on.gif","ouest.gif","ouest_on.gif","no.gif","no_on.gif",
	"fct/zoom_g.gif","fct/zoom_d.gif","fct/zoom_c.gif","fct/zoom_n.gif",
	"inf/appliquer.gif","inf/bloc_aller.gif","inf/bloc_aff.gif","inf/bloc_bas.gif", "inf/bloc_g.gif", "inf/bloc_d.gif");

	for(i in Im)
	{	swapIm[i] = new Image();
		swapIm[i].src = "img/"+Im[i];
	}
}

function compactsp(x) { // compacter les "espaces" contigus
	var r = '';
	var cprec=''
	for (var i=0; i<x.length; i++) {
		var c = x.charAt(i);
    	if (c != ' ') {r+=c; cprec=c;}
		else if (cprec!=' '){r+=c; cprec=c;}
    }
    return r;
}

function rmleadtrail(x) {// enlever les "espaces" EN DEBUT et en FIN de la chaine
	var i = 0;
	var iFirst = 0;
	var iLast = 0;
	var r='';
	while (i<x.length && x.charAt(i)==' ')	i++; // premier non espace
    iFirst = i;
	if (i==x.length) return r;  // il n'y a que des espaces!
	iLast = x.length-1;
    while (i<iLast && x.charAt(iLast)==' ')	iLast--; // dernier non espace
	for (i=iFirst; i<iLast+1; i++)	
		r += x.charAt(i);
    return r;
}

function RemplaceAccents(x)
{	iLast = x.length;
	var r="";
	for (i=0; i<iLast; i++)
	{	c = x.charAt(i);
		if (c=="à") { r += "a"; }
		else if (c=="ç") { r += "c"; }
		else if (c=="é") { r += "e"; }
		else if (c=="è") { r += "e"; }
		else if (c=="ê") { r += "e"; }
		else if (c=="ï") { r += "i"; }
		else if (c=="î") { r += "i"; }
		else if (c=="ô") { r += "o"; }
		else { r += x.charAt(i); }
	}
    return r;
}

// liste des caractères espaçant
//var liste_espace = " '-"; // espace, apostrophe, tiret; ajouter le 0xA0
var liste_espace = " "; // espace, apostrophe, tiret; ajouter le 0xA0

// liste des caractères à conserver: chiffres, lettres min/MAJ
// et tous les accentués:  C0-CF, D1-D6, D8-DD, E0-EF, F1-F6, F8-FD
var liste_ok = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
	+ String.fromCharCode(39,45,47,192,193,194,195,196,197,199,200,201,202,203,204,205,206,207,
		209,210,211,212,213,214,216,217,218,219,220,221,
		224,225,226,227,228,229,230,231,232,233,234,235,236,237,239,239,
		241,242,243,244,245,246,248,249,250,251,252,253); // ' - /

function Compactage(val){
	var r1="";
	for (i=0; i<val.length; i++){
		c = val.charAt(i);
		if (liste_espace.indexOf(c)!=-1) // caractère assimilé à un espace
			r1 += " ";
		else if (liste_ok.indexOf(c)!=-1) // caractère à conserver
			r1 += c;
		// else traitement des ligatures, si on décide de le faire
		else if (val.charCodeAt(i)==198) // ligature AE
			r1 += "AE";
		else if (val.charCodeAt(i)==230) // ligature ae
			r1 += "ae";
		else if (val.charCodeAt(i)==140) // ligature OE
			r1 += "OE";
		else if (val.charCodeAt(i)==156) // ligature oe
			r1 += "oe";
	}
	var r2 = compactsp(r1); // compacter les espaces contigus
	return rmleadtrail(r2); // puis suppression des espaces en tete et queue
}

function PrepareSaisie(x)
{	var x2 = Compactage(x);
	var x3 = RemplaceAccents(x2);
	return x3;
}

function Allera_Click()
{	var bln=0;

	if (document.Formulaire2.op.value == "show")
	{	return true;
	}

	var t=document.Formulaire2.dept;
	if (document.Formulaire.mode.value == "")
	{	// métropole saisie du libellé
		var dept=t.value;
		var mode="";
	}
	else
	{	// DOM : saisie dans la liste
		var dept=t.options[t.selectedIndex].value;
		var mode=dept;
	}

	t=document.Formulaire2.com;
	var com=t.value;

	if (dept != "<nom ou numéro>")
	{	bln+=1;
	}

	if (com != "<nom ou code postal>")
	{	bln+=2;
	}

	if (dept == "<nom ou numéro>")
	{	dept="";
	}

	if (com == "<nom ou code postal>")
	{	com="";
	}

	dept=PrepareSaisie(dept);
	com=PrepareSaisie(com);

	if (bln==0)
	{	alert("Veuillez saisir un departement ou une commune !");
	}
	else
	{	
		var f=getElem("id", "Formulaire");

		if ((mode!="") && (com==""))
		{	f.cmd.value="zoomfull";
			f.module.value="";
		}
		else if ((mode!="") && (mode!=f.mode.value))
		{	f.cmd.value="zoomfull";
			f.module.value="query";
		}
		else
		{	f.cmd.value="zoom";
			f.module.value="query";
		}
		f.op.value="locate";
		f.dept.value=dept;
		f.com.value=com;
		f.mode.value=mode;

		f.action="netenmap.php";
		f.target="";
		f.submit();
	}

	return false;
}

function dept_onfocus()
{	var t=document.Formulaire2.dept;
	if (t.value == "<nom ou numéro>")
	{	t.value="";
	}
}

function com_onfocus()
{	var t=document.Formulaire2.com;
	if (t.value == "<nom ou code postal>")
	{	t.value="";
	}
}

function Afficher_Click(niv)
{	
	var frm=getElem("id", "Formulaire_afficher");
	var sel="";
	for (i=0;i<lngNbLayers;i++)
	{	o=eval("frm.l"+i);
		if ((o != null) && (o.checked))
		{	if (sel != '')
			{	sel += ',';
			}
			sel+=i;
		}
	}

	var frm=frames['iframemap'].document.forms['Formulaire'];
	frm.cmd.value="zoom";
	frm.op.value="show";
	frm.sel.value=sel;
	frm.submit();

	return false;
}

function setNivZoom(toolindex)
{	// Changement d'outil
	var frm=frames['iframemap'].document.forms['Formulaire'];
	var bln = 0;
	if ((toolcur==null) || (toolcur != toolindex))
	{	// Changement de niveau de zoom
		frm.cmd.value="zoom";
		frm.niv.value=toolindex;
		bln=1;
	}

	if (bln == 1)
	{	frm.submit();
	}
}

function NivZoom_Over(toolindex)
{ var frm=frames['iframemap'].document.forms['Formulaire'];
	if (frm != null)
	{	toolcur=frm.niv.value;
		if ((toolcur==null) || (toolcur != toolindex))
		{	eval("t = getElem('id','niv"+toolindex+"');");
			if (t!=null)
			{	t.src = "img/fct/zoom_c.gif";
			}
		}
	}
}

function NivZoom_Out(toolindex)
{ var frm=frames['iframemap'].document.forms['Formulaire'];
	if (frm != null)
	{	toolcur=frm.niv.value;
		if ((toolcur==null) || (toolcur != toolindex))
		{	eval("t = getElem('id','niv"+toolindex+"');");
			if (t!=null)
			{	t.src = "img/fct/zoom_n.gif";
			}
		}
	}
}

function popup(url,name, w, h,p)
{	var param =  p+'toolbar=no,menubar=no,location=no,scrollbars=yes,width=' + w + ',height=' + h;
	var param2 ='top=10000,left=10000,scrollbars=no,menubar=no,width=' + w + ',height=' + h;

	// 1- Fermer la fenêtre ayant ce nom:
	// - Double avantage: en MSIE, la fenêtre annexe passe en avant-plan
	// - Même si le serveur externe est long a répondre, on n'a pas le contenu précédent,
	// mais une fenêtre vierge.
	// NB: il y a des problèmes sur MAC, et avec MSIE4 et antérieurs,
	// et également avec Opera: il sont donc exclus de la refermeture
	var nv = navigator.appName + navigator.appVersion;
	var MAC = (nv.indexOf( "Mac" ) != -1);
	var MSIE4=((nv.indexOf( "MSIE 4" ) != -1) || (nv.indexOf( "MSIE 3" ) != -1) || (nv.indexOf( "MSIE 2" ) != -1))
	var Opera = (nv.indexOf( "Opera" ) != -1);
	// alert( "MAC=" + MAC + ", MSIE4=" + MSIE4 + ", Opera=" + Opera )
	if ( !MAC && !MSIE4 && !Opera )
	{	// On est obligé d'ouvrir pour avoir un identifiant de fenêtre
		w = window.open("", name, param2);
		w.close(); // afin de pouvoir fermer
	}
	
	// 2- Ouverture de la fenêtre demandée
	w = window.open( url,name, param );
	// focus, si supporté (pas par MSIE4 et antérieurs; devrait être OK sur MAC)
	if ( !MSIE4 ) w.focus();
}

function ShowText(strText)
{	window.status=strText;
	return true;
}

function ShowText(strText)
{	window.status=strText;
}

function setCurrentTool(toolindex)
{	setNivZoom(toolindex);
}

// BROWSER SNIFFER (Sniff out the good and bad browsers)

function Is()
{	var agent = navigator.userAgent.toLowerCase();
    this.major = parseInt(navigator.appVersion);
    this.minor = parseFloat(navigator.appVersion);
    this.ns  = ((agent.indexOf('mozilla')!=-1) && ((agent.indexOf('spoofer')==-1) && (agent.indexOf('compatible') == -1)));
    this.ns2 = (this.ns && (this.major == 2));
    this.ns3 = (this.ns && (this.major == 3));
    this.ns4b = (this.ns && (this.minor < 4.04));
    this.ns4 = (this.ns && (this.major >= 4));
    this.ie   = (agent.indexOf("msie") != -1);
    this.ie3  = (this.ie && (this.major == 2));
    this.ie4  = (this.ie && (this.major >= 4));
    this.op3 = (agent.indexOf("opera") != -1);
    this.win   = (agent.indexOf("win")!=-1);
    this.mac   = (agent.indexOf("mac")!=-1);
    this.unix  = (agent.indexOf("x11")!=-1);
}

var is = new Is();

function DoAction(strAction)
{	
	var frm=frames['iframemap'].document.forms['Formulaire'];
	frm.cmd.value = strAction;
	frm.submit();
}

function showHideMapZoom(bln)
{	var elem=frames['iframemap'].getElem('id','mapzoom');
	if (bln!=1)
	{	frames['iframemap'].setModeInfo(0);
	}
	else
	{	frames['iframemap'].setModeInfo(1);
	}
	var frm=frames['iframemap'].document.forms['Formulaire'];
	frm.map_action.value = bln;
}

function majEmprise(mode,w,niv,xmin, ymin, xmax, ymax)
{	var f=getElem('id','Formulaire');

	// Changement d'outil
	var bln = 0;
	var toolcur=f.niv.value;
	eval("t=getElem('id','niv"+toolcur+"');");
	if (t!=null)
	{	t.src = "img/fct/zoom_n.gif";
	}
	
	for (i=0; i<8; i++)
	{	eval("t=getElem('id','niv"+i+"');");
		if (t!=null)
		{	if (t.src == "img/fct/zoom_c.gif")
			{	t.src = "img/fct/zoom_n.gif";
			}
		}
	}


	eval("t=getElem('id','niv"+niv+"');");
	if (t!=null)
	{	t.src = "img/fct/zoom_c.gif";
	}
	
	// maj de la distance
	eval("t=getElem('id','distance');");
	if (t!=null)
	{	t.src = "getimgdist.php?mode="+mode+"&width="+w+"&xmin="+xmin+"&ymin="+ymin+"&xmax="+xmax+"&ymax="+ymax;
	}

	f.niv.value=niv;
	f.XMIN.value=xmin;
	f.YMIN.value=ymin;
	f.XMAX.value=xmax;
	f.YMAX.value=ymax;
}

function retour()
{	var f=getElem("id", "Formulaire");
	f.cmd.value="zoom";
	f.op.value="locate";
	f.module.value="";
	f.dept.value="";
	f.com.value="";

	f.action="netenmap.php";
	f.target="";
	f.submit();

	return false;
}
