//Inicializamos JQuery
$(document).ready(function(){
    var completo = "";
    
    var prov = $('#provincias');
    var pob = $('#poblacion');
    var sec = $('#seccion');
    var tipo = $('#tipo');
    var web = $('#web');
    var usuario = $('#usuario');
    var medidas = $('#medidas');
    var comunidad = $('#comunidad');
    var seccion_web = $('#seccion_web');
    var id = $('#frmBaner #id');
    
    //Funciones ajax para baners
    $('#pais').change(function(){
        var t = this;
        $.ajax({
            url: '?seccion=baner',
            data: 'action=pais&v=' + t.value,
            dataType: 'html',
            type: 'get',
            success: function(j){
                $('#comunidad').html(j);
                
                $.ajax({
                    url: '?seccion=baner',
                    data: 'action=comunidad&v=',
                    dataType: 'html',
                    type: 'get',
                    success: function(j){
                        $('#provincias').html(j);
                        
                        $.ajax({
                            url: '?seccion=baner',
                            data: 'action=provincia&v=',
                            dataType: 'html',
                            type: 'get',
                            success: function(j){
                                $('#poblacion').html(j);
                            }
                        });
                    }
                });
            }
        });
    });
    
    $('#comunidad').change(function(){
        var t = this;
        $.ajax({
            url: '?seccion=baner',
            data: 'action=comunidad&v=' + t.value,
            dataType: 'html',
            type: 'get',
            success: function(j){
                $('#provincias').html(j);
                $.ajax({
                    url: '?seccion=baner',
                    data: 'action=provincia&v=',
                    dataType: 'html',
                    type: 'get',
                    success: function(j){
                        $('#poblacion').html(j);
                    }
                });
            }
        });
    });
    
    
    $('#frmBaner #provincias').change(function(){
        var t = this;
        $.ajax({
            url: '?seccion=baner',
            data: 'action=provincia&v=' + t.value,
            dataType: 'html',
            type: 'get',
            success: function(j){
                $('#poblacion').html(j);
            }
        });
    });
    
    
    $('#usuario').change(function(){
        var t = this;
        $.ajax({
            url: '?seccion=baner',
            data: 'action=usuario&v=' + t.value,
            dataType: 'html',
            type: 'get',
            success: function(j){
                $('#referencia_baner').html(j);
            }
        });
    });
    
    $('#selusuario').change(function(){
        var t = this;
        
        $.ajax({
            url: '?seccion=baner',
            data: 'action=selusuario&v=' + t.value,
            dataType: 'html',
            type: 'get',
            success: function(j){
                $('#ref_baner').html(j);
            }
        });
        
        
        $.ajax({
            url: '?seccion=baner',
            data: 'action=selusuario1&v=' + t.value,
            dataType: 'html',
            type: 'get',
            success: function(j){
                $('#ref_anuncio').html(j);
            }
        });
        $.ajax({
            url: '?seccion=baner',
            data: 'action=selusuario2&v=' + t.value,
            dataType: 'html',
            type: 'get',
            success: function(j){
                $('#usuarios_web').html(j);
            }
        });
        
        
        $.ajax({
            url: '?seccion=baner',
            data: 'action=selusuario3&v=' + t.value,
            dataType: 'html',
            type: 'get',
            success: function(j){
                $('#referencia_baner').html(j);
            }
        });
        
    });
    
    $('#referencia_baner').change(function(){
        var t = this;
        var u = $('#frmBaner #usuario').val();
        //alert(t.options[t.selectedIndex].value);
        if (t.options[t.selectedIndex].value != "") {
        
            $.ajax({
                url: '?seccion=baner',
                data: 'action=referencia2&v=' + t.value + '&usuario=' + u,
                dataType: 'html',
                type: 'get',
                success: function(j){
                    $('#medidas').removeAttr("disabled");
                    $('#medidas').html(j);
                    $('#medidas').attr("disabled", "disabled");
                }
            });
        }
        $.ajax({
            url: '?seccion=baner',
            data: 'action=referencia&v=' + t.value + '&usuario=' + u,
            dataType: 'html',
            type: 'get',
            success: function(j){
                $('#contenedor').html(j);
            }
        });
        
        $.ajax({
            url: '?seccion=baner',
            data: 'action=referencia1&v=' + t.value + '&usuario=' + u,
            dataType: 'html',
            type: 'get',
            success: function(j){
                $('#imgBaner').val(j);
            }
        });
        
        $.ajax({
            url: '?seccion=baner',
            data: 'action=url&v=' + t.value + '&usuario=' + u,
            dataType: 'html',
            type: 'get',
            success: function(j){
                $('#url').val(j);
            }
        });
        
    });
    
    
    $('#ref_baner').change(function(){
        var t = this;
        var u = $('#selusuario').val();
        
        $.ajax({
            url: '?seccion=baner',
            data: 'action=usuario&v=' + u + '&ref=' + t.value,
            dataType: 'html',
            type: 'get',
            success: function(j){
                $('#referencia_baner').html(j);
            }
        });
        
        $.ajax({
            url: '?seccion=baner',
            data: 'action=referencia2&v=' + t.value + '&usuario=' + u,
            dataType: 'html',
            type: 'get',
            success: function(j){
                $('#medidas').removeAttr("disabled");
                $('#medidas').html(j);
                $('#medidas').attr("disabled", "disabled");
            }
        });
        
        $.ajax({
            url: '?seccion=baner',
            data: 'action=referencia&v=' + t.value + '&usuario=' + u,
            dataType: 'html',
            type: 'get',
            success: function(j){
                $('#contenedor').html(j);
            }
        });
        
        $.ajax({
            url: '?seccion=baner',
            data: 'action=referencia1&v=' + t.value + '&usuario=' + u,
            dataType: 'html',
            type: 'get',
            success: function(j){
                $('#imgBaner').val(j);
            }
        });
        
        $.ajax({
            url: '?seccion=baner',
            data: 'action=url&v=' + t.value + '&usuario=' + u,
            dataType: 'html',
            type: 'get',
            success: function(j){
                $('#url').val(j);
            }
        });
        
        
        
    });
    
    // recuperar todos los datos del anuncio
    $('#ref_anuncio').change(function(){
        var t = this;
        var u = $('#selusuario').val();
        completo = 1;
        $('#id').val(t.value);
        
        $.ajax({
            url: '?seccion=baner',
            data: 'action=recuperar_web&v=' + t.value,
            dataType: 'html',
            type: 'get',
            success: function(j){
                $('#web').html(j);
            }
        });
        
          $.ajax({
            url: '?seccion=baner',
            data: 'action=seccion_web&v=' + t.value,
            dataType: 'html',
            type: 'get',
            success: function(j){
                $('#seccion_web').html(j);
               
            }
        });
        
        
        $('#referencia_anuncio').val(t.options[t.selectedIndex].innerHTML);
        
        $.ajax({
            url: '?seccion=baner',
            data: 'action=recuperar_pais&v=' + t.value,
            dataType: 'html',
            type: 'get',
            success: function(j){
                $('#pais').html(j);
                var p = $('#pais').val();
                $.ajax({
                    url: '?seccion=baner',
                    data: 'action=recuperar_comunidad&v=' + t.value + "&p=" + p,
                    dataType: 'html',
                    type: 'get',
                    success: function(j){
                        $('#comunidad').html(j);
                        var p = $('#comunidad').val();
                        $.ajax({
                            url: '?seccion=baner',
                            data: 'action=recuperar_provincia&v=' + t.value + "&p=" + p,
                            dataType: 'html',
                            type: 'get',
                            success: function(j){
                                $('#provincias').html(j);
                                var p = $('#provincias').val();
                                $.ajax({
                                    url: '?seccion=baner',
                                    data: 'action=recuperar_poblacion&v=' + t.value + "&p=" + p,
                                    dataType: 'html',
                                    type: 'get',
                                    success: function(j){
                                        $('#poblacion').html(j);
                                    }
                                });
                                
                            }
                        });
                    }
                });
            }
            
            
            
        });
        
        
        
        
        
        
        
        $.ajax({
            url: '?seccion=baner',
            data: 'action=recuperar_seccion&v=' + t.value,
            dataType: 'html',
            type: 'get',
            success: function(j){
                $('#seccion').html(j);
            }
        });
        
        $.ajax({
            url: '?seccion=baner',
            data: 'action=recuperar_tipo&v=' + t.value,
            dataType: 'html',
            type: 'get',
            success: function(j){
                $('#tipo').html(j);
            }
        });
        
        
        
        
        $.ajax({
            url: '?seccion=baner',
            data: 'action=baner&v=' + t.value,
            dataType: 'html',
            type: 'get',
            success: function(j){
            
                $.ajax({
                    url: '?seccion=baner',
                    data: 'action=usuario&v=' + u + '&ref=' + j,
                    dataType: 'html',
                    type: 'get',
                    success: function(j){
                        $('#referencia_baner').html(j);
                    }
                });
                
                
                $.ajax({
                    url: '?seccion=baner',
                    data: 'action=referencia2&v=' + j + '&usuario=' + u,
                    dataType: 'html',
                    type: 'get',
                    success: function(j){
                        $('#medidas').removeAttr("disabled");
                        $('#medidas').html(j);
                        $('#medidas').attr("disabled", "disabled");
                    }
                });
                
                
                $.ajax({
                    url: '?seccion=baner',
                    data: 'action=referencia&v=' + j + '&usuario=' + u,
                    dataType: 'html',
                    type: 'get',
                    success: function(j){
                        $('#contenedor').html(j);
                    }
                });
                
                $.ajax({
                    url: '?seccion=baner',
                    data: 'action=referencia1&v=' + j + '&usuario=' + u,
                    dataType: 'html',
                    type: 'get',
                    success: function(j){
                        $('#imgBaner').val(j);
                    }
                });
                
                $.ajax({
                    url: '?seccion=baner',
                    data: 'action=url&v=' + j + '&usuario=' + u,
                    dataType: 'html',
                    type: 'get',
                    success: function(j){
                        $('#url').val(j);
                    }
                });
                
                
            }
        });
        
        
        
        
        
    });
    
    //recuperar peticion de anuncio
    
      // recuperar todos los datos del anuncio
    $('#ref_anuncio2').change(function(){
        var t = this;
       
        completo = 1;
        $('#id').val(t.value);
        
        $.ajax({
            url: '?seccion=baner',
            data: 'action=recuperar_web2&v=' + t.value,
            dataType: 'html',
            type: 'get',
            success: function(j){
                $('#web').html(j);
            }
        });
        
         $.ajax({
            url: '?seccion=baner',
            data: 'action=usuario_peticion&v=' + t.value,
            dataType: 'html',
            type: 'get',
            success: function(j){
                $('#usuarios').html(j);
                }
        });
        
        $.ajax({
            url: '?seccion=baner',
            data: 'action=usuario_peticion&v=' + t.value +"&u=1",
            dataType: 'html',
            type: 'get',
            success: function(j){
                $('#id_usuario').val(j);
                }
        });
        
        $.ajax({
            url: '?seccion=baner',
            data: 'action=seccion_web_peticion&v=' + t.value,
            dataType: 'html',
            type: 'get',
            success: function(j){
                $('#seccion_web').html(j);
               
            }
        });
        
        
        
        $('#ref_anuncio').html(t.options[t.selectedIndex].innerHTML);
		$('#referencia_anuncio').val(t.options[t.selectedIndex].innerHTML);
        
        $.ajax({
            url: '?seccion=baner',
            data: 'action=recuperar_pais2&v=' + t.value,
            dataType: 'html',
            type: 'get',
            success: function(j){
                $('#pais').html(j);
                var p = $('#pais').val();
                $.ajax({
                    url: '?seccion=baner',
                    data: 'action=recuperar_comunidad2&v=' + t.value + "&p=" + p,
                    dataType: 'html',
                    type: 'get',
                    success: function(j){
                        $('#comunidad').html(j);
                        var p = $('#comunidad').val();
                        $.ajax({
                            url: '?seccion=baner',
                            data: 'action=recuperar_provincia2&v=' + t.value + "&p=" + p,
                            dataType: 'html',
                            type: 'get',
                            success: function(j){
                                $('#provincias').html(j);
                                var p = $('#provincias').val();
                                $.ajax({
                                    url: '?seccion=baner',
                                    data: 'action=recuperar_poblacion2&v=' + t.value + "&p=" + p,
                                    dataType: 'html',
                                    type: 'get',
                                    success: function(j){
                                        $('#poblacion').html(j);
                                    }
                                });
                                
                            }
                        });
                    }
                });
            }
            
            
            
        });
        
        
        
        
        
        
        
        $.ajax({
            url: '?seccion=baner',
            data: 'action=recuperar_seccion2&v=' + t.value,
            dataType: 'html',
            type: 'get',
            success: function(j){
                $('#seccion').html(j);
            }
        });
        
        $.ajax({
            url: '?seccion=baner',
            data: 'action=recuperar_tipo2&v=' + t.value,
            dataType: 'html',
            type: 'get',
            success: function(j){
                $('#tipo').html(j);
            }
        });
        
        
        
        
        $.ajax({
            url: '?seccion=baner',
            data: 'action=baner2&v=' + t.value,
            dataType: 'html',
            type: 'get',
            success: function(j){
                
                var u = $('#id_usuario').val();
                
                $.ajax({
                    url: '?seccion=baner',
                    data: 'action=referencia_baner_peticion&v=' + u + '&ref=' + j,
                    dataType: 'html',
                    type: 'get',
                    success: function(j){
                        $('#ref_baner').html(j);
                         $('#referencia_baner').val(j);
                        
                    }
                });
                
                
                $.ajax({
                    url: '?seccion=baner',
                    data: 'action=referencia2_peticion&v=' + j + '&usuario=' + u,
                    dataType: 'html',
                    type: 'get',
                    success: function(j){
                       
                        $('#medidas').html(j);
                       
                    }
                });
                
                
                $.ajax({
                    url: '?seccion=baner',
                    data: 'action=referencia_peticion&v=' + j + '&usuario=' + u,
                    dataType: 'html',
                    type: 'get',
                    success: function(j){
                        $('#contenedor').html(j);
                    }
                });
                
                
                   $.ajax({
                    url: '?seccion=baner',
                    data: 'action=id_baner&v=' + j + '&usuario=' + u,
                    dataType: 'html',
                    type: 'get',
                    success: function(j){
                        $('#id_baner').val(j);
                    }
                });
                
             
                
                $.ajax({
                    url: '?seccion=baner',
                    data: 'action=url_peticion&v=' + j + '&usuario=' + u,
                    dataType: 'html',
                    type: 'get',
                    success: function(j){
                        $('#url').html(j);
                    }
                });
                
                
            }
        });
        
        
        
        
        
    });
    
    
    ///******************************************//
    
    $('#ref_anuncio').bind('ajaxStop', function(){
      
      
        if (web.val() != "" && (seccion_web.val()==2 || seccion_web.val()==10)) {
        
            $('#datos_baner').show("slow");
           
        } else {
       
            $('#datos_baner').hide("normal");
        }
    });
    
    
    $('#ref_anuncio2').bind('ajaxStop', function(){
       
   
        if (web.val() != "" && seccion_web.val()==2) {
        
            $('#datos_baner').show("slow");
           
        } else {
       
            $('#datos_baner').hide("normal");
        }
    });
    
    
    
    /* funcion para colocar los baners pequeños*/
    
    cambiaBanersPeq = function(campos){
    
    
                $.ajax({
                    url: 'modules/buscador/parametros_baners.php?',
                    data: 'action=muestraBaner&prov=' + campos[0] + '&pob=' + campos[1]+ '&sec=' + campos[16]+ '&tipo=' + campos[3],
                    dataType: 'html',
                    type: 'get',
                    success: function(j){
                        $('#banersPequenos').html(j);
                    }
                });
        
    }
    
    
    
});

