function Survey_Details() {
  this.loaded = 0;
  
  this.root = "";
  
  this.count = 0;
  this.voted = 0;
}

function survey_details_initialize() {
  with (this) {
    if (Site.loaded == 2) {
      loaded = 1;
      root = Site.root;
      Surveys.OptionsCount();
    } else {
      window.setTimeout('Surveys.Initialize()',10);
    }
  }
}

function survey_details_options_count() {
  var query_list = Site.ScriptQuery("polls");
  
  with (this) {
    if (loaded == 1) {
      if (count == 0) {
        count = (query_list['options'] ? 1*(query_list['options']) : 0);
      }
      
      if (count != 0) {
        loaded = 2;
      } else {
        window.setTimeout('Surveys.OptionsCount()',10);
      }
    }
  }
}

function survey_details_insert_option() {
  var table;
  var row;
  var cell;
  var span;
  var field;
  
  with (this) {
    if (loaded == 2) {
      table = $('polls_options_table');
      
      if (table) {
        count++;
        
        row = table.insertRow(-1);
        
        cell = row.insertCell(-1);
        cell.style.paddingRight = "12px";
        cell.innerHTML = "<b>"+count+"</b>";
        
        cell = row.insertCell(-1);
        span = document.createElement("SPAN");
        span.id = "polls_options_field"+count;
        cell.insertBefore(span,null);
        
        field = document.createElement("INPUT");
        field.name = "options[]";
        field.type = "text";
        field.size = 45;
        field.maxlength = 1000;
        span.insertBefore(field,null);
      }
    }
  }
}

function survey_details_vote() {
  var process;
  
  var votes = new Array();
  var opts_display = new Array();
  var opts = new Array();
  var results = new Array();
  var x = 1;
  var notfound = false;
  
  var go, total, display_total, option, spage, num;
  var sums = new Array(0,0);
  
  with (this) {
    if (loaded == 1) {
      process = $('site_process');
      go = $('polls_latest_survey_submit');
      
      if (process && go) {
        num = $('polls_latest_survey_num');
        spage = $('polls_latest_survey_page');
        option = $('polls_latest_survey_option');
        total = $('polls_latest_survey_total');
        display_total = $('polls_latest_survey_display_total');
        
        while (notfound === false) {
          if ($('polls_latest_survey_votes_'+x)) {
            votes[x-1] = $('polls_latest_survey_votes_'+x);
            opts_display[x-1] = $('polls_latest_survey_opts_display_'+x);
            opts[x-1] = $('polls_latest_survey_opts_'+x);
            results[x-1] = $('polls_latest_survey_result_'+x);
            
            sums[0] = ((1*votes[x-1].value)+1);
            sums[1] = (total.value == "0" ? 100 : Math.round((sums[0]/((1*total.value)+1))*100,0) );
            
            results[x-1].innerHTML = "("+(option.value == opts[x-1].value ? sums[0] : votes[x-1].value)+" - "+sums[1]+"%)";
            opts_display[x-1].innerHTML = "<li>";
          } else {
            notfound = true;
          }
          x++;
        }
        
        total.value = ((1*total.value)+1);
        
        display_total.innerHTML = total.value+" vote"+(((1*total.value)) != 1 ? "s" : "");
        go.innerHTML = "<b style = \"color:#0254AA;\">Thank you for voting!</b>";
        
        process.src = root+"vote.php?s="+num.value+"&option="+option.value+"&page="+spage.value+"&go=x";
        
        voted = 1;
      }
    }
  }
}

Survey_Details.prototype.Initialize = survey_details_initialize;
Survey_Details.prototype.OptionsCount = survey_details_options_count;
Survey_Details.prototype.InsertOption = survey_details_insert_option;
Survey_Details.prototype.Vote = survey_details_vote;

var Surveys = new Survey_Details();
    Surveys.Initialize();

