function validateFormOnSubmitMasInfo(theForm) {
var reason = "";

  reason += validateEmptyMasInfo(theForm.nombre,'Nombre');
  reason += validateEmptyMasInfo(theForm.apellidos,'Apellidos');
  reason += validateEmailMasInfo(theForm.email);
  reason += validatePhoneMasInfo(theForm.telefono);
  reason += validateEmptyMasInfo(theForm.consulta,'Consulta / Petición');
  reason += validateCheckMasInfo(theForm.acepto);
  

  if (reason != "") {
    alert("Algunos campos requieren su atención:\n" + reason);
    return false;
  }

  return true;
}

function validateFormOnSubmitTalonario(theForm) {
var reason = "";

  reason += validateEmptyMasInfo(theForm.nombre,'Nombre');
  reason += validateEmptyMasInfo(theForm.apellidos,'Apellidos');
  reason += validateEmptyMasInfo(theForm.direccion,'Dirección');
  reason += validateEmptyMasInfo(theForm.poblacion,'Población');
  reason += validateEmptyMasInfo(theForm.pais,'Pais');
  reason += validateEmptyMasInfo(theForm.cp,'Código Postal');
  reason += validateEmailMasInfo(theForm.email);
  reason += validatePhoneMasInfo(theForm.telefono);
  reason += validateEmptySelect(theForm.selTarjetaPedir);
  reason += validateEmptyMasInfo(theForm.consulta,'Consulta / Petición');

  if (reason != "") {
    alert("Algunos campos requieren su atención:\n" + reason);
    return false;
  }

  return true;
}


function validateEmptySelect(fld) {
    var error = "";

    if ((fld.value == -1)) {
        fld.style.background = '#81B336';
        error = "Seleccione un tipo de tarjeta.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateCheckMasInfo(fld) {
    var error = "";

    if(!fld.checked){
    	fld.style.background = '#81B336';
        error = "Debe aceptar la política de privacidad.\n";
    }else{
    	fld.style.background = 'White';
    }
    
    return error;
}



function validateEmptyMasInfo(fld,texto) {
    var error = "";

    if ((fld.value.length == 0)||(fld.value==texto)) {
        fld.style.background = '#81B336';
        error = "El campo obligatorio no ha sido rellenado.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateUsernameMasInfo(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores

    if (fld.value == "") {
        fld.style.background = '#81B336';
        error = "You didn't enter a username.\n";
    } else if ((fld.value.length < 5) || (fld.value.length > 15)) {
        fld.style.background = '#81B336';
        error = "The username is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = '#81B336';
        error = "The username contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validatePasswordMasInfo(fld) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers

    if (fld.value == "") {
        fld.style.background = '#81B336';
        error = "You didn't enter a password.\n";
    } else if ((fld.value.length < 7) || (fld.value.length > 15)) {
        error = "The password is the wrong length. \n";
        fld.style.background = '#81B336';
    } else if (illegalChars.test(fld.value)) {
        error = "The password contains illegal characters.\n";
        fld.style.background = '#81B336';
    } else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
        error = "The password must contain at least one numeral.\n";
        fld.style.background = '#81B336';
    } else {
        fld.style.background = 'White';
    }
   return error;
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmailMasInfo(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;

    if (fld.value == "") {
        fld.style.background = '#81B336';
        error = "Introduzca su email.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = '#81B336';
        error = "Introduzca un email válido.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#81B336';
        error = "El email contiene caracteres no válidos.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validatePhoneMasInfo(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');

   if (fld.value == "") {
        error = "Introduzca su teléfono.\n";
        fld.style.background = '#81B336';
    } else if (isNaN(parseInt(stripped))) {
        error = "El telefono contiene caracteres no válidos.\n";
        fld.style.background = '#81B336';
    } else {
        fld.style.background = 'White';
    }
    return error;
}

