Array.prototype.in_array = function(valeur)
{
    for(i=0;i<this.length;i++)
        if(this[i]==valeur)
            return true;
    return false;
}
String.prototype.max_affiche = function(maximum)
{
	if(this.length>maximum)
		return this.substr(0,maximum)+"...";
		return this;
}
String.prototype.trim = function()
{
	return $.trim(this);
}
function retour_ajax(element,data,fonction)
{
	element.html($(data).find("codehtml").text());
	//element.find("input").after("<img class='loader_retour_ajax' src='"+dossier_admin+"/images/ajax-loader.gif' />");
	element.show("blind",function(){
			if($(data).find("js").text()!="init") // A enlever 
				eval($(data).find("js").text());
			if(fonction!=null)
				fonction();
			//element.find(".loader_retour_ajax").remove();
		});
}

function formulaire_init(form)
{
	form.append("<br /><span id='msg_erreur_obligatoire'>"+message_erreur_obligatoire+"</span>");
	form.find("#msg_erreur_obligatoire").hide();
	form.find(".champ_obligatoire").focus(function(){
			$(this).removeClass("erreur");
			form.find("#msg_erreur_obligatoire:visible").hide("slide");
		});
	
	form.find(".champ_compare:last").after("<br /><span id='msg_erreur_compare'>"+message_erreur_compare+"</span>");
	form.find("#msg_erreur_compare").hide();
	form.find(".champ_compare").focus(function(){form.find("#msg_erreur_compare:visible").hide("blind");});
	
	$.each(form.find(".champ_unique"),function(n,element){
			if($(element).attr("id")=="")
				alert("ERREUR FACIWEB : le champ unique '"+$(element).attr("name")+"' doit comporter un appel ajax dans l'id !");
			id_message="msg_erreur_unique_"+$(element).attr("name");
			$(element).after("<br /><span class='message_erreur_unique' id='"+id_message+"'>"+message_erreur_unique+"</span>");
			$(element).parent().find("#"+id_message).hide();
			$(element).focus(function(){form.find("#"+id_message+":visible").hide("blind");});
		});
}
function formulaire_submit(form)
{
	btn_submit=form.find(":submit").replaceWith("<img id='image_loader' src='"+dossier_admin+"/images/ajax-loader.gif' />");
	retour=true;
	$.each(form.find(".champ_obligatoire"),function(n,element){
			if($(element).val()=="")
			{
				form.find("#msg_erreur_obligatoire:hidden").show("slide");
				$(element).addClass("erreur");
				retour=false;
			}
		});
	valeur="";
	$.each(form.find(".champ_compare"),function(n,element){
			if(n==0)
				valeur=$(element).val();
			else
				if($(element).val()!=valeur)
				{
					form.find("#msg_erreur_compare").show("blind");
					retour=false;
				}
		});
	$.each(form.find(".champ_unique"),function(n,element){
			if($($.ajax({url: ''+dossier_logiciel+'/ajax.php',type: 'post', async: false,data:{ajax: $(element).attr("id"),valeur : $(element).val()}}).responseXML).find("data").text()=="KO")
			{
				form.find("#msg_erreur_unique_"+$(element).attr("name")).show("blind");
				retour=false;
			}
		});
	$.each(form.find(".geolocalisation"),function(n,element){
			if(form.find("[name="+$(element).attr("name")+"Lat]").length==0)
			{
				form.find("#msg_erreur_obligatoire:hidden").show("slide");
				$(element).addClass("erreur");
				retour=false;
			}
		});
	form.find("#image_loader").replaceWith(btn_submit);
	return retour;
}
