var suma = 0; 
Number.prototype.decimal = function(n) { 
pot = Math.pow(10, parseInt(n)); 
return parseInt(this * pot) / pot; 
} 

function sumar(){ 
suma=parseFloat(document.form1.select1.value) + parseFloat(document.form1.select2.value); 
document.form1.textfield.value=suma.decimal(2); 
//document.getElementById('T1').value=(document.getElementById('textfield').value)*1.16; 
 } 


function revisa_provincias(pais)
{
if (pais==2)
{
document.getElementById('provincias_pedido').style.display='none';
document.getElementById('gastos_envio').innerHTML=document.getElementById('gastos2').value+' €.';
suma=parseFloat(Decimalesj(document.getElementById('gastos2').value)) + parseFloat(Decimalesj(document.getElementById('stotal').value)); 
suma=suma.decimal(2)+'';
document.getElementById('total_pedido').innerHTML=Decimales(suma) +' €.';
}
else
{
document.getElementById('provincias_pedido').style.display='block';
document.getElementById('gastos_envio').innerHTML=document.getElementById('gastos1').value +' €.';
suma=parseFloat(Decimalesj(document.getElementById('gastos1').value)) + parseFloat(Decimalesj(document.getElementById('stotal').value)); 
suma=suma.decimal(2)+'';
document.getElementById('total_pedido').innerHTML=Decimales(suma) +' €.';
}
}

function Decimales(Numero) {
Numero = Numero.replace('.',',');
return Numero;
}

function Decimalesj(Numero) {
Numero = Numero.replace(',','.');
return Numero;
}


function valida_finalcompra()
{
if (document.forms.finalcompra.nombre.value=="" || document.forms.finalcompra.apellidos.value=="" || document.forms.finalcompra.poblacion.value=="" || document.forms.finalcompra.provincia.value=="" || document.forms.finalcompra.direccion.value=="" || document.forms.finalcompra.email.value=="" || document.forms.finalcompra.telefono.value=="" || document.forms.finalcompra.cpostal.value=="")
{
alert('Ha de cumplimentar todos los campos del formulario\npara proceder al envío de su pedido.');
return false;
}
else
{
if (!IsNumeric(document.forms.finalcompra.cpostal.value))
{
alert('El código postal sólo puede contener números.');
return false;
}
else
{
if (!valEmail(document.forms.finalcompra.email.value))
{
alert('La dirección de email no parece correcta.');
return false;
}
else
{
document.forms.finalcompra.action='/finalcompra/';
return true;
}
}
}
}


function valida_buscador()
{
if(document.forms.buscador.buscar.value=="")
  {
  alert('Indique un término para iniciar la búsuqeda.');
  return false;
  }
  else
  {
    if(document.forms.buscador.buscar.value.length<3)
    {
    alert('Ha de indicar un término más largo para buscar.');
    return false;
    }
    else
    {
    return true;
    }
  }
}


function valida_compra(id_medida, id_articulo)
{
if(document.getElementById('cantidad'+id_medida).value=="" || !IsNumeric(document.getElementById('cantidad'+id_medida).value))
  {alert('Indique la cantidad que desea comprar.');}
  else
  {
  if (parseFloat(document.getElementById('cantidad'+id_medida).value) < parseFloat(document.getElementById('pedidominimo').value))
  {alert('El pedido mínimo para este artículo es de '+document.getElementById('pedidominimo').value+' unidades.');}
  else
  {
  window.location.href='/carrito/?o=a&cant='+document.getElementById('cantidad'+id_medida).value+'&color='+document.getElementById('color'+id_medida).value+'&trolley='+document.getElementById('trolley'+id_medida).value+'&item='+id_articulo+'&medidas='+id_medida;
  }
  }
}

function IsNumeric(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}

function valEmail(valor){    // Cortesía de http://www.ejemplode.com
    re=/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/
    if(!re.exec(valor))    {
        return false;
    }else{
        return true;
    }
}

function validar_formulario_contacto()
{
if(document.forms.formulario_contacto.email.value=="" || document.forms.formulario_contacto.nombre.value=="" || document.forms.formulario_contacto.telefono.value=="" || document.forms.formulario_contacto.comentarios.value=="")
  {
  alert('Indique su nombre, su email, un teléfono de contacto y su mensaje.');
  return false;
  }
  else
  {
  document.forms.formulario_contacto.action='/envia_contacto/';
  return true;
  }
}

