function cambiaColore( idProdotto, idColore, desColore ) {
	new Ajax.Updater('taglieDisponibili', '/product_info.php', {
			parameters: { action: 'recupera_taglie', products_id: idProdotto, options_values_id: idColore }
			, method: 'get'
		});
	//Imposto il campo nascosto del colore selezionato
	//alert($('id[1]'));
	$('id[1]').setValue(idColore);
	$('coloreSelezionato').update('&nbsp;' + desColore);
	
	//Rimuovo la classe selected dai Colori
	var child = $('coloriDisponibili').childElements();
	for (i=0; i<child.length; ++i) {
		//Associo la classe al Link
		if (child[i].hasClassName('colore_selected'))
			child[i].removeClassName('colore_selected');
	}
	
	//Aggiungo la classe selected
	$('colore_' + idColore).addClassName('colore_selected');
	
	
	$('id[9000]').setValue('');
	$('tagliaSelezionata').update('&nbsp;');
	
	visualizzaImmagineByColore(idColore);
	}

function cambiaTaglia( taglia ) {
	
	//Rimuovo la classe selected dalle Taglie
	var child = $('taglieDisponibili').childElements();
	for (i=0; i<child.length; ++i) {
		if (child[i].hasClassName('taglia_selected'))
			child[i].removeClassName('taglia_selected');
	}
	
	//Aggiungo la classe selected
	$('taglia_' + taglia).addClassName('taglia_selected');
	
	$('id[9000]').setValue(taglia);
	$('tagliaSelezionata').update('&nbsp;' + taglia);
	
}


function updateComboTaglie(source) {
  var idColore = source.options[source.selectedIndex].value;
  var idProduct = $('add_product_products_id').getValue();
  //alert(idProduct);
  new Ajax.Updater('boxTaglie', '/admin/edit_orders.php', {
      parameters: { action: 'update_taglie'
                  , add_product_products_id: idProduct
                  , add_product_options_id: idColore }
    , method: 'get'
  });

}
function visualizzaImmagineByColore( newIdColore ) {
	var imgDefault = aryProductImage[0];
	var startColore = imgDefault.lastIndexOf('_') + 1;
	var endColore = imgDefault.lastIndexOf('.');
	var defaultColore = imgDefault.substring(startColore, endColore);
	var idColore = defaultColore;
	if (newIdColore != 0) idColore = newIdColore;
	
	//var idProdotto = $('products_id').getValue();
	var row = 0;
	$('thumbImages').update();
	if (!isNaN(idColore)) {
		for (i=0; i < aryProductImage.length; ++i) {
			curImg = aryProductImage[i];
			startColore = curImg.lastIndexOf('_') + 1;
			endColore = curImg.lastIndexOf('.');
			colImg = curImg.substring(startColore, endColore);
			if (curImg.length > 0 && (colImg == idColore || idColore == -1)) {
				if (row == 0 )
					cambiaImg (curImg);
				var img = new Element('img', { 'src': urlImgMicro + curImg, 'height': '60', 'width': '50', 'border': '0' });
				var a = new Element('a', { 'href': 'javascript:cambiaImg(\'' + curImg + '\');' }).update(img);
				$('thumbImages').insert(a);
				++row;
			}
		}

		//Se row = 0 non e' stato trovato il colore, ricarico con quello di Default
		if (row == 0 && defaultColore > 0) 
			visualizzaImmagineByColore(defaultColore);
		
	} else { //Altrimenti se ci sono foto ma non codificati correttamente con _IDCOLORE.jpg visualizzo tutte le foto
		visualizzaImmagineByColore( -1 );
	}
}
/* Verifico che il Colore e la Taglia siano stati selezionati
 * 
 */
function checkCartForm() {
	if ($('id[1]').getValue() == '' || $('id[9000]').getValue() == '') {
		alert('Selezionare sia il Colore che la Taglia!');
		return false;
	}
	
	return true;
}

//Nascondo o visualizzo i sub_option nella scheda di modifica articolo (Taglie)
function showSubOption( id, value, bShow ) {
	var so = 'sub_options_id_' + id;
	if (bShow) {
		$(so).show();
		//Aggiungo il nuovo colore
		aryColori[aryColori.length] = value + ';' + id;
	} else {
		$(so).hide();
		var tmpAry = new Array();
		var z = 0;
		for (i=0; i< aryColori.length; ++i) {
			var rowString = aryColori[i];
			var row = rowString.split(';'); //Testo;ID
			//Ricopio l'array eliminado l'id selezionato
			if (row[1] != id) tmpAry[z++] = aryColori[i];
		}
		aryColori = tmpAry;
	}
	//Aggiorno i Combo delle immagini
	updateComboColoreImmagine();
}

/* Carica i colori selezionati per l'articolo e popola
 * il Combo colore_immagine, sezione admin
 */
function updateComboColoreImmagine() {
	var cf = 'option[1]';
	var of = 'color_image';
	var df = of;
	/* Nr di Combo colori_immagine presenti
	 * Se aumentano il Nr di immagini aumentare il valore di c
	 */
	var c = 15; 
	
	//Ciclo per tutti i campi colore_immagini presenti
	for (z = 0; z <= c; ++z) {
		if (z > 0) of = df + z;

		if (aryColori.length == 0) { //Se non ci sono colori associati al materiale
			var opt = new Element('option', { 'value': '-1', 'text' : 'Nessun colore associato al materiale' });
			opt.update('Nessun colore associato al materiale');
			$(of).update(opt);
		} else {
			aryColori.sort();
			for (i=0; i< aryColori.length; ++i) {
				var rowString = aryColori[i];
				var row = rowString.split(';'); //Testo;ID
	
				var opt = new Element('option', { 'value': removeExtraChar( row[0] ) + '_' + row[1], 'text': row[0] });
				opt.update(row[0]);
				
				if ( i == 0 ) //Al primo giro pulisco il Combo
					$(of).update(opt);
				else
					$(of).insert(opt);
		
			}
		}
	}
}

/* Rimuovo i caratteri speciali dalla Stringa
 * 
 */
function removeExtraChar( field) {
	 var blah = new String(field);
	 blah = blah.replace(/(\s+)/g,""); // consolidate white space
	 blah = blah.replace(/^ /,""); // remove leading white space
	 blah = blah.replace(/ $/,""); // remove trailing white space
	 blah = blah.replace(/"/g,""); // remove double quotes
	 blah = blah.replace(/'/g,""); // remove single quotes
	 blah = blah.replace(/`/g,""); // remove `
	 blah = blah.replace(/~/g,""); // remove ~
	 blah = blah.replace(/!/g,""); // remove !
	 blah = blah.replace(/@/g,""); // remove @
	 blah = blah.replace(/#/g,""); // remove #
	 blah = blah.replace(/$/g,""); // remove $
	 blah = blah.replace(/%/g,""); // remove %
	 blah = blah.replace(/\^/g,""); // remove ^
	 blah = blah.replace(/&/g,""); // remove &
	 blah = blah.replace(/_/g,""); // remove _
	 blah = blah.replace(/\=/g,""); // remove =
	 blah = blah.replace(/\//g,""); // remove
	 blah = blah.replace(/\\/g,""); // remove
	 blah = blah.replace(/\(/g,""); // remove (
	 blah = blah.replace(/\) /g,""); // replace )space with -
	 blah = blah.replace(/\)/g,""); // replace ) with -
	 blah = blah.replace(/\./g,""); // replace . with -
	 blah = blah.replace(/-/g,""); // remove
	 return blah;
 }

/*
 * Slide delle icone presenti nella div
 * obj->Id dell'oggetto che contiene le thumbnail da visualizzare
 * action -> true muovo avanti, false vado indietro
 */

var stoSlide = new Array();

function move_slide( obj, action ) { 
	var ce = $(obj).childElements();
	var li = 0;//Indice dell'ultima immagine visibile
	var imgPrev = ce[0];
	var imgNext = ce[ce.length-1];
	var nrThumb = ce.length - 2;
	var nrShowImg = 3; //Numero delle immagini visualizzate
	//Dal ciclo non considero le immagini (Prima e ultima)
	//Recupero l'ultimo thumb inline
	for (var i = 1; i < ce.length-1; ++i) {
		if (ce[i].getStyle('display') == 'inline') li = i;
	}

	if ( action ) {
		ce[li+1].setStyle("display: inline");
		ce[li-2].setStyle("display: none");
		if ( nrThumb == (li+1)) {
			imgNext.removeClassName('showPointer');
			imgNext.addClassName('inactive');
			imgNext.stopObserving('click');
			imgNext.writeAttribute('onclick');
		}
		if (li+1 == nrShowImg + 1) {
			imgPrev.addClassName('showPointer');
			imgPrev.removeClassName('inactive');
			imgPrev.observe('click', function(event) {
				  move_slide(obj,false);
			});
		}
	} else {
		ce[li-3].setStyle("display: inline");
		ce[li].setStyle("display: none");
		if ((li - nrShowImg) == 1) {
			imgPrev.removeClassName('showPointer');
			imgPrev.addClassName('inactive');
			imgPrev.stopObserving('click');

		}
		if (li - nrShowImg == nrThumb - nrShowImg ) {
			imgNext.addClassName('showPointer');
			imgNext.removeClassName('inactive');
			imgNext.observe('click', function(event) {
				  move_slide(obj,true);
			});
		}
	}
}



