function compra_ayuda(explicacion)
{
	show_alert({titulo: MSG_INFORMACION, descripcion: explicacion, error: 0});
}

function compra_modificar_item(id_item, type_item, quantity, id_model)
{
	// Es un diseño pero no tiene modelo asociado
	if (type_item == 1 && !id_model) {
		mudis_show_related_models(id_item, 0, 1);
	} else {
		$.ajax({
			type: "POST",
			url: WEB_PATH + "app/ajax/compra_modificar_item.php",
			data: {id_item: id_item, type_item: type_item, quantity: quantity, id_model: id_model},
			async: false,
			success: function(response)
			{
				var v_response = response.split("|");
				if (v_response[0] == 0) {
					mudis_login_callback = "compra_modificar_item("+id_item+","+type_item+","+quantity+","+id_model+")";
					mudis_login();
				} else
				if (v_response[0] == 1) {
					window.location.href = v_response[1];
				}
			}
		});
	}

}

function compra_modificar_item_ajax(id_item, type_item, quantity, id_model)
{
	// Viene del combo de cantidad
	if (quantity == -1) quantity = $('#select_item_'+id_item+' option:selected').val();
	$.ajax({
		type: "POST",
		url: WEB_PATH + "app/ajax/compra_modificar_item.php",
		data: {id_item: id_item, type_item: type_item, quantity: quantity, id_model: id_model},
		async: false,
		success: function(response)
		{
			var v_response = response.split("|");
			$('#total_purchase').html(v_response[2]);
			$('#total_item_'+id_item).html(v_response[3]);
			$('#header_total_items').attr('value',v_response[4]);
			$('#header_total_items').html(v_response[4]);
			if (quantity == 0) $('#div_item_'+id_item).fadeOut();
		}
	});
}


function compra_set_active_tab(tab_name)
{
	$('.ui-state-default.first').removeClass('ui-tabs-selected');
	$('.ui-state-default.first').removeClass('ui-state-active');
	$('.ui-state-default.second').removeClass('ui-tabs-selected');
	$('.ui-state-default.second').removeClass('ui-state-active');
	$('.ui-state-default.third').removeClass('ui-tabs-selected');
	$('.ui-state-default.third').removeClass('ui-state-active');
	$('.ui-state-default.'+tab_name).addClass('ui-tabs-selected');
	$('.ui-state-default.'+tab_name).addClass('ui-state-active');
}

// IR A METODOS DE PAGO
function compra_paso_2()
{
	var cantidad_items = Number($('#header_total_items').attr('value'));
	if (cantidad_items > 0) {
		$.ajax({
			type: "POST",
			url: WEB_PATH + "app/ajax/compra_paso2_div.php",
			data: {},
			async: false,
			success: function(response)
			{
				compra_set_active_tab('second');
				$('.ui-tabs-panel').html(response);				
			}
		});
	} else {
		show_alert({titulo: MSG_ERROR, descripcion: MSG_COMPRA_NO_ITEMS, error: 1});
	}
}

// IR A PAGINA DE CONFIRMACION
function compra_paso_3()
{
	$.ajax({
		type: "POST",
		url: WEB_PATH + "app/ajax/compra_paso3_div.php",
		data: {},
		async: false,
		success: function(response)
		{
			compra_set_active_tab('third');
			$('.ui-tabs-panel').html(response);
		}
	});
}

// CONFIRMAR Y GUARDAR
function compra_finalizar()
{
	$.ajax({
		type: "POST",
		url: WEB_PATH + "app/ajax/compra_finalizar.php",
		data: {},
		async: false,
		success: function(response)
		{
			var v_result = response.split('|');
			if (v_result[0] == 1) {
				compra_finalizada();
			} else {
				show_alert({titulo: MSG_ERROR, descripcion: response, error: 1});
			}
		}
	});
}

// COMPRA YA FINALIZADA MOSTRAR MENSAJE
function compra_finalizada()
{
	$.ajax({
		type: "POST",
		url: WEB_PATH + "app/ajax/compra_finalizada_div.php",
		data: {},
		async: false,
		success: function(response)
		{
			// Cabezal vuelve a quedar en 0
			$('#header_total_items').html(0);
			$('#header_total_items').attr('value',0);
			
			compra_set_active_tab('finished');
			$('.ui-tabs-panel').html(response);
		}
	});
}


// BORRAR EL HISTORIAL
function compra_borrar_historial()
{
	$.ajax({
		type: "POST",
		url: WEB_PATH + "app/ajax/compra_borrar_historial_post.php",
		data: {},
		async: false,
		success: function(response)
		{
			if (response == 1) {
				$('#div_no_compras').show();
				$('#div_compras').hide();
				show_alert({titulo: MSG_INFORMACION, descripcion: MSG_COMPRA_HISTORIAL_BORRADO, error: 0});
			} else
			if (response == -1) {
				show_alert({titulo: MSG_ERROR, descripcion: MSG_ERROR_DESCONOCIDO, error: 1});
			} else {
				mudis_login_callback = 'compra_borrar_historial()';
				mudis_login();
			}
		}
	});
}