/*
// @IDEACODE_COPY@
// Copyright (c) 1999-2003 ideacode, Inc.
//
// This file contains CONFIDENTIAL and PROPRIETARY information of
// ideacode, Inc.  No part of this file may be modified, reproduced,
// stored in a retrieval system, or transmitted in any form or by any
// means -- electronic, mechanical, recording, or otherwise -- without
// the prior written consent of ideacode, Inc.
//
// ideacode, Inc. warrants this software against defects in workmanship
// for a period of TWO (2) YEARS from the DATE OF DELIVERY.  ideacode
// make NO WARRANTY, EXPRESSED OR IMPLIED, for software that uses this
// file unless explicitly declared in a similar copyright block at the
// top of said software.
//
// ideacode may, from time to time, release updates, improvements, or
// other material changes to this file WITHOUT NOTIFICATION.
//
// Questions regarding warranty coverage or to report problems with
// warranted software should be directed to support@ideacode.com
*/

/* browser id variables */
var useLayers = (document.layers) ? true : false;
var isIE = (document.all) ? true : false;

function RemoveClassOnce(el, name) {
    var i, clist, nlist;
    var c = 0;

    if (! el.className)
        return;

    nlist = new Array();
    clist = el.className.split(' ');
    for (i = 0; i < clist.length; i++) {
        if (clist[i] != name)
            nlist.push(clist[i]);
        else {
            if (c > 0)
                nlist.push(clist[i]);
            c++;
        }
    }
    el.className = nlist.join(' ');
}

function hasClassName(el, name) {
    if (! el.className)
        return false;

    var list = el.className.split(' ');
    for (var i = 0; i < list.length; i++)
        if (list[i].toLowerCase() == name.toLowerCase())
            return true;

    return false;
}

function getPageOffsetLeft(el) {
    var x;

    x = el.offsetLeft;
    if (el.offsetParent)
        x += getPageOffsetLeft(el.offsetParent);

    return x;
}

function getPageOffsetTop(el) {
    var y;

    y = el.offsetTop;
    if (el.offsetParent)
        y += getPageOffsetTop(el.offsetParent);

    return y;
}

function GetObjectByID(ID, top) {
    var obj = '';

    if (useLayers) {
        if (top == null) top = document.layers
        for (var i=0; top.length > i; i++) {
            if (top[i].id == ID)
                return top[i];
            else
                obj = GetLayer(ID, top[i].document.layers)
        }
    } else {
        obj = (isIE) ? document.all(ID) : document.getElementById(ID);
    }

    return obj;
}

function pause(numberMillis) {
    var now = new Date();
    var exitTime = now.getTime() + numberMillis;
    while (true) {
        now = new Date();
        if (now.getTime() > exitTime)
            return;
    }
}

function heredoc(s) {
    var re = /\/\*((?:.|\n)*)\*\//;
    return re.exec(s)[1];
}

function hilightRow(id, className, altID, altClassName) {
    var obj = GetObjectByID(id)
    if (obj) {
        if (obj.length) {
            for (var i = 0; i < obj.length; i++) {
                obj[i].className += ' ' + className;
                obj[i].onmouseout = function() { return _unhilightRow(id, className); }
            }
        } else {
            obj.className += ' ' + className;
            obj.onmouseout = function() { return _unhilightRow(id, className); }
        }
    }

    if (altID) {
        hilightRow(altID, altClassName);
    }

    return true;
}

function _unhilightRow(id, className) {
    var obj = GetObjectByID(id)
    if (obj) {
        if (obj.length) {
            for (var i = 0; i < obj.length; i++) {
                RemoveClassOnce(obj[i], className)
            }
        } else {
            RemoveClassOnce(obj, className)
        }
    }

    return true;
}

function SelectClosestMatch(e, id, static) {
    // get the event
    if (document.all) {
        e = window.event
    }

    // get the key and key as a character
    key = (document.layers ? e.which : e.keyCode)
    keychar = String.fromCharCode(key);

    // don't do anything if nasty keys are involved
    switch (key) {
        // these keys need to carry out their associated actions, so return true
        case 9:  // TAB
        case 13: // ENTER
        case 16: // ALT
        case 17: // CONTROL
        case 18: // SHIFT
        case 19: // PAUSE
        case 20: // CAPSLOCK
        case 27: // ESCAPE
        case 33: // PGUP
        case 34: // PGDN
        case 35: // END
        case 36: // HOME
        case 37: // RIGHT
        case 38: // UP
        case 39: // LEFT
        case 40: // DOWN
        case 45: // INSERT
        case 46: // DELETE
        case 91: // left gump key
        case 92: // right gump key
        case 93: // gimp key
        case 112: // F1
        case 113: // F2
        case 114: // F3
        case 115: // F4
        case 116: // F5
        case 117: // F6
        case 118: // F7
        case 119: // F8
        case 120: // F9
        case 121: // F10
        case 122: // F11
        case 123: // F12
        case 144: // NUMLOCK
        case 145: // SCRLOCK
            return true

        default:
            if (e.altKey || e.ctrlKey) {
                return true
            }
    }

    // handle if backspace or normal character
    if (static['str'].length > 0 && (key == 46 || key == 8)) {
        static['str'] = static['str'].substr(0, static['str'].length - 1)
    } else {
        static['str'] += keychar
    }

    // now, find the appropriate spot in the select
    el = GetObjectByID(id)

    var found = false
    if (el && el.length) {
        re = new RegExp('.*' + static['str'].toLowerCase() + '.*');
        for (var i = 0; i < el.length; i++) {
            if (re.test(el.options[i].text.toLowerCase())) {
              found = true
              el.selectedIndex = i
              break;
            }
        }
    }

    if (e.cancelBubble) {
        e.cancelBubble = true
    }

    // indicate that key's action shouldn't be carried out
    return false
}

function FindInSelect(e, select, input) {
    // get the event
    if (document.all) {
        e = window.event
    }

    // get the key and key as a character
    key = (document.layers ? e.which : e.keyCode)

    // wait until we have an enter
    if (key != 13) {
        return true
    }

    // now, find the appropriate spot in the select
    select = GetObjectByID(select)
    input  = GetObjectByID(input)

    if (input.value == '') {
        alert('Please enter a search value')
    }

    if (select && input) {
        // if this is the first time through, initialize
        if (! input.lastString || input.lastString != input.value.toLowerCase()) {
            select.startIdx = 0
            input.lastString = input.value.toLowerCase()
        }

        // add back in any that match from the original list
        re = new RegExp('.*' + input.value.toLowerCase() + '.*');
        for (var i = 0; i < select.options.length; i++) {
            if (re.test(select.options[select.startIdx+i].text.toLowerCase())) {
                select.selectedIndex = select.startIdx+i
                select.haveMatch = true
                select.startIdx += i+1 // skip over this element on next pass
                break;
            }

            // we hit the bottom
            if (select.haveMatch && select.startIdx+i == select.options.length-1) {
                alert('Search reached end of list items. Continuing from the top.')
                select.startIdx = 0
                select.haveMatch = false
                select.selectedIndex = -1
                i = 0
            }
        }
    }

    if (e.cancelBubble) {
        e.cancelBubble = true
    }

    // indicate that key's action shouldn't be carried out
    return false
}

/*
* At least three ways to do this
* 1. First load, DEEP COPY list.  Delete all from list. Add back in matches
* 2. DEEP COPY deleted. Restore deleted on next load
* 3. Pull matches to the top. Restore on next load
*
* Deleting lots of elements from a select chews CPU
*/
function FilterSelect(e, select, input) {
    // get the event
    if (document.all) {
        e = window.event
    }

    // get the key and key as a character
    key = (document.layers ? e.which : e.keyCode)

    // wait until we have an enter
    if (key != 13) {
        return false
    }

    select = GetObjectByID(select)
    input  = GetObjectByID(input)

    if (select && input) {
        if (! select.origv) {
            // store the original values
        }

        // delete (and save) any non-matching values
        alert('start')
        for (var i = 0, top = 0, lv = input.value.toLowerCase(); i < select.options.length; i++) {
            if (select.options[i].text.toLowerCase().indexOf(lv) >= 0) {
                tmp = select.options[top]
                select.options[top].text = select.options[i].text
                select.options[i].text = tmp.text

                top++
            }
        }
        alert('end')
    }

    if (e.cancelBubble) {
        e.cancelBubble = true
    }

    // indicate that key's action shouldn't be carried out
    return false
}

function whatsthis(msg) {
    newwin = window.open('','','top=150,left=150,width=300,height=225,resizable=yes,scrollbars=yes,status=no');
    if (!newwin.opener)
        newwin.opener = self;

    with (newwin.document) {
        open();
        write('<html><head><title>What\'s This?</title></head>');
        write('<body><p>' + msg + '</p>');
        write('</body></html>');
        close();
   }
}

function addTblRow(id){
    tbl = GetObjectByID(id);
    if (tbl) {
        outerTbl = tbl.children(0);
        lastRow = outerTbl.children[outerTbl.children.length-1];

        newRow = outerTbl.insertRow();
        colCnt = lastRow.children.length;
        for (i = 0; i < colCnt; i++) {
            newCol = newRow.insertCell();
            newCol.innerHTML = lastRow.children[i].innerHTML;
        }
    }
}

function toggleVisibility(id) {
    var elem;
    elem = document.getElementById(id);
    if (elem.style.visibility.toLowerCase() == 'visible') {
        elem.style.visibility = 'hidden';
    } else {
        elem.style.visibility = 'visible';
    }
}

function toggleDisplay(id) {
  var elem;
  elem = document.getElementById(id);
  if (elem.style.display == 'none' ) {
    elem.style.display = 'block';
  }
  else {
    elem.style.display = 'none';
  }
}
