﻿function mynGridMenu(sce,x,y,showDelete) {
  var grdMenuId = 'grdMenu';
  var grdMenu = document.getElementById(grdMenuId);
  if (grdMenu == null)
  {   var ctxtmenuStyle = 'grdMenu';
      var ctxtmenuUlStyle = 'grdMenuList';
      var ctxtmenuLiStyle = 'grdMenuButton';
      grdMenu = document.createElement('div');
      grdMenu.className = ctxtmenuStyle;
      grdMenu.style.position = 'absolute';
      grdMenu.style.left = x + 'px';
      grdMenu.style.top = y + 'px';              
      grdMenu.id = grdMenuId;
      var grdMenuList = document.createElement('ul');
      grdMenuList.className = ctxtmenuUlStyle;
/*  newdiv.style.height = '20px'
  newdiv.style.width = '200px'; */
      var grdMenuOptId = 'grdMenuOpt1';
      var grdOpt
      if (showDelete == 'Y')
      {
          grdOpt = document.createElement('li');
          grdOpt.id = grdMenuOptId;
          grdOpt.className = ctxtmenuLiStyle;
          grdOpt.innerHTML = "Remove Row";
          grdOpt.onclick = function() { mynGridRemRow(sce) }
          grdMenuList.appendChild(grdOpt);
          grdOpt = null;
      }
      grdMenuOptId = 'grdMenuOpt2';
      grdOpt = document.createElement('li');
      grdOpt.id = grdMenuOptId;
      grdOpt.className = ctxtmenuLiStyle;
      grdOpt.innerHTML = "Export";
      grdOpt.onclick = function() { mynGridExport(sce) }
      grdMenuList.appendChild(grdOpt);
      grdMenuOptId = 'grdMenuOpt3';
      grdOpt = null;
      grdOpt = document.createElement('li');
      grdOpt.id = grdMenuOptId;
      grdOpt.className = ctxtmenuLiStyle;
      grdOpt.innerHTML = "Hide";
      grdOpt.onclick = function() { mynGridHideMenu() }
      grdMenuList.appendChild(grdOpt);
      grdMenu.appendChild(grdMenuList);
      var theForm = document.forms[0];
      theForm.appendChild(grdMenu);
  }      
  grdMenu.style.display = 'list-item';
  grdMenu.style.position = 'absolute';
  grdMenu.style.left = x + 'px';
  grdMenu.style.top = y + 'px';                
  /* newdiv.setAttribute('id',divIdName);
  newdiv.setAttribute('style','position: absolute; left: ' + x + 'px; top: ' + y + 'px ; height 10px; width 100px ; z-index: 1000 ;');
  */
}
function mynGridRemRow(sce)
{
    var answer = confirm("Remove Item From List?");
    if (answer)
    {        
        var prnt;
        prnt = getTableDataElementParent(sce);
        if (prnt != null)
        {
            __execMynPostBack(prnt.id,'GridRemRow$');
        }
    }
    else
    {
        mynGridHideMenu();
    }
}
function mynGridExport(sce)
{
    var answer = confirm("Export Grid Data?");
    if (answer)
    {        
        var prnt;
        prnt = getTableElementParent(sce);
        if (prnt != null)
        {
            __execMynPostBack(prnt.id,'GridExport$');
        }
    }
    else
    {
        mynGridHideMenu();
    }
}
function mynGridHideMenu()
{
  var grdMenuId = 'grdMenu';
  var grdMenu = document.getElementById(grdMenuId);
  if (!(grdMenu == null))
  {
    grdMenu.style.display = 'none';
  }
}
function __doMynGridCellClick(e,showDelete)
{
	var rightclick;
	var sce;
	var prnt;
	if (!e) var e = window.event;
	if (e.which) rightclick = (e.which == 3);
	else if (e.button) rightclick = (e.button == 2);
	if (e.target) sce = e.target;
	else if (e.srcElement) sce = e.srcElement;
	if (sce.nodeType == 3) // defeat Safari bug
		sce = sce.parentNode;				    
    if (!(sce == null))
    {
        prnt = getTableElementParent(sce);
        if (prnt != null)
        {
            var trows = prnt.getElementsByTagName("tr");
            for (i=0; i < trows.length; i++)
            {
                if (trows[i].className == "GridSelectedRow")
                {
                    trows[i].className = "GridStandardRow";
                }
            }
            var trw = getTableRowElementParent(sce);
            trw.className = "GridSelectedRow";
            var grdStatId = prnt.id + "$slct";
            var grdStat = document.getElementById(grdStatId);
            if (grdStat == null)
            {
                grdStat = document.createElement("input");
                grdStat.type = "hidden";
                grdStat.name = grdStatId;
                grdStat.id = grdStatId;
                var theForm = document.forms[0];
                theForm.appendChild(grdStat);
            }
            var tdEl = sce;
            if (!(tdEl.tagName.toUpperCase() == 'TD'))
            {
                tdEl = getTableDataElementParent(sce);
            }
            if (!(tdEl == null))
            {
                grdStat.value = tdEl.id;
            }
        }
        if (rightclick)
        {
            if (window.disableGridMenuOnInput)
            {
                if (sce.tagName == "INPUT")
                {
                    return;
                }
            }
            var posx = 0;
	        var posy = 0;
	        if (e.pageX || e.pageY) 	{
		        posx = e.pageX;
		        posy = e.pageY;
	        }
	        else if (e.clientX || e.clientY) 	{
		        posx = e.clientX + document.body.scrollLeft
			        + document.documentElement.scrollLeft;
		        posy = e.clientY + document.body.scrollTop
			        + document.documentElement.scrollTop;
	        }
            mynGridMenu(sce,posx,posy,showDelete);     
        }        
    }    
}
function getTableElementParent(sce)
{
var prnt = sce.parentNode;
do
{
    //var pn = prnt.tagName;
    if (prnt.tagName.toUpperCase() == 'TABLE')
    {
        return prnt;
    }
    prnt = prnt.parentNode;
}
while (prnt != null);
}
function getTableRowElementParent(sce)
{
var prnt = sce.parentNode;
do
{
    //var pn = prnt.tagName;
    if (prnt.tagName.toUpperCase() == 'TR')
    {
        return prnt;
    }
    prnt = prnt.parentNode;
}
while (prnt != null);
}
function getTableDataElementParent(sce)
{
if (sce.tagName.toUpperCase() == 'TD')
{
    return sce;
}
var prnt = sce.parentNode;
do
{
    //var pn = prnt.tagName;
    if (prnt.tagName.toUpperCase() == 'TD')
    {
        return prnt;
    }
    prnt = prnt.parentNode;
}
while (prnt != null);
}
function mynGridAddOnKeyDown(tid,e) {
    var targ;
    var arg;
    var code;
	if (!e) var e = window.event;
	// e gives access to the event in all browsers
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;				    
    if (e.keyCode)
    {
        code = e.keyCode;
    }
    else if (e.which)
    {
        code = e.which;
    }
    if (code == 13)
    {
        arg = 'MakeAddSelect$';
        __execMynPostBack(tid, arg);
    }
    else if ((code == 115) || (code == 116))
    {
        if (code == 115)
        {
            arg = 'ImageClick$';
        }
        else
        {
            arg = 'ImageF5Click$';
        }
        __execMynPostBack(targ.id, arg);     
     }
}
function __SelectKey()
{
    var sType = document.getElementById("FormSelectType");
    if (sType == null)
    {
    return;
    }
    if (sType.value == "S")
    {
        var tbls = document.getElementsByTagName("table");
        if (tbls == null)
        {
            return;
        }
        if (tbls.length == 0)
        {
            return;
        }
        var tbl = tbls[0];
        __execMynPostBack(tbl.id,'DblClick$');
    }
}
function __UpKey()
{
    var sType = document.getElementById("FormSelectType");
    if (sType == null)
    {
    return;
    }
    if (sType.value == "S")
    {
        var tbls = document.getElementsByTagName("table");
        if (tbls == null)
        {
            return;
        }
        if (tbls.length == 0)
        {
            return;
        }
        var tbl = tbls[0];
        var selRow = -2;
        var stdRowFnd = false;
        var trows = tbl.getElementsByTagName("tr");
        for (i=0; i < trows.length; i++)
        {
            if (trows[i].className == "GridSelectedRow")
            {      
                if (i > 0)
                {
                    if (trows[i-1].className == "GridStandardRow")
                    {
                        trows[i-1].className = "GridSelectedRow";
                        trows[i].className = "GridStandardRow";
                        __setSelGridRow(tbl, trows[i-1])
                        __chkGrdScroll(trows[i-1]);
                        return;
                    }
                    else
                    {
                        //Page Up
                        __grdPgUp(tbl);
                    }
                }    
                else
                {
                    //Page Up
                    __grdPgUp(tbl);
                }  
            }
        }                
    }
}
function __grdPgUp(tbl)
{
    var pgrID = tbl.id + '$pg_info';
    var pgr = document.getElementById(pgrID);
    if (!(pgr == null))
    {
        if (pgr.value == 'A' || pgr.value == 'B')
        {
            __execMynPostBack(tbl.id,'PageBack$');
        }
    }
}
function __DownKey(frmTxt)
{
    var sType = document.getElementById("FormSelectType");
    if (sType == null)
    {
    return;
    }
    if (sType.value == "S")
    {
        if (frmTxt)
        {
            __setCmdFocus();
            return;
        }
        var tbls = document.getElementsByTagName("table");
        if (tbls == null)
        {
            return;
        }
        if (tbls.length == 0)
        {
            return;
        }
        var rwFnd = false;
        var tbl = tbls[0];
        var selRow = -2;
        var trows = tbl.getElementsByTagName("tr");
        for (i=0; i < trows.length; i++)
        {
            if (trows[i].className == "GridSelectedRow")
            {            
                selRow = i;
                rwFnd = true;
            }
            if (i == (selRow + 1))
            {
                trows[i].className = "GridSelectedRow";
                trows[selRow].className = "GridStandardRow";                
                __setSelGridRow(tbl, trows[i])
                __chkGrdScroll(trows[i]);
                return;
            }
        }        
        // no row found - select first standard
        if (!rwFnd)
        {
            for (i=0; i < trows.length; i++)
            {
                if (trows[i].className == "GridStandardRow")
                {            
                    trows[i].className = "GridSelectedRow";
                    __setSelGridRow(tbl, trows[i])
                    return;
                }
            }                       
        }
        else
        {
            //page on last row
            var pgrID = tbl.id + '$pg_info';
            var pgr = document.getElementById(pgrID);
            if (!(pgr == null))
            {
                if (pgr.value == 'A' || pgr.value == 'F')
                {
                    __execMynPostBack(tbl.id,'PageFwd$');
                }
            }
        }
    }
}
function __setSelGridRow(tbl, pRow)
{
var gCells = pRow.getElementsByTagName("td");
if (gCells == null)
{
    return;
}
if (gCells.length == 0)
{
    return;
}
var gCell = gCells[0];
var grdStatId = tbl.id + "$slct";
var grdStat = document.getElementById(grdStatId);
if (grdStat == null)
{
    grdStat = document.createElement("input");
    grdStat.type = "hidden";
    grdStat.name = grdStatId;
    grdStat.id = grdStatId;
    var theForm = document.forms[0];
    theForm.appendChild(grdStat);
}
grdStat.value = gCell.id;
}
function mynGridCol_onmousedown(e)
{    
    var targ;
    var code;
	if (!e) var e = window.event;
	// e gives access to the event in all browsers
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (!targ)
	{
	    return;
	}
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;				
    targ.style.zIndex = 9999;
    window.inMynColResize = true;	
    window.ctlMynColResize = targ;
    window.ResizeStart = targ.offsetLeft;
    window.ctlMynColResizeTarget = targ.sceColHead;
    var scrollOffsetLeft = 0;
    var prnt = targ;
    var prntDone = false;
    while (!(prnt.parentNode == null))
    {
//        if (prnt.parentNode.offsetTop)
//        {
//            scrollOffsetTop += prnt.parentNode.offsetTop;
//        }
        if (prnt.parentNode.offsetLeft)
        {
            scrollOffsetLeft += prnt.parentNode.offsetLeft;
        }                            
        if (prnt.parentNode.scrollLeft && prntDone)
        {
            scrollOffsetLeft -= prnt.parentNode.scrollLeft;
        }                                    
        prntDone = true;
        prnt = prnt.parentNode;                                
    }                                                                    
    window.ctlMynColResizeOffset = scrollOffsetLeft;    
	document.onmousemove = mynGridCol_drag;
	document.onmouseup = mynGridCol_mouseup;
	return false;
}	
function mynGridCol_drag(evnt)
{
	evnt = evnt ? evnt : event;
	if ( evnt && window.inMynColResize)
	{
		evnt.cancelBubble = true;
		evnt.returnValue = false;

		// get the new location
		// from the event object
		var x = getInt(evnt.pageX ? evnt.pageX : evnt.clientX);
        x = x - window.ctlMynColResizeOffset;
        
		// adjust for the scrollLeft of the page
		var scrollLeft = getInt(document.body.scrollLeft);
		//x = x + scrollLeft;
		
		// adjust for the width and
		// keep centered on the cursor	
		pixX = window.ctlMynColResize.offsetWidth/2; 
		x = x - pixX; 
		
		var minX = 0;
		var maxX = 0;
		/*
		// determine the min and max allowable locations
		var minX = getInt(sbar.offsetLeft);
		minX += getInt(sbar.minWidth);		
		
		var maxX = getInt(sbar.offsetLeft);
		maxX += getInt(sbar.maxWidth);
		*/

		// check to see if this location is allowed,
		// otherwise, set the Limit color to indicate that the 
		// min or max width has been reached
		if ( ( minX == 0 || x >= minX ) && ( maxX == 0 || x <= maxX) )
		{
			window.ctlMynColResize.style.left = x + "px";
			window.ctlMynColResize.style.backgroundColor = '#FFFFFF';
			/*
			if ( sbar.liveResize )
			{
				sbar.setTargetWidth();
			}
			*/
		}
    }
}
function mynGridCol_mouseup(evnt)
{

	// unhook the mouse events on the document
	document.onmousemove = null;
	document.onmouseup = null;    
	// calculate the width based on the splitterbar's location
	var width = getInt(window.ctlMynColResize.style.left);
	var target = document.getElementById(window.ctlMynColResizeTarget);
	var oWidth = target.offsetWidth;
	window.ctlMynColResize.style.backgroundColor = "transparent";
	width -= window.ResizeStart;
	width -= getInt(target.style.borderLeftWidth);
	width -= getInt(target.style.borderRightWidth);
	width += oWidth;
	// do not set the width to 0, 1 is the minimum
	if ( width < 1 ) { width = 1; }
	// set the width of the target element	
	target.style.width = width + "px";				
    if (target.className == "dropTextCell" || target.className == "textCell")
    {
        var tbl = getTableElementParent(target)        
        var trows = tbl.getElementsByTagName("tr");
        var cidx = -1;
        var tcells = trows[0].getElementsByTagName("th");
        for (nxc=0; nxc < tcells.length; nxc++)
        {
            if (tcells[nxc] == target)
            {
                cidx = nxc;
                break;
            }
        }               
        if (cidx > -1 && trows.length > 1)
        {        
            for (nxr=1; nxr < trows.length; nxr++)
            {  
                var nxrs = trows[nxr].getElementsByTagName("td");                
                var tbs = nxrs[cidx].getElementsByTagName("input");
                if (tbs.length > 0)
                {
                    if (target.className == "dropTextCell")
                    {
                       var nwid = width - 12; 
                        tbs[0].style.width = nwid + "px"; 
                    }
                    else if (target.className == "textCell")
                    {
                       var nwid = width - 4; 
                        tbs[0].style.width = nwid + "px";                             
                    }
                }                                                     
            }
        }            
    }	
    window.inMynColResize = false;	
    //window.ctlMynColResize.style.backgroundColor = "transparent";
    __SetSplitters()
}
function getInt(value)
{
	var num;
	if ( value )
	{
		num = parseInt(value);
		if (isNaN(num))
		{
			num = new Number(0);
		}
	}
	else
	{
		num = new Number(0);
	}
	return num;
}
function __mynTableScroll(evt)
{
    __SetSplitters();
}
function __SetSplitters()
{
    var tbls = document.getElementsByTagName("table");
    if (tbls == null)
    {
        return;
    }
    if (tbls.length == 0)
    {
        return;
    }
    var splWid = 3;
    var splExt = 3;
    for (ti=0; ti < tbls.length; ti++)
    {    
        var tbl = tbls[ti];       
        var prnt = tbl;
        var scrollOffsetLeft = 0;
        var scrollOffsetTop = 0;
        var tblParent;
        if (tbl.onscroll == null)
        {
            tbl.onscroll = __mynTableScroll;
        }
        if (tbl.parentNode == null)
        {
            tblParent = document.forms[0];
        }
        else
        {
            tblParent = tbl.parentNode;
        }
        scrollOffsetTop += tbl.offsetTop;
        scrollOffsetLeft += tbl.offsetLeft
        if (tblParent.onscroll == null)
        {
            tblParent.onscroll = __mynTableScroll;
        }                
//        while (!(prnt.parentNode == null))
//        {
//            if (prnt.parentNode.offsetTop)
//            {
//                scrollOffsetTop += prnt.parentNode.offsetTop;
//            }
//            if (prnt.parentNode.offsetLeft)
//            {
//                scrollOffsetLeft += prnt.parentNode.offsetLeft;
//            }                            
//            prnt = prnt.parentNode;                                
//        }                                                                    
        var tblExtRight = tblParent.offsetWidth + tblParent.scrollLeft
        var trows = tbl.getElementsByTagName("tr");
        if (!(trows.length == 0))
        {        
            scrollOffsetLeft += trows[0].offsetLeft;
            scrollOffsetTop += trows[0].offsetTop;
            var thdrs = trows[0].getElementsByTagName("th");            
            if (!(thdrs.length == 0))
            {
                for (thi=0; thi < thdrs.length; thi++)
                {
                    thd = thdrs[thi];
                    if (thd.id)
                    {
                        var spid = thd.id + '_spl';                        
                        var ctl = document.getElementById(spid);                        
                        if (ctl == null)
                        {
                            ctl = document.createElement("div");
                            ctl.name = spid;
                            ctl.id = spid;
                            ctl.style.position = "absolute";
                            ctl.style.width = "3px";
                            ctl.style.background = "transparent";
                            ctl.style.zIndex = 9999;
                            ctl.style.height = (thd.offsetHeight + splExt) + "px";
                            tblParent.appendChild(ctl);
                            /*
                            ctl.evtTracked = true;
                            ctl.onmouseover = mynGridCol_onmouseover;
                            ctl.onmouseout = mynGridCol_onmouseout;
                            ctl.onmousedown = mynGridCol_onmousedown;
                            */
                            /*                            
                            if(window.addEventListener){ // Mozilla, Netscape, Firefox                                
	                            ctl.addEventListener('mouseover', mynGridCol_onmouseover, false);
	                            ctl.addEventListener('mouseout', mynGridCol_onmouseout, false);
	                            ctl.addEventListener('mousedown', mynGridCol_onmousedown, false);
                            } else { // IE
	                            ctl.attachEvent('onmouseover', mynGridCol_onmouseover);
	                            ctl.attachEvent('onmouseout', mynGridCol_onmouseout);
	                            ctl.attachEvent('onmousedown', mynGridCol_onmousedown);
                            }
                            */                           
                        }
                        //var xpos = scrollOffsetLeft +  + tbl.scrollLeft + tblParent.scrollLeft + (thd.offsetLeft + thd.offsetWidth - splWid);                        
                        //var ypos = scrollOffsetTop + tbl.offsetTop + tbl.scrollTop + tblParent.scrollTop;
                        var rpos = scrollOffsetLeft + (thd.offsetLeft + thd.offsetWidth - splWid)
                        //if (rpos < (tblParent.offsetWidth + tblParent.scrollLeft))                        
                        var xpos = scrollOffsetLeft  - (tbl.scrollLeft + tblParent.scrollLeft) + (thd.offsetLeft + thd.offsetWidth - splWid);                        
                        var ypos = scrollOffsetTop + tbl.offsetTop - (tbl.scrollTop + tblParent.scrollTop);
                        //if (xpos < tblExtRight && ypos > ((-1) * (thd.offsetHeight + splExt)))
                        if (rpos < (tblParent.clientWidth + tblParent.scrollLeft) && ypos > ((-1) * (thd.offsetHeight + splExt)))
                        {
                            if (ypos < 0)
                            {
                                ypos = 0;
                            }
                            ctl.style.left = xpos + "px";
                            ctl.style.top = ypos + "px";
                            ctl.sceColHead = thd.id;     
                            ctl.style.cursor = "e-resize";                                                   
                            if (!ctl.onmousedown)
                            {
                                //ctl.onmouseover = mynGridCol_onmouseover;
                                //ctl.onmouseout = mynGridCol_onmouseout;
                                ctl.onmousedown = mynGridCol_onmousedown;
                            }                            
                            /*                            
                            if (!ctl.evtTracked)
                            {
                                ctl.onmouseover = mynGridCol_onmouseover;
                            
                                ctl.evtTracked = true;
                                if(window.addEventListener){ // Mozilla, Netscape, Firefox
	                                ctl.addEventListener('mouseover', mynGridCol_onmouseover, false);
	                                ctl.addEventListener('mouseout', mynGridCol_onmouseout, false);
	                                ctl.addEventListener('mousedown', mynGridCol_onmousedown, false);
                                } else { // IE
	                                ctl.attachEvent('onmouseover', mynGridCol_onmouseover);
	                                ctl.attachEvent('onmouseout', mynGridCol_onmouseout);
	                                ctl.attachEvent('onmousedown', mynGridCol_onmousedown);
                                }                                                        
                            }
                            */
                        }
                        else
                        {                            
                            ctl.style.cursor = "default";
                            if (ctl.onmousedown)
                            {
                                //ctl.onmouseover = null;
                                //ctl.onmouseout = null;
                                ctl.onmousedown = null;                            
                            }
                            /*
                            if (ctl.evtTracked)
                            {
                                ctl.evtTracked = false;
                                if(window.addEventListener){ // Mozilla, Netscape, Firefox
	                                ctl.removeEventListener('mouseover', mynGridCol_onmouseover, false);
	                                ctl.removeEventListener('mouseout', mynGridCol_onmouseout, false);
	                                ctl.removeEventListener('mousedown', mynGridCol_onmousedown, false);
                                } else { // IE
	                                ctl.detachEvent('onmouseover', mynGridCol_onmouseover);
	                                ctl.detachEvent('onmouseout', mynGridCol_onmouseout);
	                                ctl.detachEvent('onmousedown', mynGridCol_onmousedown);
                                }                                                                                        
                            }
                            */
                        }
                        //ctl.onmousedown = mynGridCol_onmousedown(ctl,thd.id)
                    }
                }
            }
        }
    }
}



