function Decimales(Numero, Decimales) {
pot = Math.pow(10,Decimales);
num = parseInt(Numero * pot) / pot;
nume = num.toString().split('.');
if(nume.length==1) {
  var deci='';
  for(i=0;i<Decimales;i++)
    deci+=String.fromCharCode(48);
  num=nume+'.'+deci;
}
return num;
}

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 calcular(){
	var cantidad= document.getElementById('cantidad');
	var resultado= document.getElementById('resultado');
	var meses= document.getElementById('meses');
	if (cantidad.value=='')
	resultado.innerHTML='';
	else
	{
	if (meses.value!=0)
	{
	
	if (IsNumeric(meses.value) || (IsNumeric(cantidad.value)))
	{
		var cuota = meses.value*cantidad.value;
		cuota = cuota.toFixed(2);
		if (cuota>18)
		resultado.innerHTML='Cuota mensual:<br /><strong>'+cuota+' &euro;/mes</strong>'
		else
		resultado.innerHTML='Cuota mensual:<br /><strong style="color:red">Lo sentimos, la cuota mínima es de 18 &euro;/mes</strong>'
	}
	}
	else
	resultado.innerHTML='';
	}
}

function validarEmail(valor) {
  if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor)){
   return (true)
  } else {
      return (false);
  }
}
		var enviar ='no';
			function trim(stringToTrim) {
			return stringToTrim.replace(/^\s+|\s+$/g,"");
		}
function comprobarcontacto(){
var msgerror='';
var telefono = trim(document.f.telefono.value);

if (document.f.nombre.value=='') 
	msgerror = '* Nombre completo\n';
	
	if (document.f.metros.value=='') 
		msgerror += '* Nº de metros a instalar\n';
if (document.f.localidad.value=='') 
	msgerror += '* Localidad\n';
	
if (document.f.provincia.value=='') 
	msgerror += '* Provincia\n';	
if (telefono=='') 
	{
	msgerror = msgerror +'* Teléfono\n';
	}
else
	{
	if (!IsNumeric(telefono)) 
		msgerror = msgerror +'* El teléfono ha de ser un número\n';
	}
	
if (document.f.email.value=='') 
	msgerror += '* Email\n';
else
	{
	if (validarEmail(document.f.email.value)==false)
		msgerror = msgerror +'* El Email parece incorrecto\n';
	}
	
if (!document.f.acepto.checked) 
	msgerror = msgerror +'* Aceptar política de privacidad\n';
	
if (msgerror==''){
 if ((telefono.length)<9)
 	msgerror = '* El teléfono ha de tener 9 cifras como mínimo\n';
	document.f.telefono.focus();
}
if (msgerror!=''){
	alert('Los siguientes campos son obligatorios:\n\n'+msgerror);
	return false;
	}
else{

	document.f.action='enviarpresupuesto.php';
	document.f.submit();
	return false;	

}
}
function financi(){
	if (document.getElementById('cantidad'))
		{
			if (parseFloat(document.getElementById('metrosx').value))
			{
			var precio=(document.getElementById('precio').value*document.getElementById('metrosx').value);
			document.getElementById('cantidad').value=Decimales(precio,2);
			}
		}
}

function comprobarcontacto2(){
var msgerror='';
var telefono = trim(document.f.telefono.value);

if (document.f.nombre.value=='') 
	msgerror = '* Nombre completo\n';

if (document.f.localidad.value=='') 
	msgerror += '* Localidad\n';
	
if (document.f.provincia.value=='') 
	msgerror += '* Provincia\n';
	

if (telefono=='') 
	{
	msgerror += '* Teléfono\n';
	}
else
	{
	if (!IsNumeric(telefono)) 
		msgerror += '* El teléfono ha de ser un número\n';
	}
	
if (document.f.email.value=='') 
	msgerror += '* Email\n';
else
	{
	if (validarEmail(document.f.email.value)==false)
		msgerror +='* El Email parece incorrecto\n';
	}
	
if (!document.f.acepto.checked) 
	msgerror +='* Aceptar política de privacidad\n';
	
if (msgerror==''){
 if ((telefono.length)<9)
 	msgerror = '* El teléfono ha de tener 9 cifras como mínimo\n';
	document.f.telefono.focus();
}
if (msgerror!=''){
	alert('Los siguientes campos son obligatorios:\n\n'+msgerror);
	return false;
	}
else{

	document.f.action='enviarcontacto2.php';
	document.f.submit();
	return false;	

}
}
