// JavaScript Document
jQuery(document).ready(function(){
	jQuery.ajax({
   		type: "POST",
   		url: "/cgi/ajax/diag_home.cgi",
   		data: "diag=init",
   		async: false
   	});
	jQuery('#obj_diet_input').click(function() {
		if(this.checked == true){
			jQuery('#obj_diet').css('display','');
		}else{
			jQuery('#obj_diet').css('display','none');
		}
	});
	jQuery('#obj_sport_input').click(function() {
		if(this.checked == true){
			jQuery('#obj_sport').css('display','');
		}else{
			jQuery('#obj_sport').css('display','none');
		}
	});
	jQuery('#continuer_home_diag').click(function() {
		jQuery.blockUI();
		jQuery('#flash_geolocalisation').hide();
		if (document.getElementById('obj_diet_input').checked == true || 
			document.getElementById('obj_sport_input').checked == true || 
			document.getElementById('obj_esthe_input').checked == true)
		{
			var virgule, lien, img,
			start = '', 
			stop = '',
			html = '',
			id = '',
			project_libelle = '',
			project_title_1 = 'Un projet alliant ',
			project_title_2 = 'Un projet ',
			project = '',
			link_to_diag = '',
			inter_diag = '',
			nb_diag = 0,
			url = '';
			
			if (document.getElementById('obj_diet_input').checked == true){
				html += ' <img style="position:relative; top:1px;"  src="/images/puce_orange.gif" />&nbsp; Vous souhaitez <strong>mincir et rééquilibrer votre alimentation</strong> ';
				id = document.getElementById('obj_diet_select').value;
				html += vouvoyerClient(document.getElementById('obj_diet_'+id).text+'.');
				html += '<br />';
				nb_diag += 1;
				project += 'Diététique';
				project_libelle += 'Diététique';
				inter_diag += 'votre diététicien(ne)';
				url += "diag=1&diag_diet=1&id_obj_diet="+id;
   				jQuery('#fiche_intervenant_diet').fadeIn('slow');
   				//$('#fiche_intervenant_diet').css('display','');
			}else{
				jQuery('#fiche_intervenant_diet').css('display','none');
			}
			if (document.getElementById('obj_sport_input').checked == true){
				html += ' <img style="position:relative; top:1px;" src="/images/puce_orange.gif" />&nbsp;  Vous souhaitez <strong>tonifier ou muscler votre silhouette</strong> ';
				id = document.getElementById('obj_sport_select').value;
				html += vouvoyerClient(document.getElementById('obj_sport_'+id).text+'.');
				html += '<br/>';
				project += ', Sport';
				nb_diag += 1;
				inter_diag += ', votre coach sportif';
				if (project_libelle != ''){
					project_libelle += ' - ';
				}
				project_libelle += 'Sport';
				if (url != ''){
					url += '&';
				}else{
					url += "diag=3&";
				}
				url += "diag_sport=1&id_obj_sport="+id;
   				jQuery('#fiche_intervenant_sport').fadeIn('slow');
			}else{
				jQuery('#fiche_intervenant_sport').css('display','none');
			}
			if (document.getElementById('obj_esthe_input').checked == true){
				html += ' <img style="position:relative; top:1px;" src="/images/puce_orange.gif" />&nbsp;  Vous souhaitez ';
				html += '<strong>lutter contre la cellulite et remodeler votre silhouette</strong>.';
				html += '<br/>';
				project += ', Esthétique';
				if (project_libelle != ''){
					project_libelle += ' - ';
				}
				project_libelle += 'Esthétique';
				nb_diag += 1;
				inter_diag += ', votre esthéticienne';
				if (url != ''){
					url += '&';
				}else{
					url += "diag=4&";
				}
				url += "diag_esthe=1";
   				jQuery('#fiche_intervenant_esthe').fadeIn('slow');
			}else{
				jQuery('#fiche_intervenant_esthe').css('display','none');
			}
			link_to_diag = jQuery.ajax({
   				type: "POST",
   				url: "/cgi/ajax/diag_home.cgi",
   				data: url,
   				async: false
   			});			
			
			virgule = project.lastIndexOf(","); // projet
			
			if(virgule > 5){
				start = project.substr(0,virgule);
				stop = project.substr(virgule+1,project.length);
				project = start + " et " + stop;
			}
			virgule = project.substr(0,1);
			if (virgule == ','){
				project = project.substr(2,project.length);
			}
			// intervenants accompagnateurs
			virgule = inter_diag.lastIndexOf(",");
			if (virgule > 0){
				start = inter_diag.substr(0,virgule);
				stop = inter_diag.substr(virgule+1,inter_diag.length);
				inter_diag = start + " et " + stop;
			}
			virgule = inter_diag.substr(0,1);
			if (virgule == ","){
				inter_diag = inter_diag.substr(2,inter_diag.length);		
			}
			html += "<br/>Pour répondre à cet objectif, je vous conseille :<br/><br/><div class=\"colorSport\" style=\"text-align:center; font-weight:bold; font-size:14px; \">";
			//html += "je vous conseille le projet suivant pendant lequel vous serez accompagné(e) et coaché(e) en ";
			//html += "permanence par "+inter_diag+".<br/><br/><div class=\"colorSport\" style=\"text-align:center; font-weight:bold; font-size:14px; \">";
			if (nb_diag == 1){
				html += project_title_2;
			}else{
				html += project_title_1;
			}
			html += project+"</div>";
			
			lien = '<a href="'+link_to_diag.responseText+'" title="Commencer mon diagnostic">';
      		lien +='<img style="left:100px; position:relative; bottom:11px;  visibility:visible; color:#FFFFFF;" src="/images/bt_commencer-diag_home.gif" id="valider_home_diag" name="valider_home_diag" />';
      		lien += '</a>';
      		img = "url('/images/home_fond_diag2.jpg')";
      		jQuery('.homeDiag').css({'background-image':img});
			jQuery('#homeDiagContenu').slideUp('slow');
			jQuery('#etape2_p').empty();
			jQuery('#etape2_p').append(html);
			jQuery('#etape2_l').append(lien);
			jQuery('#etape2').css('display','');
			jQuery('#bloc_droit_home').css('display','');
			jQuery('#type_projet').append('<strong>Votre projet '+project_libelle+'</strong>');
		}else{
			if ((jQuery('#autre_obj_input').length > 0) && (jQuery('#autre_obj_input').val() != '')){
				jQuery.get('/cgi/ajax/home_autre_objectif.cgi', { autre_objectif : jQuery('#autre_obj_input').val() },
					function (data) {
						if (data == 0) {
							alert('Veuillez sélectionner un objectif');
						}
					});

			}else{
				alert('Veuillez sélectionner un objectif');
			}
		}
		jQuery.unblockUI();
		return false;
	});
	jQuery('#retourHomeDiag').click(function () {
		jQuery('#etape2').slideDown('slow');
		jQuery('#etape2').css('display','none');
		var img = "url('/images/home_fond_diag1.jpg')";
      	jQuery('.homeDiag').css({'background-image':img});
		jQuery('#type_projet').empty();
		jQuery('#etape2_l').empty();
		/*$('#bloc_droit_home').css('display','none');*/
		/*$('#fiche_intervenant_diet').css('display','none');*/
		/*$('#fiche_intervenant_sport').css('display','none');*/
		/*$('#fiche_intervenant_esthe').css('display','none');*/
		jQuery('#valider_home_diag').css('display','none');
		jQuery('#homeDiagContenu').slideDown('slow');
		jQuery.ajax({
   			type: "POST",
   			url: "/cgi/ajax/diag_home.cgi",
   			data: "diag=init",
   			async: false
   		});
	});
	if (document.getElementById('obj_diet_input').checked == true){
		jQuery('#obj_diet').css('display','');
		cacheAutreObjectifInput();
	}else{
		jQuery('#obj_diet').css('display','none');
	}
	if (document.getElementById('obj_sport_input').checked == true){
		jQuery('#obj_sport').css('display','');
		cacheAutreObjectifInput();
	}else{
		jQuery('#obj_sport').css('display','none');
	}
	if (document.getElementById('obj_esthe_input').checked == true){
		cacheAutreObjectifInput();
	}
	
	jQuery('#obj_diet_input').click(function () {
		if (this.checked == true){
			cacheAutreObjectifInput();
		}else{
			if (document.getElementById('obj_sport_input').checked != true && document.getElementById('obj_esthe_input').checked != true)
			{
				afficheAutreObjectifInput();
			}
		}
	});
	
	jQuery('#obj_sport_input').click(function () {
		if (this.checked == true){
			cacheAutreObjectifInput();
		}else{
			if (document.getElementById('obj_diet_input').checked != true && document.getElementById('obj_esthe_input').checked != true)
			{
				afficheAutreObjectifInput();
			}
		}
	});
	
	jQuery('#obj_esthe_input').click(function () {
		if (this.checked == true){
			cacheAutreObjectifInput();
		}else{
			if (document.getElementById('obj_sport_input').checked != true && document.getElementById('obj_diet_input').checked != true)
			{
				afficheAutreObjectifInput();
			}
		}
	});
});


function cacheAutreObjectifInput() {
	jQuery('#autre_objectif').css('display','none');
}

function afficheAutreObjectifInput() {
	jQuery('#autre_objectif').css('display','');		
}

var chatButtonDraw = 0;

function chatButtonSwap(){
	var texte = document.getElementById('autre_obj_input').value;
	if (texte.length != 0){
		if (chatButtonDraw == 0){
			chatButtonDraw = 1;
			jQuery('#continuer_home_diag').css('display','none');
			jQuery('#chat_home_diag').css('display','');
		}
	}else{
		if (chatButtonDraw == 1){
			jQuery('#continuer_home_diag').css('display','');
			jQuery('#chat_home_diag').css('display','none');
			chatButtonDraw = 0;
		}
	}
}

function vouvoyerClient(phrase){
	phrase = phrase.toLowerCase();
	phrase = phrase.replace(/ ma /g, " votre ");
	phrase = phrase.replace(/ mes /g, " vos ");
	phrase = phrase.replace(/ mon /g, " votre ");
	phrase = phrase.replace(/m'/g, "vous ");
	phrase = phrase.replace(/me /g, "vous ");
	return phrase;
}


function show_and_hide_infos_consultante_droite() {

	if ( typeof this.fleche == 'undefined' ) this.fleche = 'droite';
	
	if ( this.fleche == 'droite' ) {
		this.fleche = 'gauche';
	} else {
		this.fleche = 'droite';
	}

	jQuery('#infos_consultante_droite').toggle();
	jQuery('#puce_depli').attr('src', '/images/puce_home_depli-info-consult-' + this.fleche + '.gif');
}


function validate_code_postal() {
	var cp		= jQuery('#code_postal'),
	pays		= jQuery('#pays');
	
	if((cp.val() == '') && (pays.val() == 1)) {
		cp.css("border","solid #ff0000 1px");
		jQuery('#erreur_code_postal').html("Veuillez entrer un code postal");

	} else {
	
		if( (pays.val() == 1) && (cp.val().length == 2) ) {
			// si on n'a que les 2 premiers chiffres du CP, on complète avec des zéros
			cp.val(cp.val() + '000');
		}
		
		jQuery('#submit_pays_cp').attr("disabled","disabled");
		
		jQuery.get('/cgi/ajax/home_verif_code_postal.cgi', { code_postal : cp.val(), pays : pays.val() },
			function (data) {
				if (data == 0) {
					// Code postal incorrect
					jQuery('#code_postal').css("border","solid #ff0000 1px");
					jQuery('#erreur_code_postal').html("Ce code postal n'existe pas");
				} else {
					// Code postal correct
					// => fenêtre de chargement
					overShow('rechargement_process', '<div style="color: #AAA; text-align: center; font-size: 10px; line-height: 30px; font-family:Arial, Verdanan, Helvetica, sans-serif;">Veuillez patienter... <img src="/images/busy.gif" alt="chargement" /></div>', 'Localisation en cours');
					jQuery('#dialog').jqmHide();
					
					jQuery.get('/cgi/ajax/home_geolocalisation.cgi', { code_postal : cp.val(), pays : pays.val() },
						function (data) {
							window.location.reload();
						});
					/*
					//=> actualisation des blocs "bloc_consultante", "actu1", "bloc_flash_geolocalisation"
					jQuery.getJSON('/cgi/ajax/home_rechargement_blocs.cgi', { code_postal : cp.val(), pays : pays.val() },
						function (data_json) {
							jQuery('#bloc_consultante').hide();
							jQuery('#bloc_consultante').html(data_json.bloc_consultante);
							jQuery('#bloc_consultante').fadeIn(2000);
							jQuery('#actu1').html(data_json.actu1);
							jQuery('#bloc_flash_geolocalisation').html(data_json.flash_geolocalisation);
							jQuery('#bloc_flash_slideshop').html(data_json.flash_slideshop);
							jQuery('#rechargement_process').jqmHide();
						});
					*/
				}
				jQuery('#submit_pays_cp').removeAttr("disabled");
		});
	}
	return false;
}

