/************************************************************************
* 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: IBZDate.js,v 1.3 2009/01/19 16:18:56 GOYA\admingoya Exp $"
************************************************************************/

/*-----------------------------------------------------------------------
* DESCRIPCION:
*	 Construye un objeto de tipo Field 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 DateField( parentForm, xmlDocument, currentWidget )
{
	/********************************************************
			Declaracion de atributos del objeto				*
	*********************************************************/

	this.parentForm = parentForm;
	this.widget = 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.display = new String();
	this.style = new String();
	this.visible = new String();
	this.description = 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;
	//Metodos para correr eventos
	this.clientValidationPre = null;
	this.clientValidationPost = null;
	//Metodo define metodos asociados a los eventos
	this.DefineEvents = IBZDateField_DefineEvents;
	this.Validate = IBZDateField_Validate;
	//Metodos cambio de estado
	this.IsEmpty = IBZDateField_IsEmpty;
	this.GetValue = IBZDateField_GetValue;
	this.SetValue = IBZDateField_SetValue;
	this.SetMaxLength = IBZDateField_SetMaxLength;
	this.Enable = IBZDateField_Enable;
	this.Disable = IBZDateField_Disable;
	this.SetStyle = IBZDateField_SetStyle;
	this.Focus = IBZ_Focus;

	/********************************************************
	*		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;
	}

	if( this.widget == null )
	{
		//El campo no tiene un objeto correspondiente en la interfaz Html
		alert( "El campo de fecha " + 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();

		//Cambia las propiedades del objeto segun el DDI
		this.ChangeState();

		//Se asigna el valor default que tiene el campo
		this.SetValue( xmlDocument.text );

		this.SetMaxLength( this.length );

		//Se adiciona la seccion a las estructuras de datos de control de secciones
		parentForm.fieldArray[ parentForm.fieldArray.length ] = this;
		parentForm.fieldAssocArray[ this.fieldCode ] = this;
	}
}

/*-----------------------------------------------------------------------
* 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 IBZDateField_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 IBZDateField_DefineEvents()
{
	/********************************************************
	*		Inicializacion de eventos para el objeto		*
	*********************************************************/

	this.widget.onkeyup = DateFieldEdit;
	this.widget.onchange = DateFieldValidate;
}

/*-----------------------------------------------------------------------
* DESCRIPCION:
*	 Obtiene el valor del campo
* PARAMETROS:
* RESULTADO:
*	 Valor del campo en la interfaz
* PRE:
* POST:
* OJO: 
*-------------------------------------------------------------------- */
function IBZDateField_GetValue()
{
	return this.widget.value;
}

/*-----------------------------------------------------------------------
* DESCRIPCION:
*	 Asigna como valor del campo el valor recibido como parámetro
* PARAMETROS:
* RESULTADO:
*	 El objeto en la interfaz contiene el nuevo valor del campo
* PRE:
* POST:
* OJO: 
*-------------------------------------------------------------------- */
function IBZDateField_SetValue( value )
{
	if( isBlank( value ) == false )
	{
		this.widget.value = value;
	}
}

/*-----------------------------------------------------------------------
* DESCRIPCION:
*	 Asigna como valor del campo el valor recibido como parámetro
* PARAMETROS:
* RESULTADO:
*	 El objeto en la interfaz contiene el nuevo valor del campo
* PRE:
* POST:
* OJO: 
*-------------------------------------------------------------------- */
function IBZDateField_SetMaxLength( length )
{
	if( isBlank( length ) == false )
	{
		this.widget.maxlength = length;
	}
}

/*-----------------------------------------------------------------------
* 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 IBZDateField_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 IBZDateField_Disable()
{
	this.widget.readOnly = true;
}

/*-----------------------------------------------------------------------
* 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 IBZDateField_SetStyle()
{
	this.widget.className = this.style;
}

/*-----------------------------------------------------------------------
* 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 IBZDateField_IsEmpty()
{
	return( isBlank( this.widget.value ) );
}

/*-----------------------------------------------------------------------
* DESCRIPCION:
*	 Se encarga de selecionar el campo respectivo
* PARAMETROS:
* RESULTADO:
*	 Se dispararon los procesos de validacion del campoo
* PRE:
* POST:
* OJO: 
*-------------------------------------------------------------------- */
function IBZ_Focus()
{
	this.widget.focus();
}

/************************************************************************
* External Event Handlers
************************************************************************/

/*-----------------------------------------------------------------------
* DESCRIPCION:
*	 Edita el contenido del campo de acuerdo con el formato asociado
* PARAMETROS:
*	 none
* RESULTADO:
*	 El contenido del campo queda editado de acuerdo con el formato
* PRE:
* POST:
* OJO: 
*-------------------------------------------------------------------- */
function DateFieldEdit()
{
	var dateFieldObject;

	//Obtiene el objeto asociado al campo (Estructura de datos)
	dateFieldObject = window.fieldAssocArray[ this.id ];

	if( dateFieldObject.format != null && dateFieldObject.format != "" )
	{
		eval( dateFieldObject.format );
	}
}

/*-----------------------------------------------------------------------
* DESCRIPCION:
*	 Dispara el proceso de validacion del objeto DateField
* PARAMETROS:
*	 none
* RESULTADO:
*	 El campo se valido de acuerdo con el proceso estandar de validacion
* PRE:
* POST:
* OJO: 
*-------------------------------------------------------------------- */
function DateFieldValidate()
{
	//Obtiene el objeto asociado al campo (Estructura de datos)
	dateFieldObject = window.fieldAssocArray[ this.id ];

	//Se verifica si el contenido del campo no sea vacio 
	if( dateFieldObject.IsEmpty() == true )
	{
		return;
	}

	return dateFieldObject.Validate();
}
