function getAJAXObject()
{
  // code for IE7+, Firefox, Chrome, Opera, Safari
  if (window.XMLHttpRequest)
  {
    return new XMLHttpRequest();
  }
  // code for IE6, IE5
  else if (window.ActiveXObject)
  {
    return new ActiveXObject("Microsoft.XMLHTTP");
  }
  else
  {
    return null;
  }
}

function closeAJAXForm()
{
  var ajaxDiv = document.getElementById('ajaxDiv');
  ajaxDiv.innerHTML = '';
  ajaxDiv.style.display = 'none';
}

function submitForm(formName)
{
  document.getElementById(formName).submit();
}

function trim(a)
{
  a = a.replace(/^\s+/, '');
  return a.replace(/\s+$/, '');
};

function mail_send()
{
  var mailName = document.getElementById('mail_name').value;
  var mailTitle = document.getElementById('mail_title').value;
  var mailEmail = document.getElementById('mail_email').value;
  var mailText = document.getElementById('mail_text').value;
  
  if (trim(mailName)=='' || trim(mailText)=='' || trim(mailEmail)=='' || trim(mailTitle)=='')
  {
    alert('Пожалуйста, заполните все поля!');
    return;
  }

  var xmlhttp = getAJAXObject();

  if (xmlhttp == null)
  {
    alert('Данный браузер не поддерживает технологию AJAX, необходимую для выполнения задачи. Воспользуйтесь, пожалуйста, более новым браузером.');
    return;
  }

  xmlhttp.onreadystatechange=function()
  {
    if(xmlhttp.readyState==4)
    {
      if (xmlhttp.responseText.indexOf('ok')!=-1)
      {
        alert('Ваше сообщение успешно отправлено!');
        return mail_clear();
      }
      else
      {
        alert('В данный момент отправка сообщения невозможна, т.к. произошла ошибка.'+
        ' Попробуйте позднее. При повторении ошибки обратитесь к техническому администратору.\n'+
        'Ответ сервера: '+xmlhttp.responseText);
        return;
      }
    }
  }
  
  var d = new Date();
  var params = "name="+encodeURIComponent(mailName)+"&email="+encodeURIComponent(mailEmail)+
           "&title="+encodeURIComponent(mailTitle)+"&text="+encodeURIComponent(mailText)+
           "&ajax="+d.getTime();
  
  xmlhttp.open("POST", mailURL, true);
 
  //Send the proper header information along with the request
  xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xmlhttp.setRequestHeader("Content-length", params.length);
  xmlhttp.setRequestHeader("Connection", "close");

  xmlhttp.send(params);
}

function mail_clear()
{  
  document.getElementById('mail_name').value = '';
  document.getElementById('mail_title').value = '';
  document.getElementById('mail_email').value = '';
  document.getElementById('mail_text').value = '';
  return;
}


function showhorizcenterdiv(Xwidth,divid) {
// First, determine how much the visitor has scrolled

var scrolledX, scrolledY;
if( self.pageYoffset ) {
scrolledX = self.pageXoffset;
scrolledY = self.pageYoffset;
} else if( document.documentElement && document.documentElement.scrollTop ) {
scrolledX = document.documentElement.scrollLeft;
scrolledY = document.documentElement.scrollTop;
} else if( document.body ) {
scrolledX = document.body.scrollLeft;
scrolledY = document.body.scrollTop;
}

// Next, determine the coordinates of the center of browser's window

var centerX, centerY;
if( self.innerHeight ) {
centerX = self.innerWidth;
centerY = self.innerHeight;
} else if( document.documentElement && document.documentElement.clientheight ) {
centerX = document.documentElement.clientWidth;
centerY = document.documentElement.clientheight;
} else if( document.body ) {
centerX = document.body.clientWidth;
centerY = document.body.clientheight;
}

// Xwidth is the width of the div, Yheight is the height of the
// div passed as arguments to the function:
var leftoffset = scrolledX + (centerX - Xwidth) / 2;
var topoffset = scrolledY + 10;
// the initial width and height of the div can be set in the
// style sheet with display:none; divid is passed as an argument to // the function
var o=document.getElementById(divid);
var r=o.style;
r.position='absolute';
r.top = topoffset + 'px';
r.left = leftoffset + 'px';
r.display = "block";
} 

function showdeadcenterdiv(Xwidth,Yheight,divid) {
// First, determine how much the visitor has scrolled

var scrolledX, scrolledY;
if( self.pageYoffset ) {
scrolledX = self.pageXoffset;
scrolledY = self.pageYoffset;
} else if( document.documentElement && document.documentElement.scrollTop ) {
scrolledX = document.documentElement.scrollLeft;
scrolledY = document.documentElement.scrollTop;
} else if( document.body ) {
scrolledX = document.body.scrollLeft;
scrolledY = document.body.scrollTop;
}

// Next, determine the coordinates of the center of browser's window

var centerX, centerY;
if( self.innerHeight ) {
centerX = self.innerWidth;
centerY = self.innerHeight;
} else if( document.documentElement && document.documentElement.clientheight ) {
centerX = document.documentElement.clientWidth;
centerY = document.documentElement.clientHeight;
} else if( document.body ) {
centerX = document.body.clientWidth;
centerY = document.body.clientHeight;
}

// Xwidth is the width of the div, Yheight is the height of the
// div passed as arguments to the function:
var leftoffset = scrolledX + (centerX - Xwidth) / 2;
var topoffset = scrolledY + (centerY - Yheight) / 2;
// the initial width and height of the div can be set in the
// style sheet with display:none; divid is passed as an argument to // the function
var o=document.getElementById(divid);
var r=o.style;
r.position='absolute';
r.top = topoffset + 'px';
r.left = leftoffset + 'px';
r.display = "block";
} 