
function inittree() {
  var xml=document.getElementsByName("XML")[0].value;
  var root=Xparse(xml);
  return root;
}

function parsetreeout(root) {
  var type=root.type;
  
}

XMLRPC.onerror=function(e) {
  alert(e);
}

XMLRPC.DEBUG=true;
rpcServer=XMLRPC.getService('http://www.tda.at/xmlrpc.php');
rpcServer.add("dao.create","createdao");
rpcServer.add("dao.clone","clonedao");
rpcServer.add("dao.delete","deletedao");
rpcServer.add("dao.format","formatdao");
rpcServer.add("dao.update","updatedao");


function dao_create (dao_type, dao_id, element_container) {
  var data = {
    type:      dao_type,
    id:        dao_id,
    container: element_container,
    projectid: projectid,
    sessionid: sessionid
  }
  result=rpcServer.createdao(data);
  obj=parseObj(Xparse(getParam(result)));
  return(obj);
}

function dao_clone (dao_type, dao_id, childtype, childprop) {
  var data = {
    type:      dao_type,
    id:        dao_id,
    childtype: childtype,
    childprop: childprop,
    projectid: projectid,
    sessionid: sessionid
  }
  result=rpcServer.clonedao(data);
  var id=getParam(result);
  return(id);
}
  
function dao_format (dao_type, dao_id, element_container) {
  var data = {
    type:      dao_type,
    id:        dao_id,
    container: element_container,
    projectid: projectid,
    sessionid: sessionid
  }
  result=rpcServer.formatdao(data);
  param=getParam(result);
//  alert(param.length);
//  alert (result.length);
  obj=parseObj(Xparse(param));
  return(obj);
}

function dao_delete (dao_type, dao_id) {
  var data = {
    type:      dao_type,
    id:        dao_id,
    projectid: projectid,
    sessionid: sessionid
  }
  result=rpcServer.deletedao(data);
}

function dao_update (dao_type, dao_id, props) {
  var data = {
    type:      dao_type,
    id:        dao_id,
    properties:props,
    projectid: projectid,
    sessionid: sessionid
  }
  result=rpcServer.updatedao(data);
}
                                                                                                                                                            
function parseObj(obj) {
  var me=null;
  if (obj.type=="element") {
    if (obj.name!="ROOT") {
      me=document.createElement(obj.name);
      var i;
      for (var att in obj.attributes) {
        me.setAttribute(att, obj.attributes[att]);
      }
      for (i=0; i<obj.contents.length; i++) {
        me.appendChild(parseObj(obj.contents[i]));
      }
    }
    else {
      me=document.createElement("div");
      for (i=0; i<obj.contents.length; i++) {
        me.appendChild(parseObj(obj.contents[i]));
      }
    }
  }
  else if (obj.type="chardata") {
    me=document.createTextNode(obj.value);
  }
  return me;
}

function getParam(text) {
  var i = text.indexOf("<string>");
  var res = text.substr (i+8,text.length);
  var j = res.indexOf("</string>");
  res=res.substr(0,j);
  return _entity(res);
}


function findinput(obj,name) {
  if (obj==null) { return null; }
  if (obj.nodeName==null) { return null;  }
  if ((obj.nodeName=="input") || (obj.nodeName=="INPUT")) {
    if (obj.name.indexOf(name)==0) {
      return obj;
    }
  }
  if (obj.firstChild!=null) {
    var ret=findinput(obj.firstChild,name);
    if (ret!=null) return ret;
  }
  if (obj.nextSibling!=null) {
    var ret=findinput(obj.nextSibling,name);
    if (ret!=null) return ret;
  }
  return null;
}

function identifyInputs() {
  var i=document.getElementsByTagName("input");
  for (var j=0; j!=i.length; j++) {
    i[j].id=i[j].name;
  }
  var i=document.getElementsByTagName("select");
  for (var j=0; j!=i.length; j++) {
    i[j].id=i[j].name;
  }
}


function createTextInput (node, name, value, style_class, size, maxlength) {
  var input=document.createElement("input");
  input.type=type;
  input.name=name;
//  input.className=style_class;
  input.value=value;
  input.size=size;
  input.maxlenght=maxlength;
  node.appendChild(input);
//  tree.add(input);
}

function scaleTop(div,factor) {
  var x = window.innerHeight;
  if (!x) x=document.body.offsetHeight;
  if (x<700) x=700;
  x= x * factor/100;
  div.style.top=x;
}

function scaleHeight(div,factor) {
  var x = window.innerHeight;
  if (!x) x=document.body.offsetHeight;
  if (x<700) x=700;
  x= x * factor/100;
  div.style.height=x;
}

function scaleLeft(div,factor) {
  var w=window.innerWidth;
  if (!w) w=document.body.offsetWidth;
  if (w<760) w=760;
  var x = w*factor/100;
  div.style.left=x;
}

function scaleWidth(div,factor) {
  var w=window.innerWidth;
  if (!w) w=document.body.offsetWidth;
  if (w<760) w=760;
  var x = w*factor/100;
  div.style.width=x;
}
