// Error Messages Showing
// 2009 (AlexS)

var isErrorElementsCreated = false;
var errorBlockWidth = 450;
var errorBlockHeight = 90;
var ModalWindowActive = false;
var MainForm = null;

function ShowModalErrorMessage(aForm, aErrorMessage)
{
  ShowErrorMessage(aErrorMessage);
  ModalWindowActive = true;
  MainForm = aForm;
}

function ShowErrorMessage(aErrorMessage)
{
  if (!isErrorElementsCreated) CreateErrorElements();

  var objErrorOverlay = document.getElementById('error_overlay');
  var objErrorBlock   = document.getElementById('error_block');
  var objErrorMessage = document.getElementById('error_message');

  var arrayPageSize = getPageSize();
  var arrayPageScroll = getPageScroll();

  objErrorOverlay.style.width = (arrayPageSize[0] + 'px');
  objErrorOverlay.style.height = (arrayPageSize[1] + 'px');
  objErrorOverlay.style.display = 'block';

  var errorTop  = arrayPageScroll[1] + ((arrayPageSize[3] - errorBlockHeight) / 2) - 100;
  var errorLeft = arrayPageScroll[0] + ((arrayPageSize[2] - errorBlockWidth) / 2);
  objErrorBlock.style.top = (errorTop + 'px');
  objErrorBlock.style.left = (errorLeft + 'px');
  objErrorBlock.style.width = (errorBlockWidth + 'px');
  objErrorBlock.style.height = (errorBlockHeight + 'px');
  objErrorBlock.style.display = 'block';

  var CloseButtonHtml = '<br><br><center><a href="#" onClick="hideErrorMessage();">Закрыть</a></center>';

  objErrorMessage.style.top = '20px';
  objErrorMessage.style.left = '10px';
  objErrorMessage.style.width = ((errorBlockWidth - 20) + 'px');
  objErrorMessage.style.height = ((errorBlockHeight - 60) + 'px');
  objErrorMessage.innerHTML = aErrorMessage + CloseButtonHtml;
  objErrorMessage.style.display = 'block';
}

function CreateErrorElements()
{
  var objBody = document.getElementsByTagName("body").item(0);

  var objErrorOverlay = document.getElementById('error_overlay');
  if (objErrorOverlay == null)
  {    var objErrorOverlay = document.createElement("div");
    objErrorOverlay.setAttribute('id', 'error_overlay');
    objErrorOverlay.onclick = function () { hideErrorMessage(); return false; };
    objErrorOverlay.style.backgroundColor = '#333';
    objErrorOverlay.style.opacity = '0.7';
    objErrorOverlay.style.filter = 'alpha(opacity=70)';
    objErrorOverlay.style.display = 'none';
    objErrorOverlay.style.position = 'absolute';
    objErrorOverlay.style.top = '0';
    objErrorOverlay.style.left = '0';
    objErrorOverlay.style.zIndex = '90';
    objErrorOverlay.style.width = '100%';
    objBody.insertBefore(objErrorOverlay, objBody.firstChild);
  }

// <div id="error_block">
//   <div id="error_message"></div>
// </div>

  var objErrorBlock = document.getElementById('error_block');
  if (objErrorBlock == null)
  {
    var objErrorBlock = document.createElement("div");
    objErrorBlock.setAttribute('id', 'error_block');
    objErrorBlock.style.backgroundColor = '#eeb36d';
    objErrorBlock.style.padding = '10px';
    objErrorBlock.style.border = '2px solid #df372c';
    objErrorBlock.style.zIndex = 100;
    objErrorBlock.style.display = 'none';
    objErrorBlock.style.position = 'absolute';
    objErrorBlock.style.top = '0';
    objErrorBlock.style.left = '0';
    objErrorBlock.style.zIndex = '100';
    objErrorBlock.align = 'center';
    objBody.insertBefore(objErrorBlock, objErrorOverlay.nextSibling);
  }

  var objErrorMessage = document.getElementById('error_message');
  if (objErrorMessage == null)
  {
    var objErrorMessage = document.createElement("div");
    objErrorMessage.setAttribute('id', 'error_message');
    objErrorMessage.style.display = 'none';
    objErrorMessage.style.position = 'absolute';
    objErrorMessage.style.lineHeight = '18px';
    objErrorMessage.style.top = '0';
    objErrorMessage.style.left = '0';
    objErrorMessage.style.zIndex = '110';
    objErrorBlock.appendChild(objErrorMessage);
  }

  isErrorElementsCreated = true;
}

function hideErrorMessage()
{  var objErrorOverlay = document.getElementById('error_overlay');
  var objErrorBlock = document.getElementById('error_block');

  objErrorOverlay.style.display = 'none';
  objErrorBlock.style.display = 'none';

  if (ModalWindowActive)
  {
    ModalWindowActive = false;
    if (MainForm) MainForm.submit();
    MainForm = null;
  }
}

function getPageScroll()
{

  var xScroll, yScroll;

  if (self.pageYOffset) {
    yScroll = self.pageYOffset;
    xScroll = self.pageXOffset;

  // IE 6
  }
  else if (document.documentElement && document.documentElement.scrollTop) {
    yScroll = document.documentElement.scrollTop;
    xScroll = document.documentElement.scrollLeft;
  }
  else if (document.body) {// all other IE
    yScroll = document.body.scrollTop;
    xScroll = document.body.scrollLeft;
  }

  arrayPageScroll = [xScroll, yScroll];
  return arrayPageScroll;
}

function getPageSize()
{

  var xScroll, yScroll;

  if (window.innerHeight && window.scrollMaxY) {
    xScroll = window.innerWidth + window.scrollMaxX;
    yScroll = window.innerHeight + window.scrollMaxY;
  // all but Explorer Mac
  }
  else if (document.body.scrollHeight > document.body.offsetHeight) {
    xScroll = document.body.scrollWidth;
    yScroll = document.body.scrollHeight;
  // Explorer Mac...would also work in IE 6 Strict, Mozilla and Safari
  }
  else {
    xScroll = document.body.offsetWidth;
    yScroll = document.body.offsetHeight;
  }

  var windowWidth, windowHeight;
  if (self.innerHeight) { // all except IE
    if (document.documentElement.clientHeight) {
      windowWidth = document.documentElement.clientWidth;
    }
    else {
      windowWidth = self.innerWidth;
    }
    windowHeight = self.innerHeight;
  }
  else if (document.documentElement && document.documentElement.clientHeight) { // IE 6 Strict Mode
    windowWidth = document.documentElement.clientWidth;
    windowHeight = document.documentElement.clientHeight;
  }
  else if (document.body) { // other IE
    windowWidth = document.body.clientWidth;
    windowHeight = document.body.clientHeight;
  }

  // for small pages with total height less then height of the viewport
  if (yScroll < windowHeight) {
    pageHeight = windowHeight;
  }
  else {
    pageHeight = yScroll;
  }

  // for small pages with total width less then width of the viewport
  if (xScroll < windowWidth) {
    pageWidth = xScroll;
  }
  else {
    pageWidth = windowWidth;
  }

  arrayPageSize = [pageWidth, pageHeight, windowWidth, windowHeight];
  return arrayPageSize;
}

function trim(string)
{
  return string.replace(/(^\s+)|(\s+$)/g, "");
}
