﻿// JScript File

var maxHeight = 664;
var maxWidth = 700;

function initDrag(maxH) 
{
  maxHeight = maxH;
  if (document.layers) 
  {
    // turn on event capture for these events in NN4 event model
    document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);
    return;
  } 
  else if (document.body & document.body.addEventListener) 
  {
    // turn on event capture for these events in W3C DOM event model
    document.addEventListener("mousedown", engage, true);
    document.addEventListener("mousemove", dragIt, true);
    document.addEventListener("mouseup", release, true);       
    return;
  }
  document.onmousedown = engage;
  document.onmousemove = dragIt;
  document.onmouseup = release;   
  return;
}

function release(evt) 
{
	if (selectedObj)
	{
		if (document.body && document.body.releaseCapture) 
		{        
			// stop event capture in IE/Win          
			document.body.releaseCapture();                    
		}               
		selectedObj = null;             
	}
}

function mainObject(identifier,newNote)
{          
	this.resize = false; 
	this.isNew = false;                   

	if(newNote)    
	{    
		if(isIE())
		{
			this.container = identifier.parentNode;
			this.textArea = this.container.children[1].rows[0].cells[0].children[0];                
		}
		else
		{
			this.container = identifier.parentNode;            
			this.textArea = this.container.childNodes[3].getElementsByTagName("textarea")[0];            
		}        
		this.isNew = true;        
	}
	else
	{        
		if(isIE())
		{
			this.container = identifier.parentNode.parentNode.parentNode.parentNode;        
			this.textArea = this.container.children[1].rows[0].cells[0].children[0];    
		}
		else
		{
			this.container = identifier.parentNode.parentNode.parentNode.parentNode;                    
			this.textArea = this.container.childNodes[3].getElementsByTagName("textarea")[0];               
		}
	}

	this.content = (isIE()) ?
		this.container.children[0].rows[1].cells[1] :
		this.container.childNodes[1].rows[1].cells[1];
		//children[0].children[0].children[1].children[1];        
	this.id = this.container.id;
	this.controlID = (isIE()) ?
		this.container.children[3].innerHTML :
		this.container.childNodes[7].innerHTML;
	this.text = this.textArea.value;       
	this.textArea.onchange = textChanged;   

	if(isIE())
	{
		this.container.children[2].style.visibility = "visible"; 
		this.container.children[1].style.visibility = "visible";                     
	}
	else
	{
		this.container.childNodes[5].style.visibility = "visible"; 
		this.container.childNodes[3].style.visibility = "visible";                     
	}

	this.width = this.content.style.width;
	this.height = this.content.style.height;
	this.top = (this.container.style.top == "") ? "0px" : this.container.style.top;
	this.left = (this.container.style.left == "") ? "0px" : this.container.style.left;
	this.doRefresh = refreshMainObject;   
	this.doHide = hide;         
}

function hide()
{
	if(isIE())
	{
		if(this.container.children[2].style.visibility == "visible")
		{
			this.container.children[2].style.visibility = "hidden";             
			return true;
		}
	}
	else
	{
	 if(this.container.childNodes[5].style.visibility == "visible")
		{
			this.container.childNodes[5].style.visibility = "hidden";             
			return true;
		}
	}
	return false;
}
function textChanged()
{
	bookmark.text = this.value;
}

function refreshMainObject()
{            
  if(parseInt(this.top)<0) this.top = "0px";
  if(parseInt(this.left)<0) this.left = "0px";
  
  if(parseInt(this.width) < 10) this.width = "10px";
  if(parseInt(this.height) < 10) this.height = "10px";
  
     
  if(parseInt(this.top,10)+parseInt(this.height,10) >= maxHeight) 
  {        
		if(this.resize)
		{           
			var temp = maxHeight - parseInt(this.top,10);   
			this.height = temp + "px";                               
		}
		else
		{
			var temp = maxHeight - parseInt(this.height,10);
			this.top = temp + "px";
		}
  }
  if(parseInt(this.left,10)+parseInt(this.width,10) >= maxWidth)
  {        
    if(this.resize)
    {
      var temp = maxWidth - parseInt(this.left,10);
      this.width = temp + "px";
    }
    else
    {
      var temp = maxWidth - parseInt(this.width,10);
      this.left = temp + "px";
    }
  }
  
  this.container.style.top = this.top;
  this.container.style.left = this.left;
      
  
  this.content.style.width = this.width;
  this.content.style.height = this.height;
}

function dragIt(evt) 
{
	evt = (evt) ? evt : event;
	var x, y, width, height;      
	if (selectedObj && bookmark)
	{ 
		if (evt.pageX) 
		{
				x = evt.pageX - offsetX;
				y = evt.pageY - offsetY;
		} 
		else if (evt.clientX || evt.clientY) 
		{
				x = evt.clientX - offsetX;
				y = evt.clientY - offsetY;
		}      
	  
		var left = parseInt(bookmark.left,10);
		var top = parseInt(bookmark.top,10);
		var width = parseInt(bookmark.width,10);
		var height = parseInt(bookmark.height,10);               
	  
		if(selectedObj.id.indexOf("content") == -1)
		{          
				bookmark.resize = true;  
				switch(selectedObj.className)
				{
						case "rightBottomResize":                                 
								width += x - width - left;
								height += y - height - top;
								break;
						case "rightTopResize":
								width += x - width - left;
								height += top - y;  
								top = y;                  
								break;
						case "rightMidResize":
								width += x - width - left;                    
								break;
						case "midTopResize":  
								if(top==0 && y<0) break;            
								height += top - y;
								if(height > 10)
										top = y;
								break;
						case "leftTopResize": 
								if(top==0 && y<0 || left==0 && x<0) break;                           
								width += left - x;
								height += top - y; 
	              
								if(width > 10)  
										left = x;
								if(height > 10)
										top = y;
								break;
						case "leftMidResize":  
								if(left==0 && x<0) break;
								width += left - x;
								if(width > 10)
										left = x;
								break;
						case "leftBottomResize":
								if(top==0 && y<0 || left==0 && x<0) break;        
								width += left - x;
								height += y - height - top ;   
	              
								if(width > 10)  
										left = x;
								break;
						case "midBottomResize":                
								height += y - height - top ;   
								break;
				}
		}
		else
		{         
				bookmark.resize = false;  
				left = x;
				top = y;                               
		}
		bookmark.width = width + "px";            
		bookmark.height = height + "px";
		bookmark.top = top+"px";       
		bookmark.left = left+"px";
	      
		bookmark.doRefresh();          
	}    
}  
var selectedObj;
var offsetX, offsetY;
function engage(evt) {
	evt = (evt) ? evt : event;
	setSelectedElem(evt);

	if (selectedObj)
	{       
		if (evt.pageX) 
		{
				offsetX = evt.pageX - ((typeof selectedObj.offsetLeft != "undefined") ? selectedObj.offsetLeft : selectedObj.left);
				offsetY = evt.pageY - ((selectedObj.offsetTop) ? selectedObj.offsetTop : selectedObj.top);
		} 
		else if (typeof evt.clientX != "undefined") 
		{
				offsetX = evt.clientX - ((selectedObj.offsetLeft) ? selectedObj.offsetLeft : 0);
				offsetY = evt.clientY - ((selectedObj.offsetTop) ? selectedObj.offsetTop : 0);
		} 
	      
		offsetX -= parseInt(bookmark.left);
		offsetY -= parseInt(bookmark.top);
		return false;
	}   
}
var bookmark;
function setSelectedElem(evt) {    
	var target = (evt.target) ? evt.target : evt.srcElement;
	target = (target.nodeType && target.nodeType == 3) ? target.parentNode : target;    
	var divID = (target.tagName.indexOf("TD") != -1) ? target.id : "";
	if (divID) {
		if (document.layers) {
				selectedObj = document.layers[divID];        
		} else if (document.getElementById) {
				selectedObj = document.getElementById(divID);
		}
	 
		if(null == bookmark || (bookmark.content.id != divID))        
		{
				if(bookmark != null)
						bookmark.doHide();                       
				bookmark = new mainObject(selectedObj, false);
		}
		return;
	}    
	selectedObj = null;
	return;
}

function SetBookmark()
{
	var tables = document.getElementsByTagName("TABLE");
	for(var i=0;i<tables.length;i++)
	{
		if(tables[i].id.indexOf("description_") != -1 &&
			tables[i].style.visibility == "visible")
		{
			selectedObj = document.getElementById(tables[i].id);
			bookmark = new mainObject(selectedObj,true);
		}
	}
}

function SetAction(sender)
{
	var data;

	if(sender.id.indexOf("btnAdd") != -1) 
		data = "add";    
	else if(sender.id.indexOf("btnSave") != -1)                                                
	{    
		data = "save";
		data += "|"+bookmark.controlID +"|"+ bookmark.id + "|"+bookmark.top + "|"+
		bookmark.left+"|"+bookmark.height+"|"+bookmark.width+"|"+bookmark.text;
	}
	else if(sender.id.indexOf("btnDelete") != -1)                                                
	{           
		data = "delete";    
		data += "|"+bookmark.controlID + "|"+ bookmark.id;
	}
	document.getElementById("__action").value = data;    
	bookmark = null;
}

function isIE()
{
	if(navigator.userAgent.indexOf("MSIE") != -1)
		return true;
	return false;
}

function OnCancel()
{
	if(bookmark.isNew)
	{
		var element = document.getElementById(bookmark.container.id);
		bookmark = null;
		selectedObj = null;
		element.parentNode.removeChild(element);     
	}
	else
	{
		bookmark.doHide();
		bookmark = null;
		selectedObj = null;
  }
}
