function elementFromEvent(event)
{
  if(!event) event = this.event;
    return event.target || event.srcElement;
}
function appendElementToAnother(element, newParent)
{
   $(element).parentNode.removeChild($(element));
   $(newParent).appendChild($(element));
}
function createElement(tag,id,parent,className)
{
  var element = document.createElement(tag);
 if(id!="")   
   element.id = id;
   if(className)
   {
     element.className =className; 
   }
   if(parent)
   {
     parent.appendChild(element);
   }
   return element;
}



function createDiv(id,parent,className)
{
   return createElement("div",id,parent,className);
}

/*utils*/
function setPosition(element,left,top)
{
  setTop(element,top);
  setLeft(element,left);  
}
function setSize(element,width,height)
{
  setHeight(element,height);
  setWidth(element,width);  
}
function setLeft(element,value)
{
  $(element).style.left = value+"px";
} function setTop(element,value)
{
  $(element).style.top = value+"px";
}
function setHeight(element,value)
{
  $(element).style.height = value+"px";
} function setWidth(element,value)
{
  if(value.toString().indexOf("%")!=-1) $(element).style.width = value;
  else
 
  $(element).style.width = value+"px";
} function getHeight(element)
{
  return parseInt($(element).style.height);
} function getWidth(element)
{
  return parseInt($(element).style.width);
}
function getTop(element)
{
  return parseInt($(element).style.top);
}
function getLeft(element)
{
  return parseInt($(element).style.left);
}
function setAbsolute(element)
{
   $(element).style.position ="absolute";
}
function appendElementToAnother(element, newParent)
{
   $(element).parentNode.removeChild($(element));
   $(newParent).appendChild($(element));
}
function destroyElement(element)
{
  element.parentNode.removeChild(element);
}
var isIE  = navigator.userAgent.toLowerCase().indexOf("ie")!= -1; 
var isOpera = navigator.userAgent.indexOf("Opera") > -1
// ---------------------------------------------------

function showSelectBoxes(id){
	selects = $(id).getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
}

// ---------------------------------------------------

function hideSelectBoxes(id){
	selects = $(id).getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
	}
}

// -----------------------------------------------------------------------------------

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

// ----------------------------


// -----------------------------------------------------------------------------------

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

// -----------------------------------------------------------------------------------



var DOMBuilder =Class.create();

DOMBuilder.prototype={
 
 initialize:function(container,idPart,idAsClass)
 {
 	if(container)
 	this.container = $(container);
 	
 	this.idAsClass = idAsClass;
 	this.idPart = idPart; 	 
 	this.currentNode = this.container;	
 },
 addChild:function(id,className,tag)
 {
 	return this.addTo(this.currentNode,id,className,tag);
 },
  addSibling:function(id,className,tag)
 {
 	return this.addTo(this.currentNode.parentNode,id,className,tag);
 	
 },
 indexOf:function(element)
 {
 	for(var i=0;i<element.parentNode.childNodes.length;i++)
	{
		if(element==element.parentNode.childNodes[i]) return i;
	}
	return -1;
 },
 addAfter:function(afterElement,id,className,tag)
 {
 	//debugger;
	var parent  = afterElement.parentNode;
	var childs =parent.childNodes;
	var destroyedChilds = new Array();
	var length = childs.length;
	var j=this.indexOf(afterElement)+1;
	//deletete all elements after element
	for(var i=this.indexOf(afterElement)+1;i<length;i++)
	{
		destroyedChilds.push(childs[j]);
		destroyElement(childs[j]);		
	}
	//add element
	
	this.addTo(parent,id,className,tag);
//	/ and other elements
    
	for(var i=0;i<destroyedChilds.length;i++)
	{
	  parent.appendChild(destroyedChilds[i]);	
	}
   
    return this.currentNode;
 },
 addTo:function(parentNode,id,className,tag)
 {
 	if(this.idAsClass)className = id;
 	if(!tag) 
 	this.currentNode =createDiv(parentNode.id+"_"+id,parentNode,className);
 	else
 	this.currentNode =createElement(tag,this.getId(id),parentNode,className);     
    return this.currentNode;
 } ,
 getId:function(id)
 {
 	if(this.idPart) return this.idPart+"_"+id;
 	else id;
 }
}