function email(name, domain) {
	document.write("<a href = 'mailto:" + name + "@" + domain + "'>" + name + "@" + domain + " </a>");
}
function getElementPosition(elemId){
  var elem = document.getElementById(elemId);
  var t = 0;
	while (elem)  {
        t += elem.offsetTop;
        elem = elem.offsetParent;
    }
    return t;
}
function showMessage(elemId) {
  document.getElementById("messageBox").style.top = (getElementPosition(elemId) - 5) + 'px';
  document.getElementById("messageBox").style.display = 'block';
}
function closeMessage() {
  document.getElementById("messageBox").style.display = 'none';
}
function openWin(images, w, h, title) {
  x = Number(w) + 20;
  y = Number(h) + 30;
  myWin= open("", "ScreenShot", "width="+x+",height="+y+",status=no,toolbar=no,menubar=no,directories=no,location=no,scrollbars=yes,resizable=yes");
  myWin.document.open();
  myWin.document.write('<html><head><title>' + title + '<\/title>');
  myWin.document.write('<\/head>');
  myWin.document.write('<body>');
  myWin.document.write('<div align="center"><a href="javascript:window.close()"><img src='+images+'  border="0" title="Закрыть окно"><\/a><\/div>');
  myWin.document.write('<\/body><\/html>');
  myWin.document.close();  
}
function addGood(id, count, text, a) {
  if (count > 0) {
    var req = new JsHttpRequest();
    req.onreadystatechange = function() {
      if (req.readyState == 4) {
        if (req.responseJS.reply == 'OK') {
          document.getElementById("orderframe").innerHTML = req.responseJS.qty? 'Вы <a href="/catalog/order/">заказали</a> <span id="s_qty">' + req.responseJS.count + '</span><br>на сумму <span id="s_sum">' + req.responseJS.sum + '</span> руб.' : '<div style="padding: 5px 0px 0px 3px;">Вы не выбрали ни одного товара</div>';
          if (a)
            alert('Товар ' + text + ' успешно добавлен в корзину');
          else
            showMessage('gi' + id);
        }
      }
    }
    req.open(null, '/catalog/backend.php', true);
    req.send( {id: id, 
               count: count 
              } );
  }
  else {
    alert('Количество товаров должно быть числом');
  }
}

function changeQuantity(id, action) {
  var req = new JsHttpRequest();
  req.onreadystatechange = function() {
    if (req.readyState == 4) {
      if (req.responseJS.reply == 'OK') {
        if (parseInt(req.responseJS.qty)) {
          document.getElementById("orderframe").innerHTML = 'Вы <a href="/catalog/order/">заказали</a> <span id="s_qty">' + req.responseJS.count + '</span><br>на сумму <span id="s_sum">' + req.responseJS.sum + '</span> руб.';
          document.getElementById("submit_order").disabled = false;
        }
        else {
          document.getElementById("orderframe").innerHTML = '<div style="padding: 5px 0px 0px 35px;">Ваша корзина пуста</div>';
          document.getElementById("submit_order").disabled = true;
        }
        document.getElementById("c" + id).innerHTML = req.responseJS.qty;
        document.getElementById("f" + id).innerHTML = req.responseJS.finaly;
        document.getElementById("mitogo").innerHTML = req.responseJS.sum;
      }
    }
  }
  req.open(null, '/catalog/backend.php', true);
  req.send( {id: id, 
            count: (action == 'minus'? -1 : 1)} );
}

function delRowInOrder(id, tag) {
  if (window.confirm('Удалить товар из корзины?')) {
    var req = new JsHttpRequest();
    req.onreadystatechange = function() {
      if (req.readyState == 4) {
        if (req.responseJS.reply == 'OK') {
          if (parseInt(req.responseJS.qty) > 0) {
            tag.parentNode.parentNode.style.display = 'none';
            document.getElementById("c" + id).innerHTML = req.responseJS.qty;
            document.getElementById("f" + id).innerHTML = req.responseJS.finaly;
            document.getElementById("orderframe").innerHTML = req.responseJS.qty? 'Вы <a href="/catalog/order/">заказали</a> <span id="s_qty">' + req.responseJS.count + '</span><br>на сумму <span id="s_sum">' + req.responseJS.sum + '</span> руб.' : '<div style="padding: 5px 0px 0px 35px;">Ваша корзина пуста</div>';
              document.getElementById("mitogo").innerHTML = req.responseJS.sum;
          }
          else {
            document.location.href = "/catalog/order/";
          }
        }
      }
    }
    req.open(null, '/catalog/backend.php', true);
    req.send( {id: id, del: 1 });
  }
}
