// Table client-side support
// (c) SimX Corporation 2010

if( !tablescr_loaded ) {

var ELEMENT_NODE = 1;
var TEXT_NODE = 3;

var TableScr = function( table_id, sel_en )
{
    this.autoscroll = false;
    this.table_obj = GetElem(table_id);
    this.tbody_obj = GetElem(table_id + "_tbody");
    this.sel_en = sel_en ? true : false;
    this.ProcSel = null;
    var $this = this;
    updates[table_id] = function() { $this.Refresh(); };
}

TableScr.prototype = {
    Refresh : function() {
        var style = this.table_obj.style;
        var scroll = this.autoscroll && ( style.overflow == "scroll" || style.overflow == "auto" ||
            style["overflow-y"] == "scroll" || style["overflow-y"] == "auto" );
        var $this = this;
        for( var i = 0; i < this.tbody_obj.rows.length; i++ ) {
            var row = this.tbody_obj.rows[i];
            if( this.ProcSel ) {
                if( !this.nohover ) {
                    if( !row.onmouseout )  row.onmouseout  = function() { $this.highl(this, 0); }
                    if( !row.onmouseover ) row.onmouseover = function() { $this.highl(this, 1); }
                }
                if( !row.onmousedown ) row.onclick = function() { $this.selRow(this, this.id); }
                var inputs = row.getElementsByTagName( "input" );
                if( inputs.length > 0 )
                    inputs[0].onclick = function( e ) { changeHandler( this.id, e ); if( $this.selchkd ) $this.RefreshMultiSelection(); }
                row.style.cursor = "hand";
            }
            if( scroll && row.className == "rowcur" )
                this.table_obj.scrollTop = row.offsetTop > 100 ? row.offsetTop-100 : row.offsetTop;
        }
    },
    highl : function( row_obj, mode_bool ) {
        if( row_obj.className != "rowcur" && row_obj.className != "rowsel" )
          if( mode_bool ) {
              row_obj.genClassName = row_obj.className;
              row_obj.className = "rowhl";
          } else
              row_obj.className = row_obj.genClassName;
    },
    selRow : function( row_obj, recid ) {
        if( this.sel_en && row_obj ) {
            for( var i = 1; i <= this.tbody_obj.rows.length; i++ ) {
                var row = this.tbody_obj.rows[i-1];
                row.className = i % 2 == 0 ? "roweven" : "rowodd";
            }
            row_obj.className = "rowcur";
        }
        if( this.ProcSel )
            this.ProcSel( recid );
    },
    RefreshMultiSelection : function() {
        for( var i = 0; i < this.tbody_obj.rows.length; i++ ) {
            var row_obj = this.tbody_obj.rows[i];
            var checkboxes = row_obj.getElementsByTagName( "INPUT" );
            if( checkboxes.length ) {
                if( checkboxes[0].checked )
                    row_obj.className = "rowcur";
                else
                    row_obj.className = (i+1) % 2 ? "rowodd" : "roweven";
            }
        }
    }
};

TableScr.IsNumRowsColumnsSame = function( new_rows, old_rows )
{
    {
        var j = 0;
        for( var i = 0; i < new_rows.length; i++ ) {
            if( new_rows[i].nodeType == ELEMENT_NODE && new_rows[i].nodeName == "row" ) {
                if( j++ > old_rows.length ) return false;
            }
        }
        if( !j || !old_rows.length || j != old_rows.length ) return false;
    }
    {
        var new_cells = new_rows[0].childNodes;
        var old_cells = old_rows[0].cells;
        var j = 0;
        for( var i = 0; i < new_cells.length; i++ ) {
            var new_cell = new_cells[i];
            if( new_cell.nodeType == ELEMENT_NODE ) {
                if( j++ > old_cells.length ) return false;
            }
        }
        return j == old_cells.length ? true : false;
    }
}

TableScr.UpdateTable = function( table_obj, newrows, act_recid )
{
    var tbody_obj = GetElem( table_obj.id + "_tbody" );

    for( var i = 0; i < newrows.length; i++ ) {
    if(p) console.info('row');
        var row_obj = tbody_obj.rows[i];
        var new_row = newrows[i];

        if( !new_row ) break;

        if( new_row.getAttribute('type') == 'thead' )
            return TableScr.UpdateTableHeader( table_obj.getElementsByTagName( "THEAD" )[0], new_row.firstChild );

        if( !row_obj ) continue;

        row_obj.className = new_row.getAttribute('RowClass');
        var row_id = new_row.getAttribute('recid');
        row_obj.id = row_id;

        var row_style = new_row.getAttribute( "style" );
        if( row_style )
            row_obj.style.cssText = row_style;

        var old_cell = row_obj.cells[0];
        var new_cell = new_row.firstChild;
        while( new_cell ) {
            if( !old_cell )
                old_cell = row_obj.appendChild(document.createElement('td'))
            if( new_cell.nodeName == "item" ) TableScr.UpdateCheckbox( old_cell, row_id, new_cell, new_row, row_obj, i );
            else {
                var cell_style = new_cell.getAttribute( "style" );
                if( cell_style )
                    old_cell.style.cssText = cell_style;

                if( new_cell.firstChild ) {
                    var cell_elem = old_cell;
                    var items = new_cell.getElementsByTagName( "item" );
                    if( items && items.length ) {
                        var item = items[0];
                        var elem_id = new_cell.firstChild.getAttribute("control_id");
                        if( elem_id && elem_id.length ) {
                            var a_elem = GetElemFrom( old_cell, elem_id );
                            if( a_elem ) {
                                ProcAttr( "href", item, a_elem );
                                cell_elem = a_elem;
                            }
                        }
                    }
                    else {
                        var a_list = old_cell.getElementsByTagName( "A" );
                        if( a_list && a_list.length )
                            cell_elem = a_list[0];
                    }
                    var xml_imgs = new_cell.getElementsByTagName( "IMG" );
                    if( xml_imgs && xml_imgs.length ) {
                        var html_imgs = cell_elem.getElementsByTagName( "IMG" );
                        if( html_imgs && html_imgs.length )
                            copyAttrs( xml_imgs[0], html_imgs[0] );
                    }
                    else {  // different browsers API:
                        cell_elem.innerHTML = new_cell.text ? new_cell.text : new_cell.textContent ? new_cell.textContent : "???";
                    }
                }
                else
                    old_cell.innerHTML = "";
            }
            do { new_cell = new_cell.nextSibling; } while( new_cell && new_cell.nodeType != ELEMENT_NODE );
            if( new_cell )
                do { old_cell = old_cell.nextSibling; } while( old_cell && old_cell.nodeType != ELEMENT_NODE );
        }
    }
    return true;
}

TableScr.UpdateCheckbox = function( old_cell, row_id, new_cell, new_row, row_obj, i )
{
	try{
    	var chkb = old_cell.getElementsByTagName("INPUT")[0];
   	} catch(e) {}

    if( chkb ) {
        chkb.setAttribute( "value", row_id );
        chkb.checked = new_row.getAttribute('checked') ? true : false;
        if( this.selchkd ) {
            if( chkb.checked )
                row_obj.className = "rowcur";
            else
                row_obj.className = (i+1) % 2 ? "rowodd" : "roweven";
        }
    }
    else {
        var chkb = document.createElement("INPUT");
        if( chkb ) {
            chkb.type = "checkbox";
            old_cell.appendChild( chkb );
            chkb.id = chkb.name = new_cell.getAttribute( "control_id" ) + "_" + new_row.getAttribute("num");
            chkb.setAttribute( "value", row_id );
            chkb.onmousedown = function() { event.cancelBubble = true; if( event.stopPropagation ) event.stopPropagation(); }
        }
    }
}

TableScr.UpdateTableHeader = function( thead, headers )
{
     try {
        var newrows = headers.childNodes;
        var i = 0;
        for( var new_row = headers.firstChild; new_row; new_row = new_row.nextSibling ) {
            if( new_row.nodeType == ELEMENT_NODE && new_row.nodeName == 'TR' ) {
                var  row_obj = thead.rows.item( i++ );
                if( !row_obj ) row_obj = thead.insertRow( -1 );
                var new_cell = new_row.firstChild;
                var j = 0;
                while( new_cell ) {
                    if( new_cell.nodeType == ELEMENT_NODE ) {
                        var  html_cell = row_obj.cells.item( j++ );
                        if( !html_cell ) html_cell = row_obj.insertCell( -1 );
                        if( new_cell.firstChild && new_cell.firstChild.nodeType == TEXT_NODE ) {
	                        var t = new_cell.firstChild.nodeValue;
	                        if( t != html_cell.innerHTML ) {
	                            t = t.replace( "<", "&lt;" );
	                            t = t.replace( ">", "&gt;" );
                                html_cell.innerHTML = t;
                            }
                            var align = new_cell.getAttribute( "align" );
                            if( align ) html_cell.align = align;
                            html_cell.onclick = new Function( "event", new_cell.getAttribute( "onclick" ) );
                        }
                    }
                    new_cell = new_cell.nextSibling;
                }
                while( row_obj.cells.item( j ) )
                    row_obj.deleteCell( j );
            }
        }
        while( thead.rows.item( i ) )
            thead.deleteRow( i );
    }
    catch( e ) {
        var descr = e.description ? e.description : e.message;
        alert( 'Table header regeneration caused an exception: ' + descr );
    }
    return true;
}

var UpdateTableGrid = function( ctr, pages_nodes )
{
    try {
        var a = new Array;
        for( var i = 0; i < pages_nodes.length; i++ ) {
            var name = pages_nodes.item(i).getAttribute( "name" );
            if( pages_nodes.item(i).getAttribute( "current" ) ) {
                a[a.length] = "<B class=\""
                a[a.length] = ctr.id;
                a[a.length] = "_curfont\">";
                a[a.length] = name;
                a[a.length] = "</B>&nbsp;";
            }
            else {
                a[a.length] = "<A href=\"javascript:void(0);\" onclick=\"GridSbm_" + ctr.id + "('";
                a[a.length] = name;
                a[a.length] = "');\" class=\""
                a[a.length] = ctr.id;
                a[a.length] = "_font\">";
                a[a.length] = name;
                a[a.length] = "&nbsp;</A>";
            }
        }
        ctr.innerHTML = a.join("");
    }
    catch( e ) {
        var descr = e.description ? e.description : e.message;
        alert( 'Table grid regeneration caused an exception: ' + descr );
    }
    return true;
}

var tablescr_loaded = true;

}

