//%name: table.js % %date_modified: Fri Mar 09 17:21:56 2001 % %version: 7 % 
var noTABLEENTRYSELECTED = 0;
var tableINDEXUNITIALIZED = -1;

function TableField(parent, id, onRowChoice, tableRowIndex, tableStr)
{
//	pass in index and table string so that it's initialized
//  this is so that if the table changes, we won't use the browser's cache

	this.parent       = parent;
	this.id           = id;
	this.name         = fStr + id + fStr;
	this.type         = typeTable;
	this.onMenuChange = onRowChoice; // menu/row choice active links

	// Rows are indexed from 1 to n
	this.indexValue = tableRowIndex;
	this.originalIndexValue = tableINDEXUNITIALIZED;

	this.value			= tableStr;

	// not saving the value as original value. The attachment shouldn't have to be sent to the server
	// for dosubmit functions, pointer to FxxFContent

	this.firstcall = true;

	return this;
}
function setTableValue(val)
{
	this.ctrl.load(val);
        // Need the nestref for DynLayer so that we don't have 57
        // level deep recursion.  nestref is the full document path
        // for this table field.  Fixes bug 76200
	// reset the scroll bars?
        var nestref = 'formcontainer.document.' + getParentName(this) + this.name;
	this.ctrl.activate(this.firstcall, nestref);
	this.firstcall = false;

}
function setFocusToTable()
{
	// dummy function, no focus is set
	// setting focus to a table doesn't make sense
	// define the function so you won't get javascript runtime errors
}

function getTableIndexInternal(table)
{
	return this.indexValue;
}
function clearTableSelection(tblFieldName, clearIndex)
{
	// find the image and set it to the unselected
	// make the name and set the img to the unselected image

	var returnState = true;	// assume successful

	if (clearIndex == noTABLEENTRYSELECTED || clearIndex == tableINDEXUNITIALIZED)
		return returnState;

	var field = getFieldInstanceByName(tblFieldName);

	// make the img name
	var imgName;
	var imgIndex = clearIndex -1;
	if (ie)
	{
		// window.contentlyr.elm.all.tags.("IMG")[clearIndex-1]
		imgName = 'window.contentlyr.elm.all.tags("IMG")[' + imgIndex + ']';
	}
	else
	{
		// window.contentlyr.doc.image[clearIndex-1]
		imgName = 'window.contentlyr.doc.images[' + imgIndex + ']';
	}

	var img = eval('field.ctrl.' + imgName);

	if (img != null)
		img.src = "images/notselect.gif";
	else
		returnState = false;

	return returnState;

}
function setTableSelection(tblFieldName, newIndex)
{
	// make sure that the newIndex is valid
	// change the image
	
	var returnState = true;	// assume successful

	var field = getFieldInstanceByName(tblFieldName);

	// make the img name
	var imgName;
	var imgIndex = newIndex - 1;
	if (ie)
	{
		// window.contentlyr.elm.all.tags("IMG")[newIndex-1]
		imgName = 'window.contentlyr.elm.all.tags("IMG")[' + imgIndex + ']';
	}
	else
	{
		// window.contentlyr.doc.image[newIndex-1]
		imgName = 'window.contentlyr.doc.images[' + imgIndex + ']';
	}

	var img = eval('field.ctrl.' + imgName);
	if (img != null)
		img.src = "images/select.gif";
	else
		returnState = false;

	return returnState;
}
function setTableIndexInternal(newIndex, bChgOriginal)
{
	// clear any table selection
	clearTableSelection(this.name, this.indexValue);

	if (setTableSelection(this.name, newIndex))
	{
		this.indexValue = newIndex;
		// if this is the first call then set the original value
		if (this.originalIndexValue == tableINDEXUNITIALIZED)
			this.originalIndexValue = newIndex;
		else
		if (bChgOriginal)
			this.originalIndexValue = newIndex;
	}
	else
	{
		// error so set currentindex to none selected, leave orig value
		this.indexValue = noTABLEENTRYSELECTED;
	}
}
function handleOnRowChoice(fld, newIndex)
{
	// this is assumed to be the table field, not the href or img
	var field = getFieldInstanceByName(fld);

	// index value always good since it's an event
	field.setCtrlIndex(newIndex, false);

	// if there's an active link, execute it
	if (field.onMenuChange)
	{
		doSubmit(field.name, 'MenuChoice');
	}
}
function setTableIndex(fld, newIndex)
{
	// callback to set the index in the table field

	// this is assumed to be the table field, not the href or img
	var field = getFieldInstanceByName(fld);

	field.setCtrlIndex(newIndex, true);
}
function setTableFieldVisible(visibility)
{
	if (this.layer.visibility == visibility) return;
	this.layer.visibility = visibility;
   this.visibility = visibility;
}

function resetTableValues()
{
	// refresh table partially supported on unix
	// so support updating

	// reset any refreshed table values on a front/back
	// because table values are dynamically set, we need to reset their values
	if (parent.currentWin != null)
	{
		var fields = parent.currentWin.fieldInstances;
		for(var i = 0; i < fields.length; i++)
		{
			var fld = fields[i];
			if (fld.type == parent.typeTable)
			{
				// update the table and row selector
				if (fld.value != null && fld.value != "" )
				{
					fld.setCtrlValue(fld.value);
					fld.setCtrlIndex(fld.getCtrlIndex(), false);
				}
			}
		}
	}
}

function tableInit() {
   // initialize mouse events
   document.onmousedown = mouseDown;
   document.onmousemove = mouseMove;
   document.onmouseup = mouseUp;
   if (is.ns)
      document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);
	// reset table values that have been refreshed on back/forward button
	resetTableValues();
}

function mouseDown(e) {
   if (is.ns && e.target!=document)
      routeEvent(e);
   if (ScrollTestActive())
      return false;
	
   // other mouseDown code
   return true;
}

function mouseMove(e) {
   if (is.ns && e.target!=document)
      routeEvent(e);
   if (ScrollTestActive())
      return false;

   // other mouseMove code
   return true;
}

function mouseUp(e) {
   if (is.ns && e.target!=document)
      routeEvent(e);

   // other mouseUp code
   return true;
}
