var tot_art0 = 0;
var tot_ord0 = 0;
var app = 0;
var cons = 0;
var FOLDERNEW = 'ita';

function ajax_edit(el)  {
	var old = $('#'+el).html();
	if(!isNaN(old))  {
		$('#'+el).html('<input type="text" size="5" id="mod" value="'+old+'" ' +
				'onBlur="ajax_upd(\''+el+'\',\''+old+'\')">');
		$('#'+el).ready(function(){
			$('#mod').ready(function(){
				$('#mod').focus();
			});
		});
	}
}

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());					  
					}
		});
	} 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+'</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)  {
	var content = '<p><label for="codice">Codice</label><input type="text" name="codice" id="codice" size="20" value="'+codice+'"><p>' +
			'<p><label for="valore">Valore</label><input type="text" name="valore" id="valore" size="20" value="'+valore+'"> &euro;<p>';
	$('#dialog_buono').html(content);
	$("#dialog_buono").dialog({
			bgiframe: true,
			modal: true,
			width: 400,
			title: 'Modifica Codice',
			close: function(event, ui){
					$(this).html('');
					$(this).dialog('destroy');
			},
			buttons: {
				'Salva':function(){
					$.ajax({
						type:'POST',
						url:'product_new.php',
						data: '&op=updbuono'+
						'&codice='+$('#codice').val()+
						'&valore='+$('#valore').val()+
						'&idbuono='+idbuono,
						dataType:'html',
						success: function(msg){
							 $("#dialog_buono").dialog('close');
							 $('#grid_buoni').html(msg); 
						}
					});
				},
				'Annulla': function() {
					$(this).dialog('close');
							}
			}
	});
}

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()  {
	var content = '<p><label for="codice">Codice</label><input type="text" name="codice" id="codice" size="20"></p>' +
			'<p><label for="valore">Valore</label><input type="text" name="valore" id="valore" size="20"> &euro;</p>';
	$('#dialog_addbuono').html(content);
	$("#dialog_addbuono").dialog({
			bgiframe: true,
			modal: true,
			width: 400,
			title: 'Nuovo Codice',
			close: function(event, ui){
					$(this).html('');
					$(this).dialog('destroy');
			},
			buttons: {
				'Salva':function(){
					$.ajax({
						type:'POST',
						url:'product_new.php',
						data: '&op=addbuono'+
						'&codice='+$('#codice').val()+
						'&valore='+$('#valore').val(),
						dataType:'html',
						success: function(msg){
							 $("#dialog_addbuono").dialog('close');
							 $('#grid_buoni').html(msg); 
						}
					});
				},
				'Annulla': function() {
					$(this).dialog('close');
							}
			}
	});
}

function ordinaThumbs(cid,orderby)  {
	$.ajax({
		type:'POST',
		url:'webproduct.php',
		data: '&op=list_thumbs'+
		'&cid='+cid+
		'&order='+orderby,
		dataType:'html',
		success: function(msg){
				$('#listarticoli').html(msg); 
		}
	});
}

function ordinaArticoli(cid,orderby)  {
	$.ajax({
		type:'POST',
		url:'webproduct.php',
		data: '&op=list_articoli'+
		'&cid='+cid+
		'&order='+orderby,
		dataType:'html',
		success: function(msg){
				$('#listarticoli').html(msg); 
		}
	});
}

function addArticolo(idarticolo)  {
	$.ajax({
		type:'POST',
		url: 'http://www.bsvillage.com/'+FOLDERNEW+'/webproduct.php',
		data: '&op=ajax_addtoCart'+
		'&quantity=1'+
		'&idarticolo='+idarticolo,
		dataType:'text',
		success: function(msg){
			if(msg=='' || msg=='undefined')
				location.href = 'http://www.bsvillage.com/'+FOLDERNEW+'/webproduct.php?op=viewcart';
			else
			  alert(msg);
		}
	});
}