$.ajaxSetup({
		    'beforeSend' : function(xhr) {
					if(xhr.overrideMimeType){ 
			         xhr.overrideMimeType('text/html; charset=UTF-8');
						}
				}
});

/** affiche le formulaire pour écrire une JNJ */
function affFormJnj ()
{
	base_url = BASE_URL;
	var idDiv="formJnj";
		if($("#"+idDiv).html() == "" ){
			$.get(base_url+"index.php/jnjajax/formjnj",function(texthtml){
						$("#"+idDiv).hide();
						$("#"+idDiv).html(texthtml).fadeIn("slow");
						$("#"+idDiv).slideDown("slow");
					});
		}
		else{
			$("#"+idDiv).html("");
		}
}

function aff_jnj_alea()
{
	base_url = BASE_URL;
	var idDiv="jnjalea";
			$.get(base_url+"index.php/jnjajax/jnjalea",function(texthtml){
						$("#"+idDiv).html(texthtml).fadeIn("slow");
						$("#"+idDiv).slideDown("slow");
					});
}
// sexe : 1 = fille
function aff_pseudo_alea(sexe)
{
	base_url = BASE_URL;
	var lblDiv="pseudoc";
	var idDiv="pseudoid";
	var img="imgform";
	var txtarea="textareaJnj";
			$.get(base_url+"index.php/jnjajax/get_pseudo/"+sexe,function(texthtml){
						tab = texthtml.split("|");							
						//premier pseudo
						$("#"+lblDiv).html(tab[0]).fadeIn("slow");
						$("#"+lblDiv).slideDown("slow");
						//pseudo sous l'image
						$("#"+lblDiv+"2").html(tab[0]).fadeIn("slow");
						$("#"+lblDiv+"2").slideDown("slow");
					//	$("#"+idDiv).html(tab[1]);
						document.formnew.pse_id.value = tab[1];
						if( sexe == 1) {
							$("#"+txtarea).addClass("rose").removeClass("bleu");
							 $('#'+img).attr('src', BASE_URL+"images/girl.png");
						} else {
							$("#"+txtarea).addClass("bleu").removeClass("rose");
							 $('#'+img).attr('src', BASE_URL+"images/guy.png");
						}
					});
}

function subNbChar(){
		var max = 215;
		var nb = parseInt($('#jnjFormNbChar').html());
		var txt = $('#textareaJnj').val();
		if (txt.length > max) {
			$('#textareaJnj').val(txt.substring(0,max));
		} else {
			$('#jnjFormNbChar').html((max-txt.length));
		}
}
function valid_jnj(frm){
	var valid = true;
	//on enleve le "je n'ai jamais"
	//et on verifie que ce n'est pas vide.
	var ch= String(frm.jnj.value);
	var sousch = ch.substr(15);
	if( sousch == ""){
		valid = false;
	}
	return valid;
}
function envoie_jnj(frm) {
	if(valid_jnj(frm)){
		var idDiv="formJnj";
		//alert(frm.pse_id.value);
		$.post(BASE_URL+"index.php/newjnj",{"jnj" : String(frm.jnj.value), "pse_id" : frm.pse_id.value },
				function(texthtml){
						//alert(html);
						$("#"+idDiv).hide();
						$("#"+idDiv).html(texthtml).fadeIn("slow");
						$("#"+idDiv).slideDown("slow");
					}
		);
	}
	return false;
}

/** ajout un  Moi SI */
function ajoutMSI(id)
{
	base = BASE_URL;
	$.post(base+"index.php/jnjajax/ajoutmsi",{"JNJ_id": id},
				function(texthtml){
						var nbMNP=$('#nbMNP'+id).html();
						var nbMSI=$('#nbMSI'+id).html();
						nbMSI = parseInt(nbMSI) + 1;
						$('#nb_mnp'+id).html("");
						$('#bt_MoiSi'+id).html("");
						$('#nb_mnp'+id).html("Moi Non plus ! ("+nbMNP+")").fadeIn("slow");
						$('#bt_MoiSi'+id).html("Moi Si ! ("+nbMSI+" dont Moi !)").fadeIn("slow");
						$('#bt_MoiSi'+id).css("width","100px");
						nb_Msi_user = parseInt($('#menuNbMoiSi').html());
						$('#menuNbMoiSi').html(nb_Msi_user+1);
			});	
}

/** ajout un  Moi SI */
function ajoutMNP(id)
{
	base = BASE_URL;
	$.post(base+"index.php/jnjajax/ajoutmnp",{"JNJ_id": id},
				function(texthtml){	
						var nbMNP=$('#nbMNP'+id).html();
						var nbMSI=$('#nbMSI'+id).html();
						nbMNP = parseInt(nbMNP) + 1;
						$('#bt_MoiSi'+id).html("");
						$('#nb_mnp'+id).html("");
						$('#bt_MoiSi'+id).html("Moi Si ! ("+nbMSI+")").fadeIn("slow");
						$('#nb_mnp'+id).html("Moi Non plus ! ("+nbMNP+" dont Moi!)").fadeIn("slow");
						$('#nb_mnp'+id).css("width","100px");
						nb_Mnp_user = parseInt($('#menuNbJnjTotal').html());
						$('#menuNbJnjTotal').html(nb_Mnp_user+1);
			});
}


/** affiche le formulaire pour ajouter une commentaire 
 *	et en dessous les commentaires */
function affFormComm (nomdiv, id, cat, lim)
{
	base = BASE_URL;
	//créer un timestamp pour eviter les robots
	//TODO : $.get("controleur/creerStamp.php");
	
	var idDiv=nomdiv+id;
		$.post(base+"index.php/jnjajax/formComm/",{"jnj": id,"cat":cat,"lim":lim},
				function(texthtml){																		
				$('#'+idDiv).hide();
				$('#'+idDiv).html(texthtml).fadeIn("slow");
				$('#'+idDiv).slideDown("slow");
				});

//	affComms(base,id,0);
}

/** cache le formulaire */
function hideFormRep (id)
{
	$('#formComm'+id).html("").fadeOut("slow");
}

/** ajoute le commentaire */
function ajoutComm (id, form)
{
	base = BASE_URL
	var modele=/^[a-zA-Z0-9éèêïîôö]{3,10}$/;  
	var ind = form.pseudo.value.search(modele);                                 
	if(form.F.checked){
		sexe = 1;
	} else {
		sexe = 0;
	}
  if(ind == -1){
		$('#infoReserv').html("pseudo Incorrect: 3 à 8 charactères alphanumeriques");
	}else{
		$.post(base+"index.php/jnjajax/ajoutComm",{"JNJ_id": id,"REP_pseudo":form.pseudo.value,"sexe":sexe,"txt":form.txt.value},
				function(texthtml){
					if(texthtml != "1"){
						$('#formComm'+id).html("Erreur dans votre formulaire, veuillez recommencer!"+texthtml+"<br /><br />").fadeIn("Slow");		
					} else {
						$('#formComm'+id).html("Le commentaire est bien envoyé, il apparaitra après sa validation par les administrateurs <br /><br />").fadeIn("Slow");		
					}
				});
	}
}

function verifLogin(login){
	inscr=0;
 if(verifLogin.arguments.length == 2){
     inscr = verifLogin.arguments[1];
	}
	if(login.length > 10){
		$("#infoReserv").html("Pseudo trop long : (entre 3 et 10 charactères)");
		return false;
	}else{
			//$("#infoReserv").html("Pseudo ok");
	}

return true;
}

/** affiche les commentaire du jnj(id) */
function affComms(id,lim)
{
	base = BASE_URL;
	var nomdiv = 'affcomm';
			$.post(base+"index.php/jnjajax/getComms",{"id": id,"lim": lim},
				function(texthtml){
					$('#'+nomdiv+id).hide();
					$('#'+nomdiv+id).html(texthtml).fadeIn("slow");
					$('#'+nomdiv+id).slideDown("slow");
			});		
}

/** cache le formulaire */
function hideComms(id)
{
	$('#affcomm'+id).html("").fadeOut("slow");
	hideFormRep(id);
}

/** met la personne majeur */
function EntrerOK()
{
	alert("kikou");
	base = BASE_URL;
	$.post(base+"index.php/jnjajax/entrer_majeur/",{},
			function(txthtml){alert(txthtml);});
	window.location.reload();
}

