/**
 * javascript table util for jctalentbank
 */

/**
 * Identifier untuk row yang isinya informasi "no data to be shown". Bisa applied sbg nama class atau id.
 * @author Rudi
 */
var _NODATA_ROW_NAME = "trNull";
//added by Shirly, 28 July 2006

//del list content of "Nothing found to display" before insert any data
function checkListContent(oTable) {
    if (oTable != null) {
        if (oTable.rows.length == 1 & oTable.rows.id == "trNull") {
            oTable.deleteRow(0);
        }
    }
}

//insert an empty row in a table
function insertRow(tableObj)
{
    var _tr = document.createElement("tr")
    if (tableObj.tBodies.length == 0)
    {
        var _tbody = document.createElement("tbody")
        tableObj.appendChild(_tbody);
    }
    tableObj.tBodies[0].appendChild(_tr);
    return _tr;
}

function insertOneRow(rowObj, tableObj) {
    if (tableObj.tBodies.length == 0)
    {
        var _tbody = document.createElement("tbody")
        tableObj.appendChild(_tbody);
    }
    tableObj.tBodies[0].appendChild(rowObj);
}

// delete selected row in a table
function deleteRows(arrDel, oTable, removeCallback)
{
    var _tBody = oTable.getElementsByTagName("tbody")[0];

    // check wether there are any checked rows..
    var anyChecked = false;
    if (arrDel.length != null)
    {
        for (var k = 0; k < arrDel.length; k++)
        {
            if (arrDel[k].checked)
            {
                anyChecked = true;
                break;
            }
        }
    }
    else
    {
        if (arrDel.checked) {
            anyChecked = true;
        }
    }

    if (arrDel == null || arrDel.length == 0) // anything to be deleted?
    {
        window.alert("There is no record to be deleted");
    }
    else if (!anyChecked)
    {
        window.alert("Please select record(s) to be deleted");
    }
    else // yes..
    {
        var confirmation = confirm("Are you sure want to delete these rows?");
        if (confirmation) {
            if (arrDel.length != null)
            {
                for (var k = 0; k < arrDel.length; k++)
                {
                    if (arrDel[k].checked)
                    {
                        if(removeCallback!=null) removeCallback(arrDel[k].parentNode.parentNode);
                        _tBody.removeChild(arrDel[k].parentNode.parentNode);
                        k--;
                    }
                }
            }
            else
            {
                if (arrDel.checked)
                {
                    _tBody.removeChild(arrDel.parentNode.parentNode);
                }
            }
        }
    }
}

//insert an empty cell in a row
function insertCell(trObj)
{
    var _td = document.createElement("td")
    trObj.appendChild(_td);
    return _td;
}

//print cell content on selected position row
function createCell(oRow, strWidth, strClassName, strText) {
    var oCell = document.createElement("td");
    if (!isNull(strWidth)) oCell.style.width = '' + strWidth + 'px';
    if (!isNull(strClassName)) oCell.className = strClassName;
    if (!isNull(strText)) oCell.innerHTML = strText;
    oRow.appendChild(oCell);
}

function createOneCell(strWidth, strClassName, strText) {
    var oCell = document.createElement("td");
    if (!isNull(strWidth)) oCell.style.width = '' + strWidth + 'px';
    if (!isNull(strClassName)) oCell.className = strClassName;
    if (!isNull(strText)) oCell.innerHTML = strText;
    return oCell;
}

//get value written between tag <td>..</td>
//e.g. <td><input type="hidden" id="exampleID" value="example"/></td>
//      return value is "example"
function getCellValue(oCell) {
    if (!isNull(oCell)) {
        var oElement = oCell.firstChild;
        return oElement.getAttribute("value").strip();
    } else {
        window.alert('Null object');
    }
}

//get element from given Element
function getObjectByName(sourceElement, targetStr)
{
    var arrChild = sourceElement.childNodes;
    if (arrChild != null)
    {
        if (arrChild.length != null)
        {
            for (var i = 0; i < arrChild.length; i++)
            {
                if (arrChild[i].name == targetStr)
                {
                    return arrChild[i];
                }
            }
        }
        else
        {
            if (arrChild.name == targetStr)
            {
                return arrChild;
            }
        }
    }
    return null;
}

//function getParentRowFromLink(link) {
//    var node = link.parentNode;
//    while(node.tagName!='tr') {
//        node = node.parentNode;
//    }
//    return node;
//}

/**
 * Content table need unique ids to tag each row. This function will do that and implement the id as row id.
 * @author Rudi
 */
function decorateContentTable() {
    // we are supporting up to 3 content tables per page.
    // naming convention for the tables are: tblcontent, tblcontent_1, and tblcontent_2
    var contentTbls = new Array();
    contentTbls[0] = document.getElementById('tblcontent');
    contentTbls[1] = document.getElementById('tblcontent_1');
    contentTbls[2] = document.getElementById('tblcontent_2');
    for (var k = 0; k < 3; k++) {
        if (!isNull(contentTbls[k])) {
            ___doContentTableDecoration(contentTbls[k]);
        }
    }
}

/**
 * In content tables there often a row that shows up when there are no data; "No data to be shown."
 * This row should be marked by class="trNull".
 * This function should remove such row from a content table.
 * @author rudi
 */
function removeNoDataRow(table) {
    var trs = table.rows;
    for (var i = 0; i < trs.length; i++) {
        if (Element.hasClassName(trs[i], _NODATA_ROW_NAME) || trs[i].id == _NODATA_ROW_NAME) {
            table.deleteRow(i);
            // untuk ngulang lagi dari pertama, just in case, siapa tahu
            removeNoDataRow(table);
            break;
        }
    }
}

function tagNewContentRow(row) {
    ___doRowTagging(row);
}

function extractUniqueId(linkId) {
    return linkId.substr(8, 16);
}

function ___doContentTableDecoration(tbl) {
    var bodyElem = tbl.tBodies[0];
    // content selalu ditempatkan di row yang di <tbody>
    if (isNull(bodyElem)) return;
    var trs = bodyElem.getElementsByTagName('tr');
    for (var i = 0; i < trs.length; i++) {
        if (!($(trs[i]).id == 'trNull' || $(trs[i]).hasClassName('trNull'))) {
            ___doRowTagging(trs[i]);
        }
    }
}

function ___doRowTagging(row) {
    var uniqueId = randomString();
    // now the row
    row.id = uniqueId;
    // also change the link
    var editLink = ___findEditLink(row);
    if (!isNull(editLink)) {
        editLink.id = "editLink" + uniqueId;
        //        window.alert("tagging link");   // debug
    }
}

function ___findEditLink(row) {
    var as = row.getElementsByTagName("a");
    for (var i = 0; i < as.length; i++) {
        if (Element.hasClassName(as[i], "editLink__")) {
            return as[i];
        }
    }
    return null;
}

// ngga jalan
//function ___getSubElementsByClassName(parent,className) {
//    for(i=0;i<parent.childNodes.length;i++) {
//        if(parent.getE[i].className==className) {
//            return parent.childNodes[i];
//        }
//    }
//    return null;
//}
