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, ARGUMENTS)
{
  var aFnReAr = new Array();
  for (n=2; n<ARGUMENTS.length; n++) aFnReAr[n-2] = ARGUMENTS[n];
    if (typeof attachEvent != "undefined"){
			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]);
			});  
		}
		else if (typeof addEventListener != "undefined")
		{
   	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]);
			}, false);
    }
    else alert("Can't add event")
}

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 (attrName =='name' )
  	{
   		if (this.testIe()) alert(AttrValue);
	  	else obj.setAttribute('name', 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)
	{
	} 
}

Tool_Element.prototype.returnString = function(obj)
{
 	if (typeof obj=='string') this.argument += " " + obj;
	else for (var b=0; b<obj.length; b++) this.returnString(obj[b]);
}  


Tool_Element.prototype.element = function(elementName,obj)
{
  if (typeof obj == "string") obj = document.getElementById(obj);
 	if (!obj) return false;
  
  if (testIe())
  {
    this.argument = ""; 
		for (var a=2; a<arguments.length;a++)
		{
 	  	if (arguments[a]!="")
	  	{
	    	if (typeof arguments[a]=='string')
	    	{
					this.argument += " " + arguments[a];
				}
				else
				{
 					this.returnString(arguments[a]);
 				}
			}
		}
		if (elementName=="input" || elementName=="img" || elementName=="br")
		{
			var elem = "<" + elementName + " " + this.argument + " />";
		}
		else
		{
			var elem = "<" + elementName + " " + this.argument + "></" + elementName + ">";
		}
	 	newElement = document.createElement(elem);
	}
	else
	{
	 	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 (typeof arguments[a][b]=='string' && arguments[a][b]!="")
	  				{
							argument = arguments[a][b].split("=");
							this.elementAtt(newElement,argument[0],argument[1]);
						}
					}
				}
			}
		}	
	}
	obj.appendChild(newElement);
  return newElement;
}

Tool_Element.prototype.insertText = 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.insertText(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.Empty = function(obj)
{
  if (typeof obj!="object") return false;
  try{
	  while (obj.childNodes[0]) {
  	  obj.removeChild(obj.childNodes[0]);
  	}
	}
	catch(e)
	{
		alert(e)	  
	}
}

Tool_Element.prototype.selectType = function(obj, sizeValue,  nameValue, optionValue, selectedValue)
{
  this.oSelect = this.element("select",obj);
  this.oSelect.setAttribute("size",sizeValue);
  this.oSelect.setAttribute("name",nameValue);
  this.addOption(optionValue,selectedValue,this.oSelect)
  return this.oSelect;
}

Tool_Element.prototype.addOption =function(optionValue,selectedValue,oSelect)
{
  this.oOption  = new Array();  
  for (i=0; i<optionValue[0].length; i++)
  {
    this.oOption[i] = this.element("option",oSelect);
    this.oOption[i].text=optionValue[0][i];
    this.oOption[i].value=optionValue[1][i];
  }
  this.changeSelect(oSelect,selectedValue);
}

Tool_Element.prototype.changeSelect = function(oSelect,selectedValue)
{
  oSelect.value=selectedValue;
}

Tool_Element.prototype.Text = function(Value, obj)
{
    return this.insertText(Value, obj)
}
Tool_Element.prototype.CreateElement = function(element, obj, arg)
{
    var newArray = new Array()
    for (n=1; n<arg.length; n++)
    newArray.push(arg[n]);
    if (arg.length>1) return this.element(element,obj,newArray);
    else return this.element(element,obj);
}
Tool_Element.prototype.Div = function(obj)
{
    return this.CreateElement("div",obj,arguments);
}

Tool_Element.prototype.Array = function(obj)
{
    return this.element("area",obj,arguments);
}

Tool_Element.prototype.Img = function(obj)
{
    return this.element("img",obj,arguments);
}

Tool_Element.prototype.Map = function(obj)
{
    return this.element("map",obj,arguments);
}

Tool_Element.prototype.Span = function(obj)
{
    return this.CreateElement("span",obj,arguments);
}
Tool_Element.prototype.Fieldset = function(obj,legend)
{
    var newArray = new Array();
    for (n=2; n<arguments.length;n++)
    newArray.push(arguments[n]);
    var field = this.element("fieldset",obj, newArray);
    var leg = this.element("legend", field);
    this.insertText(legend,leg);
    return field;
}
Tool_Element.prototype.Table = function(obj)
{
    return this.CreateElement("table",obj,arguments);
}
Tool_Element.prototype.Thead = function(obj)
{
    return this.CreateElement("thead",obj,arguments);
}
Tool_Element.prototype.Th = function(obj)
{
    return this.CreateElement("th",obj,arguments);
}
Tool_Element.prototype.Tbody = function(obj)
{
    return this.CreateElement("tbody",obj,arguments);
}
Tool_Element.prototype.Tr = function(obj)
{
    return this.CreateElement("tr",obj,arguments);
}
Tool_Element.prototype.Td = function(obj)
{
    return this.CreateElement("td",obj,arguments);
}
Tool_Element.prototype.Form = function(obj)
{
    return this.CreateElement("form",obj,arguments);
}
Tool_Element.prototype.Textarray = function(obj)
{
    return this.CreateElement("textarea",obj,arguments);
}
Tool_Element.prototype.Link = function(obj)
{
    return this.CreateElement("link",obj,arguments);
}
Tool_Element.prototype.Image = function(obj)
{
    return this.CreateElement("img",obj,arguments);
}

Tool_Element.prototype.Button = function(obj)
{
    return this.CreateElement("button",obj,arguments);
}

Tool_Element.prototype.Script = function(obj)
{
    return this.CreateElement("script",obj,arguments);
}

Tool_Element.prototype.Input = function(obj,Type)
{
    arguments[1]= "type='"+Type+"'";
    return this.CreateElement("input",obj,arguments);
}
Tool_Element.prototype.Radio = function(obj,Type)
{
    arguments[1]= "type='radio'";
    return this.element("input",obj,arguments);
    //    return this.CreateElement("input",obj,arguments);
}
Tool_Element.prototype.Br = function(obj)
{
    return this.element("br",obj,arguments);
}
Tool_Element.prototype.Hr = function(obj)
{
    return this.element("hr",obj,arguments);
}
Tool_Element.prototype.H1 = function(obj)
{
    return this.element("h1",obj,arguments);
}
Tool_Element.prototype.H2 = function(obj)
{
    return this.element("h2",obj,arguments);
}
Tool_Element.prototype.H3 = function(obj)
{
    return this.element("h3",obj,arguments);
}
Tool_Element.prototype.H4 = function(obj)
{
    return this.element("h1",obj,arguments);
}
Tool_Element.prototype.H4 = function(obj)
{
    return this.element("h1",obj,arguments);
}
Tool_Element.prototype.H5 = function(obj)
{
    return this.element("h5",obj,arguments);
}
Tool_Element.prototype.H6 = function(obj)
{
    return this.element("h6",obj,arguments);
}
Tool_Element.prototype.P = function(obj)
{
    return this.element("p",obj,arguments);
}
Tool_Element.prototype.A = function(obj)
{
    return this.element("a",obj,arguments);
}
Tool_Element.prototype.Iframe = function(obj)
{
    return this.element("iframe",obj,arguments);
}
Tool_Element.prototype.Click = function(obj,aFnRe)
{
    this.addEvent(obj, "click",  aFnRe, arguments)
}
Tool_Element.prototype.Change = function(obj,aFnRe)
{
    this.addEvent(obj, "change",  aFnRe, arguments)
}
Tool_Element.prototype.MouseDown = function(obj,aFnRe)
{
    this.addEvent(obj, "mousedown",  aFnRe, arguments)
}
Tool_Element.prototype.MouseMove = function(obj,aFnRe)
{
    this.addEvent(obj, "mousemove",  aFnRe, arguments)
}
Tool_Element.prototype.MouseUp = function(obj,aFnRe)
{
    this.addEvent(obj, "mouseup",  aFnRe, arguments)
}
Tool_Element.prototype.MouseOver = function(obj,aFnRe)
{
    this.addEvent(obj, "mouseover",  aFnRe, arguments)
}
Tool_Element.prototype.MouseOut = function(obj,aFnRe)
{
    this.addEvent(obj, "mouseout",  aFnRe, arguments)
}
Tool_Element.prototype.KeyUp = function(obj,aFnRe)
{
    this.addEvent(obj, "keyup",  aFnRe, arguments)
}
Tool_Element.prototype.KeyDown = function(obj,aFnRe)
{
    this.addEvent(obj, "keydown",  aFnRe, arguments)
}
Tool_Element.prototype.KeyPress = function(obj,aFnRe)
{
    this.addEvent(obj, "keypress",  aFnRe, arguments)
}
Tool_Element.prototype.Focus = function(obj,aFnRe)
{
    this.addEvent(obj, "focus",  aFnRe, arguments)
}
Tool_Element.prototype.Blur = function(obj,aFnRe)
{
    this.addEvent(obj, "blur",  aFnRe, arguments)
}

Tool_Element.prototype.InsertTextLine = function(TargeObject,TextString,ClassName)
{
  var returnObject = this.Span(TargeObject);
  if (ClassName) returnObject.className = ClassName;
  this.Text(TextString,returnObject);
  return returnObject;
}

Tool_Element.prototype.InsertSelect = function(TargeObjectSelectObj,SelectName,SelectValue,SelectedValue,SelectSize,Name) {
    if (!SelectSize) SelectSize = 1;
    if (!Name) Name  = "StandardName";
    var SelectObj = new Array();
    SelectObj[0] = SelectName;
    SelectObj[1] = SelectValue;
    var SelectField = this.selectType(TargeObjectSelectObj,SelectSize,SelectName,SelectObj,SelectedValue);
    return SelectField;
}
