$(document).ready(function(){ 
 
//// ValidaciÑn de campos (al salir del foco)
         
	jQuery("#nombre").blur(function (){
		var nombre = $('#nombre').val();
		if(nombre.length != 0){             
            $('#nombre_error').fadeOut("slow");
        } else {
            var error = true;
            $('#nombre_error').fadeIn("slow");
        }
	});
                
	jQuery("#apellidos").blur(function (){
		var apellidos = $('#apellidos').val();
		if(apellidos.length != 0){             
            $('#apellidos_error').fadeOut("slow");
        } else {
            var error = true;
         	$('#apellidos_error').fadeIn("slow");
        }
	});

	jQuery("#direccion").blur(function (){
		var direccion = $('#direccion').val();
		if(direccion.length != 0){             
            $('#dir_error').fadeOut("slow");
        } else {
            var error = true;
            $('#dir_error').fadeIn("slow");
        }
	});
                
	jQuery("#cp").blur(function (){
		var cp = $('#cp').val();
		if(cp.length != 0){  
     		if (isNaN(cp)) { 
            	$('#cp_error').fadeIn("slow");
     		}else{ 
            	$('#cp_error').fadeOut("slow");
     		} 		           
        } else {
            var error = true;
         	$('#cp_error').fadeIn("slow");
        }
	});


	jQuery("#poblacion").blur(function (){
		var poblacion = $('#poblacion').val();
		if(poblacion.length != 0){             
            $('#poblacion_error').fadeOut("slow");
        } else {
            var error = true;
            $('#poblacion_error').fadeIn("slow");
        }
	});
                
	jQuery("#provincia").blur(function (){
		var provincia = $('#provincia').val();
		if(provincia.length != 0){             
            $('#provincia_error').fadeOut("slow");
        } else {
            var error = true;
         	$('#provincia_error').fadeIn("slow");
        }
	});

	jQuery("#telefono").blur(function (){
		var telefono = $('#telefono').val();
		if(telefono.length != 0){  
     		if (isNaN(telefono)) { 
            	$('#telefono_error').fadeIn("slow");
     		}else{ 
            	$('#telefono_error').fadeOut("slow");

     		} 		           
        } else {
            var error = true;
         	$('#telefono_error').fadeIn("slow");
        }
	});	
                
	jQuery("#mail").blur(function (){
		var mail = $('#mail').val();
        if(mail.length == 0 || mail.indexOf('@') == '-1' || mail.indexOf(".") == '-1'){
             var error = true;
             $('#mail_error').fadeIn("slow");
        }else{
             $('#mail_error').fadeOut("slow");
        
       			var username = $(this).val();
          		var status=$("#status");
          		
		          if(username.length > 0){
        		    $.ajax({
              		type:"POST",
              		url:"http://www.expert.es/philips/includes/checking.php",
              		data:"mail="+username,
              		dataType:"json",
              		beforeSend:function(){
              		},
              			success:function(response){
                  			if(response==false){
                    			status.addClass("checked");
  								$('#mail_error').fadeOut("slow");
  	            				$('#mail_error2').fadeIn("slow"); 
  	            				$('#marca').val("1");
                  			}else{
                    			status.addClass("error");
                    			$('#mail_error').fadeOut("slow");
                    			$('#mail_error2').fadeOut("slow");
              					$('#marca').val("2");

                 			}
              			}
            		});
          		  }else{
              		status.html("Ingrese un usuario");
          		  }
       		}	
     });
                       
                	
       
//// ValidaciÑn de todos los campos (al hacer click boton formulario)
        $('#enviarRegistro').click(function(e){ 
            e.preventDefault();

            var error = false;
            var nombre = $('#nombre').val();
            var apellidos = $('#apellidos').val();
            var cp = $('#cp').val();
            var direccion = $('#direccion').val();
            var poblacion = $('#poblacion').val();
            var provincia = $('#provincia').val();
            var telefono = $('#telefono').val();
            var mail = $('#mail').val();
            var marca = $('#marca').val();
            var bases = $('#bases').is(':checked');

            
		var nombre = $('#nombre').val();
		if(nombre.length != 0){             
            $('#nombre_error').fadeOut("slow");
        } else {
            var error = true;
            $('#nombre_error').fadeIn("slow");
        }
                
		var apellidos = $('#apellidos').val();
		if(apellidos.length != 0){             
            $('#apellidos_error').fadeOut("slow");
        } else {
            var error = true;
         	$('#apellidos_error').fadeIn("slow");
        }

		var direccion = $('#direccion').val();
		if(direccion.length != 0){             
            $('#dir_error').fadeOut("slow");
        } else {
            var error = true;
            $('#dir_error').fadeIn("slow");
        }
                
		var cp = $('#cp').val();
		if(cp.length != 0){  
     		if (isNaN(cp)) { 
            	$('#cp_error').fadeIn("slow");
     		}else{ 
            	$('#cp_error').fadeOut("slow");
     		} 		           
        } else {
            var error = true;
         	$('#cp_error').fadeIn("slow");
        }

		var poblacion = $('#poblacion').val();
		if(poblacion.length != 0){             
            $('#poblacion_error').fadeOut("slow");
        } else {
            var error = true;
            $('#poblacion_error').fadeIn("slow");
        }
                
		var provincia = $('#provincia').val();
		if(provincia.length != 0){             
            $('#provincia_error').fadeOut("slow");
        } else {
            var error = true;
         	$('#provincia_error').fadeIn("slow");
        }

		var telefono = $('#telefono').val();
		if(telefono.length != 0){  
     		if (isNaN(telefono)) { 
            	$('#telefono_error').fadeIn("slow");
     		}else{ 
            	$('#telefono_error').fadeOut("slow");

     		} 		           
        } else {
            var error = true;
         	$('#telefono_error').fadeIn("slow");
        }
                
		var mail = $('#mail').val(); 
        if(mail.length == 0 || mail.indexOf('@') == '-1' || mail.indexOf(".") == '-1'){
             var error = true;
             $('#mail_error').fadeIn("slow");
        }else{
             $('#mail_error').fadeOut("slow");
        
       			var username = $(this).val();
          		var status=$("#status");
          		
		          if(username.length > 0){
        		    $.ajax({
              		type:"POST",
              		url:"http://www.expert.es/philips/includes/checking.php",
              		data:"mail="+username,
              		dataType:"json",
              		beforeSend:function(){
                  		status.html("<img src='../images/loading.gif'/>");
              		},
              			success:function(response){
                  			if(response==false){
                    			status.addClass("checked");
  								$('#mail_error').fadeOut("slow");
  	            				$('#mail_error2').fadeIn("slow"); 
  	            				$('#marca').val("1");
                  			}else{
                    			status.addClass("error");
                    			$('#mail_error').fadeOut("slow");
                    			$('#mail_error2').fadeOut("slow");
              					$('#marca').val("2");

                 			}
              			}
            		});
          		  }else{
              		status.html("Ingrese un usuario");
          		  }
       		}	

        	if(bases == false){
            	var error = true;
                $('#bases_error').fadeIn("slow");
       		}else{
                $('#bases_error').fadeOut("slow");
        	}        
       
           if(nombre.length == 0){
              var error = true; 
            	}else{ 
            	if(apellidos.length == 0){
            		var error = true;
            	}else{
            		if(direccion.length == 0){
		            	var error = true;
            		}else{ 
            			if(marca == 1){
		            		var error = true; 
		              	}else{    
            				if(telefono.length == 0){ 
		            			var error = true;  
		            		}else{ 
								if(poblacion.length == 0){ 
		            				var error = true;  
		            			}else{ 	  
		            				if(provincia.length == 0){ 
		            					var error = true;  
		            				}else{ 	            		
		            							if(bases == false){
		            								var error = true;
		            								}else{ 	
		            										var error = false;
		            				 						if(error == false){ 
     
    var dataString = 	'nombre='+ nombre + 
    					'&apellidos=' + apellidos + 
    					'&provincia=' + provincia +  
    					'&poblacion=' + poblacion + 
    					'&direccion=' + direccion +
    					'&telefono=' + telefono +
    					'&cp=' + cp + 
    					'&mail=' + mail;  
    
    
    
    $.ajax({      
      type: "POST",  
      url: "http://www.expert.es/philips/includes/enviarDatos.php",  
      data: dataString,  
      success: function() {
		jQuery.getScript('http://www.expert.es/publico/js/svfobject2.js', function() {
				 		$('#contDesp').hide("slow");	
				 		$('#Mensajetxt02').show("slow");
			
			});
		} 			  
      });  
    return false;  

       }}}}}}}}}
        
        });

          
        
         });
