/*
File per gli script di Giro Milano
*/


var gmtools_url = "http://webpartsv01/gmtools/xmlbwpoints.asp?fname=";
var stop_url = "http://webpartsv01/_layouts/images/GiroMilano/bw/ferm.gif";
var stop_mm_url = "http://webpartsv01/_layouts/images/GiroMilano/bw/mm.jpg";
var start_url = "http://webpartsv01/_layouts/images/GiroMilano/bw/start.gif";
var end_url = "http://webpartsv01/_layouts/images/GiroMilano/bw/end.gif";
var fa_url = "http://webpartsv01/_layouts/images/GiroMilano/bw/fa.gif";
var g_poixml_url = "http://webpartsv01/gmtools/xmlpoi.asp"; 
//var g_poibase_icons_url = "http://webpartsv01/_LAYOUTS/ATM/images/Giromilano/";
var g_poibase_icons_url = "/_LAYOUTS/ATM/images/Giromilano/";
var g_poi_info_xml_url = "http://webpartsv01/gmtools/xmlpoiinfo.asp";
var g_stop_info_xml_url = "http://webpartsv01/gmtools/xmlstop.asp";
var g_stop_lv_info_url = "http://webpartsv01/gmtools/xmllvstop.asp";

var g_add_s_id = "";
var g_add_e_id = "";
var g_pl_s_id = "";
var g_pl_e_id = "";

var g_bwcalc_id = "";

var rdlt_lv_id = "";

var aQueryString = null;

var gplat = "";
var gplon = "";
//var gbwxmlurl = "";
//var gfaparam = "";

var gmap = null;

var num_bw_polylines = 0;


var bw_polylines_array_marker = null;
var bw_icons_marker_array = null;
var bw_listeners_array = null;


var lv_polylines_array_marker = null;
var lv_icons_marker_array = null;
var lv_listeners_array = null;
var lv_markers_id = null;

var fa_marker = null;
var xmlHttp = null;
var xmlreceived = null;
var xmlreceived_1 = null;

poi_active_category = new Array();
poi_db_category_index = new Array();

poi_category_markers = new Array();
poi_category_listeners = new Array();


var g_poi_x_size = 20;
var g_poi_y_size = 20;
var g_bw_points_step = 15;
var g_amb_id = "";
var g_start_click_setted = false;
var g_end_click_setted = false;
var g_lng = 0;
var g_nl_submt_cl = 0;

var g_canshowroute_id = null;
var g_v_pos_fa_hf_id = null;
var g_v_pos_bw_hf_id = null;
var g_v_pos_nl_hf_id = null;
var g_v_pos_lv_hf_id = null;
var g_v_pos_nav_hf_id = null;
var g_v_pos_val = 0;
var g_reload_items = true;


var gmscrollStep = 5;

var gmtimerLeft = "";
var gmtimerRight = "";




Number.prototype.toRad = function() { return this * Math.PI / 180; }

function LatLon_Distance( lat1, lon1, lat2, lon2 )
{
	var R = 6371; // km
	var dLat = (lat2 - lat1 ).toRad();
	var dLon = (lon2 - lon1 ).toRad(); 
	
	var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
        Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) * 
        Math.sin(dLon/2) * Math.sin(dLon/2); 
	
	var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
	
	var d = R * c;
	return( d );
}



function setup_main_urls()
{
    //alert( "setup" );
    JSRequest.EnsureSetup();
    var currentUrl = window.location.href;
    var ggindex = window.location.href.indexOf(JSRequest.PathName);
    
    if ( ggindex != -1 )
        {
        var servrUrl = currentUrl.substr(0, ggindex);
        
        if ( servrUrl != "" )
            {
            
            gmtools_url = servrUrl + "/gmtools/xmlbwpoints.asp?fname=";
            stop_url = servrUrl + "/_layouts/images/GiroMilano/bw/ferm.gif";
            stop_mm_url = servrUrl + "/_layouts/images/GiroMilano/bw/mm.jpg";
            start_url = servrUrl + "/_layouts/images/GiroMilano/bw/start.gif";
            end_url = servrUrl + "/_layouts/images/GiroMilano/bw/end.gif";
            fa_url = servrUrl + "/_layouts/images/GiroMilano/bw/fa.gif";
            g_poixml_url = servrUrl + "/gmtools/xmlpoi.asp"; 
            //g_poibase_icons_url = servrUrl + "/_LAYOUTS/ATM/images/Giromilano/";
            g_poi_info_xml_url = servrUrl + "/gmtools/xmlpoiinfo.asp";
            g_stop_info_xml_url = servrUrl + "/gmtools/xmlstop.asp";
            g_stop_lv_info_url = servrUrl + "/gmtools/xmllvstop.asp";
            //alert( g_poi_info_xml_url );
            }
        }
        
    if ( currentUrl != "" )
        {
        var blc = currentUrl.toLowerCase();
        if ( blc.indexOf(".it/en/") != -1 )
            g_lng = 1;
        }
    //alert( gmscrollStep );
}

function my_trim( str )
{
    return str.replace(/^\s*/, "").replace(/\s*$/, "");
}

function fill_qparameters()
{
	var strHref = window.location.href;
	if ( strHref.indexOf("?") > -1 ) 
        {
        
		var strQueryString = strHref.substr(strHref.indexOf("?")+1); 
        aQueryString = strQueryString.split("&");
        
        
		}
	
	return( true );
}

function get_param_value( param_name )
{
	if ( aQueryString == null )
		return( "" );
	
	var iParam;
	var rt_value = "";
	for ( iParam = 0; iParam < aQueryString.length; iParam++ ) 
		{
		if ( aQueryString[ iParam ].indexOf( param_name ) > -1 ) 
			{
			var aParam = aQueryString[ iParam ].split( "=" ); 
			rt_value = aParam[ 1 ];
			break;
			}
		}

	return( rt_value );
}

window.onscroll = get_Scroller_Coords;

function gmload()
{
	fill_qparameters();

	gplat = get_param_value("lat");
	gplon = get_param_value("lon");
	
	
	if ( gplat == "" )
		gplat = "45.4647";


	if ( gplon == "" )
		gplon = "9.186";

    
    gplat = gplat.replace(",",".");
    gplon = gplon.replace(",",".");

    set_scroll_pos();
	
	var srv_url = "";
	var extra_param = "";
	
	gbwxmlurl = "none";
	
	var cmd_code = 0;
	
	
	if ( g_amb_id != "" )
	    {
	    var amb_fld = document.getElementById( g_amb_id );
	    if ( amb_fld != null && amb_fld.value == "1" )
	        g_reload_items = false;
	    }
    
    if ( rdlt_lv_id != "" )
    {
        var rd_fld = document.getElementById( rdlt_lv_id );
	    if ( rd_fld != null && rd_fld.value == "1" )
	        g_reload_items = false;
    }
	
	var bwfname = "";
    var lvfname = "";
    
    if ( g_nl_submt_cl == 1 )
        g_reload_items = false;
	
	if ( g_reload_items )
	{
	    bwfname = get_param_value( "bwid" );
	    lvfname = get_param_value( "lvid" );
    }


	if ( lvfname != "" && g_canshowroute_id != null )
	{
	    var gghh = document.getElementById( g_canshowroute_id );
	    if ( gghh != null && gghh.value == "0" )
	    {
	        lvfname = "";
	        g_reload_items = false;
	    }
	}

	
	if ( bwfname != "" )
	    {
	    //ensure webpart target
	    var wp_target = get_param_value("wbt");
	    if ( wp_target == "nav" )
		    srv_url = gmtools_url + bwfname;
		    
		cmd_code = 1;
		}
    
    if ( lvfname != "" )
	    {
	    //ensure webpart target
	    var wp_target = get_param_value("wbt");
	    if ( wp_target == "nav" )
		    srv_url = gmtools_url + lvfname;
		    
		cmd_code = 2;
		
		if ( get_param_value( "nl" ) == "1" )
		    cmd_code = 3;
		}

	gfaparam = get_param_value("faparam");
	//var latlon_str_fa = "";
	if ( gfaparam == "1" && g_reload_items )
	    {
		extra_param = gplat + "$" + gplon;
		cmd_code = 0;
		}
		

	
	loadgmap( gplat, gplon, 14, srv_url, extra_param, cmd_code );
}

_spBodyOnLoadFunctionNames.push('setup_main_urls');
_spBodyOnLoadFunctionNames.push('gmload');


//setup_main_urls();



document.onunload=GUnload();
//document.onunload=testunload();

//function testunload()
//{
//	alert( "Unload" );
//}



function test()
{
	fill_qparameters();
	
	gplat = get_param_value("lat");
	gplon = get_param_value("lon");

	alert( gplat );
	alert( gplon );
}



/*********************************************************************************/
function createXMLHttpObject()
{
    xmlHttp = GXmlHttp.create();

	return( xmlHttp != null );
}

/*********************************************************************************/
function PostXmlRequest( url, xmltopost, b_use_1 )
{
	
	if ( xmlHttp == null )
		return( false );
	
	if ( xmltopost == "" )
	    {
	    xmlHttp.open("GET", url, false);
	    xmlHttp.send(null);
	    }
	    else
	        {
	        xmlHttp.open("POST", url, false);
	        xmlHttp.send(xmltopost);
	        }

	if ( xmlHttp.status == 200 )
		{
		if ( !b_use_1  )
		    xmlreceived = xmlHttp.responseXML;
		    else
		        xmlreceived_1 = xmlHttp.responseXML;
		return( true );
		}

	return( false );

}

function mem_start_end()
{
    var wp_pl_id = null;
    var wp_add_id = null;
    
    var bln_pl_id = null;
    var bln_add_id = null;
    
    //alert( "start:" + g_start_click_setted +" end:" + g_end_click_setted);
    if ( g_start_click_setted )
        {
        bln_pl_id = document.getElementById("_s_place_hd" );
        bln_add_id = document.getElementById("_s_add_hd" );
        wp_pl_id = document.getElementById( g_pl_s_id );
        wp_add_id = document.getElementById( g_add_s_id );
        
        if ( bln_pl_id != null && bln_add_id != null && wp_pl_id != null && wp_add_id != null )
            {
            bln_pl_id.value = wp_pl_id.value;
            bln_add_id.value = wp_add_id.value;
            }
        }
        
    if ( g_end_click_setted )
        {
        bln_pl_id = document.getElementById("_e_place_hd" );
        bln_add_id = document.getElementById("_e_add_hd" );
        wp_pl_id = document.getElementById( g_pl_e_id );
        wp_add_id = document.getElementById( g_add_e_id );
        
        if ( bln_pl_id != null && bln_add_id != null && wp_pl_id != null && wp_add_id != null )
            {
            bln_pl_id.value = wp_pl_id.value;
            bln_add_id.value = wp_add_id.value;
            }
        }
}

/*********************************************************************************/
function get_xml_stop_info( s_code, s_descr, line_name )
{
	var xmlstring;

	xmlstring = '<?xml version="1.0" encoding="ISO-8859-1" ?>';
	xmlstring += "<stoprequest>";
	xmlstring += "<code>" + s_code + "</code>";
	xmlstring += "<descr>" + s_descr + "</descr>";
	xmlstring += "<linename>" + line_name + "</linename>";
	xmlstring += "<lng>" + g_lng + "</lng>";
	xmlstring += "</stoprequest>";
	
	if ( !createXMLHttpObject() )
		return( "" );
	
	//alert( url );
	if ( !PostXmlRequest( g_stop_info_xml_url, xmlstring, true ) )
		return( "" );
	
	if ( xmlreceived_1 == null )
		return( "" );

    var nodes_list = xmlreceived_1.documentElement.getElementsByTagName( "stopinfo" );
	
	
	if ( nodes_list == null )
	    return( "" );
    var stop_info_html = "";
	if ( nodes_list.item( 0 ).hasChildNodes )
        stop_info_html = nodes_list.item( 0 ).childNodes[ 0 ].nodeValue;
        
    return( stop_info_html );
    
}

/*********************************************************************************/
function get_xml_lv_stop_info( s_code, s_descr, line_name )
{
	var xmlstring;

	xmlstring = '<?xml version="1.0" encoding="ISO-8859-1" ?>';
	xmlstring += "<stoprequest>";
	xmlstring += "<code>" + s_code + "</code>";
	xmlstring += "<descr>" + s_descr + "</descr>";
	xmlstring += "<linename>" + line_name + "</linename>";
	xmlstring += "<lng>" + g_lng + "</lng>";
	xmlstring += "</stoprequest>";
	
	if ( !createXMLHttpObject() )
		return( "" );
	
	//alert( xmlstring );
	if ( !PostXmlRequest( g_stop_lv_info_url, xmlstring, true ) )
		return( "" );
	
	if ( xmlreceived_1 == null )
		return( "" );

    var nodes_list = xmlreceived_1.documentElement.getElementsByTagName( "stopinfo" );
	
	if ( nodes_list == null )
	    return( "" );
    var stop_info_html = "";
	if ( nodes_list.item( 0 ).hasChildNodes )
        stop_info_html = nodes_list.item( 0 ).childNodes[ 0 ].nodeValue;
        
    return( stop_info_html );
}

/*********************************************************************************/
function create_bestway_overlay( url )
{
	
	if ( !createXMLHttpObject() )
		return( false );
	
	
	if ( !PostXmlRequest( url, "", false ) )
		return( false );
	
	
	if ( xmlreceived == null )
		return( false );
	
	var bw_polyline_list = xmlreceived.documentElement.getElementsByTagName( "polyline" );
	
	if ( bw_polyline_list == null )
		return( false );
	
	num_bw_polylines = bw_polyline_list.length;

	
	if ( num_bw_polylines == 0 )
		return( false );
	
	var line_info_nodes = xmlreceived.documentElement.getElementsByTagName( "lineinfo" );

	bw_polylines_array_marker = new Array();

	bw_listeners_array = new Array();

	var iter;
	
	var lat, lon;

	var bw_polyline_points_arr = new Array();
	var tmp_bw_polyline_points_arr = new Array();

	var num_points = 0;

	var iter_child;

	var bw_point_attributes;
	
	var hex_red;
	var hex_green;
	var hex_blue;
	
	var polyline_color;

	
	var line_info;
	var line_info_index = 0;

    var iter_k;
	var iter_j;
	var end_loop_index;
	var start_loop_index;
	
	var bw_marker;	
	
	for ( iter = 0; iter < num_bw_polylines; iter++ )
		{
		
		hex_red = bw_polyline_list[ iter ].getAttribute( "hexred" );
		if ( hex_red == "0" )
			hex_red = "00";
		
		hex_green = bw_polyline_list[ iter ].getAttribute( "hexgreen" );
		if ( hex_green == "0" )
			hex_green = "00";
		
		hex_blue = bw_polyline_list[ iter ].getAttribute( "hexblue" );
		if ( hex_blue == "0" )
			hex_blue = "00";
		
		polyline_color = "#" + hex_red + hex_green + hex_blue;
		//alert( polyline_color );
		if ( bw_polyline_list[ iter ].hasChildNodes )
			{
			var xml_polyline_points = bw_polyline_list[ iter ].childNodes;

			if ( xml_polyline_points != null )
				{
			
				line_info = "";
				for ( iter_child = 0; iter_child < xml_polyline_points.length; iter_child++ )
					{
					if ( xml_polyline_points[ iter_child ].nodeName == "lineinfo" )
						{
						if ( line_info_nodes != null )
							{
							line_info = line_info_nodes[ line_info_index ].text;
							line_info_index++;
							
							}
						
						continue;
						}
					
					bw_point_attributes = xml_polyline_points[ iter_child ].attributes;
			
					if ( bw_point_attributes == null )
						{
						//alert( "KO" );
						break;
						}

				
					lat = xml_polyline_points[ iter_child ].getAttribute( "lat" );
					lon = xml_polyline_points[ iter_child ].getAttribute( "lon" );
					
					lat = lat.replace(/","/g,".");
                    lon = lon.replace(/","/g,".");

					bw_polyline_points_arr.push( new GLatLng( lat, lon ) );
					}
				
				//18/03/2008 reverse points into smaller vectors
				if ( bw_polyline_points_arr.length > g_bw_points_step )
					{
					for ( iter_k = 0; iter_k < bw_polyline_points_arr.length; iter_k += g_bw_points_step )
						{
						tmp_bw_polyline_points_arr.length = 0;

						end_loop_index = iter_k + g_bw_points_step;
						if ( iter_k == 0 )
							start_loop_index = iter_k;
							else
								start_loop_index = iter_k - 1;//must include last previous point
						
						for ( iter_j = start_loop_index; iter_j < end_loop_index && iter_j < bw_polyline_points_arr.length; iter_j++ )
							tmp_bw_polyline_points_arr.push( bw_polyline_points_arr[ iter_j ] );
						

						if ( tmp_bw_polyline_points_arr.length > 0 )
							{
							bw_marker = new GPolyline( tmp_bw_polyline_points_arr ,polyline_color, 5, 0.9 );
							gmap.addOverlay( bw_marker );
							bw_polylines_array_marker.push( bw_marker );
							}
						}
					}
					else
						{

						//alert( bw_polyline_points_arr.length );
						bw_marker = new GPolyline( bw_polyline_points_arr ,polyline_color, 5, 0.9 );
						gmap.addOverlay( bw_marker );
						
						//add click event
						
						if ( line_info != "" )
							{
							
							var pol_listener = create_line_info_listener( bw_marker, line_info );
							
							
							bw_listeners_array.push( pol_listener );
							}

						bw_polylines_array_marker.push( bw_marker );
						}
				
				//reset bwpoints array
				bw_polyline_points_arr.length = 0;
				
				}
			}

		}

	
	bw_icons_marker_array = new Array();
	//now add bw icons
	
	var num_stops = 0;
	//retrieve stops
	var xml_stops_list = xmlreceived.documentElement.getElementsByTagName( "stop" );
	
	if ( xml_stops_list != null )
		num_stops = xml_stops_list.length;

	var bw_hot_point_marker;
	
	var stop_code;
	var stop_descr;
	var line_name;
	var tt_link_str;
	var html_code;
	
	for ( iter = 0; iter < num_stops; iter++ )
		{
		lat = xml_stops_list[ iter ].getAttribute( "lat" );
		lon = xml_stops_list[ iter ].getAttribute( "lon" );
		
		stop_code = xml_stops_list[ iter ].getAttribute( "scode" );
		line_name = xml_stops_list[ iter ].getAttribute( "line" );
        stop_descr = xml_stops_list[ iter ].getAttribute( "sdescr" );
        
        tt_link_str	= "";
		if ( lat != null && lon != null )
			{
			lat = lat.replace(/","/g,".");
            lon = lon.replace(/","/g,".");
			
		    if ( stop_code != null && line_name != null )
		        {
		        stop_code = my_trim( stop_code );
		        //tt_link_str = "http://orari.atm-mi.it/" + line_name + "_" + stop_code + ".pdf";
		        
		        //html_code = get_xml_stop_info( stop_code, stop_descr, line_name );
		        //call xmllvstop to get more info into ballon
		        html_code = get_xml_lv_stop_info( stop_code, stop_descr, line_name );
		        }
			
			//call xml server to retrieve stop info....
			bw_hot_point_marker = create_bw_icon( lat, lon, stop_url, html_code );
			bw_icons_marker_array.push( bw_hot_point_marker );
			gmap.addOverlay( bw_hot_point_marker );
			}
			
		}

	//retrieve start and end
	var xml_start_end_list = xmlreceived.documentElement.getElementsByTagName( "start" );
	if ( xml_start_end_list == null )
		return( false );

	if ( xml_start_end_list.length == 1 )
		{
		lat = xml_start_end_list[ 0 ].getAttribute( "lat" );
		lon = xml_start_end_list[ 0 ].getAttribute( "lon" );
		
		
		if ( lat != null && lon != null )
			{
			lat = lat.replace(/","/g,".");
            lon = lon.replace(/","/g,".");
			
			bw_hot_point_marker = create_bw_icon( lat, lon, start_url, "" );
			bw_icons_marker_array.push( bw_hot_point_marker );
			gmap.addOverlay( bw_hot_point_marker );
			}

		}
	
	xml_start_end_list = xmlreceived.documentElement.getElementsByTagName( "end" );
	if ( xml_start_end_list == null )
		return( false );

	if ( xml_start_end_list.length == 1 )
		{
		lat = xml_start_end_list[ 0 ].getAttribute( "lat" );
		lon = xml_start_end_list[ 0 ].getAttribute( "lon" );
		
		
		if ( lat != null && lon != null )
			{
			lat = lat.replace(/","/g,".");
            lon = lon.replace(/","/g,".");
			
			bw_hot_point_marker = create_bw_icon( lat, lon, end_url, "" );
			bw_icons_marker_array.push( bw_hot_point_marker );
			gmap.addOverlay( bw_hot_point_marker );
			}

		}

	//retrieve boundy box
	var bbox_element_list = xmlreceived.documentElement.getElementsByTagName( "bbox" );
	
	if ( bbox_element_list == null )
	    return( false );
	
	
	var sw_lat = bbox_element_list[ 0 ].getAttribute( "sw_lat" );
	var sw_lon = bbox_element_list[ 0 ].getAttribute( "sw_lon" );

	var ne_lat = bbox_element_list[ 0 ].getAttribute( "ne_lat" );
	var ne_lon = bbox_element_list[ 0 ].getAttribute( "ne_lon" );
	
    sw_lat = sw_lat.replace(/","/g,".");
    sw_lon = sw_lon.replace(/","/g,".");
    
    ne_lat = ne_lat.replace(/","/g,".");
    ne_lon = ne_lon.replace(/","/g,".");

	var bw_bounds = new GLatLngBounds( new GLatLng( sw_lat, sw_lon ), new GLatLng( ne_lat, ne_lon ) );
	var bounds_zoom_level = gmap.getBoundsZoomLevel( bw_bounds );
	gmap.setCenter(bw_bounds.getCenter(), bounds_zoom_level);
	

	return( true );
}



/*********************************************************************************/
function create_lv_overlay( url )
{
	
	if ( !createXMLHttpObject() )
		return( false );
	
	
	if ( !PostXmlRequest( url, "", false ) )
		return( false );
	
	
	if ( xmlreceived == null )
		return( false );
	
	var lv_polyline_list = xmlreceived.documentElement.getElementsByTagName( "polyline" );
	
	if ( lv_polyline_list == null )
		return( false );
	
	var num_lv_polylines = lv_polyline_list.length;

	
	if ( num_lv_polylines == 0 )
		return( false );
	
	lv_polylines_array_marker = new Array();

	lv_listeners_array = new Array();
	lv_markers_id = new Array();

	var iter;
	
	var lat, lon;

	var lv_polyline_points_arr = new Array();
	var tmp_lv_polyline_points_arr = new Array();

	var num_points = 0;

	var iter_child;

	var lv_point_attributes;
	
	var hex_red;
	var hex_green;
	var hex_blue;
	
	var polyline_color;

    var iter_k;
	var iter_j;
	var end_loop_index;
	var start_loop_index;
	
	var lv_marker;	
	var line_name = "";
	
	for ( iter = 0; iter < num_lv_polylines; iter++ )
		{
		
		line_name = lv_polyline_list[ iter ].getAttribute( "lname" );
		
		hex_red = lv_polyline_list[ iter ].getAttribute( "hexred" );
		if ( hex_red == "0" )
			hex_red = "00";
		
		hex_green = lv_polyline_list[ iter ].getAttribute( "hexgreen" );
		if ( hex_green == "0" )
			hex_green = "00";
		
		hex_blue = lv_polyline_list[ iter ].getAttribute( "hexblue" );
		if ( hex_blue == "0" )
			hex_blue = "00";
		
		polyline_color = "#" + hex_red + hex_green + hex_blue;
		//alert( polyline_color );
		if ( lv_polyline_list[ iter ].hasChildNodes )
			{
			var xml_polyline_points = lv_polyline_list[ iter ].childNodes;

			if ( xml_polyline_points != null )
				{
				for ( iter_child = 0; iter_child < xml_polyline_points.length; iter_child++ )
					{
					
					lv_point_attributes = xml_polyline_points[ iter_child ].attributes;
			
					if ( lv_point_attributes == null )
						{
						//alert( "KO" );
						break;
						}

				
					lat = xml_polyline_points[ iter_child ].getAttribute( "lat" );
					lon = xml_polyline_points[ iter_child ].getAttribute( "lon" );
					
					lat = lat.replace(/","/g,".");
                    lon = lon.replace(/","/g,".");

					lv_polyline_points_arr.push( new GLatLng( lat, lon ) );
					}
				
				//18/03/2008 reverse points into smaller vectors
				if ( lv_polyline_points_arr.length > g_bw_points_step )
					{
					for ( iter_k = 0; iter_k < lv_polyline_points_arr.length; iter_k += g_bw_points_step )
						{
						tmp_lv_polyline_points_arr.length = 0;

						end_loop_index = iter_k + g_bw_points_step;
						if ( iter_k == 0 )
							start_loop_index = iter_k;
							else
								start_loop_index = iter_k - 1;//must include last previous point
						
						for ( iter_j = start_loop_index; iter_j < end_loop_index && iter_j < lv_polyline_points_arr.length; iter_j++ )
							tmp_lv_polyline_points_arr.push( lv_polyline_points_arr[ iter_j ] );
						

						if ( tmp_lv_polyline_points_arr.length > 0 )
							{
							lv_marker = new GPolyline( tmp_lv_polyline_points_arr, polyline_color, 5, 0.9 );
							gmap.addOverlay( lv_marker );
							lv_polylines_array_marker.push( lv_marker );
							}
						}
					}
					else
						{

						//alert( bw_polyline_points_arr.length );
						lv_marker = new GPolyline( lv_polyline_points_arr ,polyline_color, 5, 0.9 );
						gmap.addOverlay( lv_marker );
						
						lv_polylines_array_marker.push( lv_marker );
						}
				
				//reset bwpoints array
				lv_polyline_points_arr.length = 0;
				
				}
			}

		}
	
	lv_icons_marker_array = new Array();
	//now add bw icons
	
	var num_stops = 0;
	//retrieve stops
	var xml_stops_list = xmlreceived.documentElement.getElementsByTagName( "stop" );
	
	if ( xml_stops_list != null )
		num_stops = xml_stops_list.length;

	var lv_hot_point_marker;
	
	var stop_code;
	var stop_descr;
	var tt_link_str;
	var html_code;
	var stop_img_url = stop_url;
	
	if ( line_name == "M1" || line_name == "M2" || line_name == "M3" )
	    stop_img_url = stop_mm_url;
	    
	for ( iter = 0; iter < num_stops; iter++ )
		{
		lat = xml_stops_list[ iter ].getAttribute( "lat" );
		lon = xml_stops_list[ iter ].getAttribute( "lon" );
		
		stop_code = xml_stops_list[ iter ].getAttribute( "scode" );
		//line_name = xml_stops_list[ iter ].getAttribute( "line" );
        stop_descr = xml_stops_list[ iter ].getAttribute( "sdescr" );
        
        tt_link_str	= "";
		if ( lat != null && lon != null )
			{
			lat = lat.replace(/","/g,".");
            lon = lon.replace(/","/g,".");
			
			if ( stop_code != null )
			    stop_code = my_trim( stop_code );
		    //if ( stop_code != null && line_name != null )
		    //    {
		   //     stop_code = my_trim( stop_code );
		        //tt_link_str = "http://orari.atm-mi.it/" + line_name + "_" + stop_code + ".pdf";
		        
		    //    html_code = get_xml_lv_stop_info( stop_code, stop_descr, line_name );
		    //    }
			
			//call xml server to retrieve stop info....
			lv_hot_point_marker = create_lv_icon( lat, lon, stop_img_url, stop_code, stop_descr, line_name );
			lv_icons_marker_array.push( lv_hot_point_marker );
			lv_markers_id.push( stop_code );
			gmap.addOverlay( lv_hot_point_marker );
			}
			
		}

	//retrieve boundy box
	var bbox_element_list = xmlreceived.documentElement.getElementsByTagName( "bbox" );
	
	if ( bbox_element_list == null )
	    return( false );
	
	
	var sw_lat = bbox_element_list[ 0 ].getAttribute( "sw_lat" );
	var sw_lon = bbox_element_list[ 0 ].getAttribute( "sw_lon" );

	var ne_lat = bbox_element_list[ 0 ].getAttribute( "ne_lat" );
	var ne_lon = bbox_element_list[ 0 ].getAttribute( "ne_lon" );
	
    sw_lat = sw_lat.replace(/","/g,".");
    sw_lon = sw_lon.replace(/","/g,".");
    
    ne_lat = ne_lat.replace(/","/g,".");
    ne_lon = ne_lon.replace(/","/g,".");

	var bw_bounds = new GLatLngBounds( new GLatLng( sw_lat, sw_lon ), new GLatLng( ne_lat, ne_lon ) );
	var bounds_zoom_level = gmap.getBoundsZoomLevel( bw_bounds );
	gmap.setCenter(bw_bounds.getCenter(), bounds_zoom_level);
	

	return( true );
}

/*********************************************************************************/
function create_line_info_listener( bw_marker__, line_info_str )
{
	return( GEvent.addListener( bw_marker__,"click",function(param)
					{gmap.openInfoWindowHtml(param, "Linea:<h3>" + line_info_str + "</h3>")}) );
}


/*********************************************************************************/
function add_bw_overlay( bwxmlurl )
{
	
	if ( gmap == null )
		return( false );
	
	remove_bw_overlay();
	
	if ( !create_bestway_overlay( bwxmlurl ) )
		return( false );
	return( true );
}

/*********************************************************************************/
function add_lv_overlay( lvurl )
{
	
	if ( gmap == null )
		return( false );
	
	remove_lv_overlay();
	
	if ( !create_lv_overlay( lvurl ) )
		return( false );
	return( true );
}

/*********************************************************************************/
function create_bw_icon( lat, lon, image_name, info_record )
{
	
	if ( gmap == null )
		return;
	
	
	//add bw
	var Icon = new GIcon();
	Icon.iconSize = new GSize(16, 16);
	Icon.iconAnchor = new GPoint(9, 16);

	Icon.image = image_name;
	if ( info_record != "" )
	    Icon.infoWindowAnchor = new GPoint( 9, 2);
	
	markerOptions = { icon:Icon };
	
	var marker = new GMarker( new GLatLng( lat, lon ), markerOptions );
	
	if ( info_record != "" )
	    {
	    var event_listener = GEvent.addListener( marker, "click", 
	     	    function() {
	            marker.openInfoWindowHtml( info_record );
				
				} );

	    if ( bw_listeners_array != null )
		    bw_listeners_array.push( event_listener );


	
	    }
	
	return( marker );	

}

/*********************************************************************************/
function create_lv_icon( lat, lon, image_name, stop_code, stop_descr, line_name )
{
	if ( gmap == null )
		return;
	var Icon = new GIcon();
	Icon.iconSize = new GSize(16, 16);
	Icon.iconAnchor = new GPoint(9, 16);

	Icon.image = image_name;
	
	Icon.infoWindowAnchor = new GPoint( 9, 2);
	
	markerOptions = { icon:Icon };
	
	var marker = new GMarker( new GLatLng( lat, lon ), markerOptions );
	
    var event_listener = GEvent.addListener( marker, "click", 
	     	    function() {
	     	    
	     	    var info_record = get_xml_lv_stop_info( stop_code, stop_descr, line_name );
	     	    
	     	    if ( info_record != "" )
	     	        {
	                marker.openInfoWindowHtml( info_record );
		
		            mem_start_end();
		            }
		
				
				} );

    if ( lv_listeners_array != null )
	    lv_listeners_array.push( event_listener );


	
	return( marker );	

}

function find_lv_poi( sid )
{
    var i;
    if ( lv_markers_id == null )
        return( -1 );
	for ( i = 0; i < lv_markers_id.length; i++ )
	{
		if ( sid == lv_markers_id[ i ] )
			return( i );
	}
	return( -1 );
    
}
/*********************************************************************************/
function show_lv_info( s_id, s_descr, l_name, lat, lon )
{
	var	 p_p = find_lv_poi( s_id );
    
	if ( p_p == -1 )
		return( false );
    
	var marker = lv_icons_marker_array[ p_p ];
	
	var html_code;
	if ( marker != null )
	    {
	    zoom_at( lat, lon, 15 );
        html_code = get_xml_lv_stop_info( s_id, s_descr, l_name );
	
	    if ( html_code != "" )
	        {
	        marker.openInfoWindowHtml( html_code );
	        mem_start_end();
	        }
        }
}

/*********************************************************************************/
function remove_bw_overlay()
{
	var jj;
	if ( bw_polylines_array_marker != null )
		{

		//alert( "Remove bw overlay:" + bw_polylines_array_marker.length );
		for ( jj = 0; jj < bw_polylines_array_marker.length; jj++ )
			gmap.removeOverlay( bw_polylines_array_marker[ jj ] );
		
		bw_polylines_array_marker.length = 0;
		bw_polylines_array_marker = null;
	
		
		if ( bw_icons_marker_array != null )
			{
			

			for ( jj = 0; jj < bw_icons_marker_array.length; jj++ )
				gmap.removeOverlay( bw_icons_marker_array[ jj ] );
			
			bw_icons_marker_array.length = 0;	
			bw_icons_marker_array = null;
			}
		}

	if ( bw_listeners_array != null )
		{
		for ( jj = 0; jj < bw_listeners_array.length; jj++ )
			{
			if ( bw_listeners_array[ jj ] != null )
				GEvent.removeListener( bw_listeners_array[ jj ] );
			}
		bw_listeners_array.length = 0;
		bw_listeners_array = null;
		}
	//return( false );
}
		
/*********************************************************************************/
function remove_lv_overlay()
{
	var jj;
	
	if ( lv_polylines_array_marker != null )
	{
	    for ( jj = 0; jj < lv_polylines_array_marker.length; jj++ )
			gmap.removeOverlay( lv_polylines_array_marker[ jj ] );
		
		lv_polylines_array_marker.length = 0;
		lv_polylines_array_marker = null;
	}
	
	if ( lv_icons_marker_array != null )
    {
		for ( jj = 0; jj < lv_icons_marker_array.length; jj++ )
			gmap.removeOverlay( lv_icons_marker_array[ jj ] );
		
		lv_icons_marker_array.length = 0;	
		lv_icons_marker_array = null;
    }


	if ( lv_listeners_array != null )
		{
		for ( jj = 0; jj < lv_listeners_array.length; jj++ )
			{
			if ( lv_listeners_array[ jj ] != null )
				GEvent.removeListener( lv_listeners_array[ jj ] );
			}
		lv_listeners_array.length = 0;
		lv_listeners_array = null;
		}
	
	if ( lv_markers_id != null )
	    {
	    lv_markers_id.length = 0;
	    lv_markers_id = null;
	    }
	//return( false );
}


/*********************************************************************************/
function custom_load( lat, lon, start_zoom_level )
{
	if (GBrowserIsCompatible())
		{
		
		gmap = new GMap2(document.getElementById("map"), { mapTypes : [G_SATELLITE_MAP,G_HYBRID_MAP] } );
		/*gmap.addControl(new GLargeMapControl())*/
		gmap.setCenter(new GLatLng(lat, lon ), start_zoom_level );
		
		
		if ( bwurl != "none" )
			{
			add_bw_overlay( bwurl );
			}
		
		return( true );
		}
	
	return( false );
}

/*********************************************************************************/
function zoom_at( lat, lon, zoom_level )
{
	if ( gmap == null )
		return( false );
    
    var lat_str = lat + "";
    var lon_str = lon + "";
    
    lat_str = lat_str.replace(",",".");
    lon_str = lon_str.replace(",",".");
    
    gmap.setCenter(new GLatLng(lat_str, lon_str ), zoom_level );

}

/*********************************************************************************/
function loadgmap( lat, lon, start_zoom_level, srv_url, extraparam, cmd_code )
{
	if (GBrowserIsCompatible())
		{
		
		gmap = new GMap2(document.getElementById("map") );
		
		if ( gmap == null )
		    return;

		gmap.addControl(new GMapTypeControl());
	    gmap.addControl(new GLargeMapControl3D());
		//gmap.addMapType(G_SATELLITE_3D_MAP);

		
		//this is for map click events
		//GEvent.addListener(gmap,"click", function(overlay,latlng)
		//	{    
		//	if (latlng)
		//		{   
		//		var myHtml = "The GLatLng value is: " + gmap.fromLatLngToDivPixel(latlng) + " at zoom level " + gmap.getZoom();
		//		gmap.openInfoWindow(latlng, myHtml);
		//		}
		//	} );
        
        //this is for map click events
		GEvent.addListener(gmap,"click", function()
			{
			    if ( !gmap.scrollWheelZoomEnabled() )
			        gmap.enableScrollWheelZoom();
			} );

		//gmap.addControl(new GLargeMapControl() );

		//var maptype_control = new GMapTypeControl();
		//gmap.addControl( maptype_control );
		
		
		switch( cmd_code )
			{
			case 0:
				
				if ( extraparam != "" )
					{
					var splitted_param = extraparam.split( "$" );
					add_fa_overlay( splitted_param[ 0 ], splitted_param[ 1 ] );
					gmap.setCenter( new GLatLng(splitted_param[ 0 ], splitted_param[ 1 ] ), 15 );
					}
					else
						gmap.setCenter( new GLatLng(lat, lon ), start_zoom_level );
				break;
			
			case 1:
				if ( !add_bw_overlay( srv_url ) )
		           gmap.setCenter(new GLatLng(lat, lon ), start_zoom_level );
				break;
			
			case 2:
				if ( !add_lv_overlay( srv_url ) )
		           gmap.setCenter(new GLatLng(lat, lon ), start_zoom_level );
				break;
            
            case 3 :
            	add_lv_overlay( srv_url );
            	add_fa_overlay( lat, lon );
                gmap.setCenter(new GLatLng(lat, lon ), 15 );
				break;
			
			default:
				gmap.setCenter(new GLatLng(lat, lon ), start_zoom_level );
				break;
			}
		
		
		}
	//set metrocontainer width
	var mtcon = document.getElementById("metrocontainer" );
	
	if ( mtcon == null )
	{
	    mtcon = document.getElementById("metrocontainer1" );
	    if ( mtcon == null )
	        mtcon = document.getElementById("metrocontainer2" );
	}
	
	//alert(objid != null );
	if ( mtcon != null )
	{
	    var map_div = document.getElementById("map" );
	    
	    if ( map_div != null )
	    {
            var newsize = map_div.clientWidth + 140;
            mtcon.style.width = newsize + "px";
        }
        //alert( mtcon.style.width );

	}
	

}

/*********************************************************************************/
function add_fa_overlay( lat, lon )
{
	
	if ( gmap == null )
		return( false );
	
	remove_fa_overlay();
	//add find address icon
	var faIcon = new GIcon();
	faIcon.iconSize = new GSize(20, 20);
	//baseIcon.shadowSize = new GSize(37, 34);
	faIcon.iconAnchor = new GPoint(9, 20);

	
	//var letteredIcon = new GIcon(baseIcon);
	faIcon.image = fa_url;
	markerOptions = { icon:faIcon };
	
	fa_marker = new GMarker( new GLatLng( lat, lon ), markerOptions );
	
	gmap.addOverlay( fa_marker );	
	

}

/*********************************************************************************/
function remove_fa_overlay()
{
	if ( fa_marker != null )
		{
		gmap.removeOverlay( fa_marker );
				
		fa_marker = null;
		}
}

/*********************************************************************************/
function remove_all_overlay()
{
    //alert( "remove" );
	remove_fa_overlay();
	remove_bw_overlay();
	remove_lv_overlay();
	remove_poi_overlays();
	
	g_start_click_setted = false;
    g_end_click_setted = false;
    g_nl_submt_cl = 0;
}

/*********************************************************************************/
function msgbox_show( text )
{
	alert( text );
	return( true );
}

/*********************************************************************************/
function remove_poi_overlays()
{
	var i;
	for ( i = 0; i < poi_active_category.length; i++ )
		{
		if ( poi_db_category_index[ i ] != null )
			{
			poi_active_category[ i ] = 0;
			remove_poi_coord( poi_category_markers[ i ], poi_category_listeners[ i ] );
		
			}
		}
	
	poi_active_category.length = 0;
	poi_db_category_index.length = 0;
	poi_category_markers.length = 0;
	poi_category_listeners.length = 0;
	
}

/*********************************************************************************/
function get_poi_detail( poi_id )
{
	var xmlstring;

	xmlstring = '<?xml version="1.0" encoding="ISO-8859-1" ?>';
	xmlstring += "<poirequest>";
	xmlstring += "<poi_id>" + poi_id + "</poi_id>";
	xmlstring += "<lng>" + g_lng + "</lng>";
	xmlstring += "</poirequest>";
	
	if ( !createXMLHttpObject() )
		return( "" );
	
	//alert( url );
	if ( !PostXmlRequest( g_poi_info_xml_url, xmlstring, false ) )
		return( "" );
	
	if ( xmlreceived == null )
		return( "" );

    var nodes_list = xmlreceived.documentElement.getElementsByTagName( "record_info" );
	
	
	if ( nodes_list == null )
	    return( "" );
    var poi_info_html = "";
	if ( nodes_list.item( 0 ).hasChildNodes )
        poi_info_html = nodes_list.item( 0 ).childNodes[ 0 ].nodeValue;
        
    return( poi_info_html );
    
    
}

/*********************************************************************************/
function create_poi_icon( poi_id, poi_info, lat, lon, image_name, marker_vect, marker_listener_vect, inforec )
{
	
	if ( gmap == null )
		return;
	
	
	//add poi icon
	var Icon = new GIcon();
	Icon.iconSize = new GSize(g_poi_x_size, g_poi_x_size);
	Icon.iconAnchor = new GPoint(9, 16);

	Icon.infoWindowAnchor = new GPoint( 9, 2);
	
	Icon.image = image_name;
	markerOptions = { icon:Icon };
	
	var marker = new GMarker( new GLatLng( lat, lon ), markerOptions );


	marker_vect.push( marker );
	var event_listener = GEvent.addListener( marker, "click", 
	     	    function() {
	            marker.openInfoWindowHtml( inforec );
	            
	            mem_start_end();
	           
	            var obj_html = document.getElementById("poi_detail_info");
	            
                var rec_detail_html = get_poi_detail( poi_id );
                
                
                //pass data via xml to render table under map
				if ( obj_html != null && rec_detail_html != "" )
                    obj_html.innerHTML = rec_detail_html;
					else
					    {
					    if ( obj_html != null )
                            obj_html.innerHTML = "";
					    }
				
				
				} );

	if ( event_listener != null )
		marker_listener_vect.push( event_listener );

	
	return( marker );	
	

}

/*********************************************************************************/
function fill_poi_coord( xmltopost, marker_points_vector, image_name, marker_listener_vector )
{
	
	//alert("filling poi coord");
	
	if ( !createXMLHttpObject() )
		return( false );
	
	
	if ( !PostXmlRequest( g_poixml_url, xmltopost, false ) )
		return( false );
	
    
	
	if ( xmlreceived == null )
		return( false );

	
	var nodes_list = xmlreceived.documentElement.getElementsByTagName( "record" );
	
	
	if ( nodes_list == null )
		return( false );
	
	
	var num_poi = nodes_list.length;

	if ( num_poi == 0 )
		return( false );
	
	
	var iter;
	
	var lat, lon;

	var info_record;

	var poi_marker;
	var poi_id;
	var poi_info;
	
	for ( iter = 0; iter < num_poi; iter++ )
		{
		
		//var node_attributes = nodes_list.item( iter ).attributes;
		
		
		//if ( node_attributes == null )
		//	break;

		
		lat = nodes_list[ iter ].getAttribute( "lat" );
		lon = nodes_list[ iter ].getAttribute( "lon" );

		
		//lat = lat.replace(/","/g,".");
        //lon = lon.replace(/","/g,".");
		
		poi_id = nodes_list[ iter ].getAttribute( "id" );
		//poi_info = nodes_list[ iter ].getAttribute( "pinfo" );
		
		if ( nodes_list[ iter ].hasChildNodes )
			info_record = nodes_list[ iter ].childNodes[ 0 ].nodeValue;
			else
				info_record = "";
	
		poi_marker = create_poi_icon( poi_id, poi_info, lat, lon, image_name, marker_points_vector, marker_listener_vector, info_record );

		gmap.addOverlay( poi_marker );
		}
	
	return( true );

}

/*********************************************************************************/
function remove_poi_coord( marker_points_vector, marker_listener_vector )
{
	var jj;

	for ( jj = 0; jj < marker_points_vector.length; jj++ )
		gmap.removeOverlay( marker_points_vector[ jj ] );

	for ( jj = 0; jj < marker_listener_vector.length; jj++ )
		{
		if ( marker_listener_vector[ jj ] != null )
			GEvent.removeListener( marker_listener_vector[ jj ] );
		}

}

function showpoifromimg( dbcategory_index, icon_image, obj_id )
{
    var obj = document.getElementById( obj_id );
    if ( obj != null )
    {
    
        obj.checked = !obj.checked;
        showpoi( dbcategory_index, icon_image, obj );
    }
}

/*********************************************************************************/
function showpoi( dbcategory_index, icon_image, obj_check )
{
	var i;

	var my_cat_index = 0;
	
	var found = 0;

    var current_bounds = gmap.getBounds();
    
	var sw_lat_lon = current_bounds.getSouthWest();
	var ne_lat_lon = current_bounds.getNorthEast();

    var tmp_distance = LatLon_Distance( sw_lat_lon.lat(), sw_lat_lon.lng(), ne_lat_lon.lat(), ne_lat_lon.lng() );
	
	var xmlstring;

	xmlstring = '<?xml version="1.0" encoding="ISO-8859-1" ?>';
	xmlstring += "<poirequest>";
	xmlstring += "<category>" + dbcategory_index + "</category>";
	xmlstring += "<sw_lat>" + sw_lat_lon.lat() + "</sw_lat>";
	xmlstring += "<sw_lon>" + sw_lat_lon.lng() + "</sw_lon>";
	xmlstring += "<ne_lat>" + ne_lat_lon.lat() + "</ne_lat>";
	xmlstring += "<ne_lon>" + ne_lat_lon.lng() + "</ne_lon>";
	xmlstring += "<lng>" + g_lng + "</lng>";
	xmlstring += "<current_ext_km>" + tmp_distance + "</current_ext_km>";
	xmlstring += "</poirequest>";
    //alert( xmlstring );
	
	showhide_waiting(true);
	var fill_rt_val = false;
	//alert("show poi");
	if ( obj_check.checked )
		{ //da attivare
		//alert( poi_active_category.length );
		if ( poi_active_category.length > 0 )
			{
			
			for ( i = 0; i < poi_active_category.length; i++ )
				{
				//alert( poi_db_category_index[ i ] );
				
				if ( poi_db_category_index[ i ] == dbcategory_index )
					{
					poi_active_category[ i ] = 1;
					poi_db_category_index[ i ] = dbcategory_index;
					my_cat_index = i;
					//alert( my_cat_index );

					//if ( poi_category_markers.length - 1 <
					fill_rt_val = fill_poi_coord( xmlstring, poi_category_markers[ my_cat_index ], g_poibase_icons_url + icon_image, poi_category_listeners[ my_cat_index ] );
					found = 1;
					break;
					}
				}
			
			//alert( found );
			if ( found == 0 )
				{
				//alert( "Num Cat." + poi_active_category.length );
				//alert( "Num Cat Index." + poi_db_category_index.length );

				var index_to_insert = poi_active_category.length; 
				poi_active_category[ index_to_insert ] = 1;
				poi_db_category_index[ index_to_insert ] = dbcategory_index;
				poi_category_markers[ index_to_insert ] = new Array();
				poi_category_listeners[ index_to_insert ] = new Array();
				fill_rt_val = fill_poi_coord( xmlstring, poi_category_markers[ index_to_insert ], g_poibase_icons_url + icon_image, poi_category_listeners[ index_to_insert ] );
				}
			
			}
			else
				{
				poi_active_category[ 0 ] = 1;
				poi_db_category_index[ 0 ] = dbcategory_index;
				poi_category_markers[ 0 ] = new Array();
				poi_category_listeners[ 0 ] = new Array();

				
				fill_rt_val = fill_poi_coord( xmlstring, poi_category_markers[ 0 ], g_poibase_icons_url + icon_image, poi_category_listeners[ 0 ] );
				}
		
		
		//my_cat_index
		//alert( "Num Cat." + poi_active_category.length );
		//alert( "Num Cat Index." + poi_db_category_index.length );
		if ( !fill_rt_val )
		    {
		    if ( obj_check.checked )
			    obj_check.checked = false;
		    }
		    
		}
		else
			{
			//da eliminare
			//remove overlay
			//alert( poi_active_category.length );
			//alert( poi_db_category_index.length );
			//alert( "remove" );
			for ( i = 0; i < poi_active_category.length; i++ )
				{
				//alert( poi_db_category_index[ i ] );
				
				if ( poi_db_category_index[ i ] == dbcategory_index )
					{
					poi_active_category[ i ] = 0;
					//poi_db_category_index[ i ] = dbcategory_index;
					
					//alert( "Elimino catindex" + dbcategory_index + " array index:" + i );
					
					remove_poi_coord( poi_category_markers[ i ], poi_category_listeners[ i ] );
					break;
					}
				}
			}
	showhide_waiting(false);	
    //reset detail info
    var obj_html = document.getElementById("poi_detail_info");
	if ( obj_html != null )
        obj_html.innerHTML = "";
		
}



function set_start_end( splace, s_address, place_ctl_id, address_ctl_id, nnp_ctl_id, nnp_value, b_start )
{
    var place_element = document.getElementById(place_ctl_id);
    var address_element = document.getElementById(address_ctl_id);
    var nnp_element = document.getElementById(nnp_ctl_id);
    
    if ( place_element != null && address_element != null )
        {
	    place_element.value = splace;
	    address_element.value = s_address;
	    if ( nnp_element != null )
	        nnp_element.value = nnp_value;
	    }
    
    var wp_pl_id = null;
    var wp_add_id = null;
    
    
    if ( b_start )
    {
        wp_pl_id = document.getElementById( g_pl_s_id );
        wp_add_id = document.getElementById( g_add_s_id );
        g_start_click_setted = true;
        //wp_pl_id = document.getElementById("ctl00_m_g_df72548c_ed64_4a53_949a_ef890c40eb6f_txt_town_s" );
        //wp_add_id = document.getElementById("ctl00_m_g_df72548c_ed64_4a53_949a_ef890c40eb6f_txt_address_s" );
            
    }
    else
        {
        //wp_pl_id = document.getElementById("ctl00_m_g_df72548c_ed64_4a53_949a_ef890c40eb6f_txt_town_e" );
        //wp_add_id = document.getElementById("ctl00_m_g_df72548c_ed64_4a53_949a_ef890c40eb6f_txt_address_e" );
        
        wp_pl_id = document.getElementById(g_pl_e_id);
        wp_add_id = document.getElementById(g_add_e_id);
        g_end_click_setted = true;
        }

    if ( wp_pl_id != null )
        wp_pl_id.value = splace;
        
    if ( wp_add_id != null )
        wp_add_id.value = s_address;  
}

function treemanager( evento )
{
	var customGetByID;
	var parentID;
	var child;
	if (document.all)
	{
		document.getElementById = document.all;
		parentID = window.event.srcElement.id;
		child = document.getElementById("L2-" + parentID);
	}
	else
	{
		parentID = evento.target.id;
		child = document.getElementById("L2-" + parentID);
	}
	if (child != null)
	{
		var parentImage = document.getElementById(parentID);
		if (child.style.display == "none")
		{
			child.style.display = "block";
			parentImage.src = "/_layouts/images/GiroMilano/css/minus.gif";
			parentImage.alt = "Chiudi";
		}
		else
		{
			child.style.display = "none";
			parentImage.src = "/_layouts/images/GiroMilano/css/plus.gif";
			parentImage.alt = "Apri";
		}
	}
	return( true );
}

function popupgm(sUrl, width, height)
{
  var left= (screen.width/2) - (width/2)
  var top= (screen.height/2) - (height/2)

  var win = window.open(sUrl,"Window1","menubar=no,width=" + width + ", height=" + height + ",toolbar=no" + ",top=" + top + ",left=" + left);
  win.target='new';
}


function nlsbm_click( b_click ){
    g_nl_submt_cl = b_click ? 1 : 0;
}

function get_Scroller_Coords()
{
 var scrollX, scrollY;
      
      if (document.all)
      {
         if (!document.documentElement.scrollLeft)
            scrollX = document.body.scrollLeft;
         else
            scrollX = document.documentElement.scrollLeft;
               
         if (!document.documentElement.scrollTop)
            scrollY = document.body.scrollTop;
         else
            scrollY = document.documentElement.scrollTop;
      }   
      else
      {
         scrollX = window.pageXOffset;
         scrollY = window.pageYOffset;
      }
    
    g_v_pos_val = scrollY;
    var hf;

 
    if ( g_v_pos_fa_hf_id != null )
    {
        hf = document.getElementById(g_v_pos_fa_hf_id);
        if ( hf != null )
            hf.value = scrollY;
    }

    if ( g_v_pos_bw_hf_id != null )
    {
        hf = document.getElementById(g_v_pos_bw_hf_id);
        if ( hf != null )
            hf.value = scrollY;
    }
    
    if ( g_v_pos_nl_hf_id != null )
    {
        hf = document.getElementById(g_v_pos_nl_hf_id);
        if ( hf != null )
            hf.value = scrollY + 15;
    }

    if ( g_v_pos_lv_hf_id != null )
    {
        hf = document.getElementById(g_v_pos_lv_hf_id);
        if ( hf != null )
            hf.value = scrollY;
    }

}


function set_scroll_pos()
{
    //detect on submit
    var hf;
    var found = false;
    

    if ( g_v_pos_fa_hf_id != null )
    {
        hf = document.getElementById(g_v_pos_fa_hf_id);
        if ( hf != null )
        {
            g_v_pos_val = hf.value;
            found = true;
        }
    }
    
    if ( !found && g_v_pos_bw_hf_id != null )
    {
        hf = document.getElementById(g_v_pos_bw_hf_id);
        if ( hf != null )
        {
            g_v_pos_val = hf.value;
            found = true;
        }
    }
    
    if ( !found && g_v_pos_nl_hf_id != null )
    {
        hf = document.getElementById(g_v_pos_nl_hf_id);
        if ( hf != null )
        {
            g_v_pos_val = hf.value;
            found = true;
        }
    }
    
    if ( !found && g_v_pos_lv_hf_id != null )
    {
        hf = document.getElementById(g_v_pos_lv_hf_id);
        if ( hf != null )
        {
            g_v_pos_val = hf.value;
            found = true;
        }
    }
    
    if ( !found && g_v_pos_nav_hf_id != null )
    {
        hf = document.getElementById(g_v_pos_nav_hf_id);
        if ( hf != null )
        {
            g_v_pos_val = hf.value;
            found = true;
        }
    }
    
    
    if ( found && g_v_pos_val > 0 )
        window.scrollTo(0, g_v_pos_val);
    
    //alert (found + " " + g_v_pos_val );
    
    if ( g_v_pos_val == 0 && g_reload_items )
    {
    //search on qry string
        g_v_pos_val = get_param_value("vp");
        if ( g_v_pos_val > 0 )
            window.scrollTo(0, g_v_pos_val);
    }
    
}

function showhide_waiting(b_show) {
    var obj = document.getElementById("waitingbox");
    if (obj == null)
        return;
    
    obj.style.visibility = b_show ? "visible" : "hidden";
    return;
}
/*scroller*/
/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Mr J | http://www.huntingground.net/ */

function toLeft(id){
  document.getElementById(id).scrollLeft=0;
}

function scrollDivLeft(id){
  clearTimeout(gmtimerRight); 
  document.getElementById(id).scrollLeft += gmscrollStep;
  //alert( document.getElementById(id).scrollLeft + " " + gmscrollStep );
  gmtimerRight=setTimeout("scrollDivLeft('"+id+"')",10)
//alert( timerRight + " " + document.getElementById(id).scrollLeft );
}

function scrollDivRight(id){
  clearTimeout(gmtimerLeft);
  document.getElementById(id).scrollLeft -= gmscrollStep;
  gmtimerLeft=setTimeout("scrollDivRight('"+id+"')",10);
}

function toRight(id){
  document.getElementById(id).scrollLeft=document.getElementById(id).scrollWidth;
}

function stopMe(){
  clearTimeout(gmtimerRight);
  clearTimeout(gmtimerLeft);
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
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; for(i=0; i<a.length; i++)
   if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
/*end scroller*/
