var initPage = function(){
	/*
	Event.observe(window, 'unload', function(){
		if(map){
			GUnload();
		}
	});
	*/
	 	
	// VARIABLES GLOBALES
	tabIdAC = {};
	
	// FONCTIONS COMMUNES
	//RollOverImage();
	focusInput();
	infobulle();

	// PARSE LES .SAMAKA POUR APPLIQUER LE BASE64.DECODE
	$$('.samaka').each(function(e,i){
		e.observe('click',function(){
			var b = Kawlamaka.decode(e.getAttribute("data-url")); 
			var c = e.getAttribute("data-target");
			if(c!=null && c=="blank"){
				window.open(b,"nom");
			}else{
				window.location=b
			}
		});
	});

	
	// Autocompletion 
	if($('hsearch')){
		var MOPlus = new multiOnglet('moteur_recherche','nav','liste_contenu',{MO_contenu:'contenu',MO_pos:1,MO_duree:0.5});
		$('liste_contenu').setStyle({overflow : 'visible'})

		if($('DirectoryCities')){
			new Ajax.Autocompleter('DirectoryCities', 'Dcities', '_ajax.php?do=cities',{minChars:2,afterUpdateElement:getDirectCityId});	
		}		
		if($('RestoCities')){
			new Ajax.Autocompleter('RestoCities', 'Rcities', '_ajax.php?do=cities',{minChars:2,afterUpdateElement:getRestoCityId});
		}		
		if($('PrayerCities')){
			new Ajax.Autocompleter('PrayerCities', 'Pcities', '_ajax.php?do=cities',{minChars:2,afterUpdateElement:getPrayerCityId});
		}		
		if($('AddTitl')){
			new Ajax.Autocompleter('AddTitl', 'Additifs', '_ajax.php?do=Additifs',{minChars:2,afterUpdateElement:getAdditifId});		
		}
		//Probleme ?? si desactive, ÃƒÂ§a passe...??shopKyw
		if($('newCity')){
			$('sCountry').observe('change',function(){
				$('sCountryidt').value = $F('sCountry');
			});
			new Ajax.Autocompleter('newCity', 'sNewCities', '_ajax.php?do=cities',{
				minChars:3,
				parameters : 'id='+$F('sCountryidt'),
				afterUpdateElement:getCountryId
			});
		}
	}
		
	// Autocompletion menus gauche
		if($('Shopkyword')){ new Ajax.Autocompleter('Shopkyword', 'shopkyws', '_ajax.php?do=ShopSuggest',{minChars : 2,afterUpdateElement : getShopKyw});}	
		if($('lCities')){ new Ajax.Autocompleter('lCities', 'lDcities', '_ajax.php?do=cities',{minChars : 2,afterUpdateElement : getDirectCityId});}	
	
		function getDirectCityId(text, li) { $('mlCityidt').value = li.id; }
		function getRestoCityId(text, li) { $('resCityidt').value = li.id; }
		function getPrayerCityId(text, li) { $('PCityidt').value = li.id; }		
		function getAdditifId(text, li) { $('AddTitl').value = li.id; }				
		function getShopKyw(text, li) { $('shopKyw').value = li.id; }
		function getCountryId(text, li) { $('sCountryidt').value = li.id; }

	// TRI
	if($('frm_tri')){ $('tri').observe('change',function(){$('frm_tri').submit();	});}
		
	// DONNER AVIS
	if($('bt_donner_avis')){
		$('bt_donner_avis').observe('click',function(){
			$('bt_donner_avis').blur();
			if($('bt_donner_avis').next('form').style.display == 'none'){
				new Effect.BlindDown($('bt_donner_avis').next('form'),{duration:0.5, afterFinish:
					function(){
						$('bt_donner_avis').update('Fermer');																				  
					}
				});
			}else{
				new Effect.BlindUp($('bt_donner_avis').next('form'),{duration:0.5, afterFinish:
					function(){
						$('bt_donner_avis').update('Donner votre avis');																				  
					}
				});
			}
		});
	}

	/* Ajout Commentaire*/
	if($('bt_ajout_com')) Event.observe($('bt_ajout_com'),'click',function(){ Effect.ScrollTo($('commentaires')); });
	if($('bt_commenter')) Event.observe($('bt_commenter'),'click',function(){ Effect.ScrollTo($('commentaires')); });
	if($('comt_txt')) makeItCount('comt_txt',700);
	if($('liste_commentaires')) Event.observe($('liste_commentaires'),'click',function(){ Effect.ScrollTo($('commentaires')); });
	
	
	// LISTE ANNONCE
	if($('liste_annonce')){
		 rollItem('#liste_annonce .item');
	}
	

	
	// Address Tools
	if($('comt_form')){
		$$('#comt_form .ecrire').each(function(e){
			id = e.id.substr(6);
			e.observe('click',function(){
				ecrireCommentaire(e,id);
		   	});
		});
		$$('#utilitaire_annonce .valider_commentaire').each(function(e){
			e.observe('click',function(){
				id = $(e.previous('textarea')).id.substr(19);
				if($F(e.previous('textarea')) != ''){
					validerCommentaire(e,id);
				}
			});
		});
		$$('#comt_form .rating_container').each(function(e){
			noterAdresse(e);
		});
		$$('#utilitaire_annonce .effacer_note').each(function(e){
			e.observe('click',function(){
				e.next('input').value='0';
				noterAdresse(e.previous('.rating_container'));
				new Ajax.Request('_ajax.php?ACT=majNote&id='+id,{
					parameters : {note:0}
				});
			});
		});
	}
	
	//GEOLOCALISATION
	if($('gmap')){
		var baseIcon = new GIcon(G_DEFAULT_ICON);
		baseIcon.iconSize = new GSize(15,24);
		baseIcon.shadowSize = new GSize(0,0);
		baseIcon.iconAnchor = new GPoint(7,24);
		map = new GMap2($("gmap"));
		creerBoutons();
		var IconType = new GIcon(baseIcon);
		IconType.image = 'images/commun/marker.png';
		markerOptions = {
			icon: IconType,
			clickable : false
		}
		if($F('latitude')!='' && $F('longitude')!=''){
			point = new GLatLng($F('latitude'),$F('longitude'));
			var marker = new GMarker(point, markerOptions);
			map.addOverlay(marker);
			map.setCenter(point,15);
		}else{
			geocoder = new GClientGeocoder();
			geocoder.getLatLng($F('adresse'),function(cb){
				point = new GLatLng(cb.y,cb.x);
				var marker = new GMarker(point, markerOptions);
				map.addOverlay(marker);
				map.setCenter(point,15);
			});
		}
      
		var publisherID = 'pub-533068923148589';
		var adsManagerOptions = {
			maxAdsOnMap : 2,
			style: G_ADSMANAGER_STYLE_ADUNIT,
			channel: '1912863965'
		};
		adsManager = new GAdsManager(map, publisherID, adsManagerOptions);
		adsManager.enable();
      		
		$('bt_go').observe('click',function(){
			directionsPanel = $("route");
			directions = new GDirections(map, directionsPanel);
			directions.load('from:'+$F('from')+' to:'+$F('adresse'),{"locale":$F('langue')});
			// PAS D'ERREUR
			GEvent.addListener(directions, "load", function() {
			   $('route').update('').show();
			});			
			//TRAITEMENT DES ERREURS
			GEvent.addListener(directions, "error", function() {
				$('route').update('<div class="erreur">Impossible d\'afficher l\'itinÃƒÂ©raire pour cette adresse. (erreur '+directions.getStatus().code+')').show();
			});
		})

	}	
	
	// VISIONNEUSE
	if($('galerie_photo1')){
		visionneuse(0);
		$('bt_galerie_photo').observe('click',function(){
			$('bt_galerie_photo').hide();
			$('galerie_photo1').hide();
			$('galerie_photo2').show();
			if($$('#galerie_photo2 img').size() == 0){
				new Ajax.Request('./_ajax.php?do=photo',{
					onComplete: function(transport){
						tab = transport.responseText.evalJSON();
						tab.each(function(e){
							$$('#galerie_photo2 .cont_galerie')[0].insert({bottom:'<img src="'+e.visuel+'" alt="" />'});	  
						});
						$$('#galerie_photo2 a').each(function(e){
							e.observe('click',function(){
								$('bt_galerie_photo').show();
								$('galerie_photo1').show();
								$('galerie_photo2').hide();
							});
						});
					}
				});					
			}
		});
	}
	
	// DerniÃƒÂ¨res adresses
	if($('dernieres')){
		var sb = new SwitchBloc('listing1','item');
		rollItem('#listing1 .f_item');
		/*$$('#listing1 .item').each(function(e){
			centrerV(e.down('img').id);
		});*/
	}

	// DerniÃƒÂ¨res adresses
	if($('boutique')){
		var sb2 = new SwitchBloc('listing2','item');
		rollItem('#listing2 .f_item');
	/*	$$('#listing2 .item').each(function(e){
			centrerV(e.down('img').id);
		});*/
	}
	
	// PIED
	if($('seo1')){
		$$('#seo1 ul').each(function(e){
			e.setStyle({
				height:'95px',
				overflow:'hidden'
			});
			e.observe('mouseover',function(){
				e.style.overflow = 'auto';
			});
			e.observe('mouseout',function(){
				e.style.overflow = 'hidden';
			});
		});
	}
	
	// SOUS MENU
	$$('#smenu .niveau1').each(function(e){
		e.observe('mouseenter',function(){
			
			e.addClassName('on');
			e.down('.dhtml').show();						
		});
		e.observe('mouseleave',function(){
			
			e.removeClassName('on');
			e.down('.dhtml').hide();						
		});
	});

	
	// AUTOCOMPLETION
	$$('input.ac').each(function(e){
		AC(e);				 
	});
	
	// MA PAGE
	if($('ma_page')){
		hauteurRub('.avantages li');
	}
};

//#################
//### FONCTIONS ###
//#################


// ECRIRE COMMENTAIRE
var ecrireCommentaire = function(e,id){
	if($('commentaire'+id)){
		$('commentaire'+id).hide();
	}
	if(e.up('div').next('.depot_commentaire').style.display == 'none'){
		e.up('div').next('.depot_commentaire').show();
		e.up('div').next('.depot_commentaire').down('textarea').focus();
		e.up('.actions2').addClassName('on');
		e.update('Fermer');
	}else{
		e.up('div').next('.depot_commentaire').hide();
		e.up('.actions2').removeClassName('on');
		if(!$('commentaire'+id)){
			e.update('Ecrire un commentaire');
		}else{
			e.update('Modifer');
		}
	}
};

// VALIDER COMMENTAIRE
var validerCommentaire = function(e,id){
	new Ajax.Request('./_ajax.php?do=ajoutCommentaire',{
		parameters : {id : id, commentaire : $F(e.previous('textarea'))},
		onComplete: function(transport){
			$(e.up('.depot_commentaire')).hide();
			e.up(2).down('.ecrire').update('Modifier');
			if(!$('commentaire'+id)){
				$(e.up('.depot_commentaire')).insert({before:'<div id="commentaire'+id+'" class="commentaire">'+$F(e.previous('textarea'))+'</div>'});
				e.up(2).down('.ecrire').insert({after :'<a href="javascript:suppCommentaire(\''+id+'\')" id="suppCommentaire'+id+'">Supprimer</a>'});
			}else{
				$('commentaire'+id).update($F(e.previous('textarea')));
				$('commentaire'+id).show();
			}
		}
	});
};

// NOTER Adresse
var noterAdresse = function(e){
	id = e.id.substr(5);
	var r = new Control.Rating('note_'+id,{
		max : 5,
		input: 'input_note_'+id,  
		multiple: true ,
		updateParameterName : 'note',
		//updateUrl : '_ajax.php?do=majNote&id='+id
	});
};

// SUPPRESSION COMMENTAIRE
var suppCommentaire = function(id){
	new Ajax.Request('./_ajax.php?do=ajoutCommentaire',{
		parameters : {id : id, commentaire : ''},
		onComplete: function(transport){
			$('suppCommentaire'+id).remove();
			$('commentaire_annonce'+id).value = '';
			$('commentaire_annonce'+id).up('.depot_commentaire').hide();
			if($('ma_selection')){
				$('commentaire_annonce'+id).up('.actions2').removeClassName('on');
			}
			$('commentaire_annonce'+id).up(2).down('.ecrire').update('Ecrire un commentaire');
			if($('commentaire'+id)){
				$('commentaire'+id).remove();
			}
		}
	});	
};

// INFOBULLE
var coordMouse = function(e){
	pX = Event.pointerX(e);
	pY = Event.pointerY(e);
	w = $('infobulle').getWidth()/2;
	h = $('infobulle').getHeight();
	$('infobulle').setStyle({display:'block', top:pY-h-20+'px', left:pX-w+'px'});
};
var infobulle = function(){
	$('bandeau').insert({before:'<div id="infobulle"></div>'});
	$('infobulle').hide();
	$$('a.aide').each(function(e){
		e.next('.cont_aide').hide();
		Event.observe(e,'mouseout',function(){
			
			Event.stopObserving(document,'mousemove',coordMouse);
			pX=0;
			pY=0;
			$('infobulle').hide();
		});
		Event.observe(e,'mouseover',function(){
			
			$('infobulle').show();
			$('infobulle').update(e.next('.cont_aide').innerHTML);
			Event.observe(document,'mousemove',coordMouse);				
		});
	});
};

// AUTOCOMPLETE SIMPLE
var AC2 = function(){
	$$('input.ac2').each(function(e){
		e.insert({after:'<div id="cont_'+e.id+'" class="autocomplete"></div>'});
		new Ajax.Autocompleter(e.id,'cont_'+e.id,'_ajax.php?do=ac_localisation',{
			minChars : 3,
			afterUpdateElement : ajouteHidden
		});
	});
};
var ajouteHidden = function(e,li){
	alert(li.id);
};
// AUTOCOMPLETE
var AC = function(e){
	e.insert({after:'<div id="cont_'+e.id+'" class="autocomplete"></div>'});
	tabIdAC['liste_'+e.id] = new Array();
	tabIdAC['nom_'+e.id] = new Array();
	if(!$('liste_'+e.id)){
		e.insert({before:'<div class="liste_choix"><ul id="liste_'+e.id+'"></ul></div>'});
		e.insert({before:'<input type="hidden" id="valeur_'+e.id+'" name="valeur_'+e.id+'" />'});
	}else{
		$$('#liste_'+e.id+' li').each(function(li){
			nom_liste = 'liste_'+e.id;
			tabIdAC['liste_'+e.id].push(li.id.substr(nom_liste.length+1));
			tabIdAC['nom_'+e.id].push(li.down('span').innerHTML);
			$('valeur_'+e.id).value = tabIdAC['liste_'+e.id];
			$('localisation_recherche').value = tabIdAC['nom_'+e.id];
		});
	}
	new Ajax.Autocompleter(e.id,'cont_'+e.id,'_ajax.php?do='+e.id,{
		minChars : 3,
		afterUpdateElement : ajouteItemAC
	});
};
var ajouteItemAC = function(e,li){
	if(!$('liste_'+e.id+'_'+li.id) && li.id){
		$('liste_'+e.id).up().addClassName('on');
		$('liste_'+e.id).insert({bottom:'<li id="liste_'+e.id+'_'+li.id+'"><span>'+li.innerHTML+'</span><a href="javascript:suppItemAC(\''+e.id+'\',\''+li.id+'\',\''+li.innerHTML+'\');">[supp]</a></li>'});
		tabIdAC['liste_'+e.id].push(li.id);
		$('valeur_'+e.id).value = tabIdAC['liste_'+e.id];
		tabIdAC['nom_'+e.id].push(li.innerHTML);
		$('localisation_recherche').value = tabIdAC['nom_'+e.id].join(' - ');
	}
	$(e.id).value = $(e.id).defaultValue;
	$(e.id).blur();
	lancerRecherche($F('num_page'));
};
var suppItemAC = function(tab,num,valeur){
	tabIdAC['liste_'+tab] = tabIdAC['liste_'+tab].without(num);
	tabIdAC['nom_'+tab] = tabIdAC['nom_'+tab].without(valeur);
	$('valeur_'+tab).value = tabIdAC['liste_'+tab];
	$('localisation_recherche').value = tabIdAC['nom_'+tab];
	$('liste_'+tab+'_'+num).remove();
	if($F('valeur_'+tab) == ''){
		$('liste_'+tab).up().removeClassName('on');
	}
	lancerRecherche($F('num_page'));
};

var suppCritere = function(id){
	$(id).remove();
	lancerRecherche($F('num_page'));
};


var rollItem = function(rule){
	$$(rule).each(function(e){
		e.stopObserving('mouseenter');							  
		e.stopObserving('mouseleave');
		e.observe('mouseenter',function(){
			e.addClassName('on');
		});
		e.observe('mouseleave',function(){
			e.removeClassName('on');
		});
	});
};

// CALCUL DES HAUTEURS
var hauteurRub = function(rule){
	var tab = new Array();
	$$(rule).each(function(e,i){
		tab.push(e.getStyle('height'));
	});
	$$(rule).each(function(e,i){
		e.setStyle({height : tab.max()});
	});
};

// VISIONNEUSE ANNONCE
var visionneuse = function(num){
	if($('photo')){
		centrerV('photo');
	}
	pos = num;
	tab_photo = new Array();
	$$('#galerie_photo1 li a').each(function(e,i){
		tab_photo.push(e.href);
		e.observe('click',function(event){
			$('detail_photo').update('<img id="photo" src="'+e.href+'" alt="" />');
			$('photo').observe('load',function(){
				centrerV('photo');
			});
			e.up().siblings().each( function(n){
				n.down('a').removeClassName('on');								
			});
			e.addClassName('on');
			pos = i
			if(pos > 0){
				$('precedent_galerie').removeClassName('off');
			}else{
				$('precedent_galerie').addClassName('off');
			}
			if(pos < tab_photo.size()-1){
				$('suivant_galerie').removeClassName('off');
			}else{
				$('suivant_galerie').addClassName('off');
			}
			event.stop();
		});
	});
	$$('#galerie_photo1 li a')[num].addClassName('on');
	if(pos > 0){
		$('precedent_galerie').removeClassName('off');
		$('precedent_galerie').stopObserving('click');
		$('precedent_galerie').observe('click',function(){
			$$('#galerie_photo1 li a').each(function(t){
				t.removeClassName('on');											
			});
			pos--;
			$$('#galerie_photo1 li a')[pos].addClassName('on');
			$('detail_photo').update('<img id="photo" src="'+tab_photo[pos]+'" alt="" />');
			$('photo').observe('load',function(){
				centrerV('photo');
			});
			visionneuse(pos);
		});
	}else{
		$('precedent_galerie').addClassName('off');
		$('precedent_galerie').stopObserving('click');
	}
	if(pos < tab_photo.size()-1){
		$('suivant_galerie').removeClassName('off');
		$('suivant_galerie').stopObserving('click');
		$('suivant_galerie').observe('click',function(){
			$$('#galerie_photo1 li a').each(function(t){
				t.removeClassName('on');											
			});
			pos++;
			$$('#galerie_photo1 li a')[pos].addClassName('on');
			$('detail_photo').update('<img id="photo" src="'+tab_photo[pos]+'" alt="" />');
			$('photo').observe('load',function(){
				centrerV('photo');
			});
			visionneuse(pos);
		});
	}else{
		$('suivant_galerie').addClassName('off');
		$('suivant_galerie').stopObserving('click');
	}
};
var centrerV = function(id){
	$(id).setStyle({
		position:'absolute',
		top:'50%',
		left:'50%',
		marginLeft:'-'+$(id).offsetWidth/2+'px',
		marginTop:'-'+$(id).offsetHeight/2+'px'
	});	
};

/* BOUTONS GMAP */
var creerBoutons = function(bt){
	$("gmap").insert({top:'<div id="bt_map"></div>'});
	$("bt_map").insert({bottom:'<a href="javascript:void(0)" id="bt_plan" class="bt_map on"><span>Plan</span></a>'});
	$("bt_map").insert({bottom:'<a href="javascript:void(0)" id="bt_mixte" class="bt_map"><span>Mixte</span></a>'});
	$("bt_map").insert({bottom:'<a href="javascript:void(0)" id="bt_satelite" class="bt_map"><span>Satelitte</span></a>'});

	// ZOOM
	$("gmap").insert({top:'<div id="bt_zoom_map"></div>'});
	$("bt_zoom_map").insert({bottom:'<a href="javascript:void(0)" id="bt_zoom_in" class="bt_zoom_in"><span>Zoom +</span></a>'});
	$("bt_zoom_map").insert({bottom:'<a href="javascript:void(0)" id="bt_zoom_out" class="bt_zoom_out"><span>Zoom -</span></a>'});


	Event.observe('bt_zoom_in','click',function(){
		map.zoomIn(false, false)
	});
	Event.observe('bt_zoom_out','click',function(){
		map.zoomOut(false, false)
	});
	Event.observe('bt_satelite','click',function(){
		changeCss(this);							   
		map.setMapType(G_SATELLITE_MAP);
	});
	Event.observe('bt_mixte','click',function(){
		changeCss(this);							   
		map.setMapType(G_HYBRID_MAP);
	});
	Event.observe('bt_plan','click',function(){
		changeCss(this);							   
		map.setMapType(G_NORMAL_MAP);
	});
};
var changeCss = function(bt){
	$$("#bt_map a").each(function(e){
		e.removeClassName("on");
	});
	bt.addClassName("on");
};
var masqueBt = function(bt){
	$('bt_map').hide();
	$('bt_zoom_map').hide();
};
var afficheBt = function(bt){
	$('bt_map').show();
	$('bt_zoom_map').show();
};

/* VERIFS */
var frmContactPro = function(id){
	param_form = { mep: 'msg_champ', autoScroll: false, action:'submit'};
	var champs_form ={};
	$$('#frm_contact_pro'+id+' input').each(function(e){
		if(e.id.include('newsletter_contact') == false && e.id.include('info_contact') == false && e.id!= ''){
			if(e.id.include('nom_contact') == true){
				champs_form[e.id] = {alerte:'Votre nom est obligatoire'};
			}
			if(e.id.include('email_contact') == true){
				champs_form[e.id] = {type:'mel', alerte:'Votre email est obligatoire et doit &ecirc;tre valide'};
			}
			if(e.id.include('tel_contact') == true){
				champs_form[e.id] = {alerte:'Votre t&eacute;l&eacute;phone est obligatoire'};
			}
		}
	});
	$$('#frm_contact_pro'+id+' textarea').each(function(e){
		champs_form[e.id] = {alerte:'Votre message est obligatoire'};
	});
	
	var res = formVerif('frm_contact_pro'+id, champs_form, param_form);
	if(res == true){
		//p = $('frm_contact_pro'+id).serialize();
		new Ajax.Request('./_ajax.php?do=contactPro',{
			parameters : {
				nom_contact : $F('nom_contact'+id),
				email_contact : $F('email_contact'+id),
				tel_contact : $F('tel_contact'+id),
				message_contact : $F('message_contact'+id),
				newsletter_contact : $F('newsletter_contact'+id),
				ad_adidt : $F('msg_adidt'+id),
				form : 'frm_contact_pro'+id
			},
			onComplete:function(transport){
				transport = transport.responseText.evalJSON();
				if(transport.statut == 'ok'){
					if(!$('retour_contact')){
						$('frm_contact_pro'+id).insert({top:'<div id="retour_contact" class="confirmation"><p>'+transport.message+'</p></div>'});
					}else{
						$('retour_contact').className = 'confirmation';
						$('retour_contact').update(transport.message);
					}
					window.setTimeout(function(){
						new Effect.Fade('retour_contact',{duration:0.5, afterFinish:
							function(){
								$('retour_contact').remove();
								$('nom_contact').value = '';
								$('email_contact').value = '';
								$('tel_contact').value = '';
								$('message_contact').value = '';
							}
						});
					},5000);
				}else{
					if(!$('retour_contact')){
						$('frm_contact_pro'+id).insert({top:'<div id="retour_contact" class="erreur"><p>'+transport.message+'</p></div>'});
					}else{
						$('retour_contact').className = 'erreur';
						$('retour_contact').update(transport.message);
					}
				}
			}
		});
		return false;
	}else{
		return formVerif('frm_contact_pro'+id, champs_form, param_form);
	}
};

var frmMdp = function(id){
	param_form = { mep: 'msg_champ', autoScroll: false, action:'submit'};
	var champs_form = {
		email_mdp : {type:'mel', alerte:'Votre email est obligatoire et doit &ecirc;tre valide'}
	}
	var res = formVerif('frm_mdp', champs_form, param_form);
	if(res == true){
		p = $('frm_mdp').serialize();
		new Ajax.Request('./_ajax.php?do=envoiMdp',{
			parameters : p,
			onComplete:function(transport){
				tpl = '<p>Votre mot de passe vous a bien &eacute;t&eacute; envoy&eacute;.</p>';
				if(transport.responseText == ''){
					if(!$('retour_mdp')){
						$('frm_mdp').insert({top:'<div id="retour_mdp" class="confirmation">'+tpl+'</div>'});
					}else{
						$('retour_mdp').className = 'confirmation';
						$('retour_mdp').update(tpl);
					}
					window.setTimeout(function(){
						ib_connexion.fermer();
					},2000);
				}else{
					if(!$('retour_mdp')){
						$('frm_mdp').insert({top:'<div id="retour_mdp" class="erreur"><p>'+transport.responseText+'</p></div>'});
					}
				}
			}
		});
		return false;
	}else{
		return formVerif('frm_mdp', champs_form, param_form);
	}
};

var frmInscription = function(id){
	param_form = { mep: 'msg_global', autoScroll: false, action:'submit'};
	var champs_form = {
		email_inscription : {type:'mel', alerte:'Votre email est obligatoire et doit &ecirc;tre valide'},
		mdp_inscription :  {minchar:5, alerte:'Votre mot de passe est obligatoire'},
		mdp_inscription_2 :  {minchar:5, alerte:'La confirmation de votre mot de passe est obligatoire'}
	}
	return formVerif('frm_inscription', champs_form, param_form, {titreMessage :' ', idMsgGlobal : 'divError'});
};

//Creation compte
var frmConnexion = function(){
	param_form = { mep:'msg_global', autoScroll:false, action: 'submit'};
	champs_form = {
		email_connexion: {type:'mel', alerte:'Votre email est obligatoire et doit &ecirc;tre valide.'},
		mdp_connexion :  {minchar:5, alerte:'Le mot de passe doit dÃƒÂ©passer les 5 caractÃƒÂ¨res'}
	};
	return formVerif('frm_identification', champs_form, param_form,{idMsgGlobal:'msgIdentification'});
};

/* MEMBRE */

var frmSubscribe = function(id){
	param_form = { mep: 'msg_champ', autoScroll: false, action:'submit'};
	var champs_form ={};
	
	$$('#frm_sinscrire'+id+' input').each(function(e){
		if(e.id.include('newsletter_inscription') == false && e.id!= ''){
			
			if(e.id.include('nom_coordonnees') == true){
				champs_form[e.id] = {alerte:'Merci saisir votre nom'};
			}
			if(e.id.include('prenom_coordonnees') == true){
				champs_form[e.id] = {alerte:'Merci de saisir votre prénom'};
			}	
			if(e.id.include('pseudo_coordonnees') == true){
				champs_form[e.id] = {alerte:'Merci de saisir un pseudo valide'};
			}			
			if(e.id.include('email_coordonnees') == true){
				champs_form[e.id] = {type:'mel', alerte:'Votre email est obligatoire et doit &ecirc;tre valide'};
			}
						
		}
	});

	
	var res = formVerif('frm_sinscrire'+id, champs_form, param_form);
	if(res == true){
		new Ajax.Request('../_ajax.php?do=Subscribe',{
			parameters : {
				prenom_membre : $F('prenom_coordonnees'+id),
				email_membre : $F('email_coordonnees'+id),
				email_membre_confirmation : $F('email_coordonnees_2'+id),
				pseudo_membre : $F('pseudo_coordonnees'+id),
				pass_membre : $F('mdp_coordonnees'+id),
				pass_membre_confirmation : $F('mdp_coordonnees_2'+id),
				newsletter_inscription : $F('newsletter_inscription'+id)
			},
			onComplete:function(transport){
				transport = transport.responseText.evalJSON();
				if(transport.statut == 'ok'){
					if(!$('retour_sinscrire')){
						$('frm_sinscrire'+id).insert({top:'<div id="retour_sinscrire" class="confirmation"><p>'+transport.message+'</p></div>'});
					}else{
						$('retour_sinscrire').className = 'confirmation';
						$('retour_sinscrire').update(transport.message);
					}
					window.setTimeout(function(){
						new Effect.Fade('retour_sinscrire',{duration:0.5, afterFinish:
							function(){
								$('retour_sinscrire').remove();
								$('prenom_coordonnees').value = '';
								$('email_coordonnees').value = '';
								$('pseudo_coordonnees').value = '';
								$('mdp_coordonnees').value = '';
							}
						});
					},5000);
				}else{
					if(!$('retour_sinscrire')){
						$('frm_sinscrire'+id).insert({top:'<div id="retour_sinscrire" class="erreur"><p>'+transport.message+'</p></div>'});
					}else{
						$('retour_sinscrire').className = 'erreur';
						$('retour_sinscrire').update(transport.message);
					}
				}
			}
		});
		return false;
	}else{
		return formVerif('frm_sinscrire'+id, champs_form, param_form);
	}
};


var frmAvis = function(){
	param_form = { mep: 'msg_champ', autoScroll: false, action:'submit'};
	var champs_form = {
		message_avis : {alerte:'Votre message est obligatoire'}
	}
	var res = formVerif('frm_avis', champs_form, param_form);
	if(res == true){
		p = $('frm_avis').serialize();
		new Ajax.Request('./_ajax.php?do=donnerAvis',{
			parameters : p,
			onComplete:function(transport){
				//db('A'+transport.responseText+'A');
				tpl = 'Votre message a bien &eacute;t&eacute; envoy&eacute;.';
				if(transport.responseText == ''){
				
					if(!$('retour_avis')){
						$$('#frm_avis .f_form')[0].insert({top:'<div id="retour_avis" class="confirmation">'+tpl+'</div>'});
					}else{
						$('retour_avis').className = 'confirmation';
						$('retour_avis').update(tpl);
					}
					window.setTimeout(function(){
						new Effect.Fade('retour_avis',{duration:0.5, afterFinish:
							function(){
								$('retour_avis').remove();
								$('message_avis').value = '';
							}
						});
					},3000);
				}else{
					if(!$('retour_avis')){
						$$('#frm_avis .f_form')[0].insert({top:'<div id="retour_avis" class="erreur">'+transport.responseText+'</div>'});
					}
				}
			}
		});
		return false;
	}else{
		return formVerif('frm_avis', champs_form, param_form);
	}
};

// Ajout ville
var frmAjoutVille = function(){
	param_form = { mep: 'msg_champ', autoScroll: false, action:'submit'};
	var champs_form = {
		sCountry : {alerte:'Merci de renseigner le Pays'},
		newCity : {alerte:'Merci de renseigner une ville valide'}
	}
	var res = formVerif('frm_ajoutville', champs_form, param_form);
	if(res == true){
		p = $('frm_ajoutville').serialize();
		new Ajax.Request('./_ajax.php?do=ajoutVille',{
			parameters : p,
			onComplete:function(transport){
				transport = transport.responseText.evalJSON();
				if(transport.statut == 'ok'){
					if(!$('retour_newCity')){
						$$('#frm_ajoutville .adr_sug')[0].insert({top:'<div id="retour_newCity" class="confirmation">'+transport.message+'</div>'});
					}else{
						$('retour_newCity').className = 'confirmation';
						$('retour_newCity').update(transport.message);
					}
					window.setTimeout(function(){
						new Effect.Fade('retour_newCity',{duration:0.5, afterFinish:
							function(){
								$('retour_newCity').remove();
								$('newCity').value = '';
							}
						});
					},3000);
				}else{
					if(!$('retour_newCity')){
						$$('#frm_ajoutville .adr_sug')[0].insert({top:'<div id="retour_newCity" class="erreur">'+transport.message+'</div>'});
					}else{
						$('retour_newCity').className = 'erreur';
						$('retour_newCity').update(transport.message);
					}
				}
			}
		});
		return false;
	}else{
		return formVerif('frm_ajoutville', champs_form, param_form);
	}
};


var frmEmail = function(){
	param_form = { mep: 'msg_champ', autoScroll: false, action:'submit'};
	var champs_form = {
		email_login : {type:'mel', alerte:'Votre email est obligatoire et doit &ecirc;tre valide.'}
	}
	return formVerif('frm_email', champs_form, param_form);
};
var frmMdp = function(){
	param_form = { mep: 'msg_champ', autoScroll: false, action:'submit'};
	var champs_form = {
		mdp : {minchar:5, alerte:'Votre mot de passe est obligatoire et doit &ecirc;tre valide.'}
	}
	return formVerif('frm_mdp', champs_form, param_form);
};
var frmDemande = function(){
	param_form = { mep: 'msg_champ', autoScroll: false, action:'submit'};
	var champs_form = {
		nom_demande : {alerte:'Votre nom est obligatoire.'},
		prenom_demande : {alerte:'Votre pr&eacute;nom est obligatoire.'},
		tel_demande : {alerte:'Votre t&eacute;l&eacute;phone est obligatoire.'},
		cp_demande : {alerte:'Votre code postal est obligatoire.'}
	}
	return formVerif('frm_demande', champs_form, param_form);
};


// VERIF AJAX INSCRIPTION

//if($('checkemail_coordonnees')) checkAjax();

function verifAjax( champ ){
	new Ajax.Request( '_ajax.php?do=CheckValueAvaibility', {
		method: 'post',
		parameters: {
			field:champ.name,
			value: champ.value
		},
		onSuccess: function(transport)
		{
			var response = transport.responseText;
			
			$('check' + champ.id ).className = '' ;
			champ.removeClassName('inputError');
			$('check' + champ.id ).update('&nbsp;');
			
			if( response == 'bad' )
			{
				if (champ.id == 'pseudo_coordonnees'){
					$('check' + champ.id ).addClassName( 'bad' ).update('Ce pseudo est d&eacute;j&agrave; utilis&eacute;');
					champ.addClassName('inputError');
				} else {
					$('check' + champ.id ).addClassName( 'bad' ).update('Cet e-mail est d&eacute;j&agrave; utilis&eacute;');
					champ.addClassName('inputError');
				}
			}
			else if( response == 'wrong' && champ.id == 'email_coordonnees' )
			{
				$('check' + champ.id ).addClassName( 'bad' ).update('Cet e-mail n\'est pas valide');
				champ.addClassName('inputError');
			}
			else
			{
				$('check' + champ.id ).addClassName( 'ok' ) ;
			}
		}
	});
}
var checkAjax = function(){
	var input = $('#email_coordonnees, #pseudo_coordonnees');
	input.each( function(e,index){
		Event.observe(e, 'blur', function(evt){
			if( $F( e.id ) != '' ) verifAjax( $( e.id ) );
			else $( 'check' + e.id ).className = '' ;
		});			 
	});
};

/* AJOUT AL 2001-01-29 */
var frmRecherche = function(champ){
	if($F(champ) != '' && $F(champ) != $(champ).defaultValue){
		return true;
	}else{
		$(champ).addClassName('inputError');
		return false;
	}
};
var frmHoraire = function(){
	param_form = { mep: 'msg_champ', autoScroll: false, action:'submit'};
	var champs_form = {
		PrayerCities : {alerte:'Merci de saisir une ville.'}
	}
	return formVerif('frm_horaire', champs_form, param_form);
};
var frmAdditif = function(){
	param_form = { mep: 'msg_champ', autoScroll: false, action:'submit'};
	var champs_form = {
		AddTitl : {alerte:'Merci de saisir un additif.'}
	}
	return formVerif('frm_additif', champs_form, param_form);
};

var frmRechercheLeft = function(){
	param_form = { mep: 'msg_champ', autoScroll: false, action:'submit'};
	var champs_form = {};

	if($F('lCities') == '' || $F('lCities') == $('lCities').defaultValue){
		champs_form['lCities'] = {alerte:'Merci de saisir une ville.'};
	}
	nb_radio = 0;
	$$('#frm_recherche .ligne.radio input').each(function(e,i){
		if(e.checked == true){
			nb_radio++;
		}
	})
	if(nb_radio == 0){
		champs_form['verif_radio'] = {alerte:'Merci de cocher un type d\'adresse.'};
	}else{
		$('verif_radio_erreur').hide();
	}
	return formVerif('frm_recherche', champs_form, param_form);
};

var makeItCount = function(id, maxsize, limited){
	if(limited == null) limited = true;
	if ($(id)){
		Event.observe($(id), 'keyup', function(){charCounter(id, maxsize, limited);}, false);
		Event.observe($(id), 'keydown', function(){charCounter(id, maxsize, limited);}, false);
		charCounter(id,maxsize,limited);
	}
};

//-- Compteur Caracteres --//
var charCounter = function(id, maxlimit, limited){
	if (!$('counter-'+id)){
		$(id).insert({after: '<div id="counter-'+id+'"></div>'});
	}
	if($F(id).length >= maxlimit){
		if(limited){	$(id).value = $F(id).substring(0, maxlimit); }
		$('counter-'+id).addClassName('charcount-limit');
		$('counter-'+id).removeClassName('charcount-safe');
	} else {	
		$('counter-'+id).removeClassName('charcount-limit');
		$('counter-'+id).addClassName('charcount-safe');
	}
	//$('counter-'+id).update( $F(id).length + '/' + maxlimit );
	var restant = maxlimit - $F(id).length;
	if(restant > 1) restant = restant + " caract&egrave;res restants";
	else restant = restant + " caract&egrave;re restant";
	$('counter-'+id).update(restant);
		
};

var makeItCount = function(id, maxsize, limited){
	if(limited == null) limited = true;
	if ($(id)){
		Event.observe($(id), 'keyup', function(){charCounter(id, maxsize, limited);}, false);
		Event.observe($(id), 'keydown', function(){charCounter(id, maxsize, limited);}, false);
		charCounter(id,maxsize,limited);
	}
};

var frmAddComment = function(id){
	param_form = { mep: 'msg_champ', autoScroll: false, action:'submit'};
	var champs_form ={};
	
	$$('#frm_add_comment'+id+' input').each(function(e){
		if(e.id.include('comt_cgu') == false && e.id!= ''){
			
			if(e.id.include('comt_note').value=0){
				champs_form[e.id] = {alerte:'Merci de donner une note de votre choix'};
			}
			if(e.id.include('comt_pseudo') == true){
				champs_form[e.id] = {alerte:'Merci de saisir un pseudo valide'};
			}			
			if(e.id.include('comt_email') == true){
				champs_form[e.id] = {type:'mel', alerte:'Votre email est obligatoire et doit &ecirc;tre valide'};
			}			
			if(e.id.include('comt_cgu') == true){
				champs_form[e.id] = {alerte:'Merci de valider les Conditions GÃ©nÃ©rales d\'utilisations'};
			}
						
		}
	});
	
	$$('#frm_add_comment'+id+' textarea').each(function(e){
		champs_form[e.id] = {alerte:'Votre message est obligatoire'};
	});
	
	var res = formVerif('frm_add_comment'+id, champs_form, param_form);
	if(res == true){
		//p = $('frm_add_comment'+id).serialize();	
		new Ajax.Request('./_ajax.php?do=AddComment',{
			parameters : {
				Note : $F('comt_note'+id),
				Pseudo : $F('comt_pseudo'+id),
				Mail : $F('comt_email'+id),
				Message : $F('comt_txt'+id),
				Cgu : $F('comt_cgu'+id),
				Type : $F('comt_type'+id),
				Addidt : $F('comt_adidt'+id),
				Hadid : $F('comt_haid'+id)
			},
			onComplete:function(transport){
				transport = transport.responseText.evalJSON();
				if(transport.statut == 'ok'){
					if(!$('retour_contact')){
						$('frm_add_comment'+id).insert({top:'<div id="retour_contact" class="confirmation"><p>'+transport.message+'</p></div>'});
					}else{
						$('retour_contact').className = 'confirmation';
						$('retour_contact').update(transport.message);
					}
					window.setTimeout(function(){
						new Effect.Fade('retour_contact',{duration:0.5, afterFinish:
							function(){
								$('retour_contact').remove();
								$('comt_pseudo').value = '';
								$('comt_email').value = '';
								$('comt_txt').value = '';
								$('comt_cgu').checked = false;
							}
						});
					},5000);
				}else{
					if(!$('retour_contact')){
						$('frm_add_comment'+id).insert({top:'<div id="retour_contact" class="erreur"><p>'+transport.message+'</p></div>'});
					}else{
						$('retour_contact').className = 'erreur';
						$('retour_contact').update(transport.message);
					}
				}
			}
		});
		return false;
	}else{
		return formVerif('frm_add_comment'+id, champs_form, param_form);
	}
};

var Kawlamaka = {
	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=.",
	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;
		input = Kawlamaka._utf8_encode(input);
		while (i < input.length) {
			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);
			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;
			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}
			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
		}
		return output;
	},

	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;
		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
		while (i < input.length) {
			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));
			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;
			output = output + String.fromCharCode(chr1);
			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}
		}
		output = Kawlamaka._utf8_decode(output);
		return output;
	},

	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
		for (var n = 0; n < string.length; n++) {
			var c = string.charCodeAt(n);
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
		}
		return utftext;
	},

	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
		while ( i < utftext.length ) {
			c = utftext.charCodeAt(i);
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
		}
		return string;
	}
};
