// Questions libres
var scriptsQst = {
	aQst: [],
	lbSupprimer: "Supprimer",
	
	init: function(){
		$("#ajouterQst").click(
			function(){
				scriptsQst.ajouter('');
				return false;
			}
		);
	},
	ajouter: function(qst){
		// Suppression de la limite à 5 Questions libres if(this.aQst.length == 5) return;
		
		var id = this.aQst.length;
		var div = document.createElement("div");
		
		var inputQst = document.createElement("input");
		inputQst.type = "text";
		inputQst.value = qst.stripslashes();
		inputQst.name = "qst-"+id;
		
		var span = document.createElement("span");
		span.appendChild(document.createTextNode("-"));
		
		var supprimer = document.createElement("a");
		supprimer.href = "#";
		supprimer.id = "sup-"+id;
		supprimer.appendChild(document.createTextNode(this.lbSupprimer));
		$(supprimer).click(
			function(){
				var id = $(this).attr("id").split("-")[1];
				scriptsQst.supprimer(id);
				return false;
			}
		);
		
		div.appendChild(inputQst);
		div.appendChild(span);
		div.appendChild(supprimer);
		
		$("#cntQuestions").append(div);
		this.aQst.push(div);
		return false;
	},
	supprimer: function(id){
		var aNew = [];
		var elem = null;
		
		for(var i = 0; i < this.aQst.length; i++){
			if(i != id) aNew.push(this.aQst[i]);
			else elem = this.aQst[i];
		}
		this.aQst = aNew;

		$(elem).remove();
		// On decremente ceux du dessous
		$("#cntQuestions a").each(
			function(obj){
				var curId = $(this).attr("id").split("-")[1];
				if(curId > id)  $(this).attr("id", "cntQst-"+(curId - 1));
			}
		);
		return false;
	}
};