var currentWindow = self;

// opens a popup with max height and width and focuses it
function openPopup(uri,title,width,height)
{
	// do not stretch the popup higher than the available window height
	var maxheight = screen.availHeight-50;
	var maxwidth = screen.availWidth-100;

	// define a random number as internal title
	if (!title || title == '')
	{
		title = Math.round(Math.random()*1000);
	}
	
	// open window with parameters
	if(uri && uri != '')
	{
		// prevent opening a window which does not fits the screen height
		if(maxheight < height)
		  height=maxheight;
		if(maxwidth < width)
		  width=maxwidth;
		// class window opener
		var newWin = window.open(uri, title, "width="+width+",height="+height+",resizable=yes,status=yes");
		newWin.focus();
	}
}

// calls the common popup opener on pressing enter
function popupOnEnter(uri,title,width,height,e)
{
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;
	if(keycode == 13)
	{
	  openPopup(uri,title,width,height);
	  return false;
	}
	else
	  return true;
}

// opens an url in the window opener
function openUriInOpener(url)
{
	if(window.opener && window.opener.closed != true)
	{
	  window.opener.location.href = url;
	  window.opener.focus();
	}
}

// function to adjust height and width of a popup by outest elements
function adjustPopup(element,reresize)
{
	// default
	var height = 0, wheight = 0, width = 0, wwidth = 0;
	var contentObject = document.getElementById(element);
	var scrollObject = document.getElementById('autoscroll');
	var scrollbarwidth = 16;

	// do not stretch the popup higher than the available window height
	var maxheight = screen.availHeight-50;
	var maxwidth = screen.availWidth-100;

	// reresize for internet explorer inline window bars which where
	// loaded and shown with a short delay we recognize
	if(contentObject && scrollObject)
	{
		// determine height for element 
		height=getElementHeight(contentObject)+2;

		// set scrollbars if maxheight is reached
		if(maxheight < height)
		  height = maxheight;

		// determine height for window and resize window
		wheight = getWindowHeight();
		window.resizeBy(0,(height-wheight));
		setElementHeight(scrollObject,getWindowHeight());

		// determine width of scrollobject and current width of window
		width = getElementWidth(contentObject);
		wwidth = getWindowWidth();
		
		// add a scrollbar width if there are scrollbars
		if(maxheight < getElementHeight(contentObject))
		{
			width=width+scrollbarwidth;
		}
		if(maxwidth < width)
		  width = maxwidth;

		// change width if widths not null
		if (width > 0 && wwidth > 0)
		{
			window.resizeBy((width-wwidth),0);
		}

		// change height if height and width not null
		if (height > 0 && wheight > 0 && width > 0 && wwidth > 0 && reresize)
		{
			window.setTimeout(function ()
			{
				if(wheight != getWindowHeight() || wwidth != getWindowWidth())
				{
					adjustPopup(element,false);
				}
			}, 750);
		}
	}

	// init event handler to update scrollbar container
	window.onresize = adjustScroller;
}

// adjusts the scroll container size to get scrollbars on the full popup window
function adjustScroller()
{
	if(scrollObj = getObject('autoscroll'))
	{
		setElementWidth(scrollObj,getWindowWidth());
		setElementHeight(scrollObj,getWindowHeight());
	}
}

// set absolute position of layer to center the layer in the middle of the screen
function adjustContentLayer(positionElement)
{
	// defaults
	var npx=50, npy=50;
	var wpx = 0, wpy = 0, opx = 0, opy = 0;
	if(isObject(positionElement))
	  var posObj = positionElement;
	else
	 var posObj = getObject(positionElement);

	// set new position, width and height
	if(posObj)
	{
		// query top left window position 
		if(document.documentElement.scrollLeft)
		{
			wpx = document.documentElement.scrollLeft;
		}
		else if(document.body.scrollLeft)
		{
			wpx = document.body.scrollLeft;
		}
		else if(window.pageXOffset)
		{
			wpx = window.pageXOffset;
		}
		if(document.documentElement.scrollTop)
		{
			wpy = document.documentElement.scrollTop;
		}
		else if(document.body.scrollTop)
		{
			wpy = document.body.scrollTop;
		}
		else if(window.pageYOffset)
		{
			wpy = window.pageYOffset;
		}

		// query width and height of element to position
		opx = getElementWidth(posObj);
		opy = getElementHeight(posObj);

		// compute new position
		npx = Math.round(wpx+((getWindowWidth()-opx))/2);
		npy = Math.round(wpy+((getWindowHeight()-opy))/2);
		if(npy < 0) npy = 0;

		// set position
		setElementPosition(posObj,npx,npy);
	}
}

// adjusts only the layer frame and not its content
function adjustHideLayer(resizeElement,fromElement,toElement)
{
	// defaults
	if(isObject(resizeElement))
	  var sizObj = resizeElement;
	else
	  var sizObj = getObject(resizeElement);
	if(isObject(toElement))
	  var toObj = toElement;
	else
	  var toObj = getObject(toElement);
	var fromObj = null;
	if(fromElement)
	{
	  if(isObject(fromElement))
	    var fromObj = fromElement;
	  else
	    var fromObj = getObject(fromElement);
	}

	// set new position, width and height
	if(sizObj && toObj)
	{
		var fromPos, toPos;
		// set to page top if reference element (from) is not defined
		if(fromObj)
		  fromPos = fromObj.offsetTop;
		else
		  fromPos = 0;

		// determine toPos
		if(toObj.offsetTop-fromPos < getWindowHeight())
		  toPos = getWindowHeight();
		else
		  toPos = toObj.offsetTop-fromPos;

		// set size
		setElementHeight(sizObj,toPos);
		setElementWidth(sizObj,getWindowWidth());
	}
}

// adjust height of an element with optional offset
function adjustElement(dstElement,srcElement,offset)
{
	// get objects of element ids
	var dstObj = document.getElementById(dstElement);
	var srcObj = document.getElementById(srcElement);
	
	// adjust height
	if(dstObj && srcObj && offset)
	{
		setElementHeight(dstObj,(getElementHeight(srcObj)-offset));
	}
}

// get width of an element
function getElementWidth(object)
{
	var width = 0;
	if (object)
	{
		if (object.offsetWidth)
		{
			width=object.offsetWidth; 					
		} 
		else if (object.style.pixelWidth)
		{ 
			width=object.style.pixelWidth;			 
		}
	}
	return width;
}

// get height of an element
function getElementHeight(object)
{
	var height = 0;
	if (object)
	{
		if (object.offsetHeight)
		{
			height=object.offsetHeight; 					
		} 
		else if (object.style.pixelHeight)
		{ 
			height=object.style.pixelHeight;			 
		}
	}
	return height;
}

// set height of an element
function setElementHeight(object,height)
{
	if (object && height)
	{
		object.style.height = height+'px';
	}
}

// set width of an element
function setElementWidth(object,width)
{
	if (object && width)
	{
		object.style.width = width+'px';
	}
}

// set top left position of an element
function setElementPosition(object,x,y)
{
	if(x && y && object)
	{
		object.style.top = y+'px';
		object.style.left = x+'px';
	}
}

// determines the absolute position of an object by DOM object or DOM id
// returns:
// - object.x (horizontal position from 0 in pixels/integer)
// - object.y (vertical position from 0 in pixels/integer)
function getElementPosition(object)
{
	// locals
	var result = new Object();
	var x=0,y=0;
	
	// determine if input param is an object
	if(!isObject(object))
	  object = getObject(object);

	// determine position
	if (object) {
	while ((typeof(object)=="object")&&(typeof(object.tagName)!="undefined"))
	{
		y+=object.offsetTop;
		x+=object.offsetLeft;
		if(object.tagName.toUpperCase()=="BODY")
		 object=0;
		if(typeof(object)=="object")
		 if(typeof(object.offsetParent)=="object")
		  object=object.offsetParent;
	}
	}
	
	// and return
	result.x=x;
	result.y=y;
	return result;
}

// get height of the inner window
function getWindowHeight()
{
	var height=0;
	if (currentWindow.document.documentElement.clientHeight)
	{
		height = currentWindow.document.documentElement.clientHeight;
	}
	else if (currentWindow.document.body.clientHeight)
	{
		height = currentWindow.document.body.clientHeight;
	}
	else if (currentWindow.innerHeight)
	{
		height = currentWindow.innerHeight;
	}
	return height;
}

// get width of the inner window
function getWindowWidth()
{
	var width=0;
	if (currentWindow.document.documentElement.clientWidth)
	{
		width = currentWindow.document.documentElement.clientWidth;
	}
	else if (currentWindow.document.body.clientWidth)
	{
		width = currentWindow.document.body.clientWidth;
	}
	else if (currentWindow.innerWidth)
	{
		width = currentWindow.innerWidth;
	}
	return width;
}

// hide and unhide layers
function changeLayerVisibility(state,layer)
{
	if(isObject(layer))
	  var object = layer;
	else
	  var object = getObject(layer);
	if (state == 0 && !state && object)
	{
		object.style.display='none';
	}
	else
	{
		object.style.display='inline';
	}
}

// do not accept copypaste and autocomplete on a field in the input array
function preventSpecialUserInput(fields)
{
	var isIE,isMac = false; var object;
	if ((verOffset=navigator.userAgent.indexOf("MSIE 7"))!=-1) {
	 isIE = true;
	} else if ((verOffset=navigator.userAgent.indexOf("MSIE"))!=-1) {
	 isIE = true;
	} else if (navigator.userAgent.indexOf('Mac') != -1) {
	 isMac = true;
	}
	for(var i=0;i<fields.length;i++)
	{
		if(object = getObject(fields[i]))
		{
		  object.setAttribute('autocomplete','off');
		  object.oncontextmenu=function(){return false};
		  object.onkeydown=function(event){
			var pressed=isIE?window.event.keyCode:event.keyCode;
			var ctrl=isIE?window.event.ctrlKey:event.ctrlKey;
			var meta=false;
			if(isMac)
			 meta=event.metaKey?event.metaKey:false;
			if ((ctrl&&pressed=="86")||meta) {
			  return false;
			}
		  };
		}
	}	
}

// hack for firefox mac
function switchTextareaOverflow(flag,parent)
{
	var parentObject = getObject(parent);
	var childObjects = new Array();
	if(parentObject)
	{
		childObjects = parentObject.getElementsByTagName('TEXTAREA');
		if(childObjects)
		{
			for(i=0;i<childObjects.length;i++)
			{
				if(flag == '1')
				  childObject.style.overflow = 'auto';
				else
				  childObject.style.overflow = 'hidden';
			}
		}
	}
}

// sets the focus on a field
function focusField(field)
{
	if(obj = getObject(field))
	  obj.focus();
}

// returns a dom object by an id
function getObject(id)
{
	if(id && id != "")
	{
		return document.getElementById(id);
	}
}
function getOpenerObject(id)
{
	if(id && id != "" && window.opener && window.opener.closed != true)
	{
		return window.opener.document.getElementById(id);
	}
}

// checks if this is an object
function isObject(element)
{
	var result = false;
	if(typeof(element) == 'object' && element != null)
	  result = true;
	return result;
}

// global window variable
function setWindow(object)
{
	currentWindow = object;
}
function getWindow(object)
{
	return currentWindow;
}

/**
 * The method to setting url with JS code for <a> tag
 * example 
 * 	var js_var = 'test.com';
 *   ......
 * <a href="javascript:void(0)" onclick="javascript:setHrefForLink(this, 'http://google.com/search?site=' + js_var)">
 * @param obj
 * @param thePage
 * @return
 */
function setHrefForLink(obj, thePage) {
	if ((obj) && (thePage)) {
	   	obj.href = thePage;
	   	return true;
	} else {
	   	return false;
	}
}

// EOF