/*############################################################################################################################*/
/* VALIDA EMAILS DIGITADOS NOS FORMULARIOS */
function valida(mail){
    var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
    if(typeof(mail) == "string"){
        if(er.test(mail)){ return true; }
    }else if(typeof(mail) == "object"){
        if(er.test(mail.value)){ 
          return true;}
    }else{
        return false; }}
/* VALIDA EMAILS DIGITADOS NOS FORMULARIOS */
/*############################################################################################################################*/
function ValidaCPF() {
var nform = document.verificaCPF
	if (nform.cpfComprador.value == "") {
		showDialog('Erro','Por favor, insira os dados do seu CPF.','error');
		return false;
	}
}
/*############################################################################################################################*/
function ValidaDados() {
var nform = document.cadastroDados
	if (nform.nomeCadastro.value == "") {
		showDialog('Erro','Por favor, insira seu Nome.','error');
		return false;
	}
	if (nform.cpfCadastro.value == "") {
		showDialog('Erro','Por favor, insira os dados do seu CPF.','error');
		return false;
	}
	
	if(nform.tipoUsuario.value != "Comum"){
			if(nform.tipoUsuario.value == "Estudante"){
				if (nform.instituicaoEnsino.value == '') {
					showDialog('Erro','Por favor, insira o Nome da Instituição de ensino.','error');
					return false;
				}
				if (nform.cursoCadastro.value == '') {
					showDialog('Erro','Por favor, insira o Curso.','error');
					return false;
				}
				if (nform.matriculaCadastro.value == '') {
					showDialog('Erro','Por favor, insira o Número de matrícula.','error');
					return false;
				}
			}
			else if(nform.tipoUsuario.value == "Associado"){
				if (nform.organizacao.value == '') {
					showDialog('Erro','Por favor, insira a Organização na qual trabalha.','error');
					return false;
				}
				if (nform.cargo.value == '') {
					showDialog('Erro','Por favor, insira o Cargo.','error');
					return false;
				}
			}
		}
	
	var emailCadastro = document.getElementsByName('emailCadastro')[0];
	if (!valida(emailCadastro)) {
		showDialog('Erro','Por favor, insira seu E-mail.','error');
		return false;
	}
	if (nform.telefone.value == "") {
		showDialog('Erro','Por favor, insira o Telefone para contato.','error');
		return false;
	}
	if (nform.datanascimento.value == "") {
		showDialog('Erro','Por favor, insira a Data de seu Nascimento.','error');
		return false;
	}
	if (nform.cidade.value == "") {
		showDialog('Erro','Por favor, insira sua Cidade.','error');
		return false;
	}
	if (nform.estado.value == "--") {
		showDialog('Erro','Por favor, escolha seu Estado.','error');
		return false;
	}
	if (nform.conhecimento.value == "--") {
		showDialog('Erro','Por favor, escolha como tomou conhecimento do evento.','error');
		return false;
	}
	if (nform.loginInscricao.value == "") {
		showDialog('Erro','Por favor, insira seu Login para acesso.','error');
		return false;
	}
	if (nform.senhaInscricao.value == "") {
		showDialog('Erro','Por favor, insira a Senha para acesso.','error');
		return false;
	}
	if (nform.resenhaInscricao.value != nform.senhaInscricao.value) {
		showDialog('Erro','Por favor, insira novamente a Senha informada no campo anterior.','error');
		return false;
	}
	
}
/*############################################################################################################################*/
function ValidaContato() {
var nform = document.enviaContato
	if (nform.nomeContato.value == "") {
		showDialog('Erro','Por favor, insira seu Nome.','error');
		return false;
	}
	var emailContato = document.getElementsByName('emailContato')[0];
	if (!valida(emailContato)) {
		showDialog('Erro','Por favor, insira seu E-mail.','error');
		return false;
	}
	if (nform.assuntoContato.value == "") {
		showDialog('Erro','Por favor, escolha o Assunto.','error');
		return false;
	}
	if (nform.mensagemContato.value == "") {
		showDialog('Erro','Por favor, insira a Mensagem.','error');
		return false;
	}
}
/*############################################################################################################################*/
function ValidaAmigo() {
var nform = document.enviaAmigo
	if (nform.seuNome.value == "") {
		showDialog('Erro','Por favor, insira seu Nome.','error');
		return false;
	}
	var seuEmail = document.getElementsByName('seuEmail')[0];
	if (!valida(seuEmail)) {
		showDialog('Erro','Por favor, insira seu E-mail.','error');
		return false;
	}
	if (nform.nomeAmigo1.value == "") {
		showDialog('Erro','Por favor, insira o Nome do seu amigo.','error');
		return false;
	}
	var emailAmigo1 = document.getElementsByName('emailAmigo1')[0];
	if (!valida(emailAmigo1)) {
		showDialog('Erro','Por favor, insira o E-mail do seu amigo.','error');
		return false;
	}
	
}
/*############################################################################################################################*/
function Camposform(valor){
	if(valor == "Estudante"){
		document.getElementById('organizacao').style.display = "none";
		document.getElementById('cargo').style.display = "none";
		document.getElementById('associado1').style.display = "none";
		document.getElementById('associado2').style.display = "none";

		document.getElementById('instituicaoEnsino').style.display = "";
		document.getElementById('cursoCadastro').style.display = "";
		document.getElementById('matriculaCadastro').style.display = "";
		document.getElementById('trEstudante1').style.display = "";
		document.getElementById('trEstudante2').style.display = "";
		document.getElementById('trEstudante3').style.display = "";
	}
	else if(valor == "Associado"){
		document.getElementById('organizacao').style.display = "";
		document.getElementById('cargo').style.display = "";
		document.getElementById('associado1').style.display = "";
		document.getElementById('associado2').style.display = "";
	
		document.getElementById('instituicaoEnsino').style.display = "none";
		document.getElementById('cursoCadastro').style.display = "none";
		document.getElementById('matriculaCadastro').style.display = "none";
		document.getElementById('trEstudante1').style.display = "none";
		document.getElementById('trEstudante2').style.display = "none";
		document.getElementById('trEstudante3').style.display = "none";
	}
}

function Escondeareas()
{
		document.getElementById('instituicaoEnsino').style.display = "none";
		document.getElementById('cursoCadastro').style.display = "none";
		document.getElementById('matriculaCadastro').style.display = "none";
		document.getElementById('trEstudante1').style.display = "none";
		document.getElementById('trEstudante2').style.display = "none";
		document.getElementById('trEstudante3').style.display = "none";
}

function ValidaComentario() {
var nform = document.comentarioPost
	if (nform.nomeComentario.value == "") {
		showDialog('Erro','Por favor, insira seu Nome.','error');
		return false;
	}
	var emailComentario = document.getElementsByName('emailComentario')[0];
	if (!valida(emailComentario)) {
		showDialog('Erro','Por favor, insira seu E-mail.','error');
		return false;
	}
	if (nform.msgComentario.value == "") {
		showDialog('Erro','Por favor, insira a Mensagem.','error');
		return false;
	}
}


//** Form field Limiter v2.0- (c) Dynamic Drive DHTML code library: http://www.dynamicdrive.com
//** Feb 25th, 09'- Script creation date
//** This notice must stay intact for legal use

var fieldlimiter={

defaultoutput: "<b>[int]</b> caracteres restantes.", //default message that gets output to statusid element

uncheckedkeycodes: /(8)|(13)|(16)|(17)|(18)/, //keycodes that are not checked, even when limit has been reached. See http://www.javascriptkit.com/jsref/eventkeyboardmouse.shtml for avail keycodes

limitinput:function(e, config){
	var e=window.event || e
	var thefield=config.thefield
	var keyunicode=e.charCode || e.keyCode
	if (!this.uncheckedkeycodes.test(keyunicode)){
		if (thefield.value.length>=config.maxlength){
			if (e.preventDefault)
				e.preventDefault()
			return false
		}
	}
},

showlimit:function(config){
	var thefield=config.thefield
	var statusids=config.statusids
	var charsleft=config.maxlength-thefield.value.length
	if (charsleft<0) //if user has exceeded input limit (possible if cut and paste text into field)
		thefield.value=thefield.value.substring(0, config.maxlength) //trim input
	for (var i=0; i<statusids.length; i++){
		var statusdiv=document.getElementById(statusids[i])
		if (statusdiv) //if status DIV defined
			statusdiv.innerHTML=this.defaultoutput.replace("[int]", Math.max(0, charsleft))
	}
	config.onkeypress.call(thefield, config.maxlength, thefield.value.length)
},

cleanup:function(config){
	for (var prop in config){
		config[prop]=null
	}
},


addEvent:function(targetarr, functionref, tasktype){
	if (targetarr.length>0){
		var target=targetarr.shift()
		if (target.addEventListener)
			target.addEventListener(tasktype, functionref, false)
		else if (target.attachEvent)
			target.attachEvent('on'+tasktype, function(){return functionref.call(target, window.event)})
		this.addEvent(targetarr, functionref, tasktype)
	}
},

setup:function(config){
	if (config.thefield){ //if form field exists
		config.onkeypress=config.onkeypress || function(){}
		config.thefield.value=config.thefield.value
		this.showlimit(config)
		this.addEvent([window], function(e){fieldlimiter.showlimit(config)}, "load")
		this.addEvent([window], function(e){fieldlimiter.cleanup(config)}, "unload")		
		this.addEvent([config.thefield], function(e){return fieldlimiter.limitinput(e, config)}, "keypress")
		this.addEvent([config.thefield], function(){fieldlimiter.showlimit(config)}, "keyup")
	}
}

}