Selection = {
		
	SELECTION_MENU_DIV_ID	: "selection-menu",
	CLOSE_SELECTION_DIV_ID	: "closeSelection",
	PUB_DIV_ID				: "pub",
	BRANDS_DIV_ID			: "brands",
	EDITO_DIV_ID			: "editorial",
	CART_JET_LASER_DIV_ID	: "cart-jet-laser",
	CARTRIDGE_DIV_ID		: "cartridge",
	PRINTER_DIV_ID			: "printer",
	BRAND_DIV_ID			: "brandSelection",
	TYPE_DIV_ID				: "typeSelection",
	SERIES_DIV_ID			: "seriesSelection",
	MODEL_DIV_ID			: "modelSelection",
	MODEL_PER_COLUMN		: 30,
	
	brandId 	: null,
	brandName 	: null,
	typeId		: null,
	typeName	: null,
	seriesId	: null,
	seriesName	: null,
	modelId		: null,
	modelName	: null,
	
	readPrinter : function(){
		if(typeof(printer)=='undefined')
			return;
		if(typeof(printer.brandId)=='undefined'||
				typeof(printer.typeId)=='undefined'||
				typeof(printer.seriesId)=='undefined'||
				typeof(printer.modelId)=='undefined'||
				typeof(printer.brandName)=='undefined'||
				typeof(printer.typeName)=='undefined'||
				typeof(printer.seriesName)=='undefined'||
				typeof(printer.modelName)=='undefined')
				return;
		var div = document.getElementById(this.SELECTION_MENU_DIV_ID);
		if(div==null)
			return;
		this.brandId = printer.brandId;
		this.brandName = printer.brandName;
		this.typeId = printer.typeId;
		this.typeName = printer.typeName;
		this.seriesId = printer.seriesId;
		this.seriesName = printer.seriesName;
		this.modelId = printer.modelId;
		this.modelName = printer.modelName;
		this.updateBrand();
		this.updateType();
		if(this.seriesId!=null && this.seriesId!='' && this.seriesName!=null && this.seriesName!='')
			this.updateSeries();
		if(this.modelId!=null && this.modelId!='' && this.modelName!=null && this.modelName!='')
			this.updateModel();
		div.style.display="block";
	},
	
	start : function(){
		if(isInProgress())
			return;
		var div = document.getElementById(this.SELECTION_MENU_DIV_ID);
		if(div==null)
			return;
		div.style.display="block";
		this.loadBrand();
	},
	
	loadBrand : function(){
		if(isInProgress())
			return;
		var request = new ajax();
		request.parameters = "ajax=1&method=readAll&path=query/brand&parameter=marque";
		request.onload = Selection.brandLoaded;
		request.send("post", ROOT+"query/brand", request.parameters);
	},
	
	loadType : function(brandId, brandName){
		if(isInProgress())
			return;
		if(brandId==null || brandName==null)
			return;
		this.brandId = brandId;
		this.brandName = brandName;
		var request = new ajax();
		request.parameters = "ajax=1&method=readAll&path=query/type&brand="+this.brandId+"&parameter=type";
		request.onload = Selection.typeLoaded;
		request.send("post", ROOT+"query/type", request.parameters);
	},
	
	loadSeries : function(typeId, typeName){
		if(isInProgress())
			return;
		if(this.brandId==null || typeId==null || typeName==null)
			return;
		this.typeId = typeId;
		this.typeName = typeName;
		var request = new ajax();
		request.parameters = "ajax=1&method=readAll&path=query/series&brand="+this.brandId+"&type="+this.typeId+"&parameter=serie";
		request.onload = Selection.seriesLoaded;
		request.send("post", ROOT+"query/series", request.parameters);
	},
	
	loadModel : function(seriesId, seriesName){
		if(isInProgress())
			return;
		if(this.brandId==null || this.typeId==null || seriesId==null || seriesName==null)
			return;
		this.seriesId = seriesId;
		this.seriesName = seriesName;
		var request = new ajax();
		request.parameters = "ajax=1&method=readAll&path=query/model&brand="+this.brandId+"&type="+this.typeId+"&series="+this.seriesId+"&feature=modele";
		request.onload = Selection.modelLoaded;
		request.send("post", ROOT+"query/model", request.parameters);
	},
	
	loadCartridgeByType : function(){
		if(isInProgress())
			return;
		if(this.brandId==null || this.typeId==null)
			return;
		var request = new ajax();
		request.parameters = "ajax=1&method=readAll&path=query/cartridgeByType&brand="+this.brandId+"&type="+this.typeId;
		request.onload = Selection.cartridgeByTypeLoaded;
		request.send("post", ROOT+"query/cartridgeByType", request.parameters);
	},
	
	loadCartridgeBySeries : function(){
		if(isInProgress())
			return;
		if(this.brandId==null || this.typeId==null || this.seriesId==null)
			return;
		var request = new ajax();
		request.parameters = "ajax=1&method=readAll&path=query/cartridgeBySeries&brand="+this.brandId+"&type="+this.typeId+"&series="+this.seriesId;
		request.onload = Selection.cartridgeBySeriesLoaded;
		request.send("post", ROOT+"query/cartridgeBySeries", request.parameters);
	},
	
	brandLoaded : function(){
		var div = document.getElementById(Selection.BRAND_DIV_ID);
		if(div==null)
			return;
		Selection.hideProduct();
		
		var response = eval(this.request.responseText);
		var html = '<div class="selectionBrandCircle">&nbsp;</div>';
		html += 'Marque<br/><br/>';
		for(i=0; i<response.length; i++){
			html += '<a href="javascript:Selection.loadType('+response[i][0]+',\''+escapeSimpleQuote(response[i][1])+'\')">'+response[i][1]+'</a><br/>';
		}
		div.innerHTML = html;
		div.className = "selectionBrandOpen";
		Selection.clearType();
	},
	
	updateBrand : function(){
		var div = document.getElementById(Selection.BRAND_DIV_ID);
		if(div==null)
			return;
		div.innerHTML = '<a href="javascript:Selection.loadType('+this.brandId+',\''+escapeSimpleQuote(this.brandName)+'\');">'+this.brandName+'</a>';
		div.className = "selectionBrand";
	},
	
	typeLoaded : function(){
		var div = document.getElementById(Selection.TYPE_DIV_ID);
		if(div==null)
			return;
		Selection.hideProduct();
		var response = eval(this.request.responseText);
		var html = '<div class="selectionTypeCircle">&nbsp;</div>';
		html += 'Type<br/><br/>';
		for(i=0; i<response.length; i++){
			html += '<a href="javascript:Selection.loadSeries('+response[i][0]+',\''+escapeSimpleQuote(response[i][1])+'\')">'+response[i][1]+'</a><br/>';
		}
		div.innerHTML = html;
		div.className = "selectionTypeOpen";
		Selection.updateBrand();
		Selection.clearSeries();
	},
	
	updateType : function(){
		var div = document.getElementById(Selection.TYPE_DIV_ID);
		if(div==null)
			return;
		div.innerHTML = '<a href="javascript:Selection.loadSeries('+this.typeId+',\''+escapeSimpleQuote(this.typeName)+'\');">'+this.typeName+'</a>';
		div.className = "selectionType";
	},
	
	seriesLoaded : function(){
		var div = document.getElementById(Selection.SERIES_DIV_ID);
		if(div==null)
			return;
		Selection.hideProduct();
		var response = eval(this.request.responseText);
		var html = '<div class="selectionSeriesCircle">&nbsp;</div>';
		html += 'Série<br/><br/>';
		for(i=0; i<response.length; i++){
			html += '<a href="javascript:Selection.loadModel('+response[i][0]+',\''+escapeSimpleQuote(response[i][1])+'\')">'+response[i][1]+'</a><br/>';
		}
		html += '<div class="selectionSeriesAll"><a href="javascript:Selection.loadCartridgeByType()">Cartouches par références</a></div>';
		html += '<div class="selectionSeriesBottomLine">&nbsp;</div>';
		html += '<div class="selectionSeriesBottomCircle">&nbsp;</div>';
		div.innerHTML = html;
		div.className = "selectionSeriesOpen";
		Selection.updateType();
		Selection.clearModel();
	},
	
	updateSeries : function(){
		var div = document.getElementById(Selection.SERIES_DIV_ID);
		if(div==null)
			return;
		div.innerHTML = '<a href="javascript:Selection.loadModel('+this.seriesId+',\''+escapeSimpleQuote(this.seriesName)+'\');">'+this.seriesName+'</a>';
		div.className = "selectionSeries";
	},
	
	modelLoaded : function(){
		var div = document.getElementById(Selection.MODEL_DIV_ID);
		if(div==null)
			return;
		Selection.hideProduct();
		var response = eval(this.request.responseText);
		var html = '<div class="selectionModelCircle">&nbsp;</div>';
		html += 'Modèle<br/><br/>';
		var cols = Math.ceil(response.length/Selection.MODEL_PER_COLUMN);
		html += '<table class="model"><tr>';
		for(c=0; c<cols; c++){
			html += '<td>';
			for(i=c*Selection.MODEL_PER_COLUMN; i<(c+1)*Selection.MODEL_PER_COLUMN&&i<response.length; i++){
				html += '<a href="'+ROOT+'cartouche/'+response[i][0]+'">'+response[i][1]+'<br/>';
			}
			html += '</td>';
		}
		html += '</tr></table>';
		html += '<div class="selectionModelAll"><a href="javascript:Selection.loadCartridgeBySeries()">Cartouches par références</a></div>';
		html += '<div class="selectionModelBottomLine">&nbsp;</div>';
		html += '<div class="selectionModelBottomCircle">&nbsp;</div>';
		div.innerHTML = html;
		div.className = "selectionModelOpen";
		Selection.updateSeries();
	},
	
	updateModel : function(){
		var div = document.getElementById(Selection.MODEL_DIV_ID);
		if(div==null)
			return;
		div.innerHTML = '<a href="#">'+this.modelName+'</a>';
		div.className = "selectionModel";
	},
	
	cartridgeByTypeLoaded : function(){
		var div = document.getElementById(Selection.SERIES_DIV_ID);
		if(div==null)
			return;
		var response = this.request.responseObject;
		var html = 'Série<br/><br/>'+response.length+'&nbsp;références<br/><br/>';
		var mpc = Selection.MODEL_PER_COLUMN;
		if(response.length<=Selection.MODEL_PER_COLUMN)
			mpc = Selection.MODEL_PER_COLUMN;
		else if(response.length<1.5*Selection.MODEL_PER_COLUMN)
			mpc = Math.ceil(response.length/2);
		var cols = Math.ceil(response.length/mpc);
		html += '<div class="selectionSeriesTable">';
		html += '<table class="series"><tr>';
		for(c=0; c<cols; c++){
			html += '<td>';
			for(i=c*mpc; i<(c+1)*mpc&&i<response.length; i++){
				html += '<a href="'+ROOT+'cartouche/'+response[i][0]+'">'+response[i][1]+'<br/>';
			}
			html += '</td>';
		}
		html += '</tr></table>';
		html += '</div>';
		html += '<div class="selectionSeriesBottomLine">&nbsp;</div>';
		html += '<div class="selectionSeriesBottomCircle">&nbsp;</div>';
		div.className = "selectionSeriesFull";
		div.innerHTML = html;
		var div = document.getElementById(Selection.MODEL_DIV_ID);
		if(div==null)
			return;
		div.style.display = "none";
	},
	
	cartridgeBySeriesLoaded : function(){
		var div = document.getElementById(Selection.MODEL_DIV_ID);
		if(div==null)
			return;
		var response = this.request.responseObject;
		var html = 'Modèle<br/><br/>'+response.length+'&nbsp;références<br/><br/>';
		var cols = Math.ceil(response.length/Selection.MODEL_PER_COLUMN);
		html += '<div class="selectionModelTable">';
		html += '<table class="model"><tr>';
		for(c=0; c<cols; c++){
			html += '<td>';
			for(i=c*Selection.MODEL_PER_COLUMN; i<(c+1)*Selection.MODEL_PER_COLUMN&&i<response.length; i++){
				html += '<a href="'+ROOT+'cartouche/'+response[i][0]+'">'+response[i][1]+'<br/>';
			}
			html += '</td>';
		}
		html += '</tr></table>';
		html += '</div>';
		html += '<div class="selectionModelBottomLine">&nbsp;</div>';
		html += '<div class="selectionModelBottomCircle">&nbsp;</div>';
		div.className = "selectionModelFull";
		div.innerHTML = html;
	},
	
	hide : function(){
		var pcdiv = document.getElementById(Selection.PRINTER_DIV_ID)||
			document.getElementById(Selection.CARTRIDGE_DIV_ID)||
			document.getElementById(Selection.PUB_DIV_ID)||
			document.getElementById(Selection.BRANDS_DIV_ID);
		if(pcdiv!=null)
			pcdiv.style.display="block";
		var editodiv = document.getElementById(Selection.EDITO_DIV_ID);
		if(editodiv!=null)
			editodiv.style.display="block";
		var cartdiv = document.getElementById(Selection.CART_JET_LASER_DIV_ID);
		if(cartdiv!=null)
			cartdiv.style.display="block";
		var closeDiv = document.getElementById(Selection.CLOSE_SELECTION_DIV_ID);
		if(closeDiv!=null)
			closeDiv.style.display="none";
		var div = document.getElementById(Selection.SELECTION_MENU_DIV_ID);
		if(div!=null)
			div.style.display="none";
		Selection.clearType();
	},
	
	clearType : function(){
		var div = document.getElementById(Selection.TYPE_DIV_ID);
		if(div==null)
			return;
		div.innerHTML = "Type";
		div.className = "selectionType";
		this.clearSeries();
	},
	
	clearSeries : function(){
		var div = document.getElementById(Selection.SERIES_DIV_ID);
		if(div==null)
			return;
		div.innerHTML = "Série";
		div.className = "selectionSeries";
		this.clearModel();
	},
	
	clearModel : function(){
		var div = document.getElementById(Selection.MODEL_DIV_ID);
		if(div==null)
			return;
		div.innerHTML = "Modèle";
		div.className = "selectionModel";
		div.style.display = "block";
	},
	
	hideProduct : function(){
		var pcdiv = document.getElementById(Selection.PRINTER_DIV_ID)||
			document.getElementById(Selection.CARTRIDGE_DIV_ID)||
			document.getElementById(Selection.PUB_DIV_ID)||
			document.getElementById(Selection.BRANDS_DIV_ID);
		if(pcdiv!=null)
			pcdiv.style.display="none";
		var editodiv = document.getElementById(Selection.EDITO_DIV_ID);
		if(editodiv!=null)
			editodiv.style.display="none";
		var cartdiv = document.getElementById(Selection.CART_JET_LASER_DIV_ID);
		if(cartdiv!=null)
			cartdiv.style.display="none";
		var closeDiv = document.getElementById(Selection.CLOSE_SELECTION_DIV_ID);
		if(closeDiv!=null)
			closeDiv.style.display="block";
	},
	
	callBrand : null,
	
	openBrand : function(brand){
		if(isInProgress())
			return;
		var div = document.getElementById(this.SELECTION_MENU_DIV_ID);
		if(div==null)
			return;
		div.style.display="block";
		var request = new ajax();
		this.callBrand = brand;
		request.parameters = "ajax=1&method=readAll&path=query/brand&parameter=marque";
		request.onload = Selection.brandOpen;
		request.send("post", ROOT+"query/brand", request.parameters);
	},
	
	brandOpen : function(){
		if(Selection.callBrand==null)
			return;
		var div = document.getElementById(Selection.BRAND_DIV_ID);
		if(div==null)
			return;
		Selection.hideProduct();
		
		var brandId = 0;
		var response = eval(this.request.responseText);
		var html = '<div class="selectionBrandCircle">&nbsp;</div>';
		html += 'Marque<br/><br/>';
		for(i=0; i<response.length; i++){
			if(response[i][1]==Selection.callBrand)
				brandId = response[i][0];
			html += '<a href="javascript:Selection.loadType('+response[i][0]+',\''+escapeSimpleQuote(response[i][1])+'\')">'+response[i][1]+'</a><br/>';
		}
		if(brandId>0){
			div.innerHTML = html;
			div.className = "selectionBrandOpen";
			Selection.clearType();
			Selection.loadType(brandId, Selection.callBrand);
		}
		Selection.callBrand = null;
	}
	

}

