function table_obj()
{
	this.No_of_Cols;
	this.controlName;
	this.containerName;
	this.HeadDiv;
	this.ContentDiv;
        this.Width = 0;
        this.Height = 0;
	this.headDivID;
	this.headClass;
	this.DataDivID;
	this.DataClass;
        this.headStyle;
	this.tableStyle;
 	this.dataStyleLight;
 	this.dataStyleDark;
        this.colorChangeFlag;
	this.TDwidth   = new Array();
        this.TDAlign   = new Array();
	this.HeadTitle = new Array();
	this.data      = new Array();
	
	this.SetCols             = SetCols;
	this.SetRows             = SetRows;
	this.SetData             = SetData;
	this.SetStyle            = SetStyle;
	this.SetControlName      = SetControlName;
	this.SetCellWidth        = SetCellWidth;        
	this.GenTable            = GenTable;
	this.SetHeadTitle        = SetHeadTitle;
        this.SetHead_prop        = SetHead_prop;
	this.SetContent_prop     = SetContent_prop;
	this.SetHeightWidth      = SetHeightWidth;
	this.Sort                = Sort;
        this.adjust_title_scroll = adjust_title_scroll;
        this.SetTheme            = SetTheme;
        this.Add_New_Row         = Add_New_Row;
}

function Sort(colNo,DataType)
{
  var arr1 = this.data;

//create index row
    for ( i=1; i < arr1.length; i++)
    {
      arr1[i][0]  = i;
    }
  
  var tmp_arr = new Array();
  var tmp_row = new Array();

//tmp array with row no and col to be sorted.
    for ( i=0; i < arr1.length; i++)
    {
      tmp_row[i]  = new Array();
    }

//sort on col 
    var sortCol = colNo;    
    var totCols = this.No_of_Cols;

    for (var i=1; i < arr1.length; i++)
    { 
      for ( var j=0; j < totCols; j++)
      {
	if(j == sortCol)
        {
          tmp_arr[i] = arr1[i][j];
          tmp_row[i][1] = arr1[i][j];
        }

	if(j == 0)
        {
          tmp_row[i][j] = arr1[i][j];
        }
      }
    }

//sort tmp_arr assuming numerical data
    if ( DataType == "SORTNUM" )
      tmp_arr.sort(compareNo);
    else
      tmp_arr.sort();

//Find row position in tmp_arr (index) and update in tmp_row
    for ( i=0; i < tmp_row.length; i++)
    {
      for (var k=0; k < tmp_arr.length; k++)
      {
        if ( tmp_arr[k] == tmp_row[i][1] )
        {
	  tmp_arr[k] = -1;	
	  tmp_row[i][0] = k;
          break;
	}	
      }
    }

//  sort_arr1 is a new array to be filled with all rows sorted
    var sort_arr1 = new Array();

    for ( i=1; i < arr1.length ; i++)
    {

       var new_pos = tmp_row[i][0];
       sort_arr1[new_pos] = arr1[i];
    }

    for ( i=1; i < sort_arr1.length; i++)
    {
       sort_arr1[i][0] = i;
    }

    arr1 = sort_arr1;

    for ( i=0; i < arr1.length; i++)
    {
       this.data[i+1] = arr1[i];
    }

    this.GenTable(this.containerName);

}

function compareNo(a,b)
{ return a - b; }

function SetStyle(headStyle, dataStyleLight,dataStyleDark,tableStyle)
{
  this.headStyle      = headStyle;
  this.dataStyleLight = dataStyleLight;
  this.dataStyleDark  = dataStyleDark;
  this.tableStyle     = tableStyle;
}

function SetTheme(themeNo)
{
  this.headStyle      = "title_" + themeNo;
  this.dataStyleLight = "light_" + themeNo;
  this.dataStyleDark  = "dark_" + themeNo;
  this.tableStyle     = "border_" + themeNo;
}

function SetData(row,col,data)
{
  this.data[row][col] = data;
}

function SetRows(rows)
{
  for(var i=1; i <= rows; i++)
  {
    this.data[i] = new Array(this.No_of_Cols);
    for(var j=1; j <= this.No_of_Cols; j++)
    {
      this.data[i][j] = '';
    }
  }
}

function  SetHeightWidth(tableHeight,tableWidth)
{
	this.Height = tableHeight;
	this.Width  = tableWidth;
}

function SetControlName(controlName)
{
	this.controlName = controlName;
}


function SetHead_prop(divID,Class)
{
	this.headDivID = divID;
	this.headClass = Class;
}

function SetContent_prop(divID,Class)
{
	this.DataDivID = divID;
	this.DataClass = Class;
}

function SetCols(cols)
{
  this.No_of_Cols = cols;
  for ( var i=0; i <= cols; i++)
  {
    this.TDwidth[cols] = 0;	
  }
}

function SetCellWidth(CellNo, CellWidth,Align)
{
  this.TDwidth[CellNo] = CellWidth;
  this.TDAlign[CellNo] = Align;
}

function SetHeadTitle(CellNo,CellContent,SortFlag)
{
  if (SortFlag != 'SORTNUM' && SortFlag != 'SORTCHAR')
  {
    this.HeadTitle[CellNo] = CellContent;
  }
  else
  {
    this.HeadTitle[CellNo] = "<a class=" + this.headStyle + " href=javascript:" + this.controlName + ".Sort(" + CellNo + ",'" + SortFlag +"')>" + CellContent + "</a>";
  }
}

function GenTable(container)
{

  this.containerName = container;
  this.containerName.className    = 'control_border';
  this.containerName.style.width  = this.Width+2;
  this.containerName.style.Height = this.Height;

  if ( this.HeadDiv == null) 
    this.HeadDiv             = document.createElement("DIV");

  this.HeadDiv.id          = this.headDivID;
  this.HeadDiv.style.width = this.Width;
  this.HeadDiv.className   = this.headClass;

  if ( this.ContentDiv == null) 
    this.ContentDiv              = document.createElement("DIV");
  else
    this.ContentDiv.innerHTML   = "";

  this.ContentDiv.id           = this.DataDivID;
  this.ContentDiv.style.width  = this.Width;
  this.ContentDiv.style.height = this.Height;
  this.ContentDiv.className    = this.DataClass;

  var headTable = document.createElement("TABLE");
  headTable.className = this.tableStyle;
  headTable.border = 1;
  headTable.cellPadding = 2;
  var myRow     = headTable.insertRow();
  for(var i=1; i <= this.No_of_Cols; i++)
  {
    myCell    = myRow.insertCell();
    myCell.innerHTML = this.HeadTitle[i];
    myCell.className = this.headStyle;
    myCell.width = this.TDwidth[i];

    if ( this.TDAlign[i] != null)
	myCell.style.textAlign = this.TDAlign[i];
                
  }

  var ContentTable = document.createElement("TABLE");

  ContentTable.id  = this.controlName;

  var ColorChangeFlag = "";
  ContentTable.className = this.tableStyle;
  ContentTable.border = 1;
  ContentTable.cellPadding = 2;
  for(var i=1; i < this.data.length; i++)
  {
    myRow     = ContentTable.insertRow();
    for(var j=1; j <= this.No_of_Cols; j++)
    {
      myCell    = myRow.insertCell();
      myCell.innerHTML = this.data[i][j];
	
      if ( this.TDAlign[j] != null)
	myCell.style.textAlign = this.TDAlign[j];
                
      if ( ColorChangeFlag == "" )
      {
	myCell.className = this.dataStyleLight;
      }
      else
      {
	myCell.className = this.dataStyleDark;
      }
      
      if ( i == 1)
   	myCell.width = this.TDwidth[j];
  		  
    }
		
    if ( ColorChangeFlag == "" )
    {
      ColorChangeFlag = "X";
    }
    else
    {
      ColorChangeFlag = "";
    }
	
    this.colorChangeFlag = ColorChangeFlag;

  }

  if ( document.getElementById(this.headDivID) == null ) 
  { 
    this.HeadDiv.insertBefore(headTable); 
    this.containerName.insertBefore(this.HeadDiv);
  }  

  if ( document.getElementById(this.DataDivID) == null ) 
  { 
    this.containerName.insertBefore(this.ContentDiv);
  }  
  
  var contentHTML = new String; var scrollFunc = new String();
  scrollFunc = this.controlName + '.adjust_title_scroll()';

  contentHTML = '<div class=' + this.DataClass;
  contentHTML = contentHTML + ' id=' + this.DataDivID;

  contentHTML = contentHTML + ' onscroll=\"' + scrollFunc + '\" >';
  
  contentHTML = contentHTML + ContentTable.outerHTML + '</DIV>';
        
  document.getElementById(this.DataDivID).outerHTML = contentHTML;

  document.getElementById(this.DataDivID).style.width  = this.Width;
  document.getElementById(this.DataDivID).style.height = this.Height;
  document.getElementById(this.DataDivID).className    = this.DataClass;

}

function adjust_title_scroll()
{
  this.HeadDiv.scrollLeft = event.srcElement.scrollLeft;
  
//  if(this.containerName.style.Height < event.srcElement.scrollHeight)
    this.HeadDiv.style.overflowY = 'scroll';
       
}


function Add_New_Row()
{

  var mytable = document.getElementById(this.controlName);
  var myRow = mytable.insertRow();

  var i = this.data.length - 1;

  var ColorChangeFlag = this.colorChangeFlag;
 
  for(var j=1; j <= this.No_of_Cols; j++)
  {
    myCell    = myRow.insertCell();
    myCell.innerHTML = this.data[i][j];

    if ( this.TDAlign[j] != null)
      	myCell.style.textAlign = this.TDAlign[j];
                
    if ( ColorChangeFlag == "" )
    {
	myCell.className = this.dataStyleLight;
    }
    else
    {
	myCell.className = this.dataStyleDark;
    }
    
    if ( i == 1)
   	myCell.width = this.TDwidth[j];

  }

		
  if ( ColorChangeFlag == "" )
  {
    ColorChangeFlag = "X";
  }
  else
  {
    ColorChangeFlag = "";
  }

  this.colorChangeFlag = ColorChangeFlag;

}
