// Copyright 1999 Macromedia, Inc. All rights reserved.

//---------------   GLOBAL VARIABLES   ---------------

var helpDoc = MM.HELP_objTable;
var gDialogShown = false;

//---------------     API FUNCTIONS    ---------------

function isDOMRequired() {
	return false;
}

function objectTag() {
  var Columns = document.theForm.Cols.value;
  var Rows = document.theForm.Rows.value;
  var Border = document.theForm.Border.value;
  var Width = document.theForm.Width.value;
  var cellSpacing = document.theForm.Cellspace.value;
  var cellPadding = document.theForm.Cellpad.value;
  var unitChoice = document.forms[0].Units.selectedIndex;

  var tableCells='<'+'TD>' + dw.getDocumentDOM("document").getNBSPChar() + '<'+'/TD>';
  var tableRow='',tableContent='';
  var openTag= '<' + 'table ', spaceIndex;
  var widthAttr;

  //change any negative or non-numeric row or column value into 1
  Columns = (parseInt(Columns)== Columns && Columns>0)?Columns:1;
  Rows = (parseInt(Rows)== Rows && Rows>0)?Rows:1;

  //CREATE TABLE
  //determine contents of 1 table row
  for (i=0; i< Columns; i++)
    tableRow+=tableCells;
  tableRow = "<"+"TR>" + tableRow + "<"+"/TR>";

  //determine number of table rows & concatanate rows together
  for (i=0; i< Rows; i++)
    tableContent += tableRow;

  //add percent or pixel values to opening tag, if applicable
  if (Width)
    openTag += 'width="'+Width + ((unitChoice == 0)? '%" ' : '" ');
  //add border value, if applicable
  if (Border)
    openTag+='border="' + Border + '" ';
  //add cellspacing value, if applicable
  if (cellSpacing)
    openTag+='cellspacing="' + cellSpacing + '" ';
  //add cellpadding value, if applicable
  if (cellPadding)
    openTag+='cellpadding="' + cellPadding + '" ';

  //strip extra space from openTag, if it exists
  spaceIndex = openTag.length-1
  if (escape(openTag.charAt(spaceIndex))=='%20')
    openTag = openTag.substring(0,spaceIndex);

  openTag += '>' + tableContent;

  if (gDialogShown) {
    saveExtension(document)
  }
  gDialogShown = false; // Reset show dialog global.
  
  openTag += '<'+'/table'+'>';
  return openTag;
}

//---------------    LOCAL FUNCTIONS   ---------------

function saveExtension(curDOM) {
  var curHTML = DWfile.read(curDOM.URL);
  var tempFilename = dw.getConfigurationPath() + '/Shared/MM/Cache/empty.htm';
  if (DWfile.exists(tempFilename)) {
    var tempDOM = dw.getDocumentDOM(tempFilename);
    tempDOM.documentElement.outerHTML = curHTML;
	var atrStr = DWfile.getAttributes(curDOM.URL);
    if (tempDOM.body.outerHTML != curDOM.body.outerHTML && (atrStr.indexOf('R') == -1)){
      tempDOM.body.outerHTML = curDOM.body.outerHTML;
      DWfile.write(curDOM.URL, tempDOM.documentElement.outerHTML);
	}
  }
}

function initializeUI()
{
  document.theForm.Rows.focus(); //set focus on textbox
  document.theForm.Rows.select(); //set insertion point into textbox
  gDialogShown = true;
}
