var CaptchaImg = null;
var CaptchaNewImg = null; 
var CaptchaParent = null;
var CaptchaPrompt = null;
var CaptchaReload = null;

function captchaStart(imgId, prompt)
{
  CaptchaImg = document.getElementById(imgId);
  
  if (!CaptchaImg)
  {
    return;
  }
  
  CaptchaImg.alt = prompt;
}
function captchaLoadSound(imgId, soundPlaceholderId)
{
  CaptchaImg = document.getElementById(imgId);
  
  if (!CaptchaImg)
  {
    return;
  }
  
  var src = CaptchaImg.src;
  
  var i = src.indexOf('?get=image');
  var newSrc = src.substr(0, i) + '?get=sound' + src.substr(i + 10);

  i = newSrc.indexOf('&di=');
  if (i > 0)
    newSrc = newSrc.substr(0, i);

  newSrc += '&d=' + captchaGetTimestamp();

  var placeholder = document.getElementById(soundPlaceholderId);
  var objectSrc = "<object id='captchaSound' classid='clsid:22D6F312-B0F6-11D0-94AB-0080C74C7E95' height='0' width='0' style='width:0; height:0;'><param name='AutoStart' value='1' /><param name='Volume' value='0' /><param name='PlayCount' value='1' /><param name='FileName' value='" + newSrc + "' /><embed id='captchaSoundEmbed' src='" + newSrc + "' autoplay='true' hidden='true' volume='100' type='" + captchaGetMimeType() + "' style='display:inline;' /></object>";

  placeholder.innerHTML = '';
  placeholder.innerHTML = objectSrc;
}
function captchaGetTimestamp()
{
  var d = new Date();
  return (d.getTime() + (d.getTimezoneOffset() * 60000));
}
function captchaGetMimeType()
{
    return 'audio/x-wav';
}
function captchaReloadImage(anchor, imgId, id1Id, id2Id, prompt)
{
  CaptchaReload = anchor;
  
  if (!CaptchaReload)
  {
    return;
  }
  
  CaptchaReload.disabled = true;
  CaptchaImg = document.getElementById(imgId);
  var src = CaptchaImg.src;
  var id1 = document.getElementById(id1Id);
  var id2 = document.getElementById(id2Id);
  var id1value = id1.value;
  var id2value = id2.value;
  id1.value = id2value;
  id2.value = id1value;
    
  var newSrc = src.substr(0, src.indexOf(id1value)) + id2value +'&di=' + id1value + '&d' + captchaGetTimestamp();

  CaptchaNewImg = document.createElement('img');
  CaptchaNewImg.onload = captchaShowImage;
  CaptchaNewImg.id = CaptchaImg.id;
  CaptchaNewImg.alt = CaptchaImg.alt;
  CaptchaNewImg.src = newSrc;

  CaptchaPrompt = document.createElement('span');
  CaptchaPrompt.appendChild(document.createTextNode(prompt));

  CaptchaParent = CaptchaImg.parentNode;
  CaptchaParent.removeChild(CaptchaImg);
  CaptchaParent.appendChild(CaptchaPrompt);
}
function captchaShowImage()
{
  if(CaptchaReload && CaptchaNewImg && CaptchaParent && CaptchaPrompt)
  {
    CaptchaParent.removeChild(CaptchaPrompt);
    CaptchaParent.appendChild(CaptchaNewImg);
    CaptchaReload.disabled = false;
  }
}

