function getModello(){
	
	
	$('modello').writeAttribute('disabled', true);
	if($('prodotto')){
		$('prodotto').writeAttribute('disabled', true);
	}
	if($('schedaProdotto')){
		$('schedaProdotto').hide();
	}
	$('trovato').hide();
	$('loading').show();
	
	var brand = $('brand');
	
	var brandId = parseInt(brand.getValue()); 
	var categoria = $('categoria');
	var categoriaId = parseInt(categoria.getValue());
	
	var postString = $('formFiltro').serialize();
	
	
	if(!isNaN(brandId) && brandId > 0){
		
		var url = "/catalogo/index/ajaxGetModello";
		new Ajax.Request(url, {
		  method: 'post',
		  evalJSON: true,
		 // postBody: 'brand='+brandId+'&categoria='+categoriaId,
		  postBody: postString,
		  onSuccess: function(transport) {
				
				var ritorno = transport.responseJSON;
				if(ritorno != ""){
					var modelli = ritorno.modelli;
					$('modello').update("");
					if($('modello_all')){
						if($('modello_all').getValue() == 1){
							var optionP = new Element("option", {"value":0})
							optionP.update("Tutti");
							$('modello').insert({"bottom":optionP});		
						}
					}
					for(var i=0;i<modelli.length;i++){
						modello = modelli[i];
						var optionP = new Element("option", {"value":modello.id});
						if(modello.selected == true){
							optionP.writeAttribute("selected", "selected");
						}
						
						optionP.update(modello.descrizione);
						$('modello').insert({"bottom":optionP});	
					}
					
					if($('prodotto')){
						var optionP = new Element("option", {"value":""})
						optionP.update('[Step 3 - Prodotto]');
						$('prodotto').update(optionP);
					}
					
					$('modello').writeAttribute('disabled', false);
					$('loading').hide();
				}
				
			}
		});
	}
	else{
		$('loading').hide();
	}
	
}

function getProdotto(){	
	
	$('loading').show();
	$('trovato').hide();
	$('schedaProdotto').hide();
	$('prodotto').writeAttribute('disabled', true);
	var categoria = $('categoria');
	var categoriaId = parseInt(categoria.value); 
	
	var brand     = $('brand');
	var modello   = $('modello');
	var brandId   = parseInt(brand.value); 
	var modelloId = parseInt(modello.value); 
	
	if(!isNaN(brandId) && brandId > 0 && !isNaN(modelloId) && modelloId > 0){
		var url = "/catalogo/index/ajaxGetProdotto";
		new Ajax.Request(url, {
		  method: 'post',
		 // postBody: 'brand='+brandId+'&categoria='+categoriaId,
		  postBody: 'brand='+brandId+'&modello='+modelloId+'&categoria='+categoriaId,
		  onSuccess: function(transport) {
				
				var ritorno = transport.responseText;
				//alert(ritorno);
				if(ritorno != ""){
					eval(ritorno);
					$('prodotto').writeAttribute('disabled', false);
				}
				else{
					
					alert('Nessun prodotto trovato');
				}
				$('loading').hide();
				
			}
		});
		/*
		new Ajax.Request(url, {
		  method: 'post',
		  postBody: 'brand='+brandId+'&modello='+modelloId+'&categoria='+categoriaId,
		  onSuccess: function(transport) {
				var ritorno = transport.responseText;
				alert(ritono);
				if(ritorno != ""){
					
					eval(ritorno);
					$('prodotto').writeAttribute('disabled', false);
					
				}
				else{
					
					alert('Nessun prodotto trovato');
				}
				$('loading').hide();
			}
		});
		*/
	}
	else{
		$('loading').hide();
	}
}

/*

*/
function viewProdotto(){
	$('loading').show();
	$('trovato').hide();
	var prodotto     = $('prodotto');
	var prodottoId   = prodotto.value;
	var brand     = $('brand');
	var modello   = $('modello');
	var brandId   = parseInt(brand.value); 
	var modelloId = parseInt(modello.value); 
	if(prodottoId != ""){
		var url = "/catalogo/index/ajaxViewProdotto";
		new Ajax.Request(url, {
		  method: 'post',
		  evalJSON: true,
		  postBody: 'brand='+brandId+'&modello='+modelloId+'&codice='+prodottoId,
		  onSuccess: function(transport) {
				var ritorno = transport.responseJSON;
				
				if(ritorno != null){
					$('codice').update(ritorno.codice);
					$('descrizione').update(ritorno.descrizione);
					$('dettaglio').update(ritorno.dettaglio);
					$('note').update(ritorno.note);
					$('prezzo').update(ritorno.prezzo);
					$('nomeProdotto').update(ritorno.nome);
					$('linkRivenditore').href = "/rivenditore/index?prodotto="+ritorno.codice;
					$('schedaProdotto').show();
					//getFoto.php?codice=
					$('resultcontent').setStyle({
					  background: 'url('+ritorno.foto+') top left no-repeat'
					});
					$('clickOrdine').href = '/catalogo/ordine/index/prodotto/'+ritorno.codice;
					$('clickOrdine').href += '/brand/'+brandId+'/modello/'+modelloId;
				}
				else{
					$('codice').update('');
					$('prezzo').update('');
					$('descrizione').update('');
					$('dettaglio').update('');
					$('note').update('');
					$('nomeProdotto').update('');
					$('schedaProdotto').hide();
					$('clickOrdine').href = '#';
				}
				$('loading').hide();
				$('trovato').show();
			}
		});
	}
}

function setFilter(idName){
	var id = $(idName);
	$('alimentatoriTab').removeClassName('flap1-selected');
	$('batterieTab').removeClassName('flap1-selected');
	id.addClassName('flap1-selected');
	if(idName == 'alimentatoriTab'){
		$('categoria').value = 2;	
	}
	if(idName == 'batterieTab'){
		$('categoria').value = 1;	
	}	
}


function getProvince(e, provinciaSel){
	
	var optionP = new Element("option", {"value":"0"})
	optionP.update('Tutte');
	$('provincia').update(optionP);
	$('loading').show();
	
	var regione = $('regione');
	
	var regioneId = parseInt(regione.getValue()); 
	
	if(!isNaN(regioneId) && regioneId > 0){
		var url = "/catalogo/ordine/ajaxGetOrdineProvince";
		new Ajax.Request(url, {
		  method: 'post',
		  postBody: 'regione='+regioneId,
		  evalJSON: true,
		  onSuccess: function(transport) {
				var ritorno = transport.responseJSON;
				if(ritorno != ""){
					if(ritorno.errore){
						alert(ritorno.descrErrore);			
					}
					else{
						var province = ritorno.province;
						for(var i=0; i<province.length;i++){
							provincia = province[i];
							var optionP = new Element("option", {"value":provincia.id});
						
							if(provincia.id == provinciaSel){
								optionP.writeAttribute('selected', 'selected');
							}
							
							optionP.update(provincia.nome);
							$('provincia').insert({"bottom":optionP});
						}
						$('provincia').writeAttribute('disabled', false);	
					}
					$('loading').hide();					
				}
				
			}
		});
	}
	else{
		$('loading').hide();
	}
	
}
