var lst_lnk_id_noticia_eli;
var lst_lnk_id_noticia_act;
var lst_lnk_id_noticia_ord;
function AdministracionNoticias(){
      ocultaPanelBanner();
      
    $('titulo').focus();
    crg_lnk_id_noticia_eli();
    crg_lnk_id_noticia_act();
    crg_lnk_id_noticia_ord();
    
        $('rdSinImagen').onclick=function(){
            $('formularioNoticias').removeAttribute("enctype");
            Form.Element.disable('url');
            Form.Element.disable('fotoNoticia');
            //alert($('formularioNoticias').enctype);
        }
    
        $('rdPortaretrato').onclick=function(){
            $('formularioNoticias').enctype='multipart/form-data';
            Form.Element.enable('url');
            Form.Element.enable('fotoNoticia');
            //alert($('formularioNoticias').enctype);
        }
        $('rdPanoramica').onclick=function(){
            $('formularioNoticias').enctype='multipart/form-data';
            Form.Element.enable('url');
            Form.Element.enable('fotoNoticia');
            //alert($('formularioNoticias').enctype);
        }
        
    $('formularioNoticias').onsubmit=function(){      
        return validaNoticia();
    }
}
function publicaFotoNoticia(id_noticia){
    if(id_noticia != null){         
        alert('Noticia Cargada' );
        new Ajax.Updater('divSecciones','controladores/administracionNoticiasControlador.php',
                                            {
                                                method:'post',
                                                contentType:'application/x-www-form-urlencoded',                                                
                                                onComplete:function(respuesta){                                                    
                                                  AdministracionNoticias();
                                                  ocultaAviso();
                                                  new Effect.Highlight('tblAdministracionNoticias');
                                                },
                                                onException:function(error){
                                                   alert("se produjo un exception: " + error.name + " "+ error.message + " " + error.toString());
                                                },
                                                onFailure:function(error){
                                                   alert("se produjo una falla : " + error.name + " "+ error.message + " " + error.toString());
                                                }
                                            }
                                      );
    }else{
        alert('error no se pudo subir la foto \n revise que no sea mayor a 500Kb' );
        ocultaAviso();
    }    
}
function validaNoticia(){
    var esValido=true;
    var camposObligatorios='Hay campos obligatorios que faltan de ser llenados:\n ';
    if( $F('titulo').length<4){
        camposObligatorios = camposObligatorios + "- "+ $('titulo').name + "\n";
        esValido=false;
    }
    if( $F('noticia').length<4){
        camposObligatorios = camposObligatorios + "- "+ $('noticia').name + "\n";
        esValido=false;
    }
    if( ($('rdPortaretrato').checked==true || $('rdPanoramica').checked==true)  && $F('fotoNoticia').length<3){
        camposObligatorios = camposObligatorios + "- "+ $('fotoNoticia').name + "\n";
        esValido=false;
    }
    if( $('rdPanoramica').checked == false && $('rdPortaretrato').checked == false && $('rdSinImagen').checked == false){
        camposObligatorios = camposObligatorios + "-  orientación imagen \n";
        esValido=false;
    }
    if(esValido == false){
       alert(camposObligatorios);
       esValido= false;
    }else{
        muestraAviso();
    }
    return esValido;
}
//function noticiaGuarda_onclick(){
//    if(validaNoticia()==true){
//        new Ajax.Updater('divSecciones','controladores/administracionNoticiasGuardaControlador.php',{
//            method:'post',
//            contentType:'application/x-www-form-urlencoded',
//            parameters:$('formularioNoticias').serialize(),
//            onComplete:function(respuesta){                
//                // se vuelve a cargar
//               AdministracionNoticias();
//               new Effect.Highlight('tblNoticias');
//            },
//            onException:function(error){
//                alert('exception'+ error.name + " "+ error.message + " " + error.toString());
//            },
//            onFailure:function(error){
//                alert('falla'+ error.name + " "+ error.message + " " + error.toString());
//            }
//        });
//    }
//}

function crg_lnk_id_noticia_eli(){
   lst_lnk_id_noticia_eli = $$("a[id='lnk_id_noticia_eli']");
   lst_lnk_id_noticia_eli.each(function(elemento){
        elemento.onclick= function(){
            if( mensajeElimina() ==true){
                muestraAviso();
                    new Ajax.Updater('divSecciones','controladores/administracionNoticiasEliminaControlador.php',
                                            {
                                                method:'post',
                                                contentType:'application/x-www-form-urlencoded',
                                                parameters:"id_noticia="+elemento.name,
                                                onComplete:function(respuesta){
                                                    ocultaAviso();
                                                    
                                                 AdministracionNoticias();
                                                 new Effect.Highlight('tblAdministracionNoticias');
                                                },
                                                onException:function(error){
                                                   alert("se produjo un exception: " + error.name + " "+ error.message + " " + error.toString());
                                                },
                                                onFailure:function(error){
                                                   alert("se produjo una falla : " + error.name + " "+ error.message + " " + error.toString());
                                                }
                                            }
                                      );
            }                             
        }
   });
}
function crg_lnk_id_noticia_act(){
   lst_lnk_id_noticia_act = $$("a[id='lnk_id_noticia_act']");
   lst_lnk_id_noticia_act.each(function(elemento){
        elemento.onclick= function(){
            muestraAviso();
                /*elemento.name=id_noticia=x&estatus=y*/
                var parametros=elemento.name;               
                if(parametros.search("estatus=1")!=-1){
                    parametros=parametros.replace("estatus=1","estatus=0");
                }else{
                    parametros=parametros.replace("estatus=0","estatus=1");
                }                                
//                alert("enviados:" + parametros);
//                alert("original:" + elemento.name);
                    new Ajax.Updater('divSecciones','controladores/administracionNoticiasActualizaControlador.php',
                                            {
                                                method:'post',
                                                contentType:'application/x-www-form-urlencoded',                                                
                                                parameters:parametros,
                                                onComplete:function(respuesta){
                                                    ocultaAviso();
                                                    AdministracionNoticias();
                                                     new Effect.Highlight('tblAdministracionNoticias');
                                                },
                                                onException:function(error){
                                                   alert("se produjo un exception: " + error.name + " "+ error.message + " " + error.toString());
                                                },
                                                onFailure:function(error){
                                                   alert("se produjo una falla : " + error.name + " "+ error.message + " " + error.toString());
                                                }
                                            }
                                      );
            }                             
        
   });
}
function crg_lnk_id_noticia_ord(){
   lst_lnk_id_noticia_ord = $$("a[id='lnk_id_noticia_ord']");
   lst_lnk_id_noticia_ord.each(function(elemento){
        elemento.onclick= function(){
            //muestraAviso();
                var orden_num ='orden'+elemento.name;
                var parametros='id_noticia='+elemento.name+'&orden_num='+$F(orden_num);           
                //alert(parametros);
                    new Ajax.Updater('divSecciones','controladores/administracionNoticiasActualizaOrdenControlador.php',
                                            {
                                                method:'post',
                                                contentType:'application/x-www-form-urlencoded',                                                
                                                parameters:parametros,
                                                onComplete:function(respuesta){
                                                    ocultaAviso();
                                                    AdministracionNoticias();
                                                     new Effect.Highlight('tblAdministracionNoticias');
                                                },
                                                onException:function(error){
                                                   alert("se produjo un exception: " + error.name + " "+ error.message + " " + error.toString());
                                                },
                                                onFailure:function(error){
                                                   alert("se produjo una falla : " + error.name + " "+ error.message + " " + error.toString());
                                                }
                                            }
                                      );
            }                             
        
   });
}
