var map;
var directions;

function GLoad(adresse,code_postal,commune) {
    if (!GBrowserIsCompatible()) { alert('L\'API GoogleMaps n\'est pas compatible avec ce navigateur.'); return; }       
                        
    map=new GMap2(document.getElementById("map"));  
  
    map.addControl(new GMapTypeControl());
    map.addControl(new GLargeMapControl3D());
    map.addControl(new GOverviewMapControl());
    map.addControl(new GScaleControl());
                          						  
    if (adresse+code_postal+commune=="") {
		AfficheOrigine(0);
    } else {                                            
        directions = new GDirections(map);
        directions.load("from: 96 route de Corbeil, 91700 Sainte-Geneviève-des-Bois, France to: "+adresse+" "+code_postal+" "+commune);
        GEvent.addListener(directions,"load", function() { 
          var marker=directions.getMarker(1);
		  var s="";
		  if (adresse!="") s=s+adresse+"<br>";
		  if (code_postal!="") s=s+code_postal+"<br>";
		  if (commune!="") s=s+commune+"<br>";
          marker.openInfoWindowHtml("<span class=\"style4\">"+s+"<br><hr>"+directions.getSummaryHtml()+"</span>");
          map.addOverlay(marker);
        });
        GEvent.addListener(directions,"addoverlay", function() { 
		  map.zoomOut();
        });				
        GEvent.addListener(directions,"error", function() { 
			AfficheOrigine(1);
        });						
    }
}

function AfficheOrigine(erreur) {
	var origine=new GLatLng(48.645034, 2.334829);
	var marker=new GMarker(origine);      
	var woptions = { maxWidth : 200 };
		  
    map.setCenter(origine, 12);
	s="<center><span class=\"style4\"><img width=100 src=\"images/logos/logo0.png\">"+"<br><br>"+"Nous intervenons à Sainte Geneviève\net dans sa région.</span>";
	//if (erreur=='1') s=s+"<br><br><span class=\"style5\">Votre adresse n'a pas été reconnue.</span>";
	s=s+"</center>";
    marker.openInfoWindowHtml(s,woptions);
    map.addOverlay(marker);                       
	map.addOverlay(new EInsert(origine, "images/contenu/cercle.png", new GSize(400,400), 12));      
}

function ValideForm() {
	var email=document.form1.email.value.toLowerCase();
	
	if (email!="") {
		var reg = /^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,3}$/
		if (reg.exec(email)==null) { alert('L\'adresse émail n\'est pas valide.'); return false; }
	}

    /*if(document.form1.adresse.value=="") { alert('Merci de nous indiquer une adresse valide.'); return false; }
    if(document.form1.code_postal.value=="") { alert('Merci de nous indiquer un code postal valide.'); return false; }
    if(document.form1.commune.value=="") { alert('Merci de nous indiquer une commune valide.'); return false; }*/
    if ((email=="") && (document.form1.telephone.value=="")) { alert('Afin que nous puissions vous contacter, merci de renseigner au moins une des deux informations suivantes :\n\n- adresse émail\net/ou\n- numéro de téléphone'); return false; }
    if (document.form1.message.value=="") { alert('Merci de nous indiquer l\'objet de votre demande.'); return false; }
    return true;
}

function EInsert(point, image, size, basezoom, zindex) {
  this.point = point;
  this.image = image;
  this.size = size;
  this.basezoom = basezoom;
  this.zindex=zindex||0;
  var agent = navigator.userAgent.toLowerCase();  
  if ((agent.indexOf("msie") > -1) && (agent.indexOf("opera") < 1)){this.ie = true} else {this.ie = false}
  this.hidden = false;
} 

EInsert.prototype = new GOverlay();

EInsert.prototype.initialize = function(map) {
  var div = document.createElement("div");
  div.style.position = "absolute";
  div.style.zIndex=this.zindex;
  if (this.zindex < 0) {
     map.getPane(G_MAP_MAP_PANE).appendChild(div);
  } else {
     map.getPane(1).appendChild(div);
  }
  this.map_ = map;
  this.div_ = div;
}

EInsert.prototype.makeDraggable = function() {
  this.dragZoom_ = this.map_.getZoom();
  this.dragObject = new GDraggableObject(this.div_);  
  this.dragObject.parent = this;  
  GEvent.addListener(this.dragObject, "dragstart", function() {
    this.parent.left=this.left;
    this.parent.top=this.top;
  });
  GEvent.addListener(this.dragObject, "dragend", function() {
    var pixels = this.parent.map_.fromLatLngToDivPixel(this.parent.point);
    var newpixels = new GPoint(pixels.x + this.left - this.parent.left, pixels.y +this.top -this.parent.top);
    this.parent.point = this.parent.map_.fromDivPixelToLatLng(newpixels);
    this.parent.redraw(true);
    GEvent.trigger(this.parent, "dragend", this.parent.point);
  });    
}

EInsert.prototype.remove = function() {
    this.div_.parentNode.removeChild(this.div_);
}

EInsert.prototype.copy = function() {
    return new EInsert(this.point, this.image, this.size, this.basezoom);
}

EInsert.prototype.redraw = function(force) {
   if (force) {
    var p = this.map_.fromLatLngToDivPixel(this.point);
    var z = this.map_.getZoom();
    var scale = Math.pow(2,(z - this.basezoom));
    var h=this.size.height * scale;
    var w=this.size.width * scale;  
    this.div_.style.left = (p.x - w/2) + "px";
    this.div_.style.top = (p.y - h/2) + "px";  
    if (this.ie) {
      var loader = "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+this.image+"', sizingMethod='scale');";
      this.div_.innerHTML = '<div style="height:' +h+ 'px; width:'+w+'px; ' +loader+ '" ></div>';
    } else {
      this.div_.innerHTML = '<img src="' +this.image+ '"  width='+w+' height='+h+' >';
    }    
    if (this.dragObject) {
      if (z != this.dragZoom_) {this.dragObject.disable();}
    }  
   } 
}

EInsert.prototype.show = function() {
    this.div_.style.display="";
    this.hidden = false;
}

EInsert.prototype.hide = function() {
    this.div_.style.display="none";
    this.hidden = true;
}

EInsert.prototype.getPoint = function() {
    return this.point;
}

EInsert.prototype.supportsHide = function() {
    return true;
}

EInsert.prototype.isHidden = function() {
    return this.hidden;
}

EInsert.prototype.setPoint = function(a) {
    this.point = a;
    this.redraw(true);
}

EInsert.prototype.setImage = function(a) {
    this.image = a;
    this.redraw(true);
}

EInsert.prototype.setZindex = function(a) {
    this.div_.style.zIndex=a;
}

EInsert.prototype.setSize = function(a) {
    this.size = a;
    this.redraw(true);
}

EInsert.groundOverlay = function(image, bounds, zIndex, proj,z) {
    var proj = proj||G_NORMAL_MAP.getProjection();              
    var z = z||17;
    var sw = proj.fromLatLngToPixel(bounds.getSouthWest(),z);
    var ne = proj.fromLatLngToPixel(bounds.getNorthEast(),z);
    var cPixel = new GPoint((sw.x+ne.x)/2, (sw.y+ne.y)/2);
    var c = proj.fromPixelToLatLng(cPixel,z);
    var s = new GSize(ne.x-sw.x, sw.y-ne.y);
    return new EInsert(c, image, s, z, zIndex);
}