var j = jQuery;
j(function(){
	j('form.validar').submit(function(){
		var idform = j(this).attr('id');
		var action = j(this).attr('action');
		var form = 'form[id='+idform+']';
		var params = j(form).serialize();
		var temerro = "";
		
		var validateMail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		//alert('form:'+idform+"\n"+'action:'+action+'\n'+params);
		j(form+' .erroinput').removeClass('erroinput');
		j(form+' .erroselect').removeClass('erroselect');
		j(form+' .errotextarea').removeClass('errotextarea');
		j(form+' .errospan').removeClass('errospan');
		j(form+' .errospan').removeClass('errospan');
		//j(form+' .campos .span1',form+' .campos .span2').hide();
		j(form+' input').each(function(){
			if(!j(this).hasClass('nulo') && !j(this).hasClass('hover') && j(this).is(':visible')){
				if(this.value == this.defaultValue || this.value == ""){
					if(j(this).hasClass('inpuSelect')){j(this).addClass('erroselect');}
					else{j(this).addClass('erroinput');}
					temerro = 1;
				}
				if(j(this).hasClass('email') && !validateMail.test(j(this).val())){
					j(this).addClass("erroinput");
					temerro = 1;
				}
				if(j(this).hasClass('checkbox') && !j(this).is(':checked')){
					j(this).next().addClass('errospan');
					temerro = 1;
				}
			}
			if(j(this).hasClass('regulamento')){
				if (this.value==""){
					j(this).next().addClass('errospan');
					temerro = 1;
				}
			}

		})
		j(form+' textarea').each(function(){
			if(!j(this).hasClass('null') && !j(this).hasClass('botao') && j(this).is(':visible')){
				if(this.value == this.defaultValue || this.value == ""){
					j(this).addClass('errotextarea');
					temerro = 1;
				}
			}
		})
		if(temerro){
			j(form+' .erroPreenchimento').fadeIn();
			//j(form+' .campos .span1').fadeIn();
			return false;
		}
		else {
			j.ajax({
			data: params,
			type: 'POST',
			url: action,
			timeout: 5000,
			error: function(){ alert("Houve algum erro"); },
			success: function(retorno){ //alert(retorno);
					//j("#formParticipe .erro").fadeOut();
					j(form+' fieldset,'+form+' .titform').fadeOut(function(){
						j(form+' .retorno').fadeIn();
						document.getElementById(idform).reset();
					});
					return false;
				}
			})
			return false;
		}
		return false;
	});
	j('.retorno a').click(function(){
		j(this).parent().fadeOut(function(){
			j(this).parent().parent().find('fieldset').fadeIn();
		});
		j('.erroPreenchimento').hide();
		j('.checkBox').removeClass('checkSelct');
	})
	
	

});
