function printEtiqueta()
{
    var rad_val = 0;
    for (var i=0; i < document.formEtiquetas.tipoetiqueta.length; i++)
    {
        if (document.formEtiquetas.tipoetiqueta[i].checked)
        {
            rad_val = document.formEtiquetas.tipoetiqueta[i].value;
        }
    }
    if (rad_val == 0)
    {
        alert('Debe seleccionar un modelo de etiqueta para continuar.');
    }
    else
    {
        document.formEtiquetas.submit();
    }
}




function ordenar(campo, accion)
{
    //funcion para ordenar las columnas del listado de artículos

    //cambiamos la accion por el tema de las ofertasdiaini (color salmon)
    document.frmBusqueda.action = accion;
    switch(campo)
    {
        case 'codigo':
            if (document.frmBusqueda.orden.value == 'codigoDesc' ||
                document.frmBusqueda.orden.value != 'codigoAsc')
                document.frmBusqueda.orden.value = 'codigoAsc';
            else
                document.frmBusqueda.orden.value = 'codigoDesc';
            document.frmBusqueda.submit();
            break;

        case 'marca':
            if (document.frmBusqueda.orden.value == 'marcaDesc' ||
                document.frmBusqueda.orden.value != 'marcaAsc')
                document.frmBusqueda.orden.value = 'marcaAsc';
            else
                document.frmBusqueda.orden.value = 'marcaDesc';
            document.frmBusqueda.submit();
            break;

        case 'pvpprov':
            if (document.frmBusqueda.orden.value == 'pvpprovDesc' ||
                document.frmBusqueda.orden.value != 'pvpprovAsc')
                document.frmBusqueda.orden.value = 'pvpprovAsc';
            else
                document.frmBusqueda.orden.value = 'pvpprovDesc';
            document.frmBusqueda.submit();
            break;

        case 'tarifa_oferta':
            if (document.frmBusqueda.orden.value == 'tarifa_ofertaDesc' ||
                document.frmBusqueda.orden.value != 'tarifa_ofertaAsc')
                document.frmBusqueda.orden.value = 'tarifa_ofertaAsc';
            else
                document.frmBusqueda.orden.value = 'tarifa_ofertaDesc';
            document.frmBusqueda.submit();
            break;

        case 'tarifa':
            if (document.frmBusqueda.orden.value == 'tarifaDesc' ||
                document.frmBusqueda.orden.value != 'tarifaAsc')
                document.frmBusqueda.orden.value = 'tarifaAsc';
            else
                document.frmBusqueda.orden.value = 'tarifaDesc';
            document.frmBusqueda.submit();
            break;
    }
}

function fechaSubmit()
{
    fechaini = document.formulario.fechainiAno.value + "/" + document.formulario.fechainiMes.value + "/" + document.formulario.fechainiDia.value;
    document.formulario.fechaini.value = fechaini;

    fechafin = document.formulario.fechafinAno.value + "/" + document.formulario.fechafinMes.value + "/" + document.formulario.fechafinDia.value;
    document.formulario.fechafin.value = fechafin;

    document.formulario.submit();
}

function recalcular()
{
    if ((event.keyCode != 37) && (event.keyCode != 38) && (event.keyCode != 39) && (event.keyCode != 40))
    {
        document.formPedido.submit();
    }

}

function montre(id) {
	  if (document.getElementById) {
		  document.getElementById(id).style.display="block";
		} else if (document.all) {
		  document.all[id].style.display="block";
		} else if (document.layers) {
		  document.layers[id].display="block";
		} }

 function cache(id) {
	  if (document.getElementById) {
		  document.getElementById(id).style.display="none";
		} else if (document.all) {
		  document.all[id].style.display="none";
		} else if (document.layers) {
		  document.layers[id].display="none";
		} }


function modalOpen (dialog) {
	dialog.overlay.fadeIn('slow', function () {
		dialog.container.fadeIn('slow', function () {
			dialog.data.hide().slideDown('slow');
		});
	});
}

function modalClose (dialog) {
    dialog.data.slideUp('slow', function () {
	    dialog.container.fadeOut('slow', function () {
		    dialog.overlay.fadeOut('slow');
	    });
	});
}

function ajaxCapturar() {
   $.ajax({
    type: 'POST',
    url: 'buy_ajax.php',
    data: $("#form1").serialize(),
    beforeSend: function() {
        showHiddenDIV('visible');
    },
    success: function(msg) {
        _retorno = unescape(msg);
        _retorno = _retorno.replace(/\+/gi,' ');
        showHiddenDIV('hidden');

        resultado = _retorno.split(';');
        if (resultado[0] == "KO")
        {
            alert('Ha caducado su sesión por inactividad. Por favor recargue la página');
        }
        else
        {
            //actualizamos el miniCarrito
            $('#miniCarritoArticulos').text(resultado[1]);
            $('#miniCarritoTotal').text(resultado[2]);
        }

        $('.unitsBox').val('0');
    }
});
}

function validarEntregaRuta(sesion)
{
    //data: 'PHPSESSID=' + sesion + '&entrega=' + $('#entrega_any').val() + '/' + $('#entrega_mes').val() + '/' + $('#entrega_dia').val() + '&pedive=' + $('#pedive').val() + '&letra=' + $('#letra').val() + '&env_cli=' + $('#env_cli').val(),
    $.ajax({
     type: 'POST',
     url: 'ruta_entrega.php',
     data: 'PHPSESSID=' + sesion + '&entrega=' + $('#entrega').val() + '&pedive=' + $('#pedive').val() + '&letra=' + $('#letra').val() + '&env_cli=' + $('#env_cli').val(),
     beforeSend: function() {

     },
     success: function(msg) {
        _retorno = unescape(msg);
        _retorno = _retorno.replace(/\+/gi,' ');
        if (_retorno == "OK")
        {
            //submit
            document.formPedido.submit();
        }
        else
        {
            //fecha entrega no es valida.
            alert(_retorno);
        }
     }
   });
}

function showHiddenDIV(accion)
{
    if (accion == 'visible')
    {
        document.getElementById('divPantallaNegra').style.height = document.documentElement.clientHeight+'px';
        document.getElementById('divPantallaNegra').style.width = document.documentElement.clientWidth+'px';

        var XpositionDIV = (document.documentElement.clientWidth/2) - (document.getElementById('divPantallaBlanca').clientWidth/2);
        var YpositionDIV = (document.documentElement.clientHeight/2) - (document.getElementById('divPantallaBlanca').clientHeight/2);

        document.getElementById('divPantallaBlanca').style.left = XpositionDIV+'px';
        document.getElementById('divPantallaBlanca').style.top =  (YpositionDIV)+'px';
    }

    document.getElementById('divPantallaNegra').style.visibility = accion;
    document.getElementById('divPantallaBlanca').style.visibility = accion;
}

function calcularMargenLinea(indice)
{
    //calcular el importe una vez aplicado el % de incremento sobre el precio de tarifa
    //final = $('#importe_' + indice).text() * (1 - $('#margen_' + indice).val() / 100);
    final = $('#importe_' + indice).html() * (1 - $('#margen_' + indice).html() / 100);
    $('#final_' + indice).html(final);
    $('#final_' + indice).currency({s:".",d:",",c:2});
}

function establecerMargenes(total, margen)
{
    //establecer todas las casillas de descuento cliente con el
    //descuento que se pasa por parametro por defecto (margen)
    var i;

    for (i = 0; i < total; i++)
    {
        $('#margen_' + i).val(margen);
        calcularMargenLinea(i);
    }
}
function establecerMargen(linia, margen) {
    $('#margen_' + linia).val(margen);
    $('#margen_' + linia).html(margen);
    calcularMargenLinea(linia);
}


