Function.prototype.closure = function(obj)
{
  // Init object storage.
  if (!window.__objs) window.__objs = [];

  // Init closure storage.
  if (!this.__closureFuncs) this.__closureFuncs = [];

  // Make sure the object has an id and is stored in the object store.
  var objId = obj.__closureObjId;
  if (!objId) __objs[objId = obj.__closureObjId = __objs.length] = obj;

  // See if we previously created a closure for this object/function pair.
  var closureFunc = this.__closureFuncs[objId];
  if (closureFunc) return closureFunc;

  // Clear reference to keep the object out of the closure scope.
  obj = null;

  // Create the closure, store in cache and return result.
  var me = this;
  return this.__closureFuncs[objId] = function()
  {
    return me.apply(__objs[objId], arguments);
  };
};

Function.prototype.test = function()
{
  alert("test")
}

var tool_element = function(){
	this.eventAttr = new Array ("abort","blur","change","click","error","focus","keydown","keypress","keyup","load","mousedown","mousemove","mouseout","mouseover","mouseup","reset","resize","scroll","select","submit","unload");
	this.singleTagArray = new Array ("br","hr","img","input");
	this.htmlTagRe = /</g
	this.htmlSingleTagRe = /<(\S+).*\/>/g
	this.htmlStartTagRe = /<(\S+).*>/
	this.htmlEndTagRe = /<\/(\S+).*>/
	this.attrSymRe = /=| /g
}

tool_element.prototype.searchInArray = function(searchArray,searchValue)
{
	for (x in searchArray)  if (searchArray[x]==searchValue) return true;
	return false
}

tool_element.prototype.sortAttribute = function(sortValue)
{
  this.flag1=false;
  this.pos = 0;
  this.startpos = 0
  this.counter = 0;
  this.returnValue= new Array();
  if (sortValue) {
    while (this.res = this.attrSymRe.exec(sortValue))
    {
		  if (this.flag1)
		  {
			  if (this.res==" ") this.pos = this.attrSymRe.lastIndex
			  if (this.res=="=")
			  {
				  this.returnValue[this.counter] = sortValue.substr(this.startpos,this.pos-this.startpos-1);
				  this.counter++;
				  this.startpos =this.pos
				}
			}
		  if (this.res=="=") this.flag1=true;
		}
		this.returnValue[this.counter] = sortValue.substr(this.startpos);
		return this.returnValue;
	}
	else return "";
}

tool_element.prototype.testIe = function()
{
  if (navigator.appName=="Microsoft Internet Explorer") return true;
  else return false;
}

tool_element.prototype.addEvent = function(obj, eventName,  aFnRe)
{
  var aFnReAr = new Array();
  for (n=3; n<arguments.length; n++) aFnReAr[n-3] =arguments[n];
    try{
			obj.attachEvent("on"+eventName.toLowerCase(), function(evt){
			  aFnRe(aFnReAr[0],aFnReAr[1],aFnReAr[2],aFnReAr[3],aFnReAr[4],aFnReAr[5],aFnReAr[6],aFnReAr[7],aFnReAr[8],aFnReAr[9],aFnReAr[10],aFnReAr[11],aFnReAr[12],aFnReAr[13],aFnReAr[14]);
			});  
		}
		catch(e)
		{
    	obj.addEventListener(eventName.toLowerCase(), function(evt){ 
			  aFnRe(aFnReAr[0],aFnReAr[1],aFnReAr[2],aFnReAr[3],aFnReAr[4],aFnReAr[5],aFnReAr[6],aFnReAr[7],aFnReAr[8],aFnReAr[9],aFnReAr[10],aFnReAr[11],aFnReAr[12],aFnReAr[13],aFnReAr[14]);
			}, false);
    }
}

tool_element.prototype.elementAtt = function(obj,attrName, AttrValue)
{
try{
  if(!AttrValue) return
  AttrValue = AttrValue.replace(/'|"/g,"");
	if (attrName.toLowerCase() == "style"){
 		obj.style.cssText=AttrValue;
  	obj.setAttribute("style",AttrValue); 
  }
	else if (attrName =='class' || attrName =='className' )
  {
   	if (this.testIe()) obj.setAttribute('className', AttrValue);
	  else obj.setAttribute('class', AttrValue);
	}
  else if ( this.searchInArray(this.eventAttr,attrName) )
  {
    alert("Event properties kan ikke indsættes her!")
//	  this.addEvent(obj,attrName, AttrValue);
	}
	else
	{
	  obj.setAttribute(attrName, AttrValue);
	}
}
catch(e)
{
//  alert("test")
} 
}
tool_element.prototype.element = function(elementName,obj)
{
  if (typeof obj == "string") obj = document.getElementById(obj);
 	if (!obj) return false;
  newElement = document.createElement(elementName);
 	for (a=2; a<arguments.length;a++)
	{
	  if (arguments[a]!="")
	  {
	    if (typeof arguments[a]=='string')
	    {
				argument = arguments[a].split("=");
				this.elementAtt(newElement,argument[0],argument[1]);
			}
			else
			{
 				for (b=0; b<arguments[a].length;b++)
 				{
				  if (arguments[a][b]!="")
	  			{
						argument = arguments[a][b].split("=");
						this.elementAtt(newElement,argument[0],argument[1]);
					}
				}
			}
		}
	}	
  obj.appendChild(newElement);
  return newElement;
}

tool_element.prototype.textNode = function(textValue,obj)
{
	if(textValue=="") return false;
  if (typeof obj == "string") obj = document.getElementById(obj);
 	if (!obj) return false;
 	this.res = this.htmlTagRe.exec(textValue);
 	if (this.res) this.textNodeElm(textValue,obj);
 	else
 	{
    newText = document.createTextNode(textValue);
    obj.appendChild(newText);
    return newText;
  }
}

tool_element.prototype.textNodeElm = function(textValue,obj)
{
	this.level = 0;
	this.objLevel = new Array(obj)
  textValue=textValue.split("<").join(String.fromCharCode(31)+"<").split(">").join(">"+String.fromCharCode(31)).split(String.fromCharCode(31))
	for (n=0; n<textValue.length; n++)
	{
		this.htmlStartTagRe.lastIndex=0;
		this.htmlTagRe.lastIndex=0;
		this.htmlEndTagRe.lastIndex=0;
	  if (!this.htmlTagRe.exec(textValue[n])) this.textNode(textValue[n],this.objLevel[this.level]);
	  else if (textValue[n].charAt(0)=="<")
		{
	    if (this.res = this.htmlEndTagRe.exec(textValue[n]))
	    {
				this.level--;
			}
			else
			{
		    this.res = this.htmlStartTagRe.exec(textValue[n])
		    this.tagValue= this.res[1];
		    this.elmProp = textValue[n].replace(/ = | =|= /g,"=");
		    this.elmProp = this.elmProp.split("<"+this.res[1]+" ").join("").split("<"+this.res[1]).join("").split("/>").join("").split(" >").join("").split(">").join("");
		    this.elmProp = this.sortAttribute(this.elmProp);
				this.objLevel[this.level+1] = this.element(this.tagValue,this.objLevel[this.level],this.elmProp);
		  	if (!this.searchInArray(this.singleTagArray,this.tagValue)) this.level++;
			}
		}
	} 
}

tool_element.prototype.deleteAllElement = function(obj)
{
  while (obj.childNodes[0]) {
    obj.removeChild(obj.childNodes[0]);
  }
}

tool_element.prototype.selectType = function(obj, sizeValue,  nameValue, optionValue, selectedValue)
{
  this.oOption  = new Array();  
  this.oSelect = this.element("select",obj);
  this.oSelect.setAttribute("size",sizeValue);
  this.oSelect.setAttribute("name",nameValue);
  this.oSelect.setAttribute("id",nameValue);
  for (i=0; i<optionValue[0].length; i++)
  {
    this.oOption[i] = this.element("option",this.oSelect);
    if (selectedValue==optionValue[1][i]) this.oOption[i].selected=true; else this.oOption[i].selected=false;
    this.oOption[i].text=optionValue[0][i];
    this.oOption[i].value=optionValue[1][i];
  }
  return this.oSelect;
}

tool_element.prototype.addOption =function(optionValue,selectedValue,oSelect)
{
  for (i=0; i<optionValue[0].length; i++)
  {
    this.oOption = document.createElement("option");
    if (selectedValue==optionValue[1][i]) this.oOption.selected=true; else this.oOption.selected=false;
    this.oOption.text=optionValue[0][i];
    this.oOption.value=optionValue[1][i];
    if (this.testIe()) oSelect.add(this.oOption);
    else oSelect.appendChild(this.oOption);
  }
}

tool_element.prototype.changeSelect = function(oSelect,selectedValue)
{
  oSelect.value=selectedValue;
/*
  for (i=0; i<oSelect.options.length; i++)
  {
    if (selectedValue==oSelect.options[i].value) 
		{
		  oSelect.options[i].selected=true;
		}
		else oSelect.options[i].selected=false;
  }

*/
}

