
// Charger les bloc de la catégorie passé en paramètre
function charger_categorie( id_main_cat, style )
{
	// Enlever la classes au menu possedant la classe td_select (le menu rouge)
	$("td.td_select").removeClass();
	
	// Ajouter la classe au td cliqué (il devient rouge)
	$( "#td_menu_" + id_main_cat ).addClass("td_select");
	
	// Charger les sous-catégories de la main cat

	// Modifier le style du grand tableau
	$("#td_bg").removeClass();
	$("#td_bg").addClass(style);
	
	// Charger le contenu dans la div
	$('#div_sub_cats').html("&nbsp;&nbsp;&nbsp;<p><img src='img/icone/ajax-loader8.gif'/></p>");
	$.get("ajax_scripts/getListSubCats.php", { id: id_main_cat },
	  function(data){
		$('#div_sub_cats').html(data);
	  }
	, "text");			
}

// Vérification de la saisie des champs du formulaire contact
function verifFormContact()
{
	
	nom = document.getElementById("nom_ct").value;
	
	email = document.getElementById("email_ct").value;
	

	tel = document.getElementById("tel_ct").value;
	
	msge = document.getElementById("msg_ct").value;	

	message = "";		
	
	if(nom == "")
	{
		message += " - Votre nom\n";
	}
	
	if(email == "")
	{
		message += " - Votre email\n";
	}
	
	if(msge == "")
	{
		message += " - Votre message\n";
	}
	
	if(message != "")
	{
		alert("Veuillez indiquer :\n\n" + message);	
		return false;
	}	

	
	if(verifMail(email))
	{
		alert("Email invalide");
		return false;		
	}
	
	if(verifTel(tel))
	{
		alert("Téléphone invalide");
		return false;
	}	

}

// Initialiser le menu horizontal
$(document).ready(function()
{
	var options = {minWidth: 100, offsetTop : 5, hoverOpenDelay : 400, copyClassAttr: false, onClick: function(e, menuItem){
		//alert('you clicked item "' + $(this).text() + '"');
	}};
	$('#menuone').menu(options);
});


// Connexion des client avec ajax
function connect_client()
{
	login_c = document.getElementById("login_con").value;
	pass_c = document.getElementById("pass_con").value;
	
	if(login_c=="" || pass_c=="")
	{
		alert("Veuillez saisir le login et le mot de passe");
		return false;
	}
	else
	{
		$.post
		(
			"ajax_scripts/login.php",
			{ lg: login_c, ps: pass_c },
			function(data)
			{
				if (data==1 )
				{						
					document.location="client/a_annonce.php?aann=1";						
				}
				else
				{
					$("#msg").html("login ou mot de passe incorrect");
				}
			}
		);
	}
}

// Ajout d'un email aux abonnés
function add_news()
{
	email_news = $("#mail_news").val();
	if( email_news=="" || verifMail(email_news) )
	{
		//alert("Vérifier la saisie de l'email");
		$("#error_msg").html("Vérifier la saisie de l'email");		
		return false;
	}
	else
	{
		$.post
		(
			"ajax_scripts/ajouter_abonne.php",
			{ mail: email_news },
			function(data)
			{
				if (data == 1 )
				{						
					//alert("Vous êtes déja abonné à notre newsletter");
					$("#error_msg").html("Vous êtes déja abonné à notre newsletter");
					$("#mail_news").val("");
				}
				else
				if (data == 2 )
				{						
					//alert("Félicitations, vous êtes maintenant inscrit à notre newsletter");
					$("#error_msg").html("Félicitations, vous êtes maintenant inscrit à notre newsletter");
					$("#mail_news").val("");
				}
				/*
				else
				{
					$("#msg").html("login ou mot de passe incorrect");
				}
				*/
			}
		);
	}
}


// Ouvrir un popup en milieu de l'ecran
function popup(url, width, height) 
{
	var left   = (screen.width  - width)/2;
	var top    = (screen.height - height)/2;
	var params = 'width='+width+', height='+height;
	params += ', top='+top+', left='+left;
	params += ', directories=no';
	params += ', location=no';
	params += ', menubar=no';
	params += ', resizable=no';
	params += ', scrollbars=yes';
	params += ', status=no';
	params += ', toolbar=no';
	newwin=window.open(url,'windowname5', params);
	if (window.focus) {newwin.focus()}
	return false;
}

// Verifier la validité d'un email passé en paramètre
function verifMail(email)
{
	
	var arobase = email.indexOf("@");
	var point = email.lastIndexOf(".");
	if((arobase < 3)||(point + 2 > email.length)||(point < arobase+3))
		return true;
	return false;
}


// Ajouter une page au favorie
function addToFavorites(anchor)
{
	if (window.external)
	{
		window.external.AddFavorite(anchor.getAttribute('href'), anchor.getAttribute('title'));
	}
} 

// Recherche global
function go_rech()
{
	cat = $("#rech_cat").val();
	reg = $("#rech_reg").val();
	
	url = "annonce_cat.php?rech_cat="+cat+"&rech_reg="+reg;
	//alert(url);
	document.location = url;
}

// Verifier qu'une variable passée en paramètre est un entier mais peut contenir des espaces
function verifTel(entier)
{

	for(i=0;i<entier.length;i++)
		 if((entier.charAt(i) < "0"|| entier.charAt(i) > "9") && (entier.charAt(i) != " "))
		 return 1;
	return 0;	
}