function showQst(namePage)
{
   pageUrl = '/admin/'+namePage+'/'+document.getElementById('cmbPoll').value+'/';
   window.location = pageUrl
}

function showAnsw(idPoll)
{
   pageUrl = '/admin/viewansw/'+idPoll+'/'+document.getElementById('cmbQst').value+'/';
   window.location = pageUrl
}

function addMenuElement()
{
   page_path = document.frm.slPath.options[document.frm.slPath.selectedIndex].text;
   insertText("\n<a href='" + page_path + "'>ИМЯ РАЗДЕЛА</a>")
}

function addColor(nameColor)
{
   if ( nameColor != 0 )
   {
      textColor = "<span style='color:"+nameColor+"'></span>";
      insertText(textColor);
   }
}

function goMenuLanguage()
{
   pageUrl = '/admin/editlist/'+document.frm.slLanguage.selectedIndex+'/'
   window.location = pageUrl
}

function VerifyJS()
{
   var expdate = new Date()
   var monthFromNow = expdate.getTime()
   expdate.setTime(monthFromNow)
   document.cookie = "jsset=1; expires="+expdate.toGMTString()
}

function TextVisible(iOrdinal)
{
   document.getElementById('radiotext' + iOrdinal).disabled = false
}

function CheckExitTextField(iOrdinal, iIndex)
{
   if(document.getElementById('radiotext'+iOrdinal)!=null)
   {
      document.getElementById('radiotext'+iOrdinal).disabled = true
      document.getElementById('radiotext'+iOrdinal).value = ''
   }
}

function CheckMaxSelected(iOrdinal, iElemNum, iMaxChecked)
{
   bFinish = 0
   iIndex = 1
   iSumChecked = 0
   
   while (bFinish != 1)
   {
      if(document.getElementById('checkbox'+iOrdinal + '_' + iIndex)!=null)
      {
         if(document.getElementById('checkbox'+iOrdinal + '_' + iIndex).checked == true) iSumChecked++
         iIndex++
      }
      else bFinish = 1
      
      if (iSumChecked > iMaxChecked) document.getElementById('checkbox'+iOrdinal + '_' + iElemNum).checked = false
   }
}

function VerifyCheckBoxDate(iOrdinal, iElemNum, iCurrentQst)
{
   bFinish = 0
   iIndex = 1
   aAnswedQst[iCurrentQst] = 0

   while (bFinish != 1)
   {
      if(document.getElementById('checkbox'+iOrdinal + '_' + iIndex)!=null)
      {
         if(document.getElementById('checkbox'+iOrdinal + '_' + iIndex).checked == true) aAnswedQst[iCurrentQst] = 1
         iIndex++
      }
      else bFinish = 1
   }
}

function VerifyRadioDate(iOrdinal, iElemNum, iCurrentQst)
{
   if(document.getElementById('radio'+iOrdinal + '_' + iElemNum).checked == true) aAnswedQst[iCurrentQst] = 1
   else aAnswedQst[iCurrentQst] = 0
}

function VerifyRadioTextDate(iOrdinal, iCurrentQst)
{
   if(document.getElementById('radiotext'+iOrdinal).value.length > 0) aAnswedQst[iCurrentQst] = 1
   else aAnswedQst[iCurrentQst] = 0
}

function UnSetQuestion(iCurrentQst)
{
   aAnswedQst[iCurrentQst] = 0
}

function VerifyTextDate(iOrdinal, iCurrentQst)
{
   if(document.getElementById('text'+iOrdinal).value.length > 0) aAnswedQst[iCurrentQst] = 1
   else aAnswedQst[iCurrentQst] = 0
}

function VerifySelect(iOrdinal, iCnt, iCurrentQst)
{
   bFinish = 0
   iIndex = 1
   iSum = 0

   while (bFinish != 1)
   {
      if(document.getElementById('select'+iOrdinal + '_' + iIndex)!=null)
      {
         if(document.getElementById('select'+iOrdinal + '_' + iIndex).options[document.getElementById('select'+iOrdinal + '_' + iIndex).selectedIndex].value != -1) iSum++
         iIndex++
      }
      else bFinish = 1
   }
   
   if (iSum == iCnt) aAnswedQst[iCurrentQst] = 1
   else aAnswedQst[iCurrentQst] = 0
}

function CheckPoll()
{
   bAllAnswered = 1
   sQuestions = 'Дайте, будь-ласка, відповіді на наступні питання: '
   for (i = 0; i < iQstCnt; i++)
   {
      if (aAnswedQst[i] == null || aAnswedQst[i] == 0)
      {
         sQuestions = sQuestions + (i + 1) + ','
         bAllAnswered = 0
      }
   }
   
   if (sQuestions.charAt(sQuestions.length - 1) == ',') sQuestions = sQuestions.substr(0, sQuestions.length - 1)
   
   if (bAllAnswered == 1) return true
   else
   {
      alert(sQuestions)
      return false
   }
}

function JumpToAnswerDetail(iQstId)
{
   sUrl = '/ukr/poll/detail/'+iQstId+'/'
   window.location = sUrl
}

function JumpToResult()
{
   sUrl = '/ukr/poll/result/'+document.frm.frmPollNum.options[document.frm.frmPollNum.selectedIndex].value+'/'
   window.location = sUrl
}

function JumpToResultSmall()
{
   sUrl = '/ukr/poll/result/'+document.frmSm.frmPollNumSmall.options[document.frmSm.frmPollNumSmall.selectedIndex].value+'/'
   window.location = sUrl
}

function AditionalField()
{
   if (document.frm.cmbAnswType.selectedIndex == 1)
   {
      document.getElementById('range').style.display = 'none'
      document.getElementById('variants').style.display = 'block'
   }
   else if (document.frm.cmbAnswType.selectedIndex == 5)
   {
      document.getElementById('variants').style.display = 'none'
      document.getElementById('range').style.display = 'block'
   }
   else
   {
      document.getElementById('variants').style.display = 'none'
      document.getElementById('range').style.display = 'none'
   }
}

function JumpToUrl(sUrl)
{
   iIndex = document.frm.cmbQstNum.options[document.frm.cmbQstNum.selectedIndex].value
   sUrl = sUrl+iIndex+'/'
   window.location = sUrl
}


function insertText(text)
{
   if (document.frm.tfContent.createTextRange && document.frm.tfContent.caretPos)
   {
      var caretPos=document.frm.tfContent.caretPos;
      caretPos.text=text;//caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
   }
   else document.frm.tfContent.value += text;
}

function storeCaret()
{
    if (document.frm.tfContent.createTextRange) document.frm.tfContent.caretPos=document.selection.createRange().duplicate();
}

function previewImage(img_path)
{
   win = open("","Предпросмотр","scrollbars=yes,status=yes,width=500,height=300");
   win.document.open();
   win.document.write("<img src='"+img_path+"'>");
   win.document.close();
}

function addTable()
{
   var i = 0, j = 0;
   tableContent = '<table border=\"0\">\n';

   rowNum = prompt("Количество строк: ", 1);
   colNum = prompt("Количество столбцов: ", 1);

   for (i = 0; i < rowNum; i++)
   {
      tableContent += '<tr>\n';
      for (j = 0; j < colNum; j++)
         tableContent += '   <td></td>\n';
      tableContent += '</tr>\n';
   }

   tableContent += '</table>';
   
   insertText(tableContent);
}

function checkSociologist()
{
  if ( document.frm.tfDesc.value.length == 0 )
  {
    document.frm.tfDesc.style.backgroundColor = "#FFCCCC";
    document.frm.tfDesc.focus()
    alert('Описание пустое!');
    return false;
  }

  return true;
}

function createRequest()
{
  var req = false;
  try
  {
    req = new ActiveXObject("Msxml2.XMLHTTP");
  }
  catch (e)
  {
    try
    {
      req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e2)
    {
      req = false;
    }
  }

  if ( !req && typeof XMLHttpRequest != 'undefined') req = new XMLHttpRequest();

  return req;
}

function checkCode()
{
  var code = document.frm.tfCode.value;
  var req = createRequest();
  var url = '/checkcode/' + code;
  var result;
  result = 0;

  req.open("GET", url, false);
  req.onreadystatechange = function()
  {
    if (req.readyState == 4) result = parseInt(req.responseText);
  }

  req.send(null);

  return result;
}

function checkGuestbook()
{
  if ( document.frm.frmName.value.length == 0 )
  {
    document.frm.frmName.style.backgroundColor = "#FFCCCC";
    document.frm.frmName.focus()
    alert('Поле Имя пусте!');
    return false;
  }

  if ( document.frm.frmEMail.value.length > 0 )
  {
    pattern = /^[\w\-\.\_]{2,20}\@[\w\-\.]{2,20}\.\w{2,3}$/i;
    email = document.frm.frmEMail.value;
    if ( pattern.exec(email) == null )
    {
      document.frm.frmEMail.style.backgroundColor = "#FFCCCC";
      document.frm.frmEMail.focus()
      alert('Поле E-Mail содержит некорректный адрес!');
      return false;
    }
  }

  if ( document.frm.frmICQ.value.length > 0 )
  {
    pattern = /^\d{5,}$/;
    icq = document.frm.frmICQ.value;
    if ( pattern.exec(icq) == null )
    {
      document.frm.frmICQ.style.backgroundColor = "#FFCCCC";
      document.frm.frmICQ.focus()
      alert('Поле ICQ содержит некорректный номер!');
      return false;
    }
  }

  if ( document.frm.tfContent.value.length == 0 )
  {
    document.frm.tfContent.style.backgroundColor = "#FFCCCC";
    document.frm.tfContent.focus()
    alert("Сообщение пустое!");
    return false;
  }

  return true;
}

function checkGbAddForm()
{
  if ( !checkGuestbook() ) return false;

  if (checkCode() == 0)
  {
    document.frm.tfCode.style.backgroundColor = "#FFCCCC";
    document.frm.tfCode.focus()
    alert("Введенный код не совпадает с кодом изображенном на рисунке");
    return false;
  }

  document.frm.action = '/ukr/gb/add/';
}

function checkComment()
{
  if ( document.frm.frmName.value.length == 0 )
  {
    document.frm.frmName.style.backgroundColor = "#FFCCCC";
    document.frm.frmName.focus()
    alert('Поле Имя пусте!');
    return false;
  }

  if ( document.frm.frmEMail.value.length > 0 )
  {
    pattern = /^[\w\-\.\_]{2,20}\@[\w\-\.]{2,20}\.\w{2,3}$/i;
    email = document.frm.frmEMail.value;
    if ( pattern.exec(email) == null )
    {
      document.frm.frmEMail.style.backgroundColor = "#FFCCCC";
      document.frm.frmEMail.focus()
      alert('Поле E-Mail содержит некорректный адрес!');
      return false;
    }
  }

  if ( document.frm.tfContent.value.length == 0 )
  {
    document.frm.tfContent.style.backgroundColor = "#FFCCCC";
    document.frm.tfContent.focus()
    alert("Сообщение пустое!");
    return false;
  }

  if (checkCode() == 0)
  {
    document.frm.tfCode.style.backgroundColor = "#FFCCCC";
    document.frm.tfCode.focus()
    alert("Введенный код не совпадает с кодом изображенном на рисунке");
    return false;
  }

  document.frm.action = '/ukr/article_comment_add/';
}
