/**
 * @author isidro
 */


function validaVoluntarioForm(nome, profissao, localidade, distrito, email, telefone){
	var nomeValido = true;
	var profissaoValido = true;
	var localidadeValido = true;
	var distritoValido = true;
	var emailValido = true;
	var telefoneValido = true;
	var msgErro = "";
	$('#voluntario_errorMsg').html(msgErro);
	$('#voluntario_errorMsg').removeClass("error");
	this.voluntario_nome = nome;
	this.voluntario_profissao = profissao;
	this.voluntario_localidade = localidade;
	this.voluntario_distrito = distrito;
	this.voluntario_email = email;
	this.voluntario_telefone = telefone;
	if (this.voluntario_nome == "" || this.voluntario_nome == "Nome") {
	    nomeValido = false;
	    $('#voluntario_nome').addClass('FieldError');
       msgErro+="Verifique o campo Nome.<br/>"
	}
	else{
	    $('#voluntario_nome').removeClass('FieldError');
	}
	if (this.voluntario_profissao == "" || this.voluntario_profissao == "Profissão") {
	    profissaoValido = false;
	    $('#voluntario_profissao').addClass('FieldError');
       msgErro+="Verifique o campo Profissão.<br/>"
	}
	else{
	    $('#voluntario_profissao').removeClass('FieldError');
	}
	if (this.voluntario_localidade == "" || this.voluntario_localidade == "Localidade") {
	    localidadeValido = false;
	    $('#voluntario_localidade').addClass('FieldError');
       msgErro+="Verifique o campo Localidade.<br/>"
	}
	else{
	    $('#voluntario_localidade').removeClass('FieldError');
	}
	if (this.voluntario_distrito == "" || this.voluntario_distrito == "Distrito") {
	    distritoValido = false;
	    $('#voluntario_distrito').addClass('FieldError');
       msgErro+="Verifique o campo Distrito.<br/>"
	}
	else{
	    $('#voluntario_distrito').removeClass('FieldError');
	}
	if (this.voluntario_email == "" || this.voluntario_email == "Email") {
	    emailValido = false;
	    $('#voluntario_email').addClass('FieldError');
       msgErro+="Verifique o campo Email.<br/>"
	}
	else{
		if(this.voluntario_email.match(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/)){
			emailValido = true;	
	        $('#voluntario_email').removeClass('FieldError');
		}
		else{
			emailValido = false;
           msgErro+="Verifique o campo Email.<br/>"
	        $('#voluntario_email').addClass('FieldError');
		}
	}
	if (this.voluntario_telefone == "" || this.voluntario_telefone == "Telefone ou Telemóvel") {
	    telefoneValido = false;
	    $('#voluntario_telefone').addClass('FieldError');
       msgErro+="Verifique o campo Telefone.<br/>"
	}
	else{
	    $('#voluntario_telefone').removeClass('FieldError');
	}	
	
	if (nomeValido == true && profissaoValido == true && localidadeValido == true && distritoValido == true && emailValido == true && telefoneValido == true) {
        return true;
    }
    
        
	$('#voluntario_errorMsg').html(msgErro);
	$('#voluntario_errorMsg').addClass('error');
	return false;
}
var frmSendVoluntarioIsEnabled = true;
function sendVoluntarioForm(){
    debugger;
   if(frmSendVoluntarioIsEnabled==true && validaVoluntarioForm( $('#voluntario_nome').val(), $('#voluntario_profissao').val(), $('#voluntario_localidade').val(), $('#voluntario_distrito').val(), $('#voluntario_email').val(), $('#voluntario_telefone').val())){
       $.ajax({
           type: "POST",
           url: "SaveVoluntarioFormData.aspx",
           data: "voluntario_nome=" + $('#voluntario_nome').val() + "&voluntario_profissao=" + $('#voluntario_profissao').val() + "&voluntario_localidade=" + $('#voluntario_localidade').val() + "&voluntario_distrito=" + $('#voluntario_distrito').val() + "&voluntario_email=" + $('#voluntario_email').val() + "&voluntario_telemovel=" + $('#voluntario_telemovel').val() + "&voluntario_newsletter=" + $('#voluntario_newsletter:checked').val() + "&voluntario_tempacemaker="+$('#voluntario_tempacemaker:checked').val(),
           beforeSend: function(){frmSendVoluntarioIsEnabled = false; $('#preloaderVoluntario').css('display', 'block');},
           success: function(msg){
               $('#voluntarioFormSuccess').html(msg);
               $('#voluntarioForm').hide();
               $('#voluntarioFormSuccess').fadeIn();
               frmSendVoluntarioIsEnabled = true;
               $('#preloaderVoluntario').css('display', 'none');
           },
           error: function(msg){
               alert(msg);
               frmSendVoluntarioIsEnabled = true;
               $('#preloaderVoluntario').css('display', 'none');
           }
       });
   }
}

