function buscadorFocus()
{
	var t=document.getElementById('tx_busca');
	if (t.value=='Buscar:')
		t.value='';
}

function buscadorClick()
{
	var t=document.getElementById('tx_busca').value;
	if(t=='' || t=='Buscar:')
	{
		alert('Por favor, escriba algún termino de búsqueda');
		document.getElementById('tx_busca').value='';
		document.getElementById('tx_busca').focus();
		return;
	}
	document.getElementById('fbusc').submit();
}

function abreFamilia(id)
{
	var t=document.getElementById('menuiz').getElementsByTagName('div');
	for (var x=0;x<t.length;x++)
	{
		var i=t[x].id;
		if(i.substr(0,3)=='las')
			t[x].style.display='none';
	}
	document.getElementById('las'+id).style.display='block';
}

function checkContacta()
{
	if (document.f1.f_nombre.value=='' || document.f1.f_apellidos.value=='' || document.f1.f_especialidad.value=='' || document.f1.f_direccion.value=='' || document.f1.f_ciudad.value=='' || document.f1.f_cp.value=='' || document.f1.f_pais.value=='' || document.f1.f_telefono1.value=='' || document.f1.f_email.value=='')
	{
		alert('Por favor, rellene todos los campos marcados con *');
		return;
	}
	if (document.f1.f_pais.value==1 && document.f1.id_provincia.value=='')
	{
		alert('Por favor, rellene todos los campos marcados con *');
		return;
	}
	if (document.f1.f_pais.value!=1 && document.f1.provincia.value=='')
	{
		alert('Por favor, rellene todos los campos marcados con *');
		return;
	}

	document.f1.submit();
}

function focusLogin()
{
	document.getElementById('lU').value='';
}
function focusPass()
{
	document.getElementById('lP1').style.display='none';
	document.getElementById('lP2').style.display='inline';
	document.getElementById('lP2').focus();
}
function keyPass(e)
{
	var keycode=0;
	if (e.keyCode)
		keycode=e.keyCode;
   else keycode=e.which;
  var character=String.fromCharCode(keycode);
  alert(character);
}

function checkReg()
{
	if (document.f1.nombre.value=='' || document.f1.apellidos.value=='' || document.f1.especialidad.value=='' || document.f1.clinica.value=='' || document.f1.direccion.value=='' || document.f1.poblacion.value=='' || document.f1.cp.value=='' || document.f1.provincia.value=='' || document.f1.telefono.value=='' || document.f1.email.value=='' || document.f1.usuario.value=='' || document.f1.cif.value=='' || document.f1.s1.value=='' || document.f1.s2.value=='' || document.f1.s3.value=='' || document.f1.pass1.value=='' || document.f1.pass2.value=='')
	{
		alert('Por favor, rellene todos los campos marcados con *');
		return;
	}
	if(document.f1.pass1.value!=document.f1.pass2.value)
	{
		alert('Las contraseñas no coinciden. Por favor, escríbalas de nuevo');
		return;
	}
	if(document.f1.acepto.checked==false)
	{
		alert('Para solicitar el registro debe aceptar las condiciones de la Política de Confidencialidad de Datos');
		return;
	}
	document.f1.submit();
}

function desconexion()
{
	if(confirm('¿Desea terminar la sesión de cliente registrado?'))
		window.location.href='desconexion.php';
}

var pedido=new Array();
var cat=new Array();
var prod=new Array();
var prod_cat=new Array();
var prod_precio=new Array();

function aniadeProd(id,cantidad)
{
	if(id=='' || cantidad=='')
		return;
	pedido[id]=cantidad;
	actPedido();
}

function quitaProd(id)
{
		pedido[id]=0;
		actPedido();
}

function cancelaPedido()
{
	if(confirm('¿Desea vaciar la lista del pedido?'))
	{
		pedido=new Array();
		actPedido();
	}
}
var totalPedido=0;
function actPedido()
{
	var total=0;
	var cad="<table class='df regUser2' cellpadding='5' cellspacing='0' width='100%'><tr><td align='right' style='font-weight:bold;font-style:italic;font-size:14px;'>SU PEDIDO ACTUAL:</td><td>&nbsp;</td></tr>";
	for (var id_cat in cat)
	{
		cad+="<tr valign='top' style='font-size:11px;font-weight:bold'><td align='right'>> "+cat[id_cat]+":</td><td width='600'><table class='df' style='font-size:11px' cellpadding='0' cellspacing='0'>";
		for (var id_prod in pedido)
		{
			if(prod_cat[id_prod]==id_cat && pedido[id_prod]>0)
			{
				var cantidad=pedido[id_prod];
				var precio=cantidad*prod_precio[id_prod];
				total+=precio;
				precio=round_decimals(precio,2);
				precio=precio.toString().replace('.',',');
				cad+='<tr><td width="300">'+cantidad+' x '+prod[id_prod]+'</td><td>&nbsp;&nbsp;</td><td align="right" width="100">'+precio+" &euro;</td><td>&nbsp;&nbsp;&nbsp;&nbsp;</td><td><img src='img/quitar.png' style='cursor:pointer;cursor:hand;' onclick='quitaProd("+id_prod+")'/></td></tr>";
			}
		}
		cad+="</table></td></tr>";
	}
	if(total>0)
	{
		var iva=total*0.16;
		var totalconiva=total+iva;
		totalPedido=totalconiva;

		iva=round_decimals(iva,2);
		total=round_decimals(total,2);
		totalconiva=round_decimals(totalconiva,2);

		iva=iva.toString().replace('.',',');
		total=total.toString().replace('.',',');
		totalconiva=totalconiva.toString().replace('.',',');

		cad+="<tr style='font-size:11px;font-weight:bold'><td align='center' colspan='2' style='border-top:1px solid #000'><!--SubTotal: "+total+" &euro; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;I.V.A. (16%): "+iva+" &euro;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-->Total: "+total+" &euro;</td></tr>";
		document.getElementById('preciototal').innerHTML=total+' &euro;';
	}
	cad+="</table>";
	document.getElementById('cont_pedido').innerHTML=cad;
}

function finPedido()
{
	if(document.f1.nombre.value=='' || document.f1.email.value=='' || document.f1.telefono.value=='')
	{
		alert('Por favor, rellene los campos marcados con * para poder tramitar su pedido');
		return;
	}
	if(totalPedido==0)
	{
		alert('Debe seleccionar al menos un producto para realizar el pedido');
		return;
	}
	var lista='';
	for (var id_prod in pedido)
		lista+=id_prod+'#'+pedido[id_prod]+';';
	document.f1.pedido.value=lista;
	if(confirm('¿Enviar el pedido?'))
		document.f1.submit();
}



/* This script is Copyright (c) Paul McFedries and
Logophilia Limited (http://www.mcfedries.com/).
Permission is granted to use this script as long as
this Copyright notice remains in place.*/

function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return pad_with_zeros(result3, decimals)
}

function pad_with_zeros(rounded_value, decimal_places) {

    // Convert the number to a string
    var value_string = rounded_value.toString()

    // Locate the decimal point
    var decimal_location = value_string.indexOf(".")

    // Is there a decimal point?
    if (decimal_location == -1) {

        // If no, then all decimal places will be padded with 0s
        decimal_part_length = 0

        // If decimal_places is greater than zero, tack on a decimal point
        value_string += decimal_places > 0 ? "." : ""
    }
    else {

        // If yes, then only the extra decimal places will be padded with 0s
        decimal_part_length = value_string.length - decimal_location - 1
    }

    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length

    if (pad_total > 0) {

        // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++)
            value_string += "0"
        }
    return value_string
}

function actProv(val)
{
	if(val==1)
	{
		document.getElementById('listprov').style.display='inline';
		document.getElementById('txprov').style.display='none';
	}
	else
	{
		document.getElementById('listprov').style.display='none';
		document.getElementById('txprov').style.display='inline';
	}
}
