// Valide le formulaire avant envoi 
// src http://www.editeurjavascript.com/scripts/scripts_formulaires_3_568.php
var d = new Date();
var oembed;
var map;

//BULLETIN QUOTIDIEN   
function Aspa_Update_Info_Site(site, ech){
	//On met à jour le graph histo pour le site concerné
	var currentTime = new Date();
	document.getElementById('aspa_iqa_barplot').src = '../dev_aspa/ajax/bulletin/aspa_update_barplot.php?d='+site+'&ech='+ech+'&'+currentTime.getTime();
	if(ech == 'obs'){
		Ajax_Update_Page('d='+site+'&'+currentTime.getTime(), '../dev_aspa/ajax/bulletin/aspa_update_subiqa.php', 'GET', 'aspa_block_subiqa', true);
	}else{
		Ajax_Update_Page('d='+site+'&'+currentTime.getTime(), '../dev_aspa/ajax/bulletin/aspa_update_poliqa.php', 'GET', 'aspa_block_subiqa', true);
	}

}

//ATLAS DE LA QUALITE DE L'AIR

function Atlas_Update_Mnu(mnu){
	//On recupere la commune selectionnée
	var entity =document.getElementById("list_entite").options[document.getElementById("list_entite").selectedIndex].value;
	window.location.replace('?id='+entity+'&mnu='+mnu);
}

function Atlas_Change_SelectedIndex(idobj, index){
	document.getElementById(idobj).selectedIndex = index;
}

function Atlas_Update_GChartsByPol(idobj){
	var entity =document.getElementById("list_entite").options[document.getElementById("list_entite").selectedIndex].value;
	Ajax_Update_Page('id='+entity+'&pol='+idobj+'&'+d.getTime(), '../dev_aspa/ajax/atlas/aspa_update_piecharts.php', 'GET', 'atlas_gchartwnd', true);
}
function Atlas_Update_ListPol(idobj){
	Ajax_Update_Page('cat='+idobj+'&'+d.getTime(), '../dev_aspa/ajax/atlas/aspa_update_listbox.php', 'GET', 'hackie_2ndlist', true);
	oembed = document.getElementById('svg_embed');
	//rend invisible le svg le temps du chargement de la carte
	oembed.getSVGDocument().getElementById("poly").setAttribute('visibility','hidden');
	oembed.getSVGDocument().getElementById("map_legend").setAttribute('visibility','hidden');
	var scale =document.getElementById("list_scale").options[document.getElementById("list_scale").selectedIndex].value;
	var entity =document.getElementById("list_entite").options[document.getElementById("list_entite").selectedIndex].value;
	Ajax_Eval_Page('cat='+idobj+'&scale='+scale+'&'+d.getTime(),'../dev_aspa/ajax/atlas/aspa_update_drawmap_emi.php', 'GET', true);
	Ajax_Update_Page('id='+entity+'&cat='+idobj+'&'+d.getTime(), '../dev_aspa/ajax/atlas/aspa_update_piecharts.php', 'GET', 'atlas_gchartwnd', true);
	document.getElementById("aspa_atlas_postit").src = '../dev_aspa/ajax/atlas/update_canvas_info_com_emi.php?id='+entity+'&cat='+idobj+'&'+d.getTime();
}
function Atlas_Update_ListEch(idobj){
	oembed = document.getElementById('svg_embed');
	//rend invisible le svg le temps du chargement de la carte
	oembed.getSVGDocument().getElementById("poly").setAttribute('visibility','hidden');
	Ajax_Eval_Page('ech='+idobj+'&'+d.getTime(),'../dev_aspa/ajax/atlas/aspa_update_drawmap_iqa.php', 'GET', true);
}
function Atlas_Update_MapsChartsByEntity(idobj) {
	oembed = document.getElementById('svg_embed');
	//On recupere le mnu
	var curmnu = document.getElementById("atlas_mnu").value;
	if(curmnu == "emi"){
		var pol =document.getElementById("list_pol").options[document.getElementById("list_pol").selectedIndex].value;
		document.getElementById("aspa_atlas_postit").src = '../dev_aspa/ajax/atlas/update_canvas_info_com_emi.php?id='+idobj+'&pol='+pol+'&'+d.getTime();
		Ajax_Eval_Page('id='+idobj+'&'+d.getTime(),'../dev_aspa/ajax/atlas/aspa_update_highlight.php', 'GET', true);
		Ajax_Update_Page('id='+idobj+'&pol='+pol+'&'+d.getTime(), '../dev_aspa/ajax/atlas/aspa_update_piecharts.php', 'GET', 'atlas_gchartwnd', true);
		Atlas_Update_Map(idobj);
	}else if (curmnu== "iqa") {
		document.getElementById("aspa_atlas_postit").src = '../dev_aspa/ajax/atlas/update_canvas_info_com_iqa.php?id='+idobj+'&'+d.getTime();
		Ajax_Eval_Page('id='+idobj+'&'+d.getTime(),'../dev_aspa/ajax/atlas/aspa_update_highlight.php', 'GET', true);	
	}
}
function Atlas_Update_DrawMapByPol(idobj) {
	var entity =document.getElementById("list_entite").options[document.getElementById("list_entite").selectedIndex].value;
	var scale =document.getElementById("list_scale").options[document.getElementById("list_scale").selectedIndex].value;
	Atlas_Update_DrawMap(idobj,scale);
	document.getElementById("aspa_atlas_postit").src = '../dev_aspa/ajax/atlas/update_canvas_info_com_emi.php?id='+entity+'&pol='+idobj+'&'+d.getTime();
	Atlas_Update_GChartsByPol(idobj);
}
function Atlas_Update_DrawMapByScale(idobj) {
	var pol =document.getElementById("list_pol").options[document.getElementById("list_pol").selectedIndex].value;
	Atlas_Update_DrawMap(pol,idobj);
}
function Atlas_Update_DrawMap(pol, scale){
	oembed = document.getElementById('svg_embed');
	//rend invisible le svg le temps du chargement de la carte
	oembed.getSVGDocument().getElementById("poly").setAttribute('visibility','hidden');
	oembed.getSVGDocument().getElementById("map_legend").setAttribute('visibility','hidden');
	Ajax_Eval_Page('pol='+pol+'&scale='+scale+'&'+d.getTime(),'../dev_aspa/ajax/atlas/aspa_update_drawmap_emi.php', 'GET', true);
}
function Atlas_Update_DrawMap_Iqa(ech){
	oembed = document.getElementById('svg_embed');
	//rend invisible le svg le temps du chargement de la carte
	oembed.getSVGDocument().getElementById("poly").setAttribute('visibility','hidden');
	Ajax_Eval_Page('ech='+ech+'&'+d.getTime(),'../dev_aspa/ajax/atlas/aspa_update_drawmap_iqa.php', 'GET', true);
}

function Atlas_HighlightEntity (idobj) {
	Ajax_Eval_Page('id='+idobj+'&'+d.getTime(),'../dev_aspa/ajax/atlas/aspa_update_highlight.php', 'GET', true);
}
function Atlas_Load_GMaps() {
	if(document.getElementById("gmap")){
      		if (GBrowserIsCompatible()) {
      			// Test si SVG est ok.
			if (document.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#SVG", "1.1")) {
				_mSvgEnabled = true;
				_mSvgForced = true;
			}
			_mPreferMetric = true;
       			//Definition de la carte
			map = new GMap2(document.getElementById("gmap"),{mapTypes: [G_SATELLITE_MAP,G_HYBRID_MAP,G_NORMAL_MAP,G_PHYSICAL_MAP]});
			map.addControl(new GSmallZoomControl3D());
			//map.addControl(new GScaleControl());
			map.addControl(new GMenuMapTypeControl());
			map.setMapType(G_SATELLITE_MAP);
			map.setCenter(new GLatLng(48.2597064,7.4560424),8);
			//Restricting the range of zoom levels
			var mt = map.getMapTypes();
			for (var i=0; i<mt.length;i++){
				mt[i].getMinimumResolution = function() {return 8;}
				mt[i].getMaximumResolution = function() {return 18;}
			}
			//Centrage de la carte sur la commune choisie au hasard
			Atlas_Update_Map(document.getElementById("list_entite").options[document.getElementById("list_entite").selectedIndex].value);
		}
	}
}
function Atlas_Update_Map(idobj){
	var request = GXmlHttp.create();
	request.open("GET", "http://www.atmo-alsace.net/dev_aspa/xml/atlas/atlas_markers.xml?"+d.valueOf(), true);
	request.onreadystatechange = function() {
		if(request.readyState == 4) {
			var xmlDoc = request.responseXML;
			var markers = xmlDoc.documentElement.getElementsByTagName("marker");
			var points = new Array(0);
			//On boucle sur les markers
			for (var i = 0; i < markers.length; i++) {
				//On test si le noeud correspond bien a la commune en cours
				if(markers[i].getAttribute("id") == idobj){
					if(typeof marker != 'undefined'){Atlas_Remove_Marker(marker);}
					points[i] = new GLatLng(parseFloat(markers[i].getAttribute("lat")), parseFloat(markers[i].getAttribute("lng")));
					marker = Atlas_Create_Marker(points[i]);
					map.addOverlay(marker);
					map.setCenter(points[i],16);
					break;
				}
			}
			
		}
	}
	request.send(null);
}
function Atlas_Create_Marker(point) {
	var marker = new GMarker(point);
	return marker;
}
function Atlas_Remove_Marker(marker) {
	map.removeOverlay(marker);
}
function Atlas_GUnload() {}

//AJAX
// http://qwix.media-box.net/admin.php/2005/01/21/45-XmlhttprequestEtPhp#c706
// Permet d'envoyer des données en GET ou POST en utilisant les XmlHttpRequest 

function makeHttpObject() {
	try{return new XMLHttpRequest();}
	catch (error) {}
	try {return new ActiveXObject("Msxml2.XMLHTTP");}
	catch (error) {}
	try {return new ActiveXObject("Microsoft.XMLHTTP");}
	catch (error) {}	
	throw new Error("Could not create HTTP request object");
}

function Ajax_Update_Page(data, page, method, idobj, async) 
{ 
	if(document.all){
        	//Car document.all detect IE et OPERA http://www.javascriptkit.com/javatutors/objdetect3.shtml
        	if(window.opera){
			//Opera
			var XhrObj = new XMLHttpRequest(); 
		}else{
			//Internet Explorer 
			var XhrObj = new ActiveXObject("Microsoft.XMLHTTP");
		} 
    	}//fin if 
    	else { 
        	//Mozilla 
        	var XhrObj = new XMLHttpRequest(); 
    	}//fin else 
     
    	//définition de l'endroit d'affichage: 
    	var content = document.getElementById(idobj); 
     
    	//si on envoie par la méthode GET: 
    	if(method == "GET"){ 
        	if(data == 'null') { 
            		//Ouverture du fichier sélectionné: 
            		XhrObj.open("GET", page, async); 
        	}//fin if 
       		else { 
            		//Ouverture du fichier en methode GET 
            		XhrObj.open("GET", page+"?"+data , async); 
       		 }//fin else 
    	}//fin if 
   	else if(method == "POST") { 
        	//Ouverture du fichier en methode POST 
        	XhrObj.open("POST", page, async); 
   	}//fin elseif 

    	//Ok pour la page cible
    	XhrObj.onreadystatechange = function(){ 
        	if (XhrObj.readyState == 4 && XhrObj.status == 200){ 
            		content.innerHTML = XhrObj.responseText;
		} 
   	}
    	if(method == "GET") {
       		XhrObj.send(null);

    	}//fin if 
    	else if(method == "POST") { 
        	XhrObj.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); 
		XhrObj.send(data); 
    	}//fin elseif 
}//fin fonction

function Ajax_Eval_Page (data, page, method) { 
	if(document.all) {       
        //Car document.all detect IE et OPERA http://www.javascriptkit.com/javatutors/objdetect3.shtml
        	if(window.opera){
			//Opera
			var XhrObj = new XMLHttpRequest(); 
		}else{
			//Internet Explorer 
			var XhrObj = new ActiveXObject("Microsoft.XMLHTTP") ;
		} 
    	}else { 
        	//Mozilla 
        	var XhrObj = new XMLHttpRequest(); 
    	}//fin else 
     
    //si on envoie par la méthode GET: 
    if(method == "GET") { 
        if(data == 'null') 
        { 
            //Ouverture du fichier sélectionné: 
            XhrObj.open("GET", page); 
        }//fin if 
        else 
        { 
            //Ouverture du fichier en methode GET 
            XhrObj.open("GET", page+"?"+data); 
        }//fin else 
    }//fin if 
    else if(method == "POST") { 
        //Ouverture du fichier en methode POST 
        XhrObj.open("POST", page); 
    }//fin elseif 

    //Ok pour la page cible 
    XhrObj.onreadystatechange = function() { 
        if (XhrObj.readyState == 4 && XhrObj.status == 200){ 
            eval(XhrObj.responseText);
	} 
    }     
    if(method == "GET") { 
        XhrObj.send(null); 
    }//fin if 
    else if(method == "POST") { 
        XhrObj.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); 
        XhrObj.send(data); 
    }//fin elseif 
}//fin fonction

function Ajax_Send_Data(data, page, method, sync) 
{ 
	if(document.all){
        	//Car document.all detect IE et OPERA http://www.javascriptkit.com/javatutors/objdetect3.shtml
        	if(window.opera){
			//Opera
			var XhrObj = new XMLHttpRequest(); 
		}else{
			//Internet Explorer 
			var XhrObj = new ActiveXObject("Microsoft.XMLHTTP");
		} 
    	}//fin if 
    	else { 
        	//Mozilla 
        	var XhrObj = new XMLHttpRequest(); 
    	}//fin else   
    	//si on envoie par la méthode GET: 
    	if(method == "GET"){ 
        	if(data == 'null') { 
            		//Ouverture du fichier sélectionné: 
            		XhrObj.open("GET", page, sync); 
        	}//fin if 
       		else { 
            		//Ouverture du fichier en methode GET 
            		XhrObj.open("GET", page+"?"+data , sync); 
       		 }//fin else
		XhrObj.send(null);  
    	}//fin if 
   	else if(method == "POST") { 
        	//Ouverture du fichier en methode POST 
        	XhrObj.open("POST", page, sync);
		XhrObj.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); 
		XhrObj.send(data);  
   	}//fin elseif 
}//fin fonction

