var TimeOut, Popup;

function ElementGet(AIDOrName,AWindow)
{
  var LWindow=(AWindow?AWindow:window);
  var LElement=LWindow.document.getElementById(AIDOrName);
  
  if(LElement)return LElement;
  else return LWindow.document.getElementsByName(AIDOrName)[0];
}

function ParentWindowReload()
{
  try
  {
    var LWindow = top.opener.top;
    LWindow.location.reload();
  }
  catch(e)
  {
  }
}

function Hit(AS, AA, ATB, APopup, AMK, AW, AH, AR)
{
  var LStr = '';
  var i = 0;
  
  if (!AA)
    AA = 'A';
  
  if (!APopup)
  {
    if (!AS)
      AS = 'H';
    document.frmSubmit.S.value = AS;

    if (AA)
      document.frmSubmit.A.value = AA;

    if (SIDI)
      document.frmSubmit.SIDI.value = SIDI;

    if (SKey && MSKey)
      document.frmSubmit.SKey.value = (AMK?MSKey:SKey);
      
    if (ATB)
    {
      while (i<=ATB.length-2)
      {
        LStr = LStr + '<input type="hidden" name="' + ATB[i] + '" value="' + ATB[i+1] + '">';
        i+=2;
      }

      if (document.getElementById('frmDiv'))
        document.getElementById('frmDiv').innerHTML = LStr;
    }
    ElementGet('frmSubmit').submit();
  }
  else
  {
    if (ATB)
      while (i<=ATB.length-2)
      {
        LStr = LStr + ATB[i] + '=' + ATB[i+1] + '&';
        i+=2;
      }

    var LURL = document.location + '?' +
    'S='+ (AS?AS:'H') + '&' +
    (AA?'A='+AA+'&':'') +
    (SIDI?'SIDI='+SIDI+'&':'') +
    (MSKey?'SKey=' + MSKey + '&':'') +
    LStr;
    Popup(LURL, (AW?AW:800), (AH?AH:600), true, (AR?AR:true), true, 'WA', false, false, false, false);
  }
}

function Logon()
{
  var LLogin = (ElementGet('Login')? ElementGet('Login').value: '');
  var LPassword = (ElementGet('Password')? ElementGet('Password').value: '');
  Hit(WS_LOGIN, 'S', ['Login',LLogin,'Password',LPassword]);
}

function Exit()
{
  if (confirm('Do you want exit?'))
    top.window.close();
}

function ButtonImageSet(APrefix, AButton, ASuffix)
{
  var LButton = ElementGet(AButton);
  var LIndex = LButton.src.lastIndexOf('/');
  LButton.src = LButton.src.substring(0, LIndex) + '/' + APrefix + AButton + (ASuffix?ASuffix:'') + '.gif';
}

function ButtonEnableSet(AButton)
{
  ButtonImageSet('s', AButton);
}

function ButtonDisableSet(AButton)
{
  ButtonImageSet('', AButton);
}

function ParentWindowRefresh(ASection, AAction)
{
  if (!Popup)
    return ;

  if (Popup.closed)
  {
    Hit(ASection, AAction);
    clearTimeout(TimeOut);
  }
  else
    TimeOut = setTimeout('ParentWindowRefresh(\'' + ASection + '\', \'' + AAction + '\');', 1000);
}

function SelectAllCheck(AObjName)
{
  var LResult = ElementGet('SelectAll').checked;
  var LCollection = document.getElementsByName(AObjName);
  for (i=0;LCollection.item(i) != null; i++)
    LCollection.item(i).checked = LResult;
}

function CheckedValueGetCheck(AObjectName)
{
  var LCollection  = document.getElementsByName(AObjectName);
  var LSelectedValue;
  
  if (!LCollection)
  {
    alert('Internal Error: Please reload this page and resubmit later.');
    return false;
  }
  
  for (i=0; LCollection.item(i) != null; i++)
    if (LCollection.item(i).checked)
    {
      if (LSelectedValue)
        alert('Please select one record from list');
      else
        LSelectedValue = LCollection.item(i).value;
    }
    
  return LSelectedValue;
}

function CheckedValuesGetCheck(AObjectName)
{
  var LCollection  = document.getElementsByName(AObjectName);
  var LSelectedValues = '';

  if (!LCollection)
  {
    alert('Internal Error: Please reload this page and resubmit later.');
    return false;
  }

  for (i=0; LCollection.item(i)!=null;i++)
    if (LCollection.item(i).checked)
      LSelectedValues = (LSelectedValues?LSelectedValues + ', ':'') + LCollection.item(i).value;

  if (!LSelectedValues)
    alert('Please select some record(s) from list');
  return LSelectedValues;
}

var ContextMenuParams;

function CM_EvalArgumentProcess(AArgID)
{
  eval(ContextMenuParams[AArgID]);
}

function ContextMenuHide()
{
  if (ElementGet('divContextMenu'))
    ElementGet('divContextMenu').style.display = 'none';
  if (ElementGet('divLayoutMenu'))
    ElementGet('divLayoutMenu').style.display = 'none';
}

function ContextMenuItemHighlight(AItem, AMouseOver)
{
  AItem.style.borderStyle = (AMouseOver ? 'inset' : 'solid');
  AItem.style.backgroundColor = (AMouseOver ? '#DDDDDD' : '#EEEEEE');
}

function ContextMenuShow(ATitle, AParams, event)
{
  ContextMenuParams = AParams;

  for (i = 0; i < ContextMenuParams.length; i++)
    ElementGet('CM_Item' + i).style.display = ContextMenuParams[i] == '' ? 'none': 'inline';

  ElementGet('tdCMTitle').innerHTML = '<b>' + ATitle + '</b>';

  with (ElementGet('divContextMenu'))
  {
    style.display = 'block';
    style.left = ((document.body.clientWidth - event.clientX) < clientWidth ? event.clientX - clientWidth : event.clientX);
    style.top = ((document.body.clientHeight - event.clientY) < clientHeight ? event.clientY - clientHeight : event.clientY);
  }

  if (navigator.userAgent.indexOf('MSIE') < 0)
    event.preventDefault();
  else
    event.returnValue = false;
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function findPos(obj) 
{
  var curleft = curtop = 0;

  if (obj.offsetParent) 
  {
    do 
    {
      curleft += obj.offsetLeft;
      curtop  += obj.offsetTop;
    } while (obj = obj.offsetParent);

  }
  return [curleft,curtop];
}

function PreviewClose()
{
  document.body.removeChild(ElementGet('divCustomPreviewWindow'));
  document.body.removeChild(ElementGet('divTransparentWindow'));
}

function PhotosCountGet()
{
  if (window.PhotoCount)
    return window.PhotoCount;

  var LLast = 1;
  var LMax = 1;
  var LColl = document.getElementsByTagName('img');
  for (var i = 0; i < LColl.length; i++)
  {
    if(LColl[i].src.indexOf('small') != -1)
      LLast = new Number(LColl[i].src.substr(LColl[i].src.lastIndexOf('small') + 5, 2));
    if(LMax < LLast)
      LMax = LLast;
  }

  return LMax;
}

function ImageNavigatorInit(AObj)
{
  var LCurrentID = new Number(AObj._id);
  var LlastID = new Number(PhotosCountGet());

  if (LlastID == 1)
  {
    ElementGet('aPrev').removeAttribute('href');
    ElementGet('aNext').removeAttribute('href');
  }
  else
  if (LCurrentID.valueOf() == LlastID.valueOf())
  {
    ElementGet('aPrev').href = 'javascript:PhotoShow(\'Prev\');';
    ElementGet('aNext').removeAttribute('href');
  }
  else 
  if (LCurrentID.valueOf() == 1)
  {
    ElementGet('aPrev').removeAttribute('href');
    ElementGet('aNext').href = 'javascript:PhotoShow(\'Next\');';
  }
  else
  {
    ElementGet('aPrev').href = 'javascript:PhotoShow(\'Prev\');';
    ElementGet('aNext').href = 'javascript:PhotoShow(\'Next\');';
  }
}

function PhotoPreview(LObj)
{
  if (window.innerHeight)
  {
    var LHeight = window.innerHeight;
    var LWidth = window.innerWidth;
  }
  else
  if (document.body.clientHeight)
  {
    var LHeight = document.body.clientHeight;
    var LWidth = document.body.clientWidth;
  }
  else
  if (document.documentElement && document.documentElement.clientHeight)
  {
    var LHeight = document.documentElement.clientHeight;
    var LWidth = document.documentElement.clientWidth;
  }
  else
  {
    var LHeight = '102%';
    var LWidth = '102%';
  }
  
  var LDiv = document.createElement('div');
  LDiv.id = 'divTransparentWindow';
  LDiv.style.position = 'absolute';
  LDiv.className = 'sT';
  LDiv.style.top = '0px';    
  LDiv.style.left = '0px';
  LDiv.style.width = LWidth;    
  LDiv.style.height = findPos(ElementGet('tdFooter'))[1] + 51; 
  LDiv.style.backgroundColor = '#000000';
  LDiv.style.zIndex = '10';
  LDiv.align = 'center';
  LDiv.valign = 'middle';
  document.body.appendChild(LDiv);

  var LWL = (LWidth / 2) - 350;

  var LWindow = document.createElement('div');
  LWindow.id = 'divCustomPreviewWindow';
  LWindow.style.position = 'absolute';
  LWindow.style.left = LWL + 'px';
  LWindow.style.width = '700px';
  LWindow.style.backgroundColor = '#FFEDA9';
  LWindow.style.zIndex = '20';
  LWindow.align = 'center';
  LWindow.valign = 'top';
  LWindow.style.visibility = 'hidden';

  var LImg = document.createElement('img');
  LImg.id = 'imgPreview';
  LImg.src = LObj.src.replace('small', 'big') + '?TS=' + Date();
  LImg.style.position = 'relative';
  LImg.style.top = '-20';
  LImg.wh = LHeight;
  LImg.tdh = LDiv.height;
  LImg.cw = LWindow;
  LImg.onload = function () {
    var LWT = (this.wh / 2) - ((this.height + 100) / 2) + getScrollXY()[1];
    if (LWT > this.tdh) 
      LWT = this.tdh - 700;
    this.cw.style.top = LWT;

    this.cw.style.visibility = 'visible';
    ElementGet('tblControl').width = this.width;
  };
  LImg._id= LObj.src.substr(LObj.src.lastIndexOf('small') + 5, 2);

  var LHeader = document.createElement('div');
  LHeader.style.backgroundImage = 'url(http://tsisaryk.com/templates/images/browse/merchant/demo2/preview_header.jpg)';
  LHeader.style.width = '700px';
  LHeader.style.height = '70px';
  LHeader.align = 'right';
  LHeader.valign = 'top';
  LHeader.innerHTML = 
    '<div width="50" height="50" valign="top"><img src="http://tsisaryk.com/templates/images/browse/merchant/demo2/close.png" width="50" heigth="50" style="cursor:pointer;" onclick="PreviewClose();"></div>';

  if (document.location.href.indexOf('/en/') != -1)
  {
    var LbtnNext = 'Next photo';
    var LbtnPrev = 'Previous photo';
  }
  else
  if (document.location.href.indexOf('/ru/') != -1)
  {
    var LbtnNext =  'Следующее фото';
    var LbtnPrev = 'Предыдущее фото';
  }
  else
  {
    var LbtnNext = '&#1053;&#1072;&#1089;&#1090;&#1091;&#1087;&#1085;&#1077; &#1092;&#1086;&#1090;&#1086;';
    var LbtnPrev = '&#1055;&#1086;&#1087;&#1077;&#1088;&#1077;&#1076;&#1085;&#1108; &#1092;&#1086;&#1090;&#1086;';
  }


  LWindow.appendChild(LHeader);
  LWindow.appendChild(LImg);
  var LControl = document.createElement('div');
  LControl.style.height = '30';
  LControl.style.width = '700px';
  LControl.align = 'center';
  LControl.innerHTML = '<table id="tblControl" width="600" border="0" cellpadding="0" cellspacing="0" style="color:#E4C885;font-family:Arial;font-size:12px;"><tr><td align="left">' +
    '&lt;<a href="javascript:PhotoShow(\'Prev\');" style="allign" id="aPrev">' +
    LbtnPrev +
    '</a>' +
    '</td><td align="right">' +
    '<a href="javascript:PhotoShow(\'Next\');" id="aNext">' + 
    LbtnNext +
    '</a>' +
    '&gt;</tr></table>';
  LWindow.appendChild(LControl);

  document.body.appendChild(LWindow);
  ImageNavigatorInit(LImg);
}

function PhotoShow(ADirection)
{
  var LImg = ElementGet('imgPreview');
  var LCurrentID = new Number(LImg._id);
  if (ElementGet('a' + LCurrentID))
    ElementGet('a' + LCurrentID).href = 'javascript:PhotoShow(' + LCurrentID + ');';
  if (isNaN(ADirection))
    LCurrentID = (ADirection == 'Next'? LCurrentID + 1: LCurrentID - 1);    
  else
    LCurrentID = ADirection;    
  if (ElementGet('a' + LCurrentID))
    ElementGet('a' + LCurrentID).removeAttribute('href');
  LImg.src = LImg.src.replace(LImg._id, (LCurrentID>9?LCurrentID:'0' + LCurrentID));
  LImg._id = (LCurrentID>9?LCurrentID:'0' + LCurrentID);

  ImageNavigatorInit(LImg);
}

function LatterSend()
{
  if ((ElementGet('LeterContent').value.replace(' ', '') == '') 
    || (ElementGet('Name').value.replace(' ', '') == '') 
    || (ElementGet('EMail').value.replace(' ', '') == '')
  )
  {
    if (document.location.href.indexOf('/en/') != -1)
      alert('Please fill all required fields to sent letter');
    else
    if (document.location.href.indexOf('/ru/') != -1)
      alert('Чтобы отправить письмо необходимо заполнить все поля');
    else 
      alert('Для відправки листа необхідно заповнити всі поля');
    return false;
  }

  var LConteiner = document.createElement('iframe'); 
  LConteiner.style.display = 'none';
  LConteiner.src = 'http://tsisaryk.com/iframe.htm';
  document.body.appendChild(LConteiner);

  if (document.location.href.indexOf('/en/') != -1)
    alert('Letter was successfully sent');
  else
  if (document.location.href.indexOf('/ru/') != -1)
    alert('Письмо успешно отправлено');    
  else 
    alert('Лист успішно відправленно');
}

function LinkHighlight(AObj, AHighlight)
{  
  var LImg = AObj.getElementsByTagName('img')[0];
  LImg.src = LImg.src.replace((AHighlight?0:1),(AHighlight?1:0));
}
