var timerID		// for 500ms delay on hover popup
var popupDelay = 750;

function elementContains(elmOuter, elmInner)
{
  while (elmInner && elmInner != elmOuter)
  {
    elmInner = elmInner.parentNode;
  }
  if (elmInner == elmOuter)
  {
    return true;
  }
  return false;
}
function getPageXY(elm)
{
  var point = { x: 0, y: 0 };
  while (elm)
  {
    point.x += elm.offsetLeft;
    point.y += elm.offsetTop;
    elm = elm.offsetParent;
  }
  return point;
}

function setPageXY(elm, x, y)
{
  var parentXY = {x: 0, y: 0 };

  if (elm.offsetParent)
  {
    parentXY = getPageXY(elm.offsetParent);
  }

  elm.style.left = (x - parentXY.x) + 'px';
  elm.style.top  = (y - parentXY.y) + 'px';
}

function clearPopup()
{
	if (timerID)
		clearTimeout(timerID);
	var div = document.getElementById('mouseover');
	if (div)
		div.style.display = 'none';

}

function showdescription(obj, content)
{
	if (timerID)
		clearTimeout(timerID);
	var e = window.event;
	var posx, posy;
	if (e.pageX || e.pageY)
	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY)
	{
		posx = e.clientX + document.body.scrollLeft;
		posy = e.clientY + document.body.scrollTop;
	}
	obj.onmouseout = function()
	{
		if (timerID)
		{
			clearTimeout(timerID);
			timerID = null;
		}
	};
	timerID = window.setTimeout("firepopup('" + obj.id + "', '" + content + "', " + posx + ", " + posy + ")", popupDelay);
}


function firepopup(objid, content, posx, posy)
{
	var div = document.getElementById('popupsummarydiv');
	var frm = document.getElementById('popupsummaryiframe');
	var loading = "<html><head><LINK media='all' href='../../theme.css' type='text/css' rel='stylesheet'></head><body class='popupsummary'>Loading...</body></html>";

	if (document.all)
		frm.contentWindow.document.write(loading);
	else
		frm.innerHTML = loading;
	frm.src = "itemsummary.aspx?itemid=" + content;
	div.style.position = 'absolute';
	div.style.width = '400px';
	frm.style.height = '100px';
	div.srcObjPosy = posy;
	if (div.offsetHeight > 450)
	{
		div.style.height = "450px";
//		div.style.overflowY = "scroll";
	}
	else
	{
		div.style.height = "";
//		div.style.overflowY = "";
	}

	div.style.left = posx + 10 + 'px';

	if (posy + div.offsetHeight > document.body.offsetHeight && posy > (document.body.offsetHeight / 2))
		posy -= div.offsetHeight;
	if (document.documentElement && document.documentElement.scrollTop)
    	posy += document.documentElement.scrollTop; // IE6 no doctype
    else if (document.body)
    	posy += document.body.scrollTop // IE5, Mozilla, IE6 with DOCTYPE

	if (posy < 0)
	    posy = 0;

	div.style.top = posy + 'px';

	div.style.display = 'block';

//	hideshowcovered(div, this.document, "hidden");
	
	div.onmouseout = function(e)
	{
          var relatedTarget = null;
          if (e)
          {
            relatedTarget = e.relatedTarget;
            // work around Gecko Linux only bug where related target is null
            // when clicking on menu links or when right clicking and moving
            // into a context menu.
			if (navigator.product == 'Gecko' && navigator.platform.indexOf('Linux') != -1 && !relatedTarget)
			{
				relatedTarget = e.originalTarget;
			}
          }
          else if (window.event)
          {
            relatedTarget = window.event.toElement;
          }
        if (elementContains(this, relatedTarget))
         {
           return false;
         }
		var div = document.getElementById('popupsummarydiv');
		div.style.display="none";
		hideshowcovered(div, this.document, "visible");
	};
/*	var list = document.getElementById('popuplist');
	list.style.left = posx + "px";
	list.style.top = posy + "px";
	list.style.paddingLeft = "0px";
	var pt = getPageXY(list);
	window.status = "x:"+pt.x+" y:" + pt.y;
	debugger;
	targMenu.style.postion = "absolute";
	targMenu.style.left = posx + "px";
	targMenu.style.top = posy + "px";
	targMenu.style.padding = "0px";*/
	return(false);
}

function adjustIFrameSize(iframeWindow) 
{
	var iframeElement, height, width;
	if (iframeWindow.document.height) 
	{
		iframeElement = document.getElementById(iframeWindow.name);
		height = iframeWindow.document.height;
		width = iframeWindow.document.width;
	}
	else if (document.all) 
	{
		iframeElement = document.all[iframeWindow.name];
		if (iframeWindow.document.compatMode &&
			iframeWindow.document.compatMode != 'BackCompat') 
		{
			height = iframeWindow.document.documentElement.scrollHeight + 5;
			width = iframeWindow.document.documentElement.scrollWidth + 5;
		}
		else 
		{
			height = iframeWindow.document.body.scrollHeight + 5;
			width = iframeWindow.document.body.scrollWidth + 5;
		}
	}

	width = '600px';
	if (height > 450)
		height = 450;
	height += 'px';
	iframeElement.style.height = height;
	iframeElement.style.width = width;
   	var div = document.getElementById('divHelp');
   	var posy = div.srcObjPosy;
	if (posy + div.offsetHeight > document.body.offsetHeight && posy > (document.body.offsetHeight / 2))
		posy -= div.offsetHeight;
		
	if (document.documentElement && document.documentElement.scrollTop)
    	posy += document.documentElement.scrollTop; // IE6 no doctype
    else if (document.body)
    	posy += document.body.scrollTop // IE5, Mozilla, IE6 with DOCTYPE
	
	if (posy < 0)
	    posy = 0;
	div.style.top = posy + 'px';

}


function ShowDescription(imgBtn, id)
{
	for(var row = imgBtn; row.nodeName != "TR"; row = row.parentNode);
	var tbody = row.parentNode;
	if (imgBtn.src.indexOf("plus") > -1)
	{	
		imgBtn.src = "../../images/minus.gif";
		var newRow = tbody.insertRow(row.rowIndex + 1);
		var newCell = newRow.insertCell(-1);
		newCell = newRow.insertCell(-1);
		newCell.colSpan = row.childNodes.length - 1;
		var iFrame = document.createElement("iframe");
		iFrame.style.width="100%";
		iFrame.src = "itemsummary.aspx?itemid=" + id +"&in=list";
		iFrame.id = "itemsummary" + id;
		newCell.appendChild(iFrame);
	}
	else
	{
		imgBtn.src = "../../images/plus.gif";
		tbody.removeChild(row.nextSibling);			
	}
}
/*******************************************************************
This is the javascript function that is invoked when the checkboxlist
is clicked.

Function:    disableListItems.
Inputs:        checkBoxListId - The id of the checkbox list.

            checkBoxIndex - The index of the checkbox to verify.
            i.e If the 4th checkbox is clicked and 
            you want the other checkboxes to be
            disabled the index would be 3.
            
            numOfItems - The number of checkboxes in the list.
Purpose:  Disables all the checkboxes when the checkbox to verify is
            checked.  The checkbox to verify is never disabled.
********************************************************************/
function disableListItems(checkBoxListId, checkBoxIndex, numOfItems)
{
    // Get the checkboxlist object.
    objCtrl = document.getElementById(checkBoxListId);
    
    // Does the checkboxlist not exist?
    if(objCtrl == null)
    {
        return;
    }

    var i = 0;
    var objItem = null;
    // Get the checkbox to verify.
    var objItemChecked = 
       document.getElementById(checkBoxListId + '_' + checkBoxIndex);

    // Does the individual checkbox exist?
    if(objItemChecked == null)
    {
        return;
    }

    // Is the checkbox to verify checked?
    var isChecked = objItemChecked.checked;
   
    // Loop through the checkboxes in the list.
    for(i = 0; i < numOfItems; i++)
    {
        objItem = document.getElementById(checkBoxListId + '_' + i);

        if(objItem == null)
        {
            continue;
        }

        // If i does not equal the checkbox that is never to be disabled.
        if(i != checkBoxIndex)
        {
            // Disable/Enable the checkbox.
            objItem.disabled = isChecked;
            // Should the checkbox be disabled?
            if(isChecked)
            {
                // Uncheck the checkbox.
                objItem.checked = false;
            }
        }
    }
}

function fnTrapKD(btn, event)
{
	if (document.all)
	{
		if (event.keyCode == 13)
		{
			event.returnValue=false;
			event.cancel = true;
			btn.click();
		}
	}
	else if (document.getElementById)
	{
		if (event.which == 13)
		{
			event.returnValue=false;
			event.cancel = true;
			btn.click();
		}
	}
	else if(document.layers)
	{
		if(event.which == 13)
		{
			event.returnValue=false;
			event.cancel = true;
			btn.click();
		}
	}
}

function minmax(objName, objImg)
{
	var obj = document.getElementById(objName);
	if (obj)
	{
		if (obj.style.display == "none")
		{
			obj.style.display = "inline";
			objImg.src = "..\\images\\minimize.gif";
		}
		else
		{
			obj.style.display = "none";
			objImg.src = "..\\images\\maximize.gif";
		}
	}
}

function maximize(objName, imgName)
{
	var obj = document.getElementById(objName);
	var objImg = document.getElementById(imgName);
	if (obj)
	{
		if (obj.style.display == "none")
		{
			obj.style.display = "inline";
			if (objImg)
				objImg.src = "..\\images\\minimize.gif";
		}
	}
}

function hideshowcovered(obj, doc, showhide)
{
	if (!doc.all)
		return;
		
	function getVisib(obj){
		var value = obj.style.visibility;
		if (!value) {
			if (document.defaultView && typeof (document.defaultView.getComputedStyle) == "function") { // Gecko, W3C
				if (!Calendar.is_khtml)
					value = document.defaultView.
						getComputedStyle(obj, "").getPropertyValue("visibility");
				else
					value = '';
			} else if (obj.currentStyle) { // IE
				value = obj.currentStyle.visibility;
			} else
				value = '';
		}
		return value;
	};

	var tags = new Array("applet", "iframe", "select");
	var el = obj;

	var p = getPageXY(el);
	var EX1 = p.x;
	var EX2 = el.offsetWidth + EX1;
	var EY1 = p.y;
	var EY2 = el.offsetHeight + EY1;

	for (var k = tags.length; k > 0; ) {
		var ar = doc.getElementsByTagName(tags[--k]);
		var cc = null;

		for (var i = ar.length; i > 0;) {
			cc = ar[--i];

			if (elementContains(obj, cc))
				continue;

			p = getPageXY(cc);
			var CX1 = p.x;
			var CX2 = cc.offsetWidth + CX1;
			var CY1 = p.y;
			var CY2 = cc.offsetHeight + CY1;

			if (this.hidden || (CX1 > EX2) || (CX2 < EX1) || (CY1 > EY2) || (CY2 < EY1)) {
				if (!cc.__msh_save_visibility) {
					cc.__msh_save_visibility = getVisib(cc);
				}
				cc.style.visibility = cc.__msh_save_visibility;
			} else {
				if (!cc.__msh_save_visibility) {
					cc.__msh_save_visibility = getVisib(cc);
				}
				cc.style.visibility = showhide;
			}
		}
	}
}

function elementContains(elmOuter, elmInner)
{
  while (elmInner && elmInner != elmOuter)
  {
    elmInner = elmInner.parentNode;
  }
  if (elmInner == elmOuter)
  {
    return true;
  }
  return false;
}

function getPageXY(elm)
{
  var point = { x: 0, y: 0 };
  while (elm)
  {
    point.x += elm.offsetLeft;
    point.y += elm.offsetTop;
    elm = elm.offsetParent;
  }
  return point;
}

function setPageXY(elm, x, y)
{
  var parentXY = {x: 0, y: 0 };

  if (elm.offsetParent)
  {
    parentXY = getPageXY(elm.offsetParent);
  }

  elm.style.left = (x - parentXY.x) + 'px';
  elm.style.top  = (y - parentXY.y) + 'px';
}

function openCenteredWindow(url, w, h)
{
    var left = (screen.width - w) / 2;
    var top = (screen.height - h) / 2;
    var args = 'location=no,menubar=no,titlebar=no,toolbar=no,scrollbars=yes,height=' + h + ',width=' + w + ',left=' + left + ',top=' + top;
    window.open(url, 'popup', args);
}

function refreshItemList()
{
    if (opener)
    {
      if (opener.__doPostBack)
            opener.__doPostBack('ibRefresh','');
        else if (opener.parent.__doPostBack)
            opener.parent.__doPostBack('ibRefresh','');
     }
     else if (parent && parent.__doPostBack)
         parent.__doPostBack('ibRefresh','');
}


function getWindowSize()
{
	var sz = { width: 0, height: 0 };
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		sz.width = window.innerWidth;
		sz.height = window.innerHeight;
	} 
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
	{
		//IE 6+ in 'standards compliant mode'
		sz.width = document.documentElement.clientWidth;
		sz.height = document.documentElement.clientHeight;
	} 
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
	{
		//IE 4 compatible
		sz.width = document.body.clientWidth;
		sz.height = document.body.clientHeight;
	}
	return sz;
}

var debugWin = null;

function onDebugClose()
{
	alert("closing"); 
	debugWin = null; 
}

function getDebugDoc()
{
	if (debugWin)
	{
		try
		{
			var testDoc = debugWin.document;
		}
		catch(e)
		{
			alert(e);
			debugWin = null;
		}
	}
	if (!debugWin)
	{
		attr = 'left=0,top=0,height=' + 600 + ',width=' + 600 + ',menubar=no,scrollbars=yes,status=yes,toolbar=no,location=no,resizeable=yes';
		debugWin = window.open("#",'popup', attr);
		debugWin.onunload = onDebugClose;
		debugWin.document.writeln("<html><body style='font-family:verdana; font-size:8pt;'>");

	}
	var doc = debugWin.document;
	return doc;
}

function writeDebugString(str)
{
	var doc = getDebugDoc();
	doc.writeln(str);
}

function writeDebugObject(obj)
{
	var doc = getDebugDoc();
	
	doc.writeln("<table style='font-family:verdana; font-size:8pt;'>");
	for(var i in obj)
	{
		doc.writeln("<tr><td>" + i + "</td><td>=</td><td>" + obj[i] + "</td></tr>");
	}
	doc.writeln("</table>");
	doc.writeln("<hr>");
}