﻿// JScript File

//<![CDATA[
//function Map () {


function initialize() {


    //if (GBrowserIsCompatible()) {
		
		

      map = new google.maps.Map2(document.getElementById("map_g"));
      map.addControl(new GLargeMapControl());
      //map.addControl(new GMapTypeControl());
      map.setCenter(new google.maps.LatLng(65, 25), 5);
      geocoder = new GClientGeocoder();
      map.getContainer().style.overflow="hidden";
      // arrays to hold copies of the markers and html used by the side_bar
      // because the function closure trick doesnt work there
      var gmarkers = [];
      var htmls = [];
      i = 0;
      j = 0;
             
            
      
        

      // A function to create the marker and set up the event window
      //function createMarker(point,name,html, icon) {
//        var marker = new GMarker(point, icon);
//        GEvent.addListener(marker, "click", function() {
//          marker.openInfoWindowHtml(html);
//          document.getElementById("gm_info").innerHTML = html;
//        });
//        // save the info we need to use later for the side_bar
////        gmarkers[i] = marker;
////        htmls[i] = html;
//
////        i++;
//        return marker;
//      }
      
      // This function picks up the click and opens the corresponding info window
//      function myclick(i) {
//        gmarkers[i].openInfoWindowHtml(htmls[i]);
//      }
      
      // Create our "tiny" marker icon
        //var icon = new GIcon(G_DEFAULT_ICON);
        //icon = new GIcon(G_DEFAULT_ICON, "../layout/icon_test.png");
        //tinyIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
        //icon.iconSize = new GSize(20, 20);
        //tinyIcon.shadowSize = new GSize(22, 20);
        //icon.iconAnchor = new GPoint(6, 20);
        //icon.infoWindowAnchor = new GPoint(5, 1);

        // Set up our GMarkerOptions object literal
        //markerOptions = { icon:icon };

      
      // create the map
      
       
      
      places=[];
      var bounds = new GLatLngBounds();
      distance=[];
	  points=[];
      //var marker=[];


      // Read the data from asemat.xml
      var request = GXmlHttp.create();
      request.open("GET", "xml/center_corp.xml", true);
      request.onreadystatechange = function() {
        if (request.readyState == 4) {
          var xmlDoc = GXml.parse(request.responseText);
          // obtain the array of markers and loop through it
		  
															
          var markers = xmlDoc.documentElement.getElementsByTagName("marker");
          
          for (var i = 0; i < markers.length; i++) {
			  
            // obtain the attribues of each marker
            var lat = parseFloat(markers[i].getAttribute("lat"));
            var lng = parseFloat(markers[i].getAttribute("lng"));
            var point = new GLatLng(lat,lng);
            var html = "<div class='gm_info_window'"
            html += "onmouseover=this.style.cursor='pointer';  "
            html += ">"

            html += "<b>" + markers[i].getAttribute("name") + "</b>";
			if (markers[i].getAttribute("address") != "") {
                html += "<br/>" + markers[i].getAttribute("address");
            }
			if (markers[i].getAttribute("pob") != "") {
                html += "<br/>" + markers[i].getAttribute("pob");
            }
			if (markers[i].getAttribute("postnumber") != "") {
                html += "<br/>" + markers[i].getAttribute("postnumber") + " ";
            }
			if (markers[i].getAttribute("city") != "") {
				if (markers[i].getAttribute("postnumber") == "") {
                	html += "<br/>";
            	}
                html += markers[i].getAttribute("city");
            }
			
            if (markers[i].getAttribute("phone") != "") {
                html += "<br/>" + markers[i].getAttribute("phone");
            }
            //if (markers[i].getAttribute("cellphone") != "") {
//                html += "<br/>" + markers[i].getAttribute("cellphone");
//            } 
            if (markers[i].getAttribute("email") != "") {
                html += "<br/>" + markers[i].getAttribute("email");
            } 
            if (markers[i].getAttribute("www") != "") {
                html += "<br/><a target='_blank' href='http://" + markers[i].getAttribute("www") + "'>" + markers[i].getAttribute("www") + "</a>";
            } 
            html += "</div>";
            html = html.replace(/\[br]/ig, "<br />");
			html = html.replace(/!/g, "&")
            var service = markers[i].getAttribute("service");
			var parts = markers[i].getAttribute("parts");
			var tractors = markers[i].getAttribute("tractors");
			var id  = markers[i].getAttribute("id");
			var center = markers[i].getAttribute("center");
			var corp = markers[i].getAttribute("corp");
//            var town = markers[i].getAttribute("city");
            
            var icon;
			
			if (markers[i].getAttribute("tractors") == 'True' && markers[i].getAttribute("parts") == 'True' && markers[i].getAttribute("service") == 'False') {
					icon = new GIcon(G_DEFAULT_ICON, "pics/sales&parts.png");
					icon.shadow = "pics/shadow-parts.png"
					icon.iconSize = new GSize(25, 25);
					icon.iconAnchor = new GPoint(12, 12);
					icon.shadowSize = new GSize(38, 25);
					icon.infoWindowAnchor = new GPoint(12, 12);
			} else if (markers[i].getAttribute("tractors") == 'True' && markers[i].getAttribute("parts") == 'False' && markers[i].getAttribute("service") == 'False') {
					icon = new GIcon(G_DEFAULT_ICON, "pics/sales.png");
					icon.shadow = "pics/shadow-parts.png"
					icon.iconSize = new GSize(25, 25);
					icon.iconAnchor = new GPoint(12, 12);
					icon.shadowSize = new GSize(38, 25);
					icon.infoWindowAnchor = new GPoint(12, 12);
			} else if (markers[i].getAttribute("service") == 'True' && markers[i].getAttribute("tractors") == 'False' && markers[i].getAttribute("parts") == 'False') {
					icon = new GIcon(G_DEFAULT_ICON, "pics/service.png");
					icon.shadow = "pics/shadow-parts.png"
					icon.iconSize = new GSize(25, 25);
					icon.iconAnchor = new GPoint(12, 12);
					icon.shadowSize = new GSize(38, 25);
					icon.infoWindowAnchor = new GPoint(12, 12);
			} else if (markers[i].getAttribute("parts") == 'True' && markers[i].getAttribute("service") == 'False' && markers[i].getAttribute("tractors") == 'False') {
					icon = new GIcon(G_DEFAULT_ICON, "pics/parts.png");
					icon.shadow = "pics/shadow-parts.png"
					icon.iconSize = new GSize(25, 25);
					icon.iconAnchor = new GPoint(12, 12);
					icon.shadowSize = new GSize(38, 25);
					icon.infoWindowAnchor = new GPoint(12, 12);
			} else if (markers[i].getAttribute("tractors") == 'True' && markers[i].getAttribute("parts") == 'True' && markers[i].getAttribute("service") == 'True') {
					icon = new GIcon(G_DEFAULT_ICON, "pics/service&parts&sales.png");
					icon.shadow = "pics/shadow-service&parts&sales.png";
					icon.iconSize = new GSize(66.0, 30.0);
					icon.shadowSize = new GSize(82.0, 30.0);
					icon.iconAnchor = new GPoint(33.0, 15.0);
					icon.infoWindowAnchor = new GPoint(33.0, 15.0);

			} else if (markers[i].getAttribute("tractors") == 'True' && markers[i].getAttribute("parts") == 'False' && markers[i].getAttribute("service") == 'True') {
					icon = new GIcon(G_DEFAULT_ICON, "pics/service&sales.png");
					icon.shadow = "pics/shadow-service&parts.png";
					icon.iconSize = new GSize(60.0, 30.0);
					icon.shadowSize = new GSize(76.0, 30.0);
					icon.iconAnchor = new GPoint(30.0, 15.0);
					icon.infoWindowAnchor = new GPoint(30.0, 15.0);

			} else if (markers[i].getAttribute("tractors") == 'False' && markers[i].getAttribute("parts") == 'True' && markers[i].getAttribute("service") == 'True') {
					icon = new GIcon(G_DEFAULT_ICON, "pics/service&parts.png");
					icon.shadow = "pics/shadow-service&parts.png";
					icon.iconSize = new GSize(60.0, 30.0);
					icon.shadowSize = new GSize(76.0, 30.0);
					icon.iconAnchor = new GPoint(30.0, 15.0);
					icon.infoWindowAnchor = new GPoint(30.0, 15.0);
			}
            
			
			var tr;
			var pa;
			var se;
			var addr;
//            var icon = new GIcon(G_DEFAULT_ICON, "../layout/seo_icon.png");
//            icon.shadow = "../layout/seo_icon_shadow.png"
//            icon.iconSize = new GSize(30, 14);
//            icon.iconAnchor = new GPoint(15, 7);
//            icon.shadowSize = new GSize(38.0, 14.0);
//            icon.infoWindowAnchor = new GPoint(15.0, 7.0);
            
            places.push({point:point,html:html,icon:icon,id:id,corp:corp,parts:parts,service:service,tractors:tractors}); 
            // create the marker
//            var marker = createMarker(places[i].point,places[i].label,places[i].html, places[i].icon);
//            map.addOverlay(marker);
            
          }
          if (google.loader.ClientLocation) {
            latlng = new google.maps.LatLng(google.loader.ClientLocation.latitude, google.loader.ClientLocation.longitude);
			tr = '3';
			pa = '3';
			se = '3';
			addr = '';
            searchNear(latlng,tr,pa,se,addr);
          } else {
            latlng = new google.maps.LatLng(62.54420, 25.90153);
			tr = '2';
			pa = '2';
			se = '2';
			addr = '';
            searchNear(latlng,tr,pa,se,addr);
          }
          //alert(places[3].label);
        }
      }
      request.send(null);
    

    /*}else {
      alert("Sorry, the Google Maps API is not compatible with this browser");
    }*/
  }  
  
//  if (google.loader.ClientLocation) {
//  
//              //zoom = 8;
//              
//              }
//              else
//              {
//              latlng = new google.maps.LatLng(65, 25);
//              zoom = 13;
//              }

  function addClick(marker,n,address,tr,pa,se,sid) {
                 
                GEvent.addListener(marker, "click", function() {
                
				if (sid != '') {
					marker.openInfoWindowHtml(places[n].html2);
				} else {
					marker.openInfoWindowHtml(places[n].html);
				}

					
				  viewPersons(places[n].id,places[n].corp,address,tr,pa,se,sid);
                });
  }
  
  function searchNear(point,tr,pa,se,address) {
            map.clearOverlays();
			var dist;
            //document.getElementById("gm_info").innerHTML = "";
			if (tr=='2' && pa=='2' && se=='2' || tr=='4' && pa=='4' && se=='4' || tr=='5' && pa=='5' && se=='5') {
				map.setCenter(point,5);
				dist = 1000000;
			} else {
            	map.setCenter(point,7);
				dist = 100000;
			}
			
			if (tr == '1' && address == 'VALTIMO') {
				map.setCenter(point,7);
				dist = 80000;
			}
			
			points.length=0;
			
			var sid = '';
			var chkFound;
			chkFound = 0;
			var chkPaTr;
			chkPaTr = 0;
			var tronly;
			tronly = 0;
			var testStr = '';
			//document.getElementById('addr').value = address;
			 
            //alert(point.distanceFrom(places[8].point));
            for(i=0;i<places.length;i++) {
            
                distance[i] = point.distanceFrom(places[i].point);
                
                if (distance[i] < dist) {
					
					//alert(document.getElementById('sid').value);
					if (tr == '1' && pa == '0' && se == '1') {
						
						if (places[i].tractors == 'True' || places[i].service == 'True') {
							sid = '';
							for (var g=0;g<points.length;g++) {
								//alert(points[g].point + '  ' + places[i].point);
								if (points[g].point + '' == places[i].point + '') {
									icon = new GIcon(G_DEFAULT_ICON, "pics/service&sales.png");
									icon.shadow = "pics/shadow-service&parts.png";
									icon.iconSize = new GSize(60.0, 30.0);
									icon.shadowSize = new GSize(76.0, 30.0);
									icon.iconAnchor = new GPoint(30.0, 15.0);
									icon.infoWindowAnchor = new GPoint(30.0, 15.0);
									places[i].icon = icon;
									
									places[i].html2 = places[points[g].f].html + '<br/>' + places[i].html;
									sid = places[points[g].f].id;
									
									chkKommun(places[i].id,places[i].point,places[i].icon,i,address,tr,pa,se,sid);
									chkFound = 1;
								}
							}
							if (places[i].tractors == 'True') {	 
								points.push({point:places[i].point,f:i});	 
								
								chkPaTr = 0;
								//chkKommun(places[i].id,places[i].point,places[i].icon,i,address,tr,pa,se);
								
							}
						}
					} else if (tr == '1' && pa == '1' && se == '0') {
						
						if (places[i].tractors == 'True' && places[i].parts == 'True') {
							//alert('1');
							//points.push({point:places[i].point,f:i});
							
							chkPaTr = 1;
							chkFound = 1;
							chkKommun(places[i].id,places[i].point,places[i].icon,i,address,tr,pa,se,sid);
						} else if (places[i].tractors == 'True' || places[i].parts == 'True') {
							
							sid = '';
							for (var g=0;g<points.length;g++) {
							//alert(points[g].point + '  ' + places[i].point);
								if (points[g].point + '' == places[i].point + '') {
									
	
									if (places[points[g].f].tractors == 'True' && places[i].parts == 'True' || places[i].tractors == 'True' && places[points[g].f].parts == 'True') {
										icon = new GIcon(G_DEFAULT_ICON, "pics/sales&parts.png");
										icon.shadow = "pics/shadow-parts.png"
										icon.iconSize = new GSize(25, 25);
										icon.iconAnchor = new GPoint(12, 12);
										icon.shadowSize = new GSize(38, 25);
										icon.infoWindowAnchor = new GPoint(12, 12);
										places[i].icon = icon;
									}
									
									
									places[i].html2 = places[points[g].f].html + '<br/>' + places[i].html;
									places[i].sid = places[points[g].f].id;
									sid = places[points[g].f].id;
										 
									tronly = 0;
									chkPaTr = 1;
									var marker = new GMarker(places[i].point, places[i].icon);
									map.addOverlay(marker);
									addClick(marker,i,address,tr,pa,se,sid);
									chkFound = 1;
								}
							
							}
						
							
						/*points.push({point:places[i].point,f:i});	 
						tronly = 0;
						chkPaTr = 0;
						var marker = new GMarker(places[i].point, places[i].icon);
						map.addOverlay(marker);
						addClick(marker,i,address,tr,pa,se,sid);
						chkFound = 1;*/
						 // if tr pa se end
						}
						points.push({point:places[i].point,f:i});
					} else if (tr == '0' && pa == '1' && se == '1') {
						
						if (places[i].service == 'True' && places[i].parts == 'True') {
							//alert('1');
							//points.push({point:places[i].point,f:i});
							
							chkPaTr = 1;
							chkFound = 1;
							var marker = new GMarker(places[i].point, places[i].icon);
							map.addOverlay(marker);
							addClick(marker,i,address,tr,pa,se,sid);
						} else if (places[i].service == 'True' || places[i].parts == 'True') {
							
							sid = '';
							for (var g=0;g<points.length;g++) {
							//alert(points[g].point + '  ' + places[i].point);
								if (points[g].point + '' == places[i].point + '') {
									
	
									if (places[points[g].f].service == 'True' && places[i].parts == 'True' || places[i].service == 'True' && places[points[g].f].parts == 'True') {
										icon = new GIcon(G_DEFAULT_ICON, "pics/service&parts.png");
										icon.shadow = "pics/shadow-service&parts.png";
										icon.iconSize = new GSize(60.0, 30.0);
										icon.shadowSize = new GSize(76.0, 30.0);
										icon.iconAnchor = new GPoint(30.0, 15.0);
										icon.infoWindowAnchor = new GPoint(30.0, 15.0);
										places[i].icon = icon;
									}
									
									
									places[i].html2 = places[points[g].f].html + '<br/>' + places[i].html;
									places[i].sid = places[points[g].f].id;
									sid = places[points[g].f].id;
										 
									tronly = 0;
									chkPaTr = 1;
									var marker = new GMarker(places[i].point, places[i].icon);
									map.addOverlay(marker);
									addClick(marker,i,address,tr,pa,se,sid);
									chkFound = 1;
								}
							
							}
						
							
						/*points.push({point:places[i].point,f:i});	 
						tronly = 0;
						chkPaTr = 0;
						var marker = new GMarker(places[i].point, places[i].icon);
						map.addOverlay(marker);
						addClick(marker,i,address,tr,pa,se,sid);
						chkFound = 1;*/
						 // if tr pa se end
						}
						points.push({point:places[i].point,f:i});
					} else if (tr == '1' && places[i].tractors == 'True' && pa != '1' && se != '1') {
						sid = '';
						points.push({point:places[i].point,f:i});
						tronly = 1;
						chkPaTr = 0;
						chkFound = 1;
						
						chkKommun(places[i].id,places[i].point,places[i].icon,i,address,tr,pa,se,sid);

					} else if (pa == '1' && places[i].parts == 'True' && tr == '0' && se == '0') {
						sid = '';
						points.push({point:places[i].point,f:i});
						tronly = 0;
						chkPaTr = 0;
						var marker = new GMarker(places[i].point, places[i].icon);
						map.addOverlay(marker);
						addClick(marker,i,address,tr,pa,se,sid);
						chkFound = 1;
					} else if (se == '1' && places[i].service == 'True' && tr == '0' && pa == '0') {
						sid = '';
						points.push({point:places[i].point,f:i});
						tronly = 0;
						chkPaTr = 0;
						var marker = new GMarker(places[i].point, places[i].icon);
						map.addOverlay(marker);
						addClick(marker,i,address,tr,pa,se,sid);
						chkFound = 1;
					} else if (se == '0' && pa == '0' && tr == '0' || se == '3' && pa == '3' && tr == '3') {
						
						sid = '';
						for (var g=0;g<points.length;g++) {
							//alert(points[g].point + '  ' + places[i].point);
							if (points[g].point + '' == places[i].point + '') {
								
								if (places[points[g].f].tractors == 'True' && places[i].service == 'True' && places[points[g].f].parts == 'True' || places[points[g].f].tractors == 'True' && places[i].service == 'True' && places[i].parts == 'True' || places[i].tractors == 'True' && places[points[g].f].service == 'True' && places[points[g].f].parts == 'True') {
								
									icon = new GIcon(G_DEFAULT_ICON, "pics/service&parts&sales.png");
									icon.shadow = "pics/shadow-service&parts&sales.png";
									icon.iconSize = new GSize(66.0, 30.0);
									icon.shadowSize = new GSize(82.0, 30.0);
									icon.iconAnchor = new GPoint(33.0, 15.0);
									icon.infoWindowAnchor = new GPoint(33.0, 15.0);
									places[i].icon = icon;
								} else if (places[points[g].f].tractors == 'True' && places[i].service == 'True' && places[points[g].f].parts == 'False') {
									icon = new GIcon(G_DEFAULT_ICON, "pics/service&sales.png");
									icon.shadow = "pics/shadow-service&parts.png";
									icon.iconSize = new GSize(60.0, 30.0);
									icon.shadowSize = new GSize(76.0, 30.0);
									icon.iconAnchor = new GPoint(30.0, 15.0);
									icon.infoWindowAnchor = new GPoint(30.0, 15.0);
									places[i].icon = icon;
								} else if (places[points[g].f].tractors == 'False' && places[i].service == 'True' && places[points[g].f].parts == 'True') {
									icon = new GIcon(G_DEFAULT_ICON, "pics/service&parts.png");
									icon.shadow = "pics/shadow-service&parts.png";
									icon.iconSize = new GSize(60.0, 30.0);
									icon.shadowSize = new GSize(76.0, 30.0);
									icon.iconAnchor = new GPoint(30.0, 15.0);
									icon.infoWindowAnchor = new GPoint(30.0, 15.0);
									places[i].icon = icon;
								}
								
								
								places[i].html2 = places[points[g].f].html + '<br/>' + places[i].html;
								places[i].sid = places[points[g].f].id;
								sid = places[points[g].f].id;
								
							}
						}
						
						
						points.push({point:places[i].point,f:i});	 
						tronly = 0;
						chkPaTr = 0;
						var marker = new GMarker(places[i].point, places[i].icon);
						map.addOverlay(marker);
						addClick(marker,i,address,tr,pa,se,sid);
						chkFound = 1;
						 // if tr pa se end
					} else if (se == '2' && pa == '2' && tr == '2' && places[i].tractors == 'True') {
							
							chkPaTr = 0;
							var marker = new GMarker(places[i].point, places[i].icon);
							map.addOverlay(marker);
							addClick(marker,i,address,tr,pa,se,sid);
							chkFound = 1;
					} else if (se == '4' && pa == '4' && tr == '4' && places[i].parts == 'True') {
							
							chkPaTr = 0;
							var marker = new GMarker(places[i].point, places[i].icon);
							map.addOverlay(marker);
							addClick(marker,i,address,tr,pa,se,sid);
							chkFound = 1;
					} else if (se == '5' && pa == '5' && tr == '5' && places[i].service == 'True') {
							
							chkPaTr = 0;
							var marker = new GMarker(places[i].point, places[i].icon);
							map.addOverlay(marker);
							addClick(marker,i,address,tr,pa,se,sid);
							chkFound = 1;
					} else if (se == '1' && pa == '1' && tr == '1') {
						/*	sid = '';
						for (var g=0;g<points.length;g++) {
							//alert(points[g].point + '  ' + places[i].point);
							if (points[g].point + '' == places[i].point + '') {
								
								if (places[points[g].f].tractors == 'True' && places[i].service == 'True' && places[points[g].f].parts == 'True' || places[points[g].f].tractors == 'True' && places[i].service == 'True' && places[i].parts == 'True' || places[i].tractors == 'True' && places[points[g].f].service == 'True' && places[points[g].f].parts == 'True') {
								
									icon = new GIcon(G_DEFAULT_ICON, "pics/service&parts&sales.png");
									icon.shadow = "pics/shadow-service&parts&sales.png";
									icon.iconSize = new GSize(66.0, 30.0);
									icon.shadowSize = new GSize(82.0, 30.0);
									icon.iconAnchor = new GPoint(33.0, 15.0);
									icon.infoWindowAnchor = new GPoint(33.0, 15.0);
									places[i].icon = icon;
								}
								
								
								places[i].html2 = places[points[g].f].html + '<br/>' + places[i].html;
								places[i].sid = places[points[g].f].id;
								sid = places[points[g].f].id;
								var marker = new GMarker(places[i].point, places[i].icon);
								map.addOverlay(marker);
								addClick(marker,i,address,tr,pa,se,sid);
								chkFound = 1;
							}
						}
						
						
						points.push({point:places[i].point,f:i});	 
						tronly = 0;
						chkPaTr = 0;*/
						
						sid = '';
						for (var g=0;g<points.length;g++) {
							//alert(points[g].point + '  ' + places[i].point);
							if (points[g].point + '' == places[i].point + '') {
								
								if (places[points[g].f].tractors == 'True' && places[i].service == 'True' && places[points[g].f].parts == 'True' || places[points[g].f].tractors == 'True' && places[i].service == 'True' && places[i].parts == 'True' || places[i].tractors == 'True' && places[points[g].f].service == 'True' && places[points[g].f].parts == 'True') {
								
									icon = new GIcon(G_DEFAULT_ICON, "pics/service&parts&sales.png");
									icon.shadow = "pics/shadow-service&parts&sales.png";
									icon.iconSize = new GSize(66.0, 30.0);
									icon.shadowSize = new GSize(82.0, 30.0);
									icon.iconAnchor = new GPoint(33.0, 15.0);
									icon.infoWindowAnchor = new GPoint(33.0, 15.0);
									places[i].icon = icon;
								} else if (places[points[g].f].tractors == 'True' && places[i].service == 'True' && places[points[g].f].parts == 'False') {
									icon = new GIcon(G_DEFAULT_ICON, "pics/service&sales.png");
									icon.shadow = "pics/shadow-service&parts.png";
									icon.iconSize = new GSize(60.0, 30.0);
									icon.shadowSize = new GSize(76.0, 30.0);
									icon.iconAnchor = new GPoint(30.0, 15.0);
									icon.infoWindowAnchor = new GPoint(30.0, 15.0);
									places[i].icon = icon;
								} else if (places[points[g].f].tractors == 'False' && places[i].service == 'True' && places[points[g].f].parts == 'True') {
									icon = new GIcon(G_DEFAULT_ICON, "pics/service&parts.png");
									icon.shadow = "pics/shadow-service&parts.png";
									icon.iconSize = new GSize(60.0, 30.0);
									icon.shadowSize = new GSize(76.0, 30.0);
									icon.iconAnchor = new GPoint(30.0, 15.0);
									icon.infoWindowAnchor = new GPoint(30.0, 15.0);
									places[i].icon = icon;
								}
								
								
								places[i].html2 = places[points[g].f].html + '<br/>' + places[i].html;
								places[i].sid = places[points[g].f].id;
								sid = places[points[g].f].id;
								
							}
						}
						
						
						points.push({point:places[i].point,f:i});	 
						tronly = 0;
						chkPaTr = 0;
						var marker = new GMarker(places[i].point, places[i].icon);
						map.addOverlay(marker);
						addClick(marker,i,address,tr,pa,se,sid);
						chkFound = 1;
						 // if tr pa se end
						
					}
                
                }
                
            } // for ends
			
			if (chkFound == 0) {
				map.setCenter(point,4);
				
				for(i=0;i<places.length;i++) {
					distance[i] = point.distanceFrom(places[i].point);
					if (distance[i] < 400000) {
					
					
							if (tr == '1' && pa == '0' && se == '1') {
						
						if (places[i].tractors == 'True' || places[i].service == 'True') {
							sid = '';
							for (var g=0;g<points.length;g++) {
								//alert(points[g].point + '  ' + places[i].point);
								if (points[g].point + '' == places[i].point + '') {
									icon = new GIcon(G_DEFAULT_ICON, "pics/service&sales.png");
									icon.shadow = "pics/shadow-service&parts.png";
									icon.iconSize = new GSize(60.0, 30.0);
									icon.shadowSize = new GSize(76.0, 30.0);
									icon.iconAnchor = new GPoint(30.0, 15.0);
									icon.infoWindowAnchor = new GPoint(30.0, 15.0);
									places[i].icon = icon;
									
									places[i].html2 = places[points[g].f].html + '<br/>' + places[i].html;
									sid = places[points[g].f].id;
									
									chkKommun(places[i].id,places[i].point,places[i].icon,i,address,tr,pa,se,sid);
									chkFound = 1;
								}
							}
							if (places[i].tractors == 'True') {	 
								points.push({point:places[i].point,f:i});	 
								
								chkPaTr = 0;
								//chkKommun(places[i].id,places[i].point,places[i].icon,i,address,tr,pa,se);
								
							}
						}
					} else if (tr == '1' && pa == '1' && se == '0') {
						
						if (places[i].tractors == 'True' && places[i].parts == 'True') {
							//alert('1');
							//points.push({point:places[i].point,f:i});
							
							chkPaTr = 1;
							chkFound = 1;
							chkKommun(places[i].id,places[i].point,places[i].icon,i,address,tr,pa,se,sid);
						} else if (places[i].tractors == 'True' || places[i].parts == 'True') {
							
							sid = '';
							for (var g=0;g<points.length;g++) {
							//alert(points[g].point + '  ' + places[i].point);
								if (points[g].point + '' == places[i].point + '') {
									
	
									if (places[points[g].f].tractors == 'True' && places[i].parts == 'True' || places[i].tractors == 'True' && places[points[g].f].parts == 'True') {
										icon = new GIcon(G_DEFAULT_ICON, "pics/sales&parts.png");
										icon.shadow = "pics/shadow-parts.png"
										icon.iconSize = new GSize(25, 25);
										icon.iconAnchor = new GPoint(12, 12);
										icon.shadowSize = new GSize(38, 25);
										icon.infoWindowAnchor = new GPoint(12, 12);
										places[i].icon = icon;
									}
									
									
									places[i].html2 = places[points[g].f].html + '<br/>' + places[i].html;
									places[i].sid = places[points[g].f].id;
									sid = places[points[g].f].id;
										 
									tronly = 0;
									chkPaTr = 1;
									var marker = new GMarker(places[i].point, places[i].icon);
									map.addOverlay(marker);
									addClick(marker,i,address,tr,pa,se,sid);
									chkFound = 1;
								}
							
							}
						
							
						/*points.push({point:places[i].point,f:i});	 
						tronly = 0;
						chkPaTr = 0;
						var marker = new GMarker(places[i].point, places[i].icon);
						map.addOverlay(marker);
						addClick(marker,i,address,tr,pa,se,sid);
						chkFound = 1;*/
						 // if tr pa se end
						}
						points.push({point:places[i].point,f:i});
					} else if (tr == '0' && pa == '1' && se == '1') {
						
						if (places[i].service == 'True' && places[i].parts == 'True') {
							//alert('1');
							//points.push({point:places[i].point,f:i});
							
							chkPaTr = 1;
							chkFound = 1;
							var marker = new GMarker(places[i].point, places[i].icon);
							map.addOverlay(marker);
							addClick(marker,i,address,tr,pa,se,sid);
						} else if (places[i].service == 'True' || places[i].parts == 'True') {
							
							sid = '';
							for (var g=0;g<points.length;g++) {
							//alert(points[g].point + '  ' + places[i].point);
								if (points[g].point + '' == places[i].point + '') {
									
	
									if (places[points[g].f].service == 'True' && places[i].parts == 'True' || places[i].service == 'True' && places[points[g].f].parts == 'True') {
										icon = new GIcon(G_DEFAULT_ICON, "pics/service&parts.png");
										icon.shadow = "pics/shadow-service&parts.png";
										icon.iconSize = new GSize(60.0, 30.0);
										icon.shadowSize = new GSize(76.0, 30.0);
										icon.iconAnchor = new GPoint(30.0, 15.0);
										icon.infoWindowAnchor = new GPoint(30.0, 15.0);
										places[i].icon = icon;
									}
									
									
									places[i].html2 = places[points[g].f].html + '<br/>' + places[i].html;
									places[i].sid = places[points[g].f].id;
									sid = places[points[g].f].id;
										 
									tronly = 0;
									chkPaTr = 1;
									var marker = new GMarker(places[i].point, places[i].icon);
									map.addOverlay(marker);
									addClick(marker,i,address,tr,pa,se,sid);
									chkFound = 1;
								}
							
							}
						
							
						/*points.push({point:places[i].point,f:i});	 
						tronly = 0;
						chkPaTr = 0;
						var marker = new GMarker(places[i].point, places[i].icon);
						map.addOverlay(marker);
						addClick(marker,i,address,tr,pa,se,sid);
						chkFound = 1;*/
						 // if tr pa se end
						}
						points.push({point:places[i].point,f:i});
					} else if (tr == '1' && places[i].tractors == 'True' && pa != '1' && se != '1') {
						sid = '';
						points.push({point:places[i].point,f:i});
						tronly = 1;
						chkPaTr = 0;
						chkFound = 1;
						
						chkKommun(places[i].id,places[i].point,places[i].icon,i,address,tr,pa,se,sid);

					} else if (pa == '1' && places[i].parts == 'True' && tr == '0' && se == '0') {
						sid = '';
						points.push({point:places[i].point,f:i});
						tronly = 0;
						chkPaTr = 0;
						var marker = new GMarker(places[i].point, places[i].icon);
						map.addOverlay(marker);
						addClick(marker,i,address,tr,pa,se,sid);
						chkFound = 1;
					} else if (se == '1' && places[i].service == 'True' && tr == '0' && pa == '0') {
						sid = '';
						points.push({point:places[i].point,f:i});
						tronly = 0;
						chkPaTr = 0;
						var marker = new GMarker(places[i].point, places[i].icon);
						map.addOverlay(marker);
						addClick(marker,i,address,tr,pa,se,sid);
						chkFound = 1;
					} else if (se == '0' && pa == '0' && tr == '0' || se == '3' && pa == '3' && tr == '3') {
						
						sid = '';
						for (var g=0;g<points.length;g++) {
							//alert(points[g].point + '  ' + places[i].point);
							if (points[g].point + '' == places[i].point + '') {
								
								if (places[points[g].f].tractors == 'True' && places[i].service == 'True' && places[points[g].f].parts == 'True' || places[points[g].f].tractors == 'True' && places[i].service == 'True' && places[i].parts == 'True' || places[i].tractors == 'True' && places[points[g].f].service == 'True' && places[points[g].f].parts == 'True') {
								
									icon = new GIcon(G_DEFAULT_ICON, "pics/service&parts&sales.png");
									icon.shadow = "pics/shadow-service&parts&sales.png";
									icon.iconSize = new GSize(66.0, 30.0);
									icon.shadowSize = new GSize(82.0, 30.0);
									icon.iconAnchor = new GPoint(33.0, 15.0);
									icon.infoWindowAnchor = new GPoint(33.0, 15.0);
									places[i].icon = icon;
								} else if (places[points[g].f].tractors == 'True' && places[i].service == 'True' && places[points[g].f].parts == 'False') {
									icon = new GIcon(G_DEFAULT_ICON, "pics/service&sales.png");
									icon.shadow = "pics/shadow-service&parts.png";
									icon.iconSize = new GSize(60.0, 30.0);
									icon.shadowSize = new GSize(76.0, 30.0);
									icon.iconAnchor = new GPoint(30.0, 15.0);
									icon.infoWindowAnchor = new GPoint(30.0, 15.0);
									places[i].icon = icon;
								} else if (places[points[g].f].tractors == 'False' && places[i].service == 'True' && places[points[g].f].parts == 'True') {
									icon = new GIcon(G_DEFAULT_ICON, "pics/service&parts.png");
									icon.shadow = "pics/shadow-service&parts.png";
									icon.iconSize = new GSize(60.0, 30.0);
									icon.shadowSize = new GSize(76.0, 30.0);
									icon.iconAnchor = new GPoint(30.0, 15.0);
									icon.infoWindowAnchor = new GPoint(30.0, 15.0);
									places[i].icon = icon;
								}
								
								
								places[i].html2 = places[points[g].f].html + '<br/>' + places[i].html;
								places[i].sid = places[points[g].f].id;
								sid = places[points[g].f].id;
								
							}
						}
						
						
						points.push({point:places[i].point,f:i});	 
						tronly = 0;
						chkPaTr = 0;
						var marker = new GMarker(places[i].point, places[i].icon);
						map.addOverlay(marker);
						addClick(marker,i,address,tr,pa,se,sid);
						chkFound = 1;
						 // if tr pa se end
					} else if (se == '2' && pa == '2' && tr == '2' && places[i].tractors == 'True') {
							
							chkPaTr = 0;
							var marker = new GMarker(places[i].point, places[i].icon);
							map.addOverlay(marker);
							addClick(marker,i,address,tr,pa,se,sid);
							chkFound = 1;
					} else if (se == '4' && pa == '4' && tr == '4' && places[i].parts == 'True') {
							
							chkPaTr = 0;
							var marker = new GMarker(places[i].point, places[i].icon);
							map.addOverlay(marker);
							addClick(marker,i,address,tr,pa,se,sid);
							chkFound = 1;
					} else if (se == '5' && pa == '5' && tr == '5' && places[i].service == 'True') {
							
							chkPaTr = 0;
							var marker = new GMarker(places[i].point, places[i].icon);
							map.addOverlay(marker);
							addClick(marker,i,address,tr,pa,se,sid);
							chkFound = 1;
					} else if (se == '1' && pa == '1' && tr == '1') {
						/*	sid = '';
						for (var g=0;g<points.length;g++) {
							//alert(points[g].point + '  ' + places[i].point);
							if (points[g].point + '' == places[i].point + '') {
								
								if (places[points[g].f].tractors == 'True' && places[i].service == 'True' && places[points[g].f].parts == 'True' || places[points[g].f].tractors == 'True' && places[i].service == 'True' && places[i].parts == 'True' || places[i].tractors == 'True' && places[points[g].f].service == 'True' && places[points[g].f].parts == 'True') {
								
									icon = new GIcon(G_DEFAULT_ICON, "pics/service&parts&sales.png");
									icon.shadow = "pics/shadow-service&parts&sales.png";
									icon.iconSize = new GSize(66.0, 30.0);
									icon.shadowSize = new GSize(82.0, 30.0);
									icon.iconAnchor = new GPoint(33.0, 15.0);
									icon.infoWindowAnchor = new GPoint(33.0, 15.0);
									places[i].icon = icon;
								}
								
								
								places[i].html2 = places[points[g].f].html + '<br/>' + places[i].html;
								places[i].sid = places[points[g].f].id;
								sid = places[points[g].f].id;
								var marker = new GMarker(places[i].point, places[i].icon);
								map.addOverlay(marker);
								addClick(marker,i,address,tr,pa,se,sid);
								chkFound = 1;
							}
						}
						
						
						points.push({point:places[i].point,f:i});	 
						tronly = 0;
						chkPaTr = 0;*/
						
						sid = '';
						for (var g=0;g<points.length;g++) {
							//alert(points[g].point + '  ' + places[i].point);
							if (points[g].point + '' == places[i].point + '') {
								
								if (places[points[g].f].tractors == 'True' && places[i].service == 'True' && places[points[g].f].parts == 'True' || places[points[g].f].tractors == 'True' && places[i].service == 'True' && places[i].parts == 'True' || places[i].tractors == 'True' && places[points[g].f].service == 'True' && places[points[g].f].parts == 'True') {
								
									icon = new GIcon(G_DEFAULT_ICON, "pics/service&parts&sales.png");
									icon.shadow = "pics/shadow-service&parts&sales.png";
									icon.iconSize = new GSize(66.0, 30.0);
									icon.shadowSize = new GSize(82.0, 30.0);
									icon.iconAnchor = new GPoint(33.0, 15.0);
									icon.infoWindowAnchor = new GPoint(33.0, 15.0);
									places[i].icon = icon;
								} else if (places[points[g].f].tractors == 'True' && places[i].service == 'True' && places[points[g].f].parts == 'False') {
									icon = new GIcon(G_DEFAULT_ICON, "pics/service&sales.png");
									icon.shadow = "pics/shadow-service&parts.png";
									icon.iconSize = new GSize(60.0, 30.0);
									icon.shadowSize = new GSize(76.0, 30.0);
									icon.iconAnchor = new GPoint(30.0, 15.0);
									icon.infoWindowAnchor = new GPoint(30.0, 15.0);
									places[i].icon = icon;
								} else if (places[points[g].f].tractors == 'False' && places[i].service == 'True' && places[points[g].f].parts == 'True') {
									icon = new GIcon(G_DEFAULT_ICON, "pics/service&parts.png");
									icon.shadow = "pics/shadow-service&parts.png";
									icon.iconSize = new GSize(60.0, 30.0);
									icon.shadowSize = new GSize(76.0, 30.0);
									icon.iconAnchor = new GPoint(30.0, 15.0);
									icon.infoWindowAnchor = new GPoint(30.0, 15.0);
									places[i].icon = icon;
								}
								
								
								places[i].html2 = places[points[g].f].html + '<br/>' + places[i].html;
								places[i].sid = places[points[g].f].id;
								sid = places[points[g].f].id;
								
							}
						}
						
						
						points.push({point:places[i].point,f:i});	 
						tronly = 0;
						chkPaTr = 0;
						var marker = new GMarker(places[i].point, places[i].icon);
						map.addOverlay(marker);
						addClick(marker,i,address,tr,pa,se,sid);
						chkFound = 1;
						 // if tr pa se end
						
					}
					}
	
					}
			}
              
              
  }
  
  function addMarker(point,icon,i,address,tr,pa,se,sid) {
	  	var marker = new GMarker(point, icon);
		
		map.addOverlay(marker);
		addClick(marker,i,address,tr,pa,se,sid);
  }
  
  //listauksen funktiot
//  function go(num){
//    map.setZoom(7);
//    GEvent.trigger(marker[num],"click");
//    }

    var attract;

    function heat(num){
    attract = new GMarker(places[num].point);
    map.addOverlay(attract);
    //map.closeInfoWindow();
    }

    function cool(num){
    map.removeOverlay(attract);
    }

  
  
  //function to show the location of the searched address
    function showAddress(address,tr,pa,se) {
      //var map = new google.maps.Map2(document.getElementById("map"));
	  /*if(tr == '0' && pa == '0' && se == '0') {
		  tr = '1';
		  pa = '1';
		  se = '1';
	  }*/
      if (geocoder) {
        geocoder.getLatLng(
          address + ',finland',
          function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
				personsByKommun(address,tr,pa,se);
            	searchNear(point,tr,pa,se,address);
				
              
            }
          }
        );
      }
    }
	
	function showServicepersons() {
		
		var request = GXmlHttp.create();
		
      	request.open("GET", "xml/test2.xml", true);
	  
      	request.onreadystatechange = function() {
			alert("test");
			if (request.readyState == 4) {
				alert("xml read");
			  var xmlDoc = GXml.parse(request.responseText);
			  // obtain the array of markers and loop through it
			  
																
			  var markers = xmlDoc.documentElement.getElementsByTagName("person");
			
					 /* for (var i = 0; i < markers2.length; i++) {
						  alert("test");
						  if (markers2[i].getAttribute("id") == sid) {
							  document.getElementById("gm_info").innerHTML = markers2[i].getAttribute("name");
						  }
						  
					  }*/
					  
			}
		  }

	}
//}
    //]]>