/* ****************************************************************** */
/* **** Funciones para validar la cookie de votacion del usuario **** */
/* **** 08/10/2002 - MCO ******************************************** */
/* **** 11/06/2008 - CSA ******************************************** */
/* ****************************************************************** */
// -----------------------------------------------------------------
function getCookie(name) {
// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin === -1) {
    begin = dc.indexOf(prefix);
    if (begin !== 0) {return null;}
  } else{
    begin += 2;
  }
  var end = document.cookie.indexOf(";", begin);
  if (end === -1){
    end = dc.length;
  }
  return unescape(dc.substring(begin + prefix.length, end));
} // getCookie.

// -----------------------------------------------------------------
function check_cookie(id) {
    var yavoto = getCookie(id); // cookie sesion
    var i            =0;
    var idAlt        = '';
    var idEncGracias = 'encGracias_'+id;
    var idEncCerrada = 'encCerrada_'+id;
    var botonVotar   = 'Sbm_ACCION_'+id;
    var cerrada      ='cerrada_'+id;
    // Si el usr ya votoesta logeado, salta directo al menu sin solicitare la clave.
    //var opciones = document.getElementById("formEnc_"+id).Rad_ENCUESTA.length;
    if (yavoto === 'yavoto' || document.getElementById(cerrada).value==='true') {
      for ( i=document.getElementById("formEnc_"+id).alti.value; i<=document.getElementById("formEnc_"+id).altf.value; i++) {
        idAlt = 'Rad_ENCUESTA_'+id+'_'+i;
        document.getElementById(idAlt).style.display = 'none';
      }
      try {
      document.getElementById(botonVotar).style.display = 'none';
      if ( document.getElementById(cerrada).value === 'true' ){
        document.getElementById(idEncGracias).style.display = 'none';
        document.getElementById(idEncCerrada).style.display = '';
      } else {
        document.getElementById(idEncGracias).style.display = '';
        document.getElementById(idEncCerrada).style.display = 'none';
      }
      }catch (e1){
      }
      return true;
    }else {
      try {
          if ( document.getElementById(cerrada).value === 'true' ){
            document.getElementById(idEncGracias).style.display = '';
            document.getElementById(idEncCerrada).style.display = '';
          } else {
            document.getElementById(idEncGracias).style.display = 'none';
            document.getElementById(idEncCerrada).style.display = 'none';
          }
      }catch (e2){
      }
      return false;
    }
} // check_cookie.

// -----------------------------------------------------------------
function envia_encuesta(from,lengua,id) {
  //var opciones = document.formEnc.Rad_ENCUESTA.length;
  var formEncu = "formEnc_"+id;
  var formObj = document.forms[formEncu];
  var selecc = false;
  var x=0;
  for (x=0; x < formObj.elements.length; x++){
    if(formObj.elements[x].type === "radio"){
        if ( formObj.elements[x].checked ){
            selecc = true;
            break;
        }
    }
  }

  if (! selecc) {
    vista = getCookie('vista'); // cookie sesion
    if ( vista === 'en' || vista === 'eng'){
        alert('You must select an alternative.');
    }else{
        alert('Debe seleccionar alguna alternativa.');
    }
    return false;
  }

  if ( (selecc) && (from === 'port') ) {
    document.getElementById("formEnc_"+id).from.value = 'port';
    document.getElementById("formEnc_"+id).url.value = document.URL;
  }

  enc_ajax_post('/encuestas/php/encuesta_votar.php',lengua,id);
  return false;

} // envia_encuesta.

// -----------------------------------------------------------------
function recarga_pag() {
  document.frames['enc'].location.reload();
} // recarga_pag.


// -----------------------------------------------------------------
  function subWin(loc, nom, ancho, alto, posx, posy) {
    var options="toolbar=0,status=0,menubar=0,scrollbars=1,resizable=0,location=0,directories=0,width=" + ancho + ",height=" + alto;
    win = window.open(loc, nom, options);
    win.focus();
    win.moveTo(posx, posy);
  }

// -----------------------------------------------------------------
function verMasEncuestasSc(){
    //Muestra otras encuestas, rutina SIN cgi
    var loc = '/encuestas/port/encuesta_lista.html';
    subWin(loc, 'verMasEncuestasSc', 500, 550, 75, 50);
    //window.location.href = loc;
}

// -----------------------------------------------------------------
function verMasEncuestasCc(leng){
    //Muestra otras encuestas, rutina CON cgi
    var loc = '/cgi-bin/encuestas/encuesta_lista.cgi?leng='+leng;
    subWin(loc, 'verMasEncuestasCc', 510, 550, 75, 50);
    //window.location.href = loc;

}


// -----------------------------------------------------------------
function verResulEncuestasSc(idEncuesta,leng){
    //Muestra resultado de encuestas, rutina php
    subWin('/encuestas/php/encuesta_resul.php?id='+idEncuesta+'&leng='+leng,'verResulEncuestasSc', 510, 550, 105, 85);
}

// -----------------------------------------------------------------
function verResulEncuestasCc(idEncuesta,leng){
    //Muestra resultado de encuestas, rutina cgi
    subWin('/cgi-cpn/encuestas/encuesta_resul.cgi?id='+idEncuesta+'&leng='+leng,'verResulEncuestasCc', 510, 550, 75, 50);
}

// -----------------------------------------------------------------
function verResultadoEncuesta(idEncuesta,leng,ver_resul){
    ver_resul = (typeof ver_resul === 'undefined') ? 0 : ver_resul; //Por defecto 0
    subWin('/encuestas/php/encuesta_resul.php?id='+idEncuesta+'&leng='+leng+'&ver_resul='+ver_resul,'verResultadoEncuesta', 510, 550, 105, 85);
}

// -----------------------------------------------------------------

