var Dom = function() {}

//-------------------------------------------------------------------------------
Dom.prototype.completionStatus = 0;
Dom.prototype.getCompletionStatus = function () {return this.completionStatus;};
Dom.prototype.setCompletionStatus = function (completionStatus) {this.completionStatus = completionStatus;};
//-------------------------------------------------------------------------------
Dom.prototype.debugging = false;
Dom.prototype.getDebugging = function () {return this.debugging;};
Dom.prototype.setDebugging = function (debugging) {this.debugging = debugging; return 1;};
//-------------------------------------------------------------------------------
Dom.prototype.domLog; 
Dom.prototype.getLog = function () {return this.domLog;};
Dom.prototype.setLog = function (domLog) {this.domLog = domLog; return 1;};

//-------------------------------------------------------------------------------
Dom.prototype.appentTextContent = function(element, content) {
 if(this.debugging && this.getLog()) {this.getLog().println("Dom.prototype.appentTextContent is running...");}; 
 if(typeof element == "string") {element = document.getElementById(element)};
 
 element.appendChild(document.createTextNode(content));
 return 1;
}

//-------------------------------------------------------------------------------
Dom.prototype.changeChilds = function(element, newChildsRootElement) {
 if(this.debugging && this.getLog()) {this.getLog().println("Dom.prototype.changeChilds is running...");}; 
 if(typeof element == "string") {element = document.getElementById(element)};
 if(typeof newChildsRootElement == "string") {newChildsRootElement = document.getElementById(newChildsRootElement)};
 
 this.removeChilds(element);
 
 element.appendChild(newChildsRootElement);
 return 1;
}

//-------------------------------------------------------------------------------
Dom.prototype.changeTextContent = function(element, content) {
 if(this.debugging && this.getLog()) {this.getLog().println("Dom.prototype.changeTextContent is running...");}; 
 if(typeof element == "string") {element = document.getElementById(element)};
 if(element) {
  this.removeChilds(element); 
  element.appendChild(document.createTextNode(content));
  return 1;
 } else {
  return 0;
 };
}

//-------------------------------------------------------------------------------
Dom.prototype.copyContent = function(fromElement, toElement, removeToStatus) {
 if(this.debugging && this.getLog()) {this.getLog().println("Dom.prototype.copyContent is running...");};
 if(typeof fromElement == "string") {fromElement = document.getElementById(fromElement)};
 if(typeof toElement == "string") {toElement = document.getElementById(toElement)};
 
 if(!fromElement) {return -1;} 
 if(!toElement) {return -2;} 
 
 if(removeToStatus) {this.removeChilds(element);}

 var copyElement = fromElement.firstChild; 
 while(copyElement) {
  toElement.appendChild(copyElement.cloneNode(true));
  copyElement = copyElement.nextSibling;
 };
 
 return 1;
}

//-------------------------------------------------------------------------------
Dom.prototype.createCookie = function(name, value, days) {
 if(this.debugging && this.getLog()) {this.getLog().println("Dom.prototype.createCookie is running...");};
 var expires;
 if (days) {
  var date = new Date();
  date.setDate(date.getDate()+days);
   expires = "; expires="+date.toGMTString();
 } else {
  expires = "";
 };
 document.cookie = name+"="+value+expires+"; path=/";
 return 1;
}

//-------------------------------------------------------------------------------
Dom.prototype.createLinkElement = function(id, href, text) {
 if(this.debugging && this.getLog()) {this.getLog().println("Dom.prototype.createLinkElement is running...");};
 var linkElement = document.createElement("a");
 linkElement.id = id;
 linkElement.setAttribute("href", href);
 linkElement.appendChild(document.createTextNode(text)); 
 return linkElement;
}

//-------------------------------------------------------------------------------
Dom.prototype.doHidden = function(element) {
 if(this.debugging && this.getLog()) {this.getLog().println("Dom.prototype.doHidden is running...");};
 if(typeof element == "string") {element = document.getElementById(element)};
 element.style.visibility = "hidden";
 element.style.position = "absolute";
 element.style.left = "-1000em";
 element.style.top = "-1000em";
 return 1;
}

//-------------------------------------------------------------------------------
Dom.prototype.doVisible = function(element) {
 if(this.debugging && this.getLog()) {this.getLog().println("Dom.prototype.doVisible is running...");};
 if(typeof element == "string") {element = document.getElementById(element)};
 if(!element) {return -1;} 
 if(!element.style) {return -2;} 
 element.style.position = "static";
 element.style.left = "auto";
 element.style.top = "auto";  
 element.style.visibility = "visible";
 return 1;
}

//-------------------------------------------------------------------------------
Dom.prototype.eraseCookie = function(name) {
 if(this.debugging && this.getLog()) {this.getLog().println("Dom.prototype.eraseCookie is running...");};
 this.createCookie(name,"",-1);
 return 1;
}

//-------------------------------------------------------------------------------
Dom.prototype.getTextContent = function(element) {
 if(this.debugging && this.getLog()) {this.getLog().println("Dom.prototype.getTextContent is running...");};
 if(typeof element == "string") {element = document.getElementById(element)};
 if(!element) {return -1;} 
 if(!element.firstChild) {return -2;} 
 var childNode = element.firstChild;
 if(!childNode) {return -3;}
 if(childNode.nodeType != 3) {return -4;}
 var text = childNode.nodeValue;
 return text;
}

//-------------------------------------------------------------------------------
Dom.prototype.invertVisibility = function(element) {
 if(this.debugging && this.getLog()) {this.getLog().println("Dom.prototype.invertVisibility is running...");};
 if(typeof element == "string") {element = document.getElementById(element)};
 if(element.style.visibility == "visible" || element.style.visibility == "") {doHidden(element)} else {doVisible(element)}
 return 1;
}

//-------------------------------------------------------------------------------
Dom.prototype.readCookie = function(name) {
 if(this.debugging && this.getLog()) {this.getLog().println("Dom.prototype.readCookie is running...");};
 var nameEQ = name + "=";
 var ca = document.cookie.split(";");
 for(var i=0; i < ca.length; i++) {
  var c = ca[i];
  while (c.charAt(0)==" ") c = c.substring(1,c.length);
  if (c.indexOf(nameEQ) == 0) {
   return c.substring(nameEQ.length,c.length);
  };
 }
 return null;
}

//-------------------------------------------------------------------------------
Dom.prototype.removeChilds = function(element) { 
 if(this.debugging && this.getLog()) {this.getLog().println("Dom.prototype.removeChilds is running...");};
 if(typeof element == "string") {element = document.getElementById(element)}; 
 if(element) {
  while(element.hasChildNodes() && element.firstChild) {element.removeChild(element.firstChild);};
  return 1;
 } else {
  return 0;
 };
}

//-------------------------------------------------------------------------------
Dom.prototype.setTextContent = function(element, content) {
 if(this.debugging && this.getLog()) {this.getLog().println("Dom.prototype.setTextContent is running...");};
 return changeTextContent (element, content);
};