
var formCtrlHolder = null;
var currentDragControl = null;
var currentDragPoint = null;
var currentInvitation = null;
var pauseCounter = 0;
var currentMouseOverControl = null;
var currentDropTop = 0;
var currentDropLeft = 0;
var currentScreenPosy = 0;
var currentScreenPosx = 0;

var DragImage = null;
var DragPlusImage = null;

var DragStartX = 0;
var DragOffsetX = 0;
var DragStartY = 0;
var DragOffsetY = 0;

var IsMoveTimer = false;

var PosHorizontalOffset = 120;
var NegHorizontalOffset = 115;
var VerticalOffset = 235;

var MenuStartedDocked = true;
var PropertyStartedDocked = true;

var DNDMouseEvent = null;
var IsCopy = false;

var DragHandleArray = new Array();
var DragHandleFadedArray = new Array();

//changes how long drag image appears on screen
var HoverImageDelay = 5500;


//============================================================================================================
//	FormDesignerStartup
//	Determines if designer is visible, runs the initialization routines for wiring up resize events
//	Creating all drag handlers and sizes the window
//============================================================================================================
function FormDesignerStartup() {
	if (document.getElementById('divFdSurface') != null) {
        //register Property Window and Control Menu for drag and drop" & vbCrLf & _
        Register_Windows();
        
        //calculate correct div size for displaying Form
        SetFormDesignerDivSize();
        
        //Hookup Window Size Event" & vbCrLf & _
        SetupWindowResizeEvent();
	}
}




//============================================================================================================
//	Drag
//	contains all the Drag routines for drag and drop
//============================================================================================================
var Drag = {

	obj : null,

	init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
	{
		o.onmousedown	= Drag.start;

		o.hmode			= bSwapHorzRef ? false : true ;
		o.vmode			= bSwapVertRef ? false : true ;

		o.root = oRoot && oRoot != null ? oRoot : o ;

		if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
		if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
		if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
		if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

		o.minX	= typeof minX != 'undefined' ? minX : null;
		o.minY	= typeof minY != 'undefined' ? minY : null;
		o.maxX	= typeof maxX != 'undefined' ? maxX : null;
		o.maxY	= typeof maxY != 'undefined' ? maxY : null;

		o.xMapper = fXMapper ? fXMapper : null;
		o.yMapper = fYMapper ? fYMapper : null;

		o.root.onDragStart	= new Function();
		o.root.onDragEnd	= new Function();
		o.root.onDrag		= new Function();
	},

	start : function(e)
	{
		var o = Drag.obj = this;
		e = Drag.fixE(e);
		DNDMouseEvent = e;
		
		if (typeof o.root == 'undefined')
			return;
		
		if (o.root == null)
			return;
		
		
		o.tempTop = 0;
		o.tempLeft = 0;		

		
		//var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var y = o.tempTop;
		//var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		var x = o.tempLeft;
		
		currentScreenPosx = e.clientX + document.body.scrollLeft;
		currentScreenPosy = e.clientY + document.body.scrollTop;
		
		DragStartX = e.clientX + document.body.scrollLeft;
		DragOffsetX = 0;
		DragStartY = e.clientY + document.body.scrollTop;
		DragOffsetY = 0;
		
		o.root.onDragStart(x, y);
		

		/*
		if (typeof pageXOffset != 'undefined')
			o.lastMouseX = e.clientX + pageXOffset;
		else
			o.lastMouseX = e.clientX + window.screenLeft;
			
		if (typeof pageYOffset != 'undefined')	
			o.lastMouseY	= e.clientY + pageYOffset;
		else
			o.lastMouseY = e.clientY + window.screenTop;
		*/	
		o.lastMouseX	= e.clientX;
		o.lastMouseY	= e.clientY;
		


		if (o.hmode) {
			if (o.minX != null)	o.minMouseX	= e.clientX - x + o.minX;
			if (o.maxX != null)	o.maxMouseX	= o.minMouseX + o.maxX - o.minX;
		} else {
			if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
			if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
		}

		if (o.vmode) {
			if (o.minY != null)	o.minMouseY	= e.clientY - y + o.minY;
			if (o.maxY != null)	o.maxMouseY	= o.minMouseY + o.maxY - o.minY;
		} else {
			if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
			if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
		}
		
		//InitStartOnDrag(o.lastMouseY, o.lastMouseX);

		document.onmousemove	= Drag.drag;
		document.onmouseup		= Drag.end;

		return false;
	},

	drag : function(e)
	{
		e = Drag.fixE(e);
		DNDMouseEvent = e;
		
		var o = Drag.obj;
		
		
		var ex = 0;
		var ey = 0;
		
		/*
		if (typeof pageXOffset != 'undefined')
			ex = e.clientX + pageXOffset;
		else
			ex = e.clientX + window.screenLeft;
			
		if (typeof pageYOffset != 'undefined')	
			ey	= e.clientY + pageYOffset;
		else
			ey = e.clientY + window.screenTop;
		*/
		ex	= e.clientX;
		ey	= e.clientY;
		
		currentScreenPosx = e.clientX + document.body.scrollLeft;
		currentScreenPosy = e.clientY + document.body.scrollTop;
		
		//var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var y = o.tempTop;
		
		//var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		var x = o.tempLeft;
		
		var nx, ny;

		if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
		if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
		if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
		if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);

		nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
		ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));

		if (o.xMapper)		nx = o.xMapper(y)
		else if (o.yMapper)	ny = o.yMapper(x)

		//Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
		o.tempLeft = nx;
		//Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
		o.tempTop = ny;
		
		//DoMoveOnDrag(o.lastMouseY, o.lastMouseX);
		
		Drag.obj.lastMouseX	= ex;
		Drag.obj.lastMouseY	= ey;

		Drag.obj.root.onDrag(nx, ny);
		return false;
	},

	end : function()
	{
		document.onmousemove = null;
		document.onmouseup   = null;
		
		//Drag.obj.root.onDragEnd(	parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]), 
		//							parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
									
		//Drag.obj.root.onDragEnd(Drag.obj.root.tempLeft, Drag.obj.root.tempTop);
		Drag.obj.root.onDragEnd(Drag.obj.tempLeft, Drag.obj.tempTop);
		Drag.obj = null;
		
		//reset mouse event
		DNDMouseEvent = null;
		
		if (DragPlusImage != null) {
			DragPlusImage.style.display = "none";
			DragPlusImage = null;
		}
		

	},

	fixE : function(e)
	{
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		return e;
	}
};



/*

function InitStartOnDrag(top, left) {
	var imgibeam = document.getElementById("imgWebControl");
	imgibeam.style.top = top;
	imgibeam.style.left = left;
	imgibeam.style.display = "block";
	
	TempDragObject = imgibeam;
}
function DoMoveOnDrag(top, left) {
	if (TempDragObject != null) {
		TempDragObject.style.top = top;
		TempDragObject.style.left = left;
	}
}
*/


function InitDragOnControl(field, handle) {
	Drag.init(field, handle);

	// capture events
	if(typeof(handle)!='undefined') {
		handle.onDragStart = function(x, y) { doDragStart(this, x, y); }
		handle.onDragEnd = function(x, y) { doDragEnd(this, x, y); }
		handle.onDrag = function(x, y) { doDrag(this, x, y); }

	} else {
		field.onDragStart = function(x, y) { doDragStart(this, x, y); }
		field.onDragEnd = function(x, y) { doDragEnd(this, x, y); }
		field.onDrag = function(x, y) { doDrag(this, x, y); }	
	
	}


	function doDragStart(el, x, y) {		
		HandleDragStart(el, x, y);	
	}

	function doDragEnd(el, x, y) {
		HandleDrop(el, x, y);
	}

	function doDrag(el, x, y) {
		HandleDragMove(el, x, y);
	}	
}



function HandleDragStart(el, x, y) {
	//DragControlTypes
	// Fields - 0
	// Sections - 1
	// Row - 2
	// NewControls = 3
	// NewRows = 4
	// NewSections = 5
	// MenuSelector = 6
	// PropertyWindow = 7
	// Clipboard = 8
	
	if (el != null) {
		var controltype = el.getAttribute("vvdesigner");
		
		if (controltype != null) {

			if (controltype == "control") {
				var id = el.getAttribute("vvfieldid");
				
				if (formCtrlHolder != null) {
					var form_control = formCtrlHolder.getControlByVvId(id);
					if (form_control != null) {
						currentDragControl = new DragControl(0, form_control, x, y, 0);
						
						DragImage = document.getElementById("imgFormControl");
					}
				}

			} else if (controltype == "row") {
				var id = el.getAttribute("vvfieldid");
				
				if (formCtrlHolder != null) {
					var form_control = formCtrlHolder.getRowByVvId(id);
					if (form_control != null) {
						currentDragControl = new DragControl(2, form_control, x, y, 0);
						
						DragImage = document.getElementById("imgRowControl");
					}
				}	
			} else if (controltype == "section") {
				var id = el.getAttribute("vvfieldid");
				
				if (formCtrlHolder != null) {
					var form_control = formCtrlHolder.getSectionByVvId(id);
					if (form_control != null) {
						currentDragControl = new DragControl(1, form_control, x, y, 0);
						
						DragImage = document.getElementById("imgSectionControl");
					}
				}

			//--------------------------------------------------------------------------------------------------

			} else if (controltype == "propertywindow") {
				var propwin = formCtrlHolder.Property;
				if (propwin != null) {
					currentDragControl = new DragControl(7, propwin, x, y, 0);

					DragImage = document.getElementById(propwin.ID);
				}

			} else if (controltype == "fieldselector") {		
				var menu_control = formCtrlHolder.Menu;
				if (menu_control != null) {
					currentDragControl = new DragControl(6, menu_control, x, y, 0);
					
					DragImage = document.getElementById("divMenuSelector");
				}				

			} else if (controltype == "menu") {
				if (!isNaN(el.getAttribute("menutype"))) {
					var menutype = el.getAttribute("menutype");
				
					var item = formCtrlHolder.Menu.getMenuItemByID(el.id);
					currentDragControl = new DragControl(3, item, x, y, menutype);
					
					DragImage = document.getElementById("imgFormControl");						
				} 


			} else if (controltype == "formpart") {				
				if (!isNaN(el.getAttribute("parttype"))) {
					var parttype = el.getAttribute("parttype");
					var item = formCtrlHolder.Menu.getMenuItemByID(el.id);
					
					if (parttype == 1) { //new Row
						currentDragControl = new DragControl(4, item, x, y, 0);	
						
						DragImage = document.getElementById("imgRowControl");
					} else if (parttype == 2) {
						currentDragControl = new DragControl(5, item, x, y, 0);	
						
						DragImage = document.getElementById("imgSectionControl");
					}
				}
			} else if (controltype == "clipitem") {
				var item = formCtrlHolder.Clipboard.getClipItemByID(el.id);
				
				if (item != null) {
					var ctrlType = item.ControlType;
					currentDragControl = new DragControl(8, item, x, y, ctrlType);	
					
					if (ctrlType == 101)
						DragImage = document.getElementById("imgSectionControl");
					else if (ctrlType == 100)
						DragImage = document.getElementById("imgRowControl");
					else
						DragImage = document.getElementById("imgFormControl");
				}
			}
			
			//Drag and Drop mouse event
			if (DNDMouseEvent != null) {
				if (DNDMouseEvent.ctrlKey == true) {
					DragPlusImage = document.getElementById("imgPlus");				
				}
			}

			
		}
	}
}

function HandleTimer() {
	//DragControlTypes
	// Fields - 0
	// Sections - 1
	// Row - 2
	// NewControls = 3
	// NewRows = 4
	// NewSections = 5
	// MenuSelector = 6
	// PropertyWindow = 7
	// Clipboard = 8
	
	//this.DragControlType
	//this.Control
	//this.X
	//this.Y
	//this.ControlType
	
	var tempControl = null;
	var winScrollPos = document.getElementById("divFdSurface").scrollTop;	
	

	var droptop = 0;
	var dropleft = 0;	
					
					
	var tempPoint = new DragPoint(currentDropLeft, currentDropTop);
	
	if (currentDragPoint != null) {
		if (IsDragHover(currentDragPoint, tempPoint)) {
			if (currentMouseOverControl != null) {
			
				// Add any offsets because of scroll position
				if (DragOffsetX == 0)
					DragOffsetX = DragStartX - currentDropLeft;
					
				if (DragOffsetY == 0)
					DragOffsetY = DragStartY - currentDropTop;			
			

				if (currentDragControl.DragControlType >= 0 && currentDragControl.DragControlType <= 2) {
				//Moving an existing control, section or row on the page
				
					droptop = currentDropTop;
					dropleft = currentDropLeft;				
				
				} else if (currentDragControl.DragControlType == 6 || currentDragControl.DragControlType == 7) {
				// moving the FieldSelector Menu or Property Window around
					return;
				
				} else {
				// moving one of the menuitems for a new field, section or row
				
					dropleft = currentDropLeft;				
					droptop = currentDropTop + winScrollPos;
				
				}
				

				//------------------------------------------------------------------
				//determine offsets for calculating which control is hovered over
				
				
				

					
							
				var menudocked = formCtrlHolder.Menu.IsDocked;
				var propertydocked = formCtrlHolder.Property.IsDocked;

				
				if (PropertyStartedDocked == true && propertydocked == false)
					droptop = droptop + VerticalOffset;
				else if (PropertyStartedDocked == false && propertydocked == true)
					droptop = droptop - VerticalOffset;
					
					
				if (MenuStartedDocked == true && menudocked == false)
					dropleft = dropleft + PosHorizontalOffset;
				else if (MenuStartedDocked == false && menudocked == true)
					dropleft = dropleft - NegHorizontalOffset;

				//------------------------------------------------------------------
			
			
				if (currentDragControl.DragControlType == 0) {
					// An existing form control is being dragged around
					
					//check if over the clipboard
					if (formCtrlHolder.Clipboard.IsHoverOnClipboard(currentScreenPosy, currentScreenPosx))
						tempControl = formCtrlHolder.Clipboard;
					else
						tempControl = formCtrlHolder.getControl(droptop, dropleft);

					
				} else if (currentDragControl.DragControlType == 1) {
					// An existing Section is being dragged around
					
					// check if over the clipboard
					if (formCtrlHolder.Clipboard.IsHoverOnClipboard(currentScreenPosy, currentScreenPosx))
						tempControl = formCtrlHolder.Clipboard;
					else
						tempControl = formCtrlHolder.getSection(droptop, dropleft);
					
				} else if (currentDragControl.DragControlType == 2) {
					// An existing Row is being dragged around
					
					// check if over the clipboard
					if (formCtrlHolder.Clipboard.IsHoverOnClipboard(currentScreenPosy, currentScreenPosx))
						tempControl = formCtrlHolder.Clipboard;
					else
						tempControl = formCtrlHolder.getRow(droptop, dropleft);
					
				} else if (currentDragControl.DragControlType == 3) {
					// A new form control is being dragged around
					tempControl = formCtrlHolder.getControl(droptop, dropleft);
					
				} else if (currentDragControl.DragControlType == 4) {
					// A new Row is being dragged around
					tempControl = formCtrlHolder.getRow(droptop, dropleft);
					
				} else if (currentDragControl.DragControlType == 5) {
					// A new Section is being dragged around
					tempControl = formCtrlHolder.getSection(droptop, dropleft);
				
				} else if (currentDragControl.DragControlType == 8) {
					//an item from the clipboard is being dragged around
					var ctrlType = currentDragControl.ControlType;
					
					if (ctrlType == 101)
						// a new Section is being dragged around
						tempControl = formCtrlHolder.getSection(droptop, dropleft);
						
					else if (ctrlType == 100)
						// a new Row is being dragged around
						tempControl = formCtrlHolder.getRow(droptop, dropleft);
						
					else
						// a new Form Control is being dragged around
						tempControl = formCtrlHolder.getControl(droptop, dropleft);
				}
				
				
				if (tempControl != null) {
					if (currentMouseOverControl.VvID != tempControl.VvID) {
						currentMouseOverControl.RemoveDragInvitation();

					} else {
						currentMouseOverControl.DisplayDragInvitation();
					}

				} else {
					currentMouseOverControl.RemoveDragInvitation();
					currentMouseOverControl = null;
				}
			
			} 
		}	
	}
	IsMoveTimer = false;

}





function HandleDragMove(el, x, y) {
	//DragControlTypes
	// Fields - 0
	// Sections - 1
	// Row - 2
	// NewControls = 3
	// NewRows = 4
	// NewSections = 5
	// MenuSelector = 6
	// PropertyWindow = 7
	// Clipboard = 8
	
	//this.DragControlType
	//this.Control
	//this.X
	//this.Y
	//this.ControlType

	var tempControl = null;
	var winScrollPos = document.getElementById("divFdSurface").scrollTop;
	var imageTop=0;
	var imageLeft=0;

	if (currentDragControl != null) {
		if (currentDragControl.DragControlType >= 0 && currentDragControl.DragControlType <= 2) {
			//Moving an existing control, section or row on the page
		
			var dragControl = currentDragControl.Control;
			
			if (dragControl != null) {
			
				// calculate the current mouse location
				//currentDropTop = dragControl.Top + y + (dragControl.Height/2);
				//currentDropLeft = (dragControl.Middle + x);
				
				// Add any offsets because of scroll position
				if (DragOffsetX == 0)
					DragOffsetX = DragStartX - currentDropLeft;
					
				if (DragOffsetY == 0)
					DragOffsetY = DragStartY - currentDropTop;
					
						
				currentDropTop = currentScreenPosy + winScrollPos;
				currentDropLeft = currentScreenPosx;	
				
				imageLeft = currentScreenPosx;
				imageTop = currentScreenPosy;				
				
				//------------------------------------------------------------------
				//determine offsets for calculating which control is hovered over
				
				var droptop = currentDropTop;
				var dropleft = currentDropLeft;
				
				var menudocked = formCtrlHolder.Menu.IsDocked;
				var propertydocked = formCtrlHolder.Property.IsDocked;

				
				if (PropertyStartedDocked == true && propertydocked == false) {
					droptop = droptop + VerticalOffset;
					currentScreenPosy = currentScreenPosy + VerticalOffset;
				} else if (PropertyStartedDocked == false && propertydocked == true) {
					droptop = droptop - VerticalOffset;
					currentScreenPosy = currentScreenPosy - VerticalOffset;
				}
					
				if (MenuStartedDocked == true && menudocked == false) {
					dropleft = dropleft + PosHorizontalOffset;
					//currentScreenPosx = currentScreenPosx + PosHorizontalOffset;
				}else if (MenuStartedDocked == false && menudocked == true) {
					dropleft = dropleft - NegHorizontalOffset;
					//currentScreenPosx = currentScreenPosx - NegHorizontalOffset;
				}


				//------------------------------------------------------------------
				
				
				if (formCtrlHolder.Clipboard.IsHoverOnClipboard(currentScreenPosy, currentScreenPosx)) {
					tempControl = formCtrlHolder.Clipboard
				
				} else if (currentDragControl.DragControlType == 0) { // Existing FormFiels
					tempControl = formCtrlHolder.getControl(droptop, dropleft);
				
				} else if (currentDragControl.DragControlType == 1) { // Existing Section
					tempControl = formCtrlHolder.getSection(droptop, dropleft);
				
				} else if (currentDragControl.DragControlType == 2) { // Existing Row
					tempControl = formCtrlHolder.getRow(droptop, dropleft);
				}


				if (tempControl != null) {
					if (tempControl.ID != dragControl.ID) {

						
						if (currentMouseOverControl != null) {
							if (tempControl.ID != currentMouseOverControl.ID) {
								currentMouseOverControl.RemoveDragInvitation();
								currentMouseOverControl = tempControl;
							}						

						} else {
							currentMouseOverControl = tempControl;
						}
						
						currentDragPoint = new DragPoint(currentDropLeft, currentDropTop);

						if (IsMoveTimer == false) {
							IsMoveTimer = true;
							setTimeout("HandleTimer()", 1000);
						}
					} else if (currentMouseOverControl != null) {	
						currentMouseOverControl.RemoveDragInvitation();
						currentMouseOverControl = null;
					}		

				} else if (currentMouseOverControl != null) {	
					currentMouseOverControl.RemoveDragInvitation();
					currentMouseOverControl = null;
				}				

			}
		} else if (currentDragControl.DragControlType == 6 || currentDragControl.DragControlType == 7) {
			// moving the FieldSelector Menu or Property Window around
		
			var toolwindow = currentDragControl.Control;
			if (toolwindow != null) {
			
				// calculate the current mouse location
				currentDropTop = toolwindow.Top + y;
				currentDropLeft = toolwindow.Left + x;			
	
				if (DragImage != null) {
					DragImage.style.top = currentDropTop;
					DragImage.style.left = currentDropLeft;
					DragImage.style.display = "block";
				}
				
				return;
			}		

		} else {
			// moving one of the menuitems for a new field, section or row, or moving a clipboard item
			
			var menuitem = currentDragControl.Control;
			if (menuitem != null) {
				
				// calculate the current mouse location
				currentDropTop = menuitem.Top + y + (menuitem.Height/2) + formCtrlHolder.Menu.Top;
				currentDropLeft = menuitem.Middle + x + formCtrlHolder.Menu.Left;
				
				// Add any offsets because of scroll position
				if (DragOffsetX == 0)
					DragOffsetX = DragStartX - currentDropLeft;
					
				if (DragOffsetY == 0)
					DragOffsetY = DragStartY - currentDropTop;
				
				//currentScreenPosx = currentDropLeft + DragOffsetX;
				//currentScreenPosy = currentDropTop + DragOffsetY;
				
				imageLeft = currentDropLeft + DragOffsetX;
				imageTop = currentDropTop + DragOffsetY;
				
				currentDropLeft = currentDropLeft + DragOffsetX;
				currentDropTop = currentDropTop + DragOffsetY

				//------------------------------------------------------------------
				//determine offsets for calculating which control is hovered over
				
				var droptop = currentDropTop + winScrollPos;
				var dropleft = currentDropLeft;
				
				var menudocked = formCtrlHolder.Menu.IsDocked;
				var propertydocked = formCtrlHolder.Property.IsDocked;

				
				if (PropertyStartedDocked == true && propertydocked == false)
					droptop = droptop + VerticalOffset;
				else if (PropertyStartedDocked == false && propertydocked == true)
					droptop = droptop - VerticalOffset;
					
					
				if (MenuStartedDocked == true && menudocked == false)
					dropleft = dropleft + PosHorizontalOffset;
				else if (MenuStartedDocked == false && menudocked == true)
					dropleft = dropleft - NegHorizontalOffset;


				//------------------------------------------------------------------

				//get control being hovered over
				if (currentDragControl.DragControlType == 3) {
					// a new Form Control is being dragged around
					tempControl = formCtrlHolder.getControl(droptop, dropleft);
								
				} else if (currentDragControl.DragControlType == 4) {
					// a new Row is being dragged around
					tempControl = formCtrlHolder.getRow(droptop, dropleft);

				} else if (currentDragControl.DragControlType == 5) {
					// a new Section is being dragged around
					tempControl = formCtrlHolder.getSection(droptop, dropleft);

				} else if (currentDragControl.DragControlType == 8) {
					var ctrlType = currentDragControl.ControlType;
					
					if (ctrlType == 101)
						// a clipboard Section is being dragged around
						tempControl = formCtrlHolder.getSection(droptop, dropleft);
						
					else if (ctrlType == 100)
						// a clipboard Row is being dragged around
						tempControl = formCtrlHolder.getRow(droptop, dropleft);
						
					else
						// a clipboard Form Control is being dragged around
						tempControl = formCtrlHolder.getControl(droptop, dropleft);
				}


				if (tempControl != null) {
					if (currentMouseOverControl != null) {
						if (tempControl.ID != currentMouseOverControl.ID) {
							currentMouseOverControl.RemoveDragInvitation();
							currentMouseOverControl = tempControl;
						}
					} else {
						currentMouseOverControl = tempControl;
					}				
						
					currentDragPoint = new DragPoint(currentDropLeft, currentDropTop);

					if (IsMoveTimer == false) {
						IsMoveTimer = true;
						setTimeout("HandleTimer()", 1000);
					}

				} else if (currentMouseOverControl != null) {	
					currentMouseOverControl.RemoveDragInvitation();
					currentMouseOverControl = null;
				}	

			}
		}
		
		if (DragImage != null) {
			DragImage.style.top = imageTop - 10;
			DragImage.style.left = imageLeft - 30;
			DragImage.style.display = "block";
			
			
			if (currentDragControl.DragControlType >= 0 && currentDragControl.DragControlType <= 2) {
				//Moving an existing control, section or row on the page
				
				if (DragPlusImage != null) {
					if (DNDMouseEvent.ctrlKey == true) {				
						//Places Copy Sign in the lower right above pointer
						DragPlusImage.style.top = imageTop + 15;
						DragPlusImage.style.left = imageLeft + 10;
						
						DragPlusImage.style.display = "block";					
					} else {
						DragPlusImage.style.display = "none";
					}
				}
			}
			
			window.status="currScreenX=" + currentScreenPosx + " currScreenY=" + currentScreenPosy;
			// + " currDropTop=" + currentDropTop + " currDropLeft=" + currentDropLeft + " outerScrollTop=" + document.body.scrollTop + " innerScrollTop=" winScrollPos;
		}
	} 
}




function HandleDrop(el, x, y) {
	//DragControlTypes
	// Fields - 0
	// Sections - 1
	// Row - 2
	// NewControls = 3
	// NewRows = 4
	// NewSections = 5
	// MenuSelector = 6
	// PropertyWindow = 7
	// Clipboard = 8


	var tempControl = null;
	var winScrollPos = document.getElementById("divFdSurface").scrollTop;
	
	if (DragPlusImage != null) {
		if (DNDMouseEvent.ctrlKey == true) {
			IsCopy = true;
		}
	}
	
	
	// record the current location of the Menu
	var menuselector = document.getElementById(formCtrlHolder.Menu.ID);
	if (menuselector != null) {
		formCtrlHolder.Menu.Top = parseInt(menuselector.style.top);
		formCtrlHolder.Menu.Left = parseInt(menuselector.style.left);
	}
	
	// record the current location of the Property Window
	if (formCtrlHolder.Property != null) {
		var propwin = document.getElementById(formCtrlHolder.Property.ID);
		if (propwin != null) {
			formCtrlHolder.Property.Top = parseInt(propwin.style.top);
			formCtrlHolder.Property.Left = parseInt(propwin.style.left);
		}	
	}

	
	//--------------------------------------------------------------------------------------------------

	if (currentDragControl != null) {
		// ensure that this a control section or row from the form
		if (currentDragControl.DragControlType >= 0 && currentDragControl.DragControlType <= 2) {
			//Moving an existing control, section or row on the page
	
			var dragControl = currentDragControl.Control;
			
			if (dragControl != null) {
			
			
			
			/*
				// calculate the current mouse location
				currentDropTop = dragControl.Top + y + (dragControl.Height/2) + DragOffsetY + winScrollPos;
				currentDropLeft = dragControl.Middle + x + DragOffsetX;


				//------------------------------------------------------------------
				//determine offsets for calculating which control is hovered over
				
				var droptop = currentDropTop;
				var dropleft = currentDropLeft;
				
				var menudocked = formCtrlHolder.Menu.IsDocked;
				var propertydocked = formCtrlHolder.Property.IsDocked;

				
				if (PropertyStartedDocked == true && propertydocked == false)
					droptop = droptop + VerticalOffset;
				else if (PropertyStartedDocked == false && propertydocked == true)
					droptop = droptop - VerticalOffset;
					
					
				if (MenuStartedDocked == true && menudocked == false)
					dropleft = dropleft + PosHorizontalOffset;
				else if (MenuStartedDocked == false && menudocked == true)
					dropleft = dropleft - NegHorizontalOffset;

				//------------------------------------------------------------------

			*/











				//------------------------------------------------------------------

				// Add any offsets because of scroll position
				if (DragOffsetX == 0)
					DragOffsetX = DragStartX - currentDropLeft;
					
				if (DragOffsetY == 0)
					DragOffsetY = DragStartY - currentDropTop;
					
						
				currentDropTop = currentScreenPosy + winScrollPos;
				currentDropLeft = currentScreenPosx;	
				
			
				//------------------------------------------------------------------
				//determine offsets for calculating which control is hovered over
				
				var droptop = currentDropTop;
				var dropleft = currentDropLeft;
				
				var menudocked = formCtrlHolder.Menu.IsDocked;
				var propertydocked = formCtrlHolder.Property.IsDocked;

				
				if (PropertyStartedDocked == true && propertydocked == false) {
					droptop = droptop + VerticalOffset;
				} else if (PropertyStartedDocked == false && propertydocked == true) {
					droptop = droptop - VerticalOffset;
				}
					
				if (MenuStartedDocked == true && menudocked == false) {
					dropleft = dropleft + PosHorizontalOffset;
					//currentScreenPosx = currentScreenPosx + PosHorizontalOffset;
				}else if (MenuStartedDocked == false && menudocked == true) {
					dropleft = dropleft - NegHorizontalOffset;
					//currentScreenPosx = currentScreenPosx - NegHorizontalOffset;
				}


				//------------------------------------------------------------------

				
				// check to see if this is an intentional hover
				var tempPoint = new DragPoint(currentDropLeft, currentDropTop); 
		
				if (currentDragPoint != null) {
					if (IsDragHover(currentDragPoint, tempPoint)) { // if close to the same point then it's a hover
						if (currentMouseOverControl != null) {
						
							if (formCtrlHolder.Clipboard.IsHoverOnClipboard(currentScreenPosy, currentScreenPosx))
								tempControl = formCtrlHolder.Clipboard
							
							else if (currentDragControl.DragControlType == 0) // retrieve the control under the mouse
								tempControl = formCtrlHolder.getControl(droptop, dropleft);
								
							else if (currentDragControl.DragControlType == 1) // retrieve the section under the mouse
								tempControl = formCtrlHolder.getSection(droptop, dropleft);
								
							else if (currentDragControl.DragControlType == 2) // retrieve the row under the mouse
								tempControl = formCtrlHolder.getRow(droptop, dropleft);
							
							
							if (tempControl != null) {
							
								// if the IDs match then we dropped on a control
								if (currentMouseOverControl.VvID == tempControl.VvID) {
									
									if (currentDragControl.DragControlType == 0){ // moving an existing Form Control
										if (tempControl.VvID.toLowerCase() == "clipboard")
											MoveControlToClipboard(dragControl.VvID, IsCopy);
										else
											MoveControl(dragControl.VvID, tempControl.SectionID, tempControl.Index, IsCopy);
										
									} else if (currentDragControl.DragControlType == 1) { // moving an existing Section
										if (tempControl.VvID.toLowerCase() == "clipboard")
											MoveSectionToClipboard(dragControl.VvID, IsCopy);
										else
											MoveSection(dragControl.VvID, tempControl.RowVvID, tempControl.Index, IsCopy);
									
									} else if (currentDragControl.DragControlType == 2) { // moving an existing Row
										if (tempControl.VvID.toLowerCase() == "clipboard")
											MoveRowToClipboard(dragControl.VvID, IsCopy);
										else
											MoveRow(dragControl.VvID, tempControl.Index, IsCopy);
									}
								} else {
									if (currentMouseOverControl != null) {
										currentMouseOverControl.RemoveDragInvitation();
										currentMouseOverControl = null;
									}
									ResetDragControlPosition();				
								}						
							} else {
								if (currentMouseOverControl != null) {
									currentMouseOverControl.RemoveDragInvitation();
									currentMouseOverControl = null;
								}
								ResetDragControlPosition();				
							}

						} else {
							ResetDragControlPosition();
						}
						
					} else {
						if (currentMouseOverControl != null) {
							currentMouseOverControl.RemoveDragInvitation();
							currentMouseOverControl = null;
						}
						ResetDragControlPosition();				
					}
						
				} else {
					if (currentMouseOverControl != null) {
						currentMouseOverControl.RemoveDragInvitation();
						currentMouseOverControl = null;
					}
					ResetDragControlPosition();
				
				}
					
			}
		} else if (currentDragControl.DragControlType == 6 || currentDragControl.DragControlType == 7) {
			// moving the FieldSelector Menu or Property Window around
			DragImage = null;


			if (currentDragControl.DragControlType == 6) {
				//menu move
				
				var mainMenuList = document.getElementById("divMenuSelector");
	
				if (mainMenuList != null) 
				{
					//get current coordinates
					var menuTop = parseInt(mainMenuList.style.top);
					var menuLeft = parseInt(mainMenuList.style.left);
					
					if (formCtrlHolder.Menu != null) {
						formCtrlHolder.Menu.Top = menuTop;
						formCtrlHolder.Menu.Left = menuLeft;
						
						UpdateMenuPosition();
					}
				}
				if (formCtrlHolder.Clipboard != null) {
					formCtrlHolder.Clipboard.MenuHasMoved = true;
				}


			} else if (currentDragControl.DragControlType == 7) {
				//property window move

				var propWin = document.getElementById("divPropertyHolder");
				
				if (propWin != null) {
					//get current coordinates
					var propertyTop = parseInt(propWin.style.top);
					var propertyLeft = parseInt(propWin.style.left);
					
					if (formCtrlHolder.Property != null) {
						formCtrlHolder.Property.Top = propertyTop;
						formCtrlHolder.Property.Left = propertyLeft;
						
						UpdatePropertyPosition();
					}
				}
			}
			

		} else {
			// moving one of the menuitems for a new field, section or row
			
			// a menu item has been dropped onto the form
			var menuitem = currentDragControl.Control;
			
			if (menuitem != null) {
			
				// calculate the current mouse location
				currentDropTop = menuitem.Top + y + (menuitem.Height/2) + formCtrlHolder.Menu.Top;
				currentDropLeft = menuitem.Middle + x + formCtrlHolder.Menu.Left;
				
				// Add any offsets because of scroll position
				if (DragOffsetX == 0)
					DragOffsetX = DragStartX - currentDropLeft;
					
				if (DragOffsetY == 0)
					DragOffsetY = DragStartY - currentDropTop;
				
				
				currentDropLeft = currentDropLeft + DragOffsetX;
				currentDropTop = currentDropTop + DragOffsetY

				//------------------------------------------------------------------
				//determine offsets for calculating which control is hovered over
				
				var droptop = currentDropTop + winScrollPos;
				var dropleft = currentDropLeft;
				
				var menudocked = formCtrlHolder.Menu.IsDocked;
				var propertydocked = formCtrlHolder.Property.IsDocked;

				
				if (PropertyStartedDocked == true && propertydocked == false)
					droptop = droptop + VerticalOffset;
				else if (PropertyStartedDocked == false && propertydocked == true)
					droptop = droptop - VerticalOffset;
					
					
				if (MenuStartedDocked == true && menudocked == false)
					dropleft = dropleft + PosHorizontalOffset;
				else if (MenuStartedDocked == false && menudocked == true)
					dropleft = dropleft - NegHorizontalOffset;
				
			
				// check to see if this is an intentional hover
				var tempPoint = new DragPoint(currentDropLeft, currentDropTop);
		
				if (currentDragPoint != null) {
					if (IsDragHover(currentDragPoint, tempPoint)) { // if close to the same point then it's a hover
						if (currentMouseOverControl != null) {
						
							if (currentDragControl.DragControlType == 3) { // adding a new Form Control
								NewControl(menuitem.MenuType, currentMouseOverControl.SectionID, currentMouseOverControl.Index);
								
							} else if (currentDragControl.DragControlType == 4) { // adding a new Row
								NewRow(currentMouseOverControl.Index);
							
							} else if (currentDragControl.DragControlType == 5) { // adding a new Section
								NewSection(currentMouseOverControl.RowVvID, currentMouseOverControl.Index);
							
							} else if (currentDragControl.DragControlType == 8) { // dragging from the clipboard
								var ctrlType = currentDragControl.ControlType;
								var dragControl = currentDragControl.Control;
								
								if (ctrlType == 101)
									// a clipboard Section is being dragged around
									MoveSection(dragControl.VvID, currentMouseOverControl.RowVvID, currentMouseOverControl.Index, IsCopy);
									
								else if (ctrlType == 100)
									// a clipboard Row is being dragged around
									MoveRow(dragControl.VvID, currentMouseOverControl.Index, IsCopy);
									
								else
									// a clipboard Form Control is being dragged around
									MoveControl(dragControl.VvID, currentMouseOverControl.SectionID, currentMouseOverControl.Index, IsCopy);					
							
							}
						} else {
							ResetDragControlPosition();
						}
						
					} else {
						if (currentMouseOverControl != null) {
							currentMouseOverControl.RemoveDragInvitation();
							currentMouseOverControl = null;
						}
						ResetDragControlPosition();				
					}
						
				} else {
					if (currentMouseOverControl != null) {
						currentMouseOverControl.RemoveDragInvitation();
						currentMouseOverControl = null;
					}
					ResetDragControlPosition();
				
				}		
			}
		}
	}
	
	IsCopy = false;
}

function IsDragHover(currentDP, prevDP) {
	if ((currentDP.X >= (prevDP.X - 2)) && (currentDP.X <= (prevDP.X + 2))) {
		if ((currentDP.Y >= (prevDP.Y - 2)) && (currentDP.Y <= (prevDP.Y + 2))) {
			return true;
		}
	}
	
	return false;
}

function ResetDragControlPosition() {
	if (DragImage != null) {
		DragImage.style.top = 0;
		DragImage.style.left = 0;
		DragImage.style.display = "none";
		DragImage = null;
	}
	/*
	if (currentDragControl != null) {
		control = currentDragControl.Control;
		
		var el = document.getElementById(control.ID);
		
		if (el != null) {
			el.style.left = "0px";
			el.style.top = "0px";
		}
	}
	*/
}



function SwitchMenuToFloat() {
	var mainMenuList = document.getElementById("divMenuSelector");
	var menuselectorhandle = document.getElementById("divMenuSelectorHandle");

	if (mainMenuList != null && menuselectorhandle != null) 
	{
		var dockmode = mainMenuList.getAttribute("dockmode");
		if (dockmode != null) {
			if (dockmode == "docked") {

				//set window to be floated
				mainMenuList.setAttribute("dockmode", "float");
				mainMenuList.style.position = "absolute";
				mainMenuList.style.top = "55px";
				mainMenuList.style.left = "0px";
				mainMenuList.style.zIndex = "1000"
				
				//change cursor on handle to be move cursor
				menuselectorhandle.style.cursor = "Move"

				//turn on dragging capability
				InitDragOnControl(menuselectorhandle, mainMenuList);
			
				//setup pushpin image
				var btnMenuFloat = document.getElementById("imgMenuFloat");
				//src="~/Images/PushPin-5.gif"
				btnMenuFloat.setAttribute("src", "Images/PushedPinButton.gif");
				btnMenuFloat.setAttribute("onmouseover", "javascript: this.src='Images/PushedPinButton_hover.gif';");
				btnMenuFloat.setAttribute("onmouseout", "javascript: this.src='Images/PushedPinButton.gif';");
				
				//update current location and send to server
				if (formCtrlHolder.Menu != null) {
					var menuTop = parseInt(mainMenuList.style.top);
					var menuLeft = parseInt(mainMenuList.style.left);
					
					formCtrlHolder.Menu.IsDocked = false;
					
					formCtrlHolder.Menu.Top = menuTop;
					formCtrlHolder.Menu.Left = menuLeft;
					
					UpdateMenuPosition();					
				}
			} else {

				//set window to be docked
				mainMenuList.setAttribute("dockmode", "docked");
				mainMenuList.style.position = "relative";
				mainMenuList.style.top = "0px";
				mainMenuList.style.left = "0px";
				
				//change cursor on handle to be pointer
				menuselectorhandle.style.cursor = "Default"
				
				//make not draggable
				mainMenuList.onDragStart = null;
				mainMenuList.onDragEnd = null;
				mainMenuList.onDrag = null;	

				menuselectorhandle.onDragStart = null;
				menuselectorhandle.onDragEnd = null;
				menuselectorhandle.onDrag = null;
				
				menuselectorhandle.root = null;
							
				//setup pushpin image
				var btnMenuFloat = document.getElementById("imgMenuFloat");
				//src="~/Images/PushPin-5.gif"
				btnMenuFloat.setAttribute("src", "Images/PushPin-5.gif");
				btnMenuFloat.setAttribute("onmouseover", "javascript: this.src='Images/PushPin-5_hover.gif';");
				btnMenuFloat.setAttribute("onmouseout", "javascript: this.src='Images/PushPin-5.gif';");
				
				//update current location and send to server
				if (formCtrlHolder.Menu != null) {
					formCtrlHolder.Menu.IsDocked = true;
					formCtrlHolder.Menu.Top = 0;
					formCtrlHolder.Menu.Left = 0;
					
					UpdateMenuPosition();
				}			

			}
			
		formCtrlHolder.Clipboard.MenuHasMoved = true;
		}

	}

}



function SwitchToFloat() {
	var propWin = document.getElementById("divPropertyHolder");
	var propWinHandle = document.getElementById("divPropertyTitle");
	
	if (propWin != null && propWinHandle != null) {
		var dockmode = propWin.getAttribute("dockmode");
		
		if (dockmode != null) {
			if (dockmode == "docked") {
				
				//set window to be floated
				propWin.setAttribute("dockmode", "float");
				propWin.style.position = "absolute";
				propWin.style.top = "67px";
				propWin.style.left = "124px";
				propWin.style.zIndex = "100";
				
				//change cursor on handle to be move cursor
				propWinHandle.style.cursor = "Move";

				//turn on dragging capability
				InitDragOnControl(propWinHandle, propWin);
				
				//setup pushpin image
				var btnPropertyFloat = document.getElementById("imgPropertyFloat");
				//src="~/Images/PushPin-5.gif"
				btnPropertyFloat.setAttribute("src", "Images/PushedPinButton.gif");
				btnPropertyFloat.setAttribute("onmouseover", "javascript: this.src='Images/PushedPinButton_hover.gif';");
				btnPropertyFloat.setAttribute("onmouseout", "javascript: this.src='Images/PushedPinButton.gif';");
				
								
				//update current location and send to server
				if (formCtrlHolder.Property != null) {
					var propertyTop = parseInt(propWin.style.top);
					var propertyLeft = parseInt(propWin.style.left);
					
					formCtrlHolder.Property.IsDocked = false;
					formCtrlHolder.Property.Top = propertyTop;
					formCtrlHolder.Property.Left = propertyLeft;
					
					UpdatePropertyPosition();
				}

				SetFormDesignerDivSize();
				
			} else {

				//set window to be docked
				propWin.setAttribute("dockmode", "docked");
				propWin.style.position = "relative";
				propWin.style.top = "0px";
				propWin.style.left = "0px";
				
				//change cursor on handle to be pointer
				propWinHandle.style.cursor = "Default";
			
				//make not draggable
				propWin.onDragStart = null;
				propWin.onDragEnd = null;
				propWin.onDrag = null;	

				propWinHandle.onDragStart = null;
				propWinHandle.onDragEnd = null;
				propWinHandle.onDrag = null;
				
				propWinHandle.root = null;
				
				//setup pushpin image
				var btnPropertyFloat = document.getElementById("imgPropertyFloat");
				//src="~/Images/PushPin-5.gif"
				btnPropertyFloat.setAttribute("src", "Images/PushPin-5.gif");
				btnPropertyFloat.setAttribute("onmouseover", "javascript: this.src='Images/PushPin-5_hover.gif';");
				btnPropertyFloat.setAttribute("onmouseout", "javascript: this.src='Images/PushPin-5.gif';");
				
				//update current location and send to server
				if (formCtrlHolder.Property != null) {
					formCtrlHolder.Property.IsDocked = true;
					formCtrlHolder.Property.Top = 0;
					formCtrlHolder.Property.Left = 0;
					
					UpdatePropertyPosition();
				}
				
				SetFormDesignerDivSize();	

			}		
		
		}

	}

}







//==========================================================================================================
//	Register_Controls
//	Walks the DOM and creates class objects of the form elements
//==========================================================================================================
function Register_Windows() 
{
	/*
	try 
	{
		console.time("Registration_Timer");
	}
	*/	
	
	formCtrlHolder = new FormControlHolder();

	var mainMenuList = document.getElementById("divMenuSelector");
	var menuselectorhandle = document.getElementById("divMenuSelectorHandle");

	if (mainMenuList != null && menuselectorhandle != null) 
	{
		
		//get current docked state
		var menudockmode = mainMenuList.getAttribute("dockmode");
		
		//get current coordinates
		var selectorTop = parseInt(mainMenuList.style.top);
		var selectorLeft = parseInt(mainMenuList.style.left);
		
		//create MenuWindow and set into the holder
		var menuSelector = new MenuSelector(mainMenuList.id, selectorTop, selectorLeft);
		formCtrlHolder.Menu = menuSelector;
		
		
		var clipDrop = document.getElementById("divClipDropArea");
		if (clipDrop != null) {
			var clipDropID = clipDrop.id;
			var clipDropTop = clipDrop.offsetTop + mainMenuList.offsetTop + mainMenuList.offsetParent.offsetTop;
			var clipDropLeft = clipDrop.offsetLeft + mainMenuList.offsetLeft;
			var clipDropWidth = clipDrop.offsetWidth;
			var clipDropRight = clipDropLeft + clipDropWidth;
			var clipDropHeight = clipDrop.clientHeight;
			var clipDropBottom = clipDropTop + clipDropHeight;
			
			var clip = new Clipboard(clipDropID,clipDropTop,clipDropBottom,clipDropLeft,clipDropRight, mainMenuList.id);
			formCtrlHolder.Clipboard = clip;
		}
		
		

		// record current dock state
		if (menudockmode != null) {
			if (menudockmode == "docked") {	
				menuSelector.IsDocked = true;
				
				//setup hint for calculating hovercontrols
				MenuStartedDocked = true;
				
				//change cursor on handle to be pointer
				menuselectorhandle.style.cursor = "Default";
				
			} else {
				menuSelector.IsDocked = false;
				
				//setup hint for calculating hovercontrols
				MenuStartedDocked = false;
	
				//turn on dragging capability
				InitDragOnControl(menuselectorhandle, mainMenuList);
				
				//change cursor on handle to be move cursor
				menuselectorhandle.style.cursor = "Move";
				
				var btnMenuFloat = document.getElementById("imgMenuFloat");
				//src="~/Images/PushPin-5.gif"
				btnMenuFloat.setAttribute("src", "Images/PushedPinButton.gif");
				btnMenuFloat.setAttribute("onmouseover", "javascript: this.src='Images/PushedPinButton_hover.gif';");
				btnMenuFloat.setAttribute("onmouseout", "javascript: this.src='Images/PushedPinButton.gif';");
			}
		}		

		cntMenuItems = 0;
		cntClipItems = 0;
		
		var menuitems = mainMenuList.getElementsByTagName("div");
		for (var i = 0; i < menuitems.length; i++) { 
			var menuitem = menuitems[i];
			
			// determine if this is a Form Row
			var menuvvdesigner = menuitem.getAttribute("vvdesigner");

			if (menuvvdesigner != null) {
				if (menuvvdesigner == "menu" || menuvvdesigner == "formpart") {
					InitDragOnControl(menuitem);
					
					// load position info
					var menuID = menuitem.id;
					var menuTop = menuitem.offsetTop;
					var menuLeft = menuitem.offsetLeft;
					var menuWidth = menuitem.offsetWidth;
					var menuRight = menuLeft + menuWidth;
					var menuHeight = menuitem.clientHeight;
					var menuMiddle = menuLeft + (menuWidth/2);
					var menuBottom = menuTop + menuHeight;
					var menuType = menuitem.getAttribute("menutype");
					var partType = menuitem.getAttribute("parttype");
					
					if (menuvvdesigner == "menu") {
						var item = new MenuItem(menuID, menuTop, menuBottom, menuLeft, menuRight, menuHeight, menuMiddle, menuType, 0);
					
					}else if (menuvvdesigner == "formpart") {
						var item = new MenuItem(menuID, menuTop, menuBottom, menuLeft, menuRight, menuHeight, menuMiddle, 0, partType);
					}
					
					
					menuSelector.MenuItems[cntMenuItems] = item;
					cntMenuItems++;

				}else if (menuvvdesigner == "clipitem") {
					InitDragOnControl(menuitem);
					
					
					// load position info
					var clipID = menuitem.id;
					
					var clipTop = menuitem.offsetTop;
					var clipLeft = menuitem.offsetLeft;
					var clipWidth = menuitem.offsetWidth;
					var clipRight = clipLeft + clipWidth;
					var clipHeight = menuitem.clientHeight;
					var clipMiddle = clipLeft + (clipWidth/2);
					var clipBottom = clipTop + clipHeight;
					
					var clipCtrlType = menuitem.getAttribute("cliptype");
					var vvid = menuitem.getAttribute("vvfieldid");
				
					var item = new ClipItem(clipID, vvid, clipTop, clipBottom, clipLeft, clipRight, clipHeight, clipMiddle, clipCtrlType);
					
					clip.ClipItems[cntClipItems] = item;
					cntClipItems++;
					
					
				}

			}
		}
	}
	
	//============================================================
	

	
	
	//============================================================
	
	var propWin = document.getElementById("divPropertyHolder");
	var propWinHandle = document.getElementById("divPropertyTitle");

	if (propWin != null && propWinHandle != null) {
		//get current docked state
		var dockmode = propWin.getAttribute("dockmode");
		
		//get current coordinates
		var propertyTop = parseInt(propWin.style.top);
		var propertyLeft = parseInt(propWin.style.left);

		//create PropertyWindow and set into the holder
		var propwindow = new PropertyWindow(propWin.id, propertyTop, propertyLeft);
		formCtrlHolder.Property = propwindow;

		// record current dock state
		if (dockmode != null) {
			if (dockmode == "docked") {	
				propwindow.IsDocked = true;
				
				//setup hint for calculating hovercontrols
				PropertyStartedDocked = true;
				
				//change cursor on handle to be pointer
				propWinHandle.style.cursor = "Default";
			} else {
				propwindow.IsDocked = false;
				
				//setup hint for calculating hovercontrols
				PropertyStartedDocked = false;
								
				//turn on dragging capability
				InitDragOnControl(propWinHandle, propWin);
				
				//change cursor on handle to be move cursor
				propWinHandle.style.cursor = "Move";
				
				//setup pushpin image
				var btnPropertyFloat = document.getElementById("imgPropertyFloat");
				//src="~/Images/PushPin-5.gif"
				btnPropertyFloat.setAttribute("src", "Images/PushedPinButton.gif");
				btnPropertyFloat.setAttribute("onmouseover", "javascript: this.src='Images/PushedPinButton_hover.gif';");
				btnPropertyFloat.setAttribute("onmouseout", "javascript: this.src='Images/PushedPinButton.gif';");
			}
		}
	}
}


function Register_Controls() {
	var cntRows = 0;
	var mainOffset = 0;
	
	//if (getBrowser().toLowerCase() == "firefox") {
	//	console.time("Registration_Timer");
	//}
	
	var scrollDiv = document.getElementById("divFdSurface");
	var scrollWinOffsetTop = scrollDiv.offsetTop;
	var scrollWinOffsetLeft = scrollDiv.offsetLeft;
	
	var mainDataList = document.getElementById("ctrlFDRowView_dl1");
	if (mainDataList != null) 
	{	
		var tbody = null
		var tr = null;
		var tbody = mainDataList.firstChild;

					
		//walk the DOM to grab handle to the Rows
		while (tbody) {
			if (tbody.nodeName.toLowerCase() == "tbody") {
				tr = tbody.firstChild;
				
				while (tr) {
					if (tr.nodeName.toLowerCase() == "tr") {
						var el = tr.firstChild;
						
						while (el) {
							if (el.nodeName.toLowerCase() == "td") {
								el = el.firstChild;
								
								while (el) {
									if (el.nodeName.toLowerCase() == "table") {
										var handlename = el.getAttribute("vvdesigner");
										
										if (handlename != null) {
											if (handlename == "row") {
												if (cntRows == 0) {
													mainOffset = GetMainOffset();
												}	


												//save row into formholder
												SaveRow(el,cntRows,mainOffset,scrollWinOffsetTop,scrollWinOffsetLeft);														
												
												// increment the indexer
												cntRows = cntRows + 1;
												
												break;
											}
										}
										el = el.nextSibling;
										
												
									}
									el = el.nextSibling;
									
								}
								break;

							}						
							el = el.nextSibling;							

						}
						
						
					}				
					tr = tr.nextSibling;
						
				}
				break;
				
			}	
			tbody = tbody.nextSibling;									
											
		}	

	}
	
	//if (getBrowser().toLowerCase() == "firefox") {
	//	console.timeEnd("Registration_Timer");
	//}
	
	var scrollPosition = 0;
	var inputScrollPos = null;
	
	inputScrollPos = document.getElementById('inputCurrentScrollPosition')
	
	if (inputScrollPos != null) {
		if (!isNaN(inputScrollPos.value)) {
			scrollPosition = inputScrollPos.value;
			
			var divFdSurface = document.getElementById('divFdSurface');
			if (divFdSurface != null) {
				divFdSurface.scrollTop = scrollPosition;
			}
		}
	}	
}

function SaveRow(row,cntRows,mainOffset,scrollWinOffsetTop,scrollWinOffsetLeft) {
	var cntSections = 0;
	var cntSectionRows = 0;
	var cntControls = 0;
	
	var form_control = null;
	var form_section = null;
	var form_row = null;
	
	var el = row.firstChild;
	
	//walk the DOM to grab handle to the RowDragHandle
	while (el) {
		if (el.nodeName.toLowerCase() == "tbody") {
			el = el.firstChild;
			
			while (el) {
				if (el.nodeName.toLowerCase() == "tr") {
					el = el.firstChild;
					
					while (el) {
						if (el.nodeName.toLowerCase() == "td") {
							
							var handlename = el.getAttribute("vvdesigner");
							
							if (handlename != null) {
								if (handlename == "rowhandle") {
									//grab the RowDragHandle
									InitDragOnControl(el, row);
									break;
								}
							}			

						}						
						el = el.nextSibling;							

					}
					break;
					
				}				
				el = el.nextSibling;
					
			}
			break;

		}	
		el = el.nextSibling;									
										
	}

	// reset section's indexer
	cntSections = 0;
	







	// load position info
	var rowID = row.id;
	
	var rowTop = row.offsetTop + mainOffset + scrollWinOffsetTop;
	var rowLeft = row.offsetLeft + scrollWinOffsetLeft;
	var rowWidth = row.offsetWidth;
	var rowRight = rowLeft + rowWidth;
	var rowHeight = row.clientHeight;
	var rowMiddle = rowLeft + (rowWidth/2);
	var rowBottom = rowTop + rowHeight;
	
	var rowVvID = row.getAttribute("vvfieldid");
	var rowIndex = row.getAttribute("rowindex");
	


	// Create FormRow
	form_row = new FormRow(rowID, rowVvID, rowTop, rowBottom, rowLeft, rowRight, rowHeight, rowMiddle, rowIndex);


	//=================================================================
	//	Get collection of Sections
	//================================================================
	var sections = row.getElementsByTagName("div");

	for (var j = 0; j < sections.length; j++) { 
		var section = sections[j];
		
		// determine if this is a Form Section
		var sectionvvdesigner = section.getAttribute("vvdesigner");

		if (sectionvvdesigner != null) {
			if (sectionvvdesigner == "section") {
				var sectionOffset = 0;

				var el = section.firstChild;
				
				//walk the DOM to grab handle to the SectionDragHandle
				while (el) {
					if (el.nodeName.toLowerCase() == "table") {
						//get the section offset
						sectionOffset = el.offsetLeft;
						
						el = el.firstChild;
						
						while (el) {
							if (el.nodeName.toLowerCase() == "tbody") {
								el = el.firstChild;
								
								while (el) {
									if (el.nodeName.toLowerCase() == "tr") {
										el = el.firstChild;
										
										while (el) {
											if (el.nodeName.toLowerCase() == "td") {
												el = el.firstChild;

												while (el) {
													if (el.nodeName.toLowerCase() == "table") {
														var handlename = el.getAttribute("vvdesigner");
														
														if (handlename != null) {
															if (handlename == "sectionhandle") {
																//grab the SectionDragHandle
																InitDragOnControl(el, section);
																break;
															}
														}
													}						
													el = el.nextSibling;							
	
												}
												break;
												
											}				
											el = el.nextSibling;
												
										}
										break;

									}	
									el = el.nextSibling;									
																	
								}
								break;

							}
							el = el.nextSibling;									
															
						}
						break;

					}				
					el = el.nextSibling;	
				
											
				}
				
				// reset control's indexer
				cntControls = 0;
				cntSectionRows = 0
				
				//create first SectioRow
				var sectionRow = new SectionRow();
				
				// load position info
				var sectionID = section.id;
				
				var sectionTop = section.offsetTop + rowTop;
				var sectionLeft = section.offsetLeft + rowLeft;
				var sectionWidth = section.offsetWidth;
				var sectionRight = sectionLeft + sectionWidth;
				var sectionHeight = section.clientHeight;
				var sectionMiddle = sectionLeft + (sectionWidth/2);
				var sectionBottom = sectionTop + sectionHeight;
				
				var sectionVvID = section.getAttribute("vvfieldid");
				var sectionColumns = section.getAttribute("columns");
				//var sectionIndex = section.getAttribute("sectionindex");

				// Create FormSection
				form_section = new FormSection(sectionID, rowVvID, sectionVvID, sectionOffset, sectionTop, sectionBottom, sectionLeft, sectionRight, sectionHeight, sectionMiddle, sectionColumns, cntSections);
				
				var sectRowTop = 0;
				var currentsectionTop = rowTop;
				

				//=================================================================
				//	Get collection of Controls
				//================================================================                                
				var controls = section.getElementsByTagName("table");								
				for (var k = 0; k < controls.length; k++) { 
					var control = controls[k];
					
					// determine if this is a Form Control
					var controlvvdesigner = control.getAttribute("vvdesigner");

					if (controlvvdesigner != null) {
						if (controlvvdesigner == "control") {
	
							// load position info
							var controlID = control.id;
							var controlTop = control.offsetTop;
							var controlHeight = control.offsetHeight;
							var controlLeft = control.offsetLeft + sectionLeft + sectionOffset;
							var controlWidth = control.offsetWidth;
							var controlRight = controlLeft + controlWidth;
							var controlMiddle = controlLeft + (controlWidth/2);
							var controlVvID = control.getAttribute("vvfieldid");
							var controlIndex = control.getAttribute("vvfieldindex");
							var controlType = control.getAttribute("vvfieldtype");
							
							var handleid = "";
							var handlectrl = GetDragHover(control);
							if (handlectrl != null) {
								handleid = handlectrl.id;
							}
							
							//Determine the rows inside the Section
							if (cntControls == 0) {
								// First control of first row, setup trigger
								sectRowTop = controlTop;
								
								// setup th etop osition for first row controls
								currentsectionTop = rowTop + controlTop;
							} else {
								// Not the first control
								if (controlTop > sectRowTop) {
									// top is bigger than previous so it must be a new row
									
									//configure SectionRow
									if (cntSectionRows == 0) {
										sectionRow.Top = rowTop
									} else {
										sectionRow.Top = currentsectionTop;
									}
									
									sectionRow.Bottom = (rowTop + controlTop) - 1;
									sectionRow.Left = sectionLeft;
									sectionRow.Right = sectionRight;			
									
									//increase top position appropriately
									currentsectionTop = rowTop + controlTop;				
									
									//save SectionRow
									form_section.SectionRows[cntSectionRows] = sectionRow;
									
									//Create new Section Row
									sectionRow = new SectionRow();
									//zero control indexer
									cntControls = 0;
									// increment SectionRow indexer
									cntSectionRows = cntSectionRows + 1;
									
									//reset trigger
									sectRowTop = controlTop;
								}						
							}
							
							// create FormControl
							form_control = new FormControl(controlID, controlType, controlVvID, controlIndex, currentsectionTop, controlHeight, controlLeft, controlRight, controlMiddle, sectionVvID, handleid);
							//form_control.InnerHTML = control.innerHTML;
							
							if (controlType != 7) {
								// Initialize the Drag and Drop code
								InitDragOnControl(handlectrl, control);
								
								//control.onmouseover	= HoverDragImageOnControl;
								//control.onmouseout = HoverDragImageOnControlOff;
								//SetupHoverImageOnControl(control);
							}
							
							// populate the Controls collection       
							sectionRow.Controls[cntControls] = form_control;

							// increment Controls indexer
							cntControls = cntControls + 1;
						}
					}
				}
				//configure last SectionRow
				sectionRow.Top = rowTop + sectRowTop;
				sectionRow.Bottom = rowBottom;
				sectionRow.Left = sectionLeft;
				sectionRow.Right = sectionRight;
				
				// Save SectionRow
				form_section.SectionRows[cntSectionRows] = sectionRow;

				// Save the Section in the Sections collection of the current Row
				form_row.Sections[cntSections] = form_section;
				
				// increment the FormSection indexer
				cntSections = cntSections + 1;
			}
		}
	}
	
	// populate the Rows collection of the global variable FormControlHolder
	formCtrlHolder.Rows[cntRows] = form_row;

}

function GetMainOffset() {
	var mainOffset = 55;
	
	if (formCtrlHolder.Property.IsDocked == true) {
		if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			mainOffset = 55;
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			mainOffset = 60;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			mainOffset = 60;
		}
	} else {
		if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			mainOffset = 55;
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			mainOffset = 60;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			mainOffset = 60;
		}

	}
	
	return mainOffset;
}

function GetDragHover(control) {
	var handlectrl;

	//walk the DOM to grab handle to the SectionDragHandle
	var el = control.firstChild;
			
	while (el) {
		if (el.nodeName.toLowerCase() == "tbody") {
			el = el.firstChild;
			
			while (el) {
				if (el.nodeName.toLowerCase() == "tr") {
					el = el.firstChild;
					
					while (el) {
						if (el.nodeName.toLowerCase() == "td") {
							el = el.nextSibling;
						
							while (el) {
								if (el.nodeName.toLowerCase() == "td") {
									el = el.firstChild;
									
									while (el) {
										if (el.nodeName.toLowerCase() == "div") {
											var handlename = el.getAttribute("vvdraghover");
											
											if (handlename != null) {
												if (handlename == "0") {
													//grab the Control DragHandle
													handlectrl = el;
													break;
												}
											}
										}						
										el = el.nextSibling;							

									}
									break;
									
								}
								el = el.nextSibling;
								
							}
							break;
						}
						el = el.nextSibling;
						
					}
					break;

				}	
				el = el.nextSibling;									
												
			}
			break;

		}
		el = el.nextSibling;									
										
	}

	return handlectrl;
}


function DoMoveControl(formControl, SectionID, newIndex) {
	//document.location=document.location;

	MoveControl(formControl.VvID,SectionID,newIndex);
}

function DoResponse() {
	document.forms[0].submit();
	//location.reload(true);
	//document.submit();
	//document.location=document.location;
}


function getFormID() {
	var formID = "";
	
	if (document.getElementById('inputFormID') != null) {
		formID = document.getElementById('inputFormID').value;
	}
	
	return formID;
}

///	***********************************
/// ***	  Class FormControlHolder
///	***********************************
function FormControlHolder() {
	this.Rows = new Array();
	this.Menu = null;
	this.Property = null;
	this.Clipboard = null;
	
	//new FormControlHolder();
	
	FormControlHolder.prototype.getControl = FormControlHolder_getControl;
	
	FormControlHolder.prototype.getRow = FormControlHolder_getRow;
	FormControlHolder.prototype.getRowByVvId = FormControlHolder_getRowByVvId;
	
	FormControlHolder.prototype.getSection = FormControlHolder_getSection;
	FormControlHolder.prototype.getSectionByVvId = FormControlHolder_getSectionByVvId;
	
	FormControlHolder.prototype.getControlById = FormControlHolder_getControlById;
	FormControlHolder.prototype.getControlByVvId = FormControlHolder_getControlByVvId;
	FormControlHolder.prototype.setDragInvitation = FormControlHolder_setDragInvitation;
	FormControlHolder.prototype.setRowDragInvitation = FormControlHolder_setRowDragInvitation;
	FormControlHolder.prototype.setSectionDragInvitation = FormControlHolder_setSectionDragInvitation;
	
	
	
	
}
function FormControlHolder_setDragInvitation(dropTop, dropLeft) {
	for (var a = 0; a < this.Rows.length; a++) {
		var row = this.Rows[a];
		
		if (dropTop >= row.Top && dropTop <= row.Bottom) {
			return row.setDragInvitation(dropTop, dropLeft);
			
			//break;
		}
	}
}

function FormControlHolder_setRowDragInvitation(dropTop, dropLeft) {
	for (var a = 0; a < this.Rows.length; a++) {
		var row = this.Rows[a];
		
		if (dropTop >= row.Top && dropTop <= row.Bottom) {
			row.setRowDragInvitation(dropTop, dropLeft);
			return row;
			
			//break;
		}
	}
}

function FormControlHolder_setSectionDragInvitation(dropTop, dropLeft) {
	for (var a = 0; a < this.Rows.length; a++) {
		var row = this.Rows[a];
		
		if (dropTop >= row.Top && dropTop <= row.Bottom) {
			return row.setSectionDragInvitation(dropTop, dropLeft);
			
			//break;
		}
	}
}

function FormControlHolder_getControl(top, left){
	this.Rows.sort();
		
	for (var a = 0; a < this.Rows.length; a++) {
		var row = this.Rows[a];
		
		if (top >= row.Top && top <= row.Bottom) {
			var control = row.getControl(top, left);
			
			if (control != null) {
				return control;
			}
		}
	}
}

function FormControlHolder_getRow(top, left){
	this.Rows.sort();
		
	for (var a = 0; a < this.Rows.length; a++) {
		var row = this.Rows[a];
		
		if (top >= row.Top && top <= row.Bottom) {
			window.status="top=" + top + " left=" + left + " Row.Top=" + row.Top + " Row.Bottom=" + row.Bottom;
			return row;
		}
	}
}
function FormControlHolder_getRowByVvId(id){
	this.Rows.sort();
		
	for (var a = 0; a < this.Rows.length; a++) {
		var row = this.Rows[a];
		
		if (row.VvID.toLowerCase() == id.toLowerCase()){
			return row;
		}
	}
}

function FormControlHolder_getSection(top, left){
	this.Rows.sort();
		
	for (var a = 0; a < this.Rows.length; a++) {
		var row = this.Rows[a];
		
		if (top >= row.Top && top <= row.Bottom) {
			return row.getSection(top, left);
		}
	}
}
function FormControlHolder_getSectionByVvId(id){
	this.Rows.sort();
		
	for (var a = 0; a < this.Rows.length; a++) {
		var row = this.Rows[a];
		
		var section = row.getSectionByVvId(id);
		if (section != null)
			return section;
	}
}

function FormControlHolder_getControlByVvId(id){
	this.Rows.sort();
		
	for (var a = 0; a < this.Rows.length; a++) {
		var row = this.Rows[a];
		
		var control = row.getControlByVvId(id);
		
		if (control != null) {
			return control;
		}
	}
}

function FormControlHolder_getControlById(id){
	this.Rows.sort();
		
	for (var a = 0; a < this.Rows.length; a++) {
		var row = this.Rows[a];
		
		var control = row.getControlById(id);
		
		if (control != null) {
			return control;
		}
	}
}


///	***********************************
/// ***	     Class PropertyWindow
///	***********************************
function PropertyWindow(id, top, left) {
	this.ID = id;
	this.Top = top;
	this.Left = left;
	this.IsDocked = true;

	
	//this.MenuItems = new Array;
	
	//MenuSelector.prototype.getMenuItemByType = MenuSelector_getMenuItemByType;
	//MenuSelector.prototype.getMenuItemByID = MenuSelector_getMenuItemByID;
	//FieldSelector.prototype.getColumnIndex = FieldSelector_getColumnIndex;
}



///	***********************************
/// ***	     Class Clipboard
///	***********************************
function Clipboard(id, top, bottom, left, right, menuid) {
	this.ID = id;
	this.VvID = "Clipboard"

	this.Top = top;
	this.Bottom = bottom;
	this.Left = left;
	this.Right = right;
	
	this.MenuListID = menuid;

	this.MenuHasMoved = false;
	
	this.ClipItems = new Array;

	Clipboard.prototype.IsHoverOnClipboard = Clipboard_IsHoverOnClipboard;
	Clipboard.prototype.DisplayDragInvitation = Clipboard_DisplayDragInvitation;
	Clipboard.prototype.RemoveDragInvitation = Clipboard_RemoveDragInvitation;
	Clipboard.prototype.getClipItemByID = Clipboard_getClipItemByID;
	
}

function Clipboard_IsHoverOnClipboard(top, left) {
	if (this.MenuHasMoved == true) {
		var menulist = document.getElementById(this.MenuListID);
		var clipbrd = document.getElementById(this.ID);
		
		if (menulist != null && clipbrd != null) {
			this.Top = clipbrd.offsetTop + menulist.offsetTop + menulist.offsetParent.offsetTop;
			this.Left = clipbrd.offsetLeft + menulist.offsetLeft;
			
			var clipDropWidth = clipbrd.offsetWidth;
			this.Right = this.Left + clipDropWidth;
			var clipDropHeight = clipbrd.clientHeight;
			this.Bottom = this.Top + clipDropHeight;
			
			
			//reset flag to calculate new position
			this.MenuHasMoved = false;
		}	
	} 


	if (left >= this.Left && left <= this.Right) {
		if (top >= this.Top && top <= this.Bottom) {
			return true;
		}
	}
	return false;
}

function Clipboard_RemoveDragInvitation() {
	RemoveWhiteDragInvitation('divClipHighlight');
}


function Clipboard_DisplayDragInvitation() {
	DisplayWhiteDragInvitation('divClipHighlight');
}

function Clipboard_getClipItemByID(id) {
	for (var a = 0; a < this.ClipItems.length; a++) {
		var clipitem = this.ClipItems[a];
		
		if (clipitem.ID == id) {
			return clipitem;
		}
	}
}


///	***********************************
/// ***	     Class ClipItem
///	***********************************
function ClipItem(id, vvid, top, bottom, left, right, height, middle, ctrltype) {
	this.ID = id;
	this.VvID = vvid;
	
	this.Top = top;
	this.Bottom = bottom;
	this.Left = left;
	this.Right = right;
	this.Height = height;
	this.Middle = middle;
	
	this.ControlType = ctrltype;
}



///	***********************************
/// ***	     Class FieldSelector
///	***********************************
function MenuSelector(id, top, left) {
	this.ID = id;
	this.Top = top;
	this.Left = left;
	this.IsDocked = true;

	
	this.MenuItems = new Array;
	
	MenuSelector.prototype.getMenuItemByType = MenuSelector_getMenuItemByType;
	MenuSelector.prototype.getMenuItemByID = MenuSelector_getMenuItemByID;
	//FieldSelector.prototype.getColumnIndex = FieldSelector_getColumnIndex;
}
function MenuSelector_getMenuItemByType(menutype) {

	for (var a = 0; a < this.MenuItems.length; a++) {
		var menuitem = this.MenuItems[a];
		
		if (menuitem.MenuType == menutype) {
			return menuitem;
		}
	}
}
function MenuSelector_getMenuItemByID(id) {

	for (var a = 0; a < this.MenuItems.length; a++) {
		var menuitem = this.MenuItems[a];
		
		if (menuitem.ID == id) {
			return menuitem;
		}
	}
}


///	***********************************
/// ***	     Class MenuItem
///	***********************************
function MenuItem(id, top, bottom, left, right, height, middle, menutype, parttype) {
	this.ID = id;
	
	this.Top = top;
	this.Bottom = bottom;
	this.Left = left;
	this.Right = right;
	this.Height = height;
	this.Middle = middle;
	
	this.MenuType = menutype;
	this.PartType = parttype;
	
	//FieldSelector.prototype.getControl = FieldSelector_getControl;
	//FieldSelector.prototype.getControlByVvId = FieldSelector_getControlByVvId;
	//FieldSelector.prototype.getColumnIndex = FieldSelector_getColumnIndex;
}




///	***********************************
/// ***	      Class FormRow
///	***********************************
function FormRow(id, vvid, top, bottom, left, right, height, middle, rowindex) {
	this.ID = id;
	this.VvID = vvid;
	
	this.Top = top;
	this.Bottom = bottom;
	this.Left = left;
	this.Right = right;
	this.Height = height;
	this.Middle = middle;
	
	this.Index = rowindex;
	this.InnerHTML = "";
	
	this.Sections = new Array();

	FormRow.prototype.getControl = FormRow_getControl;
	
	FormRow.prototype.getSection = FormRow_getSection;
	FormRow.prototype.getSectionByVvId = FormRow_getSectionByVvId;
	
	FormRow.prototype.getControlById = FormRow_getControlById;
	FormRow.prototype.getControlByVvId = FormRow_getControlByVvId;
	FormRow.prototype.setDragInvitation = FormRow_setDragInvitation;
	FormRow.prototype.setRowDragInvitation = FormRow_setRowDragInvitation;
	FormRow.prototype.DisplayDragInvitation = FormRow_DisplayDragInvitation;
	
	FormRow.prototype.RemoveDragInvitation = FormRow_RemoveDragInvitation;
	FormRow.prototype.setSectionDragInvitation = FormRow_setSectionDragInvitation;
	
}

function FormRow_setDragInvitation(top, left) {
	this.Sections.sort();
	
	if (this.Sections.length > 0) {
		for (var a = 0; a < this.Sections.length; a++) {
			var section = this.Sections[a];
			
			if (left >= section.Left && left <= section.Right) {
				return section.setDragInvitation(top, left);
				//break;
			}
		}	
	}
}
function FormRow_setSectionDragInvitation(top, left) {
	this.Sections.sort();

	if (this.Sections.length > 0) {
		for (var a = 0; a < this.Sections.length; a++) {
			var section = this.Sections[a];
			
			if (left >= section.Left && left <= section.Right) {
				section.setSectionDragInvitation(top, left);
				return section;
				//break;
			}
		}	
	}
}

function FormRow_setRowDragInvitation(top, left) {
	DisplayDragInvitation(this.ID);
}
function FormRow_DisplayDragInvitation() {
	DisplayDragInvitation(this.ID);
}
function FormRow_RemoveDragInvitation() {
	RemoveDragInvitation(this.ID);
}

function FormRow_getControl(top, left) {
	this.Sections.sort();

	if (this.Sections.length > 0) {
		for (var a = 0; a < this.Sections.length; a++) {
			var section = this.Sections[a];
			
			if (left >= section.Left && left <= section.Right) {
				return section.getControl(top, left);
			}
		}	
	}
}

function FormRow_getSection(top, left){
	this.Sections.sort();

	if (this.Sections.length > 0) {
		for (var a = 0; a < this.Sections.length; a++) {
			var section = this.Sections[a];
			
			if (left >= section.Left && left <= section.Right) {
				return section;
			}
		}	
	}
}
function FormRow_getSectionByVvId(id){
	this.Sections.sort();

	if (this.Sections.length > 0) {
		for (var a = 0; a < this.Sections.length; a++) {
			var section = this.Sections[a];
			
			if (section.VvID.toLowerCase() == id.toLowerCase()){
				return section;
			}
		}	
	}
}

function FormRow_getControlByVvId(id) {
	this.Sections.sort();

	if (this.Sections.length > 0) {
		for (var a = 0; a < this.Sections.length; a++) {
			var section = this.Sections[a];
			
			var control = section.getControlByVvId(id);
			
			if (control != null) {
				return control;
			}
		}	
	}
}

function FormRow_getControlById(id) {
	this.Sections.sort();

	if (this.Sections.length > 0) {
		for (var a = 0; a < this.Sections.length; a++) {
			var section = this.Sections[a];
			
			var control = section.getControlById(id);
			
			if (control != null) {
				return control;
			}
		}	
	}
}

function AddFormSectionRow(evt) {
	var x = evt.clientX;
	var y = evt.clientY;
	
	if (formCtrlHolder != null) {
		var section = formCtrlHolder.getSection(y, x);
		if (section != null) {
			section.AddRow();
		}
	}
}

function RemoveFormSectionRow(evt) {
	var x = evt.clientX;
	var y = evt.clientY;
	
	if (formCtrlHolder != null) {
		var section = formCtrlHolder.getSection(y, x);
		if (section != null) {
			section.RemoveRow();
		}
	}
}

function AddFormSectionColumn(evt) {
	var x = evt.clientX;
	var y = evt.clientY;
	
	if (formCtrlHolder != null) {
		var section = formCtrlHolder.getSection(y, x);
		if (section != null) {
			section.AddColumn();
		}
	}
}

function RemoveFormSectionColumn(evt) {
	var x = evt.clientX;
	var y = evt.clientY;
	
	if (formCtrlHolder != null) {
		var section = formCtrlHolder.getSection(y, x);
		if (section != null) {
			section.RemoveColumn();
		}
	}
}





///	***********************************
/// ***	     Class FormSection
///	***********************************
function FormSection(id, rowvvid, vvid, sectionoffset, top, bottom, left, right, height, middle, columns, sectionindex) {
	FormSection.prototype.Init = FormSection_Init;
	FormSection.prototype.getInnerHTML = FormSection_getInnerHTML;
	FormSection.prototype.getDIVInfo = FormSection_getDIVInfo;
	FormSection.prototype.getTable1Info = FormSection_getTable1Info;
	
	FormSection.prototype.setDragInvitation = FormSection_setDragInvitation;
	FormSection.prototype.setSectionDragInvitation = FormSection_setSectionDragInvitation;
	FormSection.prototype.DisplayDragInvitation = FormSection_DisplayDragInvitation;
	FormSection.prototype.RemoveDragInvitation = FormSection_RemoveDragInvitation;
	FormSection.prototype.getInsertionPoint = FormSection_getInsertionPoint;

	FormSection.prototype.DrawSection = FormSection_DrawSection;
	FormSection.prototype.DrawHeader = FormSection_DrawHeader;
	FormSection.prototype.DrawFooter = FormSection_DrawFooter;
	FormSection.prototype.DrawControlOpener = FormSection_DrawControlOpener;
	FormSection.prototype.DrawControlCloser = FormSection_DrawControlCloser;
	FormSection.prototype.DrawInvitation = FormSection_DrawInvitation;
	FormSection.prototype.DisplaySection = FormSection_DisplaySection;
	
	FormSection.prototype.getSectionRow = FormSection_getSectionRow;
	FormSection.prototype.getControl = FormSection_getControl;
	
	FormSection.prototype.getControlById = FormSection_getControlById;
	FormSection.prototype.getControlByVvId = FormSection_getControlByVvId;

	FormSection.prototype.AddRow = FormSection_AddRow;
	FormSection.prototype.RemoveRow = FormSection_RemoveRow;
	FormSection.prototype.AddColumn = FormSection_AddColumn;
	FormSection.prototype.RemoveColumn = FormSection_RemoveColumn;
	
	FormSection.prototype.AddDropCell = FormSection_AddDropCell;
	
	this.ID = id;
	this.RowVvID = rowvvid;
	this.VvID = vvid;
	this.SectionOffset = sectionoffset;
	
	this.Top = top;
	this.Bottom = bottom;
	this.Left = left;
	this.Right = right;
	this.Height = height;
	this.Middle = middle;
	
	this.ColumnCount = columns;
	this.Index = sectionindex;
	
	this.InnerHTML = "";
	
	this.Div1_ID = "";
	this.Div1_Align = "";
	
	this.Table1_ID = "";
	this.Table1_Style = "";
	
	this.SectionHeaderHTML = "";

	this.SectionRows = new Array();
	this.Controls = new Array();
	
	this.SectionBuilder = new StringBuilder();
	
	//this.Init(ctrl);
	
}


function FormSection_Init(ctrl) {
	//get original content
	this.getInnerHTML(ctrl);
	
	//get stuff about first div tag
	//this.getDIVInfo(ctrl);
	
	//get info about Section Table
	//this.getTable1Info(ctrl);
	
	
	
}
function FormSection_getInnerHTML(ctrl) {
	//get original content
	this.InnerHTML = ctrl.innerHTML;
}
function FormSection_getDIVInfo(ctrl) {
	//get stuff about first div tag
	var el = ctrl.firstChild;
	
	while (el) {
		if (el.nodeName.toLowerCase() == "div") {
			this.Div1_ID = el.getAttribute("id");
			this.Div1_Align = el.getAttribute("align");
			break;
		}
		el = el.nextSibling;
	}
}
function FormSection_getTable1Info(ctrl) {
	//get stuff about Section Table
	var el = ctrl.firstChild;
	
	while (el) {
		if (el.nodeName.toLowerCase() == "div") {
			el = el.firstChild;
			
			while (el) {
				if (el.nodeName.toLowerCase() == "table") {
					el = el.firstChild;
					
					while (el) {
						if (el.nodeName.toLowerCase() == "tbody") {
							el = el.firstChild;
							
							while (el) {
								if (el.nodeName.toLowerCase() == "tr") {
									el = el.nextSibling;
									
									while (el) {
										if (el.nodeName.toLowerCase() == "tr") {
											el = el.firstChild;
											
											while (el) {
										
												if (el.nodeName.toLowerCase() == "td") {
													el = el.firstChild;

													while (el) {
														if (el.nodeName.toLowerCase() == "table") {
															if (el.id != "") {
																this.Table1_ID = el.id;
															}
															break;

														}				
														el = el.nextSibling;							
																				
													}
													break;
													
												}				
												el = el.nextSibling;
												
											}
											break;

										}	
										el = el.nextSibling;									
																		
									}
									break;

								}
								el = el.nextSibling;									
																
							}
							break;

						}				
						el = el.nextSibling;							
												
					}
					break;

				}	
				el = el.nextSibling;									
												
			}
			break;

		}
		el = el.nextSibling;									
											
	}
}


function FormSection_setDragInvitation(top, left) {
	var hoverControl = null;
	
	if (this.SectionRows.length > 0) {
		for (var a = 0; a < this.SectionRows.length; a++) {
			var sectionRow = this.SectionRows[a];
			
			if (top >= sectionRow.Top && top <= sectionRow.Bottom) {
				hoverControl = sectionRow.getControl(left);

				if (hoverControl != null) {
					hoverControl.setDragInvitation();
					
					return hoverControl;			
				}

			}
		}	
	}
}
function FormSection_setSectionDragInvitation(top, left) {
	DisplayDragInvitation(this.ID);
}
function FormSection_DisplayDragInvitation() {
	DisplayDragInvitation(this.ID);
}
function FormSection_RemoveDragInvitation() {
	RemoveDragInvitation(this.ID);
}




function FormSection_getInsertionPoint(top, left, insertPoint) {
	this.SectionRows.sort();
	
	if (this.SectionRows.length > 0) {
		for (var a = 0; a < this.SectionRows.length; a++) {
			var sectionRow = this.SectionRows[a];
			
			if (top >= sectionRow.Top && top <= sectionRow.Bottom) {
				var colindex = sectionRow.getColumnIndex(left);
				if (colindex >= this.ColumnCount) {
					insertPoint.ColumnIndex = 0;
					insertPoint.RowIndex = a + 1;
				} else {
					insertPoint.ColumnIndex = colindex;
					insertPoint.RowIndex = a;
				}
				
				break;
			}
		}	
	}

	if (insertPoint.ColumnIndex >= 0) {
		return true;
	} else {
		return false;
	}
}




function FormSection_DrawSection(rowIndex, colIndex) {
	var colcount = 0;
	this.DrawHeader();
	
	if (rowIndex == 0 && colIndex == 0 && this.SectionRows.length == 0) {

		this.SectionBuilder.AppendText("<tr>");
		
		this.DrawControlOpener();				
		this.DrawInvitation();
		this.DrawControlCloser();
		
		this.SectionBuilder.AppendText("</tr>");
	
	} else {
		for (var a = 0; a < this.SectionRows.length; a++) {
			var sectionRow = this.SectionRows[a];

			for (var b = 0; b < sectionRow.Controls.length; b++) {
				var control = sectionRow.Controls[b];
				
				if (colcount == 0) {
					this.SectionBuilder.AppendText("<tr>");
				}

				if (a == rowIndex && b == colIndex) {
				
					this.DrawControlOpener();				
					this.DrawInvitation();
					this.DrawControlCloser();
					
					colcount++;
					
					if (colcount == this.ColumnCount) {
						this.SectionBuilder.AppendText("</tr>");
						colcount = 0;
					}
					
					if (colcount == 0) {
						this.SectionBuilder.AppendText("<tr>");
					}
				}
				
				if (currentDragControl.FormControl.VvID != control.VvID) {
					this.DrawControlOpener();
					control.DrawControl(this.SectionBuilder);
					this.DrawControlCloser();
					
					colcount++;				
				}

				
				if (colcount == this.ColumnCount) {
					this.SectionBuilder.AppendText("</tr>");
					colcount = 0;
				}
			}
		}
		
		if (colcount > 0) {
			this.SectionBuilder.AppendText("</tr>");
		}				
	}

	this.DrawFooter();
}


function FormSection_DrawHeader() {
	this.SectionBuilder.AppendText("<div id='" + this.Div1_ID + "' align='" + this.Div1_Align + "'><table cellspacing='0' cellpadding='0'><tbody><tr>");
	this.SectionBuilder.AppendText(this.SectionHeaderHTML);
	this.SectionBuilder.AppendText("</tr><tr><td><table id='" + this.Table1_ID + "' cellspacing='0' cellpadding='0' style='" + this.Table1_Style + "'><tbody>");
}

function FormSection_DrawFooter() {
	this.SectionBuilder.AppendText("</tbody></table></td></tr></tbody></table></div>");

}

function FormSection_DrawControlOpener() {
	this.SectionBuilder.AppendText("<td valign='top' align='center' style='height: 100%;'><table cellspacing='0' cellpadding='0'><tbody><tr valign='top'><td></td><td align='center'>");
}

function FormSection_DrawControlCloser() {
	this.SectionBuilder.AppendText("</td></tr></tbody></table></td>");
}

function FormSection_DrawInvitation() {
	this.SectionBuilder.AppendText("<div style='border-style: dotted; border-width: 1px; color: black; width: 200px; height: 40px' />");

}

function FormSection_DisplaySection() {
	var section = document.getElementById(this.ID);
	
	if (section != null) {
		section.innerHTML = this.SectionBuilder.toString();
	}
}



function FormSection_getSectionRow(top) {
	this.SectionRows.sort();

	if (this.SectionRows.length > 0) {
		for (var a = 0; a < this.SectionRows.length; a++) {
			var sectionRow = this.SectionRows[a];
			
			if (top >= sectionRow.Top && top <= sectionRow.Bottom) {
				return sectionRow;		
			}
		}	
	}
}

function FormSection_getControl(top, left) {
	this.SectionRows.sort();

	if (this.SectionRows.length > 0) {
		for (var a = 0; a < this.SectionRows.length; a++) {
			var sectionRow = this.SectionRows[a];
			
			if (top >= sectionRow.Top && top <= sectionRow.Bottom) {
				return sectionRow.getControl(left);		
			}
		}	
	}
}

function FormSection_getControlByVvId(id) {
	this.SectionRows.sort();

	if (this.SectionRows.length > 0) {
		for (var a = 0; a < this.SectionRows.length; a++) {
			var sectionRow = this.SectionRows[a];
			
			var control = sectionRow.getControlByVvId(id);
			
			if (control != null) {
				return control;
			}
		}	
	}
}

function FormSection_getControlById(id) {
	this.SectionRows.sort();

	if (this.SectionRows.length > 0) {
		for (var a = 0; a < this.SectionRows.length; a++) {
			var sectionRow = this.SectionRows[a];
			
			var control = sectionRow.getControlById(id);
			
			if (control != null) {
				return control;
			}
		}	
	}
}

function FormSection_AddRow(evt) {
	ModifySection("AddRow", this.VvID);	
}

function FormSection_RemoveRow(evt) {
	console.debug("FormSection_RemoveRow called");
}
function FormSection_AddColumn(evt) {
	console.debug("FormSection_AddColumn called");
}
function FormSection_RemoveColumn(evt) {
	console.debug("FormSection_RemoveColumn called");
}

function FormSection_AddDropCell(cell) {
	var div = document.createElement("div");
	
	if (div != null) {
		div.style.borderStyle = "dotted";
		div.style.borderWidth = "1px";
		div.style.color = "black";
		
		div.style.width = "100%";
		div.style.height = "40px";
		
		cell.appendChild(div);
	}
}

///	***********************************
/// ***	     Class SectionRow		***
///	***								***
function SectionRow() {
	this.Top = 0;
	this.Bottom = 0;
	this.Left = 0;
	this.Right = 0;
	
	this.Controls = new Array;
	
	SectionRow.prototype.getControl = SectionRow_getControl;
	SectionRow.prototype.getControlById = SectionRow_getControlById;
	SectionRow.prototype.getControlByVvId = SectionRow_getControlByVvId;
	SectionRow.prototype.getColumnIndex = SectionRow_getColumnIndex;
}

function SectionRow_getColumnIndex(left) {
	var currentLeft = this.Left;

	if (this.Controls.length > 0) {
		for (var a = 0; a < this.Controls.length; a++) {
			var control = this.Controls[a];
			
			var middle = control.Middle;
			
			if (left >= currentLeft && left <= control.Middle) {
				return a;
			} else {
				currentLeft = control.Middle;
			}
		}	
	}

	if (left >= currentLeft && left <= this.Right) {
		return this.Controls.length;
	} else {
		return -1;	
	}
}

function SectionRow_getControl(left) {
	this.Controls.sort();
	
	if (this.Controls.length > 0) {
		for (var a = 0; a < this.Controls.length; a++) {
			var control = this.Controls[a];
			
			if (left >= control.Left && left <= control.Right) {
				return control;		
			}
		}	
	}
}

function SectionRow_getControlByVvId(id) {
	this.Controls.sort();

	if (this.Controls.length > 0) {
		for (var a = 0; a < this.Controls.length; a++) {
			var control = this.Controls[a];
			
			if (control.VvID == id) {
				return control;
			}
		}
	}
}

function SectionRow_getControlById(id) {
	this.Controls.sort();

	if (this.Controls.length > 0) {
		for (var a = 0; a < this.Controls.length; a++) {
			var control = this.Controls[a];
			
			if (control.ID == id) {
				return control;
			}
		}
	}
}


///	***********************************
/// ***	   Class StringBuilder		***
///	***								***
function StringBuilder() {
	this.Text = "";

	StringBuilder.prototype.AppendText = StringBuilder_AppendText;
	StringBuilder.prototype.InsertText = StringBuilder_InsertText;
	StringBuilder.prototype.toString = StringBuilder_toString;
	StringBuilder.prototype.Clear = StringBuilder_Clear;
}
function StringBuilder_AppendText(text) {
	this.Text += text;
}
function StringBuilder_InsertText(text) {
	this.Text = text + this.Text;
}
function StringBuilder_toString() {
	return this.Text;
}
function StringBuilder_Clear() {
	this.Text = "";
}


///	***********************************
/// ***	     Class DragControl		***
///	***								***
function DragControl(dragcontroltype, control, mousex, mousey, controltype) {
	// DragControlTypes
	// Fields - 0
	// Sections - 1
	// Row - 2
	// NewControls = 3
	// NewRows = 4
	// NewSections = 5
	// MenuSelector = 6
	// PropertyWindow = 7
	// Clipboard = 8

	
	this.DragControlType = dragcontroltype;
	this.Control = control;
	this.X = mousex;
	this.Y = mousey;
	this.ControlType = controltype;
}

///	***********************************
/// ***	     Class DragPoint		***
///	***								***
function DragPoint(x,y) {
	this.X = x;
	this.Y = y;
}

///	***********************************
/// ***	     Class FormControl		***
///	***								***
function FormControl(id, controltype, vvid, index, top, height, left, right, middle, sectionid, handleid) {
	this.ID = id;
	this.ControlType = controltype;
	this.VvID = vvid;
	this.Index = index;
	this.Top = top;
	this.Left = left;
	this.Right = right;
	this.Height = height;
	this.Middle = middle;
	this.InnerHTML = "";
	this.SectionID = sectionid;
	this.HandleID = handleid; 
	
	FormControl.prototype.DrawControl = FormControl_DrawControl;
	FormControl.prototype.setDragInvitation = FormControl_setDragInvitation;
	FormControl.prototype.DisplayDragInvitation = FormControl_DisplayDragInvitation;
	FormControl.prototype.RemoveDragInvitation = FormControl_RemoveDragInvitation;
}

function FormControl_DrawControl(builder) {
	builder.AppendText("<div id='" + this.ID + "' style='border-style: solid; border-width: 1px; color: black; position: relative; left: 0px; top: 0px;' vvfieldindex='" + this.Index + "' vvfieldid='" + this.VvID + "' vvdesigner='control'>");
	builder.AppendText(this.InnerHTML);
	builder.AppendText("</div>");
}

function FormControl_setDragInvitation() {
	DisplayDragInvitation(this.ID);
}
function FormControl_DisplayDragInvitation() {
	DisplayDragInvitation(this.ID);
}

function FormControl_RemoveDragInvitation() {
	RemoveDragInvitation(this.ID);
}

///	***********************************
/// ***	     Class InsertionPoint	***
///	***								***
function InsertionPoint(rowIndex, colIndex) {
	this.RowIndex = rowIndex;
	this.ColumnIndex = colIndex;
}


function DisplayDragInvitation(id) {
	var el = document.getElementById(id);
	
	if (el != null) {
		el.style.border = "3px solid #9EBFF6";
	}
}

function DisplayWhiteDragInvitation(id) {
	var el = document.getElementById(id);
	
	if (el != null) {
		el.style.border = "3px solid #FFFFFF";
	}
}
function RemoveWhiteDragInvitation(id) {
	var el = document.getElementById(id);
	
	if (el != null) {
		el.style.border = "3px solid #4e7dd1";
	}
}

function RemoveDragInvitation(id) {
	var el = document.getElementById(id);
	
	if (el != null) {
		el.style.border = "1px none #000";
	}
}
function GetCurrentPage() {
	var el = document.getElementById("inputCurrentPage");
	
	if (el != null) {
		return el.value;
	} else {
		return "1";
	}
}

//============================================================================================================================
//MoveControlToClipboard(dragControl.VvID, IsCopy);
function MoveControlToClipboard(controlID, isCopyControl) {
	var encodedData = "&action1=action=MoveControlToClipboard;controlid=" + controlID + ";page=" + GetCurrentPage() + ";isCopy=" + isCopyControl;
	
	CallFormDesigner(encodedData);
}
function MoveControl(controlID,sectionID,newIndex, isCopyControl) {
	var encodedData = "&action1=action=MoveControl;controlid=" + controlID + ";sectionid=" + sectionID + ";index=" + newIndex + ";page=" + GetCurrentPage() + ";isCopy=" + isCopyControl;
	
	CallFormDesigner(encodedData);
}
function NewControl(controltype,sectionID,newIndex) {
	var encodedData = "&action1=action=NewControl;controltype=" + controltype + ";sectionid=" + sectionID + ";index=" + newIndex + ";page=" + GetCurrentPage();
	
	CallFormDesigner(encodedData);
} 

function MoveRowToClipboard(rowID, isCopyRow) {
	var encodedData = "&action1=action=MoveRowToClipboard;rowid=" + rowID + ";page=" + GetCurrentPage() + ";isCopy=" + isCopyRow;
	
	CallFormDesigner(encodedData);
}
function MoveRow(rowID, newIndex, isCopyRow) {
	var encodedData = "&action1=action=MoveRow;rowid=" + rowID + ";index=" + newIndex + ";page=" + GetCurrentPage() + ";isCopy=" + isCopyRow;
	
	CallFormDesigner(encodedData);
}
function NewRow(rowIndex) {
	var encodedData = "&action1=action=NewRow;index=" + rowIndex + ";page=" + GetCurrentPage();
	
	CallFormDesigner(encodedData);
}

function MoveSectionToClipboard(sectionID, isCopySection) {
	var encodedData = "&action1=action=MoveSectionToClipboard;sectionid=" + sectionID + ";page=" + GetCurrentPage() + ";isCopy=" + isCopySection;
	
	CallFormDesigner(encodedData);
}
function NewSection(rowID, newIndex) {
	var encodedData = "&action1=action=NewSection;rowid=" + rowID + ";index=" + newIndex + ";page=" + GetCurrentPage();
	
	CallFormDesigner(encodedData);
}
function MoveSection(sectionID, rowID, newIndex, isCopySection) {
	var encodedData = "&action1=action=MoveSection;sectionid=" + sectionID + ";rowid=" + rowID + ";index=" + newIndex + ";page=" + GetCurrentPage() + ";isCopy=" + isCopySection;
	
	CallFormDesigner(encodedData);
}
function ModifySection(action, sectionID) {
	var encodedData = "&action1=action=" + action + ";sectionid=" + sectionID + ";page=" + GetCurrentPage();
		
	CallFormDesigner(encodedData);
}


//============================================================================================================================


function AddMenuLocationToEncodedData(encodedData) {
	var s1 = "&action99=action=fieldselector;top=" + formCtrlHolder.Menu.Top + ";left=" + formCtrlHolder.Menu.Left + ";docked=" + formCtrlHolder.Menu.IsDocked;

	encodedData = encodedData + s1;
	
	return encodedData;
}
function AddPropertyLocationToEncodedData(encodedData) {
	if (formCtrlHolder.Property != null) {
		var s1 = "&action98=action=fieldproperty;top=" + formCtrlHolder.Property.Top + ";left=" + formCtrlHolder.Property.Left + ";docked=" + formCtrlHolder.Property.IsDocked;

		encodedData = encodedData + s1;	
	}	
	return encodedData;
}


//--------------------------------------------------------------------------------------------------------


function UpdateMenuPosition() {
	var encodedData = "&action99=action=fieldselector;top=" + formCtrlHolder.Menu.Top + ";left=" + formCtrlHolder.Menu.Left + ";docked=" + formCtrlHolder.Menu.IsDocked;
	
	CallFormDesignerNoPostBack(encodedData);
}
function UpdatePropertyPosition() {
	var encodedData = "&action98=action=fieldproperty;top=" + formCtrlHolder.Property.Top + ";left=" + formCtrlHolder.Property.Left + ";docked=" + formCtrlHolder.Property.IsDocked;
	
	CallFormDesignerNoPostBack(encodedData);
}


//============================================================================================================================


function CallFormDesigner(data) {
	var responseText=''; //we expect to get back a string containing comma separated name\value pairs
	var encodedData='';
	var formId = getFormID();
	
	var wait = document.getElementById("divDisplayWait");

	
	if (document.getElementById('inputFormDesignerHelperURL') != null) {
		var pageURL = document.getElementById('inputFormDesignerHelperURL').value;
		
		//append the formId to the querystring
		if (pageURL.indexOf("?")!=-1)
			pageURL += "formid=" + formId;
		else
			pageURL += "?formid=" + formId;
		
		//get the XmlHttpRequest object
		var xmlhttp = new GetXmlHttp();
		
		//encodedData = "&userpw=" + pwd + "&fieldid=" + fieldId + "&templateid=" + templateId;
		//encodedData = AddMenuLocationToEncodedData(data);
		//encodedData = AddPropertyLocationToEncodedData(encodedData);
		encodedData = data;
				
		
		// send the data
		if (xmlhttp) {	
		
			xmlhttp.onreadystatechange = function (){
						
				if (xmlhttp && xmlhttp.readyState==1){
					//request sent to server..
				
					//display loading message...
					if (wait)
					{					
						SetWaitToCenterOfScreen(wait);
						wait.style.display = "block";	
						var innerDiv = document.getElementById("divDisplayWaitContent");	
						innerDiv.innerHTML = "Loading...<img src='./images/spinner.gif' border='0'>";
					}
				}
		
				if (xmlhttp && xmlhttp.readyState==4){
					//we got something back..	
					
					//clear loading message...
					if (wait)
					{					
						setTimeout("ClearWait()", 1000);			
					}						
					
					//get response from the server
					if (xmlhttp.status==200) {			
						DoResponse();
						
						//responseText = xmlhttp.responseText;
						
						//if (responseText.length > 0){
							
						
							//go back to the function that called this function 
							//when the response is received from the server
							//if (callerName == 'DoUserIDStamp'){
							//	DoUserIDStamp(formId,element,responseText);
							//}else{
							//	DoUserIDStampAuth(element,responseText);
							//}
						//}								
					}
				}			
			}
			
			xmlhttp.open("POST",pageURL,true);
			//Prevent caching
			xmlhttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
			//make request appear as normal form post
			xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
			//encode the arguments and post to server	   
	   		xmlhttp.send(encodedData);
		}	

	}	
			
}

function CallFormDesignerNoPostBack(data) {
	var responseText=''; //we expect to get back a string containing comma separated name\value pairs
	var encodedData='';
	var formId = getFormID()
	
	var wait = document.getElementById("divDisplayWait");

	
	if (document.getElementById('inputFormDesignerHelperURL') != null) {
		var pageURL = document.getElementById('inputFormDesignerHelperURL').value;
		
		//append the formId to the querystring
		if (pageURL.indexOf("?")!=-1)
			pageURL += "formid=" + formId;
		else
			pageURL += "?formid=" + formId;
		
		//get the XmlHttpRequest object
		var xmlhttp = new GetXmlHttp();
		
		//encodedData = "&userpw=" + pwd + "&fieldid=" + fieldId + "&templateid=" + templateId;
		//encodedData = AddMenuLocationToEncodedData(data);
		//encodedData = AddPropertyLocationToEncodedData(data);
		encodedData = data;
				
		
		// send the data
		if (xmlhttp) {	
		
			xmlhttp.onreadystatechange = function (){
						
				if (xmlhttp && xmlhttp.readyState==1){
					//request sent to server..
				
					//display loading message...
					if (wait)
					{
						SetWaitToCenterOfScreen(wait);	
						wait.style.display = "block";	
						var innerDiv = document.getElementById("divDisplayWaitContent");	
						innerDiv.innerHTML = "Loading...<img src='./images/spinner.gif' border='0'>";			
					}
				}
		
				if (xmlhttp && xmlhttp.readyState==4){
					//we got something back..	
					
					//clear loading message...
					if (wait)
					{					
						setTimeout("ClearWait()", 1000);			
					}						
					
					//get response from the server
					if (xmlhttp.status==200) {			
						//DoResponse();
						
						//responseText = xmlhttp.responseText;
						
						//if (responseText.length > 0){
							
						
							//go back to the function that called this function 
							//when the response is received from the server
							//if (callerName == 'DoUserIDStamp'){
							//	DoUserIDStamp(formId,element,responseText);
							//}else{
							//	DoUserIDStampAuth(element,responseText);
							//}
						//}								
					}
				}			
			}
			
			xmlhttp.open("POST",pageURL,true);
			//Prevent caching
			xmlhttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
			//make request appear as normal form post
			xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
			//encode the arguments and post to server	   
	   		xmlhttp.send(encodedData);
		}	

	}	
			
}

/*
function SetupHoverImageOnControl(el) {
	if (el.addEventListener){
		el.addEventListener('mouseover', HoverDragImageOnControl, false);
		//el.addEventListener('mouseout', HoverDragImageOnControlOff, false);

	} else if (el.attachEvent){
		el.attachEvent('onmouseover', HoverDragImageOnControl(this));
		//el.attachEvent('onmouseout', HoverDragImageOnControlOff);
		
	}
}
*/

function HoverDragImageOnControl(el) {
		
	if (el != null) {
		var controlVvID = el.getAttribute("vvfieldid");
		if (controlVvID != null) {
			var control = formCtrlHolder.getControlByVvId(controlVvID);
			if (control != null) {
			
				//not valid for spacers
				if (control.ControlType != 7) {
					var handleid = control.HandleID;
					var handle = document.getElementById(handleid);
					
					if (handle != null) {
						
						//Tirns off any drag surface that is not the current control under the mouse pointer
						if (DragHandleArray.length > 0) {
							//if bottom element is not the most most current  control
							//turn it off and move to the array the timer uses to turn off the element
							if (DragHandleArray[0] != handleid) {
								//pull bottom element out and turn it off and move to the timer array
								var h_id = DragHandleArray.shift();
								
								//push onto the timer array
								DragHandleFadedArray.push(h_id);
								
								//turn off drag image
								TurnHoverDragImageOnControlOff(h_id);								
							}
						}						
						
						if (handle.style.display != "block") {
							handle.style.display = "block";
							
							if( typeof( window.innerWidth ) == 'number' ) {
								//Non-IE
								handle.style.top = el.offsetTop;
								handle.style.left = el.offsetParent.offsetLeft + el.offsetLeft + (el.offsetWidth - 19);

							} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
								//IE 6+ in 'standards compliant mode'
								//handle.style.top =
								handle.style.left = (el.offsetWidth - 19)
							} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
								//IE 4 compatible
								//handle.style.top =
								handle.style.left = (el.offsetWidth - 19)
							}
							

							// if not currently in the array set timer
							if (DragHandleArray.indexOf(handleid) == -1) {
								DragHandleArray.push(handleid);
								setTimeout("HoverDragImageOnControlOff()", HoverImageDelay);			
							}	
						}
					}
				}
			}	
		}	
	}
}


function HoverDragImageOnControlOff() {
	DragHandleFadedArray
	
	if (DragHandleFadedArray != null) {
		if (DragHandleFadedArray.length > 0) {
			var handleid = DragHandleFadedArray.shift();
		
			if (handleid != null) {
				var handle = document.getElementById(handleid);
				if (handle != null) {
					handle.style.display = "none";
				}
			}		
		
		} else {
		
			if (DragHandleArray != null) {
				var handleid = DragHandleArray.shift();
				if (handleid != null) {
					var handle = document.getElementById(handleid);
					if (handle != null) {
						handle.style.display = "none";
					}
				}
			}		
		}

	}
	
	


/*
	if (e != null) {
		if (e.target != null) {
			var el = e.target;
			
			var controlVvID = el.getAttribute("vvfieldid");
			if (controlVvID != null) {
				var control = formCtrlHolder.getControlByVvId(controlVvID);
				if (control != null) {
					var handleid = control.HandleID;
					var handle = document.getElementById(handleid);
					
					if (handle != null) {
						handle.style.display = "none";
											
						//handle.style.left = "0";
						//handle.style.top = "0";
					}
				}	
			}	
		}
	}	
	
	
	*/
}

function TurnHoverDragImageOnControlOff(handleid) {
	var handle = document.getElementById(handleid);
	if (handle != null) {
		handle.style.display = "none";
	}
}

function SetupWindowResizeEvent() {
	var divFdSurface = document.getElementById('divFdSurface');
	
	if (window.addEventListener){
		window.addEventListener('resize', SetFormDesignerDivSize, false);
		divFdSurface.addEventListener('scroll', RecordScrollPosition, false);
		
		window.addEventListener('load', Register_Controls, false);
	} else if (window.attachEvent){
		window.attachEvent('onresize', SetFormDesignerDivSize);
		divFdSurface.attachEvent('onscroll', RecordScrollPosition);
		
		window.attachEvent('onload', Register_Controls);
	}
}

function SetWaitToCenterOfScreen(el) {

		CenterDialogWindow(el);
		
		/*
		var winW = 0;
		var winH = 0;

		if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			winW = window.innerWidth;
			winH = window.innerHeight;
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			winW = document.documentElement.clientWidth;
			winH = document.documentElement.clientHeight;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			winW = document.body.clientWidth;
			winH = document.body.clientHeight;
		}
		
		el.style.top = (winH / 2) - 50;
		el.style.left = (winW / 2) - 100;
		*/

}

function ClearWait() {
	var wait = document.getElementById("divDisplayWait");
	if (wait != null) { wait.style.display = "none"; }
	
	var innerDiv = document.getElementById("divDisplayWaitContent");
	if (innerDiv != null) { innerDiv.innerHTML = ""; }
}

//calculate the appropriate height and width of the FormDesigner div tag that scrolls
function SetFormDesignerDivSize() {
	var divFdSurface = document.getElementById('divFdSurface');
	
	if (divFdSurface != null) {
		var winW = 0;
		var winH = 0;
		var totalWidth = 0;
		var totalHeight = 0;

		if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			winW = window.innerWidth;
			winH = window.innerHeight;
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			winW = document.documentElement.clientWidth;
			winH = document.documentElement.clientHeight;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			winW = document.body.clientWidth;
			winH = document.body.clientHeight;
		}

		//compute width of grid
		//totalWidth = parseInt(winW - 175);
		
		//set width on designer
		//divFdSurface.style.width = totalWidth;
		
		/*
		//computer the height of the grid
		if (document.getElementById('divMenuHeader') != null) 
		{
			menuHeaderHeight = document.getElementById('divMenuHeader').offsetHeight;
			if (menuHeaderHeight == null) {
				menuHeaderHeight = 0;
			}
		} else {
			menuHeaderHeight = 30;
		}
		
		if (document.getElementById('divMeasure') != null) {
			measureHeight = document.getElementById('divMeasure').offsetHeight;
			if (measureHeight == null) {
				measureHeight = 0;
			}
		} else {
			measureHeight = 0;
		}
		
		//compute final grid height
		totalHeight=winH - menuHeaderHeight - measureHeight - 110;
		if (SetHeight<100) {
			SetHeight=100;
		} 
		*/
		if (formCtrlHolder.Property.IsDocked == true)
			totalHeight = winH - 340;
		else
			totalHeight = winH - 110;
			
		
		//set height on designer
		if (totalHeight >= 30)
			divFdSurface.style.height = totalHeight;
		else
			divFdSurface.style.height = 30;
				
	}
}

function RecordScrollPosition() {
	var divFdSurface = document.getElementById('divFdSurface');
	
	if (divFdSurface != null) {
		var scrollTop = divFdSurface.scrollTop;
		
		var inputScrollPosition = document.getElementById('inputCurrentScrollPosition');
		if (inputScrollPosition != null) {
			inputScrollPosition.value = scrollTop;	
		}
	}
}

