/* -*- javascript -*-
     Copyright 2005 TJM Enterprises, Inc..
     All Rights Reserved
     System        : SV_JS :
     Object Name   : $RCS_FILE$
     Revision      : $REVISION$
     Date          : Tue Sep 27 14:03:26 2005
     Created By    : Bradford McKesson, TJM Enterprises, Inc.
     Created       : Tue Sep 27 14:03:26 2005

     Last Modified : <030310.0724>
     ID            : $Id: js.etf,v 2.5 2004/03/25 21:58:00 jon Exp $
     Source        : $Source: /export/cvs/cvsroot/me/macros/js.etf,v $
     Description
     Notes
     $Log: js.etf,v $
     Revision 2.5  2004/03/25 21:58:00  jon
     Final release from steve

 */

// common functions
var ns4 = document.layers;
var ie4 = document.all;
var nn6 = document.getElementById && !document.all;

// Search through multiple windows and frames for a particular
// element
// return the element name inId
function find_frame(inWindow, inId) {
    if (inWindow.debug) {
        inWindow.debug('find_frame('+inWindow.name+', ' + inId+')');
    }
    var fr = undefined;
    if (inWindow.document) {
        fr = inWindow.document.getElementById(inId);
    }
    if (!fr) {
        if (inWindow.frames) {
            var ic = inWindow.frames.length;
            for(var i = 0; i < ic; i++) {
                fr = find_frame(inWindow.frames[i].window, inId);
                if (fr) {
                    break;
                }
            }
        }
    }
    if (fr && (fr instanceof Element)) {
        // fr = fr.ownerDocument.contentWindow;
    }
    return fr;
}

// change the size of inFrameWindow to inSize.
// inSize is a percentage 0-100
function resize_frameset(inFrameWindow, inSize) {
    /* plan:
       find inFrameWindow's name,
       find inFrameWindow's parent,
       find <frameset> in parent
       find position of the <frame> with inFrameWindow's name
       replace that position's row or col value with inSize
     */

    var frname = inFrameWindow.name;
    var frparent = inFrameWindow.parent;
    if (frparent == inFrameWindow) {
        // inFrameWindow is not a <frame>
        error("resize_frameset: cannot resize a non-frame window");
    }
    // debug('parent object');
    // print_obj(frparent.frames[frname], debug);

    var frameset_list = frparent.document.getElementsByTagName('frameset');
    var frameset = frameset_list[0];
    if (!frameset) {
        error("resize_frameset: cannot find frameset");
        return;
    }

    // position in frameset
    var pos = -1;
    var i = 0;
    for(i = 0; i < frparent.frames.length; i++) {
        if (frparent.frames[i] == frparent.frames[frname]) {
            pos = i;
            break;
        }
    }

    if (pos == -1) {
        error('resize_frame: cannot find '+frname+' position');
    }


    // do resize
    var sizes = frameset.rows?frameset.rows:frameset.cols;
    var sp = sizes.split(',');
    for (i = 0; i < sp.length; i++) {
        sp[i] = '*';
    }
    sp[pos] = inSize +'%'; // should really scale them down.

    if (frameset.rows) {
        frameset.rows = sp.join(',');
    } else {
        frameset.cols = sp.join(',');
    }
} // resize_frameset

// predetermined frame sizes
function frame_enlarge() {
    resize_frameset(window, '90');
}
function frame_normal() {
    resize_frameset(window, '50');
}
function frame_shrink() {
    resize_frameset(window, '10');
}


function frame_back() {
    // This function should not be used to return to a 'POST'ed page.
    // Restated: if the current page URL has or may have 'action' defined
    // then this function should not be called.
    // Specifically excludes summary_list (result of a query/form submisison).
    if (top.history && top.history.length && top.history.back) {
        // Gecko
        top.history.back();
    } else {
        window.history.go(-1);
    }
}

// Debug support
// print members of an object, passing string to function inPrinter
// example: print_obj(window, debug)
function print_obj(inObj, inPrinter) {
    var v = null;
    for(var k in inObj) {
        v = (inObj[k] && inObj[k].toString)?inObj[k].toString():inObj[k];
        inPrinter('object.'+k+'='+v);
    }
} // print_obj

//
// Home method
// adjust the frame sizes to fit data, minimize wasted space in
// top (data) frame.

// Home method
// reload the data frame
function reload_data(inURL) {
    if (parent.frames.data && parent.frames.data != window) {
        if(!inURL) {
            // this is really an error
            inURL = "http://ems.tjm-secure.com/service/index.php?view=current_data";
        }
        parent.frames.data.location.href = inURL;
        if (top.do_resize) {
            top.do_resize = 0;
        } else {
            top.do_resize = 1;
        }
    }
}

// mimic the PHP is_object() function
// Like PHP, a string is NOT an object although
// JS allows it to be treated as such.
function is_object(inCandidate) {
    return ('object' == typeof(inCandidate));
}

function is_string(inCandidate) {
    return ('string' == typeof(inCandidate));
}

// return true if inCandidate is an actual number
// (return false if inCandidate is a Number object)
function is_numeric(inCandidate) {
    return ('number' == typeof(inCandidate));
}

// mimic PHP is_array() function
function is_array(inCandidate) {
    return (inCandidate instanceof Array);
}

function is_function(inFunc) {
    return (typeof(inFunc) == 'function');
}

function isset(inCandidate) {
    var c = typeof(inCandidate);
    return ((c != 'undefined') && (c != null) && (!isNaN(inCandidate)));
}

// Attempt to copy an object instead of assign a ref to it.
// Usage: receive = clone(what);
// derived from http://www.irt.org/script/879.htm
// NOTE: typeof(clone(what)) == typeof(what) iff what is an object
// NOTE: instanceof(what) != instancof(clone(what))
function clone(what) { return new _cloneObject(what); }
function _cloneObject(what) {
    for (i in what) {
        if (typeof what[i] == 'object') {
            this[i] = new _cloneObject(what[i]);
        } else {
            this[i] = what[i];
        }
    }
}

// convert an object to a sensible human readable string
// usage: window.alert(to_string(inObj))
function to_string(inObj, inPrefix) {
    if (!inPrefix) {
        inPrefix = 'object';
    }
    var out = '';
    for (i in inObj) {
        if (typeof(inObj[i]) == 'object') {
            var nprefix = inPrefix + '.' + i;
            out = out + to_string(inObj[i], nprefix) + ";\n";
        } else {
            out = out + inPrefix + '.' + i + '=' + inObj[i] + ";\n";
        }
    }
    return out;

}
// convert an object to a sensible human readable string
// usage: window.alert(to_string(inObj))
function to_string_shallow(inObj, inPrefix) {
    if (!inPrefix) {
        inPrefix = 'object';
    }
    var out = '';
    for (i in inObj) {
        if (typeof(inObj[i]) == 'object') {
            var nprefix = inPrefix + '.' + i;
            out = out + inPrefix + '.' + i + '= [object];\n'  ;
        } else {
            out = out + inPrefix + '.' + i + '=' + inObj[i] + ";\n";
        }
    }
    return out;

}

//
// replace first occurrence of substr with repstr in src
// return modified string
function str_replace(src, substr, repstr) {
    var start = src.indexOf(substr);
    if (start < 0) {
        return src;
    }
    var stop = start + substr.length;
    var pre = src.substr(0, start);
    var post = src.substr(stop);
    return '' + pre + repstr + post;
}

//
// replace last occurrence of substr with repstr in src
// return modified string
function str_replace_last(src, substr, repstr) {
    var start = src.lastIndexOf(substr);
    if (start < 0) {
        return src;
    }
    var stop = start + substr.length;
    var pre = src.substr(0, start);
    var post = src.substr(stop);
    return '' + pre + repstr + post;
}

// Convert DOM nodeList into a javascript array.
// Empty members of inNodeList will be skipped unless
// inKeepEmptyElemnts == true
// NodeList members are appended to ioArray, preserving existing
// data in ioArray
function append_nodelist(ioArray, inNodeList, inKeepEmptyElements) {
    var len = inNodeList.length;
    var i = 0;
    for(i = 0; i < len; i++) {
        if (inNodeList[i] || inKeepEmptyElements) {
            ioArray.push(inNodeList[i]);
        }
    }
}

// Do PHP-like urlencode.
// basically do escape() then encode +
// Supposedly encodeURIComponent() is better than escape(),
// but not available pre FF 1.5 (JS 1.5)
function urlencode(inString) {

    var s = escape(inString);
    var re = /\+/gi;
    var s2 = s.replace(re, "%2b"); // none of them do +
    return s2;

}

// Taken from
// http://developer.mozilla.org/en/docs/Browser_Detection_and_Cross_Browser_Support
//
// return the rv value of a Gecko user agent
// as a floating point number.
// returns -1 for non-gecko browsers,
//          0 for pre Netscape 6.1/Gecko 0.9.1 browsers
//          number > 0 where each portion of
//          the rv value delimited by .
//          will be treated as value out of 100.
//          e.g. for rv: 3.12.42,
//          getGeckoRv() returns 3.1242
//
function geckoGetRv()
{
    if (navigator.product != 'Gecko')
    {
        return -1;
    }
    var rvValue = 0;
    var ua      = navigator.userAgent.toLowerCase();
    var rvStart = ua.indexOf('rv:');
    var rvEnd   = ua.indexOf(')', rvStart);
    var rv      = ua.substring(rvStart+3, rvEnd);
    var rvParts = rv.split('.');
    var exp     = 1;

    for (var i = 0; i < rvParts.length; i++)
    {
        var val = parseInt(rvParts[i]);
        rvValue += val / exp;
        exp *= 100;
    }

    return rvValue;
}

