function noenter(e) {
     var key;     
     if(window.event)
          key = window.event.keyCode; //IE
     else
          key = e.which; //firefox     

     return (key != 13);
}

function setActiveToolButton(toolid) {

  for (var i = 0; i < cw3_tools.length; i++) {
    var elt = xGetElementById(cw3_tools[i] + "_icon");

    if(elt == null) continue;
    
    if (cw3_tools[i] == toolid) {
      //elt.className = "toolbar_on";
    } else {
      //elt.className = "toolbar_off";
    }
  }
  var elt = xGetElementById("tool");
  elt.value = toolid;
  
}

function setActiveToolButton2() {
  var currenttool = xGetElementById('tool').value;
  var imgname = currenttool + '_icon';
  var img = xGetElementById(imgname);
  var imgsrc = img.src;
  img.src = imgsrc.replace(/.gif/,"_on.gif");
}
 
function change_image_on(img_name,img_src) { 
  reset_all_images();
  var currentImg = xGetElementById(img_name);
  var currentImgSrc = currentImg.src;
  var lastSlash = currentImgSrc.lastIndexOf('/');
  var currentImgPath = currentImgSrc.substring(0, lastSlash+1);
  currentImg.src = currentImgPath + img_src;
}

function change_image(img_name,img_src) {
  var theActualImage = xGetElementById(img_name);
  var theActualImageSrc = theActualImage.src
  var theIndex = theActualImageSrc.indexOf("_on");
  if (theIndex==-1) {
    var lastslashpos = theActualImageSrc.lastIndexOf('/');
    var baseSrc = theActualImageSrc.substring(0, lastslashpos+1);
    var newSrc = baseSrc + img_src;
    theActualImage.src = newSrc;
  }
}

function reset_all_images() { 
  //var theTools = new Array("zoomin","zoomout","pan","query_by_bbox","outline_point","outline_line","outline_rectangle","outline_poly","distance","surface", "dxf_move", "dxf_rect", "dxf_polygon");
  var theTools = cw3_tools;
  theTools.push("profile_line");
  for (var i = 0; i < theTools.length; i++) {
    var toolname = theTools[i]+"_icon";
    var theActualImage = xGetElementById(toolname);
    if (!theActualImage){
      continue;
    }
    var theActualImageSrc = theActualImage.src;
    var theOriginalImage = theActualImageSrc.replace(/_on/,"");
    theActualImage.src=theOriginalImage;
  }
}

function openConditionsUtilisation(type) {
    if (type=='MO') {
     	window.open('http://sitn.ne.ch/web/conditions_utilisation/contrat_SITN_MO.htm','Conditions_SITN_MO',"width=550,height=550,toolbar=no,menubar=no,resizable=yes,scrollbars=1");
    }
    if (type=='ORTHO') {
        window.open("http://sitn.ne.ch/web/conditions_utilisation/contratdv0022832.htm","Conditions_orthophotos","width=550,height=550,toolbar=no,menubar=no,resizable=yes,scrollbars=1");
    }
    if (type=='CN') {
        window.open("http://sitn.ne.ch/web/conditions_utilisation/contratdv5741.htm","Conditions_cartes_nationales","width=550,height=550,toolbar=no,menubar=no,resizable=yes,scrollbars=1");
    }
}

function resetSession() {
  var elm = xGetElementById('fake_reset');
  elm.name = 'reset_session';
  elm.value = 'reset_session';
  document.carto_form.posted.value=0;
  FormItemSelected();
}

function pleinEcran(URL) {
	alert(URL);
window.location.href = URL;
}

function ValidateForm() {

    var theY = document.carto_form.recentercoords_x.value;
    var theX = document.carto_form.recentercoords_y.value;
    var theScale = document.carto_form.recenter_scale.value;
    var theLayerCategory = document.carto_form.switch_id.value;
    
    //window.alert(theX);
    if ((theX < 180000) || (theX > 230000)) {
       alert("La valeur de X doit être comprise entre 180000 et 230000");
    }
    else if ((theY < 515000) || (theY > 580000)) {
       alert("La valeur de Y doit être comprise entre 515000 et 580000");
    }
    else {
    	openTheWindow(theX,theY,theScale,theLayerCategory);   
    }
}

function CommuneSelected() {
    var theNoCom = document.carto_form.ChoixCommune.value;
    var theY = document.carto_form.recentercoords_x.value;
    var theX = document.carto_form.recentercoords_y.value;
    var theScale = document.carto_form.recenter_scale.value;

    //window.alert(theX);
    //if (theNoCom == 14 ) {
		//theX = 202920;
		//theY = 557475;
		//theScale = 15000;
    //}
    
    if (theNoCom==14) openTheWindow(203000,557000,25000)
    if (theNoCom==20) openTheWindow(198000,552000,50000)
    if (theNoCom==17) openTheWindow(202000,554500,25000)
    if (theNoCom==48) openTheWindow(209000,558000,50000)
    if (theNoCom==11) openTheWindow(200000,554000,50000)
    if (theNoCom==30) openTheWindow(196500,536000,50000)
    if (theNoCom==19) openTheWindow(201300,548500,25000)
    if (theNoCom==59) openTheWindow(203000,547000,50000)
    if (theNoCom==32) openTheWindow(193000,532000,50000)
    if (theNoCom==37) openTheWindow(212000,559000,50000)
    if (theNoCom==38) openTheWindow(213000,561000,50000)
    if (theNoCom==50) openTheWindow(206600,556300,25000)
    if (theNoCom==13) openTheWindow(202000,556000,50000)
    if (theNoCom==16) openTheWindow(204000,557000,50000)
    if (theNoCom==6) openTheWindow(209600,568200,25000)
    if (theNoCom==12) openTheWindow(199500,555000,25000)
    if (theNoCom==27) openTheWindow(197500,538500,50000)
    if (theNoCom==7) openTheWindow(211000,569000,50000)
    if (theNoCom==39) openTheWindow(213500,563500,50000)
    if (theNoCom==8) openTheWindow(212000,568000,50000)
    if (theNoCom==45) openTheWindow(210000,561000,50000)
    if (theNoCom==43) openTheWindow(209000,561300,50000)
    if (theNoCom==31) openTheWindow(195000,535000,50000)
    if (theNoCom==46) openTheWindow(212000,558000,50000)
    if (theNoCom==44) openTheWindow(210000,559000,50000)
    if (theNoCom==23) openTheWindow(193000,547000,50000)
    if (theNoCom==21) openTheWindow(195000,550000,50000)
    if (theNoCom==2) openTheWindow(207000,564500,50000)
    if (theNoCom==56) openTheWindow(203500,536700,50000)
    if (theNoCom==60) openTheWindow(217000,554000,50000)
    if (theNoCom==57) openTheWindow(207000,544000,50000)
    if (theNoCom==33) openTheWindow(191000,528000,50000)
    if (theNoCom==62) openTheWindow(211000,552000,50000)
    if (theNoCom==55) openTheWindow(208000,541000,50000)
    if (theNoCom==9) openTheWindow(212000,571500,50000)
    if (theNoCom==53) openTheWindow(212000,548000,50000)
    if (theNoCom==41) openTheWindow(216500,566000,50000)
    if (theNoCom==36) openTheWindow(197000,530000,50000)
    if (theNoCom==54) openTheWindow(213000,544000,50000)
    if (theNoCom==51) openTheWindow(207000,555000,50000)
    if (theNoCom==47) openTheWindow(211000,557000,50000)
    if (theNoCom==61) openTheWindow(217000,549000,50000)
    if (theNoCom==58) openTheWindow(206000,546000,50000)
    if (theNoCom==35) openTheWindow(195000,527000,50000)
    if (theNoCom==10) openTheWindow(215000,572000,50000)
    if (theNoCom==4) openTheWindow(207000,567000,50000)
    if (theNoCom==24) openTheWindow(194500,547000,50000)
    if (theNoCom==52) openTheWindow(205000,555000,50000)
    if (theNoCom==26) openTheWindow(196000,537000,50000)
    if (theNoCom==1) openTheWindow(205000,561000,50000)
    if (theNoCom==29) openTheWindow(201000,546000,50000)
    if (theNoCom==15) openTheWindow(204500,558500,50000)
    if (theNoCom==18) openTheWindow(203000,552000,50000)
    if (theNoCom==22) openTheWindow(194000,549000,50000)
    if (theNoCom==3) openTheWindow(207000,566000,50000)
    if (theNoCom==34) openTheWindow(196000,533000,50000)
    if (theNoCom==42) openTheWindow(211500,562500,50000)
    if (theNoCom==5) openTheWindow(208000,569000,50000)
    if (theNoCom==28) openTheWindow(199000,542000,50000)
    if (theNoCom==49) openTheWindow(207000,559500,50000)
    if (theNoCom==25) openTheWindow(192000,548000,25000)
    if (theNoCom==40) openTheWindow(214000,564500,50000)
  	  
}

function openTheWindow(theX,theY,theScale,theLayerCategory) {
  var baseUrl = window.location.href;
  var expos = baseUrl.indexOf('?');
  if (expos >= 0) {
    baseUrl = baseUrl.substring(0, expos);
  }
  window.location.href = baseUrl + "?x="+theY+"&y="+theX+"&echelle="+theScale;
}

function openLocalisation() {
    var theMapSize = document.carto_form.mapsize.value;
    var theLayerCategory = document.carto_form.switch_id.value;
	
    if (document.carto_form.liste_localisation.selectedIndex == 1) 
        //window.open("sitn/localisation/consSelectAdresseIntegr_MtCl.php?theme="+theLayerCategory+"&taille="+theMapSize,null,"height=300,width=300,toolbar=no,menubar=no,resizable=yes,scrollbars=yes");
        window.open(theProject+"/localisation/consSelectAdresseIntegr_MtCl.php",null,"height=300,width=300,toolbar=no,menubar=no,resizable=yes,scrollbars=yes");
    if (document.carto_form.liste_localisation.selectedIndex == 2) 
        //window.open("sitn/localisation/consSelectParcelle.php?theme="+theLayerCategory+"&taille="+theMapSize,null,"height=300,width=300,toolbar=no,menubar=no,resizable=yes,scrollbars=yes");
        window.open(theProject+"/localisation/consSelectParcelle.php",null,"height=300,width=300,toolbar=no,menubar=no,resizable=yes,scrollbars=yes");
    if (document.carto_form.liste_localisation.selectedIndex == 3) 

//window.open("sitn/localisation/consSelectBatiment.php?theme="+theLayerCategory+"&taille="+theMapSize,null,"height=300,width=300,toolbar=no,menubar=no,resizable=yes,scrollbars=yes");
	window.open(theProject+"/localisation/consSelectBatiment.php",null,"height=300,width=300,toolbar=no,menubar=no,resizable=yes,scrollbars=yes");

    if (document.carto_form.liste_localisation.selectedIndex == 4) 
	window.open(theProject+"/localisation/consSelectArret.php",null,"height=300,width=300,toolbar=no,menubar=no,resizable=yes,scrollbars=yes");

    if (document.carto_form.liste_localisation.selectedIndex == 5) 
	window.open(theProject+"/localisation/consSelectZoneDeSport.php",null,"height=300,width=300,toolbar=no,menubar=no,resizable=yes,scrollbars=yes");
	document.carto_form.liste_localisation.selectedIndex = 0;
		
}

function openOnlyMap() {
	
	var theActualURL = window.location.href;
	theActualURL = theActualURL.replace("#","");
	if (theActualURL.indexOf("?")>0) {
		var theNewURL = theActualURL + "&mode=image";
	} else {
		var theNewURL = theActualURL + "?mode=image";
	}
	window.open(theNewURL);
	
}

function popWin(pag, w, h)
    {
    if (!w) w = 400;
    if (!h) h = 200;    
    pop = window.open(pag,'pop','dependent=yes, height=' + h+ ',width=' + w + ',status=no,toolbar=no,menubar=no,resizable=yes,scrollbars=yes');
    pop.focus();   
    }

function getBaseUrlFromCurrentWindow() {

	var theHost = window.location.host;
        var theBaseUrl = "http://"+theHost+"/";
        return theBaseUrl;

}

function redirectToSITN() {

      var theURL = "http://sitn.ne.ch/index.html?x="+theX+"&y="+theY+"&echelle="+theScale;
      window.open(theURL);

}

function ChangeTheme() {
	var theURL = window.location.href;
	theURL = theURL.replace("#","");
	if (theURL.indexOf("?")>0) {
	theURL = theURL.substring(0, theURL.indexOf("?"));
	}
	window.location.href = theURL + "?theme=" + document.carto_form.themes.value;
	}  
	
function openScalePage() {
	
	window.open("sitn/localisation/consSelectEchelle.php","Echelle",'height=100,width=190,status=no,toolbar=no,menubar=no,resizable=no,scrollbars=no'); 
	
}	


function toggleVisibility(id, NNtype, IEtype, WC3type, imgName,imgID) {
	document[imgID].src = "http://sitn.ne.ch/web/images/logos_cantons/" + imgName;
	if (document.getElementById) {
		eval("document.getElementById(id).style.visibility = \"" + WC3type + "\"");
	} else {
		if (document.layers) {
			document.layers[id].visibility = NNtype;
		} else {
			if (document.all) {
				eval("document.all." + id + ".style.visibility = \"" + IEtype + "\"");
			}
		}
	}
}

function changeObjectVisibility(objectID,theDisplay,theVisibility) {

	document.getElementById(objectID).style.display=theDisplay;
	document.getElementById(objectID).style.visibility=theVisibility;

}

function manageLocalisationVisibility() {

	if (document.carto_form.liste_localisations.selectedIndex == 1) {
		changeObjectVisibility("adresses_guidee","none","hidden");
		changeObjectVisibility("parcelles","none","hidden");
		changeObjectVisibility("adresses_motcle","block","visible");
	}
	if (document.carto_form.liste_localisations.selectedIndex == 2) {
		changeObjectVisibility("adresses_motcle","none","hidden");
		changeObjectVisibility("parcelles","none","hidden");
		changeObjectVisibility("adresses_guidee","block","visible");
	}
	if (document.carto_form.liste_localisations.selectedIndex == 3) {
		changeObjectVisibility("adresses_motcle","none","hidden");
		changeObjectVisibility("adresses_guidee","none","hidden");
		changeObjectVisibility("parcelles","block","visible");
	}

	document.carto_form.liste_localisations.selectedIndex = 0;

}

  
function showLoginPage() {
	document.getElementById("auth_container").style.display = "block";
}
  
function hideLoginPage() {
	document.getElementById("auth_container").style.display = "none";
}

 
function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function fullScreen() {

	// Va chercher l'URL de cartoweb, sans tout ce qu'il y a après le ?
	if (window.location.href.indexOf("?",0) > 0) {
		var theSitnUrl = trim(window.location.href.substring(0,window.location.href.indexOf("?",0)),"#"); 
	}else{
		var theSitnUrl = trim(window.location.href,"#"); 	
	}

	var theNewSitnUrl = theSitnUrl = theSitnUrl + "?taille=" + (document.carto_form.mapsize.selectedIndex-1);

	// En fonction de si on est dans NEAT ou pas
	if (parent.location != window.location) {
		window.parent.location.href = theNewSitnUrl;
	}else{
		window.location.href = theNewSitnUrl;
	}

}
