function Lmnt(t) {
	//Variables
	this.lmnt = undefined; 
	this.t    = t;         //The element type
	
	//Functions
	this.place = Lmnt_place;
	this.set   = Lmnt_set;
	this.setLmnt = Lmnt_setLmnt;
	this.remove = Lmnt_remove;
	this.add = Lmnt_add;
	this.getChild = Lmnt_getChild;
	this.setAttribute = Lmnt_setAttribute;
    this.clear = Lmnt_clear;
    
	
	//Init
	validTypes = new Array("table","thead","tbody","tr","th","td","div","span","b","input","select","textarea", "strong","u","em","a","br","hr","form","img");
	found = false;
	for (var i = 0; i < validTypes.length; i++) {
		if (validTypes[i] == this.t) {
			found = true;
			break;
		}
	}
	if (found) {
		this.lmnt = document.createElement(this.t);
	} else {
		this.lmnt = document.createTextNode(this.t);
	}
}
function Lmnt_draw(top,left)
{
    this.lmnt.style = "position:absolute;top:"+top+";left:"+left+";";
}
function Lmnt_set(k,v) {
	this.lmnt.setAttribute(k,v);
}
function Lmnt_place(where) {
	if (where == undefined) 
		return false;
	if (where.lmnt != undefined)
		where.lmnt.appendChild(this.lmnt);
	else
		where.appendChild(this.lmnt);
	
}
function Lmnt_add(lmnt) {
	
	if (lmnt.lmnt != undefined)
		this.lmnt.appendChild(lmnt.lmnt);
	else
		this.lmnt.appendChild(lmnt);
}
function Lmnt_remove(from) {
	from.removeChild(this.lmnt);
}
function Lmnt_getChild(i) {
	if (this.lmnt.childNodes[i] != undefined) {
		return this.lmnt.childNodes[i];
	}
}
function Lmnt_setLmnt(t,lmnt) {
	this.t = t;
	this.lmnt = lmnt;
}
function Lmnt_setAttribute(k,v) {
	this.lmnt.setAttribute(k,v);
}
function Lmnt_clear() {
    clear(this.lmnt);
}

function LmntTable(rows,cols,initialHeader) {
	this.inHeritFrom = Lmnt;
	this.inHeritFrom("table");
	this.rows = rows;
	this.cols = cols;
	
	//Functions
	this.cell = LmntTable_getCell;
	this.head = LmntTable_getHead;
	this.row = LmntTable_getRow;
	this.addRow = LmntTable_addRow;
	this.getRow = LmntTable_getRow;
	this.addCol = LmntTable_addCol;
	
	//Init
	this.thead = new Lmnt("thead");
	this.tbody = new Lmnt("tbody");
	if (isNaN(rows) || isNaN(cols)) {
		this.rows = 0;
		this.cols = 0;
		this.tbody.place(this.lmnt);
		return false;	
	}
	offset = 0; // If first is to be a head, this will be changed
	if (typeof(initialHeader) == "boolean") {
		if (initialHeader == true) {
			offset = 1;
			tr = new Lmnt("tr");
			for (var j = 0; j <= cols; j++) {
				cell = new Lmnt("th");
				cell.place(tr);
			}
			tr.place(this.thead);
			this.thead.place(this.lmnt);
		}
	}
	for (var i = offset; i <= rows; i++) {
		tr = new Lmnt("tr");
		for (var j = 0; j <= cols; j++) {
			cell = new Lmnt("td");
			cell.place(tr);
		}
		tr.place(this.tbody);
	}
	this.tbody.place(this.lmnt);
	
	//this.lmnt.appendChild(tbody.lmnt);
}
function LmntTable_getCell(row,col) {
	i = 0;
	while (this.lmnt.childNodes[i].nodeName == "THEAD") {
		i++;
	}
	tbody = this.lmnt.childNodes[i];
	if (tbody.childNodes[row] != undefined) {
		if (tbody.childNodes[row].childNodes[col] != undefined) {
			td = new Lmnt("td");
			td.lmnt = tbody.childNodes[row].childNodes[col];
			return td;
		}
	}
}
function LmntTable_getHead(row,col) {
	if (this.lmnt.childNodes[0].nodeName == "THEAD") {
		thead = this.lmnt.childNodes[0];
		if (thead.childNodes[row] != undefined) {
			if (thead.childNodes[row].childNodes[col] != undefined) {
				td = new Lmnt("th");
				td.lmnt = thead.childNodes[row].childNodes[col];
				return td;
			}
		}
	}
}
function LmntTable_addRow(pos,row) {
	
	if (typeof(row) == "undefined") {
		tr = new Lmnt("tr");
		
		for (var j = 0; j <= this.cols; j++) {
			celltype = "td";

			c = new Lmnt(celltype);
			c.place(tr);
		}
		if (typeof(pos) == "undefined") {
			this.lmnt.childNodes[0].appendChild(tr.lmnt);
			this.rows++;
		} else {
			if (this.lmnt.childNodes[0].childNodes[pos] != undefined) {
				obj = this.lmnt.childNodes[0].childNodes[pos];
				//alert(tr.lmnt.childNodes[0].nodeName);
				this.lmnt.childNodes[0].insertBefore(tr.lmnt,obj);
				this.rows++;
			}
		}
	} else {
		if (typeof(pos) == "undefined") {
			//this.lmnt.childNodes[0].insertBefore(row.lmnt,obj);
			this.lmnt.childNodes[0].appendChild(row.lmnt);
			this.rows++;
		} else {
			obj = this.lmnt.childNodes[0].childNodes[pos];
			this.lmnt.childNodes[0].insertBefore(row.lmnt,obj);
			this.rows++;
		}
	}
}
function LmntTable_addCol(pos) {
	if (typeof(pos) == "undefined") {
		for (var i = 0; i < this.lmnt.childNodes[0].childNodes.length; i++) {
			this.lmnt.childNodes[0].childNodes[i].appendChild(document.createElement("td"));
		}
		this.cols++;
	} else {
		
	}
}
function LmntTable_getRow(pos) {
	if (this.lmnt.childNodes[0].childNodes[pos] != undefined) {
		row = new Lmnt("tr");
		row.setLmnt("tr",this.lmnt.childNodes[0].childNodes[pos]);
		return row;
	}
	return false;
}
function LmntTable_countRowCells(row) {

}
function LmntInput(t,n,v,attribs) {
	this.inHeritFrom = Lmnt;
	this.inHeritFrom("input");

	this.set("type",t);
	this.set("name",n);
	this.set("value",v);
	if (typeof(attribs) == "object" && attribs[0] != undefined) {
		for (var i = 0; i < attribs.length; i=i+2) {
			if (attribs[i] == "checked")
				this.lmnt.checked = true;
			else if (attribs[i] == "selected")
				this.lmnt.selected = true;
			else
				this.set(attribs[i],attribs[i+1]);
		}
	}
}

function LmntSelect(n,values,attribs) {
	this.inHeritFrom = Lmnt;
	this.inHeritFrom("select");
	this.addOption = LmntSelect_addOption;
	this.getValue = LmntSelect_getValue;
    this.select = LmntSelect_select;
	if (typeof(values,true) == "array") {
		for (var i = 0; i < values.length; i=i+2) {
			this.addOption(values[i],values[i+2]);
		}
	}
	
	if (typeof(attribs,true) == "array") {
		for (var i = 0; i < attribs.length; i=i+2) {
			this.set(attribs[i],attribs[i+1]);
		}
	}
}
function LmntSelect_addOption(k,v,attribs) {
	opt = document.createElement("option");
	txt = document.createTextNode(k);
    if (typeof(attribs,true) == "array") {
        for (var i = 0; i < attribs.length; i=i+2) {
            
            opt.setAttribute(attribs[i],attribs[i+1]);
        }
    }
	opt.setAttribute("value",v);
	opt.appendChild(txt);
	this.lmnt.appendChild(opt);
}
function LmntSelect_getValue()
{
    return this.lmnt.value;
}
function LmntSelect_select()
{
    this.lmnt.selected = true;
}
function LmntLink(l,n,attribs) {
	this.inHeritFrom = Lmnt;
	this.inHeritFrom("a");
	
	this.lmnt.setAttribute("href",l);
	this.lmnt.appendChild(document.createTextNode(n));
	if (typeof(attribs,true) == "array") {
		for (var i = 0; i < attribs.length; i=i+2) {
			this.set(attribs[i],attribs[i+1]);
			//this.lmnt.(attribs[i].toLowerCase()) = attribs[i+1];
		}
	}
}
/*
s = src
a = alt
t = title
*/
function LmntImage(s,a,t,attribs) {
	this.inHeritFrom = Lmnt;
	this.inHeritFrom("img");
	
	this.lmnt.src = s;
	this.lmnt.setAttribute("alt",a);
	this.lmnt.setAttribute("title",t);
	if (typeof(attribs,true) == "array") {
		for (var i = 0; i < attribs.length; i=i+2) {
			this.set(attribs[i],attribs[i+1]);
			//this.lmnt.(attribs[i].toLowerCase()) = attribs[i+1];
		}
	}
}

function Container(n) {
	this.obj = document.getElementById(n);
	
	this.out = Container_out;
	this.outln = Container_outln;
	this.nl = Container_newLine;
	this.add = Container_add;
}
function Container_out(n) { this.obj.appendChild(document.createTextNode(n)); }
function Container_outln(n) { this.obj.appendChild(document.createTextNode(n));this.nl(); }
function Container_newLine() {this.obj.appendChild(document.createElement("br"));}
function Container_add(lmnt) {this.obj.appendChild(lmnt.lmnt);}
function CloneLmnt(obj) {

	for (i in obj) {
		this[i] = obj[i];
	}
	this.lmnt = obj.lmnt.cloneNode(true);
}

function LoadLmnt(id) {
    lmnt = new Lmnt();
    lmnt.lmnt = document.getElementById(id);
    return lmnt;
}