function $(id){return document.getElementById(id);}

String.prototype.trim = function(){
a = this.replace(/^\s+/, '');
return a.replace(/\s+$/, '');
};

aIDcounter = 1;


function addAnswer(ID, answerText, pos, position)
{
  if(aIDcounter > 99)
  {
    promptNow('Too many answers', 'You can\'t enter more than 20 answers for a poll.', 'none', 'Close', 'Close', 'close');
  }
  else
  {
   if(ID == 0){ID = aIDcounter;}  
    li = document.createElement('li');

    temp = '<div class="answer"><div class="but">';
    temp +=  '<a href="javascript:moveAnswer('+ ID +',\'up\');"><img src="/images/i_o_up.gif" width="15" height="16" alt="up" title="Click here to move this answer up"  /></a>';
    temp +=  '<a href="javascript:moveAnswer('+ ID +',\'dn\');"><img src="/images/i_o_dn.gif" width="15" height="16" alt="dn" title="Click here to move this answer down" /></a>';
    temp +=  '</div>';
    answerText = answerText.replace(/\"/,"&quot;");
    temp +=  '<div class="input"><input type="text" onblur="answerCheck(this);" value="'+ answerText +'" id="answer'+ ID +'" tabindex="'+(aIDcounter+10)+'"  name="aID'+ ID +'" maxlength="200" tabindex="'+ position + '" /></div>';
    temp +=  '<div class="but2">';
   temp +=  '<a href="javascript:addAnswer(0,\'New Answer\','+ ID +');"><img src="/images/i_o_new.gif" width="15" height="16" alt="new"  title="Click here to create a new answer"  /></a>';
    temp +=  '<a href="javascript:delAnswer('+ ID +');"><img src="/images/i_o_del.gif" width="15" height="16" alt="delete"  title="Click here to delete this answer"  /></a>';
    temp += '</div><p class="cf"></p></div>';

    li.innerHTML = temp;
    li.id =  ID +'.li';
  
    if(pos == 'new' || pos == 'update')
    {
      aList.appendChild(li);
    }
    else
    {
      insert = $(pos +'.li');
      if(insert == aList.lastChild)
      {
        aList.appendChild(li);
      }
      else
      {
        aList.insertBefore(li,insert.nextSibling);
      }
    }
    aIDcounter++;
  }
}

function answerCheck(elm)
{
 if(elm.value.trim().length < 1)
 {
  elm.value = "Answer";
 }
}

function moveAnswer(ID,dir)
{
  li = $(ID +'.li');

  if((li == aList.firstChild && dir == 'up') || (li == aList.lastChild && dir == 'dn'))
  {
    // Do nothing
  }
  else
  {
    if(dir == 'up')
    {
      insert = li.previousSibling;
      li = aList.removeChild(li);
      aList.insertBefore(li,insert);
    }
    else
    {
      insert = li.nextSibling.nextSibling;
      li = aList.removeChild(li);
      aList.insertBefore(li,insert);
    }
  }
}

function delAnswer(ID)
{
  pass = true;
  if(aIDcounter < 3)
  {
    pass = false;
    alert('You must have at least 2 answers in your poll.');
  }
  if(ID > 1000 && pass == true)
  {
    if(!confirm('Are you sure you want to delete this answer? \n Any votes you have collected for this answer will be lost.'))
    {
      pass = false;
    }
  }
  if(pass == true)
  {
    aList.removeChild($(ID +'.li'));
    if(ID > 1000)
    {
      $('delAnswers').innerHTML += '<input type="hidden" value="del" class="optEdit" id="answer'+ ID +'Type"  name="aType'+ ID +'"/>';
    }
    aIDcounter --;
  }
}

function submitPoll()
{
  pass = true;
  TW_pass = true;
  
  if($('pQuestion').value.trim().length < 1)
  {
    pass = false;
    $('pQuestionErr').style.display = 'block';
  }else{$('pQuestionErr').style.display = 'none';}
  
  if($('pLanguage').value < 1)
  {
    pass = false;
    $('pLanguageErr').style.display = 'block';
  }else{$('pLanguageErr').style.display = 'none';}
  
  if($('TW_username').value.trim().length < 1)
  {pass = false;TW_pass = false;}

  if($('TW_password').value.trim().length < 1)
  {pass = false;TW_pass = false;}

  if(TW_pass)
  {$('TW_passwordErr').style.display = 'none';}
  else{$('TW_passwordErr').style.display = 'block';}
  
  if(pass == true)
  {
   $('submitBut').innerHTML = '<img src="/images/but_wait.gif" width="211" height="33" />';
   $('TW_poll').submit();
  }
}

function left(str, n){
if (n <= 0)
    return "";
else if (n > String(str).length)
    return str;
else
    return String(str).substring(0,n);
}

pic1 = new Image(); 
pic1.src="/images/but_wait.gif";

