
function SgGenerateCaptcha(pollName, incorrect) {
  new Ajax.Request('/svt/road/Classic/shared/poll/captchaGenerateHtml.jsp?pollName='+pollName+'&incorrect='+incorrect,
          {
      method: 'get',
      onComplete: showResponse
  });
  function showResponse(originalRequest) {
      if (200 == originalRequest.status) {
          $('pa'+pollName+'_div').hide();
          $('pa'+pollName+'_captcha').innerHTML = originalRequest.responseText;
          $('pa'+pollName+'_captcha').show();
          try {
              $(pollName+'_captcha_string').focus();
          } catch(ex) {
              // ignore
          }
      }
      else {
          $('pa'+pollName+'_div').hide();
          $('pa'+pollName+'_captcha').innerHTML = "Det går inte att rösta just nu.";
          $('pa'+pollName+'_captcha').show();
      }
  }
  return false;
}

function SgValidateCaptcha(pollName, captchaString) {
  new Ajax.Request('/svt/road/Classic/shared/poll/captchaValidate.jsp?pollName='+pollName+'&captchaString='+captchaString,
          {
      method: 'get',
      onComplete: showResponse
  });
  function showResponse(originalRequest) {
      if (200 == originalRequest.status) {
          var isValid = originalRequest.responseXML.firstChild.firstChild.firstChild.data;
          if(isValid != "true") {
              SgGenerateCaptcha(pollName, true);
          } else {
              var form = $('pa'+pollName+'_form');
              form.action='/svt/road/Classic/shared/poll/voteSubmitSecure.jsp';
              form.submit();
          }
      }
      else {
          $('pa'+pollName+'_div').hide();
          $('pa'+pollName+'_captcha').innerHTML = "Det går inte att rösta just nu.";
          $('pa'+pollName+'_captcha').show();
      }
  }
  return false;
}

function SgVerifyHasVoted(pollForm, inputId) {
    for(var i=0; i < pollForm[inputId].length; i++) {
        var element = pollForm[inputId][i];
        if (element.checked) {
            return true;
        }
    }
    return false;
}
