//SimpleTable - builds a simple table (w/options)
function SimpleTable(cols, rows) {
	this.width = "100%";
	this.height;
	this.padding = 0;
	this.spacing = 0;
	this.border = 0;
	this.className;
	this.id;
	
	var thisRef = this;
	var cCount = parseInt(cols);
	var rCount = parseInt(rows);
		
	//Cell :)
	var _Cell = function() {
		this.value;
				
		this.attributes = new Array();
		var attr = function(n, v) {this.name = n;this.value = v;}
		//Finds attribute
		this.indexOf = function(n) {
			var attLen = this.attributes.length - 1;
			var idx = 0;
			var cAtt;
			if(attLen >= 0) {
				do {
					cAtt = this.attributes[attLen];
					if(cAtt.name == n) return attLen;
				} while(attLen--);
			}
			return -1;
		}
		
		//Sets Attribute (overwrites existing)
		this.setAttribute = function(n, v) {
			var idxOf = this.indexOf(n);
			if(idxOf != -1) {
				this.attributes[idxOf].value = v;
			} else {
				this.attributes.push(new attr(n,v));
			}
		}
	}
	//Get Cell :)
	this.Cell = function(c, r) {
		if((c >= 0 && c <= cCount) && (r >= 0 && r <= rCount)) {
			return thisRef.Rows[r][c];
		}
		return null;
	}	
		
	//set all attributes for a column
	function buildCell(col, cell) {
		if(col.attributes.length > 0) {
			var aLen = col.attributes.length - 1;
			var cAtt;
			do {
				cAtt = col.attributes[aLen];
				cell.setAttribute(cAtt.name, cAtt.value);
			} while(aLen--);
		}
	}
	
	//Create Grid
	if(cCount > 0 && rCount > 0) {
		this.Rows = new Array(rCount);
		var tRow, tCols, tRows;
		tRows = rCount-1;
		do {
			tCols = cCount-1;
			tRow = new Array();
			thisRef.Rows[tRows] = tRow;
			do {	
				tRow[tCols] = new _Cell();					
			} while(tCols-- != 0);		
		}while(tRows-- != 0);
	}
	//Output a table
	this.toHTML = function() {
		var nTable = document.createElement("table");
		if(thisRef.width != null) { nTable.setAttribute("width", thisRef.width); }
		if(thisRef.height != null) { nTable.setAttribute("height", thisRef.height); }
		if(thisRef.padding != null) { nTable.setAttribute("cellpadding", thisRef.padding); }
		if(thisRef.spacing != null) { nTable.setAttribute("cellspacing", thisRef.spacing); }
		if(thisRef.border != null) { nTable.setAttribute("border", thisRef.border); }
		if(thisRef.className != null) { nTable.setAttribute("class", thisRef.className); }
		if(thisRef.id != null) { nTable.setAttribute("id", thisRef.id); }
		
		var tr = document.createElement("tr");
		var td = document.createElement("td");
		
		var cc, rc, cTr, cTd, cl;
		var r = 0;
		var c = 0;
		rc = thisRef.Rows.length - 1;
		do {
			c = 0;
			cc = thisRef.Rows[r].length - 1;
			cTr = nTable.appendChild(tr.cloneNode(false));
			do {
				cTd = cTr.appendChild(td.cloneNode(false));
				cl = thisRef.Rows[r][c];				
				cTd.innerHTML = (cl.value != null)?cl.value:"&nbsp;";
				buildCell(cl, cTd);
			} while(c++ < cc);
		} while(r++ < rc);
		return getOuterHTML(nTable);
	}
}

//SimpleImage
function SimpleImage(src) {
	this.link;
	this.width;
	this.height;
	this.src = src;
	this.alt;
	
	var thisRef = this;
	
	this.toHTML = function() {
		var iObj = document.createElement("img");
		if(thisRef.src != null) { iObj.setAttribute("src", thisRef.src); }
		if(thisRef.width != null) { iObj.setAttribute("width", thisRef.width); }
		if(thisRef.height != null) { iObj.setAttribute("height", thisRef.height); }
		if(thisRef.alt != null) { iObj.setAttribute("alt", thisRef.alt); }
		if(thisRef.link != null) {
			iObj.setAttribute("border", 0);
			var aObj = document.createElement("a");
				aObj.setAttribute("href", thisRef.link);
				aObj.appendChild(iObj);
				
				return getOuterHTML(aObj);
		} else {
			return getOuterHTML(iObj);
		}
	}
}

// Simple Button
function SimpleButton(text) {
	this.id;
	this.action;
	this.className;
	this.width;
	this.height;
	this.value = text;
	this.isDisabled = false;
	
	var thisRef = this;
	
	this.toHTML = function() {
		var btn = document.createElement("input");
			btn.setAttribute("type", "button");
			btn.setAttribute("value", thisRef.value);
			
			if(thisRef.id != null) { btn.setAttribute("id", thisRef.id); }
			if(thisRef.action != null) { btn.setAttribute("onClick", thisRef.action); }
			if(thisRef.className != null) { btn.setAttribute("class", thisRef.className); }
			if(thisRef.width != null) { btn.setAttribute("width", thisRef.width); }
			if(thisRef.height != null) { btn.setAttribute("height", thisRef.height); }
			if(thisRef.isDisabled) { btn.setAttribute("disabled", "true"); }
		return getOuterHTML(btn);
	}
}

//Simple Select
function SimpleSelect() {
	this.id;
	this.action;
	this.className;
	this.width;
	this.isDisabled = false;
	this.multiple = false;
	this.size;
	
	var thisRef = this;
	var Items = new Array();
	
	this.AddItem = function(name, value, isDef) {
		Items.push([name, value, (isDef != null && isDef == true)?true:false]);		
	}
	
	this.SelectItem = function(idx) {
		if(idx > 0 && idx < Items.length) {
			Items[idx][2] = true;
		}
	}
	
	this.toHTML = function() {
		var sel = document.createElement("select");
		
		if(thisRef.id != null) { sel.setAttribute("id", thisRef.id); }
		if(thisRef.action != null) { sel.setAttribute("onChange", thisRef.action); }
		if(thisRef.className != null) { sel.setAttribute("class", thisRef.className); }
		if(thisRef.width != null) { sel.setAttribute("width", thisRef.width); }
		if(thisRef.size != null) { sel.setAttribute("size", thisRef.size); }
		if(thisRef.isDisabled) { sel.setAttribute("disabled", "true"); }
		if(thisRef.multiple) { sel.setAttribute("multiple", "true"); }
		
		var opt = document.createElement("option");
		if(Items.length > 0) {
			var oLen = 	Items.length-1;	
			var o = 0;
			var thisOpt;
			var thisItem;
			do {
				thisItem = Items[o];
				thisOpt = sel.appendChild(opt.cloneNode(false));
				thisOpt.innerHTML = thisItem[0];
				thisOpt.setAttribute("value", thisItem[1]);
				thisOpt.selected = thisItem[2];
			} while(o++ < oLen);
		}
		return getOuterHTML(sel);
	}
}

//Simple Link
function SimpleLink(url, title) {
	this.id;
	this.href = url;
	this.className;
	this.target;
	this.title = title;
	
	var thisRef = this;
	
	this.toHTML = function() {
		var lnk = document.createElement("a");
		if(thisRef.id != null) { lnk.setAttribute("id", thisRef.id); }
		if(thisRef.href != null) { lnk.setAttribute("href", thisRef.href); }
		if(thisRef.className != null) { lnk.setAttribute("class", thisRef.className); }
		if(thisRef.target) { lnk.setAttribute("target", thisRef.target); }
		lnk.innerHTML = thisRef.title;
		
		return getOuterHTML(lnk);
	}	
}
