/************************************************************************
* IBZfield.js											 FECHA: 12-09-2002 *
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
* PROPOSITO : Define los objetos encargados de soportar la operacion	*
*	 general de los formularios en el Framework.
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
* AUTOR: Mauricio Mendez M.											 *
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
* id = "$Id: IBZTable.js,v 1.3 2009/01/19 16:18:56 GOYA\admingoya Exp $"
************************************************************************/

/*-----------------------------------------------------------------------
* DESCRIPCION:
*	 Construye un objeto de tipo ListField en un formulario
* PARAMETROS:
*	 parentForm: Formulario al cual pertenece
*	 xmlDocument: Documento XML que define el campo
* RESULTADO:
*	 Construye un nuevo objeto
* PRE:
* POST:
* OJO: 
*-------------------------------------------------------------------- */
function Table( parentForm, xmlDocument )
{
	/********************************************************
			Variables de columnas y filas				*
	*********************************************************/
	this.columnArray = new Array();
	this.columnAssocArray = new Array();
	this.rowArray  = new Array();

	/********************************************************
			Declaracion de atributos del objeto				*
	*********************************************************/

	this.parentForm = parentForm;
	this.widget = null;
	this.tbody = null;
	this.XMLDocument = xmlDocument;
	this.tableId = new String();
	this.title = new String();
	this.security = new String();
	this.serverValidation = new String();
	this.orderColumns = new String();
	this.editable = new String();
	this.style = new String();
	this.visible = new String();
	this.description = new String();
	this.pageSize = new String();
	this.pageWindowSize = new String();

	/********************************************************
	*		Declaracion de metodos del objeto				*
	********************************************************/

	//Metodos cargue general de atributos y cambio de estado
	this.LoadProperties = IBZForm_LoadProperties;
	this.ChangeState = IBZForm_ChangeStateAll;
	this.Update = IBZForm_Update;
	//Manejo de campos y filas
	this.AddPrevious = IBZTable_AddPrevious;
	this.AddColumns = IBZTable_AddColumns;
	this.AddTBody = IBZTable_AddTBody;
	this.AddRows = IBZTable_AddRows;
	this.FillPageSize = IBZTable_FillPageSize;
	//Metodos cambio de estado
	this.Enable = IBZSection_Enable;
	this.Disable = IBZSection_Disable;
	this.Hide = IBZSection_Hide;
	this.Show = IBZSection_Show;
	this.SetStyle = IBZSection_SetStyle;

	/********************************************************
	*		Inicializacion del objeto						*
	*********************************************************/

	//Cargue de propiedades
	this.LoadProperties();
	//Cargue de objeto HTML
	this.widget = document.getElementById( this.tableId );

	if( this.widget == null )
	{
		// El campo no tiene un objeto correspondiente en la interfaz Html 
		alert( "La tabla " + this.tableId + " no tiene un objeto correspondiente en la interfaz Html" );
	}
	else
	{
		this.AddPrevious();

		this.AddColumns();
		this.AddTBody();
		this.AddRows();

		//Se adiciona la seccion a las estructuras de datos de control de secciones
		parentForm.tableArray[ parentForm.tableArray.length ] = this;
		parentForm.tableAssocArray[ this.tableId ] = this;
	}
}

/*-----------------------------------------------------------------------
* DESCRIPCION:
*	 Construye un objeto de tipo ListField en un formulario
* PARAMETROS:
*	 parentForm: Formulario al cual pertenece
*	 xmlDocument: Documento XML que define el campo
* RESULTADO:
*	 Construye un nuevo objeto
* PRE:
* POST:
* OJO: 
*-------------------------------------------------------------------- */
function IBZTable_AddColumns()
{
	var nodeObject = null;

	nodeObject = this.XMLDocument.firstChild;
	while( nodeObject != null )
	{
		if( nodeObject.nodeName == "IBZ_TableColumn" )
		{
			new Column( this, nodeObject );
		}

		//Iterar sobre el siguiente nodo
		nodeObject = nodeObject.nextSibling;
	}
}

/*-----------------------------------------------------------------------
* DESCRIPCION:
*	 Construye un objeto de tipo ListField en un formulario
* PARAMETROS:
*	 parentForm: Formulario al cual pertenece
*	 xmlDocument: Documento XML que define el campo
* RESULTADO:
*	 Construye un nuevo objeto
* PRE:
* POST:
* OJO: 
*-------------------------------------------------------------------- */
function IBZTable_AddTBody()
{
	this.tbody = this.widget.firstChild.nextSibling;

	if( this.tbody == null )
	{
		this.tbody = this.widget.firstChild;

		if( this.tbody == null )
		{
			// El campo no tiene un objeto correspondiente en la interfaz Html 
			alert( "La tabla " + this.tableId + " no tiene un cuerpo de tabla correspondiente en la interfaz Html" );
		}
		else if( this.tbody.nodeName.toUpperCase() != "TBODY" )
		{
			// El campo no tiene un objeto correspondiente en la interfaz Html 
			alert( "La tabla " + this.tableId + " no tiene un cuerpo de tabla correspondiente en la interfaz Html" );
			this.tbody = null;
		}
	}
}

/*-----------------------------------------------------------------------
* DESCRIPCION:
*	 Construye un objeto de tipo ListField en un formulario
* PARAMETROS:
*	 parentForm: Formulario al cual pertenece
*	 xmlDocument: Documento XML que define el campo
* RESULTADO:
*	 Construye un nuevo objeto
* PRE:
* POST:
* OJO: 
*-------------------------------------------------------------------- */
function IBZTable_AddRows()
{
	var childCursor = new Number();

	if( this.tbody != null )
	{
		for( childCursor = 0; childCursor < this.pageSize; childCursor ++ )
		{
			new Row( this );
		}
	}
}

/*-----------------------------------------------------------------------
* DESCRIPCION:
*	 Crea dinamicamente un objeto en un formulario
* PARAMETROS:
*	 parentForm: Formulario al cual pertenece
*	 xmlDocument: Documento XML que define el campo
* RESULTADO:
*	 Construye un nuevo objeto
* PRE:
* POST:
* OJO: 
*-------------------------------------------------------------------- */
function IBZTable_AddPrevious()
{
	var fieldObject = null;
	var nodeObject = null;

	nodeObject = this.parentForm.formXmlDOM.createElement( "IBZ_TextField" );
	nodeObject.setAttribute( "tableId", this.tableId );
	nodeObject.setAttribute( "fieldCode", "ActPageNumber" + this.tableId );
	nodeObject.setAttribute( "name", "Anterior" );
	nodeObject.setAttribute( "serverValidation", "yes" );
	nodeObject.setAttribute( "clientValidationPre", "textFieldPageSize" );
	nodeObject.setAttribute( "length", "3" );
	//nodeObject.setAttribute( "format", "parseToNumber(this, 10)" );

	fieldObject = new TextField( this.parentForm, nodeObject );
}

/*-----------------------------------------------------------------------
* DESCRIPCION:
*	 Crea dinamicamente un objeto en un formulario
* PARAMETROS:
*	 parentForm: Formulario al cual pertenece
*	 xmlDocument: Documento XML que define el campo
* RESULTADO:
*	 Construye un nuevo objeto
* PRE:
* POST:
* OJO: 
*-------------------------------------------------------------------- */
function IBZTable_FillPageSize()
{
	var rowCursor = 0;

	for( rowCursor = 0; rowCursor < this.pageSize; rowCursor ++ )
	{
		this.rowArray[ rowCursor ].Enable();
	}

	for( rowCursor = this.pageSize; rowCursor < this.rowArray.length; rowCursor ++ )
	{
		this.rowArray[ rowCursor ].Disable();
	}
}

/*-----------------------------------------------------------------------
* DESCRIPCION:
*	 Construye un objeto de tipo ListField en un formulario
* PARAMETROS:
*	 parentForm: Formulario al cual pertenece
*	 xmlDocument: Documento XML que define el campo
* RESULTADO:
*	 Construye un nuevo objeto
* PRE:
* POST:
* OJO: 
*-------------------------------------------------------------------- */
function Column( parentForm, xmlDocument )
{
	/********************************************************
			Declaracion de atributos del objeto				*
	*********************************************************/

	this.parentForm = parentForm;
	this.XMLDocument = xmlDocument;
	this.tableColumnId = new String();
	this.required = new String();
	this.security = new String();
	this.style = new String();
	this.title = new String();
	this.visible = new String();
	this.align = new String();
	this.wrap = new String();
	this.truncate = new String();
	this.maxlength = new String();

	/********************************************************
	*		Declaracion de metodos del objeto				*
	********************************************************/

	//Metodos cargue general de atributos y cambio de estado
	this.LoadProperties = IBZForm_LoadProperties;

	/********************************************************
	*		Inicializacion del objeto						*
	*********************************************************/

	//Cargue de propiedades
	this.LoadProperties();

	//Se adiciona la seccion a las estructuras de datos de control de secciones
	parentForm.columnArray[ parentForm.columnArray.length ] = this;
	parentForm.columnAssocArray[ this.tableColumnId ] = this;

}

/*-----------------------------------------------------------------------
* DESCRIPCION:
*	 Construye un objeto de tipo ListField en un formulario
* PARAMETROS:
*	 parentForm: Formulario al cual pertenece
*	 xmlDocument: Documento XML que define el campo
* RESULTADO:
*	 Construye un nuevo objeto
* PRE:
* POST:
* OJO: 
*-------------------------------------------------------------------- */
function Row( parentForm )
{
	/********************************************************
			Variables de campos 				*
	*********************************************************/
	this.objectArray = new Array();
	this.objectAssocArray = new Array();

	/********************************************************
			Variables				*
	*********************************************************/

	var nodeObject = null;

	/********************************************************
			Declaracion de atributos del objeto				*
	*********************************************************/

	this.parentForm = parentForm;
	this.tbody = this.parentForm.tbody;
	this.widget = null;
	this.XMLDocument = null;
	this.tableRowId = new String();
	this.isNewRow = new Boolean();
	this.disabled = new Boolean();
	this.rowOn = new Boolean();

	/********************************************************
	*		Declaracion de metodos del objeto				*
	********************************************************/

	//Metodos cargue general de atributos y cambio de estado
	this.SelectRow = IBZTable_SelectRow;
	this.AddXMLDOMandInterfaceFields = IBZTable_AddXMLDOMandInterfaceFields;
	this.CreateObject = IBZTable_CreateObject;
	this.Enable = IBZTable_Enable;
	this.Disable = IBZTable_Disable;

	/********************************************************
	*		Inicializacion del objeto						*
	*********************************************************/

	//Asignar la llave principal de la fila
	this.tableRowId = parentForm.rowArray.length + 1;
	this.isNewRow = false;
	this.disabled = false;

	//Aņadir la fila correspondiente HTML
	this.widget = this.tbody.insertRow( this.tbody.rows.length );
	this.SelectRow();

	this.rowOn = false;
	if( ( this.tbody.rows.length % 2 ) == 0 )
	{
		this.rowOn = true;
	}

	//Crear cada celda en la tabla HTML
	for( childCursor = 0; childCursor < this.parentForm.columnArray.length; childCursor++ )
	{
		//Obtener el objeto XML
		nodeObject = this.parentForm.columnArray[ childCursor ].XMLDocument;

		//Insertar las celdas
		tdObject = this.widget.insertCell( this.widget.cells.length );

		if( this.rowOn == true )
		{
			tdObject.className = 'clsTDOn';
		}

		//Crear el TableRow en el DOM
		this.AddXMLDOMandInterfaceFields( tdObject, nodeObject );
	}

	//Se adiciona la seccion a las estructuras de datos de control de secciones
	parentForm.rowArray[ parentForm.rowArray.length ] = this;
}

/*-----------------------------------------------------------------------
* DESCRIPCION:
*	 Crea dinamicamente un objeto en un formulario
* PARAMETROS:
*	 parentForm: Formulario al cual pertenece
*	 xmlDocument: Documento XML que define el campo
* RESULTADO:
*	 Construye un nuevo objeto
* PRE:
* POST:
* OJO: 
*-------------------------------------------------------------------- */
function IBZTable_SelectRow()
{
	var xPath = new String();

	//Busqueda de fila correspondiente
	xPath = "IBZ_TableRow[ @tableRowId = '" + this.tableRowId + "' ]";
	this.XMLDocument = this.parentForm.XMLDocument.selectSingleNode( xPath );

	//Inserta una fila en caso de no ser enviada por el servidor
	if( this.XMLDocument == null )
	{
		this.isNewRow = true;
		this.XMLDocument = this.parentForm.parentForm.formXmlDOM.createElement( "IBZ_TableRow" );
		this.XMLDocument.setAttribute( "tableRowId", this.tableRowId );
		this.parentForm.XMLDocument.appendChild( this.XMLDocument );
	}
}

/*-----------------------------------------------------------------------
* DESCRIPCION:
*	 Crea dinamicamente un objeto en un formulario
* PARAMETROS:
*	 parentForm: Formulario al cual pertenece
*	 xmlDocument: Documento XML que define el campo
* RESULTADO:
*	 Construye un nuevo objeto
* PRE:
* POST:
* OJO: 
*-------------------------------------------------------------------- */
function IBZTable_AddXMLDOMandInterfaceFields( tdObject, xmlDocument )
{
	/********************************************************
			Variables de filas				*
	*********************************************************/

	var nodeObject = null;
	var nodeName = new String();
	var xPath = new String();
	var currentObject = null;
	var atrCursor = new Number();
	var attributeObject = null;

	/********************************************************
	*		Recorrer Campos o Acciones de la columna		*
	*********************************************************/

	//Iniciar la iteracion sobre el nodo actual
	nodeObject = xmlDocument.firstChild;
	while( nodeObject != null )
	{
		//Validar solo nodos elemento
		if( nodeObject.nodeType == 1 )
		{
			nodeName = nodeObject.nodeName;

			//Indica si no es una fila enviada por el servidor
			if( this.isNewRow == false )
			{
				if( nodeName == "IBZ_Action" )
				{
					xPath = nodeName + "[ @actionId = '" + nodeObject.getAttribute( "actionId" ) + "' ]";
				}
				else
				{
					xPath = nodeName + "[ @fieldCode = '" + nodeObject.getAttribute( "fieldCode" ) + "' ]";
				}

				//Consultar si existe el campo o accion respectiva dentro de lo enviado por el servidor
				currentObject = this.XMLDocument.selectSingleNode( xPath );
			}

			//si no existe lo crea
			if( currentObject == null )
			{
				currentObject = this.parentForm.parentForm.formXmlDOM.createElement( nodeName );
			}

			//Iterar sobre los atributos
			for( atrCursor = 0; atrCursor < nodeObject.attributes.length; atrCursor ++ )
			{
				attributeObject = nodeObject.attributes( atrCursor );

				currentObject.setAttribute( attributeObject.name, attributeObject.value );
			}

			this.CreateObject( tdObject, nodeName, currentObject );

			//Adicion de cada campo
			this.XMLDocument.appendChild( currentObject );
		}

		//Iterar sobre el siguiente nodo
		nodeObject = nodeObject.nextSibling;
	}
}

/*-----------------------------------------------------------------------
* DESCRIPCION:
*	 Crea dinamicamente un objeto en un formulario
* PARAMETROS:
*	 parentForm: Formulario al cual pertenece
*	 xmlDocument: Documento XML que define el campo
* RESULTADO:
*	 Construye un nuevo objeto
* PRE:
* POST:
* OJO: 
*-------------------------------------------------------------------- */
function IBZTable_CreateObject( parentObject, nodeName, xmlDocument )
{
	var tagName = new String();
	var attributeName = new String();
	var className = new String();
	var AddObject = null;
	var newObject = null;

	if( nodeName == "IBZ_LabelField" )
	{
		tagName = "A";
		attributeName = "fieldCode";
		if( xmlDocument.getAttribute( "serverValidation" ) == "yes" )
		{
			className = "clsLabelFieldLink";
		}
		else
		{
			className = "clsLabelField";
		}
		AddObject = LabelField;
	}
	else if( nodeName == "IBZ_TextField" )
	{
		tagName = "INPUT";
		attributeName = "fieldCode";
		className = "clsTextField";
		AddObject = TextField;
	}
	else if( nodeName == "IBZ_ListField" )
	{
		tagName = "SELECT";
		attributeName = "fieldCode";
		className = "clsListField";
		AddObject = ListField;
	}
	else if( nodeName == "IBZ_Action" )
	{
		tagName = "INPUT";
		attributeName = "actionId";
		className = "clsAction";
		AddObject = Action;
	}

	//Obtener campo
	fieldCode = xmlDocument.getAttribute( attributeName );

	//Modificar el fieldCode para acomodarlo a cada fila
	xmlDocument.setAttribute( attributeName, fieldCode + "_" + this.tableRowId );

	//Crear dinamicamente el campo en la tabla
	newObject = document.createElement( tagName );
	newObject.setAttribute( "id", xmlDocument.getAttribute( attributeName ) );
	newObject.setAttribute( "name", xmlDocument.getAttribute( attributeName ) );
	newObject.setAttribute( "className", className );

	//asignacion de propiedades adicionales
	if( nodeName == "IBZ_TextField" && xmlDocument.getAttribute( "display" ) == "hidden" )
	{
		newObject.setAttribute( "type", "hidden" );
	}

	parentObject.appendChild( newObject );

	/*===================================================
		Crear en API y Modicar con base en TableRow
	===================================================*/

	//Anadir el campo text, label o listfield con el API
	newObject = new AddObject( this.parentForm.parentForm, xmlDocument, newObject );

	//Modificar el atributo al estado original
	newObject.fieldCode = fieldCode;
	xmlDocument.setAttribute( attributeName, fieldCode );

	this.objectArray[ this.objectArray.length ] = newObject;
	this.objectAssocArray[ newObject[attributeName] ] = newObject;
}

/*-----------------------------------------------------------------------
* DESCRIPCION:
*	 Crea dinamicamente un objeto en un formulario
* PARAMETROS:
*	 parentForm: Formulario al cual pertenece
*	 xmlDocument: Documento XML que define el campo
* RESULTADO:
*	 Construye un nuevo objeto
* PRE:
* POST:
* OJO: 
*-------------------------------------------------------------------- */
function IBZTable_Enable()
{
	this.disabled = false;
	this.widget.style.display = '';
}

/*-----------------------------------------------------------------------
* DESCRIPCION:
*	 Crea dinamicamente un objeto en un formulario
* PARAMETROS:
*	 parentForm: Formulario al cual pertenece
*	 xmlDocument: Documento XML que define el campo
* RESULTADO:
*	 Construye un nuevo objeto
* PRE:
* POST:
* OJO: 
*-------------------------------------------------------------------- */
function IBZTable_Disable()
{
	this.disabled = true;
	this.widget.style.display = 'none';
	//this.parentForm.XMLDocument.removeChild( this.XMLDocument );
}

