// JavaScript Document
function ListarProductos(pag){
	if(pag==null) pag="";
	
	var datos="pagajax=paginas/tienda/tabla_productos.php";
	datos+="&pag="+pag;
	datos+="&"+$('#frmFiltro').serialize();
	//MostrarAviso(datos);
	$.ajax({
		url: "paginas/ajax.php",
		data: datos,      
		global: false,
      	type: "POST",
		async: false,
		success: function(html){ $('#visor').html(html); }
	})	
}

function FichaProducto(id){
	if(id==null) id="";
	var data="&id="+id;
	CargarSeccion("tienda","ficha_producto", data);	
}

function HabilitarStock(valor){
	$('#stockminimo').attr('disabled', valor);
	$('#mas_stock').attr('disabled', valor);
	$('#btnmas_stock').attr('disabled', valor);
	$('#avisostockminimo').attr('disabled', valor);
}

function AccionesProductos(accion, id){
	var datos="pagajax=paginas/tienda/ajax_productos.php";
	datos+="&accion="+accion;
	datos+="&id="+id;
	
	if(accion=="almacenar"){
		datos+="&"+$('#frmProducto').serialize();
	}
	
	var html=$.ajax({
		url: "paginas/ajax.php",
		data: datos,      
		global: false,
      	type: "POST",
		async: false,
		success: function(){}
	}).responseText;
	
	return(html);	
}

function AlmacenarProducto(id){
	if(id==null) id="";
	var html=AccionesProductos("almacenar", id);
	if(html!=""){
		MostrarAviso(html, "Almacenar Producto");	
	}else{
		CargarSeccion("tienda", "visor_productos");	
	}
}

function IncrementarStock(id, cantidad){
	if(isNaN(cantidad)){
		MostrarAviso('La cantidad introducida es erronea. Debe ser un n\xFAmero','Incrementar Stock Producto');
	}else{
		var datos="pagajax=paginas/tienda/ajax_productos.php";
		datos+="&accion=incrementarstock";
		datos+="&id="+id;
		datos+="&cantidad="+parseInt(cantidad);
			
		$.ajax({
			url: "paginas/ajax.php",
			data: datos,      
			global: false,
			type: "POST",
			async: false,
			success: function(html){
				$('#stockactual').html(html);
				$('#mas_stock').val(0);
			}
		});
	}
}

function CambiarPublicar(id){
	if(id!=null && id!=""){
		var html=AccionesProductos('cambiarpublicar', id);
		if(html!=""){
			MostrarAviso(html, "Cambiar Publicar");	
		}else{
			ListarProductos();	
		}
	}
}

function CambiarPortada(id){
	if(id!=null && id!=""){
		var html=AccionesProductos('cambiarportada', id);
		if(html!=""){
			MostrarAviso(html, "Cambiar Portada");	
		}else{
			ListarProductos();	
		}
	}
}

function CambiarDestacado(id){
	if(id!=null && id!=""){
		var html=AccionesProductos('cambiardestacado', id);
		if(html!=""){
			MostrarAviso(html, "Cambiar Destacado");	
		}else{
			ListarProductos();	
		}
	}
}


function CambiarOferta(id){
	if(id!=null && id!=""){
		var html=AccionesProductos('cambiaroferta', id);
		if(html!=""){
			MostrarAviso(html, "Cambiar Oferta");	
		}else{
			ListarProductos();	
		}
	}
}


function CambiarNovedad(id){
	if(id!=null && id!=""){
		var html=AccionesProductos('cambiarnovedad', id);
		if(html!=""){
			MostrarAviso(html, "Cambiar Novedad");	
		}else{
			ListarProductos();	
		}
	}
}

