/*
* A very simple script to filter a table according to search criteria
*
* http://leparlement.org/filterTable
* See also http://www.vonloesch.de/node/23
*/


// term - the terms to find that was inputed by the user
// table - object reference to the table to search
// startAsShown - true if the table started as shown vs start as hidden
// hideRows - allow the rows filtered out rows to be hidden or not.
function filterTable3(term, table, firstrowIsHeader, startsAsShown, hideRows) {
    dehighlight(table);
    var terms = term.value.toLowerCase().split(" ");
    var startingRowNumber = 0;

    if (firstrowIsHeader === true) {
        startingRowNumber = 1;
    }

    // skip the first row since it usually a header row
    for (var r = startingRowNumber; r < table.rows.length; r++) {
        var display = '';
        if (startsAsShown === false) {
            display = 'none';
        }
        for (var i = 0; i < terms.length; i++) {
            if (table.rows[r].innerHTML.replace(/<[^>]+>/g, "").toLowerCase().indexOf(terms[i]) < 0 && hideRows === true) {
                display = 'none';
            } else {
                if (terms[i].length) {
                    highlight(terms[i], table.rows[r]);
                    display = 'block';
                }
            }
            table.rows[r].style.display = display;
        }
    }
}

function filterTable2(term, table) {
    dehighlight(table);
    var terms = term.value.toLowerCase().split(" ");

    if (table.rows.length == 1) {
        var display = '';
        for (var i = 0; i < terms.length; i++) {
            if (table.rows[0].innerHTML.replace(/<[^>]+>/g, "").toLowerCase().indexOf(terms[i]) < 0) {
                display = 'none';
            } else {
                if (terms[i].length) highlight(terms[i], table.rows[0]);
            }
            table.rows[0].style.display = display;
        }
    } else {
        for (var r = 1; r < table.rows.length; r++) {
            var display = '';
            for (var i = 0; i < terms.length; i++) {
                if (table.rows[r].innerHTML.replace(/<[^>]+>/g, "").toLowerCase().indexOf(terms[i]) < 0) {
                    display = 'none';
                } else {
                    if (terms[i].length) highlight(terms[i], table.rows[r]);
                }
                table.rows[r].style.display = display;
            }
        }
    }
}


/*
* Transform back each
* <span>preText <span class="highlighted">term</span> postText</span>
* into its original
* preText term postText
*/
/* TODO: This function needs to be fixed to fix escaping issues */
function dehighlight(container) {
    for (var i = 0; i < container.childNodes.length; i++) {
        var node = container.childNodes[i];

        if (node.attributes && node.attributes['class']
			&& node.attributes['class'].value == 'highlighted') {
            node.parentNode.parentNode.replaceChild(
					document.createTextNode(node.parentNode.innerText || node.parentNode.textContent),
					node.parentNode);
            // Stop here and process next parent
            return;
        } else if (node.nodeType != 3) {
            // Keep going onto other elements
            dehighlight(node);
        }
    }
}

/*
* Create a
* <span>preText <span class="highlighted">term</span> postText</span>
* around each search term
*/
function highlight(term, container) {
    for (var i = 0; i < container.childNodes.length; i++) {
        var node = container.childNodes[i];

        if (node.nodeType == 3) {
            // Text node
            var data = node.data;
            var data_low = data.toLowerCase();
            if (data_low.indexOf(term) >= 0) {
                //term found!
                var new_node = document.createElement('span');

                node.parentNode.replaceChild(new_node, node);

                var result;
                while ((result = data_low.indexOf(term)) != -1) {
                    new_node.appendChild(document.createTextNode(
								data.substr(0, result).replace(/&nbsp;/g, '\u00a0')));
                    new_node.appendChild(create_node(
								document.createTextNode(data.substr(result, term.length).replace(/&nbsp;/g, '\u00a0'))));
                    data = data.substr(result + term.length);
                    data_low = data_low.substr(result + term.length);
                }
                new_node.appendChild(document.createTextNode(data));
            }
        } else {
            // Keep going onto other elements
            highlight(term, node);
        }
    }
}

function create_node(child) {
    var node = document.createElement('span');
    node.setAttribute('class', 'highlighted');
    node.attributes['class'].value = 'highlighted';
    node.appendChild(child);
    return node;
}



/*
* Here is the code used to set a filter on all filterable elements, usually I
* use the behaviour.js library which does that just fine
*/

// JScript source code

