NElections.prototype.constructor = NElections;
function NElections()
{	// Map
	this.map=new NMap(this);

	// Themes
	this.themes=new NThemes(this);

	// statistiques
	this.stats=new NStats(this);

	// localisation
	this.loc=new NLoc(this);
}

NStats.prototype.constructor = NStats;
function NStats(p)
{
	this.parent=p;
	this.drill_down=0;
	this.ech_stat="COM"; // ["ARR", "COM", "CAN", "CIR", "DEP", "REG"]
	this.zone=0;
	this.groupe1=0;
	this.groupe2=0;
	this.oldstat1=0;
	this.oldstat2=0;
	this.stat1=0;
	this.stat2=0;
	this.taille_symbole=1;
	this.type_territ="";
	this.id_territ="";
	this.couleur1="";
	this.couleur2="";
	this.previsu=0;
	this.mask = this.stat1+'#'+this.stat2+'#'+this.taille_symbole+'#'+this.couleur1+'#'+this.couleur2;
}

NThemes.prototype.constructor = NThemes;
function NThemes(p)
{
	this.parent=p;
	this.layers = new Array(); // tableau de boolean : couche sélectionnée par utilisateur
	this.active_layer = 0; // index de la couche active
	this.selids = ""; // liste des id de la sélection
	this.xmin=0;
	this.xmax=0;
	this.ymin=0;
	this.ymax=0;
}

NLoc.prototype.constructor = NLoc;
function NLoc(p)
{
	this.parent=p;
	this.module="";
	this.etape=1;
	this.mode="";
	this.val="";
	this.val1="";
	this.val2="";
	this.val3="";
	this.val4="";
	this.cible=0;
	this.cx=0;
	this.cy=0;
}

NMap.prototype.constructor = NMap;
function NMap(p)
{
	this.parent=p;
	this.xmin=0;
	this.xmax=0;
	this.ymin=0;
	this.ymax=0;

	this.width=0;
	this.height=0;

	this.xminextent=0;
	this.xmaxextent=0;
	this.yminextent=0;
	this.ymaxextent=0;

	this.scale=0;

	this.curtool=0;
}

NMap.prototype.print = function()
{	var frm=frames['iframemap'];
	if (frm != null)
	{	
		var f=frm.document.forms['formMap'];
		if (f != null)
		{	f.target='print';
			f.action='print_def.php';
			f.submit();

			f.target='';
			f.action='netenmap2.php';
		}
	}
}

NMap.prototype.fullscreen = function()
{ var frm=frames['iframemap'];
	if (frm != null)
	{	
		var f=frm.document.forms['formMap'];
		if (f != null)
		{	s = nelections.stats;
			
			f.target='print';
			f.action='accueil_fs.php';
			var op=f.op.value;
			f.op.value='fullscreen';
			f.submit();

			f.target='';
			f.op.value=op;
			f.action='netenmap2.php';
		}
	}
}

NMap.prototype.pdf = function()
{ var frm=frames['iframemap'];
	if (frm != null)
	{
		var f=frm.document.forms['formMap'];
		if (f != null)
		{	f.target='pdf';
			f.action='print_def.php';
			var op=f.op.value;
			f.op.value='pdf';
			f.submit();

			f.target='';
			f.op.value=op;
			f.action='netenmap2.php';
		}
	}
}

NMap.prototype.download = function()
{	var frm=frames['iframemap'];
	if (frm != null)
	{	var frm=frm.document.forms['formMap'];
		var url=frm.img.value;
		
		var stat1 = 0;
		var stat2 = 0;
		var urlLeg1 = '';
		var urlLeg2 = '';
		
		var f=xGetElementById('frmStats');
		if (f != null)
		{	var stat1 = f.stat1.value;
			var stat2 = f.stat2.value;
		}
		if (stat1 != 0 || stat2 != 0)
		{	var frm=xGetElementById('frmLegende');
			if (frm != null)
			{	if (eval(frm.valLeg1) != null)
				{	urlLeg1 = frm.valLeg1.value;
				}
				if (eval(frm.valLeg2) != null)
				{	urlLeg2 = frm.valLeg2.value;
				}
			}
			else
			{	alert('La légende est en cours de modification. Elle ne sera pas sauvegardée.');
			}
		}
	
		var f=xGetElementById('frmutils');
		if (f != null)
		{	f.cmd.value="download";
			f.val.value=url;
			f.valLeg1.value=urlLeg1;
			f.valLeg2.value=urlLeg2;
			f.submit();
		}
	}
}

NMap.prototype.zoomTo = function(xmin, ymin, xmax, ymax)
{ var frm=frames['iframemap'].document.forms['formMap'];
	if (frm != null)
	{	this.xmin=xmin;
		this.ymin=ymin;
		this.xmax=xmax;
		this.ymax=ymax;

		frm.cmd.value="zoom";
		frm.niv.value=-1;
		frm.op.value="locate";
		frm.XMIN.value=xmin;
		frm.YMIN.value=ymin;
		frm.XMAX.value=xmax;
		frm.YMAX.value=ymax;
		frm.submit();
	}
}

NMap.prototype.refresh = function()
{ var frm=frames['iframemap'].document.forms['formMap'];
	if (frm != null)
	{	
		frm.cmd.value="zoom";
		frm.niv.value=-1;
		frm.op.value="locate";
		var t=this.parent.themes;
		frm.active_layer.value=t.active_layer;
		frm.selids.value=t.selids;
		frm.SELXMIN.value=t.xmin;
		frm.SELYMIN.value=t.ymin;
		frm.SELXMAX.value=t.xmax;
		frm.SELYMAX.value=t.ymax;
		frm.submit();
	}
}

NMap.prototype.zoomSel = function()
{ var themes = this.parent.themes;
	if (themes.selids == "")
	{	alert("Pas de sélection");
		return;
	}

	var xmin= themes.xmin;
	var ymin= themes.ymin;
	var xmax= themes.xmax;
	var ymax= themes.ymax;
	this.zoomTo(xmin, ymin, xmax, ymax)
}

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, n=a.length; for(i=0; i<n; 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='', cprec='', n=x.length;
	for (var i=0; i<n; 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, iFirst=0, iLast=0, r='', n=x.length;
	while (i<n && x.charAt(i)==' ')	i++; // premier non espace
    iFirst = i;
	if (i==n) return r;  // il n'y a que des espaces!
	iLast = n-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)
{	var iLast = x.length, 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="", n=val.length;
	for (i=0; i<n; 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;

	toolcur=nelections.map.niv;

	// 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)
{	toolcur=nelections.map.niv;
	if ((toolcur==null) || (toolcur != toolindex))
	{	eval("t=xGetElementById('niv"+toolindex+"');");
		if (t!=null)
		{	t.src = "images/fct/zoom_c.gif";
		}
	}
}

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 val0="", val1="", val2="", val3="", val4="", val5="", val6="";
	var frm=xGetElementById('frmRechercher');
	
	if (module == 'par')
	{	val0 = frm.elements[0].value;
		val1 = frm.elements[1].value;
		val2 = frm.elements[2].value;
		var s1 = frm.elements[3].value;
		var s2 = frm.elements[4].value;
		if ((s1 != null) || (s2 != null))
		{	if (isNaN(s1) || isNaN(s2))
			{	alert("Veuillez entrer des nombres pour le critère Surface.");
				return false;
			}
			else
			{	val3 = s1+':'+s2;
			}
		}
		val4 = frm.elements[5].value;
		var p1 = frm.elements[6].value;
		var p2 = frm.elements[7].value;
		if ((p1 != null) || (p2 != null))
		{	if (isNaN(p1) || isNaN(p2))
			{	alert("Veuillez entrer des nombres pour le critère Prix.");
				return false;
			}
			else
			{	val5 = p1+':'+p2;
			}
		}
	}
	else
	{	var nb = frm.length - 1;
		if (nb > 0) val0 = frm.elements[0].value;
		if (nb > 1) val1 = frm.elements[1].value;
		if (nb > 2) val2 = frm.elements[2].value;
		if (nb > 3) val3 = frm.elements[3].value;
		if (nb > 4) val4 = frm.elements[4].value;
		if (nb > 5) val5 = frm.elements[5].value;
		if (nb > 6) val6 = frm.elements[6].value;
		
		val4=PrepareSaisie(val4);
		val6=PrepareSaisie(val6);
	}

	val0=PrepareSaisie(val0);
	val1=PrepareSaisie(val1);
	val2=PrepareSaisie(val2);
	val4=PrepareSaisie(val4);
	
	loadEtape2(module, etape, mode, val0, val1, val2, val3, val4, val5, val6);
	
	return false;
}

function majEmprise(w, niv, scale, xmin, ymin, xmax, ymax)
{	// Modification du slider
	var o_slider = window.A_SLIDERS[0];
	o_slider.f_setValue(niv);

	// 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;
	}

	nelections.map.xmin=xmin;
	nelections.map.ymin=ymin;
	nelections.map.xmax=xmax;
	nelections.map.ymax=ymax;
	nelections.map.toolcur=niv;
	nelections.map.niv=niv;
	nelections.map.scale=scale;

//alert("majEmprise:"+xmin+","+ymin+","+xmax+","+ymax);
}

function setActiveLayer(l)
{	var t=nelections.themes, oldLayer = t.active_layer;
	if (l == oldLayer) return;
	
	var frm=frames['iframemap'].document.forms['formMap'];
	if (frm != null)
	{	frm.active_layer.value=l;
	}

	t.active_layer=l;
	t.clear(0);
}