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 PreLoad_Img()
{
}

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 setNivZoom(toolindex)
{	// Changement d'outil
	var bln = 0;

	var f=xGetElementById('Formulaire');
	var toolcur=f.niv.value;

	// Changement de niveau de zoom
	var frm=frames['iframemap'].document.forms['formMap'];
	if ((toolcur==null) || (toolcur != toolindex))
	{	// Changement de niveau de zoom
		if (frm != null)
		{	frm.cmd.value="zoom";
			frm.op.value="";
			frm.niv.value=toolindex;
			bln=1;
		}

		if (bln == 1)
		{	toolcur=toolindex;
			frm.submit();
		}
	}
}

function NivZoom_Over(toolindex)
{	var f=xGetElementById('Formulaire');
	var toolcur=f.niv.value;
	if ((toolcur==null) || (toolcur != toolindex))
	{	eval("t=xGetElementById('niv"+toolindex+"');");
		if (t!=null)
		{	t.src = "images/fct/zoom_c.gif";
			toolcur=toolindex;
		}
	}
}

function NivZoom_Out(toolindex)
{	if ((toolcur==null) || (toolcur != toolindex))
	{	eval("t=xGetElementById('niv"+toolindex+"');");
		if (t!=null)
		{	t.src = "images/fct/zoom_n.gif";
		}
	}
}

function ShowText(strText)
{	window.status=strText;
}

function setCurrentTool(toolindex)
{	setNivZoom(toolindex);
}

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['formMap'];
	if (frm != null)
	{	frm.cmd.value=strAction;
		frm.submit();
	}
}

function Rechercher_Click(module, mode, etape)
{	var bln=0;
	var val1="";
	var val2="";
	var val3="";
	var val4="";
	var frm=xGetElementById('frmRechercher');
	if (module == "parc")
	{	// liste
		var t=eval("frm."+mode);
		if (mode == "ville")
		{	if (t == null)
			{	t=eval("frm.cboville");
				var value=t.options[t.selectedIndex].value;
			}
			else
			{	var value=t.value;
			}
		}
		else
		{	var value=t.options[t.selectedIndex].value;
		}
		var typologies=frm.typologies;
		if (typologies != null)
		{	var typologie=typologies.options[typologies.selectedIndex].value;
			if (typologie != "")
			{	val1=typologie;
			}
		}
		var fer=frm.chkfer;
		if (fer != null)
		{	if (fer.checked)
			{	val2=1;
			}
		}
		var fluvial=frm.chkfluvial;
		if (fluvial != null)
		{	if (fluvial.checked)
			{	val3=1;
			}
		}
	}
	// recherche d'une parcelle
	else	if (module == "parcelle")
	{	// liste
		var t=eval("frm."+mode);
		if (mode == "ville")
		{	if (t == null)
			{	t=eval("frm.cboville");
				var value=t.options[t.selectedIndex].value;
			}
			else
			{	var value=t.value;
			}
		}
		else
		{	var value=t.options[t.selectedIndex].value;
		}
		var s=frm.surface.value;
		if (s != "")
		{	val1=s;
		}
		var typologies=frm.typologies;
		var typologie=typologies.options[typologies.selectedIndex].value;
		if (typologie != "")
		{	val2=typologie;
		}
		var fer=frm.chkfer;
		if (fer != null)
		{	if (fer.checked)
			{	val3=1;
			}
		}
		var fluvial=frm.chkfluvial;
		if (fluvial != null)
		{	if (fluvial.checked)
			{	val4=1;
			}
		}
	}
	// recherche d'un local
	else	if (module == "local")
	{	// liste
		var t=eval("frm."+mode);
		var value=t.options[t.selectedIndex].value;

		var s=frm.surface.value;
		if (s != "")
		{	val1=s;
		}
		var types=frm.types;
		var type=types.options[types.selectedIndex].value;
		if (type != "")
		{	val2=type;
		}
		var commercialisations=frm.commercialisation;
		var commercialisation=commercialisations.options[commercialisations.selectedIndex].value;
		if (commercialisation != "")
		{	val3=commercialisation;
		}
	}
	// recherche d'une entreprise
	else	if (module == "entreprise")
	{	// liste
		if (mode == "siret")
		{	var frm=xGetElementById('frmRechercherSIRET');
			var t=eval("frm."+mode);
			var value=t.value;
			if ((value.length!=9) && (value.length!=14))
			{	alert("Votre saisie est incorrecte.\nUn code SIREN comporte 9 caractères, et un code SIRET 14.");
				return false;
			}
		}
		// Nom ou activité + commune
		else
		{ var t=eval("frm."+mode);
			var value=t.value;

			var v=frm.ville.value;
			if (v != "")
			{	val1=PrepareSaisie(v);
			}
		}
	}
	// recherche d'un canton
	else	if (module == "canton")
	{	// liste
		var t=eval("frm."+mode);
		var value=t.options[t.selectedIndex].value;
	}
	// valeur dans une liste
	else if (eval("frm.cbo"+mode+"!= null"))
	{	// var t=frm.nom;
		var t=eval("frm.cbo"+mode);
		var value=t.options[t.selectedIndex].value;
	}
	// valeur dans un champ texte
	else
	{	// var t=frm.nom;
		var t=eval("frm."+mode);
		var value=t.value;
	}

	value=PrepareSaisie(value);

	var f=xGetElementById('Formulaire');
	f.module.value=module;
	f.etape.value=etape;
	f.mode.value=mode;
	f.val.value=value;
	f.val1.value=val1;
	f.val2.value=val2;
	f.val3.value=val3;
	f.val4.value=val4;
	f.submit();
	return false;
}

function majEmprise(w,niv,xmin, ymin, xmax, ymax)
{	var f=xGetElementById('Formulaire');

	// Changement d'outil
	var bln = 0;
	var toolcur=f.niv.value;
	eval("t=xGetElementById('niv"+toolcur+"');");
	if (t!=null)
	{	t.src = "images/fct/zoom_n.gif";
	}

	eval("t=xGetElementById('niv"+niv+"');");
	if (t!=null)
	{	t.src = "images/fct/zoom_c.gif";
	}
	
	// maj de la distance
	eval("t=xGetElementById('distance');");
	if (t!=null)
	{	t.src = "getimgdist.php?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;
//alert("majEmprise:"+xmin+","+ymin+","+xmax+","+ymax);
}

function fullScreen()
{ var f=xGetElementById('Formulaire');
	host=f.host.value;
	niv=f.niv.value;
	xmin=f.XMIN.value;
	ymin=f.YMIN.value;
	xmax=f.XMAX.value;
	ymax=f.YMAX.value;

	var cible=f.cible.value;
	var cx=f.cx.value;
	var cy=f.cy.value;

	var strURL="index_fs.php?host="+host+"&cmd=zoom&niv="+niv+"&op=locate&XMIN="+xmin+"&YMIN="+ymin+"&XMAX="+xmax+"&YMAX="+ymax+"&cible="+cible+"&cx="+cx+"&cy="+cy;
	xWinOpen(strURL, 800, 600);
}

function clickInfo(param)
{	document.href="cmd=zoom&op=locate"+param;
}

function changeInfos()
{	var frm=xGetElementById('frm');
	var frmSend=xGetElementById('frm_send');
	var f=xGetElementById('Formulaire');

	var t=eval("frm.info");
	var info=t.options[t.selectedIndex].value;
	frmSend.XMIN.value=f.XMIN.value;
	frmSend.YMIN.value=f.YMIN.value;
	frmSend.XMAX.value=f.XMAX.value;
	frmSend.YMAX.value=f.YMAX.value;

	frmSend.info.value=info;
	frmSend.submit();
	return false;
}
