function categorizer(baseSelId, parentSelId, categSelId, resId, idInput) {
	var currentCateg = new Array();

	var base = new Array();
	var paren = new Array();
	var categ = new Array();

	var _baseSelId = (baseSelId == undefined ? "base" : baseSelId);
	var _parentSelId = (parentSelId == undefined ? "parent" : parentSelId);
	var _categSelId = (categSelId == undefined ? "categorie" : categSelId);
	var _resId = (resId == undefined ? "res" : resId);
	var _idInput = (idInput == undefined ? "idCateg" : idInput);

	var _defaultB = undefined;
	var _defaultP = undefined;
	var _defaultC = undefined;

	this.addCategToChoice = function(id, b, p, c) {
		_id = (id == undefined ? categ[$("#" + _categSelId)[0].value][0] : id);
		_b = (b == undefined ? $("#" + _baseSelId)[0].options[$("#" + _baseSelId)[0].selectedIndex].innerHTML : b);
		_p = (p == undefined ? $("#" + _parentSelId)[0].options[$("#" + _parentSelId)[0].selectedIndex].innerHTML : p);
		_c = (c == undefined ? $("#" + _categSelId)[0].options[$("#" + _categSelId)[0].selectedIndex].innerHTML : c);
		for (i = 0; i < currentCateg.length; ++i) {
			if (currentCateg[i][0] == _id)
				return;
		}
		if ($("#" + _baseSelId)[0].selectedIndex)
			_bId = base[$("#" + _baseSelId)[0].selectedIndex];
		if ($("#" + _parentSelId)[0].selectedIndex)
			_pId = paren[$("#" + _parentSelId)[0].selectedIndex];
		currentCateg[currentCateg.length] = new Array(_id,
								  _b,
								  _p,
								  _c,
								  _bId,
								  _pId);
		this.updateCategChoice();
	}

	this.getSelectedBase = function() {
		if ($("#" + _baseSelId)[0].selectedIndex)
			return base[$("#" + _baseSelId)[0].selectedIndex - 1][0];
		return "";
	}

	this.getSelectedParent = function() {
		if ($("#" + _parentSelId)[0].selectedIndex)
			return paren[$("#" + _parentSelId)[0].selectedIndex - 1][0];
		return "";
	}

	this.getSelectedChap = function() {
		if ($("#" + _categSelId)[0].selectedIndex)
			return categ[$("#" + _categSelId)[0].selectedIndex - 1][0];
		return "";
	}

	this.setDefaults = function(b, p, c) {
		_defaultB = b;
		_defaultP = p;
		_defaultC = c;
	}

	this.removeChoice = function(i) {
		currentCateg.splice(i, 1);
		this.updateCategChoice();
	}

	this.loadBase = function() {
		$.getJSON("/categorie/GetCategoriesFromParent/0", this.execBaseRequest);
	}

	this.loadParent = function(id) {
		var _id = (id == undefined ? base[$("#" + _baseSelId)[0].value][0] : id);
		$.getJSON("/categorie/GetCategoriesFromParent/" + _id, this.execParentRequest);
	}

	this.loadCateg = function(id) {
		var _id = (id == undefined ? paren[$("#" + _parentSelId)[0].value][0] : id);
		$.getJSON("/categorie/GetCategoriesFromParent/" + _id, this.execCategRequest);
	}

	this.execBaseRequest = function(json) {
		base = [];
		paren = [];
		categ = [];
		$.each(json, function(i, item){
			base[base.length] = new Array(item.id,
						      item.baseName,
						      item.parentName,
						      item.name);
		 });
		 updateTab(_baseSelId, base);
		 updateTab(_parentSelId, paren);
		 updateTab(_categSelId, categ);
	}

	this.execParentRequest = function(json) {
		paren = [];
		categ = [];
		$.each(json, function(i, item){
			paren[paren.length] = new Array(item.id,
						      	item.baseName,
						      	item.parentName,
						      	item.name);
		 });
		 updateTab(_parentSelId, paren);
		 updateTab(_categSelId, categ);
	}

	this.execCategRequest = function(json) {
		categ = [];
		$.each(json, function(i, item){
			categ[categ.length] = new Array(item.id,
							item.baseName,
							item.parentName,
							item.name);
		 });
		 updateTab(_categSelId, categ);
	}

	var updateTab = function(id, tab) {
		html = "<option>selectionnez</option>";
		sav = -1;
		$.each(tab, function(i, item) {
			html += "<option value='" + i + "' >" + item[3] + "</option>";
			if (item[0] == _defaultB || item[0] == _defaultP || item[0] == _defaultC)
				sav = i;
		});
		$("#" + id).html(html);
		$("#" + id)[0].selectedIndex = sav + 1;
	}

	this.updateCategChoice = function(objName) {
		objName = (objName == undefined ? "catObj" : objName);
		html = "";
		$.each(currentCateg, function (i, item) {
			html += item[1] + "<img src='/img/fleche_ariane.gif' alt=''>" + item[2] + "<img src='/img/fleche_ariane.gif' alt=''>" + item[3] + " <a class='suppr' href='javascript:" + objName + ".removeChoice(" + i + ")'>Supprimer</a><br />";
		});
		$("#" + _resId).html(html);
	}

	this.setCategIdList = function() {
		if (currentCateg.length < 1) {
			alert("Vous devez choisir au moins une categorie");
			return (false);
		}
			
		value = "";
		$.each(currentCateg, function (i, item) {
			value += (value == "" ? "" : "|") + item[0];
		});
		$("#" + _idInput)[0].value = value;
		//alert($("#" + _idInput)[0].value);
		return (true);
	}

	this.getBaseIdList = function() {
		value = "";
		$.each(currentCateg, function (i, item) {
			value += (value == "" ? "" : "|") + item[0];
		});
		$("#" + _idInput)[0].value = value;
		//alert($("#" + _idInput)[0].value);
		return (value);
	}

	this.getBaseIdList = function() {
		value = "";
		$.each(currentCateg, function (i, item) {
			value += (value == "" ? "" : "|") + item[0];
		});
		$("#" + _idInput)[0].value = value;
		//alert($("#" + _idInput)[0].value);
		return (value);
	 }

	this.getBaseIdList = function() {
		value = "";
		$.each(currentCateg, function (i, item) {
			value += (value == "" ? "" : "|") + item[0];
		});
		$("#" + _idInput)[0].value = value;
		//alert($("#" + _idInput)[0].value);
		return (value);
	}
}
