    /**
    * funcion principal de validacion de la fecha
    * argumento fecha > cadena de texto de la fecha introducida por el usuario
    */
    function validarFecha(fecha)
    {
       /**
       * obtenemos la fecha introducida y la separamos en mes y año
       */
       a=fecha.value;
       mes=a.split("/")[0];
       anyo=a.split("/")[1];
	   if(a!=''){
		   if( (isNaN(mes)==true) || (isNaN(anyo)==true) )
		   {
			   alert("La fecha introducida debe estar formada sólo por números, según el formato mm/aaaa");
			   fecha.focus();
			   fecha.select();
			   return(false);
		   }
	   }
	  
       if ((mes<1) || (mes>12))
       {
           alert("El mes introducido no es válido. Por favor, introduzca un mes correcto");
           fecha.focus();
           fecha.select();
           return(false);
       }
       
       /* si el mes año introducido es menor que 1900 o mayor que 4000 > alertamos y detenemos ejecucion
       * NOTA: estos valores son a eleccion vuestra, y no constituyen por si solos fecha erronea
       */
       if ((anyo<2007) || (anyo>2100))
       {
           alert("El año introducido no es válido. Por favor, introduzca un año válido");
           fecha.focus();
           fecha.select();
		   return(false);
       } 
	  return true;
    }

function trimAll(sString) 
{
	while (sString.substring(0,1) == ' ')
	{
	sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
	sString = sString.substring(0,sString.length-1);
	}
return sString;
}

/**
* función para forzar entrada de datos
*/
function validarCampoNoVacio(theElement, nombreCampo)
{
	var r = trimAll(theElement.value);
	if(r=='')
	{
		alert("El campo "+nombreCampo+" es requerido, no puede estar vacio.");
		theElement.focus();
		return false;
	}
	else
	{
		return true;
	}
}

function esNumero(theElement, nombreCampo)
{
	/*if(!admitirCadenasVacias && theElement.value == "")
	{
		alert("Debe indicar un valor numérico en el campo: "+nombreCampo);
		theElement.focus();
		return false;	
	}*/

	if(isNaN(theElement.value) | theElement.value.indexOf(" ") > -1)
	{
		alert("Debe indicar un valor numérico en el campo: "+nombreCampo);
		theElement.focus();
		return false;
	}
	else
	{
		return true;	
	}
}

function esNombre(theElement, nombreCampo)
{
  var s = theElement.value;
  var ubicacion;
  var caracteres = "abcdefghijklmnopqrstuvwxyzñ ABCDEFGHIJKLMNOPQRSTUVWXYZÑáéíóúÁÉÍÓÚ";
  var contador = 0;
  for (var i=0; i < s.length; i++) 
  {
    ubicacion = s.substring(i, i + 1);
    if (caracteres.indexOf(ubicacion) != -1) 
	{
      contador++;
    } 
	else 
	{
      alert("ERROR: No se acepta el caracter '" + ubicacion + "', del campo " +nombreCampo);
      theElement.focus();
	  return false;
    }
  }
  return true;
}

function esEmail(theElement, nombre_del_elemento )
{
	var s = theElement.value;
	var filter=/^[A-Za-z][A-Za-z0-9_\.]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;
	if (s.length == 0 ) return true;
	if (filter.test(s))
	return true;
	else
	alert("Ingrese una dirección de correo válida");
	theElement.focus();
	return false;
}

function ValidarFormulario(form)
{
	/* campos requeridos */	
	if(!validarCampoNoVacio(form["nombreCliente"], "Nombre del cliente")) return false;
	if(!esNombre(form["nombreCliente"], "Nombre del cliente")) return false;
	
	if(!validarCampoNoVacio(form["nombreEmpresa"], "Nombre de la empresa")) return false;
	
	if(!validarCampoNoVacio(form["numeroEmpleados"], "Número de empleados")) return false;
	if(!esNumero(form["numeroEmpleados"], "Número de empleados")) return false;
	
	if(!validarCampoNoVacio(form["pais"], "País")) return false;
	if(!esNombre(form["pais"], "País")) return false;
	
	if(!validarCampoNoVacio(form["ciudad"], "Ciudad")) return false;
	if(!esNombre(form["ciudad"], "Ciudad")) return false;
	
	if(!validarCampoNoVacio(form["direccion"], "Dirección de la Empresa")) return false;
	
	if(!validarCampoNoVacio(form["codigoPais"], "Teléfono - Código de país")) return false;
	if(!esNumero(form["codigoPais"], "Teléfono - Código de país")) return false;
	
	if(!validarCampoNoVacio(form["codigoArea"], "Teléfono - Código de área")) return false;
	if(!esNumero(form["codigoArea"], "Teléfono - Código de área")) return false;
	
	if(!validarCampoNoVacio(form["telefono"], "Teléfono")) return false;
	if(!esNumero(form["telefono"], "Teléfono")) return false;
	
	if(!validarCampoNoVacio(form["email"], "E-mail")) return false;
	if(!esEmail(form["email"], "E-mail")) return false;
	
	if(!validarCampoNoVacio(form["fecha"], "Fecha de Entrega")) return false;
	if(!validarFecha(form["fecha"])) return false;
	
	if(!validarCampoNoVacio(form["descripcion"], "Descripción del Pedido")) return false;
		
	return true;
}
