/************************************************************************
* IBZList.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: IBZList.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 ListField( parentForm, xmlDocument, currentWidget )
{
	/********************************************************
			Declaracion de atributos del objeto				*
	*********************************************************/

	this.parentForm = parentForm;
	this.widget = null;
	this.widgetNew = null;
	this.widgetAdd = null;
	this.widgetRemove = null;
	this.XMLDocument = xmlDocument;
	this.fieldCode = new String();
	this.name = new String();
	this.security = new String();
	this.editable = new String();
	this.serverValidation = new String();
	this.required = new String();
	this.appearance = new String();
	this.style = new String();
	this.type = new String();
	this.title = new String();
	this.rows = new String();
	this.multiple = new String();
	this.visible = new String();
	this.description = new String();
	this.action = new String();

	/********************************************************
	*		Declaracion de metodos del objeto				*
	********************************************************/

	//Metodos cargue general de atributos y cambio de estado
	this.LoadProperties = IBZForm_LoadProperties;
	this.LoadItems = IBZlistField_LoadItems;
	this.ChangeState = IBZForm_ChangeStateAll;
	this.Update = IBZForm_Update;
	this.UpdateItems = IBZlistField_UpdateItems;
	//Metodos para correr eventos
	this.clientValidationPre = null;
	this.clientValidationPost = null;
	//Metodo define metodos asociados a los eventos
	this.DefineEvents = IBZListField_DefineEvents;
	this.Validate = IBZListField_Validate;
	this.AddDataListField = IBZListField_AddDataListField;
	//Metodos cambio de estado
	this.IsEmpty = IBZListField_IsEmpty;
	this.GetValue = IBZListField_GetValue;
	this.Enable = IBZListField_Enable;
	this.Disable = IBZListField_Disable;

	/********************************************************
	*		Inicializacion del objeto						*
	*********************************************************/

	//Cargue de propiedades
	this.LoadProperties();
	//Cargue de objeto HTML
	if( currentWidget == null )
	{
		this.widget = document.getElementById( this.fieldCode );
	}
	else
	{
		this.widget = currentWidget;
	}

	//Validar si es un campo de tipo add remove
	if( this.type == "addremove" )
	{
		this.widgetNew = document.getElementById( this.fieldCode + "New" );
		this.widgetAdd = document.getElementById( this.fieldCode + "Add" );
		this.widgetRemove = document.getElementById( this.fieldCode + "Remove" );
	}

	if( this.widget == null )
	{
		//El campo no tiene un objeto correspondiente en la interfaz Html
		alert( "El campo de lista " + this.fieldCode + " no tiene un objeto correspondiente en la interfaz Html" );
	}
	else
	{
		// Se definen los eventos sobre el widget asociado para ejecutar los procesos estandar 
		this.DefineEvents();

		//cargue de los items del formulario
		this.LoadItems();

		//Cambia las propiedades del objeto segun el DDI
		this.ChangeState();

		//Se adiciona la seccion a las estructuras de datos de control de secciones
		parentForm.listArray[ parentForm.listArray.length ] = this;
		parentForm.listAssocArray[ this.fieldCode ] = this;

		if( this.type == "addremove" )
		{
			parentForm.listAssocArray[ this.fieldCode + "New" ] = this;
			parentForm.listAssocArray[ this.fieldCode + "Add" ] = this;
			parentForm.listAssocArray[ this.fieldCode + "Remove" ] = this;
		}
	}
}

/*-----------------------------------------------------------------------
* DESCRIPCION:
*	 Con base en las propiedades asignadas del DOM se procede a ejecutar la accion
* PARAMETROS:
*	 None
* RESULTADO:
*	 Se ejecuto el proceso de construccion del formulario
* PRE:
* POST:
* OJO: 
*-------------------------------------------------------------------- */
function IBZlistField_LoadItems()
{
	var childCursor = new Number();
	var subNodeObject = null;

	if( this.XMLDocument.getAttribute("action") == "add" || this.XMLDocument.getAttribute("action") == "clear" )
	{
		//Ingresar el primer valor vacio
		if( this.XMLDocument.childNodes.length > 0 )
		{
			this.widget.options[ this.widget.length ] = new Option( "", "" );
		}

		//Iterar sobre los atributos
		//Asignarlos a array asociativo
		for( childCursor = 0; childCursor < this.XMLDocument.childNodes.length; childCursor ++ )
		{
			subNodeObject = this.XMLDocument.childNodes( childCursor );

			if( subNodeObject.nodeType == 1 )
			{
				if( this.type == "addremove" )
				{
					if( subNodeObject.getAttribute("selected") == "true" )
					{
						this.widgetNew.options[ this.widgetNew.length ] = new Option( subNodeObject.text, subNodeObject.getAttribute("return") );
					}
					else
					{
						this.widget.options[ this.widget.length ] = new Option( subNodeObject.text, subNodeObject.getAttribute("return") );
					}
				}
				else
				{
					this.widget.options[ this.widget.length ] = new Option( subNodeObject.text, subNodeObject.getAttribute("return") );
				}
			}
		}
	}
}

/*-----------------------------------------------------------------------
* DESCRIPCION:
*	 Con base en las propiedades asignadas del DOM se procede a ejecutar la accion
* PARAMETROS:
*	 None
* RESULTADO:
*	 Se ejecuto el proceso de construccion del formulario
* PRE:
* POST:
* OJO: 
*-------------------------------------------------------------------- */
function IBZlistField_UpdateItems()
{
	var childCursor = new Number();
	var subNodeObject = null;
	var selectCursor = new Number();
	var itemList = null;

	if( this.XMLDocument.getAttribute("action") == "add" )
	{
		//Iterar sobre los atributos
		//Asignarlos a array asociativo
		for( childCursor = 0; childCursor < this.XMLDocument.childNodes.length; childCursor ++ )
		{
			subNodeObject = this.XMLDocument.childNodes( childCursor );

			if( subNodeObject.nodeType == 1 )
			{
				this.widget.options[ this.widget.length ] = new Option( subNodeObject.text, subNodeObject.getAttribute("return") );
			}
		}
	}
	else if( this.XMLDocument.getAttribute("action") == "clear" )
	{
		if( this.widget.length != null )
		{
			this.widget.length = 0;
		}

		//Iterar sobre los atributos
		//Asignarlos a array asociativo
		for( childCursor = 0; childCursor < this.XMLDocument.childNodes.length; childCursor ++ )
		{
			subNodeObject = this.XMLDocument.childNodes( childCursor );

			if( subNodeObject.nodeType == 1 )
			{
				if( this.type == "addremove" )
				{
					if( subNodeObject.getAttribute("selected") == "true" )
					{
						this.widgetNew.options[ this.widgetNew.length ] = new Option( subNodeObject.text, subNodeObject.getAttribute("return") );
					}
					else
					{
						this.widget.options[ this.widget.length ] = new Option( subNodeObject.text, subNodeObject.getAttribute("return") );
					}
				}
				else
				{
					this.widget.options[ this.widget.length ] = new Option( subNodeObject.text, subNodeObject.getAttribute("return") );
				}
			}
		}
	}
	else if( this.XMLDocument.getAttribute("action") == "remove" )
	{
		if( this.widget.length != null )
		{

			//Iterar sobre los atributos
			//Asignarlos a array asociativo
			for( childCursor = 0; childCursor < this.XMLDocument.childNodes.length; childCursor ++ )
			{
				subNodeObject = this.XMLDocument.childNodes( childCursor );

				if( subNodeObject.nodeType == 1 )
				{
					for( selectCursor = 0; selectCursor < this.widget.length; selectCursor++ )
					{
						if( subNodeObject.getAttribute( "return" ) == this.widget.options[ selectCursor ].value )
						{
							this.widget.removeChild( this.widget.options[ selectCursor ] );
						}
					}
				}
			}
		}
	}
	else if( this.XMLDocument.getAttribute("action") == "select" )
	{
		if( this.widget.length != null )
		{

			//Iterar sobre los atributos
			//Asignarlos a array asociativo
			for( childCursor = 0; childCursor < this.XMLDocument.childNodes.length; childCursor ++ )
			{
				subNodeObject = this.XMLDocument.childNodes( childCursor );

				if( subNodeObject.nodeType == 1 )
				{
					if( this.widget.length != null )
					{
						for( selectCursor = 0; selectCursor < this.widget.length; selectCursor++ )
						{
							if( subNodeObject.getAttribute( "return" ) == this.widget.options[ selectCursor ].value )
							{
								this.widget.options[ selectCursor ].selected = true;
							}
						}
					}
					else if( this.widget.selectedIndex >= 0 )
					{
						if( subNodeObject.getAttribute( "return" ) == this.widget.options[ this.widget.selectedIndex ].value )
						{
							this.widget.options[ this.widget.selectedIndex ].selected = true;
						}
					}
				}
			}
		}
	}
}

/*-----------------------------------------------------------------------
* DESCRIPCION:
*	 Se encarga de disparar los procesos de validacion del campo definidos
*	 a nivel del documento DDF
* PARAMETROS:
* RESULTADO:
*	 Se dispararon los procesos de validacion del campoo
* PRE:
* POST:
* OJO: 
*-------------------------------------------------------------------- */
function IBZListField_IsEmpty()
{
	return( isBlank( this.widget.value ) );
}

/*-----------------------------------------------------------------------
* DESCRIPCION:
*	 Se encarga de disparar los procesos de validacion del campo definidos
*	 a nivel del documento DDF
* PARAMETROS:
* RESULTADO:
*	 Se dispararon los procesos de validacion del campoo
* PRE:
* POST:
* OJO: 
*-------------------------------------------------------------------- */
function IBZListField_GetValue( xmlNode )
{
	if( this.widget.multiple == true )
	{
		if( this.type != "addremove" )
		{
			for ( optionCursor = 0; optionCursor < this.widget.options.length; optionCursor++ ) {
				if( this.widget.options[ optionCursor ].selected == true )
				{
					appendListItem( xmlNode, true, this.widget.options[ optionCursor ].value, this.widget.options[ optionCursor ].innerHTML );
				}
			}
		}
	}
	else if( this.widget.selectedIndex >= 0 )
	{
		if( this.type != "addremove" )
		{
			appendListItem( xmlNode, true, this.widget.value, this.widget.options[ this.widget.selectedIndex ].innerHTML );
		}
	}
}

/*-----------------------------------------------------------------------
* DESCRIPCION:
*	 Desactiva la edición del campo, impidiendo que sea modificado por
*	 el usuario desde la interfaz
* PARAMETROS:
*	 None
* RESULTADO:
*	 None
* PRE:
* POST:
* OJO: 
*-------------------------------------------------------------------- */
function IBZListField_Enable()
{
	this.widget.readOnly = false;
}

/*-----------------------------------------------------------------------
* DESCRIPCION:
*	 Desactiva la edición del campo, impidiendo que sea modificado por
*	 el usuario desde la interfaz
* PARAMETROS:
*	 None
* RESULTADO:
*	 None
* PRE:
* POST:
* OJO: 
*-------------------------------------------------------------------- */
function IBZListField_Disable()
{
	this.widget.readOnly = true;
}

/*-----------------------------------------------------------------------
* DESCRIPCION:
*	 Se encarga de disparar los procesos de validacion del campo definidos
*	 a nivel del documento DDF
* PARAMETROS:
* RESULTADO:
*	 Se dispararon los procesos de validacion del campoo
* PRE:
* POST:
* OJO: 
*-------------------------------------------------------------------- */
function IBZListField_Validate()
{
	//Se verifica si el campo tiene un proceso de validacion asociado, para su ejecucion
	if( this.clientValidationPre != null || this.clientValidationPost != null || this.serverValidation == "yes" )
	{
		//Tiene funciones de validacion, se dispara el proceso desde el formulario
		this.parentForm.StartFieldValidation( this );
	}
}

/*-----------------------------------------------------------------------
* DESCRIPCION:
*	 Se encarga de verificar que todos los atributos esten correctamente
*	 diligenciados. 
* PARAMETROS:
* RESULTADO:
*	 Informa si el campo esta correctamente definido
* PRE:
* POST:
* OJO: 
*-------------------------------------------------------------------- */
function IBZListField_DefineEvents()
{
	/********************************************************
	*		Inicializacion de eventos para el objeto		*
	*********************************************************/

	if( this.type != "addremove" )
	{
		this.widget.onchange = listFieldValidate;
	}
	else
	{
		this.widgetAdd.onclick = listFieldAdd;
		this.widgetRemove.onclick = listFieldRemove;
	}
}

/*-----------------------------------------------------------------------
* DESCRIPCION:
*	 Dispara el proceso de validacion del objeto listField
* PARAMETROS:
*	 none
* RESULTADO:
*	 El campo se valido de acuerdo con el proceso estandar de validacion
* PRE:
* POST:
* OJO: 
*-------------------------------------------------------------------- */
function IBZListField_AddDataListField( type )
{
	var widget = null;
	var widgetNew = null;
	var xmlDocument;

	if( type == "add" )
	{
		widget = this.widget;
		widgetNew = this.widgetNew;
	}
	else if( type == "remove" )
	{
		widget = this.widgetNew;
		widgetNew = this.widget;
	}

	for( childCursor = 0; childCursor < widget.options.length; childCursor++ )
	{
		if( widget.options[ childCursor ].selected == true )
		{
			xmlDocument = this.XMLDocument.selectSingleNode( "IBZ_ListItem[ @return = '" + widget.value + "' ]" );

			if( xmlDocument != null )
			{
				if( type == "add" )
				{
					xmlDocument.setAttribute( "selected", "true" );
				}
				else if( type == "remove" )
				{
					xmlDocument.removeAttribute( "selected" );
				}

				widgetNew.appendChild( widget.options[ childCursor ] );
				childCursor--;
			}
		}
	}
}

/************************************************************************
* Event Handlers
************************************************************************/

/*-----------------------------------------------------------------------
* DESCRIPCION:
*	 Dispara el proceso de validacion del objeto listField
* PARAMETROS:
*	 none
* RESULTADO:
*	 El campo se valido de acuerdo con el proceso estandar de validacion
* PRE:
* POST:
* OJO: 
*-------------------------------------------------------------------- */
function listFieldValidate()
{
	//Obtiene el objeto asociado al campo (Estructura de datos)
	listFieldObject = window.listAssocArray[ this.id ];

	//Se verifica si el contenido del campo no sea vacio 
	if( listFieldObject.IsEmpty() == true )
	{
		return;
	}

	return listFieldObject.Validate();
}

/*-----------------------------------------------------------------------
* DESCRIPCION:
*	 Dispara el proceso de validacion del objeto listField
* PARAMETROS:
*	 none
* RESULTADO:
*	 El campo se valido de acuerdo con el proceso estandar de validacion
* PRE:
* POST:
* OJO: 
*-------------------------------------------------------------------- */
function listFieldAdd()
{
	var childCursor = Number();

	//Obtiene el objeto asociado al campo (Estructura de datos)
	listFieldObject = window.listAssocArray[ this.id ];

	listFieldObject.AddDataListField( "add" );
}

/*-----------------------------------------------------------------------
* DESCRIPCION:
*	 Dispara el proceso de validacion del objeto listField
* PARAMETROS:
*	 none
* RESULTADO:
*	 El campo se valido de acuerdo con el proceso estandar de validacion
* PRE:
* POST:
* OJO: 
*-------------------------------------------------------------------- */
function listFieldRemove()
{
	var childCursor = Number();

	//Obtiene el objeto asociado al campo (Estructura de datos)
	listFieldObject = window.listAssocArray[ this.id ];

	listFieldObject.AddDataListField( "remove" );
}


