function loadingOn()
{
	$(".loading").fadeIn();
}

function loadingOff()
{
	$(".loading").fadeOut();
}

function updateQ(linia,producto,acabado,talla,idioma)
{
        idioma = typeof(idioma) != 'undefined' ? idioma : 'es';
	loadingOn();
        unidades = $("#unidades"+linia).val();
	$.ajax({
		type: "POST",
		url: "/en/shopping-cart/update-product",
		data: "producto="+producto+"&acabado="+acabado+"&talla="+talla+"&unidades="+unidades,
		success: function(total){
                        tot = total.split("-");
                        if (unidades > parseInt(tot[1])){
                            $("#shopping-cart-line"+linia+" #unidades"+linia).val(tot[1]);
                            alert(tot[2]);
                        }
			$("#shopping-cart-line"+linia+" .total-linia").html(tot[0]);
			updateTotales(idioma);
			loadingOff();
		}
	});
}

function updateTipoEnvio( idioma, pais )
{
	loadingOn();
        if (typeof(pais) == 'undefined')
            pais = $('#pais').val();
        else
            pais = $('#envio_pais').val();
        updateTotales( idioma, pais );
        loadingOff();
}

function getTipoEnvio(){
    var tipo_envio = 'default';
    tipo_envio = $('.carrier:checked').val();
    return tipo_envio;
}

function updateTotales( idioma, pais )
{
    idioma = typeof(idioma) != 'undefined' ? idioma : 'es';
    pais = typeof(pais) != 'undefined' ? pais : '';
    var tipo_envio = getTipoEnvio();

    var dades = "nodata=true&tipo_envio="+tipo_envio;
    if (pais != ''){
        dades = dades + "&pais=" + pais;
    }
    $.ajax({
            type: "POST",
            url: "/en/sistema/get-totales",
            data: dades,
            success: function(info){
                    aux=info.split("@");
                    $("#header-totales-unidades").html(aux[0]);
                    $("#header-totales-precio").html(aux[1]);
                    $("#total-cesta-precio").html(aux[1]);
                    $("#gastos-envio-estimados-precio"+tipo_envio).html(aux[2]);
                    if (typeof aux[3] != "undefined"){$("#descuento").html(aux[3]);}
                    if (typeof aux[4] != "undefined"){$("#linia-carriers").html(aux[4]);}
                    else {$("#linia-carriers").html('');}
                    //if (typeof aux[5] != "undefined"){$("#total-bonos").html(aux[5]);}
                    //if (aux[1][0] == 0){$('.step3_table:visible').hide();}
                    //else{$('.step3_table:hidden').show();}
            }
    });
}

function deleteProduct(linia,producto,acabado,talla)
{
	loadingOn();
	$.ajax({
		type: "POST",
		url: "/en/shopping-cart/del-product",
		data: "producto="+producto+"&acabado="+acabado+"&talla="+talla,
		success: function(product_count){
			if(product_count==0)
			{
				document.location=document.location;
			}
			updateTotales();
			loadingOff();
		}
	});

	$('#shopping-cart-line'+linia).fadeOut("slow");
}

function useInvoiceData(idioma)
{
	if($("#usar_datos_facturacion").is(":checked"))
	{
		$("#datos_envio_container").hide();
                if (typeof(idioma) != 'undefined'){
                    updateTipoEnvio(idioma);
                }
	}
	else
	{
		$("#datos_envio_container").fadeIn("slow");
                if (typeof(idioma) != 'undefined'){
                    updateTipoEnvio(idioma, 'envio_pais');
                }
	}
}


function changeUserMode()
{
	if($("#new_user1").is(":checked")) 
	{
		$("#cuenta_usuario_bloque").fadeIn("slow");
		$("#full_data_bloque").fadeIn("slow");
		$("#login_bloque").hide();
		$("#mode").val("register");
	}
	else
	{
		$("#login_bloque").fadeIn("slow");
		$("#cuenta_usuario_bloque").hide();
		$("#full_data_bloque").hide();
		$("#mode").val("login");
	}
}

//codigos descuento
function onEnterCodigo(ev)
{
    if(ev==13)
    {
	loadingOn();
	$.ajax({
		type: "POST",
		url: "/en/shopping-cart/check-discount",
		data: "codigo="+$("#codigo").val(),
                success: function(check){
                        if(check == 0){
                            //$("#mensaje-codigos").html('&nbsp;&nbsp;Código no válido');
                            $("#mensaje-codigos").show();
                        }
			else{
                            updateTotales();
                            //$("#mensaje-codigos2").html('Felicidades: el descuento se ha aplicado');
                            $("#mensaje-codigos").hide();
                            $("#mensaje-codigos2").show();
                            total = $("#total-cesta-precio").html();
                            envio = $("#gastos-envio-estimados-precio").html()
                            result = parseFloat(total) - parseFloat(envio);
                            result = result * parseFloat(check);
                            //$("#descuento").html(parseFloat(result) + '  €');
                            $("#target").hide();
                            $("#form_codigo").hide();

                            //es fa per watx, per "borrar reloj regalo"
                            //deleteProduct('1','5625','5737','_u_');
                            //location.reload();

                        }
			loadingOff();
		}
	});


    }
}
