//if (document.getElementsByTagName)
//  document.getElementsByTagName('link')[0].disabled = true;
function addElement(type) {
  return document.createElement(type);
}

function addElementName(type, name) {
  var element = addElement(type);
  element.setAttribute("id", name);
  element.setAttribute("name", name);
  return element;
}

function addElementClass(type, name, myclass) {
  var element = addElementName(type, name);
  element.setAttribute("class", myclass);
  return element;
}

function addHiddenField(name, value) {
  var element = document.createElement("input");
  element.setAttribute("id", name);
  element.setAttribute("name", name);
  element.setAttribute("type", "hidden");
  element.setAttribute("value", value);
  return element;
}

function addInputField(name, type) {
  var element = document.createElement("input");
  element.setAttribute("id", name);
  element.setAttribute("name", name);
  element.setAttribute("type", type);
  return element;
}

function appendCounter(node) {
  if (node.tagName == "INPUT" || node.tagName == "SELECT") {
    if (node.id) node.id += counter;
    if (node.name) node.name += counter;
  }
}

var boxAsk = 'dpost';
var boxDate = 'dcalendar';
var boxShadow = 'dshadow';
var boxTip = 'dtooltip';

function askAction(textid,text,id,value,action) {
  if(value) {
    setPost(id,value,action);
  } else {
    setInner(textid,text);
  }
}

function askDate(input,anchor) {
//  shadow();
  var box = addElementName('div',boxDate);
//  box.addEventListener('mouseOut',boxOff(boxDate),false);
//  box.onClick = boxOff(boxDate);
//  box.onMouseOut = hideEvent();
  document.body.appendChild(box);
  hideDiv(boxDate);
  var cal = new CalendarPopup(boxDate);
  cal.setWeekStartDay(1);
  cal.showYearNavigation();
  cal.showYearNavigationInput();
  cal.select(document.getElementById(input),anchor,'dd/MM/yyyy');
  showDiv(boxDate);
}

function askPost(link,title,yes,no) {
  shadow();
  var box = addElementName('div',boxAsk);
  document.body.appendChild(box);
  hideDiv(boxAsk);
  var table = addElement('table');
  var tr1 = addElement('tr');
  var tr2 = addElement('tr');
  var td1 = addElement('td');
  var td2 = addElement('td');
  var th = addElement('th');
  th.innerHTML = title;
  th.colSpan = 2;
  td1.innerHTML = "<input type=\"button\" onclick=\"javascript:"+link+"\" value="+yes+">";
  td2.innerHTML = "<input type=\"button\" onclick=\"javascript:boxOff('"+boxAsk+"')\" value="+no+">";
  tr1.appendChild(th);
  tr2.appendChild(td1);
  tr2.appendChild(td2);
  table.appendChild(tr1);
  table.appendChild(tr2);
  box.appendChild(table);
  showDiv(boxAsk);
}

function boxOff(box) {
  deleteById(box);
  deleteById(boxShadow);
}

function changeCss(nr) {
  if (document.getElementsByTagName) {
    x = document.getElementsByTagName('link');
  } else if (document.all) {
    x = document.all.tags('link');
  } else {
    return;
  }
  for (var i=0; i<x.length; i++) {
    dis = !(x[i].title == nr);
    x[i].disabled = dis;
  }
}

function clearValue(element) {
  element.value = '';
}

var counter = 0;
function copyNode(source,destination,display) {
  counter++;
  var node = document.getElementById(source).cloneNode(true);
  node.id = '';
  node.style.display = display;
  var childi = node.childNodes;
  for (var i=0;i<childi.length;i++) {
    var childj = childi[i].childNodes;
    if (childj) {
      for (var j=0;j<childj.length;j++) {
        appendCounter(childj[j]);
      }
    }
    appendCounter(childi[i]);
  }
  document.getElementById(destination).appendChild(node);
}

function deleteById(id) {
  var element = document.getElementById(id);
  if(element) {
    deleteByNode(element);
  }
}

function deleteByNode(e) {
  e.parentNode.removeChild(e);
}

function deleteFile(file) {
  document.getElementById('delete').value=file;
  submit();
}

function goto(location) {
  window.location = location;
}

function hideDiv(id) {
  document.getElementById(id).style.display = "none";
}

function hideEvent(e) {
  if (!e) var e = window.event;
  var tg = (window.event) ? e.srcElement : e.target;
  if (tg.nodeName != 'DIV') return;
  var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
  while (reltg != tg && reltg.nodeName != 'BODY')
  reltg= reltg.parentNode
  if (reltg== tg) return;
  e.target.style.display = "none";
}

function imgsrc(id,file) {
  document.getElementById(id).src = file;
}

function initForm() {
  var form = document.createElement("form");
  form.setAttribute("action", "index.php");
  form.setAttribute("id", "form");
  form.setAttribute("method", "post");
  form.setAttribute("name", "form");
  document.body.appendChild(form);
}

function post(id,action) {
  var form = document.createElement("form");
  form.setAttribute("action",action);
  form.setAttribute("enctype","multipart/form-data");
  form.setAttribute("encoding","multipart/form-data");
  form.setAttribute("id", "form");
  form.setAttribute("method","post");
  form.setAttribute("name", "form");
//  form.style.display = "none";
  var old = document.getElementById(id);
  var free = old.parentNode.replaceChild(form,old);
  form.appendChild(free);
  submit();
}

function postDel(id,name,value) {
  set("action","delete");
  set(name,value);
  post(id,'');
}

function postIt(e) {
  post(e.id,'');
}

function resetForm() {
  document.removeChild("form");
  initForm();
  submit();
}

function set(id,value) {
  document.getElementById(id).value = value;
}

function setInner(id,value) {
  document.getElementById(id).innerHTML = value;
}

function setPost(id,value,action) {
  set(id,value);
  post(id,action);
}

function shadow() {
  var shadow = addElementName('div', boxShadow);
  document.body.appendChild(shadow);
}

function showDiv(id) {
  document.getElementById(id).style.display = "block";
}

function showTip(element, id, myclass, title, tip) {
  if(document.getElementById(id)) {
    showDiv(id);
  } else {
    var box = addElementClass('div', id, myclass);
    var table = addElement('table');
    var tr1 = addElement('tr');
    var tr2 = addElement('tr');
    var td = addElement('td');
    var th = addElement('th');
    th.innerHTML = title;
    td.innerHTML = tip;
    tr1.appendChild(th);
    tr2.appendChild(td);
    table.appendChild(tr1);
    table.appendChild(tr2);
    box.appendChild(table);
    element.appendChild(box);
  }
}

function sortForm(value) {
  var form = document.getElementById("form");
  form.criterion.value = value;
  form.submit();
}

function stackPost(frompage,fromid,topage,toid) {
  set('frompage',frompage);
  set('fromid',fromid);
  set('id',toid);
  post('dstack',topage);
}

debug = 1
function submit() {
  if (debug) {
    var form = document.getElementById("form");
    if (form) form.submit();
    else alert ("missing form named 'form'");
  } else {
    document.getElementById("form").submit();
  }
}

function toggleVis(myclass) {
  for (var s=0; s<document.styleSheets.length; s++) {
    for (var c=0; c<document.styleSheets[s].cssRules.length; c++) {
      if (document.styleSheets[s].cssRules[c].selectorText == myclass) {
        if (document.styleSheets[s].cssRules[c].style.display != "none") {
          document.styleSheets[s].cssRules[c].style.display = "none";
        } else {
          document.styleSheets[s].cssRules[c].style.display = "";
        }
      }
    }
  }
}
