/*FONCTION AJAX*/
function getXhr(){
	var xhr = null; 
	if(window.XMLHttpRequest) xhr = new XMLHttpRequest(); 
	else if(window.ActiveXObject){
		try{
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e){
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	else{
		alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
		xhr = false; 
	} 
	return xhr
}

/*AFFICHE AVEC AJAX*/
function ouvre_page(div,page,variable,type){
	var xhr = getXhr();
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
			document.getElementById(div).innerHTML = xhr.responseText;
		}
	}
	xhr.open(type,page,true);
	if(type=="POST"){
		xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhr.send(variable);
	}
	else xhr.send(null);
}

/*AFFICHE LES SELECTIONS DANS LES CHAMPS DU FORMULAIRE DE RECHERCHE SALLES*/
function aff_dans_input(type,valeur){
	var xhr = getXhr();
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
			document.getElementById('search_'+type).value = xhr.responseText;
			if(valeur=='none') document.getElementById('search_'+type).className = 'input_search';
			else document.getElementById('search_'+type).className = 'input_search_select';
		}
	}
	xhr.open('GET','moteur_rech_salle.php?type='+type+'&valeur='+valeur,true);
	xhr.send(null);
}

/*AFFICHE LE CALENDRIER DANS LA DEMANDE DE DEVIS*/
function aff_calendrier(from, num,date){
	if(from=='devis' && document.getElementById('calendrier'+num).style.display=='block') document.getElementById('calendrier'+num).style.display = 'none';
	else{
		document.getElementById('calendrier'+num).style.display = 'block';
		ouvre_page('calendrier'+num,'calendrier.php?num='+num+'&date_select='+date,'','GET');
	}
}

/*AFFICHE LA DATE CHOISIE DANS L'INPUT*/
function enr_calendrier(num,date){
	document.getElementById('calendrier'+num).style.display = 'none';
	document.getElementById('date'+num).value = date;
}

/*ENVOI DU FORMULAIRE CONSEILLER A UN AMI*/
function conseil_ami(num){
	if(num==1) var formulaire = document.forms.form_conseiller_1;
	else var formulaire = document.forms.form_conseiller_2;
	
	var xhr = getXhr();
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
			var valeur = xhr.responseText;
			if(valeur==1){
				document.getElementById('envoi_ok_conseil_'+num).style.display = 'block';
				document.getElementById('erreur_conseil_'+num).style.display = 'none';
				document.getElementById('bloc_conseiller_'+num).style.display = 'none';
				document.getElementById('erreur_conseil_'+num).innerHTML = '';
			}
			else{
				document.getElementById('erreur_conseil_'+num).style.display = 'block';
				document.getElementById('envoi_ok_conseil_'+num).style.display = 'none';
				document.getElementById('erreur_conseil_'+num).innerHTML = valeur;
			}
		}
	}
	
	var variables = 'nom_client='+formulaire.nom_client.value+'&prenom_client='+formulaire.prenom_client.value+'&email_client='+formulaire.email_client.value;
	variables = variables+'&nom_ami='+formulaire.nom_ami.value+'&prenom_ami='+formulaire.prenom_ami.value+'&email_ami='+formulaire.email_ami.value;
	variables = variables+'&id_adherent='+formulaire.id_adherent.value+'&type_adherent='+formulaire.type_adherent.value;
	
	xhr.open('POST','mail_conseille.php',true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send(variables);
}

/*AFFICHE OU MASQUE DES BLOCS*/
function display_block(bloc) {
	if(document.getElementById(bloc).style.display == 'block') document.getElementById(bloc).style.display = 'none';
	else document.getElementById(bloc).style.display = 'block';
}

/*AFFICHE ET MASQUE LES BLOCS DE TEXTES DE LA PAGE LILLE*/
function aff_txt_lille(bloc,txt) {
	var liste_div = document.getElementById('bloc_txt_lille_'+bloc).getElementsByTagName('div');
	for(var i=0;i<liste_div.length;i++){
		if(liste_div[i].id){
			if(liste_div[i].id == 'txt_lille_'+txt) liste_div[i].style.display = 'block';
			else liste_div[i].style.display = 'none';
		}
	}
}

/*TABLEAU CHAMP DU FORMULAIRE*/
var tab_num_search = new Array();
tab_num_search[1] = 'type_evt';
tab_num_search[2] = 'lieu';
tab_num_search[3] = 'nb_pers';
tab_num_search[4] = 'config';

/*AFFICHE OU MASQUE LES BLOCS DE RECHERCHE*/
function display_search(num) {	
	for(var i=1;i<tab_num_search.length;i++){
		if(num==i){
			if(document.getElementById('detail_'+tab_num_search[i]).style.display=='block') document.getElementById('detail_'+tab_num_search[i]).style.display = 'none';
			else document.getElementById('detail_'+tab_num_search[i]).style.display = 'block';
		}
		else document.getElementById('detail_'+tab_num_search[i]).style.display = 'none';
	}
}

/*COCHE TOUTES LES CASES D'UN BLOC DU FORMULAIRE DE RECHERCHE*/
function checked_all(num){
	var list_input = document.getElementById('detail_'+tab_num_search[num]).getElementsByTagName('input');
	if(document.getElementById(tab_num_search[num]+'_all').checked==true) var all_checked = 'checked';
	else var all_checked = '';
	
	for(var i=0;i<list_input.length;i++){
		if(list_input[i].id!=tab_num_search[num]+'_all') list_input[i].checked = all_checked;
	}
	
	if(all_checked=='checked') aff_dans_input(tab_num_search[num],'all');
	else aff_dans_input(tab_num_search[num],'none');
}

/*ENVOI LES VALEURS COCHEES POUR LES AFFICHER DANS L'INPUT*/
function envoi_checked(num){
	var list_input = document.getElementById('detail_'+tab_num_search[num]).getElementsByTagName('input');
	var liste_envoi = '';
	var nb = 0;
	
	for(var i=0;i<list_input.length;i++){
		if(list_input[i].id!=tab_num_search[num]+'_all' && list_input[i].checked==true){
			if(liste_envoi=='') liste_envoi = list_input[i].value;
			else liste_envoi = liste_envoi+'-'+list_input[i].value;
			nb = nb+1;
		}
	}
	
	if(liste_envoi=='') aff_dans_input(tab_num_search[num],'none');
	else{
		if(nb==(list_input.length-1)){
			document.getElementById(tab_num_search[num]+'_all').checked = 'checked';
			aff_dans_input(tab_num_search[num],'all');
		}
		else{
			document.getElementById(tab_num_search[num]+'_all').checked = '';
			aff_dans_input(tab_num_search[num],liste_envoi);
		}
	}
}

/*AFFICHE LE NOMBRE DE PERSONNES DANS LE MOTEUR DE RECHERCHE SALLE*/
function aff_nb_pers(val, efface){
	if(efface=='eff') document.getElementById('nb_pers_eff').value = '';
	
	if(val==''){
		document.getElementById('nb_pers_select').value = '';
		aff_dans_input('nb_pers','none');
	}
	else{
		document.getElementById('nb_pers_select').value = val;
		aff_dans_input('nb_pers',val);
	}
}

/*GOOGLE MAP SALLE*/
function aff_coordonnees(lat,lng) {
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("googlemap_salle"), {mapTypes:[G_NORMAL_MAP,G_SATELLITE_MAP]});
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(parseFloat(lat), parseFloat(lng)), 13);
		
		var point = new GLatLng(parseFloat(lat), parseFloat(lng));
		var marker = new GMarker(point);
		map.addOverlay(marker);
	}
}

/*AFFICHE UNE GRANDE PHOTO SALLE*/
function aff_gde_photo(nom,id){
	ouvre_page('gd_photo','salle_detail.php?nom_photo='+nom+'&id='+id,'','GET');
}

/*AFFICHE UNE GRANDE PHOTO PRESTATAIRE*/
function aff_gde_photo_presta(nom,id){
	ouvre_page('gd_photo','prestataire_detail.php?nom_photo='+nom+'&id='+id,'','GET');
}

/*AFFICHE UNE GRANDE PHOTO AGENCE*/
function aff_gde_photo_agence(nom,id){
	ouvre_page('gd_photo','agences_detail.php?nom_photo='+nom+'&id='+id,'','GET');
}

/*AFFICHE LE PANIER (SUIVANT ET PRECEDENT)*/
function aff_panier(num){
	ouvre_page('panier_salle','panier_salle.php?type=aff&num='+num,'','GET');
}

/*SUPPRIME UNE SALLE DU PANIER*/
function suppr_panier(id,num){
	ouvre_page('panier_salle','panier_salle.php?type=suppr&id='+id+'&num='+num,'','GET');
}

/*SUPPRIME UNE SALLE DU PANIER DANS LA DEMANDE DE DEVIS*/
function suppr_panier_devis(id){
	ouvre_page('panier_devis','devis_salle.php?type=suppr&id='+id,'','GET');
}

/*AJOUTE UNE SALLE AU PANIER*/
function ajout_panier(id){
	ouvre_page('panier_salle','panier_salle.php?type=ajout&id='+id,'','GET');
}

/*RECUPERER LA VALEUR COCHEE D'UN BOUTON RADIO*/
function testerRadio(radio){
	for(var i=0;i<radio.length;i++){
		if(radio[i].checked) return radio[i].value;
	}
}

/*AFFICHER/MASQUER LE CHAMP SOCIETE*/
function aff_societe_form(cat){
	if(cat=='pro') document.getElementById('bloc_societe').style.display = 'block';
	else document.getElementById('bloc_societe').style.display = 'none';
}

/*AFFICHER LE FORMULAIRE CORRESPONDANT AU CONTACT*/
function aff_form(type){
	if(type=='contact'){
		document.getElementById('bloc_cat_pro').style.display = 'block';
		var catpro = testerRadio(document.forms.form_contact.cat_pro);
		if(catpro=='pro') document.getElementById('bloc_societe').style.display = 'block';
		else document.getElementById('bloc_societe').style.display = 'none';
		document.getElementById('bloc_siret').style.display = 'none';
		document.getElementById('bloc_objet').style.display = 'block';
	}
	else{
		document.getElementById('bloc_cat_pro').style.display = 'none';
		document.getElementById('bloc_societe').style.display = 'block';
		document.getElementById('bloc_siret').style.display = 'block';
		document.getElementById('bloc_objet').style.display = 'none';
	}
}

/*AFFICHE LES PHOTOS OU LA VIDEO DANS LA PAGE DETAIL*/
function aff_visuel(type){
	if(type=='photo'){
		document.getElementById('bloc_visuel_photos').style.display = 'block';
		document.getElementById('bloc_visuel_video').style.display = 'none';
	}
	else{
		document.getElementById('bloc_visuel_photos').style.display = 'none';
		document.getElementById('bloc_visuel_video').style.display = 'block';
	}
}

/*GRANDE GOOGLE MAP ADHERENT*/
function aff_gde_gm(lat,lng) {
	if (GBrowserIsCompatible()) {
		var mapplan = new GMap2(document.getElementById("grande_google_map"), {mapTypes:[G_NORMAL_MAP,G_SATELLITE_MAP]});
		mapplan.addControl(new GLargeMapControl());
		mapplan.addControl(new GMapTypeControl());
		mapplan.setCenter(new GLatLng(parseFloat(lat), parseFloat(lng)), 15);
		
		var baseIcon = new GIcon();
		baseIcon.iconSize=new GSize(32,32);
		baseIcon.shadowSize=new GSize(0,0);
		baseIcon.iconAnchor=new GPoint(16,32);
		baseIcon.infoWindowAnchor=new GPoint(16,32);
		
		var iconspe = new GIcon(baseIcon, "imgs/ico_google_map.png", null, null);
		var point = new GLatLng(parseFloat(lat), parseFloat(lng));
		var marker = new GMarker(point,iconspe);
		mapplan.addOverlay(marker);
	}
}

/*GOOGLE MAP ITINERAIRE*/
var geocoder = null;
var addressMarker;
var gdir;

function load_itineraire(adresse) {
  if (GBrowserIsCompatible()) {      
	var map = new GMap2(document.getElementById("map_itineraire"));

	gdir = new GDirections(map, document.getElementById("liste_directions"));
	GEvent.addListener(gdir, "load", onGDirectionsLoad);
	GEvent.addListener(gdir, "error", handleErrors);

	setDirections("Paris", adresse);
  }
}

function setDirections(adresse_from, adresse_to) {
  gdir.load("from: " + adresse_from + " to: " + adresse_to,{ "locale": "fr_FR" });
}

function handleErrors(){
   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS) alert("ERREUR : L'une des adresses est fausse ou inconnue.");
   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR) alert("ERREUR : Itinéraire introuvable.");
   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY) alert("ERREUR : Itinéraire introuvable."); 
   else if (gdir.getStatus().code == G_GEO_BAD_KEY) alert("ERREUR : La carte ne peut être affichée.");
   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST) alert("ERREUR : Itinéraire introuvable.");
   else alert("ERREUR");
}

function onGDirectionsLoad(){ 
	// Use this function to access information about the latest load()
	// document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
}

function RunFoo(swf, hauteur, largeur) {
document.write("<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0\" width=\""+hauteur+"\" height=\""+largeur+"\">");
document.write("<param name=\"movie\" value=\""+swf+"\" />");
document.write("<param name=\"quality\" value=\"high\" />");
document.write("<param name=\"menu\" value=\"false\" />");
document.write("<param name=\"wmode\" value=\"transparent\" />");
document.write("<embed src=\""+swf+"\" quality=\"high\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" menu=\"false\" width=\""+hauteur+"\" height=\""+largeur+"\" wmode=\"transparent\"></embed>");
}

/*COMPTE UN CLIC SUR LE LIEN DU SITE WEB D'UN ADHERENT*/
function clic_site_adherent(id_adherent,type){
	var xhr = getXhr();
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200) var reponse = xhr.responseText;
	}
	xhr.open('GET','compteur_pub.php?compte_site=ok&type='+type+'&id_adherent='+id_adherent,true);
	xhr.send(null);
}

/*ENVOI LA LISTE DES ADHERENTS POUR NOUVELLE DEMANDE DE DEVIS*/
function send_propo() {
	var liste_cases = document.forms.form_autres_ad.getElementsByTagName('input');
	var cochee = 'non';
	
	for(var i=0;i<liste_cases.length;i++){
		if(liste_cases[i].type=="checkbox" && liste_cases[i].checked) cochee = 'oui';
	}
	
	if(cochee=='oui') document.forms.form_autres_ad.submit();
	else alert('Sélectionnez des adhérents pour leur envoyer votre demande de devis');
}
