var titulo2;
var titulo;

function numerosOk() {
var ok="true";

return ok;
	
}
function echeck(str) {

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	
	
	
    if (lstr==0){
	
		
	   
	   return false
	}
			
	if (str.indexOf(at)==-1){
	   
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    
	    return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
	    
	    return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    
	    return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
	   
	    return false
	 }
	
	 if (str.indexOf(" ")!=-1){
	    
	    return false
	 }

		 return true					
}


function check_politicas() {
resultado = false
if ( $("#leido").is(':checked') ) resultado=true;

return resultado;
}

function enviar() {
	
	//alert($("#group5:checked").val());

	
	if ( (check_politicas()) && (numerosOk()) && ($("#email").val()!="") && ($("#name").val()!="") && ($("#address").val()!="") && ($("#cp").val()!="") && ($("#movil").val()!="") && ($("#last").val()!="") && (echeck($("#email").val())) ) {
	
		acepto = 0;
		if ( $("#acepto").is(':checked') ) acepto=1;
		
		nino1 = $("#group1:checked").val();
		nino2 = $("#group2:checked").val();
		nino3 = $("#group3:checked").val();
		nino4 = $("#group4:checked").val();
		nino5 = $("#group5:checked").val();
		nino6 = $("#group6:checked").val();
		
		
		nacimiento = $("#ano").val()+"-"+$("#mes").val()+"-"+$("#dia").val();
		
		fecha1 = $("#ano1").val()+"-"+$("#mes1").val()+"-"+$("#dia1").val();
		fecha2 = $("#ano2").val()+"-"+$("#mes2").val()+"-"+$("#dia2").val();
		fecha3 = $("#ano3").val()+"-"+$("#mes3").val()+"-"+$("#dia3").val();
		fecha4 = $("#ano4").val()+"-"+$("#mes4").val()+"-"+$("#dia4").val();
		fecha5 = $("#ano5").val()+"-"+$("#mes5").val()+"-"+$("#dia5").val();
		fecha6 = $("#ano6").val()+"-"+$("#mes6").val()+"-"+$("#dia6").val();
	
		ladata = "name=" + $("#name").val() + "&last=" + $("#last").val() + "&email=" + $("#email").val();
		ladata = ladata + "&address=" + $("#address").val() + "&address2=" + $("#address2").val() + "&cp=" + $("#cp").val();
		ladata = ladata + "&movil=" + $("#movil").val() + "&tel=" + $("#tel").val() + "&sex=" + $("#sex").val();
		ladata = ladata + "&dni=" + $("#dni").val() + "&nacimiento=" + nacimiento + "&acepto=" + acepto;
		ladata = ladata + "&fecha1=" + fecha1 + "&fecha2=" + fecha2 + "&fecha3=" + fecha3 + "&fecha4=" + fecha4 + "&fecha5=" + fecha5 + "&fecha6=" + fecha6;
		ladata = ladata + "&nino1=" + nino1 + "&nino2=" + nino2 + "&nino3=" + nino3 + "&nino4=" + nino4 + "&nino5=" + nino5 + "&nino6=" + nino6;	

		$.ajax({
			  type: "POST",
			  url: "registra.php",
			  data: ladata,
			  dataType: "script",
			  beforeSend: function(objeto){
				//$("#invitaciones").html("Mensaje: Cargando...");
				saving();
			  },
			  success: function(msg){
				//$("#invitaciones").animate({width:400, height:400}, "slow");
				//$("#invitaciones").html(msg);
				closeForm();
			  },
			  error: function(objeto, quepaso, otroobj){
				alert("ERROR!! : "+quepaso); 
				expandForm();
			  }
		});

		
	} else {
		alert("Error en el Email o Formulario incompleto!");
	}

}

function closeForm() {
	
	
    $("#formulario").animate({ 
        width: '0px',
        height: '0px',
        top: '190px',
        left: "430px",
        
        opacity: 0.9,
        marginLeft: "0in",
       
        borderWidth: "1px"
      }, 1 );
 
	
	
};

function formError(errortext) {
	
	
    $("#formulario").animate({ 
        width: '310px',
        height: '60px',
        top: '160px',
        left: "280px",
        
        opacity: 0.9,
        marginLeft: "0in",
       
        borderWidth: "1px"
      }, 500 );
    
    $("#email").css({'width' : '220px'});
	$("#titulo").css({'width' : '300px'});
	$("#titulo").css({'text-align':'center'});
	$("#titulo").html('<span style="color:white;font-size:12px;font-weight:bold;margin-top:40px;">'+errortext+'</span>');
	$("#titulo").css({'height':'40px'});	
	$("#titulo2").text("");
	$("#titulo").css({'visibility':'visible'});
	$("#titulo2").css({'visibility':'hidden'});
	$("#spinicio").css({'height':'0px'});
	
	
};

function saving() {
	
	
    $("#formulario").animate({ 
        width: '310px',
        height: '60px',
        top: '160px',
        left: "280px",
        
        opacity: 0.9,
        marginLeft: "0in",
       
        borderWidth: "1px"
      }, 500 );
    
    $("#email").css({'width' : '220px'});
	$("#titulo").css({'width' : '300px'});
	$("#titulo").css({'text-align':'center'});
	$("#titulo").html('<span style="color:white;font-size:12px;font-weight:bold;margin-top:40px;">saving data, thank you</span>');
	$("#titulo").css({'height':'40px'});	
	$("#titulo2").text("");
	$("#titulo").css({'visibility':'visible'});
	$("#titulo2").css({'visibility':'hidden'});
	$("#spinicio").css({'height':'0px'});
	
	
};

function cerrarForm(w,h,t,l) {
	
	
    $("#formulario").animate({ 
        width: w,
        height: h,
        top: t,
        left: l,
        
        opacity: 0.9,
        marginLeft: "0in",
       
        borderWidth: "1px"
      }, 500 );
    
	$("#titulo2").css({'visibility':'hidden'});
	$("#titulo2").css({'height':'0px'});
	$("#formulario").css({'opacity' : '0.9'}); 
	$("#email").css({'width' : '220px'});
	$("#titulo").css({'width' : '300px'});
	$("#titulo").css({'text-align':'center'});
	$("#titulo").css({'height':'30px'});
	$("#tname").css({'visibility':'hidden'});
	$("#name").css({'visibility':'hidden'});    
    

	$("#titulo").html(titulo);

	$("#titulo2").text("");
	$("#titulo").css({'visibility':'visible'});

	$("#spinicio").css({'height':'0px'});

	
};

function expandForm() {
    $("#tname").css({'visibility':'visible'});
    $("#name").css({'visibility':'visible'});
    $("#email").css({'width' : '300px'}); 
    $("#titulo").css({'visibility':'hidden'});
    $("#titulo").css({'height':'0px'});
    $("#titulo2").css({'visibility':'visible'});
    $("#titulo2").text(titulo2);
    $("#titulo").text("");
    $("#titulo").css({'height':'0px'});
    $("#spinicio").css({'height':'40px'});
    
    $("#formulario").animate({ 
      width: "780px",
      height: "320px",
      top: "20px",
      left: "30px",
      
      opacity: 0.9,
      marginLeft: "0in",
     
      borderWidth: "1px"
    }, 500 );	
	
}


  $(document).ready(function(){
	  
	titulo = $("#titulo").html();  
	titulo2 = $("#titulo").text();
	$("#titulo2").css({'visibility':'hidden'});
	$("#titulo2").css({'height':'0px'});
	$("#formulario").css({'opacity' : '0.9'}); 
	$("#email").css({'width' : '220px'});
	$("#titulo").css({'width' : '300px'});
	$("#titulo").css({'text-align':'center'});
	$("#titulo").css({'height':'30px'});
	$("#tname").css({'visibility':'hidden'});
	$("#name").css({'visibility':'hidden'});	
	
    // Using multiple unit types within one animation.
    $("#email").click(function(){
    	expandForm();
    });
    
     
   

  });
  