//<![CDATA[
var OPTIONS = {};
var map;
var highlightCircle;
var locs;
var isClicked; // pointer
var clickedColor = 'rgb(253, 231, 178)';
var hoverColor = 'rgb(253, 196, 71)';
var normalColor = '#fff';
var initializeit = true;
var villecp = '';
var geo;
var listDiv;

var gdirs;
var poly = new Array();
var gpolies = new Array();
var directionGmarkers = new Array();
var dirLat = '49.1565368';
var dirLng = '2.438545';
var dirToPoint;


function initialize(options) {
	if (GBrowserIsCompatible()) {
		if (typeof(options) == 'undefined')	options = {};
		//default
		if (typeof(options.isList) == 'undefined') options.isList = true;
		OPTIONS = options;
		geo = new GClientGeocoder();
		geo.setBaseCountryCode('fr');
		
		dirToPoint = new GLatLng(dirLat, dirLng);
		locs = new Array();
		isClicked = new Array();
		listDiv = $('#defil');
		listDiv.empty();
		
		if (initializeit) {
			map = new GMap2(document.getElementById('google_map'));
			if (CURRENT_PAGE != 'mobile')
				map.addControl(new GSmallMapControl());
      		
			//map.addControl(new GMapTypeControl());
			map.setCenter(new GLatLng(46.227638, 2.213749), 5); // France
			map.setMapType(G_NORMAL_MAP);
			map.enableScrollWheelZoom();
			
			initializeit = false;
		}		
					
		showLocations();
	}
}

function showLocations() {
	var lat = '';
	var lng = '';

	$.get(SITE_ROOT + '/ajax/location/loc/' + escape(villecp), {}, function(data) { 
       geo.getLocations(data, function (result) {
        	if (result.Status.code == 200) {
        		if (result.Placemark.length > 1) {
        			// do nothing
        		} else {
        			//dirHtml = "";
        			var p = result.Placemark[0].Point.coordinates;
        			//var point = new GLatLng(p[1], p[0]);
        			lat = p[1];
        			lng = p[0];
        		}
        	} else {
        		if (villecp != '') {
	        		var reason = result.Status.code;
	        		alert("Impossible de trouver \"" + villecp + "\" " + reason);
	        	}
        	}
        	if (lat == '' && typeof(OPTIONS.lat) != 'undefined') {
        		lat = OPTIONS.lat;
        	}
        	if (lng == '' && typeof(OPTIONS.lng) != 'undefined') {
        		lng = OPTIONS.lng;
        	}
			var searchUrl = SITE_ROOT + '/ajax/locations/lat/' + lat + '/lng/' + lng;
			if (OPTIONS.isList) {
				if (villecp.match(/paris/i) || villecp.match(/750/i)) { // paris only
					searchUrl = searchUrl + '/radius/2';
				}
			}
			if (CURRENT_PAGE == 'mobile') {
				searchUrl = searchUrl + '/radius/5';
			}
			searchUrl = searchUrl + '/page/' + CURRENT_PAGE;
			
			xmlDataMarker(searchUrl, lat, lng);
        });
	});
}


function xmlDataMarker(searchUrl, latitude, longtitude) {
	GDownloadUrl(searchUrl, function(data, responseCode) {
		var xml = GXml.parse(data);
		var markers = xml.documentElement.getElementsByTagName('marker');
		map.clearOverlays();

		if (markers.length == 0) {
			if (villecp != '') {	
				alert('Cette valeur n\'est pas pr\351sente dans notre base de donn\351es.');
			}
			map.setCenter(new GLatLng(46.227638, 2.213749), 5); // France
		} else {
			if (typeof(REDIRECT_TO_MAP) != 'undefined' && CURRENT_PAGE == 'mobile') {
				window.location = SITE_ROOT + '/index/mobile-map/lat/' + latitude + '/lng/' +  longtitude;
			}
			var bounds = new GLatLngBounds();
			for (var i = 0; i < markers.length; i++) {	
				var id = markers[i].getAttribute('id');
				var nat_id = markers[i].getAttribute('nat_id');
				var name = markers[i].getAttribute('name');
				var address = markers[i].getAttribute('address');
				var city = markers[i].getAttribute('city');
				var zip = markers[i].getAttribute('zip');
				var tel = markers[i].getAttribute('tel');
				var fax = markers[i].getAttribute('fax');
				var activite_s = markers[i].getAttribute('activite_s');
				var activite_es = markers[i].getAttribute('activite_es');
				var photo_v = markers[i].getAttribute('photo_v');
				var photo_l = markers[i].getAttribute('photo_l');
				var photo_l_w = markers[i].getAttribute('photo_l_w');
				var photo_l_h = markers[i].getAttribute('photo_l_h');
				
				var lat = parseFloat(markers[i].getAttribute('lat'));
				var lng = parseFloat(markers[i].getAttribute('lng'));
				var point = new GLatLng(lat, lng);

				var loc = new Object();
				loc.point = point;
				loc.id = id;
				loc.nat_id = nat_id;
				loc.name = name;
				loc.address = address;
				loc.city = city;
				loc.zip = zip;
				loc.tel = tel;
				loc.fax = fax;
				loc.activite_s = activite_s;
				loc.activite_es = activite_es;
				loc.photo_v = photo_v;
				loc.photo_l = photo_l;
				loc.photo_l_w = photo_l_w;
				loc.photo_l_h = photo_l_h;
				loc.lat = lat;
				loc.lng = lng;
				locs[i] = loc;

				var marker = createMarker(loc);
				map.addOverlay(marker);
				if (OPTIONS.isList) {
					var list = createList(marker, loc);
					listDiv.append(list);
				}
				bounds.extend(point);
			}

			if (CURRENT_PAGE == 'renault-sports') {
				$('.imgselect').hover(
						function() {
							$(this).attr('src', SITE_ROOT + '/renault_sports_assets/images/btn_select_over.gif');
						},
						function() {
							$(this).attr('src', SITE_ROOT + '/renault_sports_assets/images/btn_select.gif');
						}   			
				);
			}
			map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
		}
	});
}


function createList(marker, loc) {
	/**
	<div class="blocgarage">
      <h4 class="titregarage">RENAULT Retail group - Paris palais Royal</h4>
      <p class="textegarage"> 6 rue De Valois <br />
        75001 PARIS</p>
      <a href="form_brochure2.html"><img src="<?php echo $this->baseUrl() ?>/images/btn_select.gif" class="imgselect" /></a>
      <div class="spacer"></div>
    </div>
	 */
	var imagePath = 'images';
	var formMod = CURRENT_PAGE + '-form';
	//if (CURRENT_PAGE == 'brochure') formMod = 'brochure-form';
	//else if (CURRENT_PAGE == 'essai') formMod = 'essai-form';
	var href = '';
	
	
	switch (CURRENT_PAGE) {
		case 'mobile':
			href = '#'; 
			break;
	
		case 'renault-sports':
			href = SITE_ROOT + '/index/' + formMod + '/page/' + CURRENT_PAGE + '/id/' + loc.id;
			imagePath = 'renault_sports_assets/images'; 
			break;
			
		case 'clio-campus':
			href = SITE_ROOT + '/index/' + formMod + '/page/' + CURRENT_PAGE + '/mot/' + MOT + '/coul/' + COUL + '/opt/' + OPT + '/prix/' + PRIX + '/id/' + loc.id;
			imagePath = 'formulaire_fichiers';
			break;
	
		case 'autodeclic':
			href = SITE_ROOT + '/index/' + formMod + '/page/' + CURRENT_PAGE + '/nomDuVehicule/' + NOM_DU_VEHICULE + '/act/' + ACT + '/id/' + loc.id;
			imagePath = 'autodeclic_assets/images';
			
		default:
			href = SITE_ROOT + '/index/' + formMod + '/page/' + CURRENT_PAGE + '/mod/' + escape(CURRENT_MOD) + '/nsmod/' + CURRENT_NSMOD + '/id/' + loc.id;
			break;
	
	}
	
	var div = $('<div></div>').attr('class', 'blocgarage').attr('id', loc.id);
	var selectButton = $('<a></a>').attr('href', href).append($('<img/>').attr('src', SITE_ROOT + '/' + imagePath + '/btn_select.gif').attr('class', 'imgselect'));
		
	if (CURRENT_PAGE == 'clio-campus') {
		selectButton = $('<a></a>').attr('href', href).append($('<div></div>').attr('class', 'imgselect').html('&nbsp;')).css('text-decoration', 'none');
	} else if (CURRENT_PAGE == 'mobile') {
		selectButton = $('<a></a>').attr('href', href).append($('<img/>').attr('src', SITE_ROOT + '/mobile_assets/img/bout_situez_sur_carte.jpg'))
			.click(function() {
				$('#defil').hide();
				openWindow(marker, loc);
			});
	}
	
	div.append(
		$('<h4></h4>').attr('class', 'titregarage').html(loc.name),
		$('<p></p>').attr('class', 'textegarage').html(loc.address + '<br />' + loc.zip + ' ' + loc.city),
		selectButton,
		$('<div></div>').attr('class', 'spacer')
	
	);
	
	GEvent.addDomListener(div, 'mouseover', function() {
		divColor(div, loc, clickedColor, hoverColor);
	});
  
	GEvent.addDomListener(div, 'mouseout', function() {
		divColor(div, loc, clickedColor, normalColor);	
	});
	
	return div;
}


function divColor(div, loc, activeColor, normalColor) {
    if (isClicked[loc.id]) {
       	div.css("backgroundColor", activeColor);
    } else {
    	div.css("backgroundColor", normalColor);
    }	
}

function resetListDivColor() {
	for (var i = 0; i < locs.length; i++) {
		$("#"+locs[i].id).css("background-color", normalColor);	
		isClicked[locs[i].id] = false;
	}
}

function slideListDiv(loc) {
	var divOffset = listDiv.offset().top;
    var pOffset = $('#'+loc.id).offset().top;
    var pScroll = pOffset - divOffset;
    listDiv.animate({scrollTop: '+=' + pScroll + 'px'});	
}

function createMarker(loc) {
	var icon = new GIcon();
	//icon.image = SITE_ROOT + '/images/jaune.png';
	icon.image = natIdImage(loc.nat_id);
	icon.iconSize =  new GSize(31, 31);
    icon.iconAnchor = new GPoint(16,32);
    icon.infoWindowAnchor = new GPoint(16,16);

	var marker = new GMarker(loc.point, icon);

	GEvent.addListener(marker, 'click', function() {
		resetListDivColor();
		slideListDiv(loc);
		
		$("#"+loc.id).css("background-color", clickedColor);	
		isClicked[loc.id] = true;
	});
	
    GEvent.addListener(marker, "mouseover", function() {
  		highlight(marker, loc);
    });

    GEvent.addListener(marker, "mouseout", function() {
  		unhighlight(loc);
    });
    
	GEvent.addListener(marker, 'click', function() {
		if (CURRENT_PAGE == 'mobile') {
			openWindow(marker, loc);
		}
	});
    
    return marker;
}

function mainWindowHtml(loc) {
	var minContentHmtl = '<div style="width: 250px;">';
	minContentHmtl += '<div class="title">' + loc.name + '</div>';
	minContentHmtl += '<div class="others">';
	minContentHmtl += loc.address + '<br />';
	minContentHmtl += loc.zip + ' ' + loc.city +'<br /><br />';
	minContentHmtl += '<span><a href="javascript:directionShowMarker(' + toSource(loc) + ');"><img src="' + SITE_ROOT + '/mobile_assets/img/bout_syrendre.png"/></a></span><br />';
	minContentHmtl += '<span>&nbsp;</span><br />';
	minContentHmtl += '<span><a href="' + SITE_ROOT + '/index/mobile-form2/page/mobile/id/' + loc.id + '"><img src="' + SITE_ROOT + '/mobile_assets/img/bout_reserver_essai.png"/></a></span><br />';
	minContentHmtl += '</div>';
	minContentHmtl += '</div>';
	return minContentHmtl;	
}

function openWindow(marker, loc) {
	marker.openInfoWindowHtml(mainWindowHtml(loc));
}

function highlight(marker, loc) {
    var polyPoints = makePolyPoints(marker);
       
    highlightCircle = new GPolygon(polyPoints,"#000000",2,0.0,"#fdc447",0.6);
   	map.addOverlay(highlightCircle);
	divColor($('#'+loc.id), loc, clickedColor, hoverColor);	
}

function unhighlight(loc) {
	map.removeOverlay(highlightCircle);
	divColor($('#'+loc.id), loc, clickedColor, normalColor);		    	
}

function makePolyPoints(currentMarker){
    var markerPoint = currentMarker.getLatLng();
    var polyPoints = Array();

    if (highlightCircle) {
    	map.removeOverlay(highlightCircle);
    }

    var mapNormalProj = G_NORMAL_MAP.getProjection();
    var mapZoom = map.getZoom();
    var clickedPixel = mapNormalProj.fromLatLngToPixel(markerPoint, mapZoom);

    //var polySmallRadius = 20;
    var polyNumSides = 20;
    var polySideLength = 18;

    for (var a = 0; a<(polyNumSides+1); a++) {
	   var aRad = polySideLength*a*(Math.PI/180);
	   //var polyRadius = polySmallRadius; 
	   var pixelX = clickedPixel.x + 20 * Math.cos(aRad);
	   var pixelY = -3 + clickedPixel.y + 10 * Math.sin(aRad);
	   var polyPixel = new GPoint(pixelX,pixelY);
	   var polyPoint = mapNormalProj.fromPixelToLatLng(polyPixel,mapZoom);
	   polyPoints.push(polyPoint);
    }

    return polyPoints;
}


function natIdImage(nId){
	var retImage = '';
	var ret = 1;

    if (nId == 1 || nId == 2 || nId == 3 || nId == 9 ||
    		nId == 10 || nId == 11 || nId == 12 || nId == undefined || nId == ''
	) {	
		ret = 1;
	} else if (nId == 5 || nId == 21 || nId == 25 ||
		nId== 26 || nId == 27 || nId == 30
	) {
		ret = 2;	    	
	}
	
	if (ret == 1) {
		retImage = '/images/icon.png';
	} else if (ret == 2) {
		retImage = '/images/icon2.png';
	}
	 
	if (CURRENT_PAGE != 'mobile') {
		retImage = '/images/jaune.png';	
	}
	
	retImage = SITE_ROOT + retImage;
	return retImage;
}


function searchVilleCp() {
	as_json.clearSuggestions();
	villecp = $('#villecp').val();
	
	if (villecp == '' || villecp == 'Ville / Code postal') {
		alert('Vous devez saisir une ville');
		return false;
	}
	
	initialize(OPTIONS);
}


function directionShowMarker(loc) {
	gdirs = new GDirections;
	
	GEvent.addListener(gdirs, "load", dirs);
    GEvent.addListener(gdirs, "error", function () {
    	//GLog.write("Failed: " + gdirs.getStatus().code);
    	alert('Impossible d\'obtenir la direction');
    });
	
    var point = new GLatLng(loc.lat, loc.lng);
	if (point) {
		var bounds = new GLatLngBounds();
		var fromPoint = point;
       
		map.clearOverlays();
		gpolies.length = 0;

        if (dirToPoint) {
        	bounds.extend(fromPoint);
			bounds.extend(dirToPoint);
        
			var icon = new GIcon();
			icon.image = SITE_ROOT + '/mobile_assets/img/icon_A.png';
  			icon.iconSize =  new GSize(97, 37);
  			icon.iconAnchor = new GPoint(16, 32);
    		icon.infoWindowAnchor = new GPoint(16,16);		

    		var markerTo = new GMarker(dirToPoint, {icon: icon});
		    directionGmarkers[0] = markerTo;
        	map.addOverlay(markerTo);
    		
            
            var icon2 = new GIcon();
	    	icon2.image = SITE_ROOT + '/mobile_assets/img/icon_B.png';
	    	icon2.iconSize =  new GSize(19, 34);  			
			icon2.iconAnchor = new GPoint(16, 32);
    		icon2.infoWindowAnchor = new GPoint(16,16);	
        	
        	
        	var markerFrom = new GMarker(fromPoint, {icon: icon2});
		    directionGmarkers[1] = markerFrom;
			GEvent.addListener(markerFrom, 'click', function() {
				if (CURRENT_PAGE == 'mobile') {
					openWindow(markerFrom, loc);
				}
			});
		    
		    map.addOverlay(markerFrom);
        	
        	
        	
        	directionDisplay();
        	map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
        }
	}
}

function dirs() {
    poly = gdirs.getPolyline();
    map.addOverlay(poly);
    gpolies.push(poly);
}

function directionDisplay() {
    var points = [];
    for (var i = 0; i < directionGmarkers.length; i++) {
        if (directionGmarkers[i]) {
            var p = directionGmarkers[i].getPoint();
            points.push(p);
        }
    }
    if (points.length == 2) {
    	directionWaypoints(points[0], points[1]);
    }
    
}

function directionWaypoints(p1, p2) {
    gdirs.loadFromWaypoints([p1, p2], {getPolyline: true, getSteps: true, locale: "fr_FR"});
}

/**
 * utilities 
 */
function toSource(obj) {
	var ret = '';
    jQuery.each(obj, function(i, val) {
      	//if (typeof val == 'object')
      	//	ret += i + ": "+ (toSource(val)) + ", ";
      	//else
      		ret += i + ": '"+ addslashes(val) + "', ";
      
    });
    
    ret = ret.substring(0, ret.length - 2);
    return '{' + ret + '}';
}

function addslashes( str ) {
    return (str+'').replace(/([\\"'])/g, "\\$1").replace(/\0/g, "\\0");
}

function isValidEmail(str) {
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return !filter.test(str) ? false : true;
}


$(function() {
	if (CURRENT_PAGE == 'clio-campus') {
		clickedColor = "rgb(195, 218, 128)";
		hoverColor = "rgb(195, 218, 128)";
		normalColor = "#fff";
	} else if (CURRENT_PAGE == 'renault-sports') {
		normalColor = "rgb(24, 24, 24)";
	}
	
	
	$('#search').click(searchVilleCp);
	
   	$('#villecp').focus(function() {
   		if ($(this)[0].value == 'Ville / Code postal' ) {
   			$(this)[0].value = '';
   		}
  	}).blur(function() { 
  		if ($(this)[0].value == '' ) {
   			$(this)[0].value = 'Ville / Code postal';
   		}
   	});
   	
   	

   	
   	//essai form
   	$('.essaiForm').submit(function() {
   		//alert('essai form');
   		var error = false;
   		var errorMsg = 'Tous les champs sont requis !';
   		
   		
   		$('.erreur2').hide();
   		$('.required').each(function(i) {
   			if ($(this).attr('id') == 'email') {
   				if ( !isValidEmail($(this).val()) ) {
   					error = true;
   					errorMsg = 'Votre adresse e-mail n\'est pas valide';
   				}
   			}
   			
   			if ($(this).val() == '') {
   				error = true;
   				errorMsg = 'Tous les champs sont requis !';
   			}
   		});
   		
   		if (error) {
   			$('.erreur2').show().text(errorMsg);
   			return false;
   		}
   		
   		$.post(SITE_ROOT + '/index/essai-form-submit/id/' + ID, $(this).serialize(), 
   			function(data) {
   				if (data.exec) {	
	   				if (data.id) {
	   					window.location = SITE_ROOT + '/index/essai-confirm/page/' +  CURRENT_PAGE + '/nsmod/' + CURRENT_NSMOD + '/id/' + data.id;
	   				}
   				} else {
   					alert('error');
   					alert(data);
   				}
   			},
   			'json'
   		);
   	});
   	
   	//renault sports
   	$('#renaultSports').submit(function() {
   		//alert('renault sports');
   		var error = false;
   		var errorMsg = 'Tous les champs sont requis !';
   		
   		
   		$('.erreur2').hide();
   		$('.required').each(function(i) {
   			if ($(this).attr('name') == 'DCLIPART_DCLICOM_E_MAIL') {
   				if ( !isValidEmail($(this).val()) ) {
   					error = true;
   					errorMsg = 'Votre adresse e-mail n\'est pas valide';
   				}
   			}
   			
   			if ($(this).val() == '') {
   				error = true;
   				errorMsg = 'Tous les champs sont requis !';
   			}
   		});
   		
   		
   		if ($('input[name="DCLIPART_DCLICOM_CD_TITRE"]:checked').length == 0 || $('input[name="INT_MODELE"]:checked').length == 0 || 
   				$('input[name="DCLISPEC_OPTIN"]:checked').length == 0 ) {

   				error = true;
   				errorMsg = 'Tous les champs sont requis !';
   			}
   		
   		
   		if (error) {
   			if ($('.erreur2').length == 1) {
   				$('.erreur2').show().text(errorMsg);
   			} else {
   				alert(errorMsg);
   			}
   			return false;
   		}
   		
   		$.post(SITE_ROOT + '/index/renault-sports-form-submit/id/' + ID, $(this).serialize(), 
   			function(data) {
   				if (data.exec) {	
	   				if (data.id) {
	   					var model = $('input[name="INT_MODELE"]:checked').val();
	   					
	   					switch (model) {
	   						case 'Clio R.S.':
	   							model = 'NOUVELLE_CLIO_RS';
	   							break;
	   						case 'Megane R.S.':
	   							model = 'NOUVELLE_MEGANE_RS';
	   							break;	   					
	   						case 'Twingo R.S.':
	   							model = 'TWINGO_RS';
	   							break;	   		   					
	   					}
	   					window.location = SITE_ROOT + '/index/renault-sports-confirm/page/' +  CURRENT_PAGE + '/id/' + data.id + '/model/' + model;
	   				}
   				} else {
   					alert('error');
   					alert(data);
   				}
   			},
   			'json'
   		);
   	}); 
   	//brochure form
   	$('.brochureForm').submit(function() {
   		//alert('brochure form');
   		var error = false;
   		var errorMsg = 'Tous les champs sont requis !';
   		
   		$('.erreur1').hide();
   		$('.required').each(function(i) {
   			if ($(this).attr('id') == 'email') {
   				if ( !isValidEmail($(this).val()) ) {
   					error = true;
   					errorMsg = 'Votre adresse e-mail n\'est pas valide';
   				}
   			}
   			
   			if ($(this).val() == '') {
   				error = true;
   				errorMsg = 'Tous les champs sont requis !';
   			}
   		});
   		
  		if (error) {
   			$('.erreur1').show().text(errorMsg);
   			return false;
   		}
   		
   		$.post(SITE_ROOT + '/index/brochure-form-submit/id/' + ID, $(this).serialize(), 
   			function(data) {
   				if (data.exec) {	
	   				if (data.id) {
	   					window.location = SITE_ROOT + '/index/brochure-confirm/page/' + CURRENT_PAGE + '/nsmod/' + CURRENT_NSMOD + '/id/' + data.id;
	   				}
   				} else {
   					alert('error');
   					alert(data);
   				}
   			},
   			'json'
   		);
   	});   	

   	
   	//clio campus form
   	$('#clioCampusFormSubmit').click(function() {
   		//alert('clioCampusForm');
   		var error = false;
   		var errorMsg = 'Tous les champs sont requis !';
   		
   		$('.erreur1').hide();
   		$('.required').each(function(i) {
   			if ($(this).attr('id') == 'email') {
   				if ( !isValidEmail($(this).val()) ) {
   					error = true;
   					errorMsg = 'Votre adresse e-mail n\'est pas valide';
   				}
   			}
   			
   			if ($(this).val() == '') {
   				error = true;
   				errorMsg = 'Tous les champs sont requis !';
   			}
   		});
   		
  		if (error) {
   			$('.erreur1').show().text(errorMsg);
   			return false;
   		}
   		
   		$.post(SITE_ROOT + '/index/clio-campus-form-submit/id/' + ID + '/page/' + CURRENT_PAGE + '/mot/' + MOT + '/coul/' + COUL + '/opt/' + OPT + '/prix/' + PRIX, $('.clioCampusForm').serialize(), 
   			function(data) {
   				if (data.exec) {	
	   				if (data.id) {
	   					window.location = SITE_ROOT + '/index/clio-campus-confirm/page/' + CURRENT_PAGE + '/mot/' + data.mot + '/coul/' + data.coul+ '/opt/' + data.opt + '/prix/' + data.prix + '/id/' + data.id;
	   					//alert(data.exec);
	   				}
   				} else {
   					alert('error');
   					alert(data.toSource());
   				}
   			},
   			'json'
   		);
   	});   	   	
   	
   	
   	
   	//autodeclic form
   	$('.autodeclicFormSubmit').submit(function() {
   		var error = false;
   		var errorMsg = 'Tous les champs sont requis !';
   		
   		$('#erreur').hide();
   		$('.required').each(function(i) {
   			if ($(this).attr('id') == 'email') {
   				if ( !isValidEmail($(this).val()) ) {
   					error = true;
   					errorMsg = 'Votre adresse e-mail n\'est pas valide';
   				}
   			}
   			
   			if ($(this).val() == '') {
   				error = true;
   				errorMsg = 'Tous les champs sont requis !';
   			}
   		});
   		
  		if (error) {
   			$('#erreur').show().text(errorMsg);
   			return false;
   		}
   		
   		$.post(SITE_ROOT + '/index/autodeclic-form-submit/id/' + ID + '/page/' + CURRENT_PAGE, $(this).serialize(), 
   			function(data) {
   				if (data.exec) {	
	   				if (data.id) {
	   					window.location = SITE_ROOT + '/index/autodeclic-confirm/page/' + CURRENT_PAGE +  '/nomDuVehicule/' + NOM_DU_VEHICULE + '/act/' + ACT + '/id/' + data.id;
	   					//alert(data.toSource());
	   				}
   				} else {
   					alert('error');
   					alert(data.toSource());
   				}
   			},
   			'json'
   		);
   	});  
   	
   	
   	//mobile form 2
   	$('#mobileForm2Submit').click(function() {
   		var error = false;
   		var errorMsg = 'Tous les champs sont requis !';
   		
   		$('.required').each(function(i) {
   			if ($(this).attr('id') == 'email') {
   				if ( !isValidEmail($(this).val()) ) {
   					error = true;
   					errorMsg = 'Votre adresse e-mail n\'est pas valide';
   				}
   			}
   			
   			if ($(this).val() == '') {
   				error = true;
   				errorMsg = 'Tous les champs sont requis !';
   			}
   		});
   		
  		if (error) {
   			alert(errorMsg);
   			return false;
   		}
   		
   		$.post(SITE_ROOT + '/index/mobile-form2-submit/id/' + ID + '/page/' + CURRENT_PAGE, $(this).serialize(), 
   			function(data) {
   				if (data.exec) {	
	   				if (data.id) {
	   					//window.location = SITE_ROOT + '/index/mobile-form2-confirm/page/' + CURRENT_PAGE +  '/id/' + data.id;
	   				}
   				} else {
   					alert('error');
   					alert(data.toSource());
   				}
   			},
   			'json'
   		);
   	});     	
   	
   	
   	
});
//]]>