function quickedit(art_id) {
  document.getElementById('name'+art_id).style.display = 'none';
  document.getElementById('nameinput'+art_id).style.display = 'inline';
  document.getElementById('price'+art_id).style.display = 'none';
  document.getElementById('priceinput'+art_id).style.display = 'inline';
  document.getElementById('amount'+art_id).style.display = 'none';
  document.getElementById('amountinput'+art_id).style.display = 'inline';
  document.getElementById('visible'+art_id).style.display = 'none';
  document.getElementById('visibleinput'+art_id).style.display = 'inline';
  var button = document.getElementById('quickedit'+art_id);
  button.setAttribute('onclick','quickedit_save("'+art_id+'")');
  button.innerHTML = 'Spara';
}

function quickedit_save(art_id) {
  var name = document.getElementById('nameinput'+art_id).value;
  var price = document.getElementById('priceinput'+art_id).value;
  var amount = document.getElementById('amountinput'+art_id).value;
  var visible = '0';
  if (document.getElementById('visibleinput'+art_id).checked == true)
    visible = '1';
  var xmlHttp = GetXmlHttpObject();
  if(xmlHttp == null)
  {
    alert("Your browser does not support AJAX!");
  }
  xmlHttp.onreadystatechange=function()
  {
    if(xmlHttp.readyState==4)
    {
      if (xmlHttp.responseText == 'true') {
        document.getElementById('nameinput'+art_id).style.display = 'none';
        document.getElementById('name'+art_id).innerHTML = name;
        document.getElementById('name'+art_id).style.display = 'inline';
        document.getElementById('priceinput'+art_id).style.display = 'none';
        document.getElementById('price'+art_id).innerHTML = price;
        document.getElementById('price'+art_id).style.display = 'inline';
        document.getElementById('amountinput'+art_id).style.display = 'none';
        document.getElementById('amount'+art_id).innerHTML = amount;
        document.getElementById('amount'+art_id).style.display = 'inline';
        document.getElementById('visibleinput'+art_id).style.display = 'none';
        if (visible == '1')
          document.getElementById('visible'+art_id).innerHTML = 'Ja';
        else
          document.getElementById('visible'+art_id).innerHTML = 'Nej';
        document.getElementById('visible'+art_id).style.display = 'inline';
        var button = document.getElementById('quickedit'+art_id);
        button.setAttribute('onclick','quickedit("'+art_id+'")');
        button.innerHTML = 'Snabb&auml;ndra';
      } else {
        console.log(xmlHttp.responseText);
      }
    }
  }
  var url = "ajax.php?action=quickedit&art="+art_id+"&name="+name+"&price="+price+"&amount="+amount+"&visible="+visible;
  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);
}

function updateSubcat() {
  var xmlHttp = GetXmlHttpObject();
  if(xmlHttp == null)
  {
    alert("Your browser does not support AJAX!");
  }
  xmlHttp.onreadystatechange=function()
  {
    if(xmlHttp.readyState==4)
    {
      var subcats = document.getElementById('subcats').innerHTML = xmlHttp.responseText;
      console.log('allt klart');
    }
  }
  var category = document.getElementById('categories').value;
  console.log('kategori: '+category);
  var url = "ajax.php?action=updateSubcategoriesList&cat_id=" + category;
  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);
}

function update_img(image) {
  var img = document.getElementById('article_big');
  var a = document.getElementById('article_big_link');
  img.src = image;
  img.alt = image;
  a.href = image;
}

//depricated
function showAddArticleForm(where) {
  var xmlHttp = GetXmlHttpObject();
  if(xmlHttp == null)
  {
    alert("Your browser does not support AJAX!");
  }
  xmlHttp.onreadystatechange=function()
  {
    if(xmlHttp.readyState==4)
    {
      var div = document.getElementById('addArticleFormDiv');
      div.innerHTML = xmlHttp.responseText;
    }
  }
  var url = "ajax.php?action=showAddArticleForm&where=" + where;
  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);
}

//depricated
function hideAddArticleForm(page) {
  var div = document.getElementById('addArticleFormDiv');
  div.innerHTML = "<div class='header_button'><div class='header_button_left'></div><a href='#' onclick='showAddArticleForm("+page+")' class='header_button_middle'>L&auml;gg till ny artikel</a><div class='header_button_right'></div></div>";
}

//depricated
function addManufacturer() {
  var xmlHttp = GetXmlHttpObject();
  if(xmlHttp == null)
  {
    alert("Your browser does not support AJAX!");
  }
  xmlHttp.onreadystatechange=function()
  {
    if(xmlHttp.readyState==4)
    {
      var select = document.getElementById('manufacturers');
      select.innerHTML = xmlHttp.responseText;
    }
  }
  var manufacturer = document.getElementById("newManufacturer");
  var url = "ajax.php?action=addManufacturer&name=" + manufacturer.value;
  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);
}

//depricated
function addCategory() {
  var xmlHttp = GetXmlHttpObject();
  if(xmlHttp == null)
  {
    alert("Your browser does not support AJAX!");
  }
  xmlHttp.onreadystatechange=function()
  {
    if(xmlHttp.readyState==4)
    {
      var select = document.getElementById('categories');
      select.innerHTML = xmlHttp.responseText;
    }
  }
  var category = document.getElementById("newCategory");
  var url = "ajax.php?action=addCategory&name=" + category.value;
  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);
}

function setThumb(image) {
  var xmlHttp = GetXmlHttpObject();
  if(xmlHttp == null)
  {
    alert("Your browser does not support AJAX!");
  }
  xmlHttp.onreadystatechange=function()
  {
    if(xmlHttp.readyState==4)
    {
      var span = document.getElementById('addedImages');
      span.innerHTML = xmlHttp.responseText;
    }
  }
  var url = "ajax.php?action=setThumb&image=" + image;
  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);
}

function deleteImage(image) {
  var xmlHttp = GetXmlHttpObject();
  if(xmlHttp == null)
  {
    alert("Your browser does not support AJAX!");
  }
  xmlHttp.onreadystatechange=function()
  {
    if(xmlHttp.readyState==4)
    {
      var span = document.getElementById('addedImages');
      span.innerHTML = xmlHttp.responseText;
    }
  }
  var url = "ajax.php?action=deleteImage&image=" + image;
  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);
}

function sendOrder() {
  var xmlHttp = GetXmlHttpObject();
  if(xmlHttp == null)
  {
    alert("Your browser does not support AJAX!");
  }
  xmlHttp.onreadystatechange=function()
  {
    if(xmlHttp.readyState==4)
    {
      self.location.href = 'min_sida.php';
    }
  }
  var msg = encodeURI(document.getElementById("ordermsg").value);
  var url = "sendOrder.php?msg=" + msg;
  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);
}

function GetXmlHttpObject() {
  var xmlHttp=null;
  try
  {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
  }
  catch (e)
  {
    // Internet Explorer
    try
    {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	  ie = true;
    }
    catch (e)
    {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	  ie = false;
    }
  }
  return xmlHttp;
}

