//************************ABSTRACT CLASSES*****************************
function AbstractDropDown()
{
	this.id="";
	this.remoteTextId="";
	this.activeElement = -1;
	this.knownHeight = -1;
	this.dropdownId = "";
	this.mouseOver = "";
	this.hasButton = true;
	this.dropDownWidth = "";
	this.dropdownIsVisible=true;
	this.buttonClick = false;
	this.dropdownElements = Array();
	this.first = true;
	this.floatDropDown = true;
	this.alwaysVisible = false;
	this.enterSubmit   = false;
	this.JSonclick     = "";
}

AbstractDropDown.prototype.getDropDownElement = function (value)
{
	if(!this.dropdownElements)return null;
	for(var i=0;i<this.dropdownElements.length;i++)
	{
		var item = this.dropdownElements[i];
		if(item.value == value){return item;}
	}
	return null;
}

AbstractDropDown.prototype.selectDropDownElement = function (value)
{
	if(!this.dropdownElements)return null;
	for(var i=0;i<this.dropdownElements.length;i++)
	{
		var item = this.dropdownElements[i];
		if(item.value == value){this.activeElement=i;return item;}
	}
	return null;
}

AbstractDropDown.prototype.addElement = function(value,text)
{
	//text  = unescapeHTMLSpecialChars(text);
	//value = unescapeHTMLSpecialChars(value);
	//alert(value);
	var item = {value:value,text:text};
	this.dropdownElements[this.dropdownElements.length] = item;
}

AbstractDropDown.prototype.clear = function()
{
	this.dropdownElements = Array();
	var dropDownDiv = document.getElementById(this.dropdownId);
	
}


AbstractDropDown.prototype.handleKeyEvent = function(e)
{
 if(!e)e=window.event;
 var object = this;
 var node = document.getElementById(this.id);
 var remote = document.getElementById(this.remoteTextId);
 var last = this.activeElement;
   if(e.keyCode==13) { //enter knop
	stopEvent(e);
	if(this.activeElement>=0)
	{
		node.value = this.dropdownElements[this.activeElement].text;
		remote.value = this.dropdownElements[this.activeElement].value;
		this.fireChangeEvent();
	}
	else if(this.dropdownElements.length>0)
	{
		node.value = this.dropdownElements[0].text;
		remote.value = this.dropdownElements[0].value;
		this.fireChangeEvent();
	}
	this.hideDropDown();
	if(this.enterSubmit)agSubmitViaEnter(e, node);
	eval(object.JSonclick);
  } 
  else if(e.keyCode==38) {  //pijltje omhoog
	if(this.activeElement>0)
	{
		this.activeElement = this.activeElement-1;
		node.value = this.dropdownElements[this.activeElement].text;
		document.getElementById(this.remoteTextId).value = this.dropdownElements[this.activeElement].value;
		this.refreshDropDown(last,"up");
	}else{
		this.refreshDropDown(last,"show");
	}
  }
  else if(e.keyCode==40) { //pijltje omlaag
	if(this.activeElement<this.dropdownElements.length-1)
	{
		this.activeElement = this.activeElement+1;
		node.value = this.dropdownElements[this.activeElement].text;
		document.getElementById(this.remoteTextId).value = this.dropdownElements[this.activeElement].value;
		this.refreshDropDown(last,"down");	//laat alleen de div zien
	}else{
		this.refreshDropDown(last,"show");
	}
	if(!this.filled)
	{
		this.knownHeight = 0;
		this.handleButtonRequest(); //doe een database request voor de inhoud
		this.filled = true;
	}
  }
  else if(e.keyCode==8) //backspace
  {
	this.lastValue= "";
  }
}
AbstractDropDown.prototype.handleButtonRequest = function(){}

AbstractDropDown.prototype.refreshDropDown = function(last,direction) //ververs de dropdown zonder alles opnieuw te renderen
{
	var node = document.getElementById(this.id);
	node.title = node.value;
	var dropDownDiv = document.getElementById(this.dropdownId);
	var previous = document.getElementById(this.dropdownId + "Element" + last);
	if(last>=0)
		previous.className = ""; //maakt de achtergrond van het vorige element weer wit
	this.showDropDownDiv();
	if(this.first)
	{	
		this.setDimensions(dropDownDiv,node);
		this.first = false;
	}
	if(this.activeElement>=0 && direction!="mouse")
	{
		var selected = document.getElementById(this.dropdownId + "Element" + this.activeElement);
		if(this.knownHeight < 0 && last>=0)
			this.knownHeight = calculatePreviousChildrenHeight(previous);  //berekent de hoogte van alle element voor het huidige element
			
		if(direction=="up")
			this.knownHeight -= getElementHeight(previous);
		else if(direction=="down")
			this.knownHeight += getElementHeight(previous);
		this.selectActiveElement();
	}
}

AbstractDropDown.prototype.selectActiveElement = function()  //het element in het lijstje selecteren
{
	if(this.activeElement<0)return;
	var dropDownDiv = document.getElementById(this.dropdownId);
	var dropHeight = getElementHeight(dropDownDiv);
	//alert(dropHeight);
	var selected = document.getElementById(this.dropdownId + "Element" + this.activeElement);
	var elHeight = getElementHeight(selected);
	selected.className = this.mouseOver;
	if(this.knownHeight < 0)this.knownHeight = calculatePreviousChildrenHeight(selected); 
	if(this.knownHeight<elHeight)
	{
		dropDownDiv.scrollTop = 0;
		this.knownHeight = 0;
	}
	else if(this.knownHeight>dropHeight+dropDownDiv.scrollTop-elHeight)
		dropDownDiv.scrollTop = this.knownHeight-dropHeight+elHeight;    //scrolled de div mee met het geselecteerde item(omlaag)
	else if(this.knownHeight<dropDownDiv.scrollTop)   //scrolled de div mee met het geselecteerde item(omhoog)
		dropDownDiv.scrollTop = this.knownHeight;
}


AbstractDropDown.prototype.switchDropDownDiv = function() //het menu verbergen/laten zien
{
	var input = document.getElementById(this.id);
	var dropDownDiv = document.getElementById(this.dropdownId);
	var object = this;
	//alert(this.dropdownIsVisible);
	input.select();
	if(dropDownDiv.style.display != "none")
	{	
		this.hideDropDown();
	}else{
		this.showDropDownDiv();
		this.selectActiveElement();
	}
}


AbstractDropDown.prototype.showDropDownDiv = function() //laat het menu zien zonder het opnieuw te renderen
{	
	var dropDownDiv = document.getElementById(this.dropdownId);
	var input     	= document.getElementById(this.id);
	var object = this;
	if(this.floatDropDown)
	{
		dropDownDiv.style.position = "absolute";
		dropDownDiv.style.left = getRealLeftPos(input) + "px";
		dropDownDiv.style.top = getRealTopPos(input) + getElementHeight(input) + 1 + "px";
		//alert(dropDownDiv.style.left);
		//alert(dropDownDiv.style.top);
	}else{
		dropDownDiv.style.position = "relative";
	}
	dropDownDiv.style.display = "";
	dropDownDiv.style.zIndex = 999999;
	this.dropdownIsVisible = true;
		
}

AbstractDropDown.prototype.hideDropDown = function() //sluit het menu
{
	if(this.alwaysVisible&&this.dropdownElements.length>0)return;
	if(this.buttonClick)
	{
		this.buttonClick = false;
		return;
	}
	var dropDownDiv = document.getElementById(this.dropdownId);
	var object = this;
	if(dropDownDiv.hasFocus)return;

	this.dropdownIsVisible = false;
	var element = document.getElementById(this.id);
	dropDownDiv.style.display = "none";
}
AbstractDropDown.prototype.setDimensions = function(dropDownDiv,input)   //plaatst het lijstje onder het tekstvak
{ 
		var width = getElementWidth(input);
		if(this.dropDownWidth!="")
			dropDownDiv.style.width = this.dropDownWidth;
		else if(this.hasButton)
			dropDownDiv.style.width = width +20+ "px"; 
		else
			dropDownDiv.style.width = width +  "px";	
		
		var height = 0;

		for(var i=0;i<dropDownDiv.childNodes.length;i++)
		{
			var element = dropDownDiv.childNodes[i];
			try{
				if(element.style&&(element.style.position).toLowerCase() == "absolute") continue;
				height+=getElementHeight(element);
				//alert("element:" + element + "height" + getElementHeight(element));
			}catch(e){}
			if(height>=200)
			{
				dropDownDiv.style.height = 200+ "px";
				return;
			}
		}
		dropDownDiv.style.height = height +1+ "px";
}

AbstractDropDown.prototype.showDropDown = function()     //render het menu en laat het zien
{
	var dropDownDiv = document.getElementById(this.dropdownId);
	var input     	= document.getElementById(this.id);
	var valueinput  	= document.getElementById(this.remoteTextId);	
	var object = this;
	this.validateSelect();
	dropDownDiv.onfocus  = function(e){this.hasFocus = true;};
	dropDownDiv.onblur   = function(e){this.hasFocus = false;object.hideDropDown();};
	dropDownDiv.style.zIndex = 999999;
	dropDownDiv.innerHTML = "";
	//this.setDimensions(dropDownDiv,input);
	  for (var i = 0; i < this.dropdownElements.length; i++)
	  {
		//remove empty items
		if(this.dropdownElements[i].text == "")
		{
			this.dropdownElements.splice(i,1);
			i--;
			continue;
		}	  
		//--
		var newOptionNode = document.createElement("div");
		newOptionNode.title = newOptionNode.innerHTML = escapeHTMLSpecialChars(this.dropdownElements[i].text); //escapeHTMLSpecialChars
		newOptionNode.id = this.dropdownId + "Element" + i;
		newOptionNode.style.width = "100%";
		var newOptionNodeValue = document.createElement("div");
		newOptionNodeValue.innerHTML = this.dropdownElements[i].value;
		newOptionNodeValue.id = this.dropdownId + "Element" + i + "_VALUE";
		newOptionNodeValue.style.display = "none";
		if(this.dropDownWidth!="")
			newOptionNode.style.width =this.dropDownWidth;
		newOptionNode.onmouseover = function(){
							 object.knownHeight = -1;
							 if(object.activeElement>=0)
							 {
								object.refreshDropDown(object.activeElement,"mouse");
						      }
							  var text = this.id;
							  text = text.replace(object.dropdownId + "Element","");
							  object.activeElement = parseInt(text);
							 this.className = object.mouseOver;
							};
		newOptionNode.onmouseout  	= function(){this.className="";};
		newOptionNode.onmousedown	= function(e){
										object.knownHeight = -1;
										setTimeout(function(){object.buttonClick=false; try{input.focus();}catch(er){}},80);
										var newvalue = document.getElementById(this.id+"_VALUE").innerHTML;
										var item = object.selectDropDownElement(newvalue);
										valueinput.value = item.value;
										input.value = item.text;
										input.title = item.text;
										//var text = this.id;
										//text = text.replace(object.dropdownId + "Element","");
										//object.activeElement = parseInt(text);
										object.fireChangeEvent();
										eval(object.JSonclick);
										if(object.enterSubmit)
										{
											var closestSubmitNode = _findClosestSubmit(input, null); //ActiveGrid function
    										if (closestSubmitNode)
     										 	closestSubmitNode.onclick();
										}
									}; 
		newOptionNode.onmouseup = function(e){		
												stopEvent(e);
											};
		if(this.activeElement == i)
			newOptionNode.className = this.mouseOver;
		dropDownDiv.appendChild(newOptionNode);
		dropDownDiv.appendChild(newOptionNodeValue);
	  }

	this.filled = true;
	this.knownHeight = -1;
	if(!this.hasFocus&&!this.alwaysVisible)return;
	this.showDropDownDiv();
	this.setDimensions(dropDownDiv,input);
}
AbstractDropDown.prototype.validateSelect = function(){} //functie om de waarde te valideren (wordt oa gebuikt in de hinting select)
AbstractDropDown.prototype.fireChangeEvent = function() //fired de onchange events zoals bij een normale <SELECT\>
{
	var valueInput = document.getElementById(this.remoteTextId);
	fireEvent(valueInput,"change");

}

AbstractDropDown.prototype.stopKey = function(e) //stopt het keydown event als het geen tab is.
{
	if(!e)e=window.event;
	if(e.keyCode!=9) //9= tab-key
		stopEvent(e);
}

AbstractDropDown.prototype.setValue = function(inputId) //zet de waarde van de dropdown
{
	var item = this.selectDropDownElement(inputId);
	var textNode   = document.getElementById(this.id);
	var valueNode  = document.getElementById(this.remoteTextId); 
	if(item && valueNode && textNode)
	{
		valueNode.value = item.value;
		textNode.value  = item.text;
		return true;
	}
	else{return false;}
}
AbstractDropDown.prototype.selectElementByChar = function(e)
{
//functie die het mogelijk maakt om te typen in het dropdown en dan het juiste element selecteerd (zelfde gedrag als dropdowns in firefox)
    //getting the key
    var code;
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	if(code<47)return; //skip special chars
	var character = String.fromCharCode(code);
	character = character.toLowerCase();
	//selecting the element
	if(!this.dropdownElements)return;
	if(this.searchStr && this.lastKeyDown && (new Date()).getTime() - this.lastKeyDown.getTime() < 700)
	{
		this.searchStr += character;
	}else{
		this.searchStr  = character;
	}
	this.lastKeyDown = new Date();
	var last = this.activeElement;
	//find new element
	var found = false;
	var count = this.activeElement;
	for(var j=0;j<this.searchStr.length;j++)
	{	
		for(var i=count;i<this.dropdownElements.length;i++)
		{
			var item = this.dropdownElements[i];
			var curVal = item.text;
			if(curVal.length<1)continue;
			if(curVal.substr(0,this.searchStr.length).toLowerCase() == this.searchStr && (i != this.activeElement || this.searchStr.length>1))
			{
				this.activeElement = i;
				var textNode    = document.getElementById(this.id);
				var valueNode   = document.getElementById(this.remoteTextId); 
				valueNode.value = item.value;
				textNode.value  = item.text;
				textNode.title  = item.text;
				found = true;
				break;
			}
		}
		if(!found)
		{
			if(this.searchStr.length > 1)
			{
				this.searchStr = this.searchStr.substr(this.searchStr.length-1,1);
				j=-1;
			}else if(count > 0){
				count=0;
				j=-1;
			}
		}else{
			break;
		}
	}
	this.knownHeight = -1;
	var previous = document.getElementById(this.dropdownId + "Element" + last).className = "";
	this.refreshDropDown(this.activeElement,"show");
}

//ABSTRACT WINDOWS
function AbstractWindowControl()
{
	this.windows = [];
		this.idCounter  = 0;
}
//vraagt de breedte op
AbstractWindowControl.prototype.getMaxY = function(x)
{
	var y = 0;
	for(var i=0;i<this.windows.length;i++)
	{
		var win = this.windows[i];
		if(win.yPos+1>y && win.xPos==x)y = win.yPos+1;
	}
	return y;
}
//geeft de hoogte van een bepaalde column
AbstractWindowControl.prototype.getMaxX = function()
{
	var x = 0;
	for(var i=0;i<this.windows.length;i++)
	{
		var win = this.windows[i];
		if(win.xPos+1>x)x = win.xPos+1;
	}
	return x;
}
AbstractWindowControl.prototype.getWindow = function(x,y)
{
	for(var i=0;i<this.windows.length;i++)
	{
		var win = this.windows[i];
		if(win.xPos == x && win.yPos == y)return win;
	}
	return null;	
}
AbstractWindowControl.prototype.getWindowById = function(id)
{
	for(var i=0;i<this.windows.length;i++)
	{
		var win = this.windows[i];
		if(win.id == id)return win;
	}
	return null;	
}
AbstractWindowControl.prototype.removeWindowById = function(id)
{
	for(var i=0;i<this.windows.length;i++)
	{
		var win = this.windows[i];
		if(win.id == id)
		{
			this.windows.splice(i,1);
			return true;
		}
	}
	return false;	
}
//geeft grootste column waarde
AbstractWindowControl.prototype.getLargestColumn = function()
{
	var maxX = this.getMaxX();
	var uberY = 0;
	for(var x = 0;x<maxX;x++)
	{
		var maxY = this.getMaxY(x);
		if(maxY>uberY)
			uberY = maxY;
	}
	return uberY;
}
AbstractWindowControl.prototype.getNewId = function()
{
	this.idCounter++;
	return this.idCounter;
}
AbstractWindowControl.prototype.loadData = function()
{
	this.windows = [];
	
	var valueNode = document.getElementById(this.remoteValueId);
	if(!valueNode)
	{
		alert("EljakimWindowControl: Cannot find ValueInput with id: "+ this.remoteValueId);
		return;
	}
	var dataString = valueNode.value;

	this.parseDataString(dataString);
}
AbstractWindowControl.prototype.parseDataString = function(dataString)
{
	if(!dataString)
	{
		this.windows = [];
		return;
	}

	dataArray = dataString.split(";");
	
	for(var i=0;i<dataArray.length;i++)
	{
		var data = dataArray[i];
		if(data=="")continue;
		data = data.split(",");
		var window = {	xPos:parseInt(data[0]),
						yPos:parseInt(data[1]),
						xform:data[2],
						xformid:data[3],
						label:data[4],
						memo:data[5]
					};
		this.windows[this.windows.length] = window;

	}
	
	
}

AbstractWindowControl.prototype.saveData = function()
{
	dataString = "";
	for(var i=0;i<this.windows.length;i++)
	{
		var window  = this.windows[i];
		dataString += window.xPos + "," + window.yPos + "," + window.xform + "," + window.xformid + "," + window.label + "," + window.memo+";";
	}
	var valueNode = document.getElementById(this.remoteValueId);
	valueNode.value = dataString;
}
AbstractWindowControl.prototype.fixPositions = function()
{
	//elementen doorschuiven
	for(var i=0;i<this.windows.length;i++)
	{
		var win = this.windows[i];
		upperX = Math.ceil(win.xPos);
		upperY = Math.ceil(win.yPos);
		if(win.xPos < upperX)
		{
			this.correctX(upperX,win.yPos,1);
			win.xPos = upperX;
		}
		if(win.yPos < upperY)
		{
			this.correctY(win.xPos,upperY,1);
			win.yPos = upperY;
		}
	}
	//lege posities weghalen
	var maxX = this.getMaxX();
	for(var x = 0;x<maxX;x++)
	{
		var maxY = this.getMaxY(x);
		if(maxY == 0)
			this.correctX(x,y,-1);
		for(var y=0;y<maxY-1;y++)
		{
			var win = this.getWindow(x,y);
			if(!win)
			{
				this.correctY(x,y,-1);
			}
		}
	}
}
AbstractWindowControl.prototype.correctX = function(x,y,shift)
{
	for(var i=0;i<this.windows.length;i++)
	{
		var win = this.windows[i];
		if(win.xPos>=x)win.xPos+=shift;
	}
}

AbstractWindowControl.prototype.correctY = function(x,y,shift)
{
	for(var i=0;i<this.windows.length;i++)
	{
		var win = this.windows[i];
		if(win.xPos==x && win.yPos>=y)win.yPos+=shift;
	}
}

//****************************************NORMAL CLASSES************************************************************
//EljakimWindowList====================================================================================================
EljakimWindowList.prototype = new AbstractWindowControl();

function EljakimWindowList(id,remoteID,itemclass)
{
	this.id = id;
	this.remoteValueId = remoteID;
	this.classname = itemclass;
	
	this.loadData();
	this.render();
}

EljakimWindowList.prototype.render = function()
{
	var source = document.getElementById(this.id);
	source.innerHTML = "";
	for(var i=0;i<this.windows.length;i++)
	{
		var window = this.windows[i];
		var div = document.createElement("div");
		div.id = this.id + "_DragWindowItem_" + i;
		div.className = this.classname;
		source.appendChild(div);
		new EljakimDraggableWindow(div.id,0,0,window.label,window.memo,window.xformid,false);
	}
}



//EljakimWindowManager================================================================================================
EljakimWindowManager.prototype = new AbstractWindowControl();
function EljakimWindowManager(parentId,HiddenTextInputIdValue)
{
	this.parentId	= parentId;
	this.remoteValueId = HiddenTextInputIdValue;
	this.id 		= parentId + "_main";
	/*this.windows    = [ {xPos:0,yPos:0,xform:"testPagina",label:"testLabel"},
						{xPos:1,yPos:0,xform:"testPagina2",label:"testLabel2"},
						{xPos:2,yPos:0,xform:"testPagina3",label:"testLabel3"},
						{xPos:3,yPos:0,xform:"testPagina4",label:"testLabel4"},
						{xPos:4,yPos:0,xform:"testPagina4",label:"testLabel5"},
						{xPos:1,yPos:1,xform:"testPagina4",label:"testLabel6"},
						{xPos:2,yPos:1,xform:"testPagina4",label:"testLabel7"},
						{xPos:3,yPos:1,xform:"testPagina4",label:"testLabel8"},
						{xPos:4,yPos:1,xform:"testPagina5",label:"testLabel9"}
					   ];*/

	
	this.loadData();
	this.fixPositions();
	this.saveData();
	this.render();

}
EljakimWindowManager.prototype.renderOne = function()
{
	var source = document.getElementById(this.parentId);
	source.innerHTML = "";
	var dropId = this.parentId + "_droppable_" + this.getNewId();
	var dropDiv = document.createElement("div");

	dropDiv.id = dropId;
	dropDiv.className = "EljakimDropWindow";			
	source.appendChild(dropDiv);
	new EljakimDroppableWindow(dropId,0,0,this);
}


EljakimWindowManager.prototype.render = function()
{

	droppables.clear();
	
	if(this.windows.length==0)
		this.renderOne();
	
	var source = document.getElementById(this.parentId);
	source.innerHTML = "";
	var table  = document.createElement("table");
	var tBody  = document.createElement("tbody");
	var tRow   = document.createElement("tr");
	table.className = "EljakimWindowManagerTable";
	source.appendChild(table);
	table.appendChild(tBody);
	tBody.appendChild(tRow);
	var maxX = this.getMaxX();
	var uberY = this.getLargestColumn();
	
	for(var x = 0;x<maxX;x++)
	{
		var dropTD 		= document.createElement("td");
		var dropTDId = this.parentId + "_droppable_" + this.getNewId();
		dropTD.id = dropTDId;
		dropTD.className = "EljakimEmptyDropColumn";
		
		var elementTD 	= document.createElement("td");
		elementTD.className = "EljakimDropColumn";
		//elementTD.style.width = calcWidth;
		tRow.appendChild(dropTD);
		tRow.appendChild(elementTD);
		
		new EljakimDroppableWindow(dropTDId,x-0.5,-0.5,this);
		
		var maxY = this.getMaxY(x);
		var calcWidth = Math.round(100/maxX) + "%";
		for(var y=0;y<maxY;y++)
		{
			var window = this.getWindow(x,y);

			if(window)
			{
				var dropDiv = document.createElement("div");
				var dropId = this.parentId + "_droppable_" + this.getNewId();
				dropDiv.className = "EljakimEmptyDropWindow"; 
				dropDiv.id = dropId;
				elementTD.appendChild(dropDiv);
				new EljakimDroppableWindow(dropId,x,y-0.5,this);	
				
				    dropId = this.parentId + "_droppable_" + this.getNewId();
				var dragId = this.parentId + "_draggable_" + this.getNewId();
				
				dropDiv = document.createElement("div");
				var dragDiv = document.createElement("div");
				dropDiv.id = dropId;
				dragDiv.id = dragId;
				dropDiv.className = "EljakimDropWindow";
				dragDiv.className = "EljakimDragWindow";
				window.id  = dragId;
				
				elementTD.appendChild(dropDiv);
				dropDiv.appendChild(dragDiv);
				//new EljakimDroppableWindow(dropId,x,y,this); <-is niet nodig
				new EljakimDraggableWindow(window.id,x,y,window.label,window.memo,0,this); // +" x:" +x +" y:" + y,window.formid
				
			}
		}
		var dropDiv = document.createElement("div");
		var dropId = this.parentId + "_droppable_" + this.getNewId();
		dropDiv.id = dropId;
		if(y<uberY)
			dropDiv.className = "EljakimExtraDropWindow";
		else
			dropDiv.className = "EljakimEmptyDropWindow";
		elementTD.appendChild(dropDiv);
		new EljakimDroppableWindow(dropId,x,maxY-0.5,this);
	}
	var dropTD 		= document.createElement("td");
	if(this.windows.length>0)
		dropTD.className = "EljakimEmptyDropColumn";
	else
		dropTD.className = "EljakimExtraDropWindow";
	var dropTDId = this.parentId + "_droppable_" + this.getNewId();
	dropTD.id = dropTDId;
	tRow.appendChild(dropTD);
	new EljakimDroppableWindow(dropTDId,maxX-0.5,-0.5,this);
}

EljakimWindowManager.prototype.getEmptyX= function(x,y)
{
	var count = 0;
	var minX = x;
	for(var i=0;i<this.windows.length;i++)
	{
		var win = this.windows[i];
		if(win.yPos<y && y - win.xPos < minX)
		{
				minX = x - win.yPos;
		}
	}
	return minX;
}
EljakimWindowManager.prototype.getEmptyY= function(x,y)
{
	var count = 0;
	for(var i=0;i<this.windows.length;i++)
	{
		var win = this.windows[i];
		if(win.yPos<y && win.xPos==x)count++;
	}
	return count;
}


EljakimWindowManager.prototype.dropEvent = function(x,y,draggable)
{
	var window = this.getWindowById(draggable.id);

	var target = this.getWindow(x,y);
	if(target)
	{
		return;
	}
	if(!window)
	{ //er wordt een nieuw window toegevoegd
		window = {	xPos:x,
					yPos:y,
					xformid:draggable.windowObject.xformid,
					xform:"empty",
					label:draggable.windowObject.label,
					memo:draggable.windowObject.memo
				};
		this.windows[this.windows.length] = window;
	}
	window.xPos = x;
	window.yPos = y;
	this.fixPositions();
	this.saveData();
	this.render();
}


function EljakimDroppableWindow(id,x,y,parent)
{
	var object = this;
	this.id = id;
	this.x = x;
	this.y = y;
	var element = document.getElementById(this.id);
	this.backgroundColor = element.style.backgroundColor;
	element.style.background = "none";
	var drop = new Droppable(this.id);
	drop.hoverPercentage = 5;
	drop.onDropEvent  = function(draggable,droppable,dropObject){parent.dropEvent(x,y,draggable);};
	drop.onHoverEvent = function(draggable,droppable,dropObject){
																	droppables.doFunction(function(node,obj){
																							node.style.backgroundColor = "transparent";
																							});
																	droppable.style.backgroundColor = object.backgroundColor;
																	
																};
	droppables.add(drop);
}

function EljakimDraggableWindow(id,x,y,label,memo,formid,parent)
{	
	this.id = id;
	this.label = label;
	this.x = x;
	this.y = y;
	this.xformid = formid;
	this.memo = memo;
	var node = document.getElementById(this.id);
	var labeldiv = document.createElement("div");
	labeldiv.className = "EljakimWindowLabel";
	labeldiv.innerHTML = label;
	node.appendChild(labeldiv);
	var memodiv = document.createElement("div");
	memodiv.className = "EljakimWindowMemo";
	memodiv.innerHTML = memo;
	node.appendChild(memodiv);
	if(parent)
	{
		var closeImg = document.createElement("div");
		closeImg.className = "DraggableWindowCloseImg";
		node.insertBefore(closeImg,node.firstChild);
		closeImg.onclick = function(){	parent.removeWindowById(id);
									  	parent.fixPositions();
										parent.saveData();
										parent.render();
									};
	}
	var drag = new Draggable(this.id);
	node.windowObject = this;
	drag.onDragEndEvent = function(){droppables.doFunction(function(node,obj){
																			node.style.backgroundColor = "transparent";
																			});};
	drag.onNoHoverEvent = function(){droppables.doFunction(function(node,obj){
																			node.style.backgroundColor = "transparent";
																			});};
	
}


//=====================EljakimWindowOutput
EljakimWindowOutput.prototype = new AbstractWindowControl();
function EljakimWindowOutput(id,functionCall,HiddenTextInputIdXForm,HiddenTextInputIdValue,parentAjaxFrame)
{
	this.parentId = id;
	this.remoteId = HiddenTextInputIdXForm;
	this.remoteValueId = HiddenTextInputIdValue;
	this.functionCall = functionCall;
	this.parentAjaxFrame = parentAjaxFrame;
	
	
	/*this.windows    = [ {xPos:0,yPos:0,xform:"p1",label:"testLabel"},
						{xPos:1,yPos:0,xform:"p2",label:"testLabel2"},
						{xPos:2,yPos:0,xform:"p3",label:"testLabel3"},
						{xPos:3,yPos:0,xform:"p3",label:"testLabel4"},
						{xPos:4,yPos:0,xform:"p2",label:"testLabel5"},
						{xPos:1,yPos:1,xform:"p1",label:"testLabel6"},
						{xPos:2,yPos:1,xform:"p1",label:"testLabel7"},
						{xPos:3,yPos:1,xform:"p2",label:"testLabel8"},
						{xPos:4,yPos:1,xform:"p3",label:"testLabel9"}
					   ];*/
					   
	this.loadData();
	this.fixPositions();
	this.saveData();
	this.render();
	var object = this;
	setTimeout(function(){object.loadPages();},50);
}



EljakimWindowOutput.prototype.render = function()
{
	var source = document.getElementById(this.parentId);
	var table  = document.createElement("table");
	var tBody  = document.createElement("tbody");
	var tRow   = document.createElement("tr");
	table.className = "EljakimWindowManagerTable";
	source.appendChild(table);
	table.appendChild(tBody);
	tBody.appendChild(tRow);
	var maxX = this.getMaxX();
	var calcWidth = Math.round(100/maxX) + "%";
	var uberY = this.getLargestColumn();

	for(var x = 0;x<maxX;x++)
	{
		var elementTD 	= document.createElement("td");
		elementTD.className = "EljakimDropColumn";
		elementTD.id = this.parentId + "_droppable_" + this.getNewId();
		elementTD.style.width = calcWidth;
		tRow.appendChild(elementTD);
				
		var maxY = this.getMaxY(x);
		for(var y=0;y<maxY;y++)
		{
			var window = this.getWindow(x,y);
			if(window)
			{
								
				var dragId = this.parentId + "_draggable_" + this.getNewId();
				var dragDiv = document.createElement("div");
				dragDiv.id = dragId;
				dragDiv.className = "EljakimFormWindow";
				window.id  = dragId;
				
				elementTD.appendChild(dragDiv);
			}
		}
	}
}

EljakimWindowOutput.prototype.buildHref = function(href)
{
    form = document.forms.masterForm
    qindex = href.indexOf("?")
    if (qindex >= 0) {
        qstring = href.substring(qindex+1)
        separator = '?'
        if (form.action.indexOf('?') >=0) {
            separator = '&'
            }
        hrefAction = form.action + separator + qstring
    }
	return hrefAction;
}

EljakimWindowOutput.prototype.loadPages = function()
{
	var object = this;
	
	for(var x = 0;x<this.windows.length;x++)
	{
		var window = this.windows[x];
		var remoteTextInput   = document.getElementById(this.remoteId);
		if(remoteTextInput)
			remoteTextInput.value = window.xform; 
		var functionCall      = this.buildHref(this.functionCall.replace(this.parentAjaxFrame,encodeURI(window.id)));
		//maak de ajaxcall met een check of we niet van pagina verandert zijn voordat de reply van de server terug is gekomen
		var ajaxCall          = new AjaxFormPoster(function(xml){if(document.getElementById(object.parentId)){generatePage(xml);}},functionCall,document.forms.masterForm);
		ajaxCall.post(functionCall);
	}
}




//Accordion group=======================================================================================================
var AG_Accordions = new Object();
function EljakimAccordionGroup(id,opened,leftImage,downImage,closeAllOthers)  //een div die geanimeerd open en dicht klapt
{
  this.leftImage = leftImage;
  this.downImage = downImage;
  this.closeAllOthers = closeAllOthers;
  this.elementId = id;
  this.status = "";
  this.opened = opened;
  var object = this;
  AG_Accordions[this.elementId] = this;
  this.initialize();
}
EljakimAccordionGroup.prototype.initialize = function()
{ 
  var object = this;
  var node = document.getElementById(this.elementId);
  var header = document.getElementById("header_" + this.elementId);
  
  if(!node || !node.style || !header || !header.style)setTimeout(function(){object.initialize();},10);
  
  this.newheight = calculateChildrenHeights(node)+1;
  if(this.opened == "1")
  {
        node.style.height = this.newheight + "px";
		node.style.overflow = "visible";
  }
  header.onclick = function(){object.togglePanel()};
  node.toggle = function(){object.togglePanel()};
  node.close = function(){object.status = "closing";object.hidePanel()};
  node.open = function(){object.status = "opening";object.showPanel()};

}

EljakimAccordionGroup.prototype.togglePanel = function(){
  var node = document.getElementById(this.elementId);
  if (parseInt(node.style.height) == 0){
    node.style.display = "block";
	this.newheight = calculateChildrenHeights(node)+1;
	this.status = "opening";
	
	if(this.closeAllOthers)	{
		for (var id in AG_Accordions)
		{
			if(id != this.elementId && AG_Accordions[id].exists()){
				document.getElementById("img_" + id).src = this.downImage;
				AG_Accordions[id].status = "closing";
				AG_Accordions[id].hidePanel();
			}
		}
	}
	
    this.showPanel();
	document.getElementById("img_" + this.elementId).src = this.leftImage;
  }else{
    this.status = "closing";
    this.hidePanel();
	document.getElementById("img_" + this.elementId).src = this.downImage;
  }
}

EljakimAccordionGroup.prototype.hidePanel = function(){
	if(this.status!="closing")return;
	var node = document.getElementById(this.elementId);
	var height = parseInt(node.style.height);
	node.style.overflow = "hidden";
	if(height<31)
	{
		height=0;
		node.style.height=height+"px";
		node.style.display="none";
		return;
	}
	if (height > 0){
	  height -= 30;
	  node.style.height = height + "px";
	  var object = this;
	  window.setTimeout(function(){object.hidePanel()},10);
	}
}
EljakimAccordionGroup.prototype.exists = function()
{
	var node = document.getElementById(this.elementId);
	if(node)	return true;
	else 		return false;
}
EljakimAccordionGroup.prototype.showPanel = function(){
	if(this.status!="opening")return;
	var node = document.getElementById(this.elementId);
	
	node.style.display = "block";
	node.style.overflow = "hidden";
	
	var height = parseInt(node.style.height);
	if(height>=this.newheight-30)
	{
		height=this.newheight;
		node.style.height=height+"px";
		node.style.overflow = "visible";
		return;
	}
	if (height < this.newheight){
	  height += 30;
	  node.style.height = height + "px";
	  var object = this;
	  window.setTimeout(function(){object.showPanel()},10);
	}
}

//EljakimCascadingDropDown=============================================================================================
EljakimCascadingDropDown.prototype = new AbstractDropDown()  //een dropdown die zijn waarde bepaald aan de hand van een waarde van een ander element en een ActiveGrid actie

function EljakimCascadingDropDown(id,remoteId,hiddenId,dropdownId,buttonId,buttonImg,width,mouseOver,firstItemValue,firstItemText)
{
		this.id  = id;
		this.remoteNodeId = remoteId;
		this.dbTable 	= "";
		this.dbValue 	= "";
		this.dbText	= "";
		this.submitAction   = "";
		this.firstItemValue = firstItemValue;
		this.firstItemText  = firstItemText;
    
		this.remoteTextId = hiddenId;
		this.activeElement = -1;
		this.knownHeight = -1;
		this.dropdownId = dropdownId;
		this.mouseOver = mouseOver;
		this.dropDownWidth = width;
		this.dropdownIsVisible = true;
		this.dropdownElements = Array();

		var node = document.getElementById(id);
		if(!node){alert("Can't find InputNode(id="+id+")");return;}
		var remotenode = document.getElementById(remoteId);
		if(!remotenode){alert("Can't find EljakimHiddenIdTextInput(id="+remoteId+")");return;}
		var hidden = document.getElementById(hiddenId);
		if(!hidden){alert("Can't find HiddenInputNode(id="+hiddenId+")");return;}
		var dropdownDiv = document.getElementById(dropdownId);
		if(!dropdownDiv){alert("Can't find DropDownDiv(id="+dropdownId+")");return;}
		
		var object = this;
		node.onkeydown = function(e){object.handleKeyEvent(e);object.selectElementByChar(e);object.stopKey(e);};
		node.onblur = function(){object.hasFocus = false;
								setTimeout(function(){object.hideDropDown();},50);
								object.validateSelect();
								};
		node.onfocus = function(){object.hasFocus=true;};
		node.onchange = function(){object.fireChangeEvent();};
		this.keysPressed = 0;
		var button = document.getElementById(buttonId);
	
		if(buttonId)
		{
			this.hasButton = true;
			button.style.backgroundImage = buttonImg;
		}
		var dropDownDiv = document.getElementById(dropdownId);
		node.onclick = button.onclick = function(e){object.switchDropDownDiv();object.setDimensions(dropDownDiv,node);};
		button.onmousedown = function(){
										object.buttonClick = true;
										setTimeout(function(){object.buttonClick = false},80)
										;};
		this.filled = false;
		this.hasFocus = false; 
		this.dropdownIsVisible = true;
		this.knownHeight = 0;
		//this.createOptions();
}

EljakimCascadingDropDown.prototype.doInit = function ()
{
	  var node = document.getElementById(this.id);
	  var hidden = document.getElementById(this.remoteTextId);
	  if(hidden.value!="" && hidden.value!="0")
		  {		
				var element = this.selectDropDownElement(hidden.value);
				if(element)
				{
					node.value = element.text;
				}
		  }else{
				node.value = this.firstItemText;
				hidden.value = this.firstItemValue;
		  }
		  node.title = node.value;
  this.setRemoteEvent();
		this.showDropDown();
		this.hideDropDown();
}

EljakimCascadingDropDown.prototype.setRemoteEvent = function()
{
   var object = this;
   var remoteNode = document.getElementById(this.remoteNodeId);
   try{ remoteNode.attachEvent('onchange',function(){object.doEljakimCascadingPost();} );}catch(e){}
   try{ remoteNode.addEventListener("change",function(){object.doEljakimCascadingPost();}, false);}catch(e){}
   setTimeout(function(){object.doEljakimCascadingPost()},100);

}

EljakimCascadingDropDown.prototype.doEljakimCascadingPost = function(){
	var remoteNode = document.getElementById(this.remoteNodeId);
	if(remoteNode.value==0||remoteNode.value=="")return;
	showLoadingDiv();
	var object = this;
	var ajaxCall = new AjaxFormPoster(function(xml){object.createEljakimCascadingOptions(xml);},this.submitAction,document.forms.masterForm);
	ajaxCall.post();
}

EljakimCascadingDropDown.prototype.createOptions = function()
{
  var dropdown = document.getElementById(this.dropdownId);
  var node     = document.getElementById(this.id);
  this.activeElement = -1;
  var items = dropdown.innerHTML.split(";++;");
  dropdown.innerHTML = "";
  this.clear();
  for(var i=0;i<items.length-1;i++)
  {
	 var item = items[i].split(";==;");
     this.addElement(item[0],item[1]);
  }
  this.first = true;
  this.showDropDown();
  this.hideDropDown();
  
}

EljakimCascadingDropDown.prototype.createEljakimCascadingOptions = function(xmlDoc)
{
  var target = document.getElementById(this.id);
  var valueText  = document.getElementById(this.remoteTextId);
  var value = valueText.value;
  
  this.onRoute = false;
  this.activeElement = -1;
  var optionNodes = xmlDoc.selectNodes(this.dbTable);
  this.clear();
  if(this.firstItemText!="")
  {
	this.addElement(this.firstItemValue,this.firstItemText); 
  }
  for(var i=0;i<optionNodes.length;i++)
  {
	 try{
     this.addElement(Sarissa.getText(optionNodes[i].selectSingleNode(this.dbValue)),
	 			     Sarissa.getText(optionNodes[i].selectSingleNode(this.dbText)));
	}catch(e){alert("Wrong XPath(s) specified in CascadingDropDown");}
  }

  if(value!="")
  {
		var element = this.selectDropDownElement(value);
		if(element)
		{
			valueText.value = element.value;
			target.value = element.text;
		}
  }
  this.first = true;
  this.showDropDown();
  if(this.dropdownElements.length<=0)
	this.hideDropDown();

}

//EljakimCascadingTextField==================================================================================================
function EljakimCascadingTextField(id,remoteId) //een textfield (input) dat zijn waarde bepaald aan de hand van een ander element en een activeGrid Action
{
  this.nodeId  = id;
  this.remoteNodeId = remoteId;
  this.dbTable 	= "";
  this.dbText	= "";
  this.submitAction   = "";
  this.overwrite = false;
  var object = this;
  setTimeout(function(){object.setRemoteEvent();},50);
}

EljakimCascadingTextField.prototype.setRemoteEvent = function()
{
   var object = this;
   var remoteNode = document.getElementById(this.remoteNodeId);
   if(!remoteNode)
   {
		alert("Error: Cascading textfield was unable to find remote element(textfield, dropdown etc) with id="+this.remoteNodeId);
		return;
   }
   addEvent(remoteNode,"change",function(){setTimeout(function(){object.doEljakimCascadingPost();},100)});
   setTimeout(function(){object.doEljakimCascadingPost()},100);
}

EljakimCascadingTextField.prototype.doEljakimCascadingPost = function()
{
	var remoteNode = document.getElementById(this.remoteNodeId);
	var node = document.getElementById(this.nodeId);
	if(remoteNode.value==0||remoteNode.value=="")return;
	if(!this.overwrite && node.value!="" && node.value!=0)return; //als er al een waarde is ingevuld houden we die en passen we niets meer aan
	node.disabled = true;
	showLoadingDiv();
	var object = this;
	var ajaxCall = new AjaxFormPoster(function(xml){object.createEljakimCascadingOptions(xml);},this.submitAction,document.forms.masterForm);
	ajaxCall.post();
}

EljakimCascadingTextField.prototype.createEljakimCascadingOptions = function(xmlDoc)
{
	var node = document.getElementById(this.nodeId);
	node.disabled = false;
	var last = node.value;
	if(!this.overwrite && last!="" && last!=0)return;
	var optionNodes = xmlDoc.selectNodes(this.dbTable);
	var value = last;
	try{
			value = Sarissa.getText(optionNodes[0].selectSingleNode(this.dbText));
		}catch(e){
			value = last;
		}
	node.value = value;
	if(value != last)
	{
		fireEvent(node,"change");
	}
}



//EljakimHorizontalLayoutGroup=========================================================================================
function makeMyTable(node,left,right)	//past de breedtes van een tabel aan met 2 kolommen
{
  var found = 0;
  var childnode = node.firstChild.firstChild;
  for(var i = 0;i<childnode.childNodes.length;i++)
  {
	var mynode = childnode.childNodes[i];
	if(typeof mynode.width != "undefined"){
		if(found==0)
		{
			//mynode.width=left;
			mynode.style.width = left;
		}
		else
		{
			//mynode.width = right;
			mynode.style.width = right;
		}
		mynode.align = "left";
		mynode.style.textAlign = "left";
		found++;
		//mynode.style.align = left;
	}
  }
}
//EljakimImageAlertButton==============================================================================================

function doImageAlertButtonSubmit(element)
{
	var bool = confirm(element.question);
	if(bool)
	{
		element.action();
	}
}


//EljakimImageTableLink================================================================================================
function EljakimImageTableLink(id, hoverClass, omitColumnsRigth,omitColumnsLeft,doClosePopup,javascript,hovereffect) //geeft het de hele table row dezelfde onclick action als dit element en regelt de onhover van de table row
{
  this.id = id;
  var node = document.getElementById(this.id);
 
  var parent = node;
  while(true)
  {
		if(parent.nodeName && parent.nodeName.toLowerCase() == "td"){
			break;
		}else if(parent == document.body){
			return;
		}else{
			parent = parent.parentNode;
		}
  }
  var grandParent = parent.parentNode;
  this.omitColumnsLeft  = parseInt(omitColumnsLeft);
  this.omitColumnsRigth = parseInt(omitColumnsRigth);
  this.javascript = javascript;
  this.doClosePopup = doClosePopup;
  if(hovereffect == "true")
  {
	if(grandParent.onmouseout)grandParent.onmouseout();
	var myclass = grandParent.className;
	grandParent.onmouseover = function(){this.className = hoverClass;};
	grandParent.onmouseout = function() {this.className = myclass;};
  }
  var object = this;
  setTimeout(function(){object.makeRow(parent);},1);
  //this.makeRow(parent);
}

EljakimImageTableLink.prototype.makeRow = function(parent)
{
  //var node = document.getElementById(this.id);
  //var parent = node.parentNode;
  var grandParent = parent.parentNode;
  var parentlist = new Array();
  var parents = grandParent.childNodes;
   var object = this;
  for(var i = 0;i<parents.length;i++)
  {
  	if(parents[i].nodeName.toLowerCase()=="td")
   	{
    		parentlist[parentlist.length] = parents[i];
    }
  }
  for(var i = this.omitColumnsLeft;i<parentlist.length-this.omitColumnsRigth;i++)
  {
	if(this.doClosePopup=="true"||this.doClosePopup=="1")
	{
		parentlist[i].onclick = function() {closePopup();eval(object.javascript);return anchorSubmitForm(document.getElementById(object.id));};
	}
	else
	{
    	parentlist[i].onclick = function() {eval(object.javascript);return anchorSubmitForm(document.getElementById(object.id));};
	}
  }
}


//EljakimTodayDateInput================================================================================================
function EljakimTodayDateInput(sourceId,targetId,name,value,format,internalFormat,predicate,errorDiv,error,hiddenError,showButton)
{
	var object = this;
    this.inputId 			 = targetId;
	this.inputIdHidden		 = targetId + "_HIDDEN";
    this.format   			 = format;
    this.internalFormat 	 = internalFormat;
    this.predicate      	 = predicate;
    this.errorDiv    		 = errorDiv;
    this.error       		 = error;
    this.hiddenError 		 = hiddenError;
    this.sourceId 			 = sourceId;
    var object 				 = this;
	//alert(value);
	this.date = '';
	//from parsers.js:
	this.dateParser 		= new DateParser(this.format);
	this.dateRendererIntern	= new DateRenderer(this.internalFormat);
	this.dateRendererExtern	= new DateRenderer(this.format);

	var dateInput 			= document.createElement("input");
	dateInput.className = "cdatetime";
	var datehiddenInput 	= document.createElement("input");
	datehiddenInput.type   	= "hidden";
	datehiddenInput.name 	= name;
	dateInput.id 			= this.inputId;
	datehiddenInput.id 		= this.inputIdHidden;
	dateInput.getValueByDate = function(){object.date};
	dateInput.setValueByDate = function(date){object.date=date;object.render();};
	dateInput.clearValue = function(){object.clear();};
	addEvent(dateInput,"change",function(){object.parse();});
	addEvent(dateInput,"keypress",function(e){
													if(!e)e=window.event;
													if(e.keyCode==13) //enter
													{
														object.parse();
														
														setTimeout(function(){ //vind de dichstbijzijnde button en click
																var el = _findClosestSubmit(dateInput, null);
																el.onclick();
														 },10);
														return;
													}
													object.pressKey(e);
												});
									
	addEvent(dateInput,"blur",function(){	
											setTimeout(
														function(){object.checkDatePredicate();}
														,10);
										});
	

	var source               = document.getElementById(this.sourceId);
	source.appendChild(dateInput);
	source.appendChild(datehiddenInput);

	if(value)
	{
		var valueParser = new DateParser(this.internalFormat);
		this.date = valueParser.parse(value);
		this.render();
	}
	
	if(showButton=='0')return;
	button = document.createElement("input");
	button.type = "button";
	button.value = "Nu";
	button.className = "agLinkButton";
	button.id = this.inputId+"_button";
	button.onmouseup = function(){object.setTodayDate();fireEvent(dateInput,"change");};
	button.onkeydown = function(e){if(!e)e=window.event; if(e.keyCode==13){object.setTodayDate();fireEvent(dateInput,"change");}};
	var space = document.createElement("span");
	space.innerHTML = "&nbsp;";
	source.appendChild(space);
	source.appendChild(button);

}

EljakimTodayDateInput.prototype.parse = function()
{
	var dateInput 		= document.getElementById(this.inputId);
	var datehiddenInput = document.getElementById(this.inputIdHidden);
	if(dateInput.value == ""){
		this.clear();
		return;
	}
	this.date 				= this.dateParser.parse(dateInput.value);
	this.render();
}

EljakimTodayDateInput.prototype.clear = function()
{
		var dateInput 		= document.getElementById(this.inputId);
		var datehiddenInput = document.getElementById(this.inputIdHidden);
		dateInput.value 	 	= "";
		datehiddenInput.value 	= "";
		this.date = '';
}

EljakimTodayDateInput.prototype.render = function(){
		var dateInput 		= document.getElementById(this.inputId);
		var datehiddenInput = document.getElementById(this.inputIdHidden);
		dateInput.value 	 	= this.dateRendererExtern.render(this.date);
		datehiddenInput.value 	= this.dateRendererIntern.render(this.date);
}

EljakimTodayDateInput.prototype.pressKey = function(e)
{
	var key = e.keyCode? e.keyCode : e.charCode
	key = String.fromCharCode(key);
	//only digits and special chars...
	if(key.match(new RegExp('\[A-z]+')))
		stopEvent(e);
}

EljakimTodayDateInput.prototype.setTodayDate = function()
{
	this.date = new Date();
	this.render();
}

EljakimTodayDateInput.prototype.checkDatePredicate = function()
{
	//Example predicates: >=11-03-1985 , !=today , ==@RemoteElementId

	var element = document.getElementById(this.inputId);
	var predicate="";
	try{
		if(this.predicate==""||element.value=="")return;
	    predicate = this.predicate.replace(" ","");
	}catch(e){return;}
	predicate = predicate.replace("&lt;","<");
	predicate = predicate.replace("&gt;",">");
	var comparator;
	var value;

	//splitting value/comparator
	var coms = ["<",">","=","!"];
	var i;
	for(i=0;i<predicate.length;i++)
	{
		if(findElement(coms,predicate.charAt(i))<0)
			break;
	}
	comparator = predicate.substring(0,i);
	value     = predicate.substring(i,predicate.length);

	//getting value (the date)

	var date;
	var parser;
	if(value.toLowerCase() == "today") //date is ' today'
	{
		date = new Date();
	}else if(value.substr(0,1)== "@"){   //getting date value from remote inpute element
		var remoteElement = document.getElementById(value.substring(1,value.length));
		if(!remoteElement||remoteElement.value=="")return;
		date = this.dateParser.parse(remoteElement.value);
	}else{                              //getting date value from predicate and parse it
		date = this.dateParser.parse(value);
	}
	//building errormsg
	var error = document.getElementById(this.errorDiv);
	error.style.width = element.style.width;
	var object = this;
	error.onclick     = function(){this.className = object.hiddenError;};
	error.style.top   = getRealTopPos(element) + getElementHeight(element) + "px";
	error.style.left  = getRealLeftPos(element)-2 + "px" ;
	//parsing date
	var localdate = this.dateParser.parse(element.value);
	//alert("local:"+localdate +" remote:" + date)
	//show error message when predicate fails
	if(!eval(localdate.getTime() + comparator + date.getTime()))
		error.className = this.error;
	else
		error.className = this.hiddenError;
}
//EljakimCustomDropDown========================================================================================================
EljakimCustomDropDown.prototype = new AbstractDropDown();

function EljakimCustomDropDown(id,hiddenId,dropdownId,buttonId,buttonImage,selectedClass,width,enterSubmit,javascriptEval) //een remake van de standaard SELECT zodat ie m wel goed rendert in IE...
{
		var node = document.getElementById(id);
		var hidden = document.getElementById(hiddenId);
		var object = this;
		this.remoteTextId = hiddenId;
		this.id = id;
		this.dropdownId = dropdownId;
		this.activeElement = -1;
		this.dropdownElements = Array();
		node.onkeydown = function(e){if(!e)e=window.event;object.handleKeyEvent(e);object.selectElementByChar(e);object.stopKey(e);};
		node.onblur = function(){object.hasFocus = false;
								 setTimeout(function(){object.hideDropDown();},50);
								 object.validateSelect();
								 };
		node.onfocus = function(){object.hasFocus=true;};
		node.onchange = function(){object.fireChangeEvent();};
		var remoteNode = document.getElementById(this.remoteTextId);
		remoteNode.setValue = node.setValue = function(input){object.setValue(input);};
		this.mouseOver    = selectedClass;
		this.keysPressed = 0;
		this.dropDownWidth = width;
		if(enterSubmit=="1")
			this.enterSubmit=true;
		else
			this.enterSubmit=false;	
		this.JSonclick = javascriptEval;
		
		var button = document.getElementById(buttonId);
		if(buttonId)
		{
			this.hasButton = true;
			button.style.backgroundImage=buttonImage;
		}
		var dropDownDiv = document.getElementById(dropdownId);
		button.onclick = node.onclick = function(e){
														object.switchDropDownDiv();
														object.setDimensions(dropDownDiv,node);
														//this.style.backgroundColor = "rgb("+Math.round(Math.random()*255)+","+Math.round(Math.random()*255)+","+Math.round(Math.random()*255)+")";
													};
		button.onmousedown = function(){
										object.buttonClick = true;
										setTimeout(function(){object.buttonClick = false},80)
										;};
		this.filled = false;
		//this.first = true;
		//this.handleButtonRequest();
		this.hasFocus = false; 
		this.dropdownIsVisible = true;
		this.knownHeight = 0;
}

EljakimCustomDropDown.prototype.doInit = function()
{
		var node = document.getElementById(this.id);
		var hidden = document.getElementById(this.remoteTextId);
		var object = this;
		var element = this.selectDropDownElement(hidden.value);
		if(element!=null)
		{
			node.value = element.text;
		}
		else if(this.dropdownElements.length>0)
		{
			node.value = this.dropdownElements[0].text;
			hidden.value = this.dropdownElements[0].value;
			this.activeElement = 0;
		}
		node.title = node.value;
		this.showDropDown();
		
		this.hideDropDown();
}




//=======================================================================================COUPLED HINTING SELECT
CoupledHintingSelect.prototype = new AbstractDropDown();
//Een dropdown die filtert op zijn waardes doormiddel van een activegrid action
function CoupledHintingSelect(id,hiddenId,remoteId,dropdownId,submission,xpath,xpathText,xpathValue,width,firstText,firstValue,selectedClass,buttonId,buttonImage,floatDropDown,alwaysVisible,enterSubmit,javascriptEval)
{
		var node = document.getElementById(id);
		if(!node){alert("Cannot find TextInput!(id="+id+")");return;}
		var object = this;
		this.hasTyped = false;
		if(floatDropDown=="1")
			this.floatDropDown=true;
		else
			this.floatDropDown=false;
		if(alwaysVisible=="1")
			this.alwaysVisible=true;
		else
			this.alwaysVisible=false;	
		if(enterSubmit=="1")
			this.enterSubmit=true;
		else
			this.enterSubmit=false;	
			
		this.JSonclick = javascriptEval;
		this.id = id;
		this.dropdownId = dropdownId;
		var dropdown = document.getElementById(this.dropdownId);
		this.submission = submission;
		this.activeElement = -1;
		this.dropdownElements = Array();
		
		addEvent(node,"paste",function(e){setTimeout(function(){object.doCoupledHintPost(false);},10);});
		addEvent(node,"keyup",function(e){object.handleHintRequest(e,false);});
		addEvent(node,"keydown",function(e){object.hasTyped=true;object.handleKeyEvent(e);});
		addEvent(node,"blur",function(){object.hasFocus = false;
										object.hasTyped = false;
										setTimeout(function(){object.hideDropDown();},50);
										object.validateSelect();
										}	);
		addEvent(node,"focus",function(){object.hasFocus=true;});

		this.ajaxCall = function(http){object.onAjaxCallBack(http);};
		this.xpath = xpath;
		this.xpathValue = xpathValue;
		this.xpathItem = xpathText;
		this.remoteTextId = hiddenId;
		var remote = document.getElementById(hiddenId);
		if(remote.value==""){remote.value = firstValue;}
		this.mouseOver    = selectedClass;
		this.keysPressed = 0;
		this.dropDownWidth = width;			
		this.firstItemText  = firstText;
		this.firstItemValue = firstValue;
		var tempNode = document.getElementById(remoteId);
		if(!tempNode)alert("Cannot find EljakimHiddenIdTextInput with id: " + remoteId);
		node.name = tempNode.name;
		tempNode.name="";
		this.filled = false;
		node.ondblclick = function(e){object.handleButtonRequest();};
		node.onclick = function(){this.select();};
		node.onchange = function(){object.fireChangeEvent();};
		
		remote.setValue = node.setValue = function(input){object.setValue(input);};
		
		var button = document.getElementById(buttonId);
		if(button)
		{
			button.style.backgroundImage=buttonImage;
			this.hasButton = true;
			button.onclick = function(e){   node.focus();
										object.hasFocus=true;
										if((node.value=="" || node.value == object.firstItemText) && object.filled ==false)
										{
											object.handleButtonRequest();
											object.filled = true;
										}else if(object.filled ==false){
											object.handleHintRequest(e,false);			
										}else{
											object.switchDropDownDiv();
											object.setDimensions(dropdown,node);
										}
									};
			button.onmousedown = function(){
										object.buttonClick = true;
										setTimeout(function(){object.buttonClick = false},80)
										;};
		}
		this.onRoute = false;
		this.hasFocus = false; 
		this.dropdownIsVisible = true;
		this.knownHeight = 0;
		node.title = node.value;

}

CoupledHintingSelect.prototype.doInit = function()
{
	//setting the first value:
	var remote = document.getElementById(this.remoteTextId);
	var node   = document.getElementById(this.id);
	var item   = null;
	var dropdown = document.getElementById(this.dropdownId);
	//is er al een waarde ingevuld die geselecteerd moet worden?
	if(remote.value!="" && remote.value!="0")
	{
		var item = this.selectDropDownElement(remote.value);
	}else if(this.firstItemValue == "" && this.dropdownElements.length>0){ //zoniet kunnen we de eerste waarde selecteren?
		var item = this.dropdownElements[0];
	}
	if(item)
	{
			remote.value = item.value;
			node.value   = item.text; 
	}else{
			remote.value = this.firstItemValue;
			node.value   = this.firstItemText; 
	}
	if(this.alwaysVisible)
	{
			this.showDropDown();
	}else{
		this.showDropDown();
		this.setDimensions(dropdown,node);
		this.hideDropDown();
	}
	
	node.title = node.value;
}

CoupledHintingSelect.prototype.validateSelect = function()    //valideer de invoer
{
	var textInput   = document.getElementById(this.id);
	var valueInput   = document.getElementById(this.remoteTextId);
	if(textInput.value=="")
	{
	   valueInput.value = this.firstItemValue;
	   textInput.value  = this.firstItemText;
	   return;
	}
	if(this.hasFocus||this.onRoute)return;
	
	if(this.dropdownElements.length<1)
	{
		textInput.value = this.firstItemText;
		valueInput.value= this.firstItemValue;
	}
	else if(this.dropdownElements.length>=1)
	{
		var lastvalue = valueInput.value;
		if(this.activeElement<=this.dropdownElements.length&&this.activeElement>=0)
		{
			textInput.value  = this.dropdownElements[this.activeElement].text;
			valueInput.value = this.dropdownElements[this.activeElement].value;
		}
		else
		{
			textInput.value  = this.dropdownElements[0].text;
			valueInput.value = this.dropdownElements[0].value;
		}
		if(lastvalue != valueInput.value)
			this.fireChangeEvent();
	}
    textInput.title = textInput.value;
}

CoupledHintingSelect.prototype.handleHintRequest = function(e)     //als er getypt wordt, wannneer doen we een request?
{
	var element = document.getElementById(this.id);
	var clear = false;
	if(element.value==""){this.hideDropDown();return;}
	if(!e)e=window.event;
	if(e.keyCode==38||e.keyCode==40||e.keyCode==13||e.keyCode==37||e.keyCode==39)return;
	this.lastValue = element.value;
	var object = this;
	setTimeout(function(){object.doCoupledHintPost()},500);
	this.keysPressed+=1;
	this.onRoute = true;
}

CoupledHintingSelect.prototype.handleButtonRequest = function()  //button request
{
	var element = document.getElementById(this.id);
	var lastColor = element.style.color;
	//element.style.color = "#FFFFFF";
    try{element.focus();}catch(e){}
	this.onRoute = true;
	this.keysPressed+=1;
	this.lastValue = element.value;
	var clear= true;
	element.value="%";
	this.doCoupledHintPost(clear);
	element.style.color = lastColor;
}

CoupledHintingSelect.prototype.doCoupledHintPost = function(clear){    //de post/request
		var element = document.getElementById(this.id);
		this.keysPressed-=1;
		if(this.keysPressed>0)return;
		this.keysPressed=0;
		if(element.value==""){element.value = "%";clear = true;}
		var object = this;
		element.value = element.value.replace("'","\\'");
		var ajaxCall = new AjaxFormPoster(function(xml){object.createHintOptions(xml);},this.submission,document.forms.masterForm);
		ajaxCall.post();
		element.value = element.value.replace("\\'","'");
		if(clear)
		{
			element.value=this.lastValue;
			element.style.color = this.lastColor;
		}
		 if(!this.hasTyped && this.hasFocus && element)
		 {
			element.select();
		 }
	}

CoupledHintingSelect.prototype.createHintOptions = function(xmlDoc){      //de interne lijst renderen
  var node = document.getElementById(this.id);
  this.onRoute = false;
  this.activeElement = -1;
  var optionNodes = xmlDoc.selectNodes(this.xpath);
  this.clear();
	//alert(target.xpath);

  for(var i=0;i<optionNodes.length;i++)
  {
	try{
					var value = Sarissa.getText(optionNodes[i].selectSingleNode(this.xpathValue));
					var text  = Sarissa.getText(optionNodes[i].selectSingleNode(this.xpathItem));
	}catch(e){alert("Wrong XPath(s) specified in CoupledHintingSelect");}
	this.addElement(value,text);
  }
  if(this.firstItemText!="")
  {
	this.addElement(this.firstItemValue,this.firstItemText); 
  }
  this.first = true;
  this.showDropDown();
  if(this.dropdownElements.length<=0)
	this.hideDropDown();
  if(!this.hasTyped && this.hasFocus && node)
  {
	node.select();
  }
}



//=================================================
function capitalizeAfterBlur(id,length)
{
			length = parseInt(length);
			if(!length)length = 1;
			var input = document.getElementById(id);
			input.capLength = length;
			var onblurfunc = function(){
											var len = input.capLength;
											if(len>input.value.length)
												len = input.value.length;
											var value = input.value.substr(len,input.value.length-1);
											var cap = input.value.substr(0,len);
											input.value = cap.toUpperCase() + value;
											
										}
			addEvent(input,"change",onblurfunc);
			onblurfunc();
}
//===================EljakimPopupDiv============================
var popupList = new Array();
function EljakimPopupGroup(id,defaultImage,hoverImage,selectedImage,showCloseButton,javascriptOnOpen)
{
	var object = this;

	this.divId = id + "_popup";
	this.imgId = id + "_img";
	this.butId = id + "_but";
	this.defaultImage = defaultImage;
	if(hoverImage)
		this.hoverImage = hoverImage;
	else
		this.hoverImage = defaultImage;
	if(this.selectedImage)	
		this.selectedImage = selectedImage;
	else
		this.selectedImage = defaultImage;
	object.button=true;
	var cacheimg = document.createElement("img");
	cacheimg.src = this.hoverImage;
	cacheimg.src = this.selectedImage;	
	
	var div = document.getElementById(this.divId);
	var img = document.getElementById(this.imgId);
	var but = document.getElementById(this.butId);
	
	img.onmouseover = "this.src='"+this.hoverImage+"'";
	img.onmouseout  = "this.src='"+this.defaultImage+"'"; 
	div.onmouseup = function(e){stopEvent(e);};
	but.onclick = img.onclick = function(){object.switchDiv();object.button=true;};
    if(showCloseButton!="1")but.style.display="none";
	var func = function(){setTimeout(function(){if(!object.button)object.documentClick();object.button=false;},1);};
	//removeEvent(document.body,"click",func);

	try{
		removeEvent(document.body,"click",popupList[id]);
	}catch(e){}
	popupList[id] = func;
	addEvent(document.body,"click",func);

	this.hasMouse = false;
	this.JSonOpen = javascriptOnOpen;
	
}
EljakimPopupGroup.prototype.switchDiv = function()
{
	var object = this;
	var div = document.getElementById(this.divId);
	var img = document.getElementById(this.imgId);
	if(!div)return;
	if(div.style.visibility!="hidden")
		this.hide();
	else
		this.show();
}
EljakimPopupGroup.prototype.show = function()
{
	var object = this;
	var div = document.getElementById(this.divId);
	var img = document.getElementById(this.imgId);
	if(!div)return;
	div.style.visibility = "visible";
	div.style.position = "absolute";
	div.style.left = getRealLeftPos(img) + "px";
	div.style.top = getRealTopPos(img) + getElementHeight(img) + 1 + "px";
	div.style.zIndex = 999999;
	div.onmouseover = function(){object.mouseIn();};
	div.onmouseout = function(){object.mouseOut();};
	img.src = this.selectedImage; 
	eval(this.JSonOpen);
}
EljakimPopupGroup.prototype.hide = function()
{
	var div = document.getElementById(this.divId);
	var img = document.getElementById(this.imgId);
	if(!div)return;
	div.style.position = "absolute";
	div.style.visibility = "hidden";
	div.style.left = -3000 + "px";
	div.style.top  = -3000 + "px";
	img.src = this.defaultImage;
	img.onmouseover = "this.src='"+this.hoverImage+"'; this.style.backgroundColor='"+this.highlightColor+"';";
	img.onmouseout  = "this.src='"+this.defaultImage+"'; this.style.backgroundColor='';"; 
}
EljakimPopupGroup.prototype.mouseIn = function()
{	
	this.hasMouse = true;
}
EljakimPopupGroup.prototype.mouseOut = function()
{	
	this.hasMouse = false;
}
EljakimPopupGroup.prototype.documentClick = function()
{
	var object = this;

	if(!this.hasMouse)
		object.hide();
}

function hidePopup(element)
{		
			element.style.position = "absolute";
			element.style.visibility = "hidden";
			element.style.left = -3000 + "px";
			element.style.top  = -3000 + "px";
}
//===================================================EljakimTabLink

function EljakimTabLink(id,imgId,imgURL1,imgURL2,Class1,Class2,Class3,submitRef,isSelected,clearAll)
{
	
	this.id = id
	this.imgId        = imgId;
	this.defaultImgURL   = imgURL1;
	this.selectedImgURL	 = imgURL2;
	var PRELOADimg = new Image();
	PRELOADimg.src = imgURL2;
	PRELOADimg="";
	this.defaultClass     	= Class1;
	this.hoverClass      	= Class2;
	this.selectedClass    	= Class3;
	this.clearAll = clearAll;
	this.submit = submitRef;
	var row = document.getElementById(this.id);
	var object = this;
	row.onclick		= function(){object.clearTabBar();object.selectItem();hrefSubmitForm(object.submit);return false;};
	row.onmouseover	= function(){object.hover();};
	row.onmouseout	= function(){object.hover();};
	row.tabLink     = this;
	if(isSelected != "False"){object.clearTabBar();object.selectItem();}
}


EljakimTabLink.prototype.hover = function()
{
	var menuItem = document.getElementById(this.id);
	if(menuItem.className != this.selectedClass){
		if (menuItem.className == this.defaultClass) 
			menuItem.className = this.hoverClass;
		else
			menuItem.className = this.defaultClass;
		return;
	}
}

EljakimTabLink.prototype.selectItem = function()
{	
	var menuItem = document.getElementById(this.id);
	this.selected = true;
	menuItem.className = this.selectedClass;
	var img = document.getElementById(this.imgId);
	img.src = this.selectedImgURL;
}

EljakimTabLink.prototype.clearTabBar = function()
{
  var menuItem = document.getElementById(this.id);
  var nodes; 
  if(this.clearAll)
	nodes = document.getElementsByTagName("tr");
  else
	nodes = menuItem.parentNode.childNodes;
  for (i=0; i<nodes.length; i++){
	var item = nodes[i];
	if(item.tabLink)
	{
		var tab = item.tabLink;
		tab.selected = false;
		item.className = tab.defaultClass;
		var img = document.getElementById(tab.imgId);
		img.src = tab.defaultImgURL;
	}
  }
}

function clickSelectedTabs()
{
	var nodes = document.getElementsByTagName('tr');
	for (i=0; i<nodes.length; i++)
	{
		var item = nodes[i];
		if(item.tabLink&&item.tabLink.selected)
		{
			item.onclick();
		}
	}
}
//===================EljakimTextArea================================================
function EljakimTextArea(id,outputMode,charlimit,style)
{
	this.id = id;
	this.dragId = id+"_dragger";
	this.mousedown = false;
	this.charLimit = charlimit;
	if(outputMode=="true")
	{
		this.renderOutput(style);
	}else{
		this.render();
	}
}
EljakimTextArea.prototype.renderOutput = function(style)
{
	var node =  document.getElementById(this.id);
	var newNode = document.createElement('div');
	newNode.className = "EljakimTextAreaOutputDiv"; 
	newNode.setAttribute("style",style);
	var string = node.innerHTML;
	string = string.replace(/\n/g,'<br/>');
	newNode.innerHTML = string
	node.parentNode.replaceChild(newNode,node);
}
EljakimTextArea.prototype.render = function()
{
	var object = this;
	var node =  document.getElementById(this.id);
	var cookieHeight = readCookie("TEXTAREA_"+this.id);
	if(cookieHeight)
		node.style.height = cookieHeight;
	var parent = node.parentNode;
	var dragDiv = document.createElement("div");
	dragDiv.id = this.dragId;
	dragDiv.className = "EljakimTextAreaDragDiv";
	var newWidth = getElementWidth(node);
	if(newWidth)
		dragDiv.style.width = newWidth +"px";
	//dragDiv.innerHTML = "DRAG ME!!";
	parent.appendChild(dragDiv);
	dragDiv.onmousedown = function(){object.doMouseDown();};
	var body = document.body;
	addEvent(body,"mousemove",function(e){object.resize(e);});
	addEvent(body,"mouseup",function(){object.doMouseUp();});
	addEvent(body,"keyup",function(){object.doKeyUp();});
}
EljakimTextArea.prototype.resize = function(e)
{
	if(!this.mousedown)return;
	if(!e)e = window.event;
	var mouse = getMouseXY(e);
	var node =  document.getElementById(this.id);
	if(!node)return;
	var dragDiv = document.getElementById(this.dragId);
	var objectTop = getRealTopPos(node);
	var newHeight = mouse.y - objectTop - getElementHeight(dragDiv)/2;
	if(newHeight<20)newHeight = 20;
	node.style.height = newHeight + "px";
	deselect();
}
EljakimTextArea.prototype.doMouseDown = function()
{
	this.mousedown = true;
	var node =  document.getElementById(this.id);
	if(!node)return;
	var dragDiv = document.getElementById(this.dragId);
	dragDiv.className = "EljakimTextAreaDragDivResize";
	node.className = "EljakimTextAreaResize";
}
EljakimTextArea.prototype.doMouseUp = function()
{
	this.mousedown = false;
	var node =  document.getElementById(this.id);
	if(!node)return;
	var dragDiv = document.getElementById(this.dragId);
	dragDiv.className = "EljakimTextAreaDragDiv";
	node.className = "EljakimTextArea";
	createCookie("TEXTAREA_"+this.id,node.style.height,365);
}
EljakimTextArea.prototype.doKeyUp = function()
{
	var node =  document.getElementById(this.id);
	if (!node) return;
	if (this.charLimit != 0)
	{
		node.value = node.value.substring(0, this.charLimit);
	}
}
//=============EljakimLink=============================================================
function EljakimLink(id,action,agLinkId,doAction,JSonclick,JSonmouseover,JSonmouseout,JSonmousedown)
{
	var link = document.getElementById(id);
	if(agLinkId != "")
	{
		link.id = agLinkId;
	}
	if(doAction!="1")
	{
		link.onclick = function(){eval(JSonclick);};
	}else{
		var activegridAction = "hrefSubmitForm('<%= item.getRelativeHref() %>');windowYScroll('<%= item.getScrollTo() %>');";
		link.onclick = function(){eval(JSonclick + action);};
	}
	link.onmouseover = function(){eval(JSonmouseover);};
	link.onmouseout  = function(){eval(JSonmouseout);};
	link.onmousedown = function(){eval(JSonmousedown);};
}
//===================EljakimPriorityButton===============================================
function EljakimPriorityButton(idDiv,idHidden,editable,options)
{
	var object = this;
	this.id = idDiv;
	this.valueId = idHidden;
	
	this.options = options
	/*this.options = [
						{value:0,defaultImg:defaultImg,selectedImg:greenImg,label:"low"},
						{value:1,defaultImg:defaultImg,selectedImg:yellowImg,label:"middle"},
						{value:2,defaultImg:defaultImg,selectedImg:orangeImg,label:"high"},
						{value:3,defaultImg:defaultImg,selectedImg:redImg,label:"critical"}
					];*/
	var source = document.getElementById(this.id);
	var hidden   = document.getElementById(this.valueId);
	
	if(editable != "true")
	{
		var item = this.get(hidden.value);
		var img = document.createElement("img");
		if(item)
		{
			img.title  = item.label;
			img.src    = item.selectedImg;
		}else
			img.src    = this.options[0].defaultImg;
		source.appendChild(img);
		return;
	}
	
	var table = document.createElement("table");
	table.style.borderCollapse= "collapse"; 
	
	var tbody = document.createElement("tbody");
	var row   = document.createElement("tr");
	
	table.appendChild(tbody);
	tbody.appendChild(row);
	for(var i=0;i<this.options.length;i++)
	{
			var item = this.options[i];
			var preloadImg = new Image();
			preloadImg.src = item.defaultImg;
			preloadImg.src = item.selectedImg;
			
			var td  = document.createElement("td");
			var img = document.createElement("img");
			img.src = item.defaultImg;
			img.id  = this.id + "_option_" + item.value;
			img.style.cursor = "pointer";
			img.object = this;
			img.title  = item.label;
			img.onclick = new Function("this.object.set("+item.value+");");
			td.appendChild(img);
			row.appendChild(td);
	}
	
	source.appendChild(table);
	
	if(hidden.value != "")this.set(hidden.value);
	
}

EljakimPriorityButton.prototype.clear = function()
{
	for(var i=0;i<this.options.length;i++)
	{
		var item = this.options[i];
		var img = document.getElementById(this.id + "_option_" + item.value);
		img.src = item.defaultImg;
	}
}

EljakimPriorityButton.prototype.get = function(value)
{
	for(var i=0;i<this.options.length;i++)
	{
		if(this.options[i].value == value)
			return this.options[i];
	}
	return null;
}

EljakimPriorityButton.prototype.set = function(value)
{
	this.clear();
	var item = this.get(value);
	if(!item)return;
	var img = document.getElementById(this.id + "_option_" + item.value);
	img.src = item.selectedImg;
	var hidden   = document.getElementById(this.valueId);
	hidden.value = item.value;
}

//TOOLS========================================================================================================================================================================================\


//post een FORM met behulp van ajax. Deze verschilt met de versie van Activegrid in de zin dat deze ook parallelle request ondersteunt
function AjaxFormPoster(returnFunction,submission,form)
{
	this.returnFunction = function(xml){returnFunction(xml);};
	this.submission = submission;
	this.form = form;
}

AjaxFormPoster.prototype.post = function(action) 
{    //de post/request
		showLoadingDiv();
		var parameters = createParameterString();//element.value = element.value.replace("'","\\'");
		var separator = "?";
		if (this.form.action.indexOf('?') >=0) {
			separator = '&';
		}
		var formAction = this.form.action + separator + this.submission;
		if(action)formAction = action;
		var object = this;
		ActiveGridAJAXPost(formAction, parameters, function(http){object.callBack(http);});
}

AjaxFormPoster.prototype.callBack = function(agHttpRequest)
{
	if(agHttpRequest.readyState == 4){
	   if (agHttpRequest.status == 200) {
		 lastMessage = agHttpRequest.responseText;
		 //alert(lastMessage);
		 var xmlDoc = (new DOMParser()).parseFromString(agHttpRequest.responseText, "text/xml");
			 if (xmlDoc.parseError != 0){
			   displayClientSideErrors(Sarissa.getParseErrorText(xmlDoc));
				 }else{
			   if ((xmlDoc.selectNodes("/AJAXIncludeXFormResponse/errors/pageErrors/item").length > 0) || (xmlDoc.selectNodes("/AJAXIncludeXFormResponse/errors/fieldErrors/field").length > 0)) {
				hideLoadingDiv();
				 displayPageErrors(xmlDoc);
			   } else {
				 this.returnFunction(xmlDoc);
				 hideLoadingDiv();
			   }
			 }
		   } else {
			 lastMessage = agHttpRequest.responseText;
			 displayClientSideErrors("There was a server error");
			 hideLoadingDiv();
		   }
	}
}
//=========================Validate functions==============================================================================================================================
function eljakimCheckReg(re, divid, message,containerID) {
	var formElement = document.getElementById(divid);
	if (formElement != null) {
    var formValue = document.getElementById(divid).value;
    if (re==/.*/ || re =="" || formValue.match(re)) {
      //matched, remove error
      if (errorHistory[divid]) {
      	eljakimClearError(divid,containerID);
      	errorHistory[divid] = false;
      }
    } else {
      //did not match
    	eljakimShowError(divid,containerID ,message);
    	errorHistory[divid] = true;
    	numberOfErrors++;
    }
  }
}

function eljakimShowError(divId , containerID,message)
{	
	var error = document.getElementById(divId+"_error");
	var node = document.getElementById(divId);
	var con = document.getElementById(containerID);
	if(!con)
	{
		setTimeout(function(){eljakimShowError(divId , containerID,message);},100);
		return;
	}
	if(!error)
	{
		error = document.createElement("div");
		error.id = divId+"_error";
		con.appendChild(error);
	}
	error.innerHTML = message;
	var object = this;
	node.oldClassName = node.className;
	node.className = "agEljakimInputError";
	//error.onclick     = function(){this.className = "agEljakimHiddenError";};
	error.className = "agEljakimError";
	con.style.display = "block";

}
function eljakimClearError(divId,containerID)
{
	var error = document.getElementById(divId+"_error");
	var node = document.getElementById(divId);
	var con = document.getElementById(containerID);
	if(node && node.oldClassName)
		node.className = node.oldClassName;
	if(con)con.style.display = "none";
}
