function fefm_cer_documento_identificacao_tipo(elem){
	/*elem.value possible values:
	 * "" - escolher
	 * "bi" - B.I.
	 * "cc" - Cartão do Cidadão
	 * "passaporte" - Passaporte
	 * "autorizacao_residencia" - Autorização de Residência
	 * */
	switch (elem.value) {
	case "cc":
		document.getElementById('td_doc_id_data').innerHTML = 'Data de Validade';
		document.getElementById('td_doc_id_local').style.display = 'none';
		document.getElementById('td_doc_id_local_input').style.display = 'none';
		document.getElementById('cer_documento_identificacao_emissao_local').value = ' ';

		break;

	default:
		document.getElementById('td_doc_id_data').innerHTML = 'Data de Emissão';
		document.getElementById('td_doc_id_local').style.display = '';
		document.getElementById('td_doc_id_local_input').style.display = '';
		document.getElementById('cer_documento_identificacao_emissao_local').value = '';
		break;
	}
	//alert(elem.value);
}

function fefmvalidate() {
    //validate required
    var afields = ['nome', 'email', 'contacto', 'organizacao', 'funcao',
                    'nome_f', 'contribuinte', 'morada_f', 'cp1_f', 'cp2_f', 'localidade_f',
                    'pais_f', 'nome_m', 'morada_m', 'cp1_m', 'cp2_m', 'localidade_m',
                    'pais_m', 'cer_naturalidade', 'cer_nacionalidade', 'cer_data_nascimento',
                    'cer_documento_identificacao_tipo',
                    'cer_documento_identificacao_numero', 'cer_documento_identificacao_emissao_local',
                    'cer_documento_identificacao_emissao_data'];
    var anames = ['Nome', 'E-mail', 'Contacto', 'Organização', 'Função',
                    'Nome', 'Contribuinte', 'Morada', 'Código Postal', 'Código Postal', 'Localidade',
                    'País', 'Nome', 'Morada', 'Código Postal', 'Código Postal', 'Localidade',
                    'País', 'Naturalidade', 'Nacionalidade', 'Data de Nascimento',
                    'Tipo de documento de Identificação',
                    'Nº Documento de Identificação', 'Local de emissão',
                    'Data de emissão'];
    for(i=0; i<afields.length; i++) {
        field = document.getElementById(afields[i]);
        if(field.value=='') {
            alert('O campo ' + anames[i] + ' é de preenchimento obrigatório.');
            return false;
        }
    }
    //validate contacto
    var contacto = document.getElementById('contacto').value;
    if(!validphone(contacto)) {
        alert('Contacto inválido.');
        return false;
    }
    //validate sexo
    var sexom = document.getElementById('sexom');
    var sexof = document.getElementById('sexof');
    if(sexom.checked==false && sexof.checked==false) {
        alert('O campo sexo é de preenchimento obrigatório..');
        return false;
    }
    //validate cps
    var cp1_f = document.getElementById('cp1_f').value;
    var cp1_m = document.getElementById('cp1_m').value;
    if(cp1_f.length<4 || cp1_m.length<4) {
        alert('Código Postal inválido.');
        return false;
    }
    var cp2_f = document.getElementById('cp2_f').value;
    var cp2_m = document.getElementById('cp2_m').value;
    if(cp2_f.length<3 || cp2_m.length<3) {
        alert('Código Postal inválido.');
        return false;
    }
    //validate email
    var email = document.getElementById('email').value;
    var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
    if(!email.match(emailExp)) {
        alert('E-mail inválido.');
        return false;
    }
    //validate contribuinte
    var contribuinte = document.getElementById('contribuinte').value;
    if(!validcontribuinte(contribuinte)) {
        alert('Contribuinte inválido.');
        return false;
    }
    //validate cer_data_nascimento
    var cer_data_nascimento = document.getElementById('cer_data_nascimento').value;
    if(!validdate(cer_data_nascimento)) {
        alert('Data de Nascimento inválida.');
        return false;
    }
    //validate cer_documento_identificacao_emissao_data
    var cer_documento_identificacao_emissao_data = document.getElementById('cer_documento_identificacao_emissao_data').value;
    if(!validdate(cer_documento_identificacao_emissao_data)) {
        alert('Data de Emissão inválida.');
        return false;
    }
    //validate forma_pagamento
    var forma_pagamento = document.getElementById('forma_pagamento');
    if(!forma_pagamento.checked) {
        alert('Não foi seleccionada nenhuma forma de pagamento.');
        return false;
    }
    return true;
}

function validdate(string) {
    if(string=="" || string=="AAAA-MM-DD" || string.length<9)
        return false;
    return true;
}

function copyaddress() {
    var name_f = document.getElementById('nome_f');
    var name_m = document.getElementById('nome_m');
    var add_f = document.getElementById('morada_f');
    var add_m = document.getElementById('morada_m');
    var cp1_f = document.getElementById('cp1_f');
    var cp1_m = document.getElementById('cp1_m');
    var cp2_f = document.getElementById('cp2_f');
    var cp2_m = document.getElementById('cp2_m');
    var localidade_f = document.getElementById('localidade_f');
    var localidade_m = document.getElementById('localidade_m');
    var pais_f = document.getElementById('pais_f');
    var pais_m = document.getElementById('pais_m');
    var checkcopyaddress = document.getElementById('checkcopyaddress');
    if(checkcopyaddress.checked) {
        name_m.value = name_f.value;
        add_m.value = add_f.value;
        cp1_m.value = cp1_f.value;
        cp2_m.value = cp2_f.value;
        localidade_m.value = localidade_f.value;
        pais_m.value = pais_f.value;
    } else {
        name_m.value = '';
        add_m.value = '';
        cp1_m.value = '';
        cp2_m.value = '';
        localidade_m.value = '';
        pais_m.value = '';
    }
}
