/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
var formErrors = new Array();

function isValidInt(userInput, errorMsg, isReq){
    var mask=/^\d+$/;
    if (isReq == false && userInput.value == "") return true;
    if (mask.test(userInput.value)) return true;
    formErrors[formErrors.length] = errorMsg;
    return false;
}

function isValidChar(userInput, errorMsg, isReq){
    var mask=/^[\w\-]+$/;
    if (isReq == false && userInput.value == "") return true;
    if (mask.test(userInput.value)) return true;
    formErrors[formErrors.length] = errorMsg;
    return false;
}

function isValidEmail(userInput, errorMsg, isReq){
    var mask=/^\w[\w\-\.]+\@\w[\w\-]+(\.[\w\-]+)+$/;
    if (isReq == false && userInput.value == "") return true;
    if (mask.test(userInput.value)) return true;
    formErrors[formErrors.length] = errorMsg;
    return false;
}

function isValidAny(userInput, errorMsg, isReq){
    if (isReq == true && userInput.value == "") {
        formErrors[formErrors.length] = errorMsg;
        return false;
    }
    return true;
}

function clearErr(id){
    var toClean = id+"_err";
    document.getElementById(toClean).style.color = "white";
    if (formErrors.length > 0){
        for (var i=0; i<formErrors.length; i++){
            if (formErrors[i] == toClean) {
                formErrors.splice(i, 1);
            }
        }
    }
}
function validate(){
    var bool = true;
    isValidAny(document.getElementById('nome'), 'nome_err', true);
    isValidAny(document.getElementById('apelido'), 'apelido_err', true);
    isValidAny(document.getElementById('numid'), 'numid_err', false);
    isValidInt(document.getElementById('nif'), 'nif_err', false);
    isValidAny(document.getElementById('passaporte'), 'passaporte_err', false);
    isValidAny(document.getElementById('aeroporto'), 'aeroporto_err', true);
    isValidAny(document.getElementById('data_nascimento'), 'data_nascimento_err', true);
    isValidAny(document.getElementById('local_nascimento'), 'local_nascimento_err', true);
    isValidAny(document.getElementById('pais'), 'pais_err', true);
    isValidAny(document.getElementById('endereco'), 'endereco_err', true);
    isValidAny(document.getElementById('telemovel'), 'telemovel_err', true);
    isValidAny(document.getElementById('telefone'), 'telefone_err', true);
    isValidInt(document.getElementById('fax'), 'fax_err', false);
    isValidEmail(document.getElementById('email'), 'email_err', true);
    isValidAny(document.getElementById('profissao'), 'profissao_err', true);
    isValidAny(document.getElementById('habilitacoes'), 'habilitacoes_err', true);
    isValidAny(document.getElementById('local_trabalho'), 'local_trabalho_err', true);
     isValidAny(document.getElementById('associacao'), 'associacao_err', false);
    isValidAny(document.getElementById('iniciativas'), 'iniciativas_err', false);
    var len = document.form_registo.residencia.length;
    var count = 0;
    for(var i =0 ; i < len;i ++ ){
        if (document.form_registo.residencia[i].checked) {
            isValidAny(document.form_registo.residencia[i].checked, 'residencia_err', true);
        }else{
            count++;
        }
    }
    if(count == len){
        formErrors[formErrors.length] = 'residencia_err';
    }
    if(!document.getElementById('declaracao').checked){
        formErrors[formErrors.length] = 'declaracao_err';
    }
    if (formErrors.length > 0){
        for (var i=0; i<formErrors.length; i++){
            document.getElementById(formErrors[i]).style.color="red";
        }
        bool =  false;
        document.getElementById("erros").innerHTML = "Por favor, preencha os campos assinalados.";
    }
    return bool;
}