// Validation
function setLabel(i, m, s){
	var l = findObj(i.id + '_err'); if(!l){ l = document.createElement('span'); l.id = i.id + '_err'; var c = i.nextSibling; while(c && c.nextSibling) c = c.nextSibling; i.parentNode.insertBefore(l, c); } i.className = m == '' ? '' : (s ? s : (i.disabled ? 'disabledErro' : 'erro')); l.className = m == '' ? '' : (s ? s : 'erro'); l.innerHTML = m + ' ';
return(m == '' ? 0 : 1);
}
// Site Rules
Behaviour.register({
	'input.required': function(e){ e.onfocus = function(){ setLabel(this, ''); }; },
	'textarea.required': function(e){ e.onfocus = function(){ setLabel(this, ''); }; },
	'input#nome': function(e){ if(e.type == 'hidden') return; e.validate = function(){ setLabel(this, ''); return isEmpty(this, 'Nome'); }; },
	'select#dia': function(e){ if(e.type == 'hidden') return; e.validate = function(){ var f = this.form; return isDate(f.dia, f.mes, f.ano, 'Data de nascimento'); }; },
	'select#mes': function(e){ if(e.type == 'hidden') return; e.validate = function(){ var f = this.form; return isDate(f.dia, f.mes, f.ano, 'Data de nascimento'); }; },
	'select#ano': function(e){ if(e.type == 'hidden') return; e.validate = function(){ var f = this.form; return isDate(f.dia, f.mes, f.ano, 'Data de nascimento'); }; },
	'select#profissao': function(e){
		if(e.type == 'hidden') return;
		e.onchange = function(){
			e.form.conselho_reg.disabled = (this.selectedIndex == 0 || this.options[this.selectedIndex].text == 'Outra');
		};
		e.validate = function(){ return setLabel(this, this.selectedIndex <= 0 ? 'Profissão é obrigatória' : ''); };
	},
	'input#conselho_reg': function(e){
		if(e.type == 'hidden') return;
		if(e.form.profissao.selectedIndex == 0 || e.form.profissao.options[e.form.profissao.selectedIndex].text == 'Outra') e.disabled = true;
		e.validate = function(){
			this.form.action = this.disabled ? this.form.action.replace(/20/, '30') : this.form.action.replace(/30/, '20');
			setLabel(this, '');
		return this.disabled ? 0 : isEmpty(this, 'Conselho regional');
		};
	},
	'input#email': function(e){ if(e.type == 'hidden') return; e.validate = function(){ return isOk(this, 'E-mail', !isMail(this)); }; },
	'input#rg': function(e){ if(e.type == 'hidden') return; e.validate = function(){ setLabel(this, ''); return isEmpty(this, 'RG'); }; },

	'input#ddd1': function(e){ onlyNumbers(e); },
	'input#telefone1': function(e){ onlyNumbers(e); },

	'input#cpf': function(e){ if(e.type == 'hidden') return; onlyNumbers(e); e.validate = function(){ setLabel(this, ''); return isOk(this, 'CPF', !isCPF(this)); }; },
	'input#ddd': function(e){ if(e.type == 'hidden') return; onlyNumbers(e); e.validate = function(){ if(this.form.telefone.value != ''){ setLabel(this, ''); return isEmpty(this, 'DDD'); } }; },
	'input#telefone': function(e){ if(e.type == 'hidden') return; onlyNumbers(e); e.validate = function(){ setLabel(this, ''); this.form.ddd.validate(); return isEmpty(this, 'Telefone'); }; },

	'input#ddd1': function(e){ if(e.type == 'hidden') return; onlyNumbers(e); e.validate = function(){ return this.form.telefone1.value != '' ? isEmpty(this, 'DDD') : 0; }; },
	'input#telefone1': function(e){ if(e.type == 'hidden') return; onlyNumbers(e); e.validate = function(){ return this.form.ddd1.value != '' ? isEmpty(this, 'Telefone') : 0; }; },
/*
	'input#ddd_residencial': function(e){ if(e.type == 'hidden') return; onlyNumbers(e); e.validate = function(){ if(this.form.residencial.value != ''){ setLabel(this, ''); return isEmpty(this, 'DDD'); } }; },
	'input#residencial': function(e){ if(e.type == 'hidden') return; onlyNumbers(e); e.validate = function(){ setLabel(this, ''); this.form.ddd_residencial.validate(); return isEmpty(this, 'Telefone'); }; },
*/
	'input#cep': function(e){ if(e.type == 'hidden') return; onlyNumbers(e); e.onblur = function(skipAjax){ if(skipAjax == true) return isCEP(this, this.form.digito, ''); else doCEP(this, this.form.digito, '', true); } },
	'input#digito': function(e){ if(e.type == 'hidden') return; onlyNumbers(e); e.onblur = e.validate = function(skipAjax){ if(skipAjax == true) return isCEP(this.form.cep, this, ''); else doCEP(this.form.cep, this, ''); }; },
	'input#endereco': function(e){ if(e.type == 'hidden') return; e.validate = function(){ setLabel(this, ''); return isEmpty(this, 'Endereço'); }; },
	'input#numero': function(e){ if(e.type == 'hidden') return; e.validate = function(){ setLabel(this, ''); return isEmpty(this, 'Número'); }; },
	'input#bairro': function(e){ if(e.type == 'hidden') return; e.validate = function(){ setLabel(this, ''); return isEmpty(this, 'Bairro'); }; },
	'input#cidade': function(e){ if(e.type == 'hidden') return; e.validate = function(){ setLabel(this, ''); return isEmpty(this, 'Cidade'); }; },
	'select#estado': function(e){ if(e.type == 'hidden') return; e.validate = function(){ return isSelected(this, 'Estado'); }; },

	'input#cep_res': function(e){ if(e.type == 'hidden') return; onlyNumbers(e); e.onblur = function(skipAjax){ doCEP(this, this.form.digito_res, '_res', true); } },
	'input#digito_res': function(e){ if(e.type == 'hidden') return; onlyNumbers(e); e.onblur = function(skipAjax){ doCEP(this.form.cep_res, this, '_res'); } },
	/*
	'input#endereco_res': function(e){ if(e.type == 'hidden') return; e.validate = function(){ setLabel(this, ''); return isEmpty(this, 'Endereço'); }; },
	'input#numero_res': function(e){ if(e.type == 'hidden') return; e.validate = function(){ setLabel(this, ''); return isEmpty(this, 'Número'); }; },
	'input#bairro_res': function(e){ if(e.type == 'hidden') return; e.validate = function(){ setLabel(this, ''); return isEmpty(this, 'Bairro'); }; },
	'input#cidade_res': function(e){ if(e.type == 'hidden') return; e.validate = function(){ setLabel(this, ''); return isEmpty(this, 'Cidade'); }; },
	'select#estado_res': function(e){ if(e.type == 'hidden') return; e.validate = function(){ return isSelected(this, 'Estado'); }; },
*/
	'input#formacao[]': function(e){ e.validate = function(){	return isEmpty(this, 'Faculdade'); }; },
	'select#concluido[]': function(e){ e.validate = function(){ return isSelected(this, 'Ano'); }; },

	'input#utiliza': function(e){ e.validate = function(){ var checked = this.form.utiliza[0].checked || this.form.utiliza[1].checked; findObj('utiliza_err').style.display = checked ? 'none' : 'block'; return checked ? 0 : 1; }; },
	'select#desde': function(e){ e.validate = function(){ return this.form.utiliza[0].checked ? isSelected(this, 'Desde quando') : setLabel(this, ''); }; },

	'input#laser': function(e){ e.validate = function(){ var checked = this.form.laser[0].checked || this.form.laser[1].checked; findObj('laser_err').style.display = checked ? 'none' : 'block'; return checked ? 0 : 1; }; },
	'input#marca[1]': function(e){ e.validate = function(){
		var i = 0, checked = this.form.laser[0].checked == false ? true : false;
		while(!checked && typeof(this.form['marca[' + i + ']']) != 'undefined'){
			if(this.form['marca[' + i + ']'].checked)
				checked = true;
			i++;
		}
		findObj('aparelho_err').style.display = checked ? 'none' : 'block'; return checked ? 0 : 1;
		};
	},
	'textarea#mensagem': function(e){ if(e.type == 'hidden') return; e.validate = function(){ return isEmpty(this, 'Mensagem'); }; },
	'input#nome_associado': function(e){ e.validate = function(){ this.className = this.value == '' ? 'erro' : ''; return(this.value == '' ? 1 : 0); }; },
	'input#cpf_associado': function(e){ e.validate = function(){ this.className = this.value == '' ? 'erro' : ''; return(this.value == '' ? 1 : 0); }; },
	'input#nome_parente': function(e){ e.validate = function(){ this.className = this.value == '' ? 'erro' : ''; return(this.value == '' ? 1 : 0); }; },
	'input#cpf_parente': function(e){ e.validate = function(){ this.className = this.value == '' ? 'erro' : ''; return(this.value == '' ? 1 : 0); }; },
	'input#nome_assegurado': function(e){ e.validate = function(){ this.className = this.value == '' ? 'erro' : ''; return(this.value == '' ? 1 : 0); }; },
	'select#parentesco': function(e){ e.validate = function(){ this.className = this.selectedIndex == 0 ? 'erro' : ''; return(this.selectedIndex == 0 ? 1 : 0); }; },
	'form': function(e){ e.onsubmit = function(){ var e = 0; for(var i = 0, f = this.elements; i < f.length; i++) if(typeof(f[i].validate) != 'undefined'){ e += f[i].validate(true);} return(e == 0); }; }
});