//////////////////////////////////////////////////////////////////////
//  Utils.js
//////////////////////////////////////////////////////////////////////

// General-purpose utilities


//////////////////////////////////////////////////////////////////////
//  Generic utilities
//////////////////////////////////////////////////////////////////////

function isblank(s) {
    for(var i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
    }
    return true;
}

function trim(s) {
    if (s == null) return null;
    return s.replace(/(^\s+)|(\s+$)/g, "");
}

function quote(s) { return '"'+s+'"'; }
function bra(s) { return '('+s+')'; }
function sqrBra(s) { return '['+s+']'; }
function angBra(s) { return '<'+s+'>'; }
function escAngBra(s) { return '&lt;'+s+'&gt;'; }


//////////////////////////////////////////////////////////////////////
//  Form utilities
//////////////////////////////////////////////////////////////////////

// Returns the index of radio button selected
function getRadioSelectedIdx(field) {
    for (i = 0; i < field.length; i++) {
        if (field[i].checked) return i;
    }
    return -1;
}

// Returns an array of all text options selected (can be made by 1 element).
function getSelOptionsSelected(field) {
    ret = new Array();
    //alert("Elements: " + field.options.length);
    for (i = 0; i < field.options.length; i++) {
        if (field.options[i].selected) {
            //alert("Found: " + field.options[i].text);
            ret[ret.length] = field.options[i].text;
        }
    }
    return ret;
}

//////////////////////////////////////////////////////////////////////
//  DOM utilities
//////////////////////////////////////////////////////////////////////

function getChildNodesByTagName(node, childTagName) {
    var childs = node.childNodes;
    var selChilds = new Array();
    var j=0;
    for (i=0; i < childs.length; i++)
        if (childs.item(i).tagName==childTagName)
            selChilds[j++] = childs.item(i);
    return selChilds;
}

function getParentNodesByTagName(node, requiredTagName, stopNode) {
    var selParents = new Array();
    var j=0;
    var curNode = node.parentNode;
    while (curNode!=null && curNode!=stopNode) {
        if (curNode.tagName == requiredTagName)
            selParents[j++] = curNode;
        curNode = curNode.parentNode;
    }
    return selParents;
}

//////////////////////////////////////////////////////////////////////
//  Debug utilities
//////////////////////////////////////////////////////////////////////

var isDebugOn;
var debugOut;

function setDebugWindow(debugWindow, debugOnOff) {
    debugOut = debugWindow;
    isDebugOn = debugOnOff;
}

function debug(t) {
    if (!isDebugOn) return;
    var p = document.createElement("p");
    p.appendChild(document.createTextNode(t));
    debugOut.appendChild(p);
}

function clearDebug() {
    if (!isDebugOn) return;
    while (debugOut.hasChildNodes())
        debugOut.removeChild(debugOut.lastChild);
}

