var markers_route=""
var index_click=new Array();
function addPunto(tipo){

	 map.controls[1].leaveMode();
	
	 if (!index_click[tipo]){
	 		index_click[tipo]=(map.events.listeners.click.length)
	 		
	 	map.events.register("click", map, function(e) { 
		   coord_utm = map.getLonLatFromViewPortPx(e.xy);
		   coord_utmx=coord_utm.lon
		   coord_utmy=coord_utm.lat
		 
		   //bisogna convertire le coordinate da utm33 a geografico wgs84
		   	
				XGeo=(UTM2LL(coord_utmx,coord_utmy,33))[0]
				YGeo=(UTM2LL(coord_utmx,coord_utmy,33))[1]
				
				//funzione per la geocodifica inversa  
				// revers(XGeo,YGeo)
	   	switch (tipo) {
	   		case "P":
	   		
	   		 addPartenza(XGeo,YGeo,true,"",coord_utm);
	   		 break;
	   		case "A":
	   			addArrivo(XGeo,YGeo,true,"",coord_utm);
	   			 break;
	   		case "PS":
	   			addPassaggio(XGeo,YGeo,true,"",coord_utm);
	   			 break;
	   		case "B":
	   			addBarriera(XGeo,YGeo,true,"",coord_utm);
	   			 break;
	   		  
	   	}
   })
  }else{
  	n_eliminati=0
  	for (var obj in index_click){
  			
 
  	 		object=index_click[obj]-n_eliminati
  	 		map.events.unregister("click",map,map.events.listeners["click"][object].func);
  	 		n_eliminati++
  	 		index_click[obj]=null
  	
  	 	
  	}
  	//index_click[tipo]=index_click[tipo]-n_eliminati
  	addPunto(tipo)
  }
}
var Icona_P
var Icona_A
var Icona_PS
var Icona_B
var Icona_ALL
function remove_marker_route(tipo){
				indice=map.getLayerIndex(eval("Icona_"+tipo))
			
				if (indice==-1) {
		   			eval("Icona_"+tipo+"= new OpenLayers.Layer.Markers('"+tipo+"') ;")
				    map.addLayer(eval("Icona_"+tipo));
				  
				   
				}else if (tipo=="P" || tipo=="A"){
					 map.layers[indice].clearMarkers();
				}
}
var featureP
var featureA
var featurePS
function add_marker_route(coord_utm,tipo,idmarker){
				remove_marker_route(tipo)
				bordericon=""
				id_poi=null
				id_tem_marker=""
				switch (tipo){
					case "P":
					 if (idmarker!="" && map.layers[0].markers){
					  	icon=map.layers[0].markers[idmarker].icon.clone();
					  	bordericon="2px solid green"; 
					  	bordercolor="green";
					  	id_poi=RTPartenzaIdPoi
					  	id_tem_marker=RTPartenzaTematismoPoi
						}else
							icon = OpenLayers.Marker.defaultIcon("PK_layout/img/BandierinaMappaPartenza.gif");
						break;
					case "A":
					 if (idmarker!="" && map.layers[0].markers){
					  	icon=map.layers[0].markers[idmarker].icon.clone();
					  	bordericon="2px solid red"; 
					  	bordercolor="red";
					  	id_poi=RTArrivoIdPoi
					  	id_tem_marker=RTArrivoTematismoPoi
						}else
							icon = OpenLayers.Marker.defaultIcon("PK_layout/img/BandierinaMappa.gif");
						break;
				 case "B":
						icon = OpenLayers.Marker.defaultIcon("PK_layout/img/BandierinaMappaBarriera.gif");
						break;
					case "PS":
					  if (idmarker!="" && map.layers[0].markers){
					  	icon=map.layers[0].markers[idmarker].icon.clone();
					  	bordericon="2px solid #800080"; 
					  	bordercolor="#800080";
					  	id_poi=RTStopIdPoi[RTStopsCount-1]
					  	id_tem_marker=RTStopTematismoPoi[RTStopsCount-1]
						}else{
							icon = OpenLayers.Marker.defaultIcon("PK_layout/img/BandierinaMappaPassaggio.gif");
						}
						break;
				}
				popup=null
				if (bordericon!=""){
				    data_feature=map.layers[0].features[idmarker].data
				   // contentHTML='<iframe src="georss/georss_item_store.asp?'+location+'&id_tem='+id_tematismo+'&tlx='+tlx+'&tly='+tly+'&brx='+brx+'&bry='+bry+'&width='+width_view+'&nome='+this.nome+'&comune='+this.comune+'&indirizzo='+this.indirizzo+'&idatt='+this.attrattori+'&tema='+this.tema+'&id_marker='+i+'&id_lingua='+curLang+'" width="180" id="geo" name="geo" frameborder=0 scrolling="no" height="'+27*n_righe+'" align="center"></iframe>'
             tlx=map.getExtent().left
             tly=map.getExtent().top
             brx=map.getExtent().right
             bry=map.getExtent().bottom
				     data_feature['popupContentHTML'] ='<iframe src="../georss/georss_item_store.asp?'+coord_utm+'&id_tem='+id_tem_marker+',&tlx='+tlx+'&tly='+tly+'&brx='+brx+'&bry='+bry+'&width='+map.getSize().w+'&nome=&comune=&indirizzo=&idatt='+id_poi+'&tema=0" width="180" id="geo" name="geo" frameborder=0 scrolling="no" height="27" align="center"></iframe>'
             noclick=1
				} else{
				 	  data_feature = {};
				 	  noclick=0
				} 
				 		data_feature.icon=icon
		   	   	
		   	   	eval("feature"+tipo+" = new OpenLayers.Feature(sfondo,coord_utm,data_feature);");	
	
		   		 	//marker = new OpenLayers.Marker(coord_utm,icon);
		  		           
           	marker = eval("feature"+tipo).createMarker();
        
        		eval("Icona_"+tipo).addMarker(marker);
        		if (noclick==1)
            		marker.events.register("click", eval("feature"+tipo), m_click);
                                                             
        
        
       
		    if (bordericon!="") marker.icon.setBorder(bordericon,bordercolor)
}

 function m_click(evt) {
 					
            if (popup == null) {
                popup = this.createPopup();
               // popup.setContentHTML(data_feature.ContentHTML);
                popup.setBackgroundColor("yellow");
                popup.setOpacity(0.7);
                map.addPopup(popup);
            } else {
                map.removePopup(popup);
                popup.destroy();
                popup = null;
            }
            Event.stop(evt);
        }        	
	
function rimuovi_icone(tipo,nmarker){
			indice=map.getLayerIndex(eval("Icona_"+tipo))
			if (indice!=-1 ) {
		   switch (tipo){
					case "P":		   
						 map.layers[indice].clearMarkers();
						 break;
					case "A":		   
						 map.layers[indice].clearMarkers();
						 break;
					case "B":		   
						 map.layers[indice].removeMarker(map.layers[indice].markers[nmarker]);
						 break;
					case "PS":
						   
						 map.layers[indice].removeMarker(map.layers[indice].markers[nmarker]);
						 break;
						}
				}else if (tipo=="ALL"){
				
					 if (map.getLayerIndex(Icona_P)!=-1)  map.layers[map.getLayerIndex(Icona_P)].destroy();
					 if (map.getLayerIndex(Icona_A)!=-1) 	map.layers[map.getLayerIndex(Icona_A)].destroy();
					 if (map.getLayerIndex(Icona_B)!=-1)   map.layers[map.getLayerIndex(Icona_B)].destroy();
					 if (map.getLayerIndex(Icona_PS)!=-1)  map.layers[map.getLayerIndex(Icona_PS)].destroy();
					 if (map.getLayerIndex(vectorLayer)!=-1)  map.layers[map.getLayerIndex(vectorLayer)].destroy();
					 cancel_all();
					 
		        			
			}
}	

function salva_perc(){
	
	if (creo_itin) {
	 // document.getElementById("ifrmSaveRouting").src="save_perc.asp?id="+creo_itin
	    var form_obj=document.getElementById("formSaveRouting");
	    var formRoutingGeocoding = document.getElementById("formRoutingGeocoding");	 
	    
	    form_obj.arcxml_request.value=formRoutingGeocoding.XMLRequest.value;
	    form_obj.arcxml_response.value=coordinate;
	    form_obj.idpoi_partenza.value=RTPartenzaIdPoi;
	    form_obj.civ_partenza.value=RTPartenzaCiv;
	    form_obj.typepoi_partenza.value=RTPartenzaTypePoi;
	    form_obj.tempoi_partenza.value=RTPartenzaTematismoPoi;
	    form_obj.idpoi_arrivo.value=RTArrivoIdPoi;
	    form_obj.civ_arrivo.value=RTArrivoCiv;
	    form_obj.typepoi_arrivo.value=RTArrivoTypePoi;
	    form_obj.tempoi_arrivo.value=RTArrivoTematismoPoi;
	    
	    indirizzo_b="";
	    for (var k=0;k<RTBarCount;k++){
	    	indirizzo_b=indirizzo_b+RTBarDesc[k]+"$"
	    }
	    form_obj.desc_barriera.value=indirizzo_b;
	    form_obj.elenco_passaggio.value=RTStopNames.join("$");
	    form_obj.idpoi_passaggio.value=RTStopIdPoi.toString();
	    form_obj.typepoi_passaggio.value=RTStopTypePoi.toString();
	    form_obj.tempoi_passaggio.value=RTStopTematismoPoi.toString();
	    form_obj.time.value=RTTotals.time;
	    form_obj.submit();
	}   
}    

function cancel_all(){
	//punti di partenza e arrivo
 RTPartenzaNames = "";
 RTPartenzaDesc = "";
 RTPartenzaCiv = "";
 RTPartenza = false;
 RTPartenzaX = 0;
 RTPartenzaY = 0;
//MC 02/10/07
RTPartenzaIdPoi = 0;
RTPartenzaTematismoPoi ="" ;
RTPartenzaTypePoi = "";
RTPartenzaTitlePoi = "";

RTArrivoNames = "";
RTArrivoDesc = "";
RTArrivoCiv = "";
RTArrivo = false;
RTArrivoX = 0;
RTArrivoY = 0;

RTArrivoIdMarker=0;
RTArrivoIdPoi = 0;
RTArrivoTematismoPoi ="" ;
RTArrivoTypePoi = "";
RTArrivoTitlePoi = "";


RTStopNames = new Array();
RTStopDesc = new Array();
RTStopsCount = 0;
RTStopX = new Array();
RTStopY = new Array();
RTStopCiv =new Array();

RTStopIdMarker = new Array();
RTStopIdPoi = new Array();
RTStopTematismoPoi = new Array();
RTStopTypePoi = new Array();
RTStopTitlePoi = new Array();
//end MC


//barriere
 RTBarNames = new Array();
 RTBarDesc = new Array();
 RTBarCount = 0;
 RTBarX = new Array();
 RTBarY = new Array();
 RTBarCiv =new Array();
 RTType = "quickest"; // or "shortest"
 RTLang = 0;
 RTHwyPref = 0;
 RTPrecision = 0.0;
 RTStopsChanged = false;
 RTBarsChanged = false;
 RTReply    = "";
 boolOrder = false;
}

function open_diagramma(coord,name_form){
	if (name_form) 
		 form_obj=document.getElementById("diagramma2");
	else
	   form_obj=document.getElementById("diagramma");
	form_obj.coord_shape.value=coord;
	doSubmit(form_obj);
}
function doSubmit(form){
	
	window.open('diagramma_owt.asp', 'grafico', 'scrollbars=0,menubar=0,toolbar=0,location=0,status=0,width=600,height=480,resizable=0');
//  window.open('http://moon.webgis.planetek.it/pkt219/diagramma_owt.asp', 'grafico', 'scrollbars=0,menubar=0,toolbar=0,location=0,status=0,width=600,height=480,resizable=0');
 
  form.submit()
  return true;

}

function mostra_opzioni(){
	
	obj_route=document.getElementById("menu_route").contentWindow

	if (obj_route.document.getElementById("avanzate").style.display=="none"){
		if(((map.getSize()).h-10)<550){
			altez=map.getSize().h-10
		}else altez=510
			document.getElementById("MenuRouting2").style.height=altez
			document.getElementById("row_route").style.height=altez
	  if (navigator.userAgent.indexOf("MSIE") >= 0) 
	  	obj_route.document.getElementById("avanzate").style.display="";
	  else
	   obj_route.document.getElementById("avanzate").style.display="table-row";
	   
	   obj_route.document.getElementById("f_avanzate").src="../PK_layout/img/FrecciaGiuTrasp.gif"
	}else{
			document.getElementById("MenuRouting2").style.height=200
			document.getElementById("row_route").style.height=200
		  obj_route.document.getElementById("avanzate").style.display="none"
		  obj_route.document.getElementById("f_avanzate").src="../PK_layout/img/FrecciaSuTrasp.gif"
		}
}
