if(window["gLoaded"] != true)
{
	window["gLoaded"] = true;

	var xmlpath = "";
	var xmlfile = "";
	var syspath = "";
	function setSysPath(path)
	{
		syspath = path;		
	}
	function setXmlFile(file)
	{
		xmlfile = file;		
	}
	function setXmlPath(path)
	{
		xmlpath = path;
	}


	// document.getElementsByAttribute([string attributeName],[string attributeValue],[boolean isCommaSeparatedList:false])
	document.getElementsByAttribute=function(attrN,attrV,multi){
		attrV=attrV.replace(/\|/g,'\\|').replace(/\[/g,'\\[').replace(/\(/g,'\\(').replace(/\+/g,'\\+').replace(/\./g,'\\.').replace(/\*/g,'\\*').replace(/\?/g,'\\?').replace(/\//g,'\\/');
		var
			multi=typeof multi!='undefined'?
				multi:
				false,
			cIterate=typeof document.all!='undefined'?
				document.all:
				document.getElementsByTagName('*'),
			aResponse=[],
			re=new RegExp(multi?
				'\\b'+attrV+'\\b':
				'^'+attrV+'$'),
			i=0,
			elm;
		while((elm=cIterate.item(i++))){
			if(re.test(elm.getAttribute(attrN)||''))
				aResponse[aResponse.length]=elm;
		}
		return aResponse;
	}


/*	var enableDebug = false;
	
	var gLoaded = false;
	var objectToMove = null;
	var markedObject = null;
	var highest_zindex = 1;
	var offsetX, offsetY;
	var initHeight;
	var initWidth;
	var gPushViewOnMouseMove=null;
	var snapToGrid=false;
	var bMouseDown=false;
	
	var xmlpath = "";
	var xmlfile = "";
	var syspath = "";
	

	// grid width and height in pixels
	var snapToGridSize = 10;
	
	// 1 = move | 2 = resize
	var moveMode = null;
		
	
	
	function resizeComponent(obj, parentDiv)
	{
		obj.style.width = parentDiv.style.width;
		obj.style.height = parentDiv.style.height;		
	}
	
	function setXmlFile(file)
	{
		xmlfile = file;		
	}
	function setXmlPath(path)
	{
		xmlpath = path;
	}
	function setSnapToGrid(value)
	{
		snapToGrid = value;		
	}
	function setHighestZindex(index)
	{
		if(index > highest_zindex)
			highest_zindex = index;
	}
	function getHighestZindex()
	{
		return highest_zindex;
	}
	
	var marker = document.createElement('div');
	marker.style.border = 'dashed 1px black';
	marker.style.position = 'absolute';
	marker.style.width = '100px';
	marker.style.height = '100px';
	marker.style.zIndex = '0';
	marker.style.display = 'none';
	document.body.appendChild(marker);
	
	//alert(marker);
	function updateMarker()
	{
		marker.style.top = (parseInt(markedObject.offsetTop)-2)+'px';
		marker.style.left = (parseInt(markedObject.offsetLeft)-2)+'px';
		marker.style.width = (markedObject.offsetWidth+4)+'px';
		marker.style.height = (markedObject.offsetHeight+4)+'px';
	}
	
	function initMove(e, obj)
	{
		if (e == null) 
			e = window.event;
		
		objectToMove = obj;
		markedObject = objectToMove;

		moveMode = 1;
		offsetX = e.clientX - obj.offsetLeft + document.body.scrollLeft;
		offsetY = e.clientY - obj.offsetTop + document.body.scrollTop;
		
		if(obj.getAttribute('editmode') == 1)
		{
			marker.style.display = 'block';
			updateMarker();
		}
		
		resizeFix.style.display = 'block';
		resizeFix.style.top = (offsetY-200)+'px';
		resizeFix.style.left = (offsetX-200)+'px';

	}

	function endMove(obj) 
	{

		//	document.getElementById('debug1').value = obj.getAttribute('editmode');
		resizeFix.style.display = 'none';

		if(obj.getAttribute('editmode') == 1)
			savePosition(obj.getAttribute('path'),obj.getAttribute('xmlfile'),obj.getAttribute('formNr'), obj.offsetLeft, obj.offsetTop);
		updateMarker();
		objectToMove = null;
	}
	
	var resizeFix = document.createElement('div');
	resizeFix.style.position = 'absolute';
	resizeFix.style.width = '400px';
	resizeFix.style.height = '400px';
	resizeFix.style.zIndex = '999999';
	resizeFix.style.border = 'solid 0px red';
	
	resizeFix.style.display = 'none';
	
	var oldOnLoad123 = document.body.onload;
	
	function OnLoad123 ()
	{
		document.body.appendChild(resizeFix);
		if(oldOnLoad123)
			oldOnLoad123();
	}
	if(document.selection)
		document.body.onload = OnLoad123;
	else
		OnLoad123();
	
	function initResize(e, obj) 
	{
		if (e == null)
			e = window.event;
	
		moveMode = 2;
		objectToMove = obj;
		markedObject = objectToMove.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
		
		if(markedObject.getAttribute('locked_size') == '1')
		{
			objectToMove = null;
			return;
		}
		
		//alert(markedObject);
		initWidth= obj.style.width;
		initHeight = obj.style.height;
	
		initHeight = parseInt(initHeight.replace("px",""));
		initWidth = parseInt(initWidth.replace("px",""));
		
		offsetX = e.clientX - obj.offsetLeft + document.body.scrollLeft;
		offsetY = e.clientY - obj.offsetTop + document.body.scrollTop;
	
	
		resizeFix.style.display = 'block';
		resizeFix.style.top = (offsetY-200)+'px';
		resizeFix.style.left = (offsetX-200)+'px';
	
		bMouseDown=true;
		

		if(markedObject.getAttribute('editmode') == 1)
		{
			marker.style.display = 'block';
			updateMarker();
		}

		if ( e.stopPropagation ? e.stopPropagation() : e.cancelBubble = true );

	}
	
	function endResize(e, obj) 
	{
		resizeFix.style.display = 'none';
		objectToMove = null;
		saveSize(obj.getAttribute('path'),obj.getAttribute('xmlfile'),obj.getAttribute('formNr'), obj.offsetWidth, obj.offsetHeight);
		
		//updateResizedImage(obj);
		
		if ( e.stopPropagation ? e.stopPropagation() : e.cancelBubble = true );
	}

	/*function updateResizedImage(obj)
	{
		var objs = obj.getElementsByTagName('img');
		
		for(var i = 0; i < objs.length; i++)
		{
			if(objs[i].getAttribute('baseURL'))
			{
				
				alert(objs[i].getAttribute('baseURL'));
			}
		}
		
		
	}*/
/*
	function changeZindex(obj)
	{
		highest_zindex = highest_zindex +1;
		obj.style.zIndex = highest_zindex;
		
		if(obj.getAttribute('editmode') == 1)
			saveZindex(obj.getAttribute('path'),obj.getAttribute('xmlfile'),obj.getAttribute('formNr'), highest_zindex);
	}		
	

	if(document.onmousemove)
	{
		gPushViewOnMouseMove=document.onmousemove;
	}
	document.onmouseup = function(e)
	{
		bMouseDown=false;

	}	
	document.onmousedown = function (e)
	{
		bMouseDown=true;
	}
	function ViewOnMouseMove(e)
	{
		if(moveMode == 1 || moveMode == 2)
		{
			if(document.selection)
				document.body.onselectstart = function() {return false};
			else
				document.body.style.MozUserSelect='none';
		}
		else
		{
			if(document.selection)
				document.body.onselectstart = function() {return true};
			else
				document.body.style.MozUserSelect='';
		}
			
		if (e == null)
			e = window.event;


		if (objectToMove == null)
		{
			if(gPushViewOnMouseMove)
				gPushViewOnMouseMove(e);	

			return;
		}
		
		diffX = e.clientX - offsetX + document.body.scrollLeft;
		diffY = e.clientY - offsetY + document.body.scrollTop;
		
//		document.getElementById('debug1').value = document.body.scrollLeft;
//		document.getElementById('debug2').value = document.body.scrollTop;		
		
		//move window
		if(moveMode == 1)
		{
			
			if(snapToGrid)
			{
				near_x = parseInt(diffX / snapToGridSize) * snapToGridSize;
				near_y = parseInt(diffY / snapToGridSize) * snapToGridSize;
		
				objectToMove.style.left = near_x;
				objectToMove.style.top = near_y;
			}
			else
			{
				objectToMove.style.left = diffX;
				objectToMove.style.top = diffY;
			}

			if (bMouseDown==false)
			{

				//onmouseup events here
				endMove(objectToMove);

				return;
			}			
		}
		
		// resize window
		else if (moveMode == 2)
		{
			if (bMouseDown==false)
			{
				//onmouseup events here
				endResize(e, objectToMove);
				
				return;
			}
			if(snapToGrid)
			{				
				objectToMove.style.width = initWidth + diffX;
				objectToMove.style.height = initHeight + diffY ;

				objectToMove.style.width = parseInt(objectToMove.offsetWidth/snapToGridSize +1)*snapToGridSize;
				objectToMove.style.height = parseInt(objectToMove.offsetHeight/snapToGridSize +1)*snapToGridSize;
				
			}
			else
			{
				objectToMove.style.width = initWidth + diffX - snapToGridSize;
				objectToMove.style.height = initHeight + diffY - snapToGridSize;
			}

		}



		if(gPushViewOnMouseMove)
			gPushViewOnMouseMove(e);

		resizeFix.style.top = (e.clientY + document.body.scrollTop-200)+'px';
		resizeFix.style.left = (e.clientX + document.body.scrollLeft-200)+'px';

		updateMarker();

	}
	
	document.onmousemove=ViewOnMouseMove;

	function MoveMarkedObject(deltaX,deltaY)
	{
		markedObject.style.top = (parseInt(markedObject.style.top)+deltaY)+'px';
		markedObject.style.left = (parseInt(markedObject.style.left)+deltaX)+'px';
	}

	function showAddForm(path,filename) 
	{

		var centerX = screen.width /2 - 150;
		var centerY = screen.height /4 - 100;
		
		var win = window.open(syspath+"/add_form.cfm?xml="+path+"/"+filename, "ShowAddForm", "left=" + centerX + ", top=" + centerY + ", width=300, height=200, location=no, menubar=no, resizable=no, scrollbars=yes, status=no, titlebar=no, toolbar=no");
		win.focus();
	}
	function showEditForm(path,filename, id)
	{
		var centerX = screen.width - 300;
		var centerY = screen.height /8 - 100;
		
		var win = window.open(syspath+"/edit_form.cfm?xml="+path+"/"+filename+"&id=" + id, "ShowEditForm", "left=" + centerX + ", top=" + centerY + ", width=300, height=800, location=no, menubar=no, resizable=yes, scrollbars=yes, status=no, titlebar=no, toolbar=no");
		win.focus();
		
	}
	
	function GetXmlHttpObject(handler) 
	{
	
		var objXmlHttp = null;
	
		if (navigator.userAgent.indexOf("MSIE") >= 0) 
		{
	
			var strName = navigator.appVersion.indexOf("MSIE 5.5") >= 0 ? "Microsoft.XMLHTTP" : "Msxml2.XMLHTTP";
	
			try 
			{ 
				objXmlHttp = new ActiveXObject(strName);
				objXmlHttp.onreadystatechange = handler;
	
				return objXmlHttp;
			} 
			catch(e) 
			{ 
				alert("Error: Could not initiate ActiveX object!");
	
				return;
			} 
		}
		else if (navigator.userAgent.indexOf("Mozilla") >=0 ) 
		{
			objXmlHttp = new XMLHttpRequest();
			objXmlHttp.onload = handler;
			objXmlHttp.onerror = handler;
			
			return objXmlHttp;
		}
	}
		
	var execOnAjaxDone = null;
		
	function stateChanged() 
	{ 
		if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
			if(execOnAjaxDone)
				{
					execOnAjaxDone();
					execOnAjaxDone = null;
				}
	}
	
	function savePosition(path,file,id, x, y) 
	{
		xmlHttp.open("POST", syspath+"/save_position.cfm", true);
		xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		xmlHttp.send("filename="+path+"/"+file+"&id=" + id + "&x=" + x + "&y=" + y);
	
	}
	
	function saveSize(path,file,id, width, height) 
	{
		xmlHttp.open("POST", syspath+"/save_size.cfm", true);
		xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		xmlHttp.send("filename="+path+"/"+file+"&id=" + id + "&width=" + width + "&height=" + height);
	}
	
	function saveZindex(path,file,id, zindex)
	{
		
		xmlHttp.open("POST", syspath+"/save_zindex.cfm", true);
		xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		xmlHttp.send("filename="+path+"/"+file+"&id=" + id + "&zindex=" + zindex);
	}
	
	function deleteForm(path,file,id)
	{
		execOnAjaxDone = function()
		{
			document.location = document.location;
		}
		
		
		xmlHttp.open("POST", syspath+"/delete_form.cfm", true);
		xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		
		if(document.selection)
			xmlHttp.onreadystatechange = stateChanged;
		else
			xmlHttp.onload = stateChanged;
		
		xmlHttp.send("xmlpath="+path+"&xmlfile="+file+"&id=" + id);
	}


	
	function lockSize(obj)
	{
		obj.setAttribute('locked_size','1');
		xmlHttp.open("POST", syspath+"/set_locked_size.cfm", true);
		xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		xmlHttp.send("filename="+obj.getAttribute('path')+"/"+obj.getAttribute('xmlfile')+"&id=" + obj.getAttribute('formnr') + "&locked=1");
	}
	function unlockSize(obj)
	{
		obj.setAttribute('locked_size','0');
		xmlHttp.open("POST", syspath+"/set_locked_size.cfm", true);
		xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		
		xmlHttp.send("filename="+obj.getAttribute('path')+"/"+obj.getAttribute('xmlfile')+"&id=" + obj.getAttribute('formnr') + "&locked=0");
	}



	var xmlHttp = GetXmlHttpObject(stateChanged);
	
	
	
	function OnDropContainer(droptargetid,sourcetype,sourceid)
	{
		
		var path = decodeURI(droptargetid.replace('%3A',':').split('&')[0]);
		var xmlfile = decodeURI(droptargetid.replace('%3A',':').split('&')[1]);
		var pos_x = droptargetid.split('&')[2];
		var pos_y = droptargetid.split('&')[3];
		//alert('type: '+ sourcetype + '\nassetid: ' + sourceid + '\ndroptargetid: ' + droptargetid + '\nxmlfile: ' + xmlfile);
		
		var gg = encodeURI(document.location);
		gg = gg.replace(/&/g,'%26').replace(/=/g,'%3D');
		document.location = syspath+"/_drophandlers/handler.cfm?xmlpath="+xmlpath+'&xmlfile='+xmlfile+'&x='+pos_x+'&y='+pos_y+'&id='+sourceid+'&type='+sourcetype+ '&return='+gg; 
		
	}
	
	function OnDropFrame(droptargetid,sourcetype,sourceid)
	{
		var path = decodeURI(droptargetid.replace('%3A',':').split('&')[0]);
		var xmlfile = decodeURI(droptargetid.replace('%3A',':').split('&')[1]);
		var pos_x = droptargetid.split('&')[2];
		var pos_y = droptargetid.split('&')[3];
		var formid = droptargetid.split('&')[4];
		var locked_size = droptargetid.split('&')[5];
		
		var gg = encodeURI(document.location);
		gg = gg.replace(/&/g,'%26').replace(/=/g,'%3D');
		
		if(locked_size == '1')
			showCropWindow(path,xmlfile,pos_x,pos_y,formid,locked_size,sourceid,sourcetype);
		else
			document.location = syspath+"/_drophandlers/handler.cfm?formid="+formid+"&xmlpath="+xmlpath+'&xmlfile='+xmlfile+'&x='+pos_x+'&y='+pos_y+'&id='+sourceid+'&type='+sourcetype+ '&return='+gg; 
		
	}

	function showCropWindow(path,xmlfile,pos_x,pos_y,formid,locked_size,sourceid,sourcetype)
	{
		var centerX = screen.width - 300;
		var centerY = screen.height /8 - 100;
		
		var win = window.open(syspath+"/_components/cropedimage/crop.cfm?formid="+formid+"&xmlpath="+xmlpath+"&xmlfile="+xmlfile+'&x='+pos_x+'&y='+pos_y+'&id='+sourceid+'&type='+sourcetype, "ShowEditForm", "left=" + centerX + ", top=" + centerY + ", width=640, height=480, location=no, menubar=no, resizable=yes, scrollbars=no, status=no, titlebar=no, toolbar=no");
		win.focus();
	}

	var DnDDragObject = null;
	
	var DnDOldMouseMove = document.onmousemove;
	var DnDOldMouseUp = document.onmouseup;

	document.onmouseup = function DnDOnMouseUp(e)
	{
		
		if(DnDOldMouseUp)
			DnDOldMouseUp(e);
		
		if(DnDDragObject)
		{

			document.body.removeChild(DnDDragObject);

			if(document.selection)
				document.onselectstart = function() {return true};
			else
				document.body.style.MozUserSelect='';
			
			var xpos = document.selection?window.event.clientX + document.body.scrollLeft : e.pageX + document.body.scrollLeft;
			var ypos = document.selection?window.event.clientY + document.body.scrollTop : e.pageY;
			
			var forms = document.getElementsByName('view_builder_Edit_Form');
			
			var hitObject = null;
			var hitZIndex = -99999;
			for(var i = 0; i < forms.length; i++)
			{
				if(xpos >= forms[i].offsetLeft && xpos <= forms[i].offsetLeft + forms[i].offsetWidth	
					&& ypos >= forms[i].offsetTop && ypos <= forms[i].offsetTop + forms[i].offsetHeight)
				{
					Trace(forms[i].style.zIndex)
					if(forms[i].style.zIndex > hitZIndex)
						hitObject = forms[i];		
				}
			}
			if(hitObject)
				OnDropFrame(xmlpath+'&'+xmlfile+'&'+xpos+'&'+ypos+'&'+hitObject.getAttribute('formnr')+'&'+hitObject.getAttribute('locked_size'),DnDDragObject.getAttribute('DnDType'),DnDDragObject.getAttribute('DnDId'));	
			else
				OnDropContainer(xmlpath+'&'+xmlfile+'&'+xpos+'&'+ypos,DnDDragObject.getAttribute('DnDType'),DnDDragObject.getAttribute('DnDId'));	
			DnDDragObject = null;
				
		}
		
	}

	document.onmousemove = function DnDOnMouseMove(e)
	{
		if(DnDOldMouseMove)
			DnDOldMouseMove(e);
		if(DnDDragObject)
		{
			if(document.selection)
			{
				DnDDragObject.style.top=window.event.clientY + document.body.scrollTop;
				DnDDragObject.style.left=window.event.clientX + document.body.scrollLeft;
			}
			else
			{
				DnDDragObject.style.top=e.pageY;
				DnDDragObject.style.left=e.pageX + document.body.scrollLeft;
			}
		}
	}
	
	
	function DnDOnMouseDown(that)
	{
		if(DnDDragObject != null)
			document.body.removeChild(DnDDragObject);
		
		DnDDragObject = that.cloneNode(true);
		
		
		DnDDragObject.style.position='absolute';
		DnDDragObject.style.zIndex='99999';
		DnDDragObject.style.top='-100';
		DnDDragObject.style.left='-100';
		
		DnDDragObject.onmousedown=function(){};
		
		document.body.appendChild(DnDDragObject);
		
		if(document.selection)
			document.onselectstart = function() {return false};
		else
			document.body.style.MozUserSelect='none';
	}
	
	
	var dbg = null;

function startDebug()
	{
		dbg = document.createElement('div');
		dbg.style.height = '200px';
		dbg.style.width = '600px';
		dbg.style.background = '#efefef';
		dbg.style.overflow = 'scroll';
		dbg.style.position = 'absolute';
		dbg.style.bottom = '0px';
		dbg.style.right = '0px';
		dbg.style.zIndex = '9999999';
		var oldDocumentOnLoad = window.onload;
		
		document.body.appendChild(dbg);
		
		Trace('Debuging window initialized.');

		
	}
	
	function Trace(msg)
	{
		window.setTimeout( function()
		{
			if(dbg)
				dbg.innerHTML += msg+'<br />'; 
		}
		,0); 
	}
	
*/	
}

