var ajax = new Object();

ajax.has_xml_http = false;
ajax.xml_http     = null;
ajax.result_cache = new Object();

//
// 12 Nov 2007 11:23:15 MLM code base path
//

var base_url = '/cc-common/polling_tool';
//var base_url = '/projects/poll_tool/user';

/****************************************************************************************/
ajax.getXmlHttpObject = function()
{
  var obj = null;
  
  //get xml object for IE
  try {
    obj = new ActiveXObject("Msxml2.XMLHTTP");
  }
  catch(e) {
    try {
      obj = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch(et) {
      obj = null;
    }
  }
  
  //get XML object for mozilla
  if (!obj && typeof XMLHttpRequest != "undefined")
  {
    obj = new XMLHttpRequest();
  }
  
  return obj;
};
ajax.has_xml_http = ajax.getXmlHttpObject() ? true : false;

/****************************************************************************************/
ajax.cacheResults = function(key, page, value)
{
  ajax.result_cache[key] = value;
//  ajax.result_cache[key + '-' + page] = value;
};

/****************************************************************************************/
ajax.getFromCache = function(key)
{
  return ajax.result_cache[key];
};

/****************************************************************************************/
ajax.throwError = function(s)
{
  alert(s);
};

/****************************************************************************************/
ajax.sendRequest = function(url, method, do_eval)
{


  if (ajax.xml_http && ajax.xml_http.readyState!=0)
  {
    ajax.xml_http.abort()
  }
  
  
  ajax.xml_http = ajax.getXmlHttpObject();
  
   if (ajax.xml_http)
  { 
  
          
    ajax.xml_http.open(method, url, true);
    ajax.xml_http.onreadystatechange = function()
    {
                        
      if (ajax.xml_http.readyState==4 && ajax.xml_http.responseText)
      {
      
              if (do_eval)
        {
                eval(ajax.xml_http.responseText);
        }
        else
        {
                
          document.write(ajax.xml_http.responseText);
          
        }
      }
    };
    ajax.xml_http.send(null);
  }
};



function insertPoll(pollID)
{
     ajax.sendRequest(base_url + "/a.php?poll_id="+pollID,"GET", true);
}




var running_poll = null;
var poll_queue = new Array(40);
var subpoll_queue = new Array(40);
var num_queued = 0;

function insertPoll2(pollID,subPollId)
{
       
// Nov. 22, 2011: Commented the next section out to avoid potential race conditions. Use separate callbacks for each poll.

   for(i = 0;i < 40; i++)
   {
     if(poll_queue[i] == pollID)
     {
        poll_queue[i] = '';
        subpoll_queue[i] = '';
     }
   }
/*
        if(running_poll != null)
        {
                poll_queue[num_queued] = pollID;          
                if(subPollId)
                {
                        subpoll_queue[num_queued] = subPollID;          
                }
                num_queued++;
                return true;
        }
        else
        {
                        running_poll = pollID;
        }
        var url = null;
*/
        if(subPollId)
        {
          url = base_url + "/insertPoll.php?poll_id=" + pollID + "&subpoll_id=" + subPollId;
          getPollContents(url, subPollId);
          //ajax.sendRequest(url, "GET",true);
            
            
        }
        else
        {
          //ajax.sendRequest(base_url + "/insertPoll.php?poll_id="+pollID, "GET",true);
          url = base_url + "/insertPoll.php?poll_id="+pollID;
            getPollContents(url, pollID);
        }
}

//Function that will make a jquery ajax request and display the returned poll content - Scott Wilkin 2011/11/22
function getPollContents(requestURL, pollID){
    jQuery.ajax({
       type: "GET",
       url: requestURL,
       dataType: "html",
       success: function(data){
            $('#start_poll_' + pollID).html('');
            $('#stop_poll_' + pollID).html('');
            $('#poll_' + pollID).html(unescape(data.replace(/\+/g," ")));

       },
       error: function(){
            if(console){
                console.log('There was an error loading poll ID '+pollID);
            }
       }
     });
}




function submitVote()
{
  var getinfo;
  var user;
  var pass;
  var email;
  var optionID;
  var extraGet;

  getinfo = document.location.pathname + escape(document.location.search);

  var pollID = document.getElementById('POLL_ID').value;
  var url = base_url + "/a.php?poll_id="+pollID+"&redirect="+getinfo;

  if(eval(document.getElementById('poll_username')))
  {
    
     user = document.getElementById('poll_username').value;
     pass = document.getElementById('poll_password').value;
     if(user == '' || pass == '')
     {
         alert('Please enter user name & password.');
         return true;
     }
     
     extraGet = '&p='+pass+"&u="+user;
     url = url + extraGet;
          
  }

  if(eval(document.getElementById('poll_email')))
  {
     email = document.getElementById('poll_email').value;
     if(email == '')
     {
       alert('Please enter email address');
       return true;
     }
     url = url + '&email='+email;
  }
  if(document.getElementById('option_id').value != 'NOTSET')
  {

    optionID = document.getElementById('option_id').value;
    url = url + '&optionID='+optionID;
    ajax.sendRequest(url, "GET", true );


  }
  else
  {
    alert("Please make a selection");
  }
}

function submitVote2(pollID)
{
        
        var newurl;
        //var pollID;
  var getinfo;
  var user;
  var pass;
  var email;
  var optionID;
  var extraGet;
  var newEmail;

  getinfo = document.location.pathname + escape(document.location.search);

  //pollID = document.getElementById('POLL_ID').value;
  newurl = base_url + "/insertPoll.php?poll_id="+pollID+"&redirect="+getinfo;

  if(eval(document.getElementById('poll_username')))
  {
    
     user = document.getElementById('poll_username').value;
     pass = document.getElementById('poll_password').value;
     if(user == '' || pass == '')
     {
         alert('Please enter user name & password.');
         return true;
     }
     extraGet = "&p="+pass+"&u="+user;
     newurl = newurl + extraGet;
  }

  if(eval(document.getElementById('poll_email')))
  {
     email = document.getElementById('poll_email').value;

    newEmail = email.replace(" ", "");
    email = newEmail;

     if(email == '')
     {
       alert('Please enter email address');
       return true;
     }
     newurl = newurl + "&email="+email;
     
  }
  
  if(document.getElementById('option_id').value != 'NOTSET')
  {
    
    optionID = document.getElementById('option_id').value;
    newurl = newurl + "&optionID="+optionID;
    ajax.sendRequest(newurl, "GET", true );

  }
  else
  {
    alert("Please make a selection");
  }
}


function setVote(optionID)
{
   document.getElementById('option_id').value = optionID;
}


function writePoll(data,polling_id)
{
   var info;
   var newHTML;
   newHTML =  unescape(data.replace(/\+/g," "));


 
   document.getElementById('poll_'+polling_id).innerHTML = newHTML;
   if(document.getElementById('start_poll_'+polling_id))
   {
            document.getElementById('start_poll_'+polling_id).innerHTML = '';
   }
   if(document.getElementById('stop_poll_'+polling_id))
   {
     document.getElementById('stop_poll_'+polling_id).innerHTML = '';
   }
   
   for(i = 0;i < 40; i++)
   {
     if(poll_queue[i])
     {
        running_poll = null;
        insertPoll2(poll_queue[i],subpoll_queue[i]);
        return true;
     }
   }
   return true;
  
}


