var m_Version = 0

//--------------------------------------------------------------
//getters/setters
//--------------------------------------------------------------
function setRecordPattern(str)
{
 document.getElementById('inputRecordPattern').value = str;
}
//--------------------------------------------------------------
function getRecordPattern()
{
 return document.getElementById('inputRecordPattern').value;
}
//--------------------------------------------------------------
function setSnapshotPattern(str)
{
 document.getElementById('inputSnapshotPattern').value = str;
}
//--------------------------------------------------------------
function getSnapshotPattern()
{
 return document.getElementById('inputSnapshotPattern').value;
}
//--------------------------------------------------------------
//--------------------------------------------------------------
function setTimePattern(str)
{
 document.getElementById('inputTimePattern').value = str;
}
//--------------------------------------------------------------
function getTimePattern()
{
 return document.getElementById('inputTimePattern').value;
}
//--------------------------------------------------------------
function setEnabled(val)
{
 document.getElementById('enabledId').checked = !!val;
}
//--------------------------------------------------------------
function getEnabled()
{
 return document.getElementById('enabledId').checked;
}
//--------------------------------------------------------------
//--------------------------------------------------------------
function setPreviewRec(str)
{
 document.getElementById('preViewRec').textContent = str;
}
//--------------------------------------------------------------
function setPreviewSnap(str)
{
 document.getElementById('preViewSnap').textContent = str;
}
//--------------------------------------------------------------
function setPreviewTime(str)
{
 document.getElementById('preViewTime').textContent = str;
}
//--------------------------------------------------------------
//--------------------------------------------------------------
//
//--------------------------------------------------------------
function reloadRecPreview()
{
 window.CHtmlDialog.callLua2('updateRecordPreview', getRecordPattern(),getTimePattern() );
}
//--------------------------------------------------------------
function reloadSnapPreview()
{
 window.CHtmlDialog.callLua2('updateSnapshotPreview', getSnapshotPattern(),getTimePattern() );
}
//--------------------------------------------------------------
function reloadTimePreview()
{
 window.CHtmlDialog.callLua1('updateTimePreview', getTimePattern() );
}
//--------------------------------------------------------------
function resetToDefaults()
{
 window.CHtmlDialog.callLua('resetToDefaults');
}
//--------------------------------------------------------------
function startUp()
{
 updateEnabled();
 document.getElementById('inputRecordPattern').focus();  
}
//--------------------------------------------------------------
function updateEnabled()
{
 let color = getEnabled() ? 'black' : 'grey';
  
 document.getElementById('inputRecordPattern').style.color = color;
 document.getElementById('preViewRec').style.color = color;
 
 document.getElementById('inputSnapshotPattern').style.color = color;
 document.getElementById('preViewSnap').style.color = color;
 
 document.getElementById('inputTimePattern').style.color = color;
 document.getElementById('preViewTime').style.color = color;

}
//--------------------------------------------------------------
function addVar(str)
{
 let input = undefined;
 let rec  = document.getElementById('inputRecordPattern');
 let snap = document.getElementById('inputSnapshotPattern');

 if (rec === document.activeElement) 
     input = rec;
 else if (snap === document.activeElement)
     input = snap;
 
 if (input === undefined) return;
 let scrollPos = input.scrollTop;
 let pos = 0;
 let browser = ((input.selectionStart || input.selectionStart == "0") ? 
    "ff" : (document.selection ? "ie" : false ) );
  if (browser == "ie") { 
    input.focus();
    let range = document.selection.createRange();
    range.moveStart ("character", -input.value.length);
    pos = range.str.length;
  }
  else if (browser == "ff") { pos = input.selectionStart };

  let front = (input.value).substring(0, pos);  
  let back = (input.value).substring(pos, input.value.length); 
  input.value = front+str+back;
  pos = pos + str.length;
  if (browser == "ie") { 
    input.focus();
    let range = document.selection.createRange();
    range.moveStart ("character", -input.value.length);
    range.moveStart ("character", pos);
    range.moveEnd ("character", 0);
    range.select();
  }
  else if (browser == "ff") {
    input.selectionStart = pos;
    input.selectionEnd = pos;
    input.focus();
  }
  input.scrollTop = scrollPos; 
}
//--------------------------------------------------------------
//translate
//--------------------------------------------------------------
function tr(str)
{ 
 return new Promise(function(resolve, reject) 
  {
   window.CHtmlDialog.htmlTranslate(str,'simpleTV::recordPattern'
        ,function(data)
        {
          resolve(data);
        });
  });
}
//--------------------------------------------------------------
function doTranslate()  
{
 //Caption
 tr('Record Pattern').then(data => document.getElementById('mainCaptionText').textContent  = data + ' ' + m_Version);
 
 //ToolBar
 tr('Channel').then(data => document.getElementById('toollBarButtonCnannelName').textContent = data);
 tr('Group').then(data => document.getElementById('toollBarButtonGroupName').textContent = data);
 tr('ExtFilter').then(data => document.getElementById('toollBarButtonExtFilterName').textContent = data);
 tr('Time').then(data => document.getElementById('toollBarButtonTime').textContent = data);
 tr('Timeshift').then(data => document.getElementById('toollBarButtonTimeshift').textContent = data);
 tr('Reset').then(data => document.getElementById('toollBarButtonReset').textContent = data);
 
 tr('EPG').then(data => document.getElementById('toollBarButtonEpgTitle').textContent = data);
 tr('EPG category').then(data => document.getElementById('toollBarButtonEpgCat').textContent = data);
 tr('EPG start').then(data => document.getElementById('toollBarButtonEpgStart').textContent = data);
 tr('EPG end').then(data => document.getElementById('toollBarButtonEpgEnd').textContent = data);
  
 //Inputs
 tr('Format for record').then(data => {
 let label = document.getElementById('labelRecordPatern');
 let val = getRecordPattern();
 label.innerHTML = data + label.innerHTML;
 setRecordPattern(val);
 });
 tr('Format for snapshot').then(data => {
 let label = document.getElementById('labelSnapshotPatern');
 let val = getSnapshotPattern();
 label.innerHTML = data + label.innerHTML;
 setSnapshotPattern(val);
 });
 tr('Format for time').then(data => {
 let label = document.getElementById('labelTimePatern');
 let val = getTimePattern();
 label.innerHTML = data + label.innerHTML;
 setTimePattern(val);
 });
}
//--------------------------------------------------------------
