// JavaScript Document
Cadastro = {
	envia:function(){
		if (this.serialize()) {
			var par;
			$("strerror").innerHTML = "";
			
			par = this.q.join("&");
			new Ajax.Request("cadastro.php", {
					method: 'post' 
					, parameters: par 		               					  			
					,	onComplete:function( txt ) {
							Element.update("strerror", txt.responseText);								 								
						}
				});
			
		}
	},
	valida:function(el){
		switch(el.name){
			case "nome":
			if (!el.value) return this.throwException('Informe o nome',el);
			break;
			case "atividade1":
			if (el.value!="OUTRA") atividade.value=el.value;
			case "atividade":
			if (!el.value && atividade1.value=="OUTRA") return this.throwException('Informe a outra atividade',el);
			break;
			case "data_nasc":
			if (!el.value) return this.throwException('Informe a data de nascimento',el);
			break;
			case "rg":
			if (!el.value) return this.throwException('Informe o RG/Identidade',el);
			break;	
			case "cod_pais":
			if (!el.value) return this.throwException('Informe o País',el);
			break;	
			case "cidade":
			if (!el.value) return this.throwException('Informe a Cidade',el);
			break;
			case "estado":
			if (!el.value) return this.throwException('Informe o Estado',el);
			break;
			case "endereco":
			if (!el.value) return this.throwException('Informe o Endereço',el);
			break;
			case "email":
			if (!(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(el.value)))
			return this.throwException('Informe um email válido',el);
			break;
			case "telefone":
			if (!el.value) return this.throwException('Informe o Telefone',el);
			break;
		}
		return true;
	},
	serialize:function(){
		var els = $("frmgeral").elements;
		this.q = [];
		for(var i=0,len=els.length;i<len;i++){
			this.q.push(els[i].name+"="+els[i].value);
			if (!this.valida(els[i])) return false;
		}
		return true;
	},
	throwException:function(error,obj){
		$("strerror").innerHTML = error;
		obj.focus();
		obj.select();
		return false;
	}
}
