﻿var pxDOM = new prDOM();

//Класс-утилита для работы с DOM
function prDOM() {}

prDOM.prototype.byId = function(Id) {
	return document.getElementById(Id);
}

prDOM.prototype.byTag = function(Owner, Tag) {
	return Owner.getElementsByTagName(Tag);
}

prDOM.prototype.newElement = function(Name, className, owner, text) {
	var e = document.createElement(Name);
	if (className) e.className = className;
	if (owner) this.addChild(owner, e);
	if (text) this.setText(e, text);	
	return e;
}

prDOM.prototype.setText = function(owner, text) {
	if (owner.tagName == "INPUT") {
		owner.value = text;
		return;
	}
	owner.innerHTML = text;
}

prDOM.prototype.newText = function(Text) {
	return document.createTextNode(Text);
}

prDOM.prototype.setClass = function(target, className) {
  target.className = className;
}

prDOM.prototype.newTable = function(className) {
	var Result = this.newElement("table");
	Result.className = className;
	return Result;
}

prDOM.prototype.newCaption = function(className) {
    var Result = this.newElement("caption");
	Result.className = className;
	return Result;
}

prDOM.prototype.newTableBody = function() {
    return this.newElement("tbody");
}

prDOM.prototype.newRow = function(className) {
    var Result = this.newElement("tr");
	Result.className = className;
	return Result;
}

prDOM.prototype.newCell = function(className) {
    var Result = this.newElement("td");
	Result.className = className;
	return Result;
}

prDOM.prototype.setRowValue = function(Row, ValueArray) {
  var Count = ValueArray.length;
  var cell = null;
  for (var i = 0; i < Count; i++) {
    if (Row.cells.length < Count) cell = this.newCell("");
    this.addChild(Row, cell);
    this.addChild(cell, ValueArray[i]);
  }
}

prDOM.prototype.attrValue = function(ownerAttr, Index) {
	return ownerAttr.attributes[Index].nodeValue;
}

prDOM.prototype.attrValueByName = function(ownerAttr, name) {
	return ownerAttr.getAttribute(name);
}

prDOM.prototype.addChild = function(Owner, Child) {
	if (!Owner) return;
	Owner.appendChild(Child);
}

prDOM.prototype.insertBefore = function(owner, Before, Child) {
	owner.insertBefore(Child, Before);
}

prDOM.prototype.removeChild = function(Owner, Child) {
	Owner.removeChild(Child);
}

prDOM.prototype.addElement = function(owner, name, className, value) {
  var Element = this.newElement(name);
  this.setClass(Element, className);
  this.addChild(Element, this.newText(value));
  this.addChild(owner, Element);  
  return Element;
}

prDOM.prototype.newInputText = function(owner, className, value) {
	var input = this.newElement("input", className, owner, value);
	input.type = "text";
	input.value = value;
	return input;
}

function getFormEditorValue(editor) {
	if (editor.type == "text") return editor.value;
	if (editor.type == "checkbox") return editor.checked;
	if (editor.tagName == "TEXTAREA") return editor.value;	
	if (editor.tagName == "SELECT") return optionValue(editor);
	if (editor.nodeType == "3") return editor.nodeValue;	
	return editor.innerHTML;
}

function optionValue(editor) {
/*	if (editor.selectedIndex && editor.selectedIndex >= 0 && editor.selectedIndex < editor.options.length)
		return editor.options[editor.selectedIndex].value;*/
	for (var i = 1; i < editor.options.length; i++) {
		if (editor.options[i].selected) return editor.options[i].value;
	}
	return '';
}

function findOptionValue(editor, text) {
	for (var i = 1; i < editor.options.length; i++) {
		if (editor.options[i].innerHTML == text) return editor.options[i].value;
	}
	return '';
}

function setOptionSelected(editor, text) {
	for (var i = 1; i < editor.options.length; i++) {
		if (editor.options[i].innerHTML == text) {
			editor.options[i].selected = true;
			return;
		}
	}
	return '';
}

function optionText(editor) {
	for (var i = 1; i < editor.options.length; i++) {
		if (editor.options[i].selected) return editor.options[i].innerHTML;
	}
	return '';
}
