j = jQuery;
j(document).ready(function() {
	j('form.login').submit(function(){
		var formid = j(this).attr('id');
		var form = 'form[id='+formid+']';
		var action = j(this).attr('action');
		var params = j(form).serialize();
		var temerro = "";
		var validateMail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		j('input,textarea').removeClass('erro');
		j(form+' input').each(function(){
			if(!j(this).hasClass('pula') && j(this).is(':visible') && this.type != 'image' && this.type != 'submit' && this.type != 'radio'){
				if(this.value == this.defaultValue || this.value == ''){
					j(this).addClass('erro');
					temerro = 1;
				}
			}
			if(j(this).hasClass('email')){
				if(!validateMail.test(j(this).val())){
					j(this).addClass('erro');
					temerro = 1;
					if(j(this).prev().find('.erroemail').is(':hidden')){	
						j(this).prev().find('.erroemail').fadeIn();
						setTimeout(function () {
							j('.erroemail').fadeOut();
						}, 3000);
					}
				}
			}
		})
		if(temerro){
			return false; 
		}
		else{
			var formdimensoes = j(form);
			formdimensoes.removeAttr("width");
			formdimensoes.removeAttr("height");
			var largura = formdimensoes.width();
			var altura  = formdimensoes.height();
			j(form).addClass('formload');
			j(form).css({height:altura, width:largura});
			j(form+' fieldset').fadeOut();
			setTimeout(function () {
				j.ajax({
				data: params,
				type: 'POST',
				url: action,
				timeout: 15000,
				error: function(retorno){
					j(form).removeClass('formload');
					j(form).find('fieldset').fadeIn();
					},
				success: function(retorno){
						if(j(form).hasClass('recuperarsenha')){
							if(retorno == 'true'){
								j(form).removeClass('formload');
								j(form).find('#retorno1').fadeIn();
							}
							else{
								j(form).removeClass('formload');
								j(form).find('#retorno2').fadeIn();
							}
						}
						else{
							if(retorno != 'erro'){
								window.location="../area-restrita/";
							}
							else{
								j(form).removeClass('formload');
								j(form).find('.retorno').fadeIn();
							}
						}
					}
				})
			}, 2000);
		}
		return false;
	});
	j('.retorno a').click(function(){
		j(this).parent().fadeOut(function(){
			j(this).parent().parent().find('fieldset,.titform').fadeIn();
		});									
	})
});
