cursorPos = 0;

function show_sub_url(embedded_id)
{
   div_style = eval("document.all.em"+embedded_id+".style.display")

   if ( div_style == 'none' )
   {
      eval("document.all.em"+embedded_id+".style.display = 'inline'");
      eval("document.all.br"+embedded_id+".style.display = 'none'");
   }
   else
   {
      eval("document.all.em"+embedded_id+".style.display = 'none'");
      eval("document.all.br"+embedded_id+".style.display = 'inline'");
   }
}

function goMenuLanguage(menu_type)
{
   pageUrl = '/admin/'+menu_type+'/'+document.frm.slLanguage.selectedIndex+'/'
   window.location = pageUrl
}

function addMenuElement()
{
   pathPage = document.frm.slPath.options[document.frm.slPath.selectedIndex].text.substr(1, document.frm.slPath.options[document.frm.slPath.selectedIndex].text.length - 2);
   document.frm.tfContent.value += '\nИМЯ РАЗДЕЛА|' + pathPage;
}

function addColor(nameColor)
{
   if ( nameColor != 0 )
   {
      textColor = "<span style=\"color:"+nameColor+"\"></span>";
      insertText(textColor);
   }
}

function insertText(text)
{
   // IE && Opera
   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;
   }
   // Mozilla && FireFox
   else if ( document.frm.tfContent.setSelectionRange )
   {
      var strFirst = document.frm.tfContent.value.substr(0, cursorPos);
      var strEnd   = document.frm.tfContent.value.substr(cursorPos, document.frm.tfContent.value.length);
      document.frm.tfContent.value = strFirst + text + strEnd;
      cursorPos = strFirst.length + text.length
      document.frm.tfContent.selectionStart = document.frm.tfContent.selectionEnd = cursorPos;
   }
   else document.frm.tfContent.value += text;
}

function storeCaret()
{
   if (document.frm.tfContent.createTextRange) document.frm.tfContent.caretPos = document.selection.createRange().duplicate();
   else cursorPos = document.frm.tfContent.selectionStart;
}

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 = '\n<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 addRowUpload()
{
  var tblFiles = document.getElementById('idTblFiles');
  var lastRow = tblFiles.rows.length;

  var i = lastRow + 1;
  var row = tblFiles.insertRow(lastRow);

  var cell = row.insertCell(0);
  var el = document.createElement('input');
  el.setAttribute('type', 'file');
  el.setAttribute('name', 'tfFile_' + i);
  el.setAttribute('size', '30');
  el.setAttribute('value', i);
  cell.appendChild(el);

  document.getElementById('hFilesNum').value = i;
}

function removeRowUpload()
{
  var tblFiles = document.getElementById('idTblFiles');
  var lastRow = tblFiles.rows.length;
  if (lastRow > 1)
  {
    tblFiles.deleteRow(lastRow - 1);
    document.getElementById('hFilesNum').value =  document.getElementById('hFilesNum').value - 1;
  }
}

function checkFeedback() {

    var pattern = /^[\w\-\.\_]{1,100}\@[\w\-\.]{1,100}\.\w{2,10}$/;
    var tfEmail = $('#tfEmail');

    var tfName = $('#tfName');
    if ( tfName.val().length == 0 ) {
        tfName.focus();
            alert('Поле "Ваше имя" пустое!');
        return false;
    }

    if (tfEmail.val().length > 0) { 
        if ( pattern.exec(tfEmail.val()) == null ) {
            tfEmail.focus();
            alert('Поле E-Mail введено не правильно!');
            return false;
        }
    }

    var tfPhone = $('#tfPhone');
    if ( tfPhone.val().length == 0 ) {
        tfPhone.focus();
        alert('Поле "Ваш контактный телефон" пустое! Формат поля: (код) номер');
        return false;
    }

    var tfMsg = $('#tfMsg');
    if ( tfMsg.val().length == 0 ) {
        tfMsg.focus();
        alert('Поле "Текст сообщения" пустое!');
        return false;
    }

    var tfCode = $('#code');
    if ( tfCode.val().length == 0 ) {
        tfCode.focus();
        alert('Поле "Введите код" пустое!');
        return false;
    }

    /*var tfCaptcha = $('#tfCaptcha');
    if ( tfCaptcha.val().length == 0 ) {
        tfCaptcha.focus();
        alert('Введите текст, изображенное на рисунке!');
        return false;
    }*/

    return true;
}

function validatePhone(e) {
    var theEvent = e || window.event;
    var key = theEvent.keyCode || theEvent.which;
    key = String.fromCharCode( key );
    var regex = /[^A-Za-z,]/;
    if( !regex.test(key) ) {
        theEvent.returnValue = false;
        theEvent.preventDefault();
    }
}

$(document).ready(function(){
    $("#gmtbl1 tr:gt(9)").css("display", "none");
});


