function trim(cadena)
{
  return(cadena.replace(/^\s*|\s*$/g,""));
}

function validar()
{
  nombre    = trim(document.getElementById('nombre').value);
  apellidos = trim(document.getElementById('apellidos').value);
  direccion = trim(document.getElementById('direccion').value);
  cp        = trim(document.getElementById('cp').value);
  pais      = trim(document.getElementById('pais').value);
  estado    = trim(document.getElementById('estado').value);
  ciudad    = trim(document.getElementById('ciudad').value);
  email     = trim(document.getElementById('email').value);
  telefono  = trim(document.getElementById('telefono').value);
  sexo      = trim(document.getElementById('sexo').value);
  //FECHA
  anioNacObj = document.getElementById('anioNac');
  anioNac    = anioNacObj.options[anioNacObj.selectedIndex].value;
  mesNacObj  = document.getElementById('mesNac');
  mesNac     = mesNacObj.options[mesNacObj.selectedIndex].value;
  diaNacObj  = document.getElementById('diaNac');
  diaNac     = diaNacObj.options[diaNacObj.selectedIndex].value;
  
  ocupacion = trim(document.getElementById('ocupacion').value);
  
  errorNombre    = validarNombre(nombre);
  errorApellidos = validarApellidos(apellidos);
  errorDireccion = validarDireccion(direccion);
  errorCp        = validarCp(cp);
  errorCiudad    = validarCiudad(ciudad);
  errorEstado    = validarEstado(estado);
  errorPais      = validarPais(pais);
  errorEmail     = validarEmail(email);
  errorTelefono  = validarTelefono(telefono);
  errorFechaNac  = validarFechaNac(diaNac,mesNac,anioNac);
  errorOcupacion = validarOcupacion(ocupacion);

  f = diaNac+'/'+mesNac+'/'+anioNac;
  //errorMayoriaEdad = validarEdad(f);

  if(errorNombre != "")
  {
    document.getElementById('registrar').disabled=true;
	document.getElementById('msj-error').innerHTML = errorNombre;
	
	document.getElementById('registrar').style.background= '#000';
	document.getElementById('registrar').style.color= '#394551';	
    return(false);
  }
  else if(errorApellidos != "")
  {
    document.getElementById('registrar').disabled=true;
	document.getElementById('msj-error').innerHTML = errorApellidos;
	document.getElementById('registrar').style.background= '#000';
	document.getElementById('registrar').style.color= '#394551';	
    return(false);
  }
  else if(errorDireccion != "")
  {
    document.getElementById('registrar').disabled=true;
	document.getElementById('msj-error').innerHTML = errorDireccion;
	document.getElementById('registrar').style.background= '#000';
	document.getElementById('registrar').style.color= '#394551';	    
	return(false);
  }
  else if(errorCp != "")
  {
    document.getElementById('registrar').disabled=true;
	document.getElementById('msj-error').innerHTML = errorCp;
	document.getElementById('registrar').style.background= '#000';
	document.getElementById('registrar').style.color= '#394551';	
    return(false);
  }
  else if(errorCiudad != "")
  {
    document.getElementById('registrar').disabled=true;
	document.getElementById('msj-error').innerHTML = errorCiudad;
	document.getElementById('registrar').style.background= '#000';
	document.getElementById('registrar').style.color= '#394551';	
    return(false);
  }
  else if(errorEstado != "")
  {
    document.getElementById('registrar').disabled=true;
	document.getElementById('msj-error').innerHTML = errorEstado;
	document.getElementById('registrar').style.background= '#000';
	document.getElementById('registrar').style.color= '#394551';	
    return(false);
  }
  else if(errorPais != "")
  {
    document.getElementById('registrar').disabled=true;
	document.getElementById('msj-error').innerHTML = errorPais;
	document.getElementById('registrar').style.background= '#000';
	document.getElementById('registrar').style.color= '#394551';	
    return(false);
  }
  else if(errorEmail != "")
  {
    document.getElementById('registrar').disabled=true;
	document.getElementById('msj-error').innerHTML = errorEmail;
	document.getElementById('registrar').style.background= '#000';
	document.getElementById('registrar').style.color= '#394551';	
    return(false);
  }
  else if(errorFechaNac != "")
  {
    document.getElementById('registrar').disabled=true;
	document.getElementById('msj-error').innerHTML = errorFechaNac;
	document.getElementById('registrar').style.background= '#000';
	document.getElementById('registrar').style.color= '#394551';	
    return(false);
  }
  else if(errorTelefono != "")
  {
    document.getElementById('registrar').disabled=true;
	document.getElementById('msj-error').innerHTML = errorTelefono;
	document.getElementById('registrar').style.background= '#000';
	document.getElementById('registrar').style.color= '#394551';	
    return(false);
  }
  else if(errorOcupacion != "")
  {
    document.getElementById('registrar').disabled=true;
	document.getElementById('msj-error').innerHTML = errorOcupacion;
	document.getElementById('registrar').style.background= '#000';
	document.getElementById('registrar').style.color= '#394551';	
    return(false);
  }
/*
  else if(errorMayoriaEdad != "")
  {
    document.getElementById('registrar').disabled=true;
	document.getElementById('msj-error').innerHTML = errorMayoriaEdad;
	document.getElementById('registrar').style.background= '#000';
	document.getElementById('registrar').style.color= '#394551';	
    return(false);
  }
*/
  else
  {
    document.getElementById('registrar').disabled  = false;
	document.getElementById('msj-error').innerHTML = "";
	document.getElementById('registrar').style.background= '#1a446c';
	document.getElementById('registrar').style.color= '#fff';
    return(true);
  }
}

function presionarBoton()
{
  if(validar())
    document.getElementById('formRegistro').submit();

}

function validarNombre(nombre)
{
  if(nombre == "")
    error = "El campo Nombre es obligatorio";
  else if(nombre.length < 3)
    error = "El contenido del campo nombre es demasiado corto...";
  else
    error = "";
  return(error);
}
function validarApellidos(apellidos)
{
  if(apellidos == "")
    error = "El campo Apellidos es obligatorio";
  else if(apellidos.length < 4)
    error = "El contenido del campo Apellidos es demasiado corto...";
  else
    error = "";
  return(error);
}
function validarDireccion(dir)
{
  if(dir == "")
    error = "El campo Direccion es obligatorio";
  else if(dir.length <= 8)
    error = "El contenido del campo Direccion es demasiado corto...";
  else
    error = "";
  return(error);
}
function validarCp(cp)
{
  if(cp == "")
    error = "El campo Codigo Postal es obligatorio";
  else if(cp.length < 4)
    error = "El contenido del campo Codigo Postal es demasiado corto...";
  else if(!cp.match(/^[0-9]{4,6}$/))
    error = "El contenido del campo Codigo Postal debe ser num&eacute;rico ...";
  else
    error = "";
  return(error);
}
function validarPais(pais)
{
  if(pais == "")
    error = "El campo Pais es obligatorio";
  else if(pais.length < 4)
    error = "El contenido del campo Pais es demasiado corto...";
  else
    error = "";
  return(error);
}
function validarEstado(edo)
{
  if(edo == "")
    error = "El campo Estado es obligatorio";
  else if(edo.length < 2)
    error = "El contenido del campo Estado es demasiado corto...";
  else
    error = "";
  return(error);
}
function validarCiudad(ciudad)
{
  if(ciudad == "")
    error = "El campo Ciudad es obligatorio";
  else if(ciudad.length < 2)
    error = "El contenido del campo Ciudad es demasiado corto...";
  else
    error = "";
  return(error);
}
function validarEmail(email)
{
  if(email == "")
    error = "El campo Correo Electr&oacute;nico es obligatorio";
  else if(email.match(/^[-A-z0-9\.]+@([-A-z0-9]+\.){1,3}[-A-z0-9]{2,4}$/))
    error = "";
  else  
    error = "Ingresa un Correo Electr&oacute;nico v&aacute;lido";
  return(error);
}
function validarTelefono(telefono)
{
  if(telefono != "") 	
  {
  	if(telefono.length < 5)
      error = "El contenido del campo TELEFONO es demasiado corto...";
  	else if(telefono.match(/^[0-9]{5,20}$/))
      error = "";
    else
      error = "S&oacute;lo se aceptan n&uacute;meros en el campo " +
            "Tel&eacute;fono, sin espacios";
  }	
  return(error);
}
function validarFechaNac(dia,mes,anio)
{
  error = "";

  if(dia == 0)
    error = "Ingresa el D&iacute;a de la Fecha de Nacimiento";
  else if(mes == 0)
    error = "Ingresa el Mes de la Fecha de Nacimiento";
  else if(anio == 0)
    error = "Ingresa el A&ntilde;o de la Fecha de Nacimiento";
  else
  {
    if((anio % 4) == 0)
    {
      if((mes == 2) && (dia > 29))
        error = "Ingresa una Fecha de Nacimiento v&aacute;lida";
    }
    else if((mes == 2) && (dia > 28))
      error = "Ingresa una Fecha de Nacimiento v&aacute;lida";

    if(((mes == 4) || (mes == 6) || (mes == 9) || (mes == 11)) &&
             (dia > 30))
      error = "Ingresa una Fecha de Nacimiento v&aacute;lida";

    if(error=="")
    {  
      if(anio==1989 && mes>=4)
      {
        fecha = new Date();
        diaHoy= fecha.getDate();
        if(diaHoy<dia)
          error = "A&uacute;n no eres mayor de EDAD ...";
      }
    }
  }

  return(error);
}
function validarOcupacion(ocupacion)
{
  if(ocupacion != "")
  {
	if(ocupacion.length < 5)
      error = "El contenido del campo ocupacion es demasiado corto...";
    else
      error = "";
  }
  return(error);
}
function validarEdad(fecha)
{
  error="";
  edad = calcularEdad(fecha);

alert(edad);
  if(edad<18)
    error = "NO eres Mayor de edad...";

  return error;
} 
function calcularEdad(fecha)
{
    hoy=new Date()

    var array_fecha = fecha.split("/")
    if (array_fecha.length!=3)
       return false

    var ano
    ano = parseInt(array_fecha[2]);
    if (isNaN(ano))
       return false

    var mes
    mes = parseInt(array_fecha[1]);
    if (isNaN(mes))
       return false

    var dia
    dia = parseInt(array_fecha[0]);
    if (isNaN(dia))
       return false

    if (ano<=99)
       ano +=1900
alert("aņo"+ano);
    edad=hoy.getYear()- ano - 1;

    if (hoy.getMonth() + 1 - mes < 0)
       return edad

    if (hoy.getMonth() + 1 - mes > 0)
       return edad+1

    if (hoy.getUTCDate() - dia >= 0)
       return edad + 1

    return edad
} 

