// JavaScript Document
$(document).ready(function()
{
	
	var dataEvent =  $("#dataEvent").text();
	
	$("#form_cadastro").submit(function(){
	
		if ($("#email0").val() == '' && $("#email1").val() == '' && $("#email2").val() == '' && $("#email3").val() == '' && $("#nome0").val() == '' && $("#nome1").val() == '' && $("#nome2").val() == '' && $("#nome3").val() == '' ){
			
		//alert("preencha os campos");
		
		
			$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
					{ 
					//add message and change the class of the box and start fading
					$(this).html('Preencha ao menos um nome e e-mail!').addClass('messageboxerror').fadeTo(900,1);
					});		
		
		
		return false;
		
		}
	
	
	
		var total = 4;
		var nomeValores = [];
		var valores = []; 
	
	
		for(var i = 0; i < total; i++){
		    
			//alert(i);
			
		if ($("#nome"+i).val() != '' || $("#email"+i).val() != ''){
                 
				  if ($("#nome"+i).val() == ''){
					  
					$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
					{ 
					//add message and change the class of the box and start fading
					$(this).html('Por favor preencha corretamente o campo nome').addClass('messageboxerror').fadeTo(900,1);
					});		
					  return false;
					  }
					if($("#email"+i).val()== ''){
						
					$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
					{ 
					//add message and change the class of the box and start fading
					$(this).html('Por favor preencha corretamente o campo email').addClass('messageboxerror').fadeTo(900,1);
					});		

						
			
					removeAtributo();
					 return false;
					}else{
						//alert('entro no else');
						var d = document;
						nomeValores.push($("#nome"+i).val());
						valores.push($("#email"+i).val());
//alert("O campo " + d.getElementById("email"+i).value + " deve ser conter um endereco eletronico!")
	//var teste = d.getElementById("email1").value;
	
	
		if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(d.getElementById("email"+i).value))) {
		$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
					{ 
					//add message and change the class of the box and start fading
					$(this).html("O campo " + d.getElementById("email"+i).value + " deve conter um endereco eletronico válido!").addClass('messageboxerror').fadeTo(900,1);
					});	
		//document.id_form.campo_email.focus();
		return false;
	}
		
       
					}  
			
			
				 
			 		}else{
        		  
				 $("#nome"+i).attr("disabled","disabled");
				 $("#email"+i).attr("disabled","disabled");
				 
				// alert('no');
					};
				  
				
				}
	
	$("#msgbox").removeClass().addClass('messagebox').text('Verificando os dados ...').fadeIn(1000);
		//check the username exists or not from ajax
		$.post("cadastro_valida.php",{ nomes: nomeValores, emails: valores, dataEvent: dataEvent }, function(data)
        {
			//alert(data);
		  if(data=='yes') //if correct login detail
		  {
			  //alert('opa');
		  	$("#msgbox").fadeTo(200,0.1,function()  //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html('Enviado com sucesso!').addClass('messageboxok').fadeTo(900,1,
              function()
			  { 
			  	 //redirect to secure page
				 //document.location='lista_user.php';
				  removeAtributo();
				 $("#form_cadastro")[0].reset();
				 $("#msgbox").css("display","none");

			  });
			  
			});
		  }
		  else 
		  {
			   //alert('zuo');
			    removeAtributo();
		  	$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html(data).addClass('messageboxerror').fadeTo(900,1);
			});		
          }
				
        });
 		return false; //not to post the  form physically
	});


	
});


function removeAtributo(){
	
	 $("#form_cadastro input:text").removeAttr("disabled","disabled");
	
	}

