// ************************************************************************************************************************************************************************
// © 2006 http://www.bot.su/ Public Domain (20061217)
// ************************************************************************************************************************************************************************


var Dom = function() {};
Dom.prototype = new Root();
Dom.prototype.created = "20060101"; 
Dom.prototype.version = "20061018"; 

Dom.prototype.appendTextContent = function(element, content) {
 if(this.mustTrace()) {this.getLog().println("Dom.appendTextContent 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.mustTrace()) {this.getLog().println("Dom.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.mustTrace()) {this.getLog().println("Dom..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.clearTextContent = function(element) {
 if(this.mustTrace()) {this.getLog().println("Dom.changeTextContent is running...");}; 
 if(typeof element == "string") {element = document.getElementById(element)};
 if(element) {
  this.removeChilds(element); 
  return 1;
 } else {
  return 0;
 };
}

Dom.prototype.copyContent = function(fromElement, toElement, removeToStatus) {
 if(this.mustTrace()) {this.getLog().println("Dom.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.mustTrace()) {this.getLog().println("Dom.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.mustTrace()) {this.getLog().println("Dom.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.mustTrace()) {this.getLog().println("Dom.doHidden is running...");};
 if(typeof element == "string") {element = document.getElementById(element)};
 if(!element) {return -1;};
 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.mustTrace()) {this.getLog().println("Dom.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.mustTrace()) {this.getLog().println("Dom.eraseCookie is running...");};
 this.createCookie(name,"",-1);
 return 1;
}

Dom.prototype.getTextContent = function(element) {
 if(this.mustTrace()) {this.getLog().println("Dom.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.mustTrace()) {this.getLog().println("Dom.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.mustTrace()) {this.getLog().println("Dom.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.mustTrace()) {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.mustTrace()) {this.getLog().println("Dom.prototype.setTextContent is running...");};
 return this.changeTextContent(element, content);
};

