// Init sliders

$(document).ready(function(){
  var i=10;
  function load(i){
    $("#slider"+i).slider({
      value:10,
      min: 0,
      max: 10,
      step: 1,
      slide: function(event, ui){
        $("#iSlider"+i).val(ui.value);
        $("#sSlider"+i).html(ui.value);
      }
    });
    $("#iSlider"+i).val($("#slider"+i).slider("value"));
  };
  while(i--)
    load(i);
});


// Data
BG={
  vars:{
    antivirus_scanning:1,
    central_management:2,
    efficiency:3,
    email:4,
    firewall:5,
    install_ease:6,
    phone_support:7,
    quantity:8,
    realtime:9,
    scan_speed:10,
    spyware:11,
    network_size:12,
    system_type:13,
    virus_update:14,
    firewall_flag:15
  },
  pos:0,
  form:$('#BGForm').get(0),
  questions:[[ // Home
     'BGQ1',
     'BGQ2',
     'BGQ3',
     'BGQ4',
     'BGQ5'
   ],
   [  // Office
     'BGQ1',
     'BGQ2',
     'BGQ3',
     'BGQ4',
     'BGQ5',
     'BGQ6'//,
//     'BGQ7'
   ],
   [  // Portable
     'BGQ1',
     'BGQ2',
     'BGQ3',
     'BGQ4',
     'BGQ5'
   ]
  ],
  current:null
};

// Init
BG.current=BG.questions[0];
$('#BGQ1',BG.form).show();

// Methods

BG.getElement=function(a)
{
  var e=BG.form.elements,p=[],i=e.length,f;
  while(i--)
    {
      f=e[i];
      if(f['name'])
	{
	  if(f['type']=='radio'&&!f['checked'])
	    continue;

	  if(f['name']==a)
	    return f;

	  p.push(f);
	}
    }

  return a?null:p;
};

/**
 *  Submit the form
 */
BG.submit=function()
{
  //BG.form.submit();
  
  var e=BG.getElement(),p=[],i=e.length,f;
  while(i--)
    {
      f=e[i];
      p.push(escape(f['name'],1)+'='+escape(f['value'],1));
    }

  window.location.href = BG.url_get_result + '?' + p.join('&');
  /*
  $.ajax({
    type:'POST',
    url: BG.url_get_result,
    cache: false,
    data: p.join('&'),
    success: function(html){
      $("#results").html(html);
    }
  });
  */
};

/**
 * Show the question
 * a Question index
 */
BG.show=function(a)
{
  var f=BG.form;
  // Hide all questions
  $('div.BG-Question',f).hide();

  // Show current question
  $('#'+BG.current[a],f).show();
};

/**
 * Move to the previous question
 */
BG.back=function()
{
  var p=BG.pos,f=BG.form;

  if(--p<=0)
  {
    p=0;
    $('#BGBack',f).get(0).disabled=true;
  }

  $('#BGNext',f).html('Next');

  BG.show(p);
  BG.pos=p;
};

/**
 * Move to the next question
 */
BG.next=function()
{
  var a,f=BG.form,p=BG.pos;

  
  
  
  // If submit is pressed since pos should be == to max questions
  if(p>=BG.current.length-1)
  {
    BG.storeAnswer(BG.current.length-1);
    BG.submit();
    return;
  }
  
  try{
	this_step = (p+2);
	total_steps = BG.current.length;
	//alert("this_step: " + this_step);
	//alert("total_steps: " + total_steps);
	
	var progress_msg = "Step " + this_step + " of " + total_steps;
	if(this_step <= total_steps) {
		document.getElementById("progress").innerHTML = progress_msg;
	}
  }
  catch(e){
   
  }
  // If pos == 0
  if(!p++)
  {
    a=$('input[name="system_type"]:checked',f).val();
    BG.current=BG.questions[a-1];
  }

  if(p==1)
  {
    if(a===undefined)
      a=$('input[name="system_type"]:checked',f).val();

    $('#BGQ3a',f).html(a==1?'office':'home');
  }

  BG.storeAnswer(p-1);

  // If pos >= current max questions
  if(p>=BG.current.length-1)
  {
    // Change button to submit
    $('#BGNext',f).html('Submit');
  }

  $('#BGBack',f).get(0).disabled=false;
  BG.show(p);
  BG.pos=p;
};

/**
 * Store the answer of a question
 * pos Question previous position
 */
BG.storeAnswer=function(pos)
{
  var e,a=[],i,eid;

  switch(pos)
  {
  case 0:
    a.push('system_type');
    break;

  case 1:
    a.push('quantity');
    break;

  case 2:
    a.push('antivirus_scanning');
    a.push('firewall');
    a.push('spyware');
    a.push('realtime');
    a.push('virus_update');
    a.push('email');
    a.push('install_ease');
    a.push('scan_speed');
    a.push('efficiency');
  case 3:
    a.push('phone_support');
    break;

  case 4:
    a.push('firewall_flag');
    break;

  case 5:
    a.push('network_size');
    break;

  case 6:
    a.push('central_management');
    break;

  default:
    break;
  }

  i=a.length;
  while(i--)
    {
      e=BG.getElement(a[i]);
      eid=BG.vars[a[i]];
      $.ajax({
	type:'POST',
	url: BG.url_store_answer,
	cache: false,
	data: 'vid='+eid+'&a='+e.value,
	success: function(html){}
      });
    }
};

/////////////////////////////

BG.url_store_answer='/buyers_guide/json.php?method=store_answer';
BG.url_get_result='/buyers_guide/Customized-Antivirus-Recommendations.html';
