function saveAutorizacion()
{
        flag_nombre=0;
          flag_foto=0;
          flag_page=0;

           check_flag_foto =document.getElementById("id_flag_foto");
           check_flag_nombre=document.getElementById("id_flag_nombre");
           check_flag_page=document.getElementById("id_flag_page");

           if(check_flag_foto.checked)
           {
               flag_foto=1;
           }


           if(check_flag_nombre.checked)
           {
               flag_nombre=1;
           }

           if(check_flag_page.checked)
           {
               flag_page=1;
           } 

         parametros="flag_nombre="+flag_nombre;
         parametros+="&flag_foto="+flag_foto;
         parametros+="&flag_page="+flag_page;
         parametros+="&action=saveconfiguracion";

    $.ajax({
                    type: "POST",
                    url: rootPath+"encuesta.ajaxclass.php",
                    data: parametros,
                    success: function(msg)
                    {
                        location.reload(true);
                    }
    });
}

function sendVoto(item_id,serie_id,encuesta_id,signo,ordenar)
{
      item_id=item_id;
      serie_id=serie_id;
      encuesta_id=encuesta_id;
      signo=signo;

      nombre=document.getElementById("nombre-item-"+item_id).innerHTML;

       nombre =nombre.replace("<br>"," - ");
     nombre =nombre.replace("<BR>"," - ");



      fbmensaje=nombre;

      imagen= document.getElementById("img-item-"+item_id).src;



mensaje(document.getElementById("img-"+signo+"-"+item_id).className);
     if(document.getElementById("img-"+signo+"-"+item_id).className!="manito_set")
     {
         parametros="item_id="+item_id;
         parametros+="&signo="+signo;
         parametros+="&serie_id="+serie_id;
         parametros+="&encuesta_id="+encuesta_id;
         parametros+="&action=addvoto";

         mensaje("parametros:"+parametros);
         

         item_selecionado=0;

         for (var k in arrEncuesta)
         {
            
            if(""+k==""+encuesta_id)
                {
                 

                  if(arrEncuesta[""+k].length>0)
                  {
                    for(var m in arrEncuesta[""+k] )
                    {
                      if(""+serie_id==""+m)
                      {item_selecionado=arrEncuesta[""+k][m];}
                    }
                  }
                  else
                  {
                      item_selecionado=0;
                  }
                }
         }

          mensaje("item_selecionado:"+item_selecionado+", item actual:"+item_id)

         enproceso=true;

         var itemsCantidad = new Array();
         var itemsElemento = new Array();

         var TotalEncuesta = parseInt(clearComas(document.getElementById("e-t-"+encuesta_id+"-"+serie_id).innerHTML));

        mensaje("item_selecionado :"+TotalEncuesta);

         $.ajax({
                    type: "POST",
                    url: rootPath+"encuesta.ajaxclass.php",
                    data: parametros,
                    success: function(msg){

                    mensaje("RESULTADO AJAX:"+msg);

                    if(msg=="inicia sessión!")
                      { mensaje("inica sesion");
                        centerPopup(document.getElementById(""+item_id));
                        loadPopup();

                      }
                      else
                      {
                         if (item_selecionado==0)
                         {
                           cn= parseInt(clearComas(document.getElementById("n-"+signo+"-"+item_id).innerHTML));
                           document.getElementById("n-"+signo+"-"+item_id).innerHTML=format_number(cn+1);
                           TotalEncuesta=TotalEncuesta+1;
                           document.getElementById("e-t-"+encuesta_id+"-"+serie_id).innerHTML=format_number(TotalEncuesta);
                         }
                         else
                         {
                           signo_aux=signo;
                           if(encuesta_id==5)
                           {
                           if (signo=="0"){signo_aux ="1"}else{signo_aux ="0"}
                           }
                           cn= parseInt(clearComas(document.getElementById("n-"+signo_aux+"-"+item_selecionado).innerHTML));
                           document.getElementById("n-"+signo_aux+"-"+item_selecionado).innerHTML=format_number(cn-1);

                           cn= parseInt(clearComas(document.getElementById("n-"+signo+"-"+item_id).innerHTML));
                           document.getElementById("n-"+signo+"-"+item_id).innerHTML=format_number(cn+1);
                         }



                         //items=  document.getElementsByName("e-"+encuesta_id+"-"+serie_id);
                         items = $("[name='"+"e-"+encuesta_id+"-"+serie_id+"']");
                         mensaje("item:"+items.length);
                        

                         
                              
                                 for (var i = 0; i < items.length; i++)
                                 {
                         	       
                                       objcantidad= document.getElementById("n-"+signo+"-"+items[i].id);
                                       document.getElementById("p-"+signo+"-"+items[i].id).innerHTML=porcentaje(objcantidad.innerHTML,TotalEncuesta);


                                       itemsCantidad[""+items[i].id] =clearComas(objcantidad.innerHTML);

                                       signo_aux=signo;
                                       if(encuesta_id==5)
                                       {
                                       if (signo=="0"){signo_aux ="1"}else{signo_aux ="0"}
                                       }
                                       
                                       objcantidad= document.getElementById("n-"+signo_aux+"-"+items[i].id);
                                       document.getElementById("p-"+signo_aux+"-"+items[i].id).innerHTML=porcentaje(objcantidad.innerHTML,TotalEncuesta);

                                       document.getElementById("img-"+signo_aux+"-"+items[i].id).className="manito";//+signo_aux;

                                       itemsElemento[""+items[i].id]=items[i];
                                 }

                           
                           document.getElementById("img-"+signo+"-"+item_id).className="manito_set";//+signo;


                          if(ordenar)
                          {
                          itemsCantidad =sortByNumericValue(itemsCantidad);
                          seccion1= document.getElementById("se-"+encuesta_id+"-"+serie_id);
                          seccion1.innerHTML="";

                       


                          for (var j in itemsCantidad){

                             
                              element=itemsElemento[itemsCantidad[j].clave];
                            //seccion1.appendChild(element);

                            $("#se-"+encuesta_id+"-"+serie_id).append(element);
                            }

                           var br = document.createElement('br');
                           br.className="clearfloat";
                           seccion1.appendChild(br);
                          }
                                    foundEncuesta=false;

                                    for (var l in arrEncuesta)
                                    {
                                        if(""+l==""+encuesta_id)
                                        {
                                          foundEncuesta=true;
                                            
                                        }
                                    }

                                    if(foundEncuesta)
                                    {
                                        foundSerie =false;

                                        for (var l2 in arrEncuesta[""+encuesta_id])
                                        {
                                        if(""+l2==""+serie_id)
                                        {
                                          foundSerie=true;

                                        }
                                        }

                                        if(foundSerie)
                                        {
                                            arrEncuesta[""+encuesta_id][""+serie_id]=item_id;
                                        }
                                        else
                                        {
                                        arr= new Array();
                                        arr[""+serie_id]=item_id;
                                        arrEncuesta[""+encuesta_id]=arr;
                                        }

                                    }else
                                    {
                                        arr= new Array();
                                        arr[""+serie_id]=item_id;
                                        arrEncuesta[""+encuesta_id]=arr;
                                    }



                                    
                                            mensaje("cambio de item selecionado");

                              enproceso=false;

                                FB.init({
                                    appId:'193769507315128', cookie:true,
                                    status:true, xfbml:true
                                 });
                               
                               FB.ui( {
                                     method: 'feed',
                                     name: 'Pulgarsitio.com',
                                     link: 'http://www.pulgarsitio.com/',
                                     picture: imagen,
                                     caption: 'Sondeo Electoral on-line',
                                     description:'Mi voto: '+fbmensaje,
                                     message:'En el sondeo electoral on-line voté por ' +nombre
                                   });
                                /*

                                FB.ui({method:
                                       'apprequests',
                                        message: 'En el sondeo electoral on-line (www.pulgarsitio.com) voté para las elecciones 2011, ingresa y vota también :)',
                                        data: ''});

*/



                      }
                     mensaje("voto procesado!!!");
                     enproceso=false;
                   


                    }
                    });
      
     }
     else
         {mensaje("click ingnorado: class:"+document.getElementById("img-"+signo+"-"+item_id).className);}
}

var encuesta_id=0;

function  registrarVoto()
{
     $(".voto1").click(function()
       {
         mensaje("click");

         item_id=$(this).attr("item");
         serie_id=$(this).attr("serie");
         encuesta_id=7;
         signo = 1;

         if(enproceso==false)
         {  sendVoto(item_id,serie_id,encuesta_id,signo,false);

         }
         else
             { mensaje("click ignorado!!!");  }



       });


     $(".voto2").click(function()
       {

         item_id=$(this).attr("item");
         serie_id=$(this).attr("serie");
         encuesta_id=4;
         signo = 1;

         if(enproceso==false)
         {  sendVoto(item_id,serie_id,encuesta_id,signo,false);}
         else
         { mensaje("click ignorado!!!");  }

  

       });


     $(".voto3").click(function()
       {
         item_id=$(this).attr("item");
         serie_id=$(this).attr("serie");
         encuesta_id=5;
         signo = $(this).attr("signo");

         if(enproceso==false)
         {  sendVoto(item_id,serie_id,encuesta_id,signo,false);

         }
         else
             { mensaje("click ignorado!!!");  }

       });

     
     $("#auth_submit").click
     (
      function()
      {

           saveAutorizacion();



      }
     )

   

}



$(document).ready(function (){

  registrarVoto();


$("#popupSeguridadClose").click(function(){
disablePopupSeguridad();
});

$("#popupSeguridadOpen").click(function(){

ubicarPopup();
loadPopupSeguridad();
});






//CLOSING POPUP
//Click the x event!
$("#popupContactClose").click(function(){
disablePopup();
});
//Click out event!
//$("#backgroundPopup").click(function(){
//disablePopup();
//});
//Press Escape event!
$(document).keypress(function(e){
if(e.keyCode==27 & popupStatus==1){
disablePopup();
}
});




});

   



