// This js handles the ajax calls via iframe for the poll system

// Hide/Show the poll form and poll results  
function hidePollForm()
{
       x = document.getElementById("poll_form");
       x.style.display= "none";
}
function showPollForm()
{
       x = document.getElementById("poll_form");
       x.style.display= "block";
}
function hidePollResult()
{
       x = document.getElementById("poll_result");
       x.style.display= "none";
}
function showPollResult()
{
       x = document.getElementById("poll_result");
       x.style.display= "block";
       stretch_graphs();  // Should only get called once since you can't hide results
}

// When you click to cast your vote this get's called
function submitPoll(choice_id, poll_id, story_id, section_id)
{
  poll_value = getPollValue();
  
  if (poll_value != -1)
  {
    // Build a url with the form data to send the iframe to to exec the perl script
    url="/survey-bin/tabulate_poll.cgi?mi_pb_cache=1&expire_days=5&path=/&already_voted_error=You have already voted. Limit 1 vote per user.&save=Cast Vote&value=Cast Vote"
    url = url + "&poll_id=" + poll_id; 
    url = url + "&section_id=" + section_id;
    url = url + "&story_id=" + story_id;
    url = url + "&poll_response=" + poll_value;
      
    x = document.getElementById("communication_iframe");
    x.src = url;
  }
  else
  {
    alert("Please choose an option to vote for")
  }
}

// This helper function looks at the list of poll responses and returns the index of the one selected
 function getPollValue(){
    var radios = document.forms.poll_responses.poll_response;
    for(var i=0; i<radios.length; i++){
        if(radios[i].checked) return (i + 1);
    }
    return -1;
}    

// This function is called from the child iframe to register your poll has
// Been submitted and to pass in an update of the results after your vote
function handleResponse(msg, graphical_results) {
   if (msg = '1') 
    {
       hidePollForm();
       showPollResult();
    }
    else
    {
       showPollForm();
       hidePollResult();
    }

   x = document.getElementById("poll_result");
   x.innerHTML = graphical_results;
}

// Grab the cookie so we can check if they've already submitted or not
function getCookie (name) {
    var dc = document.cookie;
    var cname = name + "=";
    var clen = dc.length;
    var cbegin = 0;
    
    while (cbegin < clen) {
    var vbegin = cbegin + cname.length;
    
    if (dc.substring(cbegin, vbegin) == cname) {
    var vend = dc.indexOf (";", vbegin);
    if (vend == -1) vend = clen;
    
    return unescape(dc.substring(vbegin, vend));
    }
    
    cbegin = dc.indexOf(" ", cbegin) + 1;
    
    if (cbegin== 0) break;
    }
    return null;
}

// Onload, hide either the poll form or the poll results based on the cookie
function visibilityCheck(poll_id)
{
    // Get the cookie and parse it to see if this poll ID has been submitted before
    content = getCookie('mi_polls');
    sval=content.split(";");
    num_cookies = sval.length;
    key = new Array();
    for (x = 0; x < num_cookies / 2 + 1; x++)
    {
        qval=sval[x].split("=");
        id = qval[0];
        poll_submitted = qval[1];
        key[id]  = poll_submitted;
    }
    
    if (key[poll_id]==1) {
        hidePollForm();
        showPollResult();
    }
    else
    {
           showPollForm();
           hidePollResult();
    }
}

// The size of the box this stuff is going into can vary quite a bit so
// This will scale up the size of those bars on the bar graph so they take up
// More of the box.. If your graphs are running off the edge, bring .9 down to .8
function  stretch_graphs()
{
    x = document.getElementById("poll_result");
    y = document.getElementById("poll_form");
    
    if (x.offsetWidth > 0)
      div_width = x.offsetWidth;
    else
      div_width = y.offsetWidth;

    // Magical formula or deciding how much to increase the bar width    
    multiplier = (div_width * .9 - 20)/100;

    // Get each of those bars       
    var x=document.getElementsByName("bar_result");
    for (count = 0; count < x.length; count++)
    {
      pixels = x[count].width;
      if (pixels > 0)
        x[count].width = pixels * multiplier;
      
    }
    
}
