/*
	Autore Vincenzo Miele
	** La funziona AJAX prende in input una categoria
	e a partire da essa estrae e costruisce in un menù select
	tutti i brand associati a quella categoria.
*/
// Option di default per la selezione
var optionSelBrand = new Element('option');
optionSelBrand.update('-- Selezionare brand --');
optionSelBrand.setAttribute('value', '');

var optionSelModello = new Element('option');
optionSelModello.update('-- Selezionare modello --');
optionSelModello.setAttribute('value', '');
	
function getBrand(){
	var form = $('formTicker');
	var post = form.serialize();
	var categoria = $('categoria');
	var catId = parseInt(categoria.getValue());
	$('loadingCategoria').hide();
	$('loadingBrand').hide();
	$('loadingModello').hide();
	$('listaProdotti').hide();	
	
					
	if(catId > 0 && !isNaN(catId)){
		$('loadingBrand').show();
		var url = "/catalogo/index/tickerGetBrand";
		new Ajax.Request(url, {
		  method: 'post',
		  evalJSON: true,
		  postBody: post,
		  onSuccess: function(transport) {
				var ritorno = transport.responseJSON;
				
				if(ritorno != ""){
					// Elimino tutti gli option attuali
					$('brand').update('');
					$('modello').update('');
					$('brand').insert(optionSelBrand, {position: 'bottom'});
					$('modello').insert(optionSelModello, {position: 'bottom'});
					/*
					var brand = $('brand').childElements();
					for(var i=0;i<brand.length;i++){
						brand[i].remove();
					}
					*/					
					
					if(ritorno.numBrand > 0){
						var brand= ritorno.brand
						var numBrand = brand.length;
						
						for(var i=0; i<numBrand; i++){
							var option = new Element('option');
							option.update(brand[i].nome);
							option.setAttribute('value', brand[i].id);
							$('brand').insert(option, {position: 'bottom'});
						}
					}					
					$('loadingBrand').hide();
				}				
			}
		});
	}
	else{
		$('brand').update('');
		$('brand').insert(optionSelBrand, {position: 'bottom'});
		$('modello').update('');
		$('modello').insert(optionSelModello, {position: 'bottom'});
	}
}

/*
	Autore Vincenzo Miele
	** La funziona AJAX prende in input un modello
	e a partir da esso estrae e costruisce in un menù select
	tutti i modelli associati al brand.
*/
function getModello(){
	var form = $('formTicker');
	var post = form.serialize();
	var brand = $('brand');
	var brandId = parseInt(brand.getValue());
	$('loadingCategoria').hide();
	$('loadingBrand').hide();
	$('loadingModello').hide();
	$('listaProdotti').hide();
					
	if(brandId > 0 && !isNaN(brandId)){
		$('loadingModello').show();
		var url = "/catalogo/index/tickerGetModello";
		new Ajax.Request(url, {
		  method: 'post',
		  evalJSON: true,
		  postBody: post,
		  onSuccess: function(transport) {
				var ritorno = transport.responseJSON;
				if(ritorno != ""){
					// Elimino tutti gli option attuali
					$('modello').update('');
					/*
					var modello = $('modello').childElements();
					for(var i=0;i<brand.length;i++){
						brand[i].remove();
					}
					*/					
					
					$('modello').insert(optionSelModello, {position: 'bottom'});
					
					if(ritorno.numModello > 0){
						var modello= ritorno.modello
						var numModello = modello.length;
						
						for(var i=0; i<numModello; i++){
							var option = new Element('option');
							option.update(modello[i].nome);
							option.setAttribute('value', modello[i].id);
							//option.setAttribute('onchange', 'javascript:getModello('+brand[i].id+')');
							$('modello').insert(option, {position: 'bottom'});
						}
					}
					
					$('loadingModello').hide();
				}				
			}
		});
	}
	else{
		$('modello').update('');
		$('modello').insert(optionSelModello, {position: 'bottom'});
	}
}

/*
	Autore Vincenzo Miele
	** La funziona AJAX prende in input un modello
	e a partir da esso estrae e costruisce in un menù select
	tutti i modelli associati al brand.
*/

function getProdotti(){
	
	var form = $('formTicker');
	var post = form.serialize();
	var brand = $('brand');
	var brandId = parseInt(brand.getValue());
	var auth = $('auth');
	var authId = parseInt(auth.getValue());
	$('loadingCategoria').hide();
	$('loadingBrand').hide();
	$('loadingModello').hide();
	$('listaProdotti').hide();
	
					
	if(brandId > 0 && !isNaN(brandId)){
		//$('loadingModello').show();
		var url = "/catalogo/index/tickerGetProdotti";
		new Ajax.Request(url, {
		  method: 'post',
		  evalJSON: true,
		  postBody: post,
		  onSuccess: function(transport) {
			
				var ritorno = transport.responseJSON;
				
				if(ritorno != ""){
					// Elimino tutti gli option attuali
					
					//$('tabProdotti').update('');
					$('listaProdotti').update('');
					if(ritorno.numProdotti > 0){
						var tabProdotti = new Element('TABLE', {'class': 'tabProdotti', 'id': 'tabProdotti'});
						
						var tbody = new Element('TBODY');	
						
						var prodotto= ritorno.prodotto
						var numProdotti = prodotto.length;
						
						var tr = new Element('TR');
						// Codice
						var th = new Element('TH');
						th.setAttribute('class', 'codice');
						th.update('Codice');
						tr.insert(th);
						
						// Descrizione
						var th = new Element('TH');
						th.update('Descrizione');
						tr.insert(th);
						
						// dettaglio
						var th = new Element('TH');
						th.update('Dettaglio');
						tr.insert(th);
											
						
						// Prezzo
						/*
						var th = new Element('TH');
						th.update('Prezzo');
						tr.insert(th);
						*/
						// Operazioni
						if(authId == 1){
							var th = new Element('TH');
							th.setAttribute('class', 'operazioni');
							th.update('');
							tr.insert(th);
						}
						//$('tabProdotti').insert(tr);
						tbody.insert(tr);
						//tabProdotti.insert(tbody);
						
						for(var i=0; i<numProdotti; i++){
							
							var tr = new Element('TR');
							
							// Codice
							var td = new Element('TD');
							//td.setAttribute('class', 'codice');
							td.update(prodotto[i].codice);
							tr.insert(td);
							
							// Descrizione
							var td = new Element('TD');
							td.update(prodotto[i].descrizione);
							tr.insert(td);
							
							// dettaglio
							var td = new Element('TD');
							td.update(prodotto[i].dettaglio);
							tr.insert(td);
							
							// Prezzo
							/*
							var td = new Element('TD');
							td.update(prodotto[i].prezzo);
							tr.insert(td);
							*/
							// Add
							
							if(authId == 1){
								var td = new Element('TD');
								var a = new Element('A', {'href': prodotto[i].url, 'class': 'cart', 'target': '_parent'}).update('Aggiungi');
								td.update(a);
								tr.insert(td);
							}
							//$('tabProdotti').insert(tr);
							//tabProdotti.insert(tr);
							tbody.insert(tr);
						    //tabProdotti.insert(tbody);
						
							//$('tabProdotti').innerHTML = "<tr><td>AA</td></tr>";
						
						}
						tabProdotti.insert(tbody);
					}
					// Con Explorer 7 non fa l'update... non so il perchè devo fare del debug
					$('listaProdotti').update('');
					// tabProdotti
					/*
					var tabProdotti = new Element('TABLE', {'class': 'tabProdotti', 'id': 'tabProdotti'});
					var tbody = new Element('TBODY');					
					var tr = new Element('TR');
					// Codice
					var th = new Element('TH');
					//th.setAttribute('class', 'codice');
					th.update('Codice');
					tr.insert(th);
					tbody.insert(tr);
					tabProdotti.insert(tbody);
					*/
					$('listaProdotti').insert(tabProdotti);
					
					$('listaProdotti').show();
				}				
			}
		});
	}
	else{
		
	}
}
