/***************/
//var dim_flag e flags_num vengono definite in PrintJS.php!!!
/******/

function min(a,b){
    if (a<b){
	return a; 
    } else {
	return b;
    }
	return true;
}

function max(a,b){
    if (a>b){
	return a;
    } else {
	return b;
    }
	return true;
}

var left, right, top, bottom;
var xmouse, ymouse, xfrom, yfrom, xto, yto, xabs, yabs, x, y, xfromabs, yfromabs;
var xmin, ymin, xmax, ymax, mapwidth, mapheight, i,xflag,yflag;

// netscape only object variables
var maplayerobj, mapimageobj, boxleftobj, boxrightobj, boxtopobj, boxbottomobj;

var flagid,flagX,flagY,multipath,modType;

var browser, type, thispoi;

var ShowRect=false;
browser="netscape4";

function checkBrowser(){
	
	if ( ("browser"+navigator.appVersion.toString()).search('4.')>0 && ("browser"+navigator.appName.toString()).search('Netscape')>0){
		browser="netscape4";
		return;} 
	if ((("browser"+navigator.appVersion.toString()).search('MSIE 6.0'))>0){
		//browser="explorer6";
		browser="explorer6";
		return;}
	if ((("browser"+navigator.appVersion.toString()).search('MSIE 5.'))>0){	
		browser="explorer5";
		return;}
	if ((("browser"+navigator.appName.toString()).search('Netscape'))>0){	
		browser="netscape6";
		return;}	
}


function initUbiJs(){
    checkBrowser();
    modType=1;
    flagid=1;
    flagX=new Array();//ascisse Multipath
    flagY=new Array();//ordinate Multipath
    mapwidth=Number(document.global_form.mapwidth.value);
    mapheight=Number(document.global_form.mapheight.value);
    xfrom=-1;
    yfrom=-1;
    xto=-1;
    yto=-1;
    
    x=-1;
    y=-1;
 
    type="move";
    // set handlers
    switch (browser){
	case "explorer6" :
		maplayerIE.style.position="relative";
		document.onmousedown=mousedown;
      		document.onmousemove=mousemove;
      		document.onmouseup=mouseup;
      		
      		maplayerobj=document.getElementById('maplayerIE');
      		boxright.style.zIndex=1000;
      		boxleft.style.zIndex=1000;
      		boxtop.style.zIndex=1000;
      		boxbottom.style.zIndex=1000;
	break;
	case "explorer5" :
		maplayerIE.style.position="relative";
		document.onmousedown=mousedown;
      		document.onmousemove=mousemove;
      		document.onmouseup=mouseup;
      		
      		maplayerobj=document.getElementById('maplayerIE');
      		boxright.style.zIndex=1000;
      		boxleft.style.zIndex=1000;
      		boxtop.style.zIndex=1000;
      		boxbottom.style.zIndex=1000;
	break;
	case "netscape4" :
		document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);
      		document.onmousedown=mousedown;
      		document.onmousemove=mousemove;
      		document.onmouseup=mouseup;
      		
      		maplayerobj=document.layers['contentlayer'];
      		boxleftobj=document.layers['boxleft'];
      		boxrightobj=document.layers['boxright'];
      		boxtopobj=document.layers['boxtop'];
      		boxbottomobj=document.layers['boxbottom'];
      		
      		boxrightobj.zIndex=100;
      		boxleftobj.zIndex=100;
      		boxtopobj.zIndex=100;
      		boxbottomobj.zIndex=100;
      		
	break;
	
	case "netscape6" :
		document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);
      		document.onmousedown=mousedown;
      		document.onmousemove=mousemove;
      		document.onmouseup=mouseup;
      		
      		
      		maplayerobj=document.getElementById('maplayerIE');
      		boxleftobj=document.getElementById('boxleft');
      		boxrightobj=document.getElementById('boxright');
      		boxtopobj=document.getElementById('boxtop');
      		boxbottomobj=document.getElementById('boxbottom');

      		boxrightobj.style.zIndex=1000;
      		boxleftobj.style.zIndex=1000;
      		boxtopobj.style.zIndex=1000;
      		boxbottomobj.style.zIndex=1000;
	break;

	}
}//END init()


function mousemove(evt){
	switch  (browser){
		case "explorer5" :
			if(document.global_form.mode.value=="Multipath"){
			maplayerobj.style.cursor="crosshair";
			ShowRect=false;
			}
			else {  maplayerobj.style.cursor="default";
				if(document.global_form.Xmapstatus.value!="") {
					ShowRect=true;}
			}
		break;
		case "explorer6" :
			if(document.global_form.mode.value=="Multipath"){
			maplayerobj.style.cursor="crosshair";
			ShowRect=false;
			}
			else {
				maplayerobj.style.cursor="default";
				if(document.global_form.Xmapstatus.value!="") 
					ShowRect=true;
			}
		break;
		case "netscape4" :
			if(document.global_form.mode.value=="Multipath"){
			ShowRect=false;
			}else {
				if(document.global_form.Xmapstatus.value!="") {
					ShowRect=true;}
			}
		break; 
		case "netscape6" :
			if(document.global_form.mode.value=="Multipath"){
			ShowRect=false;
			}else {
				if(document.global_form.Xmapstatus.value!="") {
					ShowRect=true;}
			}
		break;        
	}       

	/**********************/
    // ShowRect is activated via PHP
  	if (ShowRect){
  		switch (browser){
			case "explorer5" :
				xabs = window.event.clientX + document.body.scrollLeft;
				yabs = window.event.clientY + document.body.scrollTop;
				x = xabs - maplayerobj.offsetLeft;//il nuovo layer di riferimento è il contentlayer
				y = yabs - maplayerobj.offsetTop;
			break;
			case "explorer6" :
				xabs = window.event.clientX + document.body.scrollLeft;
				yabs = window.event.clientY + document.body.scrollTop;
				y = yabs - maplayerobj.offsetTop; 
				x = xabs - maplayerobj.offsetLeft;
				
			break;
			case "netscape4" :
				xabs = evt.pageX;
				yabs = evt.pageY;
				x = xabs - maplayerobj.pageX;
				y = yabs - maplayerobj.pageY;
			break;
			case "netscape6" :
			       
				xabs = evt.pageX;
				yabs = evt.pageY;
				x = xabs - maplayerobj.offsetLeft;
				y = yabs - maplayerobj.offsetTop;
			break;		
			
			
		}
    
    
  		if (type=="drag"){
			// if we are dragging the rectangle
			// keep second point inside boundaries
			if (x>mapwidth){ 
			    xabs-=(x-mapwidth);
			    x=mapwidth; 
			}
			if (x<0){
			    xabs-=x;
			    x=1; 
			}
			if (y>mapheight) {
			    yabs-=(y-mapheight+1);
			    y=mapheight-1; 
			}
			if (y<0){ 
			    yabs-=y;
			    y=1; 
			}
			
  		}

    		if (x > mapwidth) x = -1;
    		if (y > mapheight) y = -1;


    		// check if we are over a poi
               
    		check_poi();
   

   
                if ((type=="drag")&&(x>0)&&(y>0)){
                //if we are dragging the rectangle
                        switch (browser){
	          		case "explorer5" :
	          		boxright.style.left=xabs;
	          		boxbottom.style.top=yabs;
	          		
	          		// find the rectangle coords
	          		left=min(xfromabs, xabs);
	          		right=max(xfromabs, xabs);
	          		top=min(yfromabs, yabs);
	          		bottom=max(yfromabs, yabs);
	          		
	          		if((left!=right)&&(top!=bottom)){//max 16/01/2002
		          		boxright.style.visibility="visible";
		          		boxbottom.style.visibility="visible";
		          		boxtop.style.visibility="visible";
		          		boxleft.style.visibility="visible";
	          		}else{
		          		boxright.style.visibility="hidden";
		          		boxbottom.style.visibility="hidden";
		          		boxtop.style.visibility="hidden";
		          		boxleft.style.visibility="hidden";	
	          		}
        			
        			
	          		
	          		boxleft.style.top=top;
	          		boxright.style.top=top;
	          		boxleft.style.height=(bottom-top);
	          		boxright.style.height=(bottom-top);
	          		
	          		boxtop.style.left=left;
	          		boxbottom.style.left=left;
	          		boxtop.style.width=(right-left);
	          		boxbottom.style.width=(right-left);
	          	break;
	          	case "explorer6" :
	          		boxright.style.left=xabs;
	          		boxbottom.style.top=yabs;
	          		
	          		// find the rectangle coords
	          		left=min(xfromabs, xabs);
	          		right=max(xfromabs, xabs);
	          		top=min(yfromabs, yabs);
	          		bottom=max(yfromabs, yabs);
	          		
	          		if((left!=right)&&(top!=bottom)){
		          		boxright.style.visibility="visible";
		          		boxbottom.style.visibility="visible";
		          		boxtop.style.visibility="visible";
		          		boxleft.style.visibility="visible";
	          		}else{
		          		boxright.style.visibility="hidden";
		          		boxbottom.style.visibility="hidden";
		          		boxtop.style.visibility="hidden";
		          		boxleft.style.visibility="hidden";	
	          		}
        		
	          		boxleft.style.top=top;
	          		boxright.style.top=top;
	          		boxleft.style.height=(bottom-top);
	          		boxright.style.height=(bottom-top);
	          		
	          		boxtop.style.left=left;
	          		boxbottom.style.left=left;
	          		boxtop.style.width=(right-left);
	          		boxbottom.style.width=(right-left);
	          	break;
	          	case "netscape4" ://ns4 ns6
	          		// find the rectangle coords
	          		left=min(xfromabs, xabs);
	          		right=max(xfromabs, xabs);
	          		top=min(yfromabs, yabs);
	          		bottom=max(yfromabs, yabs);
	          		
	          		if((left!=right)&&(top!=bottom)){
	          			// show it!
		          		boxrightobj.visibility="show";
		          		boxbottomobj.visibility="show";
		          		boxleftobj.visibility="show";
		          		boxtopobj.visibility="show";
	          		}else{
		          		boxrightobj.visibility="hidden";
		          		boxbottomobj.visibility="hidden";
		          		boxleftobj.visibility="hidden";
		          		boxtopobj.visibility="hidden";
	          		}
        		
	          		
	          		// MOVE AND CLIP THE LINES
	          		boxtopobj.left=left;
	          		boxtopobj.top=top;
	          		boxtopobj.clip.right=(right-left);
        		
	          		boxbottomobj.left=left;
	          		boxbottomobj.top=bottom;
	          		boxbottomobj.clip.right=(right-left);
        		
	          		boxleftobj.left=left;
	          		boxleftobj.top=top;
	          		boxleftobj.clip.bottom=(bottom-top); // bottom>top 
        		
	          		boxrightobj.left=right;
	          		boxrightobj.top=top;
	          		boxrightobj.clip.bottom=(bottom-top); // bottom>top 
        		
	          	break;
	          	case "netscape6" ://ns4 ns6
	          		// find the rectangle coords
	          		left=min(xfromabs, xabs);
	          		right=max(xfromabs, xabs);
	          		top=min(yfromabs, yabs);
	          		bottom=max(yfromabs, yabs);
	          		
	          		if((left!=right)&&(top!=bottom)){
	          			// show it!
		          		boxrightobj.style.visibility="visible";
		          		boxbottomobj.style.visibility="visible";
		          		boxleftobj.style.visibility="visible";
		          		boxtopobj.style.visibility="visible";
	          		}else{
		          		boxrightobj.style.visibility="hidden";
		          		boxbottomobj.style.visibility="hidden";
		          		boxleftobj.style.visibility="hidden";
		          		boxtopobj.style.visibility="hidden";
	          		}
	          		// MOVE AND CLIP THE LINES
	          		////alert(boxrightobj.style.zIndex);
	          		boxtopobj.style.left=left;
	          		boxtopobj.style.top=top;
	          		boxtopobj.style.width=(right-left);
        		
	          		boxbottomobj.style.left=left;
	          		boxbottomobj.style.top=bottom;
	          		boxbottomobj.style.width=(right-left);
        		
	          		boxleftobj.style.left=left;
	          		boxleftobj.style.top=top;
	          		boxleftobj.style.height=(bottom-top); // bottom>top 
        		
	          		boxrightobj.style.left=right;
	          		boxrightobj.style.top=top;
	          		boxrightobj.style.height=(bottom-top); // bottom>top 
	          		
	          	break;
	                }
	        }
        }
        return false;
}

function mostraElencoPOI() {
if(poilist.style.display=="none"||poilist.style.display==""){poilist.style.display="block";}
else{poilist.style.display="none";}
}

function mostraElencoPathInfo() {
if(pathinfolist.style.display=="none"||pathinfolist.style.display==""){pathinfolist.style.display="block";}
else{pathinfolist.style.display="none";}
}

function mostraCalcoloPercorsoDa() {
if(calcolopercorsoda.style.display=="none"||calcolopercorsoda.style.display==""){calcolopercorsoda.style.display="block";}
else{calcolopercorsoda.style.display="none";}
}

function mostraCalcoloPercorsoFinoA() {
if(calcolopercorsofinoa.style.display=="none"||calcolopercorsofinoa.style.display==""){calcolopercorsofinoa.style.display="block";}
else{calcolopercorsofinoa.style.display="none";}
}

function mostraCalcoloPercorsoClick() {
if(calcolopercorsoclick.style.display=="none"||calcolopercorsoclick.style.display==""){
calcolopercorsoclick.style.display="block";
setMode("Multipath");
}
else{
calcolopercorsoclick.style.display="none";
setMode("Normal");
}
}

function mousedown(evt){
  if (ShowRect){//showrect
    if ((x>0)&&(x<mapwidth)&&(y>0)&&(y<mapheight)){
	// good point
	xfrom=x;
	yfrom=y;
	type="drag";
	// this is to draw the rectangle
	switch (browser){
		case "explorer5" :
			// show it!
	    		xfromabs=xabs;
	    		yfromabs=yabs;
	    		boxleft.style.left=xabs;
	    		boxtop.style.top=yabs;
		break;
		case "explorer6" :
			// show it!
	    		xfromabs=xabs;
	    		yfromabs=yabs;
	    		boxleft.style.left=xabs;
	    		boxtop.style.top=yabs;
		break;
		case "netscape4":
	                xfromabs=xabs;
	                yfromabs=yabs;
	                boxleftobj.left=xabs;
	                boxtopobj.top=yabs;
		break;
		case "netscape6":
	                xfromabs=xabs;
	                yfromabs=yabs;
	                boxleftobj.style.left=xabs;	                
	                boxtopobj.style.top=yabs;                
		break;
	}
	
    } else {
	type="move";
	xfrom=-1;
	yfrom=-1;
    }
  }
  
  //MULTIPATH
  switch(browser){
		case "explorer5" :
			xflag=window.event.clientX+document.body.scrollLeft;
		  yflag=window.event.clientY+document.body.scrollTop;
			if((document.global_form.mode.value=="Multipath")
				&&((xflag>maplayerobj.offsetLeft)&&(xflag<maplayerobj.offsetLeft+mapwidth))
				&&((yflag>maplayerobj.offsetTop)&&(yflag<maplayerobj.offsetTop+mapheight))){
				if(flagid<=flags_num){
				document.getElementById("flag"+flagid).style.top=yflag-dim_flag/2;
				document.getElementById("flag"+flagid).style.left=xflag-dim_flag/2;
				document.getElementById("flag"+flagid).style.visibility="visible";
				document.getElementById("flag"+flagid).style.zIndex=100;
				flagX[flagX.length]=xflag-maplayerobj.offsetLeft;
				flagY[flagY.length]=yflag-maplayerobj.offsetTop;
				flagid++;
				document.global_form.Xmultipath.value=flagX.toString();
				document.global_form.Ymultipath.value=flagY.toString();
				}
				else{
				  alert("Raggiunto il numero massimo di tappe posizionate.");
  			        }
			}

		break;
		case "explorer6" :
		        xflag=window.event.clientX+document.body.scrollLeft;
		        yflag=window.event.clientY+document.body.scrollTop;
			if((document.global_form.mode.value=="Multipath")
				&&((xflag>maplayerobj.offsetLeft)&&(xflag<maplayerobj.offsetLeft+mapwidth))
				&&((yflag>maplayerobj.offsetTop)&&(yflag<maplayerobj.offsetTop+mapheight))){
				if(flagid<=flags_num){
				document.getElementById("flag"+flagid).style.top=yflag-dim_flag/2;
				document.getElementById("flag"+flagid).style.left=xflag-dim_flag/2;
				document.getElementById("flag"+flagid).style.visibility="visible";
				document.getElementById("flag"+flagid).style.zIndex=100;
				flagX[flagX.length]=xflag-maplayerobj.offsetLeft;
				flagY[flagY.length]=yflag-maplayerobj.offsetTop;
				flagid++;
				document.global_form.Xmultipath.value=flagX.toString();
				document.global_form.Ymultipath.value=flagY.toString();
				}
				else{
				  alert("Raggiunto il numero massimo di tappe posizionate.");
  			        }
			}
		break;
		case "netscape4" :
			xflag=evt.pageX;
			yflag=evt.pageY;
		        if((document.global_form.mode.value=="Multipath")
			&&((xflag>maplayerobj.pageX)&&(xflag<maplayerobj.pageX+mapwidth))
			&&((yflag>maplayerobj.pageY)&&(yflag<maplayerobj.pageY+mapheight))){
			if(flagid<=flags_num){
			document.layers["flag"+flagid].pageY=yflag-dim_flag/2;
			document.layers["flag"+flagid].pageX=xflag-dim_flag/2;
			document.layers["flag"+flagid].visibility="show";
			document.layers["flag"+flagid].zIndex=100;
			flagX.push(xflag-maplayerobj.pageX);
			flagY.push(yflag-maplayerobj.pageY);
			flagid++;
			document.global_form.Xmultipath.value=flagX.toString();
			document.global_form.Ymultipath.value=flagY.toString();
			}
			else{
			   alert("Raggiunto il numero massimo di tappe posizionate.");
         }
			}
		break;
		case "netscape6":
			xflag=evt.pageX;
			yflag=evt.pageY;
			if((document.global_form.mode.value=="Multipath")
				&&((xflag>maplayerobj.offsetLeft)&&(xflag<maplayerobj.offsetLeft+mapwidth))
				&&((yflag>maplayerobj.offsetTop)&&(yflag<maplayerobj.offsetTop+mapheight))){
				if(flagid<=flags_num){
				document.getElementById("flag"+flagid).style.top=yflag-dim_flag/2;
				document.getElementById("flag"+flagid).style.left=xflag-dim_flag/2;
				document.getElementById("flag"+flagid).style.visibility="visible";
				document.getElementById("flag"+flagid).style.zIndex=100;
				flagX[flagX.length]=xflag-maplayerobj.offsetLeft;
				flagY[flagY.length]=yflag-maplayerobj.offsetTop;
				flagid++;
				document.global_form.Xmultipath.value=flagX.toString();
				document.global_form.Ymultipath.value=flagY.toString();
				}
				else{
				  alert("Raggiunto il numero massimo di tappe posizionate.");
  			        }
			}
		break;
  }
}

function mouseup(evt){
    // hide rectangle
    if (ShowRect){
    	switch (browser){
 		case "netscape4"://ns4 
	    		boxrightobj.visibility="hidden";
	    		boxbottomobj.visibility="hidden";
	    		boxleftobj.visibility="hidden";
	    		boxtopobj.visibility="hidden";
	    	break;
	    	
	    	case "netscape6"://ns6
	    		boxrightobj.style.visibility="hidden";
	    		boxbottomobj.style.visibility="hidden";
	    		boxleftobj.style.visibility="hidden";
	    		boxtopobj.style.visibility="hidden";
	    	break;
	    	default:
	    		boxright.style.visibility="hidden"; 
			boxbottom.style.visibility="hidden";
			boxtop.style.visibility="hidden";   
			boxleft.style.visibility="hidden";
	    	break;
			           
	}

	if ((x>0)&&(x<mapwidth)&&(y>0)&&(y<mapheight)&&
	    (xfrom!=-1)&&(yfrom!=-1)){
	    // good point
	    type="move";
	    
	    if (thispoi!=-1){
		// we clicked on a poi
		MakeWindow(PoiURL[thispoi]);
	    } else {
		// we haven't clicked on a poi
		// go zoom or recenter
		xto=x;
		yto=y;
		
		// swap coords to have from<to
		if (xfrom>xto){
		    tmp=xfrom;
		    xfrom=xto;
		    xto=tmp;
		}
		if (yfrom>yto){
		    tmp=yfrom;
		    yfrom=yto;
		    yto=tmp;
		}
		
		document.global_form.Xxfrom.value=xfrom;
		document.global_form.Xyfrom.value=yfrom;
		document.global_form.Xxto.value=xto;
		document.global_form.Xyto.value=yto;
		
		print_global_form();
		
		setAction(21);	
		
	    }
	} else {
	    // bad point
	    
	    xfrom=-1;
	    yfrom=-1;
	    xto=-1;
	    yto=-1;
	}
    }
}



////////////////////////////////////////////////////////////////////////////////
// POI
////////////////////////////////////////////////////////////////////////////////

function check_poi(){
if(ShowRect){
if(!(document.global_form.mode.value=="Multipath")){//multipath
  thispoi=-1;
  if ((type=="move")&&(x>0)&&(y>0)){
    for (var i=PoiX.length; i>=0; i--){	
      if (thispoi==-1){
				// if we haven't found a poi yet
				if ((Math.abs(x-PoiX[i])<PoiDist) && (Math.abs(y-PoiY[i])<PoiDist)){
	  			// we're inside the square
				  thispoi=i;
				  
				  if (thispoi!=PoiOld){
				  	switch (browser){
							case "explorer5" :
								mylayer=eval("poi"+thispoi);
		      			mylayer.style.left=xabs;
		      			mylayer.style.top=yabs;
								break;
							case "explorer6" :
								mylayer=eval("poi"+thispoi);
		      			mylayer.style.left=xabs;
		      			mylayer.style.top=yabs;
								break;
							case "netscape4" :
								document.layers["poi"+thispoi].left=xabs;
					      document.layers["poi"+thispoi].top=yabs;
								break;
							case "netscape6" :
								document.getElementById("poi"+thispoi).style.left=xabs;
					      document.getElementById("poi"+thispoi).style.top=yabs;
								break;
						}
				    // it's a new POI
				    // move it
				  }
				}
      }
    }
  }
  
  if (thispoi!=PoiOld){
    // if something changed
    if (thispoi!=-1){
      switch(browser){
      	case "netscape4":
      		document.layers["poi"+thispoi].visibility="show";
		document.layers["poi"+thispoi].zIndex=100;
      	break;
      	case "netscape6":
      		document.getElementById("poi"+thispoi).style.visibility="visible";
		document.getElementById("poi"+thispoi).style.zIndex=1000;
      	break;
      	default:{
      		mylayer=eval("poi"+thispoi);
		mylayer.style.visibility="visible";
		mylayer.style.zIndex=100;
		maplayerobj.style.cursor='help';
		mylayer.style.cursor='help';
      		}
      	}
    }

    if (PoiOld!=-1){
      switch(browser){
      	case "netscape4":
      		document.layers["poi"+PoiOld].visibility="hidden";
      	break;
      	case "netscape6":
      	       document.getElementById("poi"+PoiOld).style.visibility="hidden";
      	break;
      	default:{
      		mylayer=eval("poi"+PoiOld);
		mylayer.style.visibility="hidden";
		maplayerobj.style.cursor='default';
		mylayer.style.cursor='default';
      		}
      	}
    }
    
    PoiOld=thispoi;
  }
   return true;
 } 
}
}

//multipath
function ClearFlags(){
	switch (browser){
		case "netscape4" :
			for(i=1;i<=flags_num;i++){
		          document.layers["flag"+i].pageY=0;
			  document.layers["flag"+i].pageX=0;
			  document.layers["flag"+i].visibility="hidden";
			  document.layers["flag"+i].zIndex=0;
		          flagX.splice(0,flags_num-1);//svuoto l'array delle coordinate
		          flagY.splice(0,flags_num-1);//svuoto l'array delle coordinate
	                }
	                flagid=1;
	        break;
	        default:
	        	for(i=1;i<=flags_num;i++){
		          document.getElementById("flag"+i).style.top=0;
		          document.getElementById("flag"+i).style.left=0;
		          document.getElementById("flag"+i).style.visibility="hidden";
		          document.getElementById("flag"+i).style.zIndex=0;
		          flagX=new Array();
		          flagY=new Array();
		          document.global_form.Xmultipath.value='';
		          document.global_form.Ymultipath.value='';
	                }
	                flagid=1;
	        break;
	}
}
//end multipath


function setMode(){
	var mode,id;
	if(document.global_form.Xmapstatus!=''){
		mode=["Normal","Multipath"];
		id=modType % mode.length;
		document.global_form.mode.value=mode[id];
		if(document.global_form.mode.value!='Multipath')
				ClearFlags();
		modType++;
	}
	return document.global_form.mode.value;
}

function setMode(modType){//modType assume i valori Normal e Multipath
	if(document.global_form.Xmapstatus!=''){
		if(document.global_form.mode.value=='Multipath')
				ClearFlags();
		document.global_form.mode.value=modType;
	}
}


function setZoom(scale,zoom_factor){//
  var CurScale;
  CurScale=Math.round(scale*zoom_factor);
	document.global_form.Xscale.value=CurScale;
	document.global_form.Xmovetype.value='CZ';
	document.global_form.Xaction.value=20;
		print_global_form();
document.global_form.submit();
	return true;
}


function setAction(action){
	var submit = true;
	if(action==4){//delete path
		document.global_form.XPathId.value='';
		document.global_form.XXstop.value='';
		document.global_form.XYstop.value='';
		document.global_form.Xstopdesc.value='';
		document.global_form.XXmlPath.value='';
		document.global_form.XPntInf.value='';
		action=20;
	}
	
	if(action==3 && document.global_form.mode.value=='Multipath') {
		action=24;
	}
	
	if (action == 'mappa_indirizzo' 
			|| action == 'mappa_percorso'
			|| action == 'aggiungi_tappa'
			|| action == 'mappa_coordinate_indirizzo_to') {
		if (!document.global_form.Xcityfrom
					|| document.global_form.Xcityfrom.value == '') {
			alert("Il campo \'Città\' è obbligatorio.");
			submit = false;
		}
	}
	
	if (action == 'mappa_percorso' 
			|| action == 'mappa_coordinate_indirizzo_from') {
		if (!document.global_form.Xcityto
					|| document.global_form.Xcityto.value == '') {
			alert("Il campo \'Città\' è obbligatorio.");
			submit = false;
		}
	}
	
	if (action == 'mappa_multitappa') {
		if (!document.global_form.listaTappe
					|| !document.global_form.listaTappe.options
					|| document.global_form.listaTappe.options.length < 2) {
			alert("La lista tappe deve contenere almeno due elementi.");
			submit = false;
		}
	}
	
	if (action == 'mappa_coordinate_click') {
		if (!document.global_form.Xmultipath 
					|| document.global_form.Xmultipath.value == ''
					|| document.global_form.Xmultipath.value.indexOf(',') == -1) {
			alert("E' neccessario selezionare almeno due tappe.");
			submit = false;
		}
	}
	
	if (submit) {
		document.global_form.Xaction.value=action;
		
		print_global_form();
		
		document.global_form.submit();
		
		return true;
	}
} 
  
function setMoveType(mtype){
	document.global_form.Xmovetype.value=mtype;
	document.global_form.Xaction.value=20;
		print_global_form();
document.global_form.submit();
	return true;
}  
   
function setAddress(Xcoord,Ycoord){
	document.global_form.XmapCX.value=Xcoord;
	document.global_form.XmapCY.value=Ycoord;
	document.global_form.Xaction.value=20;
		print_global_form();
document.global_form.submit();
	return true;
	}
  
//aggiunta tappa: viene fatta da PHP
function setStop(Xcoord,Ycoord,Xnewstopdesc){
	document.global_form.XXnew.value=Xcoord;
	document.global_form.XYnew.value=Ycoord;
	document.global_form.Xnewstopdesc.value=Xnewstopdesc;
	document.global_form.Xaction.value=2;//aggiunta tappa
		print_global_form();
document.global_form.submit();
	return true;
	}
function setStart(Xcoord,Ycoord,Xcity,Xaddr,Xnum,Xnewstopdesc){
	document.global_form.XXstop.value=Xcoord;
	document.global_form.XYstop.value=Ycoord;
	document.global_form.Xcityto.value=Xcity;
	document.global_form.Xaddressto.value=Xaddr;
	document.global_form.Xnumberto.value=Xnum;
	document.global_form.Xnewstopdesc.value=Xnewstopdesc;
	document.global_form.Xaction.value=2;//aggiunta tappa
		print_global_form();
document.global_form.submit();
	return true;
	}
//invocata quando AddressList restituisce un elenco di più destinazioni a seguito di richiesta percorso tra un punto ed un indirizzo
//viene sostituita l'ultima tappa agiunta (destinazione del percorso) con quella prescelta
function ChangeDestination(Xcoord,Ycoord,Xnewstopdesc){
	 arrXXstop=document.global_form.XXstop.value.split(",");
	 arrXYstop=document.global_form.XYstop.value.split(",");
	 arrXstopdesc=document.global_form.Xstopdesc.value.split(",");
	 arrXXstop[arrXXstop.length-1]=Xcoord;//sostiuisco le coordinate dell'ultima tappa
	 arrXYstop[arrXYstop.length-1]=Ycoord;
	 arrXstopdesc[arrXstopdesc.length-1]=Xnewstopdesc;
	 document.global_form.XXstop.value=arrXXstop;
	 document.global_form.XYstop.value=arrXYstop;
	 document.global_form.Xstopdesc.value=arrXstopdesc;
	 setAction(3);//richiedo il percorso sulle nuove coordinate
	}

function goToDetail(x1,y1,x2,y2){//dettaglio del percorso testuale
	document.global_form.XmapNWX.value=x1;
	document.global_form.XmapNWY.value=y1;
	document.global_form.XmapSEX.value=x2;
	document.global_form.XmapSEY.value=y2;
	document.global_form.Xaction.value=23;
		print_global_form();
document.global_form.submit();
	return true;
}

/*********Scripts per gestione tappe*******/
function ChangeStopCoord(i,j){
	 var xtmp,ytmp,desctmp;
	 arrXXstop=document.global_form.XXstop.value.split(",");
	 arrXYstop=document.global_form.XYstop.value.split(",");
	 arrXstopdesc=document.global_form.Xstopdesc.value.split(",");
	 //tmp values
	 xtmp=arrXXstop[i];
	 ytmp=arrXYstop[i];
	 desctmp=arrXstopdesc[i];
	 //change i to j
	 arrXXstop[i]=arrXXstop[j];
	 arrXYstop[i]=arrXYstop[j];
	 arrXstopdesc[i]=arrXstopdesc[j];
	 //change j to tmp
	 arrXXstop[j]=xtmp;
	 arrXYstop[j]=ytmp;
	 arrXstopdesc[j]=desctmp;
	 
	 document.global_form.XXstop.value=arrXXstop;
	 document.global_form.XYstop.value=arrXYstop;
	 document.global_form.Xstopdesc.value=arrXstopdesc;
}

function moveStop(Tmove,obj){
	var texttmp;
	if(obj.selectedIndex>0 && Tmove=='up'){//solo se è selezionato l'indice e se l'indice non è zero
		texttmp=obj.options[obj.selectedIndex].text;
		obj.options[obj.selectedIndex].text=obj.options[obj.selectedIndex-1].text;
		obj.options[obj.selectedIndex-1].text=texttmp;
		ChangeStopCoord(obj.selectedIndex-1,obj.selectedIndex);
		obj.options[obj.selectedIndex-1].selected=true;
		
	}
	else{
		if(obj.selectedIndex>-1 
			&& obj.selectedIndex < obj.length-1 
			&& Tmove=='down'){//solo se è selezionato l'indice e se l'indice è compreso tra 0 e la lunghezza meno uno...
			texttmp=obj.options[obj.selectedIndex].text;
			obj.options[obj.selectedIndex].text=obj.options[obj.selectedIndex+1].text;
			obj.options[obj.selectedIndex+1].text=texttmp;
			ChangeStopCoord(obj.selectedIndex,obj.selectedIndex+1);
			obj.options[obj.selectedIndex+1].selected=true;
			
		}
	}
}
function delStop(obj){
	arrXXstop=document.global_form.XXstop.value.split(",");
	arrXYstop=document.global_form.XYstop.value.split(",");
	arrXstopdesc=document.global_form.Xstopdesc.value.split(",");
	if(obj.selectedIndex!=-1){
	    for(i=obj.selectedIndex;i<obj.length-1;i++){
			obj.options[i].text=obj.options[i+1].text;
			arrXXstop[i]=arrXXstop[i+1];
			arrXYstop[i]=arrXYstop[i+1];
			arrXstopdesc[i]=arrXstopdesc[i+1];
		}
		arrXXstop.length=arrXXstop.length-1;
		arrXYstop.length=arrXYstop.length-1;
		arrXstopdesc.length=arrXstopdesc.length-1;
		document.global_form.XXstop.value=arrXXstop;
		document.global_form.XYstop.value=arrXYstop;
		document.global_form.Xstopdesc.value=arrXstopdesc;
		obj.length=obj.length-1;
		
		print_global_form();
document.global_form.submit();
	}
}


function MakeWindow(PageUrl)
{
var popupWindow = open( PageUrl, "Picture", "width=600,height=300,menubar=no,resizable=yes,scrollbars=yes,status=no" );
  popupWindow.focus();

}


function print_global_form() {}

/************End Script gestione tappe************/

function ingrandisciMappa(){
	document.global_form.maptype.value = 'big';
	document.global_form.mapwidth.value = 726;
	document.global_form.mapheight.value = 521;
	document.global_form.Xmovetype.value='CZ';
	document.global_form.Xaction.value=20;
	print_global_form();
document.global_form.submit();
}

function rimpicciolisciMappa(){
	
	document.global_form.maptype.value = 'small';
	document.global_form.mapwidth.value = 536;
	document.global_form.mapheight.value = 352;
	document.global_form.Xmovetype.value='CZ';
	document.global_form.Xaction.value=20;
	print_global_form();
document.global_form.submit();
}

function stampaMappa(){
	document.global_form.mapwidth.value = 640;
	document.global_form.mapheight.value = 500;
	document.global_form.Xmovetype.value='CZ';
	document.global_form.Xaction.value=20;
	document.global_form.action='/servizi/mappe/stampa.jhtml';
	print_global_form();
	document.global_form.submit();
}


function setCoordinateDestinazione(xTo, yTo){
	
	document.global_form.Xxto.value = xTo;
	document.global_form.Xyto.value = yTo;
	
	print_global_form();
	
	setAction('mappa_centro_coordinate');
}
