// $Header: /CANSI/wwwroot/prototype/php/js/courseLibrary.js 1     28/07/06 6:23p Rswaminathan $

function $(element_name) {

    return document.getElementById(element_name);
}

function update_grid(lang) {

    var xc = $('gridFilters_discipline_XC');
    var tm = $('gridFilters_discipline_TM');
    var province = $('gridFilters_locations').value;

    var rows = $('courseLibrary_grid').rows;

    var count=0;

    // skip first row (table headers)
    for (var i=1; i < rows.length; i++) {

        // split the id attr ignoring the first key
        var params = rows[i].id.split('_');

        // test for province
        if (province != 'ALL' && province != params[1]) {

            // hide row
            hide_row(rows[i]);

        } else if (!xc.checked && xc.value == params[2]) {

            // hide row
            hide_row(rows[i]);

        } else if (!tm.checked && tm.value == params[2]) {
    
            // hide row
            hide_row(rows[i]);

        } else {

            // show row and update the counter
            if (navigator.appVersion.match(/\bMSIE\b/))
				rows[i].style.display = 'block';
			else
				rows[i].style.display = 'table-row';
            count++;

        }
    }

    // change the status field
   	if (lang == 'fr-CA')
   		$('courseLibrary_gridStats').innerHTML = count + ' Cours';
	else
   		$('courseLibrary_gridStats').innerHTML = count + ' Course' + (count == 1 ? '' : 's');
    if (count > 0)
    {
    	$('courseLibrary_gridStats').style.visibility = 'visible';
    	$('norecords').style.display = 'none';
    }
    else
    {
    	$('courseLibrary_gridStats').style.visibility = 'hidden';
    	$('norecords').style.display = 'block';
    }
}

function hide_row(row_id) {

    row_id.style.display = 'none';
}

// idea from javascript anthology by james edwards, cameron adams, sitepoint publishing
function sortColumn(a) {

    var th = a.parentNode;
    var tr = th.parentNode; // <tr> that holds the <th>'s in the <thead>
    var ths = tr.getElementsByTagName('th');
    var table = tr.parentNode.parentNode;
    var tbody = table.getElementsByTagName('tbody')[0];
    var trs = tbody.getElementsByTagName('tr'); // <tr>'s that hold the data rows in th <tbody>

    // the index of the clicked column
    var column = 0;

    // first loop through all the <th>'s to figure out the index
    // of the clicked column and to set the css class names
    // and to set the special sortOrder property on the <th>

    // loop through all of the <th>'s inside the <thead>
    for (var i=0; i < ths.length; i++) {

        // ths[i].className = ths[i].className == 'sort-asc' ? 'sort-desc' : 'sort-asc';
        
        // if the current <th> is the same as the one clicked on
        if (ths[i] == th) {

            // set the clicked column index to be i
            column = i;
            
            // if the clicked <th> was sorted desc and is re-clicked
            if (ths[i].sortOrder == 'desc' && ths[i].clicked) {

                // revert the <th> to the original state
                ths[i].sortOrder = 'asc';
                ths[i].className = 'sort-asc';
                a.title = 'Sort descending';

            } else { // either the clicked <th> was sorted asc or is not re-clicked

                // if the clicked <th> was sorted asc and is not re-clicked
                if (ths[i].sortOrder == 'asc' && !ths[i].clicked) {

                    ths[i].className = 'sort-asc';

                } else { // clicked <th> was sorted asc but is re-clicked

                    ths[i].sortOrder = 'desc';
                    ths[i].className = 'sort-desc';
                    a.title = 'Sort ascending';

                }

            }

            // set the current clicked property to true
            ths[i].clicked = true;

        } else { // for all other <th>'s

            ths[i].clicked = false;
            ths[i].className = '';
            ths[i].firstChild.title = ths[i].sortOrder == 'asc' ? 'Sort ascending' : 'Sort descending';

        }

    }


    // make a copy of the <tbody>
    // pass false to cloneNode as we don't care about children
    var tbody_new = tbody.cloneNode(false);

    // loop through the <tr>'s inside the <tbody>
    for (var i=0; i < trs.length; i++) {

        var trs_new = tbody_new.childNodes;

        // the text within the <td> being ordered of the current row
        var value = getAllInsideText(trs[i].cells[column]);

        // loop through the <tr>'s within the <tbody> of the copy
        for (var j=0; j < trs_new.length; j++) {

            // get all text within the <td> of the current <tr> inside the copy
            var value_new = getAllInsideText(trs_new[j].cells[column]);

            // if the two values to be compared are numeric, cast them like so
            if (value == parseInt(value, 10) && value_new == parseInt(value_new, 10)) {

                value     = parseInt(value, 10);
                value_new = parseInt(value_new, 10);

            } else if (value == parseFloat(value, 10) && value_new == parseFloat(value_new, 10)) {

                value     = parseFloat(value, 10);
                value_new = parseFloat(value_new, 10);

            }

            if (ths[column].sortOrder == 'desc') {

                if (value >= value_new) {
                    break;
                }

            } else {

                if (value <= value_new) {
                    break;
                }

            }

        }

        // handles the first case (i=0) when there are no rows in tbody_new
        if (j >= trs_new.length) {

            tbody_new.appendChild(trs[i].cloneNode(true));
            
        } else {

            tbody_new.insertBefore(trs[i].cloneNode(true), trs_new[j]);

        }

    }

    table.replaceChild(tbody_new, tbody);
    return false;

}

// also from javascript anthology
function getAllInsideText(node) { // node will usually be a <td> if called from outside this function

    nodeChildren = node.childNodes;
    var insideText = '';

    // loop through all children of <td>
    // can be <span>'s, <a>'s, <strong>'s, <em>'s, etc., or just a pure text node
    for (var i=0; i < nodeChildren.length; i++) {

        // if the child is just a plain text node
        if (nodeChildren[i].nodeType == 3) {

            // ensure the textNode is not just whitespace
            if (!/^\s*$/.test(nodeChildren[i].nodeValue)) {

                // append the text node contents to our local variable
                insideText += nodeChildren[i].nodeValue;

            } 

        } else { // if the childNode is an element node

                // make a recursive call to the same function to get all text 
                // within the element node, passing the element node as argument
                // to the function
                insideText += getAllInsideText(nodeChildren[i]);

        }                

    }

    return insideText;

}
