var tot_art0 = 0;
var tot_ord0 = 0;
var app = 0;
var cons = 0;
var FOLDERNEW = 'ita';
var enableClick = true;

function ajax_edit(el) {
	if (enableClick) {
		var old = $('#' + el).html();
		if (old == '&nbsp;')
			old = '';
		el.indexOf('titolo1') >= 0 ? size = 80 : size = 5;
		$('#' + el).html(
				'<input type="text" size="' + size + '" id="mod" value="' + old
						+ '" ' + 'onBlur="ajax_upd(\'' + el + '\',\'' + old
						+ '\')">');
		$('#' + el).ready(function() {
			$('#mod').ready(function() {
				$('#mod').focus();
			});
		});
	}
	enableClick = false;
}

function ajax_edit_costo(el, idfornitore) {
	if (enableClick) {
		var old = $('#' + el).html();
		if (old == '&nbsp;')
			old = '';
		el.indexOf('titolo1') >= 0 ? size = 80 : size = 5;
		$('#' + el).html(
				'<input type="text" size="' + size + '" id="mod" value="' + old
						+ '" ' + 'onBlur="ajax_upd_costo(\'' + el + '\',\''
						+ old + '\',' + idfornitore + ')">');
		$('#' + el).ready(function() {
			$('#mod').ready(function() {
				$('#mod').focus();
			});
		});
	}
	enableClick = false;
}

function ajax_edit_codice(el, idfornitore) {
	if (enableClick) {
		var old = $('#' + el).html();
		if (old == '&nbsp;')
			old = '';
		el.indexOf('titolo1') >= 0 ? size = 80 : size = 5;
		$('#' + el).html(
				'<input type="text" size="' + size + '" id="mod" value="' + old
						+ '" ' + 'onBlur="ajax_upd_codice(\'' + el + '\',\''
						+ old + '\',' + idfornitore + ')">');
		$('#' + el).ready(function() {
			$('#mod').ready(function() {
				$('#mod').focus();
			});
		});
	}
	enableClick = false;
}

function ajax_upd(el, old) {
	// if (!isNaN($('#mod').val())) {
	arrMod = el.split('_');
	$.ajax({
		type : 'POST',
		url : 'product_new.php',
		data : 'field=' + arrMod[0] + '&value=' + $('#mod').val()
				+ '&idarticolo=' + arrMod[1] + '&op=upd_field',
		dataType : 'html',
		success : function(msg) {
			$('#' + el).html($('#mod').val());
			enableClick = true;
		}
	});
	// }
	// else
	// $('#' + el).html(old);
}

function ajax_upd_costo(el, old, idfornitore) {
	// if (!isNaN($('#mod').val())) {
	arrMod = el.split('_');
	$.ajax({
		type : 'POST',
		url : 'product_new.php',
		data : 'idfornitore='
				+ (idfornitore > 0 ? idfornitore : $(
						'#new_fornitore_' + arrMod[1]).val()) + '&value='
				+ $('#mod').val() + '&idarticolo=' + arrMod[1]
				+ '&op=upd_costo',
		dataType : 'html',
		success : function(msg) {
			$('#' + el).html($('#mod').val());
			enableClick = true;
		}
	});
	// }
	// else
	// $('#' + el).html(old);
}

function ajax_upd_codice(el, old, idfornitore) {
	// if (!isNaN($('#mod').val())) {
	arrMod = el.split('_');
	$.ajax({
		type : 'POST',
		url : 'product_new.php',
		data : 'idfornitore='
				+ (idfornitore > 0 ? idfornitore : $(
						'#new_fornitore_' + arrMod[1]).val()) + '&value='
				+ $('#mod').val() + '&idarticolo=' + arrMod[1]
				+ '&op=upd_codice',
		dataType : 'html',
		success : function(msg) {
			$('#' + el).html($('#mod').val());
			enableClick = true;
		}
	});
	// }
	// else
	// $('#' + el).html(old);
}

function ajax_del_promozioni(idarticolo, tipo, cid) {
	$.ajax({
		type : 'POST',
		url : 'product_new.php',
		data : 'tipo=' + tipo + '&idarticolo=' + idarticolo + '&cid=' + cid
				+ '&op=del_prom',
		dataType : 'html',
		success : function(msg) {
			$('#promozioni').html(msg);
		}
	});
}

function ajax_add_promozioni(tipo, cid) {
	var idarticolo = $('#idarticolo_' + tipo).val();
	$.ajax({
		type : 'POST',
		url : 'product_new.php',
		data : 'tipo=' + tipo + '&idarticolo=' + idarticolo + '&cid=' + cid
				+ '&op=add_prom',
		dataType : 'html',
		success : function(msg) {
			$('#promozioni').html(msg);
		}
	});
}

function check_uncheck() {
	$('input:checkbox').each(function(i) {
		if ($('#allcorrelati').is(':checked'))
			this.checked = true;
		else
			this.checked = false;
	})
}

/* MENU */
function onAnimate(show) {
	// $(this).fadeIn('slow').show();
	if (show) {
		$(this).css('visibility', 'hidden').show().css('width',
				$(this).innerWidth()).hide().css('visibility', 'visible')
				.fadeIn('normal');
	} else {
		$(this).fadeOut('fast');
	}
}

var MENU_COUNTER = 1;
function loadMenu() {
	if (this.id == 'dynamicMenu') {
		$('> ul > li', this).remove();

		var ul = $('<ul></ul>');
		var t = MENU_COUNTER + 10;
		for (; MENU_COUNTER < t; MENU_COUNTER++) {
			$('> ul', this).append('<li>Item ' + MENU_COUNTER + '</li>');
		}
	}
}

function unloadMenu() {
	if (MENU_COUNTER >= 30) {
		MENU_COUNTER = 1;
	}
}

// We're passed a UL
function onHideCheckMenu() {
	return !$(this).parent().is('.LOCKED');
}

// We're passed a LI
function onClickMenu() {
	$(this).toggleClass('LOCKED');
	return true;
}

/* END MENU */
function ajax_servizi(mode) {
	if (tot_art0 == 0 && tot_ord0 == 0) {
		tot_art0 = $('#tot_art_val').val();
		tot_ord0 = $('#tot_ord_val').val();
		if ($('#app').is((':checked')))
			app = 2.5;
		if ($('#cons').is((':checked')))
			cons = 30;
	}
	if ($('#' + mode).is(':checked')) {
		$.ajax({
			type : 'POST',
			url : '/webproduct.php',
			data : mode + '=1' + '&op=upd_' + mode,
			dataType : 'html',
			success : function(msg) {
				$('#' + mode).checked = true;
				$('#' + mode + '_value').html('+ ' + msg + ' &euro;');
				switch (msg) {
				case '2,50':
					var msg_val = 2.5;
					app = msg_val;
					break;

				case '30,00':
					var msg_val = 30;
					cons = msg_val;
					break;
				}
				var tot_art = new NumberFormat(parseFloat(tot_art0) + cons
						+ app);
				tot_art.setPlaces('2', false);
				tot_art.setSeparators(true, '.', ',');
				var tot_ord = new NumberFormat(parseFloat(tot_ord0) + cons
						+ app);
				tot_ord.setPlaces('2', false);
				tot_ord.setSeparators(true, '.', ',');
				$('#tot_art').html(tot_art.toFormatted() + ' &euro;');
				$('#tot_ord').html(tot_ord.toFormatted() + ' &euro;');
			}
		});
	} else {
		$.ajax({
			type : 'POST',
			url : '/webproduct.php',
			data : mode + '=0' + '&op=upd_' + mode,
			dataType : 'html',
			success : function(msg) {
				$('#' + mode).checked = false;
				$('#' + mode + '_value').html('+ 0,00 &euro;');
				switch (msg) {
				case '2,50':
					var msg_val = 2.5;
					app = 0;
					break;

				case '30,00':
					var msg_val = 30;
					cons = 0;
					break;
				}
				// alert($('#tot_art_val').val()+' '+$('#tot_ord_val').val());
				var tot_art = new NumberFormat(parseFloat(tot_art0) + cons
						+ app);
				tot_art.setPlaces('2', false);
				tot_art.setSeparators(true, '.', ',');
				var tot_ord = new NumberFormat(parseFloat(tot_ord0) + cons
						+ app);
				tot_ord.setPlaces('2', false);
				tot_ord.setSeparators(true, '.', ',');
				$('#tot_art').html(tot_art.toFormatted() + ' &euro;');
				$('#tot_ord').html(tot_ord.toFormatted() + ' &euro;');
			}
		});
	}
}

function ajax_contrassegno() {
	if (tot_ord0 == 0)
		tot_ord0 = $('#tot_ord_val').val();
	if ($('#pagamento').val() == 3) {
		var tot_ord = new NumberFormat(parseFloat(tot_ord0)
				+ parseFloat($('#contr_val').val()));
		tot_ord.setPlaces('2', false);
		tot_ord.setSeparators(true, '.', ',');
		$('#tot_ord').html(tot_ord.toFormatted() + ' &euro;');
		$('#td_contr').show();
	} else if (tot_ord0 > 0) {
		var tot_ord = new NumberFormat(parseFloat(tot_ord0));
		tot_ord.setPlaces('2', false);
		tot_ord.setSeparators(true, '.', ',');
		$('#tot_ord').html(tot_ord.toFormatted() + ' &euro;');
		$('#td_contr').hide();
	}
}

// CATEGORIE
function ajax_add_cat(idbuono) {
	var content = '<label for="cid">Seleziona</label><select name="cid" id="cid" style="width:300px"></select>';
	$('#dialog_cat').html(content);
	$.ajax({
		type : 'POST',
		url : 'product_new.php',
		data : '&op=list_categorie',
		dataType : 'json',
		success : function(msg) {
			$('#cid')
					.ready(
							function() {
								for ( var i = 0; i < msg.rows.length; i++) {
									$('#cid').append(
											'<option value="' + msg.rows[i].cid
													+ '">' + msg.rows[i].titolo
													+ '</option>');
								}
							});
		}
	});
	$("#dialog_cat").dialog(
			{
				bgiframe : true,
				modal : true,
				width : 400,
				title : 'Aggiungi Categoria',
				close : function(event, ui) {
					$(this).html('');
					$(this).dialog('destroy');
				},
				buttons : {
					'Salva' : function() {
						$.ajax({
							type : 'POST',
							url : 'product_new.php',
							data : '&op=addcat' + '&cid=' + $('#cid').val()
									+ '&idbuono=' + idbuono,
							dataType : 'html',
							success : function(msg) {
								$("#dialog_cat").dialog('close');
								$('#grid_buoni').html(msg);
							}
						});
					},
					'Annulla' : function() {
						$(this).dialog('close');
					}
				}
			});
}

function ajax_del_cat(idbuono, cid) {
	var content = '<p>Vuoi togliere questa categoria dal buono sconto?</p>';
	$('#dialog_delcat').html(content);
	$("#dialog_delcat").dialog(
			{
				bgiframe : true,
				modal : true,
				width : 400,
				title : 'Togli Categoria',
				close : function(event, ui) {
					$(this).html('');
					$(this).dialog('destroy');
				},
				buttons : {
					'Conferma' : function() {
						$.ajax({
							type : 'POST',
							url : 'product_new.php',
							data : '&op=delcat' + '&cid=' + cid + '&idbuono='
									+ idbuono,
							dataType : 'html',
							success : function(msg) {
								$("#dialog_delcat").dialog('close');
								$('#grid_buoni').html(msg);
							}
						});
					},
					'Annulla' : function() {
						$(this).dialog('close');
					}
				}
			});
}

// ARTICOLI
function ajax_add_art(idbuono) {
	var content = '<label for="idarticolo">Seleziona</label><select name="idarticolo" id="idarticolo" style="width:300px"></select>';
	$('#dialog_art').html(content);
	$.ajax({
		type : 'POST',
		url : 'product_new.php',
		data : '&op=list_articoli',
		dataType : 'json',
		success : function(msg) {
			$('#idarticolo').ready(
					function() {
						for ( var i = 0; i < msg.rows.length; i++) {
							$('#idarticolo').append(
									'<option value="' + msg.rows[i].idarticolo
											+ '">' + msg.rows[i].titolo
											+ '</option>');
						}
					});
		}
	});
	$("#dialog_art").dialog(
			{
				bgiframe : true,
				modal : true,
				width : 400,
				title : 'Aggiungi Articolo',
				close : function(event, ui) {
					$(this).html('');
					$(this).dialog('destroy');
				},
				buttons : {
					'Salva' : function() {
						$.ajax({
							type : 'POST',
							url : 'product_new.php',
							data : '&op=addart' + '&idarticolo='
									+ $('#idarticolo').val() + '&idbuono='
									+ idbuono,
							dataType : 'html',
							success : function(msg) {
								$("#dialog_art").dialog('close');
								$('#grid_buoni').html(msg);
							}
						});
					},
					'Annulla' : function() {
						$(this).dialog('close');
					}
				}
			});
}

function ajax_del_art(idbuono, idarticolo) {
	var content = '<p>Vuoi togliere questo articolo dal buono sconto?</p>';
	$('#dialog_delart').html(content);
	$("#dialog_delart").dialog(
			{
				bgiframe : true,
				modal : true,
				width : 400,
				title : 'Togli Articolo',
				close : function(event, ui) {
					$(this).html('');
					$(this).dialog('destroy');
				},
				buttons : {
					'Conferma' : function() {
						$.ajax({
							type : 'POST',
							url : 'product_new.php',
							data : '&op=delart' + '&idarticolo=' + idarticolo
									+ '&idbuono=' + idbuono,
							dataType : 'html',
							success : function(msg) {
								$("#dialog_delart").dialog('close');
								$('#grid_buoni').html(msg);
							}
						});
					},
					'Annulla' : function() {
						$(this).dialog('close');
					}
				}
			});
}

// GRUPPI
function ajax_add_gruppo(idbuono) {
	var content = '<label for="idgruppo">Seleziona</label><select name="idgruppo" id="idgruppo" style="width:300px"></select>';
	$('#dialog_gruppo').html(content);
	$.ajax({
		type : 'POST',
		url : 'product_new.php',
		data : '&op=list_gruppi',
		dataType : 'json',
		success : function(msg) {
			$('#idgruppo').ready(
					function() {
						for ( var i = 0; i < msg.rows.length; i++) {
							$('#idgruppo').append(
									'<option value="' + msg.rows[i].idgruppo
											+ '">' + msg.rows[i].titolo
											+ '</option>');
						}
					});
		}
	});
	$("#dialog_gruppo").dialog(
			{
				bgiframe : true,
				modal : true,
				width : 400,
				title : 'Aggiungi Gruppo',
				close : function(event, ui) {
					$(this).html('');
					$(this).dialog('destroy');
				},
				buttons : {
					'Salva' : function() {
						$.ajax({
							type : 'POST',
							url : 'product_new.php',
							data : '&op=addgruppo' + '&idgruppo='
									+ $('#idgruppo').val() + '&idbuono='
									+ idbuono,
							dataType : 'html',
							success : function(msg) {
								$("#dialog_gruppo").dialog('close');
								$('#grid_buoni').html(msg);
							}
						});
					},
					'Annulla' : function() {
						$(this).dialog('close');
					}
				}
			});
}

function ajax_del_gruppo(idbuono, idgruppo) {
	var content = '<p>Vuoi togliere questo gruppo dal buono sconto?</p>';
	$('#dialog_delgruppo').html(content);
	$("#dialog_delgruppo").dialog(
			{
				bgiframe : true,
				modal : true,
				width : 400,
				title : 'Togli Gruppo',
				close : function(event, ui) {
					$(this).html('');
					$(this).dialog('destroy');
				},
				buttons : {
					'Conferma' : function() {
						$.ajax({
							type : 'POST',
							url : 'product_new.php',
							data : '&op=delgruppo' + '&idgruppo=' + idgruppo
									+ '&idbuono=' + idbuono,
							dataType : 'html',
							success : function(msg) {
								$("#dialog_delgruppo").dialog('close');
								$('#grid_buoni').html(msg);
							}
						});
					},
					'Annulla' : function() {
						$(this).dialog('close');
					}
				}
			});
}

// USERS
function ajax_add_user(idbuono) {
	var content = '<label for="idwebuser">Seleziona</label><select name="idwebuser" id="idwebuser" style="width:300px"></select>';
	$('#dialog_user').html(content);
	$.ajax({
		type : 'POST',
		url : 'product_new.php',
		data : '&op=list_users',
		dataType : 'json',
		success : function(msg) {
			$('#idwebuser').ready(
					function() {
						for ( var i = 0; i < msg.rows.length; i++) {
							$('#idwebuser').append(
									'<option value="' + msg.rows[i].idwebuser
											+ '">' + msg.rows[i].titolo + ' '
											+ msg.rows[i].mail + '</option>');
						}
					});
		}
	});
	$("#dialog_user").dialog(
			{
				bgiframe : true,
				modal : true,
				width : 400,
				title : 'Aggiungi Utente',
				close : function(event, ui) {
					$(this).html('');
					$(this).dialog('destroy');
				},
				buttons : {
					'Salva' : function() {
						$.ajax({
							type : 'POST',
							url : 'product_new.php',
							data : '&op=adduser' + '&idwebuser='
									+ $('#idwebuser').val() + '&idbuono='
									+ idbuono,
							dataType : 'html',
							success : function(msg) {
								$("#dialog_user").dialog('close');
								$('#grid_buoni').html(msg);
							}
						});
					},
					'Annulla' : function() {
						$(this).dialog('close');
					}
				}
			});
}

function ajax_del_user(idbuono, idwebuser) {
	var content = '<p>Vuoi togliere questo utente dal buono sconto?</p>';
	$('#dialog_deluser').html(content);
	$("#dialog_deluser").dialog(
			{
				bgiframe : true,
				modal : true,
				width : 400,
				title : 'Togli Utente',
				close : function(event, ui) {
					$(this).html('');
					$(this).dialog('destroy');
				},
				buttons : {
					'Conferma' : function() {
						$.ajax({
							type : 'POST',
							url : 'product_new.php',
							data : '&op=deluser' + '&idwebuser=' + idwebuser
									+ '&idbuono=' + idbuono,
							dataType : 'html',
							success : function(msg) {
								$("#dialog_deluser").dialog('close');
								$('#grid_buoni').html(msg);
							}
						});
					},
					'Annulla' : function() {
						$(this).dialog('close');
					}
				}
			});
}

// CODICE
function ajax_mod_codice(idbuono, codice, valore, percentuale, minimo, start,
		end) {
	$('#start_edit').datepicker();
	$('#end_edit').datepicker();
	$('#codice_edit').val(codice);
	$('#start_edit').val(start);
	$('#end_edit').val(end);
	$('#valore_edit').val(valore);
	$('#percentuale_edit').val(percentuale);
	$('#minimo_edit').val(minimo);
	$('#idbuono_edit').val(idbuono);
	$('#dialog_buono').dialog('open');
}

function ajax_del_buono(idbuono) {
	var content = '<p>Vuoi eliminare questo buono sconto?</p>';
	$('#dialog_delbuono').html(content);
	$("#dialog_delbuono").dialog({
		bgiframe : true,
		modal : true,
		width : 400,
		title : 'Elimina Buono',
		close : function(event, ui) {
			$(this).html('');
			$(this).dialog('destroy');
		},
		buttons : {
			'Conferma' : function() {
				$.ajax({
					type : 'POST',
					url : 'product_new.php',
					data : '&op=delbuono' + '&idbuono=' + idbuono,
					dataType : 'html',
					success : function(msg) {
						$("#dialog_delbuono").dialog('close');
						$('#grid_buoni').html(msg);
					}
				});
			},
			'Annulla' : function() {
				$(this).dialog('close');
			}
		}
	});
}

function ajax_add_codice() {
	$('#start').datepicker();
	$('#end').datepicker();
	$('#dialog_addbuono').dialog('open');
}

function ordinaThumbs(cid, orderby, idattributo, idvalore, start, end,
		produttore, elencoTab) {
	filtro = '';
	$("a[id^='filtro']").each(function() {
		name = $(this).attr('id');
		arrName = name.split("-");
		// if (parseInt($(this).attr('title')) != idvalore && arrName[1] !=
		// idattributo)
		filtro += '&' + name + '[]=' + $(this).attr('title');
	});
	if (idattributo && idvalore)
		filtro += '&filtro-' + idattributo + '[]=' + idvalore;
	if (!isNaN(start) && !isNaN(end)) {
		filtro += '&start=' + start + '&end=' + end;
	}
	$("a[id^='produttore']").each(function() {
		name = $(this).attr('id');
		arrName = name.split("-");
		filtro += '&produttore[]=' + arrName[1];
	});
	if (produttore != '')
		filtro += '&produttore[]=' + produttore;

	if (elencoTab == 1)
		filtro += '&tab=1';
	oscuraFiltri();
	$
			.ajax({
				type : 'POST',
				url : '/webproduct.php',
				data : '&op=list_thumbs' + '&cid=' + cid + '&order=' + orderby
						+ filtro,
				dataType : 'html',
				success : function(msg) {
					$('#listarticoli').html(msg);
					if (filtro == '')
						toggleFiltri();
					mostraFiltri();
					/* FB.XFBML.parse($(".btnFB")); */
					reloadSocial();
				}
			});
}

function toggleFiltri() {
	$('#cont-filtri').toggle('slow');
	$('#link_ricerca').html() == "&gt;&gt; APRI LA RICERCA AVANZATA &lt;&lt;" ? $(
			'#link_ricerca').html("<< NASCONDI RICERCA AVANZATA >>")
			: $('#link_ricerca').html(">> APRI LA RICERCA AVANZATA <<");
	$('#link_ricerca').html() == "&gt;&gt; APRI LA RICERCA AVANZATA &lt;&lt;" ? $(
			'#link_ricerca')
			.attr(
					'onClick',
					'toggleFiltri(); _gaq.push([\'_trackEvent\', \'Filtri\', \'Utilizzo\', \'Apertura\']);')
			: $('#link_ricerca')
					.attr(
							'onClick',
							'toggleFiltri(); _gaq.push([\'_trackEvent\', \'Filtri\', \'Utilizzo\', \'Chiusura\']);');
}

function pulisciFiltri(cid) {
	$.ajax({
		type : 'POST',
		url : '/webproduct.php',
		data : '&op=list_thumbs' + '&cid=' + cid,
		dataType : 'html',
		success : function(msg) {
			$('#listarticoli').html(msg);
		}
	});
}

function delFiltroProd() {
	$('a[id^="produttore"]').each(function(index, value) {
		if (index == 0)
			$(this).attr('id', '');
	});
}

function oscuraFiltri() {
	$('#oscura-filtri').height($('.box-filtri-ricerca').height());
	$('#oscura-filtri').width($('.box-filtri-ricerca').width());
	$('#oscura-filtri').show();
}

function mostraFiltri() {
	$('#oscura-filtri').hide();
}

function ordinaArticoli(cid, min, max, page, orderby) {
	$.ajax({
		type : 'POST',
		url : '/webproduct.php',
		data : '&op=list_thumbs' + '&cid=' + cid + '&min=' + min + '&max='
				+ max + '&page=' + page + '&order=' + orderby,
		dataType : 'html',
		success : function(msg) {
			$('#listarticoli').html(msg);
		}
	});
}

function addArticolo(idarticolo) {
	$.ajax({
		type : 'POST',
		url : '/webproduct.php',
		data : '&op=ajax_addtoCart' + '&quantity=1' + '&idarticolo='
				+ idarticolo,
		dataType : 'text',
		success : function(msg) {
			if (msg == '' || msg == 'undefined')
				location.href = '/webproduct.php?op=viewcart&bk=cat';
			else
				alert(msg);
		}
	});
}

function getMenu(cid1, cid2) {
	var content = $('#cat' + cid2).html();
	if (content == "") {
		$
				.ajax({
					type : 'POST',
					url : '/webproduct.php',
					data : '&op=ajax_getMenu&cid1=' + cid1 + '&cid2=' + cid2,
					dataType : 'json',
					success : function(msg) {
						if (msg.count > 0) {
							var row = msg.record;
							var html = '';
							for ( var i = 0; i < row.lenght; i++) {
								html += '<li id="'
										+ msg.id
										+ '" style="white-space: nowrap;"><a href="http://www.bsvillage.com/'
										+ msg.folder + '/' + cid2 + '-' + cid1
										+ '-' + row[i].cid + '.html">'
										+ row[i].titolo + '(' + row[i].num
										+ ')</a></li>';
							}
							$('#cat' + cid2).append(html);
							// $('ul.jd_menu_vertical').jdMenu({onShow:
							// loadMenu, onHide: unloadMenu, offset: 1,
							// onAnimate: onAnimate});
						}
					}
				});
	}
}

function saveAttributo(cid) {
	$
			.ajax({
				type : 'POST',
				url : 'product_new.php',
				data : 'op=saveAttr' + '&cid=' + cid + '&nome='
						+ $("#nome_attr").val(),
				dataType : 'json',
				success : function(msg) {
					if (msg.length > 0) {
						$('#list_attributi').html('');
						list = '<tr><td colspan=2>&nbsp;</td></tr><td colspan=2 align=center class=error>ATTRIBUTI DI RICERCA</td>';
						for (i = 0; i < msg.length; i++) {
							list += '<tr>'
									+ '<td>'
									+ msg[i].nome
									+ '</td>'
									+ '<td align="center"><a href="javascript:;" onClick="openAttributo('
									+ msg[i].id
									+ ','
									+ cid
									+ ');"><img src="imgs/ico_edit.gif" border="0"></a>&nbsp;'
									+ '<a href="javascript:;" onClick="javascript: if(confirm(\'Confermi di eliminare questo attributo?\')) delAttributo('
									+ msg[i].id
									+ ','
									+ cid
									+ ');"><img src="imgs/ico_delete.gif" border="0"></a>'
									+ '</td>' + '</tr>';
						}
						$('#list_attributi').html(list);
					}
				}
			});
}

function openAttributo(id, cid) {
	var strurl = location.href;
	$
			.ajax({
				type : 'POST',
				url : 'product_new.php',
				data : '&op=getAttributo' + '&id=' + id + '&cid=' + cid,
				dataType : 'json',
				success : function(msg) {
					$("#nome_var_edit").val(msg[0].nome);
					$("#mod_attributo")
							.dialog(
									{
										bgiframe : true,
										modal : true,
										width : 400,
										title : 'Modifica Attributo',
										close : function(event, ui) {
											$(this).dialog('close');
										},
										buttons : {
											'Salva' : function() {
												$
														.ajax({
															type : 'POST',
															url : 'product_new.php',
															data : 'op=updateAttr'
																	+ '&id='
																	+ id
																	+ '&cid='
																	+ cid
																	+ '&nome='
																	+ $(
																			"#nome_var_edit")
																			.val(),
															dataType : 'json',
															success : function(
																	msg) {
																list = '<tr><td colspan=2>&nbsp;</td></tr><td colspan=2 align=center class=error>ATTRIBUTI DI RICERCA</td>';
																if (msg.length > 0) {
																	$(
																			'#list_attributi')
																			.html(
																					'');
																	for (i = 0; i < msg.length; i++) {
																		list += '<tr>'
																				+ '<td>'
																				+ msg[i].nome
																				+ '</td>'
																				+ '<td align="center"><a href="javascript:;" onClick="openAttributo('
																				+ msg[i].id
																				+ ','
																				+ cid
																				+ ');"><img src="imgs/ico_edit.gif" border="0"></a>&nbsp;'
																				+ '<a href="javascript:;" onClick="javascript: if(confirm(\'Confermi di eliminare questo attributo?\')) delAttributo('
																				+ msg[i].id
																				+ ','
																				+ cid
																				+ ');"><img src="imgs/ico_delete.gif" border="0"></a>'
																				+ '</td>'
																				+ '</tr>';
																	}
																	$(
																			'#list_attributi')
																			.html(
																					list);
																}
																$(
																		"#mod_attributo")
																		.dialog(
																				'close');
															}
														});
											},
											'Annulla' : function() {
												$("#mod_attributo").dialog(
														'close');
											}
										}
									});
				}
			});
}

function delAttributo(id, cid) {
	$
			.ajax({
				type : 'POST',
				url : 'product_new.php',
				data : '&op=delAttributo' + '&id=' + id + '&cid=' + cid,
				dataType : 'json',
				success : function(msg) {
					list = '<tr><td colspan=2>&nbsp;</td></tr><td colspan=2 align=center class=error>ATTRIBUTI DI RICERCA</td>';
					if (msg.length > 0) {
						$('#list_attributi').html('');
						for (i = 0; i < msg.length; i++) {
							list += '<tr>'
									+ '<td>'
									+ msg[i].nome
									+ '</td>'
									+ '<td align="center"><a href="javascript:;" onClick="openVariante('
									+ msg[i].id
									+ ','
									+ cid
									+ ');"><img src="imgs/ico_edit.gif" border="0"></a>&nbsp;'
									+ '<a href="javascript:;" onClick="javascript: if(confirm(\'Confermi di eliminare questo attributo?\')) delVariante('
									+ msg[i].id
									+ ','
									+ cid
									+ ');"><img src="imgs/ico_delete.gif" border="0"></a>'
									+ '</td>' + '</tr>';
						}
					}
					$('#list_attributi').html(list);
				}
			});
}

/* VARIANTI */
function addVariante() {
	$('#add_attr').toggle();
}

function saveVariante() {
	var nome = $('#nome_attr').val();
	var cids = $('#cids').val();
	if (nome) {
		var json = '{"nome":"' + nome + '","cids":[' + cids.join() + ']}';
		$.ajax({
			type : 'GET',
			url : 'product_new.php',
			data : 'op=saveVariante&json=' + json,
			dataType : 'json',
			success : function(msg) {
				$('#list_attr').html(msg.json);
			}
		});
	} else {
		alert('Devi inserire il nome della variante.');
	}
}

function editVariante(idattributo) {
	$
			.ajax({
				type : 'POST',
				url : 'product_new.php',
				data : 'op=getVariante&idattributo=' + idattributo,
				dataType : 'json',
				success : function(msg) {
					// msg = msg[0];
					var html = '<p>Nome Variante: <input type="text" id="nome_var_edit" size="30" value="'
							+ stripslashes(msg.nome)
							+ '"></p>'
							+ '<p>Seleziona le categorie</p>'
							+ '<p><select id="cids_edit" name="cids" multiple="multiple" size="10">';
					for ( var i = 0; i < msg.varianti.length; i++) {
						html += '<option value="' + msg.varianti[i].cid + '"';
						if (in_array(msg.varianti[i].titolo, msg.cids))
							html += ' selected';
						html += '>' + msg.varianti[i].titolo + '</option>';
					}
					html += '</select></p>';
					html += '<p><input type="button" value="SALVA" onClick="updVariante('
							+ idattributo + ');"></p>';
					$("#mod_attr").html(html);
					$("#mod_attr").dialog({
						bgiframe : true,
						modal : true,
						width : 700,
						title : 'Modifica Variante',
						close : function(event, ui) {
							$(this).html('');
							$(this).dialog('destroy');
						},
						buttons : {
							'Chiudi' : function() {
								$.ajax({
									type : 'POST',
									url : 'product_new.php',
									data : 'op=getListAttributi',
									dataType : 'html',
									success : function(msg) {
										$('#list_attr').html(msg);
									}
								});
								$(this).dialog('close');
							}
						}
					});
				}
			});
}

function updVariante(idattributo) {
	var nome = $('#nome_var_edit').val();
	var cids = $('#cids_edit').val();
	if (nome) {
		var json = '{"idattributo":' + idattributo + ',"nome":"' + nome
				+ '","cids":[' + cids.join() + ']}';
		$.ajax({
			type : 'GET',
			url : 'product_new.php',
			data : 'op=updVariante&json=' + json,
			dataType : 'json',
			success : function(msg) {
				alert('Variante modificata con successo!');
				$('#list_attr').html(msg.json);
				$("#mod_attr").dialog('close');
			}
		});
	} else {
		alert('Devi inserire il nome della variante.');
	}
}

function delVariante(idattributo, cid) {
	if (confirm('Sei sicuro di voler eliminare questa variante? Procedere?')) {
		$.ajax({
			type : 'POST',
			url : 'product_new.php',
			data : 'op=delVariante&idattributo=' + idattributo,
			dataType : 'html',
			success : function(msg) {
				$('#list_attr').html(msg);
			}
		});
	}
}

function getValori(idattributo) {
	$.ajax({
				type : 'POST',
				url : 'product_new.php',
				data : 'op=getValori&idattributo=' + idattributo,
				dataType : 'json',
				success : function(msg) {
					var html = '<p>Inserisci un nuovo valore(es. range [5-10]m):<input type="text" id="new_val" size="20"></p>'
							+ '<p>Inserisci un range di valori: da <input type="text" id="start_val" size="2"> a <input type="text" id="end_val" size="2"> passo <input type="text" id="passo_val" size="2"></p>'
							+ '<input type="button" value="SALVA" onClick="saveValore('
							+ idattributo
							+ ')">'
							+ '<div id="elenco_val" style="height:400px;overflow:auto;">'
							+ msg.json + '</div>';
					$('#list_val_attr').html(html);
					$("#list_val_attr").dialog({
						bgiframe : true,
						modal : true,
						width : 500,
						title : 'Variante: ' + stripslashes(msg.attributo),
						close : function(event, ui) {
							$(this).html('');
							$(this).dialog('destroy');
						},
						buttons : {
							'Chiudi' : function() {
								$(this).dialog('close');
							}
						}
					});
				}
			});
}

function saveValore(idattributo) {
	var valore = $('#new_val').val();
	var start, end, passo;
	$('#start_val').val() != "" ? start = $('#start_val').val() : start = 0;
	$('#end_val').val() != "" ? end = $('#end_val').val() : end = 0;
	$('#passo_val').val() != "" ? passo = $('#passo_val').val() : passo = 0;
	var json = '{"idattributo":' + idattributo + ',"valore":"' + valore
			+ '","start":' + start + ',"end":' + end + ',"passo":' + passo
			+ '}';
	$.ajax({
		type : 'GET',
		url : 'product_new.php',
		data : 'op=saveValore&json=' + json,
		dataType : 'json',
		success : function(msg) {
			$('#elenco_val').html(msg.json);
		}
	});
}

function editValore(idvalore, idattributo) {
	var html = '<input type="text" id="value_val_' + idvalore
			+ '" size="20" value="' + $('#edit_val_' + idvalore).html()
			+ '">&nbsp;<input type="button" value="SALVA" onClick="updValore('
			+ idvalore + ',' + idattributo + ')">';
	var cont = $('#edit_val_' + idvalore).html();
	if (cont.indexOf('<input') >= 0) {
	} else
		$('#edit_val_' + idvalore).html(html);
}

function updValore(idvalore, idattributo) {
	var new_valore = $('#value_val_' + idvalore).val();
	if (new_valore) {
		var json = '{"idattributo":' + idattributo + ',"valore":"' + new_valore
				+ '","idvalore":' + idvalore + '}';
		$.ajax({
			type : 'GET',
			url : 'product_new.php',
			data : 'op=updValore&json=' + json,
			dataType : 'json',
			success : function(msg) {
				alert('Valore modificato con successo!');
				$('#elenco_val').html(msg.json);
			}
		});
	} else {
		alert('Devi inserire il valore per l\'attributo.');
	}
}

function delValore(idvalore, idattributo) {
	if (confirm('Sei sicuro di voler eliminare questo valore? Procedere?')) {
		$.ajax({
			type : 'POST',
			url : 'product_new.php',
			data : 'op=delValore&idvalore=' + idvalore + '&idattributo='
					+ idattributo,
			dataType : 'html',
			success : function(msg) {
				$('#elenco_val').html(msg);
			}
		});
	}
}

function addSconto() {
	$('#add_sconto').toggle();
	if ($('#mod_attr').is(':visible'))
		$('#mod_attr').hide();
}

function saveSconto() {
	var titolo = $('#titolo').val();
	var sconto = $('#sconto').val();
	var dal = $('#dal').val();
	var al = $('#al').val();
	var spedizione = $('#spedizione').val();
	var fornitore = $('#fornitore').val();
	switch ($('input[type="radio"][name="tipo"]:checked').val()) {
	case "1":
		var cids = $('#cids').val();
		var json = '{"titolo":"' + titolo + '","sconto":"' + sconto
				+ '","dal":"' + dal + '","al":"' + al + '","spedizione":'
				+ spedizione + ',"cids":[' + cids.join()
				+ '],"idarticolo":0,"fornitore":""}';
		break;

	case "2":
		var idarticolo = $('#idarticolo').val();
		var json = '{"titolo":"' + titolo + '","sconto":"' + sconto
				+ '","dal":"' + dal + '","al":"' + al + '","spedizione":'
				+ spedizione + ',"idarticolo":[' + idarticolo.join()
				+ '],"cids":0,"fornitore":""}';
		break;

	case "3":
		var json = '{"titolo":"' + titolo + '","sconto":"' + sconto
				+ '","dal":"' + dal + '","al":"' + al + '","spedizione":'
				+ spedizione + ',"idarticolo":[],"cids":0,"fornitore":"'
				+ fornitore + '"}';
		break;
	}

	if (titolo && sconto) {
		$.ajax({
			type : 'GET',
			url : 'product_new.php',
			data : 'op=saveSconto&json=' + json,
			dataType : 'json',
			success : function(msg) {
				$('#list_attr').html(msg.json);
				$('#add_sconto').toggle();
			}
		});
	} else {
		alert('Devi inserire il titolo e il valore dello sconto.');
	}
}

function editSconto(id) {
	$
			.ajax({
				type : 'POST',
				url : 'product_new.php',
				data : 'op=getSconto&id=' + id,
				dataType : 'json',
				success : function(msg) {
					$('#titolo_edit').val(msg.titolo);
					$('#sconto_edit').val(msg.sconto);
					$('#dal_edit').val(msg.dal);
					$('#al_edit').val(msg.al);
					$('#spedizione_edit').val(msg.tipo);
					$('#fornitore_edit').val(msg.fornitore);
					$('#idsconto').val(id);
					$('#cids_edit option').attr('selected', '');
					if (msg.cids.length > 0) {
						$('input[type="radio"][name="tipo_edit"][value="1"]')
								.click();
						$('#cids_edit option').each(function(i) {
							var cid = parseInt($(this).val());
							if (msg.cids.indexOf(cid) >= 0) {
								$(this).attr('selected', 'selected');
							}
						});
					} else if (msg.idarticolo.length > 0) {
						$('input[type="radio"][name="tipo_edit"][value="2"]')
								.click();
						$('#cid_edit')
								.change(
										function() {
											if ($('#cid_edit').val() != "") {
												$
														.ajax({
															type : 'POST',
															url : 'product_new.php',
															data : 'op=getListArticoli&cid='
																	+ $(
																			'#cid_edit')
																			.val(),
															dataType : 'json',
															success : function(
																	msg2) {
																if (msg2.rows.length > 0) {
																	$(
																			'#idarticolo_edit')
																			.html(
																					'');
																	for ( var i = 0; i < msg2.rows.length; i++) {
																		var idarticolo = parseInt(msg2.rows[i].idarticolo);
																		var html = '<option value="'
																				+ msg2.rows[i].idarticolo
																				+ '"';
																		if (msg.idarticolo
																				.indexOf(idarticolo) >= 0)
																			html += ' selected';
																		html += '>'
																				+ msg2.rows[i].titolo
																				+ '</option>'
																		$(
																				'#idarticolo_edit')
																				.append(
																						html);
																	}
																}
															}
														});
											}
										});
						$('#cid_edit').val(msg.cid);
						$('#cid_edit').change();
					} else {
						$('#cid_edit')
								.change(
										function() {
											if ($('#cid_edit').val() != "") {
												$
														.ajax({
															type : 'POST',
															url : 'product_new.php',
															data : 'op=getListArticoli&cid='
																	+ $(
																			'#cid_edit')
																			.val(),
															dataType : 'json',
															success : function(
																	msg2) {
																if (msg2.rows.length > 0) {
																	$(
																			'#idarticolo_edit')
																			.html(
																					'');
																	for ( var i = 0; i < msg2.rows.length; i++) {
																		var idarticolo = parseInt(msg2.rows[i].idarticolo);
																		var html = '<option value="'
																				+ msg2.rows[i].idarticolo
																				+ '"';
																		html += '>'
																				+ msg2.rows[i].titolo
																				+ '</option>'
																		$(
																				'#idarticolo_edit')
																				.append(
																						html);
																	}
																}
															}
														});
											}
										});
						$('input[type="radio"][name="tipo_edit"][value="3"]')
								.click();
					}
					$('#mod_attr').show();
				}
			});
}

function updateSconto() {
	var titolo = $('#titolo_edit').val();
	var sconto = $('#sconto_edit').val();
	var dal = $('#dal_edit').val();
	var al = $('#al_edit').val();
	var id = $('#idsconto').val();
	var tipo = $('#spedizione_edit').val();
	var fornitore = $('#fornitore_edit').val();
	switch ($('input[type="radio"][name="tipo_edit"]:checked').val()) {
	case "1":
		var cids = $('#cids_edit').val();
		var json = '{"id":' + id + ',"titolo":"' + titolo + '","sconto":"'
				+ sconto + '","dal":"' + dal + '","al":"' + al + '","tipo":'
				+ tipo + ',"cids":[' + cids.join()
				+ '],"idarticolo":0,"fornitore":""}';
		break;

	case "2":
		var idarticolo = $('#idarticolo_edit').val();
		var json = '{"id":' + id + ',"titolo":"' + titolo + '","sconto":"'
				+ sconto + '","dal":"' + dal + '","al":"' + al + '","tipo":'
				+ tipo + ',"idarticolo":[' + idarticolo.join()
				+ '],"cids":0,"fornitore":""}';
		break;

	case "3":
		var json = '{"id":' + id + ',"titolo":"' + titolo + '","sconto":"'
				+ sconto + '","dal":"' + dal + '","al":"' + al + '","tipo":'
				+ tipo + ',"idarticolo":[],"cids":0,"fornitore":"' + fornitore
				+ '"}';
		break;
	}

	if (titolo && sconto) {
		$.ajax({
			type : 'GET',
			url : 'product_new.php',
			data : 'op=updateSconto&json=' + json,
			dataType : 'json',
			success : function(msg) {
				$('#list_attr').html(msg.json);
				$('#mod_attr').hide();
			}
		});
	} else {
		alert('Devi inserire il titolo e il valore dello sconto.');
	}
}

function delSconto(id) {
	if (confirm('Sei sicuro di voler eliminare questo sconto? Proseguire?')) {
		$.ajax({
			type : 'POST',
			url : 'product_new.php',
			data : 'op=deleteSconto&id=' + id,
			dataType : 'json',
			success : function(msg) {
				$('#list_attr').html(msg.json);
			}
		});
	}
}

function selTipo(radio) {
	switch ($(radio).val()) {
	case "1":
		$('#cont-cat').show();
		$('#cont-art').hide();
		break;

	case "2":
		$('#cont-art').show();
		$('#cont-cat').hide();
		break;

	case "3":
		$('#cont-art').hide();
		$('#cont-cat').hide();
		break;
	}
}

function selTipoEdit(radio) {
	switch ($(radio).val()) {
	case "1":
		$('#cont-cat_edit').show();
		$('#cont-art_edit').hide();
		break;

	case "2":
		$('#cont-art_edit').show();
		$('#cont-cat_edit').hide();
		break;

	case "3":
		$('#cont-art_edit').hide();
		$('#cont-cat_edit').hide();
		break;
	}
}

function listArticoli(update) {
	if (update) {
		if ($('#cid_edit').val() != "") {
			$.ajax({
				type : 'POST',
				url : 'product_new.php',
				data : 'op=getListArticoli&cid=' + $('#cid_edit').val(),
				dataType : 'json',
				success : function(msg) {
					if (msg.rows.length > 0) {
						$('#idarticolo_edit').html('');
						for ( var i = 0; i < msg.rows.length; i++) {
							$('#idarticolo_edit').append(
									'<option value="' + msg.rows[i].idarticolo
											+ '">' + msg.rows[i].titolo
											+ '</option>');
						}
					}
				}
			});
		}
	} else {
		if ($('#cid').val() != "") {
			$.ajax({
				type : 'POST',
				url : 'product_new.php',
				data : 'op=getListArticoli&cid=' + $('#cid').val(),
				dataType : 'json',
				success : function(msg) {
					if (msg.rows.length > 0) {
						$('#idarticolo').html('');
						for ( var i = 0; i < msg.rows.length; i++) {
							$('#idarticolo').append(
									'<option value="' + msg.rows[i].idarticolo
											+ '">' + msg.rows[i].titolo
											+ '</option>');
						}
					}
				}
			});
		}
	}

}

function reloadSocial() {
	$('.facebox-fblike').each(function() {
		$('.btnFB').each(function() {
			FB.XFBML.parse($(this).get(0));
		});
	});
}

/* FORNITORE */
function modFornitore(id) {
	$.ajax({
		type : 'POST',
		url : 'product_new.php',
		data : 'op=getFornitore&id=' + id,
		dataType : 'json',
		success : function(msg) {
			$('#fornitore_mod').val(msg.fornitore);
			$('#upd-fornitore').unbind('click');
			$('#upd-fornitore').bind(
					'click',
					function() {
						var dati = 'op=updFornitore&id=' + id + '&fornitore='
								+ $('#fornitore_mod').val();
						$.ajax({
							type : 'POST',
							url : 'product_new.php',
							data : dati,
							dataType : 'json',
							success : function(msg) {
								location.reload();
							}
						});
					});
			$('#mod-fornitore').show('slow');
		}
	});
}

function delFornitore(id) {
	$.ajax({
		type : 'POST',
		url : 'product_new.php',
		data : 'op=delFornitore&id=' + id,
		dataType : 'json',
		success : function(msg) {
			location.reload();
		}
	});
}

function salvaFornitore() {
	var dati = 'op=salvaFornitore&fornitore=' + $('#fornitore').val();
	$.ajax({
		type : 'POST',
		url : 'product_new.php',
		data : dati,
		dataType : 'json',
		success : function(msg) {
			location.reload();
		}
	});
}

/* CODICI */
function modCodici(idarticolo, idfornitore) {
	$.ajax({
		type : 'POST',
		url : 'product_new.php',
		data : 'op=getCodice&idarticolo=' + idarticolo + '&idfornitore='
				+ idfornitore,
		dataType : 'json',
		success : function(msg) {
			$('#costo_mod').val(msg[0].costo);
			$('#codice_mod').val(msg[0].codice);
			$('select[name="idfornitore_mod"]').val(msg[0].idfornitore);
			$('#upd-codice').unbind('click');
			$('#upd-codice').bind(
					'click',
					function() {
						var dati = 'op=updCodice&idarticolo=' + idarticolo
								+ '&idfornitore='
								+ $('select[name="idfornitore_mod"]').val()
								+ '&costo=' + $('#costo_mod').val()
								+ '&codice=' + $('#codice_mod').val()
								+ '&idfornitore_old=' + idfornitore;
						$.ajax({
							type : 'POST',
							url : 'product_new.php',
							data : dati,
							dataType : 'json',
							success : function(msg) {
								location.reload();
							}
						});
					});
			$('#mod-codice').fadeIn();
		}
	});
}

function delCodice(idarticolo, idfornitore) {
	$.ajax({
		type : 'POST',
		url : 'product_new.php',
		data : 'op=delCodice&idarticolo=' + idarticolo + '&idfornitore='
				+ idfornitore,
		dataType : 'json',
		success : function(msg) {
			location.reload();
		}
	});
}

function setPrincipale(idarticolo, idfornitore) {
	$.ajax({
		type : 'POST',
		url : 'product_new.php',
		data : 'op=setPrincipale&idarticolo=' + idarticolo + '&idfornitore='
				+ idfornitore,
		dataType : 'json',
		success : function(msg) {
			location.reload();
		}
	});
}

function salvaCodice(idarticolo) {
	var dati = 'op=salvaCodice&idfornitore='
			+ $('select[name="idfornitore"]').val() + '&idarticolo='
			+ idarticolo + '&costo=' + $('#costo').val() + '&codice='
			+ $('#codice').val() + '&fornitore_add='
			+ $('#fornitore_add').val();
	$.ajax({
		type : 'POST',
		url : 'product_new.php',
		data : dati,
		dataType : 'json',
		success : function(msg) {
			location.reload();
		}
	});
}

/* VARIANTI ARTICOLO */
function modVarianteArticolo(id) {
	$.ajax({
		type : 'POST',
		url : 'product_new.php',
		data : 'op=getVarianteArticolo&id=' + id,
		dataType : 'json',
		success : function(msg) {
			$('#nome_var_mod').val(msg[0].nome);
			$('#codice_var_mod').val(msg[0].codice);
			$('#descrizione_var_mod').val(msg[0].descrizione);
			$('#upd-codice').unbind('click');
			$('#id_var').val(msg[0].id);
			$('#mod-variante-articolo').fadeIn();
		}
	});
}

function delVarianteArticolo(id) {
	$.ajax({
		type : 'POST',
		url : 'product_new.php',
		data : 'op=delVarianteArticolo&id=' + id,
		dataType : 'json',
		success : function(msg) {
			location.reload();
		}
	});
}
