function validarContactenos(f) {
    if((f.email != null) &&  (!isValidEmail(f.email))) {
        alert("Ha d'introduir una adreça de c/e vàlida");
    } else {
        f.submit();
    }
}

function isValidEmail(emailForm) {
    var idx_at = emailForm.value.indexOf('@');
    if (idx_at == -1 || idx_at == 0) return false;
    var idx_dot = emailForm.value.indexOf('.', (idx_at + 2));
    if (idx_dot == -1 ) return false;
    return true;
}

function isEmptyField(campo) {
    if (campo.value.length==0 || campo.value==null) return true;
    else return false;
}

function changeMovility(f) {
    if (f.movility[f.movility.selectedIndex].value == '1') {
        f.cityMovility.disabled = false;
    } else {
        f.cityMovility.disabled = true;
    }
}

function changeWork(f) {
    if (f.workCapio[f.workCapio.selectedIndex].value == '1') {
        f.centerWorkCapio.disabled = false;
        f.monthIniWork.disabled = false;
        f.monthEndWork.disabled = false;
        f.yearIniWork.disabled = false;
        f.yearEndWork.disabled = false;
    } else {
        f.centerWorkCapio.disabled = true;
        f.monthIniWork.disabled = true;
        f.monthEndWork.disabled = true;
        f.yearIniWork.disabled = true;
        f.yearEndWork.disabled = true;
    }
}

function controlarCurriculum() {
    var f = document.forms[2];

    if (f.workCapio[f.workCapio.selectedIndex].value == '1') {
        f.centerWorkCapio.disabled = false;
        f.monthIniWork.disabled = false;
        f.monthEndWork.disabled = false;
        f.yearIniWork.disabled = false;
        f.yearEndWork.disabled = false;
    } else {
        f.centerWorkCapio.disabled = true;
        f.monthIniWork.disabled = true;
        f.monthEndWork.disabled = true;
        f.yearIniWork.disabled = true;
        f.yearEndWork.disabled = true;
    }

    if (f.movility[f.movility.selectedIndex].value == '1') {
        f.cityMovility.disabled = false;
    } else {
        f.cityMovility.disabled = true;
    }

    var div = document.getElementById("div_comentarios");
    if (f.idUser.value != '') {
        div.style.display = 'block';
    } else {
        div.style.display = 'none';
    }

    var div2 = document.getElementById("div_vip");
    if (f.admin.value != '' && f.admin.value == '1') {
        div2.style.display = 'block';
    } else {
        div2.style.display = 'none';
    }
}

function validarFormulario(f) {

    if((f.email != null) &&  (!isValidEmail(f.email))) {
        alert("Ha d''introduir una adreça de c/e vàlida");
    }
    else if ((f.nif != null) && (!isValidNif(f.nif.value)) ) {
         alert('Debe introducir un numero identificativo valido.');
    }
    else if ((f.cp != null) && (!IsPostalCode(f.cp.value)) ) {
         alert('Debe introducir un codigo postal valido.');
    }
    else if ((isBadFormat(f.initYear)) || (!isInteger(f.initYear.value))) {
        alert("Ha d''introduir una data d''inici vàlida");
    }
    else if ((f.initYear.value < 1900) || (f.initYear.value > 2078)) {
        alert("Ha d''introduir una data d''inici vàlida");
    }
    else if ((isBadFormat(f.endYear)) || (!isInteger(f.endYear.value))) {
        alert("Ha d''introduir una data d''fin vàlida");
    }
     else if ((f.endYear.value < 1900) || (f.endYear.value > 2078)) {
         alert("Ha d''introduir una data d''fin vàlida");
    }
    else if((f.movility[f.movility.selectedIndex].value == '1') && (f.cityMovility.value == '0')) {
        alert('Debe introducir una ciudad de movilidad geográfica');
    } else if((f.workCapio[f.workCapio.selectedIndex].value == '1') && (f.centerWorkCapio.value == '0')) {
        alert('Debe introducir el centro del grupo Capio para el que trabajó');
    } else if((f.workCapio[f.workCapio.selectedIndex].value == '1') && ((f.monthIniWork.value == '') || (f.yearIniWork == '') || (!isInteger(f.yearIniWork.value))))  {
        alert('Debe introducir la fecha de inicio en la que trabajó para el grupo Capio, numerico y sin signos de puntuacion');
    } else if((f.workCapio[f.workCapio.selectedIndex].value == '1') && ((f.monthEndWork.value == '') || (f.yearEndWork == '') || (!isInteger(f.yearEndWork.value))))  {
        alert('Debe introducir la fecha de inicio en la que trabajó para el grupo Capio, numerico y sin signos de puntuacion');
    } else if (f.conditions.checked == false) {
        alert ('Debe aceptar las condiciones de politica y privacidad');
    } else if ((f.numColegiatura.value != "") && ((f.numColegiatura.value.length != 6) || (isInteger(f.numColegiatura.value) == false))) {
        alert("El número de colegiatura debe ser un código numérico de 6 dígitos")
    }
    else if (f.cursos.value.length > 500) {
        alert("El texto descriptorio de cursos es demasiado largo. Introduzca un texto mas corto.");
    }
    else if (f.titulacionesOtras.value.length > 500) {
        alert("El texto descriptorio de otras titulaciones es demasiado largo. Introduzca un texto mas corto.");
    }
    else if (f.descriptionExperience.value.length > 1000) {
        alert("El texto descriptorio de la experiencia es demasiado largo. Introduzca un texto mas corto.");
    }
    else if (f.curriculum.value.length > 4000) {
        alert("El texto descriptorio del curriculum es demasiado largo. Introduzca un texto mas corto.");
    }
    else {
        f.submit();
    }
}

function isBadFormat(s){
    if(s!= null && s.value!=null) {
        if(s.value == "") {
            return true;
        }
        if(!isNaN(s.value)) {
            return false;
        }
    }
    return true;
}

function hideLevel( _levelId ) {
    var thisLevel = document.getElementById( _levelId );
    if(thisLevel != null) {
        thisLevel.style.display = "none";
    }
}

function showLevel( _levelId ) {
    if ( _levelId != "null" ) {
        var thisLevel = document.getElementById( _levelId );
        if(thisLevel != null) {
            //alert(thisLevel.style);
            if ( thisLevel.style.display == "none") {
                thisLevel.style.display = "block";
            } else {
                hideLevel( _levelId);
            }
        }
    }
}

function changeTypeContact(f) {
    var v = f.typeContact.options[f.typeContact.selectedIndex].value;
    var sug = document.getElementById("tr_sugerencia");
    var inf = document.getElementById("tr_information");
    var env = document.getElementById("tr_enviar");
    if (v == '0') {
        sug.style.display = 'block';
        env.style.display = 'block';
        inf.style.display = 'none';
    } else if (v == '1') {
        sug.style.display = 'none';
        env.style.display = 'block';
        inf.style.display = 'block';
    } else {
        sug.style.display = 'none';
        env.style.display = 'none';
        inf.style.display = 'none';
    }
}

function isInteger(value){
    if (value.indexOf('.') >= 0) {
        return false;
    }
    if (isNaN(value)) {
        return false;
    }else{
        return true;
    }
}

function CleanNewsletter(t) {
    if(t.value == 'Introduce tu e-mail ')
        t.value="";
}

function ValidarNewsletter(f) {
    var t = f.email;
    if(t.value == 'Introduce tu e-mail ') {
       alert("Debe introducir su email")
    } else {
       f.submit();
    }
}

function isValidNif(s) {
    var letras=0;
    if (s.length==0)
    {
      return(false);
    }
    for (var i=0; i<s.length; i++)
    {
      var ch = s.substring (i, i+1);
      if ( (ch < "0" || ch > "9") && (ch < "a" || ch > "z") && (ch < "A" || ch > "Z") )
      {
        return(false);
      }
      if ( (ch >= "a" && ch <= "z") || (ch >= "A" && ch <= "Z") )
      {
        letras++;
      }
    }
    if (letras>2)
    {
      alert("El n&uacute;mero de documento identificativo facilitado no es correcto");
      return(false);
    }

    return(true)
}

function IsPostalCode(s)
{
if (s.length != 5) return false;
return IsUnsignedInteger(s);
}

function IsUnsignedInteger(number)
{
	if( number.search('[^0-9]') == -1 ) return true;
	return false;
}
var DaysInMonth = new Array()
DaysInMonth[0] = 31 //Los dias que tiene Enero
DaysInMonth[1] = 29 //Los dias que tiene Febrero se calculan mas adelante por si el año es bisiesto
DaysInMonth[2] = 31 //Los dias que tiene Marzo
DaysInMonth[3] = 30 //Los dias que tiene Abril
DaysInMonth[4] = 31 //Los dias que tiene Mayo
DaysInMonth[5] = 30 //Los dias que tiene Junio
DaysInMonth[6] = 31 //Los dias que tiene Julio
DaysInMonth[7] = 31 //Los dias que tiene Agosto
DaysInMonth[8] = 30 //Los dias que tiene Septiembre
DaysInMonth[9] = 31 //Los dias que tiene Octubre
DaysInMonth[10] = 30 //Los dias que tiene Noviembre
DaysInMonth[11] = 31 //Los dias que tiene Diciembre

//La funcion "IsDay(YourDay, YourMonth)" chequea si "YourDay" es un numero de dia valido para el mes "YourMonth"
//Las variables "YourDay" y "YourMonth" son cadenas de caracteres
function IsDay(YourDay, YourMonth)
{
if (IsUnsignedInteger(YourDay) && IsMonth(YourMonth)) //Si "YourDay" es un numero entero sin signo valido y "YourMonth" es un numero de mes valido
{
return (parseInt(YourDay) > 0 && parseInt(YourDay) <= DaysInMonth[YourMonth - 1]) ? 1 : 0 //Si "YourDay" esta entre 1 y los dias que tiene el mes "YourMonth" devuelve verdadero si no devuelve falso
}
else return 0 //Si "YourDay" no es un numero entero sin signo valido o "YourMonth" no es un numero de mes valido devuelve falso
}

function IsMonth(s)
{
    if (IsUnsignedInteger(s)) //Si "YourMonth" es un número entero sin signo valido
    {
        return (parseInt(s) > 0 && parseInt(s) <= 12) ? true : false
    }
    else
        return false
}

function IsYear(s)
{
if (IsUnsignedInteger(s))
{
    return (parseInt(s) > 1900 && parseInt(s) < 3000) ? true : false //Si "YourYear" es mayor que 1900 y menor que 3000 devuelve verdadero si no devuelve falso
}
    else
        return false
}

function IsLeapYear(s)
{
    if (IsUnsignedInteger(s)) //Si "YourYear" es un numero entero sin signo valido
    {
        return ((s % 4 == 0 && s % 100 != 0) || (s % 400 == 0)) ? true : false
    }
    else
        return false
}

function IsDate(fecha)
{
    var partesFecha = new Array()
    partesFecha = fecha.split("/")
    if (partesFecha.length != 3) return false;
    var Day = partesFecha[0]
    var Month = partesFecha[1]
    var Year = partesFecha[2]

    if (IsYear(Year) && !IsLeapYear(Year))
    {
        DaysInMonth[1] = 28
    }
    else if (IsYear(Year) && IsLeapYear(Year))
    {
        DaysInMonth[1] = 29
    }
    return (IsMonth(Month) && (IsDay(Day,Month)) )
}


