﻿
        var lay1;
        var lay2;
        var col1 = -1;
        var col2 = -1;
        var tbl;
        var x;
        var y;
        var text;
        var inmove = false;
        var inresize = false;
        var curEl;
        var curWidth;
        var nextColumn;
        var nextColumnWidth = 0;
        function MDN(el, e) {
            x = (window.Event) ? e.pageX : event.clientX;
            y = (window.Event) ? e.pageY : event.clientY;
            if (el.style.cursor == 'crosshair') {
                el.style.cursor = 'move';
                inmove = true;
                text = (window.Event) ? el.textContent : el.outerText;
                tbl = el.parentNode.parentNode;
                lay1 = document.getElementById("lay1");
                lay1.innerHTML = text;
                lay1.style.display = "block";
                lay1.style.left = el.offsetLeft + 'px';
                lay1.style.top = el.offsetTop + 18 + 'px';
                lay1.style.left = x + 'px';
                lay1.style.top = y + 18 + 'px';
                lay1.style.width = el.offsetWidth + 'px';
                lay1.style.height = tbl.offsetHeight;
                var topRow = tbl.childNodes[0];
                for (var i = 0; i < topRow.childNodes.length; i++) {
                    var Header = (window.Event) ? topRow.childNodes[i].textContent : topRow.childNodes[i].innerText;
                    if (Header == text)
                    { col1 = i; }

                }
            } else if (el.style.cursor == 'e-resize') {
                curEl = el;

                inresize = true;
                curEl.style.zIndex = 101;
                lay2 = document.getElementById("lay2");
                lay2.style.display = "block";
                lay2.style.left = x + 'px';
                lay2.style.top = y + 1 + 'px';
                lay2.style.height = tbl.offsetHeight;
                curWidth = curEl.style.width;


            }

        }


        function RememberWidth(fldId) {

            var fldColW = '';

            var Trow = tbl.childNodes[0];

            for (var t = 0; t < Trow.cells.length; t++) {

                fldColW += Trow.cells[t].style.width + ',';
            }
            createCookie(fldId + 'W', fldColW, 7);
        }

        function RememberOrder(fldId) {

            var fldColOrder = '';

            var Trow = tbl.childNodes[0];

            for (var t = 0; t < Trow.cells.length; t++) {
                var header = (window.Event) ? Trow.cells[t].textContent : Trow.cells[t].outerText;
                fldColOrder += header + ',';

            }
            createCookie(fldId, fldColOrder, 7);

        }

        function MAP(el, e, fldId) {


            if (inresize) {
               // alert('resize');
                inresize = false;
                lay2.style.display = "none";
                el.style.cursor = 'default';
                curEl.style.width = curWidth;
                if (nextColumnWidth > 0) {
                    nextColumn.style.width = nextColumnWidth;
                }
            }
            RememberOrder(fldId);
            RememberWidth(fldId);

            if (inmove) {
                inmove = false;
                lay1.style.display = "none";
                el.style.cursor = 'default';
                text = (window.Event) ? el.textContent : el.outerText;
                var topRow = tbl.childNodes[0];
                for (var i = 0; i < topRow.childNodes.length; i++) {
                    var header = (window.Event) ? topRow.childNodes[i].textContent : topRow.childNodes[i].innerText;

                    if (header == text)

                    { col2 = i; }
                }
                MovCol(fldId);
            }

        }

        function MOV(el, e) {

            x = (window.Event) ? e.pageX : event.clientX;
            y = (window.Event) ? e.pageY : event.clientY;

            tbl = el.parentNode.parentNode;

            var b = Sys.UI.DomElement.getBounds(el);

            var width = 0;

            if (inresize == false) {

                width = parseInt(b.width) + parseInt(b.x);

                if (x >= width - 2 && x <= width + 2) {
                    el.style.cursor = 'e-resize';

                } else {

                    el.style.cursor = 'crosshair';

                }
            } else {
                el.style.cursor = 'e-resize';

            }


            if (inmove) {
                el.style.cursor = 'move';


                lay1.style.left = x + 'px';
                lay1.style.top = y + 18 + 'px';
                lay1.style.height = tbl.offsetHeight;
            } else if (inresize) {

                lay2.style.left = x + 'px';
                lay2.style.top = y + 1 + 'px';
                lay2.style.height = tbl.offsetHeight;
                var bounds = Sys.UI.DomElement.getBounds(curEl);

                var adj = 0;
                width = x - bounds.x;
                if (curWidth == '') {
                    curWidth = 0;
                }

                nextColumn = curEl.nextSibling;
                if (nextColumnWidth == 0) {
                    nextColumnWidth = nextColumn.clientWidth;

                }


                if (width < parseInt(curWidth)) {
                    //make the next column bigger
                    if (nextColumn.nodeName == 'TH') {
                        nextColumnWidth = parseInt(nextColumnWidth) + parseInt(curWidth) - width;

                    }
                }

                else if (width > parseInt(curWidth)) {
                    //  make the next column smaller
                    if (nextColumn.nodeName == 'TH') {
                        nextColumnWidth = parseInt(nextColumnWidth) - (width - parseInt(curWidth));

                    }
                }

                curWidth = width + 'px';



            }

        }
        function MOT(el, e) {
            //el.style.cursor = 'default';


        }
        function MovCol(fldId) {

            var topRow = tbl.childNodes[0];
            var param = (window.Event) ? 2 : 1;
            var nbCol = topRow.childNodes.length - param;
            if (col1 != col2) {



                for (var i = 0; i < tbl.childNodes.length; i++) {
                    var row = tbl.childNodes[i];
                    if (row.childNodes.length - param == nbCol) {

                        if (col1 < col2) {
                            var cell = row.childNodes[col1];
                            var newCell = row.childNodes[col2 + param - 1];
                        } else {
                            var cell = row.childNodes[col1];
                            var newCell = row.childNodes[col2];

                        }

                        row.removeChild(cell);
                        row.insertBefore(cell, newCell);

                    }


                }


            }


            RememberOrder(fldId);


        }

        function createCookie(name, value, days) {
            if (days) {
                var date = new Date();
                date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
                var expires = "; expires=" + date.toGMTString();
            }
            else var expires = "";
            document.cookie = name + "=" + value + expires + "; path=/";
        }

        function readCookie(name) {
            var nameEQ = name + "=";
            var ca = document.cookie.split(';');
            for (var i = 0; i < ca.length; i++) {
                var c = ca[i];
                while (c.charAt(0) == ' ') c = c.substring(1, c.length);
                if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
            }
            return null;
        }


