// #################################################################################
// #    File Name   : tableAct3.htc
// #    Project     : Japanese Grammar Note
// #    System      : Search Table Manupilation Behaviour
// #    Purpose     : Generate Dynamic Table of Contents
// #    Function    : DIARY Control
// #    Model       : This programe is based on the Document Object Model (DOM)
// #    Language    : JavaScript (Client Side Scripting. ver: 5.x)
// #    Include     : jpread.js
// #    Date Issued : 2002/03/27(Wed)-16:34:21
// #    Base URL    : http://www.all.co.nz/
// #    Location    : js/
// #    Usage       :
// #    Browsers    : IE5.x and above
// #    Requirement : <BODY ID='oBody'>
// #    Restriction : None
// #    Author      : H. Sekine (Able LL Systems Dpt. CHC, NZL)
// #    Copyright   : (C) 2002 Southern Cross Virtual Co., Ltd. All Rights Reserved.
// #    Contact     : info@all.co.nz
// #    Tab Stop    : 4 (Suitable TAB stop size to view this Source Code)
// # ===============================================================================
// #    ** Revision **
// # Date   Description                                             Sign      Mark
// # ------+-------------------------------------------------------+---------+------
// #
// #################################################################################
@set @DEBUG = 0 // Program Debug Flag: ON(>=1)/OFF(==0)/(==9) Test Release Mode
// ###########################################################################
// #                            << NOTICE >>                                 #
// # THIS PROGRAM CODE BELONGS TO SOUTHERN CROSS VIRTUAL CO., LTD. IT IS     #
// # CONSIDERED A TRADE SECRET AND IS NOT TO BE DIVULUGED OR USED BY PARTIES #
// # WHO HAVE NOT RECEIVED WRITTEN AUTHORIZATION FROM THE OWNER.             #
// # ----------------------------------------------------------------------- #
// #                            << 通  知 >>                                 #
// # 本プログラムはSOUTHERN CROSS VIRTUAL CO., LTD.の財産です。本プログラム  #
// # はトレード・シークレットとみなされ、当該権利者の書面による許可なくして、#
// # 漏洩または使用を禁じます。                                              #
// ###########################################################################

<PUBLIC:COMPONENT>
<public:event    name="onrowselect" ID="rowSelect" />
<public:property name="hlColor" />
<public:property name="slColor" />
<public:property name="dragColor" />
<public:attach event="ondetach" onevent="cleanup()" />
<public:attach event="oncontentready" onevent="init()" />

<script type="text/javascript" language="JavaScript">
var giCurrRow   = -1;
var giSelRow    = -1;
var giLastClick = -1;
var goTbody     = null;
var goTheadRow  = null;
var giColCount  = null;
var gbDragMode  = false;
var giArrayHit  = false;
var goDragItem;
var goDragItemStyle;
var gaArrHitTest = new Array();

var IMG_BLANK   = "dude07232001blank.gif";
var IMG_DOWN    = "dude07232001down.gif";
var IMG_UP      = "dude07232001up.gif";
<!------------[ Inserted By Skn23: Start:2004/11/10(Wed)-22:04:11 ]-------->
var TBROWS = null;
var TBNUM  = null;
var PIVOT  = null;
var iSortField2 = -1;
var gbReverse = new Array(6);
gbReverse[0] = false;
gbReverse[1] = false;
gbReverse[2] = false;
gbReverse[3] = false;
gbReverse[4] = false;
gbReverse[5] = false;
<!------------[ Inserted By Skn23: End:  2004/11/10(Wed)-22:04:11 ]-------->

if (element.tagName == 'TABLE'){
    with(element){
        attachEvent('onmouseover', onMouseOver);
        attachEvent('onmouseout', onMouseOut);
        attachEvent('onclick', onClick);
    }
} else {
    alert("Error: 'tableAct3.htc' not attached to a table element");
}

function cleanup(){
    hilite(-1);
    with(element){
        detachEvent('onmouseover', onMouseOver);
        detachEvent('onmouseout', onMouseOut);
        detachEvent('onclick', onClick);
    }
}

function onClick(){
    var srcElem = window.event.srcElement;

    while((srcElem.tagName != "TR") && (srcElem.tagName != "TABLE")){
        srcElem = srcElem.parentElement;
    }
    if ((srcElem.tagName != "TR") || (srcElem.rowIndex == 0)) return;
    if (giSelRow != -1) giSelRow.runtimeStyle.backgroundColor = '';
    srcElem.runtimeStyle.backgroundColor = slColor;
    giSelRow = srcElem;
    var oEvent = createEventObject();
    oEvent.selected = giSelRow;
    rowSelect.fire(oEvent);
}

function onMouseOver(){
    var srcElem = window.event.srcElement;
    while((srcElem.tagName != "TR") && (srcElem.tagName != "TABLE")){
        srcElem = srcElem.parentElement;
    }
    if (srcElem.tagName != "TR") return;
    if (srcElem.rowIndex > 0){
        hilite(srcElem);
    } else {
        hilite(-1);
    }
}

function onMouseOut(){
    hilite(-1);
}

function hilite(newRow){
    if (hlColor != null){
        if ((giCurrRow != -1) && (giCurrRow != giSelRow)){
            giCurrRow.runtimeStyle.backgroundColor = '';
        }
        if ((newRow != -1) && (newRow != giSelRow)){
            newRow.runtimeStyle.backgroundColor = hlColor;
        }
    }
    giCurrRow = newRow;
}

function init(){
    goTbody = element.tBodies(0);
    if (!goTbody) return;
    var thead = element.tHead;
    if (!thead) return;
    goTheadRow = thead.children[0];
    if (goTheadRow.tagName != "TR") return;
    goTheadRow.runtimeStyle.cursor = "hand";
    giColCount = goTheadRow.children.length;

    var i, l, clickCell;
    var cx = 0;
    var cy = 0;
    var c;

    for (i=0; i<giColCount; i++){
        l = document.createElement("IMG");
        with(l){
            src     = IMG_BLANK;
            id      = "srtImg";
            width   = 25;
            height  = 11;
            style.display = "none";
        }
        clickCell = goTheadRow.children[i];
        clickCell.selectIndex = i;
        clickCell.insertAdjacentElement("beforeEnd", l);
        clickCell.attachEvent("onclick", doClick);
        gaArrHitTest[i] = new Array();

        c = clickCell.offsetParent;

        if ((cx == 0) && (cy == 0)){
            while(c.offsetParent != null){
                  cy += c.offsetTop;
                  cx += c.offsetLeft;
                  c   = c.offsetParent;
            }
        }

        gaArrHitTest[i][0] = cx + clickCell.offsetLeft;
        gaArrHitTest[i][1] = cy + clickCell.offsetTop;
        gaArrHitTest[i][2] = clickCell;
        gaArrHitTest[i][3] = cx + clickCell.offsetLeft + eval(clickCell.width);

        clickCell.attachEvent("onmousedown",onMouseDown);
    }
    defaultTitleColor = goTheadRow.children[0].currentStyle.backgroundColor;

    with(element.document){
        attachEvent("onmousemove",   onMouseMove);
        attachEvent("onmouseup",     onMouseUp);
        attachEvent("onselectstart", onSelect);
    }
}
/* =================================== 2006/09/19(Tsu)-18:03:45 By SKN23
//function DeterminSortFieldByCaps( oObject ){
//    var sTargetText = oObject.innetText;
//    var oParent = oObject.parentElement;
//    for (var i=0; i<oParent.children.length; i++){
//        if (sTargetText == oParent.children(i).innerText) rerurn(i);
//    }
//    return (-1);
//}
======================================================================= */
function doClick(e){
    var clickObject = e.srcElement;
    while((clickObject.tagName != "TH") && (clickObject.tagName != "TD")){
        clickObject = clickObject.parentElement;
    }
    iSortField2  = clickObject.selectIndex;
    cmbSort.selectedIndex = iSortField2 + 1;

    var imgcol = goTheadRow.all('srtImg');
    var imgLen = imgcol.length;
    for (var x = 0; x<imgLen; x++){
        with(imgcol[x]){
            src = IMG_BLANK;
            style.display = "none";
        }
    }

    if (giLastClick == clickObject.selectIndex){
        if (gbReverse[iSortField2]  == false){
            clickObject.children[0].src = IMG_DOWN;
            gbReverse[iSortField2] = true;
        } else {
            clickObject.children[0].src = IMG_UP;
            gbReverse[iSortField2] = false;
        }
    } else {
        gbReverse[iSortField2] = false;
        giLastClick = clickObject.selectIndex;
        clickObject.children[0].src = IMG_UP;
    }
    clickObject.children[0].style.display = "";

// return;

    goTbody = oSearchTable.all('oSearchBody');  // by skn23
    if (goTbody == null) return;
<!------------[ Inserted By Skn23: Start:2004/11/10(Wed)-22:04:11 ]-------->
    if (iSortField2 == -1) return;
    TBROWS = goTbody.rows;
    TBNUM  = TBROWS.length;
//  alert("TBROWS=" + clickObject.parentElement.tagName + " TBNUM=" + TBNUM + " NAME=" + clickObject.name + ((gbReverse[iSortField2]) ? " 降順" : " 昇順"));
    window.status = " ... Sorting[" + ((gbReverse[iSortField2]) ? "降順" : "昇順") + "] oSearchTable for " + TBNUM + " at Fileld: " + clickObject.name + " ・・・ しばらくお待ちください。";
    TblQuickSort2( TBROWS, 0, TBNUM, iSortField2 );
    window.status = " ... Completed!!";
<!------------[ Inserted By Skn23: End:  2004/11/10(Wed)-22:04:11 ]-------->
}
function InitHeader(){
    var cx = 0;
    var cy = 0;
    var c, clickCell;
<!------------[ Inserted By Skn23: Start ]-------->
    var thead = oSearchTable.tHead;
    if (!thead) return;
    goTheadRow = thead.children[0];
<!------------[ Inserted By Skn23: End   ]-------->

    for (i=0; i<giColCount; i++){
        clickCell = goTheadRow.children[i];
        clickCell.selectIndex = i;
        c = clickCell.offsetParent;

        if ((cx == 0) && (cy == 0)){
            while(c.offsetParent != null){
                cy += c.offsetTop;
                cx += c.offsetLeft;
                c   = c.offsetParent;
            }
        }
        gaArrHitTest[i][0] = cx + clickCell.offsetLeft;
        gaArrHitTest[i][1] = cy + clickCell.offsetTop;
        gaArrHitTest[i][2] = clickCell;
        gaArrHitTest[i][3] = cx + clickCell.offsetLeft + eval(clickCell.width);
    }
}

function onSelect(){
    return false;
}

function ChangeHeader(iChange){
    var arrLen = gaArrHitTest.length;
    for (var y=0; y<arrLen; y++){
        if (gaArrHitTest[y][2].currentStyle.backgroundColor == dragColor){
            gaArrHitTest[y][2].style.backgroundColor = defaultTitleColor;
        }
    }

    if (iChange == "-1") return;

    gaArrHitTest[iChange][2].style.backgroundColor = dragColor;
}

function onMouseUp(e){
    if (!gbDragMode) return;
    gbDragMode = false;

    var iSelected = goDragItem.selectIndex;

    goDragItem.removeNode(true);
    goDragItem = null;

    ChangeHeader(-1);

    if (((giArrayHit - 1) < 0) || (iSelected < 0)) return;  // default faliure

    CopyRow(iSelected, (giArrayHit - 1));

    iSelected  = 0;
    giArrayHit = -1;
}

function onMouseDown(e){
    gbDragMode  = true;
    with(e){
        var src = srcElement;
        var c   = srcElement;
    }

    while((src.tagName != "TH") && (src.tagName != "TD")){
        src = src.parentElement;
    }
    var SrcCurrentStyle = src.currentStyle;
    goDragItem = document.createElement("DIV");
    goDragItemStyle = goDragItem.style;
    goDragItem.innerHTML       = src.innerHTML;
    goDragItemStyle.height     = SrcCurrentStyle.height;
    goDragItemStyle.width      = SrcCurrentStyle.width;
    goDragItemStyle.background = SrcCurrentStyle.backgroundColor;
    goDragItemStyle.fontColor  = SrcCurrentStyle.fontColor;
    goDragItemStyle.position   = "absolute";
    goDragItem.selectIndex     = src.selectIndex;
    while(c.offsetParent != null){
        goDragItemStyle.y += c.offsetTop;
        goDragItemStyle.x += c.offsetLeft;
        c = c.offsetParent;
    }
    with(goDragItemStyle){
        borderStyle = "outset";
        display     = "none";
    }
    src.insertBefore(goDragItem);
}

function onMouseMove(e){
    if (!gbDragMode || !goDragItem) return;
    var midWObj = goDragItem.style.posWidth / 2;
    var midHObj = 12;

    var intTop  = e.clientY + element.document.body.scrollTop;
    var intLeft = e.clientX + element.document.body.scrollLeft;

    var cx = 0, cy = 0;
    var elCurrent = goDragItem.offsetParent;

    while(elCurrent.offsetParent != null){
        with(elCurrent){
            cx += offsetTop;
            cy += offsetLeft;
        }
        elCurrent = elCurrent.offsetParent;
    }
    with(goDragItem.style){
        pixelTop  = intTop  - cx - midHObj;
        pixelLeft = intLeft - cy - midWObj;
    }

    if (goDragItem.style.display == "none") goDragItem.style.display = "";

    giArrayHit = CheckHit(intTop , intLeft , e);

    with(e){
        cancelBubble = false;
        returnValue  = false;
    }
}
function CheckHit(x,y,e){
    midWObj = goDragItem.style.posWidth / 2;
    midHObj = 12;

    if ( ((x) > (gaArrHitTest[0][1] + 20) ) || ( (x) < (gaArrHitTest[0][1]) ) ){
        ChangeHeader(-1);
        return -1;
    }

    for (var i=0; i < giColCount; i++){
        if ( (y) > (gaArrHitTest[i][0]) && (y) < (gaArrHitTest[i][3] )){
            ChangeHeader(i);
            return i + 1;
        }
    }
    return -1;
}
function CheckHit2(x, y, e){
    if (((x) > (gaArrHitTest[0][1] + 20)) || ((x) < (gaArrHitTest[0][1]))){
        ChangeHeader(-1);
        return -1;
    }

    for (var i=0; i < giColCount; i++){
        if ((y) > (gaArrHitTest[i][0]) && (y) < (gaArrHitTest[i][3])){
            ChangeHeader(i);
            return (i + 1);
        }
    }
    return -1;
}
// ******************************************************************
// *    QUICK SORT partition
// ******************************************************************
function partition2(rows, l, r, pos){
    var i, j;

    i = l - 1;
    j = r;

    PIVOT = rows[r].cells[pos].innerText;

    if (gbReverse[iSortField2] == false){
        do{
            while(rows[++i].cells[pos].innerText < PIVOT);
            while((i < --j) && (PIVOT < rows[j].cells[pos].innerText));
            if (i >= j) break;
            rows[i].swapNode(rows[j]);
        }while(1);
    } else {
        do{
            while(rows[++i].cells[pos].innerText > PIVOT);
            while((i < --j) && (PIVOT > rows[j].cells[pos].innerText));
            if (i >= j) break;
            rows[i].swapNode(rows[j]);
        }while(1);
    }

    rows[i].swapNode(rows[r]);

    return(i);
}
// ******************************************************************
// *    QUICK SORT Engine for Table List
// ******************************************************************
function TblQuickSort2(rows, start, end, pos){
    // rows : Table BODY ROWS Collection Object
    // start: Table row start number to be sorted (0,1,2,...)
    // start: Table row end   number to be sorted (0,1,2,...)
    // pos  : Table column position to be used as Sort-Key field (0,1,2,...)

//    if ((0 > pos) || (pos > 6)) return;   2006/09/19(Tsu)-14:56:36
    if ((0 > pos) || (pos > 5)) return;
    var left;
    var right;
    var v;
    var sp;
    var low  = new Array();
    var high = new Array();

    low[0]  = start;
    high[0] = end-1;
    sp = 1;

    while(sp > 0){
        sp --;
        left    = low[sp];
        right   = high[sp];
        if (left >= right){
        } else {
            v = partition2(rows, left, right, pos);
            if ((v - left) < (right - v)){
                low[sp]     = v + 1;
                high[sp++]  = right;
                low[sp]     = left;
                high[sp++]  = v - 1;
            } else {
                low[sp]     = left;
                high[sp++]  = v - 1;
                low[sp]     = v + 1;
                high[sp++]  = right;
            }
        }
    }
}

function CopyRow(from, to){
    if (from == to) return;
<!------------[ Inserted By Skn23: Start ]-------->
    var thead = oSearchTable.tHead;
    if (!thead) return;
    goTheadRow = thead.children[0];
<!------------[ Inserted By Skn23: End ]-------->
    goTbody = oSearchTable.all('oSearchBody');
    if (goTbody == null) return;

    var i, iRowInsert, saveObj, saveWidth;
    var saveAlign, saveOver, saveOut, saveID, saveTitle;
    var origfrom = from;
    var origto   = to;
    var iDiff    = 0;
    var childFrom1, childFrom2;
    var childLen = goTheadRow.children.length;
    var rowLen   = goTbody.rows.length;

// ******************************************************************
// *    Search Table Header Row Swapping
// ******************************************************************

    with(goTheadRow){
        var oFromObj1 = children[from];
        var oFromObj2 = children[from-1];
        var oFromObj3 = children[from+1];
        var oToObj    = children[to];
    }

    with(oFromObj1){
        saveObj     = innerHTML;
        saveWidth   = width;
        saveTitle   = title;
    }

    if (from > to){
        iDiff = from - to;
        for (i=0; i<iDiff; i++){
            oFromObj1.innerHTML = oFromObj2.innerHTML;
            oFromObj1.width     = oFromObj2.width;
            oFromObj1.title     = oFromObj2.title;
            from --;
            with(goTheadRow){   // 2004/11/14(Sun)-00:04:01
                oFromObj1 = children[from];
                oFromObj2 = children[from-1];
            }
        }
    } else {
        iDiff = to - from;
        for (i=0; i<iDiff; i++){
            oFromObj1.innerHTML = oFromObj3.innerHTML;
            oFromObj1.width     = oFromObj3.width;
            oFromObj1.title     = oFromObj3.title;
            from ++;
            with(goTheadRow){   // 2004/11/14(Sun)-00:04:08
                oFromObj1 = children[from];
                oFromObj3 = children[from+1];
            }
        }
    }

    with(oToObj){
        innerHTML   = saveObj;
        width       = saveWidth;
        title       = saveTitle;
    }

    for (i=0; i<childLen; i++){
        goTheadRow.children[i].selectIndex = i;
    }

    InitHeader();

//  window.status = ".... Completed!!";

// ******************************************************************
// *    Search Table Body Rows Swapping
// ******************************************************************

//  window.status = "Search Table Body Rows Swapping ...";

    for (iRowInsert=0; iRowInsert<rowLen; iRowInsert++){
        from = origfrom;
        to   = origto;
        with(goTbody.children[iRowInsert].children[from]){
            saveObj   = innerHTML;
            saveAlign = style.textAlign;
            saveOver  = onmouseover;
            saveOut   = onmouseout;
            saveID    = id;
            saveTitle = title;  // v3.1 2004/11/09(Tsu)-22:12:47
            saveName  = name;   // v3.2 2004/11/12(Fri)-02:21:03
        }

        if (from > to){
            iDiff = from - to;
            for (i=0; i<iDiff; i++){
                childFrom1 = goTbody.children[iRowInsert].children[from];
                childFrom2 = goTbody.children[iRowInsert].children[from-1];

                childFrom1.innerHTML        = childFrom2.innerHTML;
                childFrom1.style.textAlign  = childFrom2.style.textAlign;
                childFrom1.onmouseover      = childFrom2.onmouseover;
                childFrom1.onmouseout       = childFrom2.onmouseout;
                childFrom1.id               = childFrom2.id;
                childFrom1.title            = childFrom2.title; // v3.1
                childFrom1.name             = childFrom2.name;  // v3.2
                from --;
            }
        } else {
            iDiff = to - from;
            for (i=0; i<iDiff; i++){
                childFrom1 = goTbody.children[iRowInsert].children[from];
                childFrom2 = goTbody.children[iRowInsert].children[from+1];

                childFrom1.innerHTML        = childFrom2.innerHTML;
                childFrom1.style.textAlign  = childFrom2.style.textAlign;
                childFrom1.onmouseover      = childFrom2.onmouseover;
                childFrom1.onmouseout       = childFrom2.onmouseout;
                childFrom1.id               = childFrom2.id;
                childFrom1.title            = childFrom2.title; // v3.1
                childFrom1.name             = childFrom2.name;  // v3.2
                from ++;
            }
        }

        with(goTbody.children[iRowInsert].children[to]){
            innerHTML       = saveObj;
            style.textAlign = saveAlign;
            onmouseover     = saveOver;
            onmouseout      = saveOut;
            id              = saveID;
            title           = saveTitle;    // v3.1
            name            = saveName;     // v3.2
        }
    }
//  window.status = ".... Completed!!";
}
</script>
</PUBLIC:COMPONENT>

// #################################################################################
// #    << End of tableAct3.htc >> (C) 2002 Southern Cross Virtual Co., Ltd. (v1.2)
// #################################################################################
