function getXmlHttp(){
  var xmlhttp;
  try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
    try {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
      xmlhttp = false;
    }
  }
  if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    xmlhttp = new XMLHttpRequest();
  }
  return xmlhttp;
}


function sel_tags(div, tags){

   var divv = document.getElementById(''+div+'');
   if(divv.style.display == 'none') {
   divv.style.display = '';
   var xmlhttp = getXmlHttp();
   var current_date = new Date();
   xmlhttp.open('GET', 'http://'+location.host+'/ajax/tags.php?tags='+tags, true);
   xmlhttp.onreadystatechange = function() {
   if (xmlhttp.readyState == 4)
   {
    if(xmlhttp.status == 200)
    {

     divv.innerHTML = xmlhttp.responseText;
    }
   }
 }
xmlhttp.send(null);
   }
   else
   {
    divv.style.display = 'none';
   }
}

function show_tags (id, title) {
  if(document.getElementById('tag'+id).checked == true){
  document.getElementById('tags').value = document.getElementById('tags').value+''+title+', ';
    
  }
  else
  {
  document.getElementById('tags').value = replace(''+title+', ', "", document.getElementById('tags').value);
  }
  document.getElementById('show_tags').innerHTML = document.getElementById('tags').value;
  
}

function replace(search, replace, subject){
       var ra = replace instanceof Array,
               sa = subject instanceof Array,
               l = (search = [].concat(search)).length,
               replace = [].concat(replace),
               i = (subject = [].concat(subject)).length;
       while(j = 0, i--)
               while(subject[i] = subject[i].split(search[j]).join(ra ? replace[j] || "" : replace[0]), ++j < l);
       return sa ? subject : subject[0];
}

function calendar(xm, xd, xy){



   var xmlhttp = getXmlHttp();
   var current_date = new Date();
   xmlhttp.open('GET', 'http://'+location.host+'/calendar.php?xm='+xm+'&xd='+xd+'&xy='+xy+'', true);
   xmlhttp.onreadystatechange = function() {
   if (xmlhttp.readyState == 4)
   {
    if(xmlhttp.status == 200)
    {

     document.getElementById('calendar').innerHTML = xmlhttp.responseText;
    }
   }
 }
xmlhttp.send(null);
}

function add_news_more () {

  var count = document.getElementById('countnews').value;
  document.getElementById('countnews').value = eval(count+'+1');
  var newDiv = document.createElement('div');
  var xmlhttp = getXmlHttp();
  var current_date = new Date();
  xmlhttp.open('GET', 'http://'+location.host+'/ajax/add_news_more.php?count='+document.getElementById('countnews').value+'&date='+current_date+'', true);
  xmlhttp.onreadystatechange = function() {
  if (xmlhttp.readyState == 4)
  {
   if(xmlhttp.status == 200)
   {

//     document.getElementById('calendar').innerHTML = xmlhttp.responseText;
   newDiv.innerHTML = xmlhttp.responseText;
   document.getElementById('add_news_more').appendChild(newDiv);
   }
  }
 }
xmlhttp.send(null);


}

function add_value () {
  var count = document.getElementById('countvalue').value;
  document.getElementById('countvalue').value = eval(count+'+1');
  var newDiv = document.createElement('div');
   newDiv.innerHTML = '<input type=text name="question_'+document.getElementById('countvalue').value+'" size=70 style="width=500px">';
   document.getElementById('add_value').appendChild(newDiv);
}

function base_sort () {
   if(document.getElementById('base_sort').style.display == 'none') {
   document.getElementById('base_sort').style.display = '';
   }
   else
   {
   document.getElementById('base_sort').style.display = 'none';
   }
}

function get_base_sort(id)
{
  var f = document.getElementById('base_search');
  var addstr = 'index.php?go=Base&in=base&id='+id;
   for(var i=0; i<f.elements.length; i++)
   { el=f.elements[i];

     if (el.selectedIndex!=null)
       val=el.options[el.selectedIndex].value;

     if (val)
       addstr += '&bs['+replace('search', '', f.elements[i].name)+']['+val+']=1';
     val='';
   }

   location.href= addstr;
}

function checkform(f) {
  var errMSG = ""; 
  // цикл ниже перебирает все элементы в объекте f, 
  // переданном в качестве параметра
  // функции, в данном случае - наша форма.            
  for (var i = 0; i<f.elements.length; i++) 
    // если текущий элемент имеет атрибут required
    // т.е. обязательный для заполнения
    if (null!=f.elements[i].getAttribute("required")) 
       // проверяем, заполнен ли он в форме
        if (isEmpty(f.elements[i].value)) // пустой
            errMSG += "  " + f.elements[i].name + "\n"; // формируем сообщение
                                                       // об ошибке, перечисляя 
                                                       // незаполненные поля
        // если сообщение об ошибке не пусто,
        // выводим его, и возвращаем false     
        if ("" != errMSG) {
            alert("Не заполнены обязательные поля:\n");
            return false;
        }
}

function isEmpty(str) {
   for (var i = 0; i < str.length; i++)
      if (" " != str.charAt(i))
          return false;
      return true;
}


function addbigpix (id) {

var oknoWidth=800; var oknoHeight=640;
if (self.innerWidth)
    oknoWidth = self.innerWidth;
else if (document.documentElement && document.documentElement.clientWidth)
    oknoWidth = document.documentElement.clientWidth;
else if (document.body)
    oknoWidth = document.body.clientWidth;

    if (self.innerHeight)
    oknoHeight = self.innerHeight;
else if (document.documentElement && document.documentElement.clientHeight)
    oknoHeight = document.documentElement.clientHeight;
else if (document.body)
    oknoHeight = document.body.clientHeight;

    newImg = new Image(); 
    newImg.src = id; 

var box_block = document.getElementById('box_block');
var scrollt = document.body.scrollTop;
var scrolll = document.body.scrollLeft;

     box_block.style.top = eval(scrollt+'+(('+oknoHeight+'/2)-30)');
     box_block.style.left = eval(scrolll+'+(('+oknoWidth+'/2)-30)');


   document.getElementById('box_page').style.zIndex = '10';
   document.getElementById('box_page').style.display = '';
   document.getElementById('box_block').style.zIndex = '20';
   document.getElementById('box_block').style.display = '';
    document.getElementById('box_page').style.top = scrollt;
    document.getElementById('box_page').style.left = scrolll;


newImg.onload=function(){

  
    box_block.style.top = eval(scrollt+'+(('+oknoHeight+'/2)-('+newImg.height+'/2))-30');
    box_block.style.left = eval(scrolll+'+(('+oknoWidth+'/2)-('+newImg.width+'/2)-30)');
    document.getElementById('addbigfoto').src = id;
  
}
if(newImg.complete)
{
    box_block.style.top = eval(scrollt+'+(('+oknoHeight+'/2)-('+newImg.height+'/2))-30');
    box_block.style.left = eval(scrolll+'+(('+oknoWidth+'/2)-('+newImg.width+'/2)-30)');
    document.getElementById('addbigfoto').src = id;
}
}

function hidebox () {
   document.getElementById('addbigfoto').src = 'images/1x1.gif';
   document.getElementById('box_block').style.zIndex = '-10';
   document.getElementById('box_block').style.display = 'none';
   document.getElementById('box_page').style.zIndex = '-10';
   document.getElementById('box_page').style.display = 'none';
}

function opr_wh()
{
var oknoWidth=800; var oknoHeight=640;
if (self.innerWidth)
    oknoWidth = self.innerWidth;
else if (document.documentElement && document.documentElement.clientWidth)
    oknoWidth = document.documentElement.clientWidth;
else if (document.body)
    oknoWidth = document.body.clientWidth;

    if (self.innerHeight)
    oknoHeight = self.innerHeight;
else if (document.documentElement && document.documentElement.clientHeight)
    oknoHeight = document.documentElement.clientHeight;
else if (document.body)
    oknoHeight = document.body.clientHeight;

var box_block = document.getElementById('box_block');
var scrollt = document.body.scrollTop;
var scrolll = document.body.scrollLeft;
if(box_block!=undefined)
{

     box_block.style.top = eval(scrollt+'+(('+oknoHeight+'/2)-('+document.getElementById('addbigfoto').height+'/2)-30)');
     box_block.style.left = eval(scrolll+'+(('+oknoWidth+'/2)-('+document.getElementById('addbigfoto').width+'/2)-30)');
     document.getElementById('box_page').style.top = scrollt;
    document.getElementById('box_page').style.left = scrolll;
}
}


function edit_email(id) {

  var email = document.getElementById('email_'+id).value;  
  if(document.getElementById('status_edit_'+id).value == '0')
  {
    document.getElementById('status_edit_'+id).value = '1';
    document.getElementById('edit_'+id+'').innerHTML = '<input id="new_email_'+id+'" type="text" value="'+email+'" style="width:70%"><input onclick="savee_email('+id+')" type="button" value="Сохранить">';
  }
  else
  {
    document.getElementById('status_edit_'+id).value = '0';
    document.getElementById('edit_'+id+'').innerHTML = email;
  }  

}


function savee_email (id) {
  
   var new_email = document.getElementById('new_email_'+id).value;  
//  alert(id+' - '+new_email);

   var xmlhttp = getXmlHttp();
   var current_date = new Date();
//   alert('http://'+location.host+'/add_email.php?id='+id+'&email='+new_email+'&date='+current_date);

   xmlhttp.open('GET', 'http://'+location.host+'/ajax/add_email.php?id='+id+'&email='+new_email, true);
   xmlhttp.onreadystatechange = function()
   {
    if (xmlhttp.readyState == 4)
    {
     if(xmlhttp.status == 200)
     {
//      alert(xmlhttp.responseText);
      if(xmlhttp.responseText == 'Ok')
      {
       document.getElementById('edit_'+id+'').innerHTML = new_email;
       document.getElementById('status_edit_'+id).value = '0';
       document.getElementById('email_'+id).value = new_email;
      }
     }
    }
   }
   xmlhttp.send(null);
}
function div_hide(id) {
  
  if(document.getElementById(''+id+'').style.display == 'none')
  {
    document.getElementById(''+id+'').style.display = '';
  }
  else
  {
    document.getElementById(''+id+'').style.display = 'none';
  }
}

function news_textt (id) {
  

  
   var xmlhttp = getXmlHttp();
   var current_date = new Date();

   xmlhttp.open('GET', 'http://'+location.host+'/ajax/news_text.php?id='+id+'&date='+current_date, true);
   xmlhttp.onreadystatechange = function()
   {
    if (xmlhttp.readyState == 4)
    {
     if(xmlhttp.status == 200)
     {
//      alert(xmlhttp.responseText);
       document.getElementById('news_text').value = replace('src="/images', 'src="http://'+location.host+'/images', xmlhttp.responseText);

     }
    }
   }
   xmlhttp.send(null);

}

function replace(search, replace, subject){
       var ra = replace instanceof Array,
               sa = subject instanceof Array,
               l = (search = [].concat(search)).length,
               replace = [].concat(replace),
               i = (subject = [].concat(subject)).length;
       while(j = 0, i--)
               while(subject[i] = subject[i].split(search[j]).join(ra ? replace[j] || "" : replace[0]), ++j < l);
       return sa ? subject : subject[0];
}


function add_subscribe (id, title) {
  

   var xmlhttp = getXmlHttp();
   var current_date = new Date();
   var ansver;
   var metod;

   if(document.getElementById('subscribe'+id).checked==true)
   {
    ansver = "Ви погодились на розсилку";
    metod = 1;
   }
   else
   {
    ansver = "Ви відмовилися від розсилки";
    metod = 0;
   }

   xmlhttp.open('GET', 'http://'+location.host+'/ajax/add_subscribe.php?id='+id+'&metod='+metod+'&date='+current_date, true);
   xmlhttp.onreadystatechange = function()
   {
    if (xmlhttp.readyState == 4)
    {
     if(xmlhttp.status == 200)
     {
//      alert(ansver+" \""+title+"\"\n"+xmlhttp.responseText);
      alert(ansver+" \""+title+"\"");

     }
    }
   }
   xmlhttp.send(null);

}

function ch_link_name(val, val_old) {
  

   if(val!=val_old)
   {
   var xmlhttp = getXmlHttp();
   var current_date = new Date();

   xmlhttp.open('GET', 'http://'+location.host+'/ajax/ch_link_name.php?link_name='+val+'&date='+current_date, true);
   xmlhttp.onreadystatechange = function()
   {
    if (xmlhttp.readyState == 4)
    {
     if(xmlhttp.status == 200)
     {

     if(xmlhttp.responseText!='0')
     {
      alert('Название "'+val+'" уже используется для другой страницы');
     }

     }
    }
   }
   xmlhttp.send(null);
   }
}


function add_subscribe_a (idd, u, id, title) {
  

   var xmlhttp = getXmlHttp();
   var current_date = new Date();
   var ansver;
   var metod;

   if(document.getElementById('subscribe'+id+'_'+idd).checked==true)
   {
    ansver = "Ви погодились на розсилку";
    metod = 1;
   }
   else
   {
    ansver = "Ви відмовилися від розсилки";
    metod = 0;
   }

   xmlhttp.open('GET', 'http://'+location.host+'/ajax/add_subscribe_admin.php?id='+id+'&member_mail='+u+'&metod='+metod+'&date='+current_date, true);
   xmlhttp.onreadystatechange = function()
   {
    if (xmlhttp.readyState == 4)
    {
     if(xmlhttp.status == 200)
     {
//      alert(ansver+" \""+title+"\"\n"+xmlhttp.responseText);
      alert(ansver+" \""+title+"\"");

     }
    }
   }
   xmlhttp.send(null);

}