/* for DefaultButton */
function fnTrapKD(btn, event){
	if (document.all) {
		if (event.keyCode == 13) {
			event.returnValue = false;
			event.cancel = true;
			btn.click();
			return false;
		}
	}
	else if (document.getElementById) {
		if (event.which == 13) {
			event.returnValue = false;
			event.cancel = true;
			btn.click();
			return false;
		}
	}
}
function fnTrapKDLB(btn, event){
	if (document.all) {
		if (event.keyCode == 13) {
			event.returnValue = false;
			event.cancel = true;
			__doPostBack(btn.id, '');
			return true;
		}
	}
	else if (document.getElementById) {
		if (event.which == 13) {
			event.returnValue = false;
			event.cancel = true;
			__doPostBack(btn.id, '');
			return true;
		}
	}
}

/* table context menu methods */
// object constructor
function ContextMenu(name, menuId, className) {
	// define object's properties
	this.name = name;
	this.className = className;
	this.isShown = false;
	this.menuId = menuId;
	ContextMenu.AllMenus.push(this);
}

// static methods
ContextMenu.AllMenus = new Array();
ContextMenu._autoHideInt = null;
ContextMenu.ShownCount = 0;
ContextMenu.AutoHideOn = false;
ContextMenu.autoHideHandler = new EventHandler(document, "mouseover", "autoHide", ContextMenu, false);
ContextMenu.autoHide = function()
{
	if (ContextMenu.ShownCount > 0)
		ContextMenu._autoHideInt = setTimeout("ContextMenu.hideAll()", 1500);
}
ContextMenu.hideAll = function()
{
	if (ContextMenu._autoHideInt != null)
		clearInterval(ContextMenu._autoHideInt);

	// close all menus
	for (var i = 0; i < ContextMenu.AllMenus.length; i++)
	{
		if (ContextMenu.AllMenus[i].isShown)
			ContextMenu.AllMenus[i].close();
	}
}

ContextMenu.prototype = 
{
	SubMenus: new Array(),
	
	setAlignLeft: function(b)
	{
		this._alignLeft = b;
	},
	close: function() 
	{
		if (ContextMenu._autoHideInt != null)
			clearInterval(ContextMenu._autoHideInt);

		// check for a parent menu and close it
		if (this.isShown == true)
		{
			if (! this._subMenu)
			{
				EventManager.remove(this.onKeyHandler);
				EventManager.remove(this.onMouseOutHandler);
				EventManager.remove(this.onMouseOverHandler);
				EventManager.remove(this.onClickHandler);
			}

			var pageBody = document.getElementsByTagName("body").item(0); // body of page
			var div = document.getElementById(this.name);
			var mybody = document.getElementsByTagName("body").item(0);
			mybody.removeChild(div);
			this.isShown = false;
			ContextMenu.ShownCount--;
		    
			// close all sub menus
			for (var i = 0; i < this.SubMenus.length; i++)
			{
				this.SubMenus[i].close();
			}
			// close parent
			if (this.parentMenu != null)
				this.parentMenu.close();
				
			if (ContextMenu.AutoHideOn == true)
			{
				EventManager.remove(ContextMenu.autoHideHandler);
				ContextMenu.AutoHideOn = false;
			}
		}
	},

	itemClicked: function(command, postback) 
	{
		this.close();
		// menu will be closed by document.onlclick
		if (postback == 'url')
			window.location.href = command;
		else if (postback == 'postback')
		{
			var args = this.parameterString.replace("&", "%26");
			__doPostBack(command, args);
		}
	},

	showSub: function(targ, parentMenu)
	{
		if (! this.isShown)
		{
			this._subMenu = true;
			this.show(targ, '', '');
			parentMenu.SubMenus.push(this);
			this.parentMenu = parentMenu;
		}
	},
    
	show: function(targ, strParams, strEnabled) 
	{
		this.parameterString = strParams;
		this.close();
    	
		// get menu
		var pageBody = document.getElementsByTagName("body").item(0); // body of page
		var div = document.createElement('div'); // create iframe
		div.style.position = 'absolute';
		div.style.display = "none";
		div.className = this.className;
		div.id = this.name;
		pageBody.appendChild(div);
    	
		var menu = document.getElementById(this.menuId);
		// replace params
		div.innerHTML = menu.innerHTML;
	    
		// replace the params
		if (strParams.length)
		{
			var paramArray = strParams.split(',');
			for (paramPos in paramArray)
			{
				if (paramArray[paramPos].length > 0)
				{
					var regex = new RegExp('\\$' + (parseInt(paramPos) + 1), 'g');
					div.innerHTML = div.innerHTML.replace(regex, paramArray[paramPos]);
				} // end param length check
			} // end for - split stParams
		} // end if strParams
	    
		// enable / disable items
		if (strEnabled)
		{
			var paramArray = strEnabled.split(',');
			for (paramPos in paramArray)
			{
				if (paramArray[paramPos].length > 0)
				{
					values = paramArray[paramPos].split(':');
					if (values[1] == "False")
					{
						//               table     -             tbody              -           row
						var obj = div.childNodes[0].getElementsByTagName('tbody')[0].childNodes[values[0]];
						if (obj.removeEventListener)
							obj.removeEventListener('click', obj.onclick, false);
						obj.onclick = null;
						div.childNodes[0].getElementsByTagName('tbody')[0].childNodes[values[0]].className = "disabled";
						
						// find link (a) and remove href
						if (obj.getElementsByTagName('a').length > 0)
						{
							var text = obj.getElementsByTagName('a')[0].innerHTML;
							obj.getElementsByTagName('td')[1].innerHTML = text;
							
						}
					}
				} // end param length check
			} // end for - split strEnabled
		} // end if strEnabled


		div.style.visibility = "hidden";
		div.style.display = "block";
		if (this._subMenu)
		{
			div.style.top = _findPosY(targ);

			div.style.left = _findPosX(targ) + targ.offsetWidth;
		}
		else
		{
			div.style.top = _findPosY(targ) + targ.offsetHeight;
			if (this._alignLeft)
				div.style.left = _findPosX(targ);
			else
				div.style.left = _findPosX(targ) + targ.offsetWidth - div.offsetWidth;
		}
		div.style.display = "";
		div.style.visibility = "visible";
		div.style.display = "block";

		this.element = div;
        
   		// our events
		this.onMouseOutHandler = new EventHandler(this.element, "mouseout", "onMouseOut", this, false);
		this.onMouseOverHandler = new EventHandler(this.element, "mouseover", "onMouseOver", this, false);
		if (! this._subMenu)
		{
			this.onKeyHandler = new EventHandler(document, "keypress", "escPressed", this, false);
			this.onClickHandler = new EventHandler(document, "click", "onClick", this, false);

			EventManager.add(this.onKeyHandler);
		}
		EventManager.add(this.onMouseOutHandler);
		EventManager.add(this.onMouseOverHandler);
       
		this.isShown = true;
		ContextMenu.ShownCount++;
	},
    
	onMouseOut: function(e)
	{
		if (! this._subMenu)
			EventManager.add(this.onClickHandler);

		if (ContextMenu.AutoHideOn == false)
		{
			// add an event on document to close menus
			EventManager.add(ContextMenu.autoHideHandler);
			ContextMenu.AutoHideOn = true;
		}
	},

	onMouseOver: function(e)
	{
		if (ContextMenu._autoHideInt != null)
			clearInterval(ContextMenu._autoHideInt);
		if (ContextMenu.AutoHideOn == true)
		{
			EventManager.remove(ContextMenu.autoHideHandler);
			ContextMenu.AutoHideOn = false;
		}
		if (! this._subMenu)
			EventManager.remove(this.onClickHandler);
	},
    
	onClick: function(e)
	{
		if (this.isShown)
			this.close();
	},

	escPressed: function(e) 
	{
		var code;
		if (e.keyCode) code = e.keyCode;
		else if (e.which) code = e.which;
    	
		if (code == 27)
		{
			this.close();
		}
	}
}


/*********************/
/* Events Classes */

function EventHandler(element, eventType, fn, obj, useCapture)
{
	this.useCapture = useCapture || false;
	this.element = element;
	this.eventType = eventType;
	var self = this;

	this.__fixE = function(event) {
		if (!event) event = window.event;
		
		if (event.target)
			if (event.target.nodeType == 3) event.target = event.target.parentNode;
		else if (event.srcElement)
			event.target = event.srcElement;

		return event;
	}
	
	this.callback = function(e) {
		e = self.__fixE(e);
		// call the eventhandler
		var args = new Array;
		args[0] = e;
		obj[fn].apply(obj, args);
	}
}

EventManager =
{
	add: function (eventHandler)
	{
		if (eventHandler.element.addEventListener)
		{
			eventHandler.element.addEventListener(eventHandler.eventType, eventHandler.callback, eventHandler.useCapture);
			return true;
		}
		else if (eventHandler.element.attachEvent)
			return eventHandler.element.attachEvent("on" + eventHandler.eventType, eventHandler.callback);
	},
    
	remove: function (eventHandler)
	{
		if (eventHandler.element.removeEventListener)
		{
			eventHandler.element.removeEventListener(eventHandler.eventType, eventHandler.callback, eventHandler.useCapture);
			return true;
		}
		else if (eventHandler.element.detachEvent)
			return eventHandler.element.detachEvent("on" + eventHandler.eventType, eventHandler.callback);
	}
}

/* End EventManager */

function hideDiv(id, imageId) {
	var obj = document.getElementById(id);
	var img = document.getElementById(imageId);
	if (obj.style.display == "none")
	{
		obj.style.display = "";
		if (img)
			img.setAttribute("src", "/jobitcommon/images/minus.gif");
	}
	else
	{
		obj.style.display = "none";
		if (img)
			img.setAttribute("src", "/jobitcommon/images/plus.gif");
	}
}


//	re_image = new RegExp(parentId + '__ctl([\\d]+)_' + imageId);
//	re_grid  = new RegExp(parentId + '__ctl([\\d]+)_' + gridId);
//	var arr_images = document.getElementsByTagName('img');
//	var arr_grids  = document.getElementsByTagName('table');
//	
//	if (arr_grids.length == 0)
//		return;
//	
//	var visible = true;
//	for(i = 0; i < arr_grids.length; i++) {
//		elm = arr_grids[i];
//		if (re_grid.test(elm.id)) {
//			visible = (elm.style.display == "none");
//			break;
//		}
//	}
//	
//	for(i = 0; i < arr_grids.length; i++) {
//		elm = arr_grids[i];
//		if (re_grid.test(elm.id)) {
//			if (visible)
//				elm.style.display = "";
//			else
//				elm.style.display = "none";
//		}
//	}
//	for(i = 0; i < arr_images.length; i++) {
//		elm = arr_images[i];
//		if (re_image.test(elm.id)) {
//			if (visible)
//				elm.setAttribute("src", "/jobitcommon/images/minus.gif");
//			else
//				elm.setAttribute("src", "/jobitcommon/images/plus.gif");
//		}
//	}


function showAllDivs(images, tables) {
	for (key in images)
	{
		document.getElementById(images[key]).setAttribute("src", "/jobitcommon/images/minus.gif");
	}
	for( key in tables)
	{
		document.getElementById(tables[key]).style.display = "table";
	}
}


// popup window

// object constructor
function Popup() {
	// define object's properties
	this.name = "";
	//this.init();
	
	this.eventObj = null;
	this.offset = 0;
	this.x = 0;
	this.y = 0;
	this.below = false;
	this.allowClose = false; // enables esc and document click to close
			
	// attach object's methods
	this.findPosX = _findPosX;
	this.findPosY = _findPosY;
}

function Popup(name) {
	// define object's properties
	this.name = name;
	//this.init();
	
	this.eventObj = null;
	this.offset = 0;
	this.x = 0;
	this.y = 0;
	this.below = false;
	this.allowClose = false; // enables esc and document click to close
			
	// attach object's methods
	this.findPosX = _findPosX;
	this.findPosY = _findPosY;
}

Popup.prototype = {
	init: function ()
	{
		this.eventObj = null;
		this.offset = 0;
		this.x = 0;
		this.y = 0;
		this.below = false;
		this.allowClose = false; // enables esc and document click to close
				
		// attach object's methods
		this.findPosX = _findPosX;
		this.findPosY = _findPosY;
	},
	
	setCenter: function (cen)
	{
		this.center = cen;
	},

	setOffset: function(os)
	{
		this.offset = os;
	},

	getEventObj: function()
	{
		return this.eventObj;
	},

	setEventObj: function(eventObj)
	{
		this.eventObj = eventObj;
	},

	setContent: function(content)
	{
		this.content = content;
		var div = document.getElementById(this.name + '_popupDiv_');
		if (div != null) {
			this.hide();
			this.show();
		}
	},

	setX: function(x)
	{
		this.x = x;
	},

	setY: function(y)
	{
		this.y = y;
	},

	setBelow: function(b)
	{
		this.below = b;
	},

	show: function()
	{
		var pageBody = document.getElementsByTagName("body").item(0); // body of page

		var iframe = document.createElement('iframe'); // create iframe
		
		if (browser('msie')) {
			pageBody.appendChild(iframe); // add to the page body

			iframe.id = this.name + '_popupFrame_';
			iframe.src = 'javascript:false;';
			iframe.frameBorder = 0;
			iframe.style.position = 'absolute';
			iframe.style.display = 'none';
		}

		var div = document.getElementById(this.name + '_popupDiv_');
		if (div == null) {
			div = document.createElement('div'); // create iframe
		}
		
		div.style.position = 'absolute';
		div.style.display = "none";

		div.id = this.name + '_popupDiv_';
		pageBody.appendChild(div); // add to the page body

		// show the popup
		//eventObj = document.getElementById(objId); // used to position the tooltip

		if ((this.content != null) && (this.content != "")) { // position and show tooltip
			div.innerHTML = this.content;
			
			if (browser('msie')) {
				g = window.document.body.scrollTop;
			}
			else {
				g = window.pageYOffset;
			}
		
			// weird hack to stop the start at left move to position
			div.style.visibility = "hidden";
			div.style.display = "block";

			if (this.center) {
				var top = ((pageBody.clientHeight / 2) + g) - (div.offsetHeight / 2);
				div.style.top  = top;
				div.style.left = (pageBody.clientWidth / 2) - (div.offsetWidth  / 2);
			}
			else {
				if (this.below) {
					div.style.top  = this.findPosY(this.eventObj) + (this.eventObj.offsetHeight + this.y);
					div.style.left = this.findPosX(this.eventObj) + this.x + this.offset;
				}
				else {
					var top = this.findPosY(this.eventObj) - (div.offsetHeight + this.x)
					if (top < 0)
						top = 0;
					div.style.top  = top;
					div.style.left = this.findPosX(this.eventObj) - ((div.offsetWidth - this.eventObj.offsetWidth) + this.y) + this.offset;
				}
			}

			if (browser('msie')) {
				iframe.style.width = div.offsetWidth;
				iframe.style.height = div.offsetHeight;
				iframe.style.left = div.style.left;
				iframe.style.top = div.style.top;
			}

			// weird hack to stop the popup from start at left the moving
			if (! this.center)
			{
				var l = parseInt(div.style.left);
				div.style.left = 10;
				var w = div.offsetWidth;
				div.style.left = l;
				if (l + w >= pageBody.clientWidth)
				{
					div.style.left = pageBody.clientWidth - (parseInt(div.style.left) + div.offsetWidth);
				}
			}
			div.style.display = "none";
			div.style.visibility = "visible";
			
			div.style.display = "block";
			iframe.style.display = 'block';
			
			this.element = div;
			
       		if (this.allowClose)
       		{
       			// our events
				this.onKeyHandler = new EventHandler(document, "keypress", "escPressed", this, false);
				this.onMouseOutHandler = new EventHandler(this.element, "mouseout", "onMouseOut", this, false);
				this.onMouseOverHandler = new EventHandler(this.element, "mouseover", "onMouseOver", this, false);
				this.onClickHandler = new EventHandler(document, "click", "onDocClick", this, false);
				
				EventManager.add(this.onKeyHandler);
				EventManager.add(this.onClickHandler);
				EventManager.add(this.onMouseOutHandler);
				EventManager.add(this.onMouseOverHandler);
			}
		}
		else {
			this.hide();
		}
	},

	hide: function()
	{
		var iframe = document.getElementById(this.name + '_popupFrame_');
		var div = document.getElementById(this.name + '_popupDiv_');
		var mybody = document.getElementsByTagName("body").item(0);

		if (iframe != null && browser('msie')) {
			mybody.removeChild(iframe);
		}

		if (div != null) {
			mybody.removeChild(div);
		}

		if (this.allowClose)
		{
			EventManager.remove(this.onKeyHandler);
			EventManager.remove(this.onMouseOutHandler);
			EventManager.remove(this.onMouseOverHandler);
			EventManager.remove(this.onClickHandler);
		}
	},
	
	// event handlers
	onMouseOut: function(e)
	{
		EventManager.add(this.onClickHandler);
		//this.autoHiding = setTimeout('this.close()', 2000);
	},

	onMouseOver: function(e)
	{
		//clearInterval(this.autoHiding);
		EventManager.remove(this.onClickHandler);
	},
    
	onDocClick: function(e)
	{
		this.hide();
	},

	escPressed: function(e) 
	{
		var code;
		if (e.keyCode) code = e.keyCode;
		else if (e.which) code = e.which;
    	
		if (code == 27)
			this.hide();
	}
}

/* helpers */

function _findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x) {
		curleft += obj.x;
	}
	return curleft;
}

function _findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y) {
		curtop += obj.y;
	}
	return curtop;
}

//
function browser(string)
{
	place = navigator.userAgent.toLowerCase().indexOf(string) + 1;
	thestring = string;
	return place;
}

//checks all DataGrid CheckBoxes with the given name with the given value
function CheckAllCheckBoxes(idarray) {
	for (key in idarray)
	{
		if(document.getElementById(idarray[key]).checked == true)
		{
			document.getElementById(idarray[key]).checked = false;
		}
		else
		{
			document.getElementById(idarray[key]).checked = true;
		}
	}
}

//common stuff for hoverhelp
document.getElementsByClassName = function (needle)
{
  var         my_array = document.getElementsByTagName("*");
  var         retvalue = new Array();
  var        i;
  var        j;

  for (i = 0, j = 0; i < my_array.length; i++)
  {
	var c = " " + my_array[i].className + " ";
	if (c.indexOf(" " + needle + " ") != -1)
	  retvalue[j++] = my_array[i];
  }
  return retvalue;
}
function addEvent(obj, evType, fn)
{
	if (obj.addEventListener)
	{
		obj.addEventListener(evType, fn, true);
		return true;
	} 
	else if (obj.attachEvent)
	{
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	} 
	else 
	{
		return false;
	}
}
