function DataGrid()
{

  this.prop    = new Array();
  this.data    = new Array();
  this.element = new Array();
  this.i_table = new table_obj;

  this.containerDiv;
  this.controlName;
  this.Height;
  this.Width;
  this.Rows = 1;
  this.theme   = 1;
  this.toolbar = '';

  this.colName      = colName;
  this.colInputType = colInputType;
  this.colDatatype  = colDataType;
  this.colSize      = colSize;
  this.colMaxlength = colMaxlength;
  this.colMandatory = colMandatory;
  this.colScript    = colScript;
  this.colWidth     = colWidth;
  this.colDefault   = colDefault;

  this.GenInputTable     = GenInputTable;
  this.setData           = setData;
  this.addRow            = addRow;
  this.uploadExcel       = uploadExcel;
  this.downloadExcel     = downloadExcel;
}


function downloadExcel()
{

// Start Excel and get Application object.
  var oXL     = new ActiveXObject("Excel.Application");
      
// Get a new workbook.
  var oWB    = oXL.Workbooks.Add();
  var oSheet = oWB.ActiveSheet;

  var down_Table = new Array();
  down_Table = this.element;
  var tab_name     = this.controlName + '.i_table';
  var dataTable    = document.getElementById(tab_name).firstChild;
  //get the TBODY element 

  for (var i=0;i<dataTable.childNodes.length;i++)
  { 
    var currTr = dataTable.childNodes[i]; 
    if (currTr.nodeName == "TR") 
    { 
      for (var j=0;j<currTr.childNodes.length;j++)
      { 
        var currTd = currTr.childNodes[j]; 
        if (currTd.nodeName == "TD") 
        { 
//        down_Table[i+1][j+1] = currTd.childNodes(0).value;
          oSheet.Cells(i+1, j+1).Value = currTd.childNodes(0).value;
        }  
      } 
    } 
    window.status = 'Downloading Record ' + i;
  }

  oXL.Visible = true;

}


function uploadExcel()
{
  var filePath    = this.controlName + '_excel';
  var filePathObj = document.getElementById(filePath);
  if ( filePathObj == null )
    return;
  
  if ( filePathObj.value == '' )
  { alert('Please select file using Browse button'); return; }

  var uploadData = new Array();

  var myExit = false;
  var col    = 1; 
  var row    = 1;

  var oXL = new ActiveXObject("Excel.Application");
 
  var oWB = oXL.Workbooks.Open(filePathObj.value);
  var oSheet = oWB.ActiveSheet;
 
  while ( myExit != true )  //count no of data rows
  {
    if ( oSheet.Cells(row, 1).Value == "" || oSheet.Cells(row, 1).Value == null )
    { myExit = true; row = row - 1; }
    else
    { row = row + 1; }
  }
      
  var maxRow = row;
  var maxCol = this.prop.length;
 
  for ( var i = 1; i <= maxRow; i++ )
  {
    uploadData[i] = new Array();
    for ( var j = 1; j <= maxCol; j++ )
    {
      uploadData[i][j] = oSheet.Cells(i,j).Value;
      window.status = i;
    }
  }    

  oXL.Workbooks.Close();
  oXL.Quit();

  var htmltext;

  for (var ctr=1; ctr < uploadData.length; ctr++ )
  {
    var row                = this.element.length;
    this.element[row]      = new Array();
    this.i_table.data[row] = new Array();

    for(var col=1; col < this.prop.length; col++)
    {

//    alert(uploadData[ctr][col]);

      var InputName = new String; var InputID = new String;var ScriptText = new String;
      InputName = this.prop[col][2];
      InputName = InputName.replace(/#/gi,row);
      InputID   = this.controlName + '-' + row + '-' + col;
      ScriptText = InputValidation( this.prop[col][4], this.prop[col][8] );

      switch ( this.prop[col][3] )
      {
        case 'INPUT':
         htmltext = '<Input type=\"TEXT\" ';
         htmltext = htmltext + ' size=\"'  + this.prop[col][5]   + '\" maxlength=\"' + this.prop[col][6] + '\"';
         htmltext = htmltext + ' value=\"' + uploadData[ctr][col]  + '\" ';
         htmltext = htmltext + ' name=\"'  + InputName           + '\"';
         htmltext = htmltext + ' id=\"'    + InputID             + '\"';
         htmltext = htmltext + ScriptText  + ' >';
         break;
        case 'SELECT':
         htmltext = '<SELECT ';
         htmltext = htmltext + ' name=\"' + InputName + '\"';
         htmltext = htmltext + ' id=\"'   + InputID   + '\"';
         htmltext = htmltext + ScriptText + ' >';

         var selOptions = document.getElementById(this.prop[col][4]);
         for(var i=0; i < selOptions.options.length; i++)
         {
           htmltext = htmltext + '<option value=' + selOptions.options[i].value ;
           if ( selOptions.options[i].value == uploadData[ctr][col] )
             htmltext = htmltext + ' selected ';
           htmltext = htmltext + '>' + selOptions.options[i].text + '</option>';
         }
         htmltext = htmltext + '</SELECT>';
         break;
        case 'CHECKBOX':
         htmltext = '<Input type=\"CHECKBOX\" ';
         htmltext = htmltext + ' size=\"'  + this.prop[col][5]    + '\"';
         htmltext = htmltext + ' value=\"' + 'X' + '\" ';
         htmltext = htmltext + ' name=\"'  + InputName            + '\"';
         htmltext = htmltext + ' id=\"'    + InputID              + '\"';
         if ( uploadData[ctr][col] == 'X' || uploadData[ctr][col] == 'x' ) htmltext = htmltext + ' checked ';
         htmltext = htmltext + ScriptText  + ' >';
         break;
        case 'BUTTON':
         htmltext = '<Input type=\"BUTTON\" ';
         htmltext = htmltext + ' size=\"'  + this.prop[col][5]   + '\"';
         htmltext = htmltext + ' value=\"' + this.prop[col][10]  + '\" ';
         htmltext = htmltext + ' name=\"'  + InputName           + '\"';
         htmltext = htmltext + ' id=\"'    + InputID             + '\"';
         htmltext = htmltext + ScriptText  + ' >';
         break;
      }

      this.element[row][col] = htmltext;
      this.i_table.SetData(row,col, this.element[row][col]);

    } //col properties

    this.i_table.Add_New_Row();
    window.status = ctr + ' inserted';
  }  //end of ctr for

  var msg = 'No of Records uploaded : ' + maxRow + '\n( Each Row with ' + maxCol + ' Columns )';
  alert(msg);

}

function colName(fieldno, fieldname, title)
{
  this.prop[fieldno] = new Array();

  this.prop[fieldno][1] = title;      //display title
  this.prop[fieldno][2] = fieldname;  //input field name itab(?)-pernr
}

function colInputType(fieldno, inputtype)
{
  this.prop[fieldno][3] = inputtype;      //TEXT, SELECT, CHECKBOX, RADIO
}

function colDataType(fieldno, datatype)
{
  this.prop[fieldno][4] = datatype;      //CHAR,NUMC,DEC,DATE
}

function colSize(fieldno, size)
{
  this.prop[fieldno][5] = size;         //input size
}

function colMaxlength(fieldno, maxlength)
{
  this.prop[fieldno][6] = maxlength;
}

function colMandatory(fieldno, mandatory)
{
  this.prop[fieldno][7] = mandatory;
}

function colScript(fieldno, script)
{
  this.prop[fieldno][8] = script;
}


function colWidth(fieldno, width)
{
  this.prop[fieldno][9] = width;
}

function colDefault(fieldno, defaultVal)
{
  this.prop[fieldno][10] = defaultVal;
}

function setData(row, col, data)
{
  if( this.data[row] == null)
    this.data[row] = new Array();

  this.data[row][col] = data;
}



function NUMC()
{
  if ( event.keyCode < 48 || event.keyCode > 57 )
    event.returnValue = false;
}

function CHAR()
{
  if ( event.keyCode < 48 && event.keyCode != 32 )
    event.returnValue = false;

  if ( event.keyCode > 57 && event.keyCode < 65 )
    event.returnValue = false;

  if ( event.keyCode > 90 && event.keyCode < 97 )
    event.returnValue = false;

  if ( event.keyCode > 122 )
    event.returnValue = false;

}

function DEC()
{
  if ( event.keyCode < 45 || event.keyCode > 57 )
      event.returnValue = false;
  if ( event.keyCode == 47 )
      event.returnValue = false;

  if ( event.srcElement.value.indexOf(".") >= 0 && event.keyCode == 46 )
     event.returnValue = false;

  if ( event.keyCode == 45 )
  {
    if ( event.srcElement.value.length != 0 )
         event.returnValue = false;
    else if ( event.srcElement.value.indexOf("-") >= 0 )
            event.returnValue = false;

  }
}


function DAT()
{

  if ( event.keyCode < 47 || event.keyCode > 57 )
      event.returnValue = false;

  if ( event.srcElement.value.indexOf("/") != event.srcElement.value.lastIndexOf("/") )
  {
    if ( event.keyCode == 47 )
       event.returnValue = false;
  }
}

function MMDDYYYY()
{
  if ( event.srcElement.value == '' ) return false;
    var mydate = new Date(event.srcElement.value);

  if ( mydate != 'NaN')
  { 
    event.srcElement.value = (mydate.getMonth()+1) + '/' + mydate.getDate() + '/' + mydate.getUTCFullYear();
  }
  else
  {
    alert('Enter Date in MM/DD/YYYY format');
    event.srcElement.focus();
  }
}

function DDMMYYYY()
{
  if ( event.srcElement.value == '' ) return false;
   var mydate = new Date(event.srcElement.value);

  if ( mydate != 'NaN')
  { 
    event.srcElement.value =  mydate.getDate()  + '/' + (mydate.getMonth()+1) + '/' + mydate.getUTCFullYear();
  }
  else
  {
    alert('Enter Date in MM/DD/YYYY format');
    event.srcElement.focus();
  }
}

function InputValidation(mytype, myscript )
{

  var valText = '';

  if ( mytype == 'NUMC'      ||
       mytype == 'DEC'       ||
       mytype == 'DDMMYYYY'  ||
       mytype == 'MMDDYYYY'  
     )
  {
    valText = ' style=\"{text-align:right;}\" '
  }

  switch ( mytype )
  {
    case 'NUMC':
      valText = valText + ' onkeypress=\"NUMC()\" ';
      break;
    case 'DEC':
      valText = valText + ' onkeypress=\"DEC()\" ';
      break;
    case 'DDMMYYYY':
      valText = valText + ' onkeypress=\"DAT()\" onblur=\"DDMMYYYY()\" ';
      break;
    case 'MMDDYYYY':
      valText = valText + ' onkeypress=\"DAT()\" onblur=\"MMDDYYYY()\" ';
      break;
    case 'CHAR':
      valText = valText + ' onkeypress=\"CHAR()\" ';
      break;
  }

  valText = valText + ' ' + myscript;

  return valText;
}

function addRow()
{

    var htmltext;

    var row                = this.element.length;
    this.element[row]      = new Array();
    this.i_table.data[row] = new Array();

    for(var col=1; col < this.prop.length; col++)
    {

      var InputName = new String; var InputID = new String;var ScriptText = new String;
      InputName = this.prop[col][2];
      InputName = InputName.replace(/#/gi,row);
      InputID   = this.controlName + '-' + row + '-' + col;
      ScriptText = InputValidation( this.prop[col][4], this.prop[col][8] );

      switch ( this.prop[col][3] )
      {
        case 'INPUT':
         htmltext = '<Input type=\"TEXT\" ';
         htmltext = htmltext + ' size=\"'  + this.prop[col][5]   + '\" maxlength=\"' + this.prop[col][6] + '\"';
         htmltext = htmltext + ' value=\"' + this.prop[col][10]  + '\" ';
         htmltext = htmltext + ' name=\"'  + InputName           + '\"';
         htmltext = htmltext + ' id=\"'    + InputID             + '\"';
         htmltext = htmltext + ScriptText  + ' >';
         break;
        case 'SELECT':
         htmltext = '<SELECT ';
         htmltext = htmltext + ' name=\"' + InputName + '\"';
         htmltext = htmltext + ' id=\"'   + InputID   + '\"';
         htmltext = htmltext + ScriptText + ' >';

         var selOptions = document.getElementById(this.prop[col][4]);
         for(var i=0; i < selOptions.options.length; i++)
         {
           htmltext = htmltext + '<option value=' + selOptions.options[i].value ;
           if ( selOptions.options[i].value == this.prop[col][10] )
             htmltext = htmltext + ' selected ';
           htmltext = htmltext + '>' + selOptions.options[i].text + '</option>';
         }
         htmltext = htmltext + '</SELECT>';
         break;
        case 'CHECKBOX':
         htmltext = '<Input type=\"CHECKBOX\" ';
         htmltext = htmltext + ' size=\"'  + this.prop[col][5]   + '\"';
         htmltext = htmltext + ' value=\"' + 'X'  + '\" ';
         htmltext = htmltext + ' name=\"'  + InputName           + '\"';
         htmltext = htmltext + ' id=\"'    + InputID             + '\"';
         htmltext = htmltext + ScriptText  + ' >';
         break;
        case 'BUTTON':
         htmltext = '<Input type=\"BUTTON\" ';
         htmltext = htmltext + ' size=\"'  + this.prop[col][5]   + '\"';
         htmltext = htmltext + ' value=\"' + this.prop[col][10]  + '\" ';
         htmltext = htmltext + ' name=\"'  + InputName           + '\"';
         htmltext = htmltext + ' id=\"'    + InputID             + '\"';
         htmltext = htmltext + ScriptText  + ' >';
         break;
      }

      this.element[row][col] = htmltext;
      this.i_table.SetData(row,col, this.element[row][col]);

    } //col properties


    this.i_table.Add_New_Row();
}

function GenInputTable()
{
  var htmltext;

  for(var row=1; row < this.data.length; row++)
  {
    this.element[row] = new Array();

    for(var col=1; col < this.prop.length; col++)
    {

      var InputName = new String; var InputID = new String;var ScriptText = new String;
      InputName = this.prop[col][2];
      InputName = InputName.replace(/#/gi,row);
      InputID   = this.controlName + '-' + row + '-' + col;
      ScriptText = InputValidation( this.prop[col][4], this.prop[col][8] );

      switch ( this.prop[col][3] )
      {
        case 'INPUT':
         htmltext = '<Input type=\"TEXT\" ';
         htmltext = htmltext + ' size=\"'  + this.prop[col][5]   + '\" maxlength=\"' + this.prop[col][6] + '\"';
         htmltext = htmltext + ' value=\"' + this.data[row][col] + '\" ';
         htmltext = htmltext + ' name=\"'  + InputName           + '\"';
         htmltext = htmltext + ' id=\"'    + InputID             + '\"';
         htmltext = htmltext + ScriptText  + ' >';
         break;
        case 'SELECT':
         htmltext = '<SELECT ';
         htmltext = htmltext + ' name=\"' + InputName + '\"';
         htmltext = htmltext + ' id=\"'   + InputID   + '\"';
         htmltext = htmltext + ScriptText + ' >';

         var selOptions = document.getElementById(this.prop[col][4]);
         for(var i=0; i < selOptions.options.length; i++)
         {
           htmltext = htmltext + '<option value=' + selOptions.options[i].value ;
           if ( selOptions.options[i].value == this.data[row][col] )
             htmltext = htmltext + ' selected ';
           htmltext = htmltext + '>' + selOptions.options[i].text + '</option>';
         }
         htmltext = htmltext + '</SELECT>';
         break;
        case 'CHECKBOX':
         htmltext = '<Input type=\"CHECKBOX\" ';
         htmltext = htmltext + ' size=\"'  + this.prop[col][5]   + '\"';
         htmltext = htmltext + ' value=\"' + 'X' + '\" ';
         htmltext = htmltext + ' name=\"'  + InputName           + '\"';
         htmltext = htmltext + ' id=\"'    + InputID             + '\"';
         if ( this.data[row][col] == 'X' || this.data[row][col] == 'x' ) htmltext = htmltext + ' checked ';
         htmltext = htmltext + ScriptText  + ' >';
         break;
        case 'BUTTON':
         htmltext = '<Input type=\"BUTTON\" ';
         htmltext = htmltext + ' size=\"'  + this.prop[col][5]   + '\"';
         htmltext = htmltext + ' value=\"' + this.data[row][col] + '\" ';
         htmltext = htmltext + ' name=\"'  + InputName           + '\"';
         htmltext = htmltext + ' id=\"'    + InputID             + '\"';
         htmltext = htmltext + ScriptText  + ' >';
         break;
      }

      this.element[row][col] = htmltext;

    } //col properties

  } // rows data

  if ( this.toolbar != '')
  {
    var toolBar = document.createElement("DIV");

    var toolText = '<input type=button onclick=\"' + this.controlName + '.addRow();\" value=\"Add\">';
    //var toolText = toolText + '<input id=' + this.controlName + '_excel type=\"file\">';
    //var toolText = toolText + '<input type=button value=\"Upload\"   onclick=\"' +  this.controlName + '.uploadExcel();\">';
   // var toolText = toolText + '<input type=button value=\"Download\" onclick=\"' +  this.controlName + '.downloadExcel();\">';

    toolBar.innerHTML = toolText;

    this.containerDiv.insertBefore(toolBar);
  }

  var myjobj       = eval(this.controlName);
  var tab_name     = this.controlName + '.i_table';
  var headDIVid    = this.controlName + '_head';
  var contentDIVid = this.controlName + '_cont';
  var myTable      = eval(tab_name);
 
  myTable.SetControlName(tab_name);

  myTable.SetHeightWidth(this.Height,this.Width);

  myTable.SetHead_prop(headDIVid,'head_div');
  myTable.SetContent_prop(contentDIVid,'content_div');

  myTable.SetTheme(this.theme);

  myTable.SetCols(this.prop.length - 1);
  myTable.SetRows(this.element.length - 1);

  for(var col=1; col < this.prop.length; col++)
  {
    myTable.SetCellWidth(col, this.prop[col][9], 'center');
    myTable.SetHeadTitle(col, this.prop[col][1] );
  }

  for(var row=1; row < this.element.length; row++)
  {
    for(var col=1; col < this.prop.length; col++)
    {
      myTable.SetData(row,col, this.element[row][col]);
    }    
  } 

  myTable.GenTable(this.containerDiv);

  if ( this.element.length-1 < this.Rows )
  {
    var newRows = this.Rows - this.element.length + 1;
    if ( newRows > 0 )
    {
      for ( var i =1; i <= newRows; i++)
        this.addRow();
    }
  }

}
