function surveyVote(id, inputOther)
{
  var answer = -1
  for (var i = 0; i < document.forms.survey.answer.length; i++ )
  {
    if ( document.forms.survey.answer[i].checked )
    {
      answer = document.forms.survey.answer[i].value
      break
    }
  }
  if ( answer == -1 )
  {
    parent.showAlert( surveyChooseAnswerMsg, 'info')
    return
  }
  var other = ""
  if ( inputOther )
    other = document.forms.survey.othertext.value
  var params = {
                id: id,
                answer: answer,
                other: other }
  var result = rpcQuery('rpc.php?function=surveys.vote', params)
  if ( result[0] == 'OK' )
  {
    parent.showAlert( result[1], 'info')
    location.reload()
    return
  }
  parent.showAlert( result[1], 'info')
}

function selectAnswer(answer)
{
  for (var i = 0; i < document.forms.survey.answer.length; i++ )
  {
    if ( document.forms.survey.answer[i].value == answer )
    {
      document.forms.survey.answer[i].checked = true
      break
    }
  }
}


$('document').ready( function () 
{
  $('#surveyForm input:button:last').click( function() 
  {
    //$(this).attr('disabled',true).next().show();
    $.post( 'ajax.php?function=surveys.vote', $(this).parents('form').serialize(), handleSurveyForm,'json');

  })
})

function handleSurveyForm ( res )
{
  $('#surveyMsg').text( res.desc ).show(300);
  $('#surveyForm input:button:last').removeAttr('disabled').next().hide();
  $('#survey .question').removeClass('error');

  if ( res.result == 'OK') {
   setTimeout( handleSurveyFormSuccess, 5000);
  } else if ( typeof(res.validateInfo) != 'undefined') {
    for( i = 0; i < res.validateInfo.length; i++ ) {
      $('#survey .q_' + res.validateInfo[i]['id']).addClass('error');
    }
  }

}

function handleSurveyFormSuccess()
{
  $("#surveyMsg").text('').hide(300)
  document.getElementById("surveyForm").reset()
}
