/*
** RecordsExplorer class
*/

function RecordsExplorer() {
	// Identificaton in database
	this.Id = 0;
	// Identification in memory
	this.GUID = '';
	this.Embedded = false;
	
	this.dbId = 0;
	this.Title = '';
	this.Subtitle = '';
	this.IconUrl = 'images/recordsExplorer.gif';
	this.SelectMode = 'none'; // single | multi | none
	this.KeyField = '';
	this.ShowFilters = true;
	this.HideFilteredColumns = false;
	this.HideListFilteredColumns = true;
	this.ShowOnlyRegisteredColumns = false;
	this.AutoRefreshAfterListFilterChange = true;
	this.AutoRefreshAfterAction = true;
	this.UserRightToModify = '';
	
	this.QueryOnLoad = true;
	
	with (this.NewAction = new RecordsExplorerAction()) {
		Caption = "Nuevo"
		IconUrl = 'images/newAction.gif';
	};
	with (this.DefaultItemAction = new RecordsExplorerAction()) {
		Caption = "Modificar"
		IconUrl = 'images/defaultItemAction.gif';
	};
	with (this.DeleteAction = new RecordsExplorerAction()) {
		Caption = "Eliminar"
		IconUrl = 'images/deleteAction.gif';
	};
		
	this.SortField = '';
	this.SortOrder = '';
	
	this.SourceType = ''; // xml | sql | self | rsfile | provider
	this.SourceData = '';
	this.LocalFilter = false;
	this.LocalSort = false;
	this.NoFilter = false;
	
	this.RecordsPerPage = 0;
	this.CurrentPage = 1;
	
	// 20071102 ddignazi
	this.AllowXMLExport = false;
	this.AllowCopyToClipboard = true;
	
	this.Filters = new Array();
	this.SingleActions = new Array();
	this.MultiActions = new Array();
	this.Columns = new Array();
	
	function _AddFilter(oFilter) {
		return reAddFilter(this, oFilter);
	};
		
	function _AddSingleAction(oAction) {
		return reAddSingleAction(this, oAction);
	};
	
	function _AddMultiAction(oAction) {
		return reAddMultiAction(this, oAction);
	};
	
	function _AddColumn(oColumn) {
		return reAddColumn(this, oColumn);
	};
	
	this.AddFilter = _AddFilter;
	this.AddSingleAction = _AddSingleAction;
	this.AddMultiAction = _AddMultiAction;
	this.AddColumn = _AddColumn;
	
	function _Open(sURLParams) {
		reOpen(this, sURLParams);
	};
	
	this.Open = _Open;
};

function reCreateObject(sRecordsExplorer) {
	var oRecordsExplorer = JSON.parse(sRecordsExplorer);
	if (!oRecordsExplorer) {
		oRecordsExplorer = new RecordsExplorer();
		
		oRecordsExplorer.Title = 'Nueva consulta';
		oRecordsExplorer.SourceType = 'sql';
	};
	return oRecordsExplorer;
};

function reOpen(oRecordsExplorer, sURLParams, sTarget, sCallbackFunctionName) {
	var sURL = parRoot + "utils/recordsExplorer/recordsExplorer.asp";
	
	if (sURLParams && sURLParams != '') {
		if (sURLParams.substr(0, 1) == '?') sURLParams = sURLParams.substr(1, sURLParams.length - 1);
		if (sURLParams.substr(0, 1) == '&') sURLParams = sURLParams.substr(1, sURLParams.length - 1);
		
		sURL += '?' + sURLParams;
	};
	
	if (!sTarget) sTarget = '_blank';
	
	var oContainer = jsrsContextPool[ jsrsGetContextID() ];
	oContainer = oContainer.container;
	
	var oPage = (jsrsBrowser == 'IE' ) ? oContainer.document : oContainer.contentDocument;
	oPage.open();
	oPage.charset = "windows-1252";
	oPage.write("<html>");
	oPage.write("<body style='display: none;'>");
	oPage.write("<form name='RecordsExplorer' method='POST' ");
	oPage.write(" target='" + sTarget + "' ");
	oPage.write(" action='" + sURL + "'>");
	oPage.write("<input type='hidden' name='cmd' value='query'/>");
	if (sCallbackFunctionName) {
		oPage.write("<input type='hidden' name='reCallbackFunctionName' value='" + sCallbackFunctionName + "'/>");
	};
	oPage.write("<input type='hidden' name='reConfig' value='" +  escape(JSON.stringify(oRecordsExplorer)) + "'/>");
	oPage.write("</form>");
	oPage.write("</body>");
	oPage.write("</html>");
	oPage.close();

	// 20061221 ddignazi
	if (isContextVisible()) {
		oPage.cookie = 'eFrameworkVisible=1;path=/';
	};
	
	oPage.forms['RecordsExplorer'].submit();

	// 20070112 ddignazi: hides IFRAME in Firefox
	if (!document.all) {
		oContainer.style.display = 'none';
	};
};

function reEdit(oRecordsExplorer, iIdentifier, sCallbackFunctionName) {
	var sURL = parRoot + "utils/recordsExplorer/reEditor.asp";
	
	var oContainer = jsrsContextPool[ jsrsGetContextID() ];
	oContainer = oContainer.container;
	
	var oPage = (jsrsBrowser == 'IE' ) ? oContainer.document : oContainer.contentDocument;
	oPage.open();
	oPage.charset = "windows-1252";
	oPage.write("<html>");
	oPage.write("<body style='display: none;'>");
	oPage.write("<form id='RecordsExplorer' name='RecordsExplorer' method='POST' ");
	oPage.write(" target='_blank' ");
	oPage.write(" action='" + sURL + "'>");
	oPage.write("<input type='hidden' name='cmd' value='query'/>");
	oPage.write("<input type='hidden' name='codOpcion' value='" + iIdentifier + "'/>");
	oPage.write("<input type='hidden' name='anidado' value='1'/>");
	oPage.write("<input type='hidden' name='funcionRetornar' value='" + sCallbackFunctionName + "'/>");
	oPage.write("<input type='hidden' name='reEditor' value='" +  escape(JSON.stringify(oRecordsExplorer)) + "'/>");
	oPage.write("</form>");
	oPage.write("</body>");
	oPage.write("</html>");
	oPage.close();

	// 20061221 ddignazi
	if (isContextVisible()) {
		oPage.cookie = 'eFrameworkVisible=1;path=/';
	};
	
	oPage.forms['RecordsExplorer'].submit();
	
	// 20070112 ddignazi: hides IFRAME in Firefox
	if (!document.all) {
		oContainer.style.display = 'none';
	};
};

function reCallback(sCallbackFunctionName, sRecordsExplorer) {
	eval(sCallbackFunctionName + '(sRecordsExplorer)');
};


/*
** RecordsExplorerFilter class
*/

function RecordsExplorerFilter(sField, sCaption, sDataType, sOperator) {
	this.GUID = '';
	this.Field = (sField) ? sField : '';
	this.RelatedField = '';
	this.Caption = (sCaption) ? sCaption : '';
	this.DataType = (sDataType) ? sDataType : 'vbString';
	this.Operator = (sOperator) ? sOperator : 'equal';
	this.DefaultValue = null;
	this.DefaultValueRelativeFrom = 'today'; // today | startofmonth | endofmonth | startofweek | endofweek | firstdayofyear | lastdayofyear
	this.ValuesListType = '';
	this.ValuesListData = '';
	this.KeyField = 'KeyField';
	this.TextField = 'TextField';
	this.OnlyByArgument = false;
	this.OnlyReplaceTags = false;
	// 20071102 ddignazi
	this.NoSelectedItemText = '';
};

function reAddFilter(oRecordsExplorer, oFilter) {
	if (!oFilter) oFilter = new RecordsExplorerFilter();
	if (oRecordsExplorer) oRecordsExplorer.Filters.push(oFilter);
	return oFilter;
};

function reInsertFilter(oRecordsExplorer, iIndex, oFilter) {
	var vPrevFilters = new Array();
	
	if (!oFilter) oFilter = new RecordsExplorerFilter();

	if (iIndex > 0) {
		vPrevFilters = oRecordsExplorer.Filters.slice(0, iIndex);
	};
	
	vPrevFilters = vPrevFilters.concat(oFilter);
	
	oRecordsExplorer.Filters = vPrevFilters.concat(oRecordsExplorer.Filters.slice(iIndex));
	
	return oFilter;
};

function reRemoveFilter(oRecordsExplorer, iIndex) {
	var vPrevFilters = new Array();
	var vNextFilters = new Array();
	
	if (iIndex > 0) {
		vPrevFilters = oRecordsExplorer.Filters.slice(0, iIndex);
	};
	if (iIndex < oRecordsExplorer.Filters.length - 1) {
		vNextFilters = oRecordsExplorer.Filters.slice(iIndex + 1);
	};
	
	oRecordsExplorer.Filters = vPrevFilters.concat(vNextFilters);
	
	return true;
};

function reMoveUpFilter(oRecordsExplorer, iIndex) {
	var oFilter;
	
	oFilter = oRecordsExplorer.Filters[iIndex];
	
	reRemoveFilter(oRecordsExplorer, iIndex);
	iIndex--;
	reInsertFilter(oRecordsExplorer, iIndex);
	
	oRecordsExplorer.Filters[iIndex] = oFilter;
	
	return true;
};

function reMoveDownFilter(oRecordsExplorer, iIndex) {
	var oFilter;
	
	oFilter = oRecordsExplorer.Filters[iIndex];
	
	reRemoveFilter(oRecordsExplorer, iIndex);
	iIndex++;
	reInsertFilter(oRecordsExplorer, iIndex);
	
	oRecordsExplorer.Filters[iIndex] = oFilter;
	
	return true;
};




/*
** RecordsExplorerAction class
*/

function RecordsExplorerAction(sCaption, sURL, sTarget) {
	this.GUID = '';
	this.Caption = (sCaption) ? sCaption : '';
	this.Type = 'url'; // url | callback | re | run | di | ds
	this.Url = (sURL) ? sURL : '';
	this.UrlArguments = '';
	this.RunContext = 0;
	this.RunId = '';
	this.Target = (sTarget) ? sTarget : '_blank';
	this.IconUrl = '';
	this.Style = '';
	this.UserRightToLaunch = '';
};

function reAddSingleAction(oRecordsExplorer, oAction) {
	if (!oAction) oAction = new RecordsExplorerAction();
	if (oRecordsExplorer) oRecordsExplorer.SingleActions.push(oAction);
	return oAction;
};

function reInsertSingleAction(oRecordsExplorer, iIndex, oAction) {
	var vPrevActions = new Array();
	
	if (!oAction) oAction = new RecordsExplorerAction();
	
	if (iIndex > 0) {
		vPrevActions = oRecordsExplorer.SingleActions.slice(0, iIndex);
	};
	
	vPrevActions = vPrevActions.concat(oAction);
	
	oRecordsExplorer.SingleActions = vPrevActions.concat(oRecordsExplorer.SingleActions.slice(iIndex));
	
	return oAction;
};

function reRemoveSingleAction(oRecordsExplorer, iIndex) {
	var vPrevActions = new Array();
	var vNextActions = new Array();
	
	if (iIndex > 0) {
		vPrevActions = oRecordsExplorer.SingleActions.slice(0, iIndex);
	};
	if (iIndex < oRecordsExplorer.SingleActions.length - 1) {
		vNextActions = oRecordsExplorer.SingleActions.slice(iIndex + 1);
	};
	
	oRecordsExplorer.SingleActions = vPrevActions.concat(vNextActions);
	
	return true;
};

function reMoveUpSingleAction(oRecordsExplorer, iIndex) {
	var oAction;
	
	oAction = oRecordsExplorer.SingleActions[iIndex];
	
	reRemoveSingleAction(oRecordsExplorer, iIndex);
	iIndex--;
	reInsertSingleAction(oRecordsExplorer, iIndex);
	
	oRecordsExplorer.SingleActions[iIndex] = oAction;
	
	return true;
};

function reMoveDownSingleAction(oRecordsExplorer, iIndex) {
	var oAction;
	
	oAction = oRecordsExplorer.SingleActions[iIndex];
	
	reRemoveSingleAction(oRecordsExplorer, iIndex);
	iIndex++;
	reInsertSingleAction(oRecordsExplorer, iIndex);
	
	oRecordsExplorer.SingleActions[iIndex] = oAction;
	
	return true;
};

function reAddMultiAction(oRecordsExplorer, oAction) {
	if (!oAction) oAction = new RecordsExplorerAction();
	if (oRecordsExplorer) oRecordsExplorer.MultiActions.push(oAction);
	return oAction;
};

function reInsertMultiAction(oRecordsExplorer, iIndex, oAction) {
	var vPrevActions = new Array();
	
	if (!oAction) oAction = new RecordsExplorerAction();
	
	if (iIndex > 0) {
		vPrevActions = oRecordsExplorer.MultiActions.slice(0, iIndex);
	};
	
	vPrevActions = vPrevActions.concat(oAction);
	
	oRecordsExplorer.MultiActions = vPrevActions.concat(oRecordsExplorer.MultiActions.slice(iIndex));
	
	return oAction;
};

function reRemoveMultiAction(oRecordsExplorer, iIndex) {
	var vPrevActions = new Array();
	var vNextActions = new Array();
	
	if (iIndex > 0) {
		vPrevActions = oRecordsExplorer.MultiActions.slice(0, iIndex);
	};
	if (iIndex < oRecordsExplorer.MultiActions.length - 1) {
		vNextActions = oRecordsExplorer.MultiActions.slice(iIndex + 1);
	};
	
	oRecordsExplorer.MultiActions = vPrevActions.concat(vNextActions);
	
	return true;
};

function reMoveUpMultiAction(oRecordsExplorer, iIndex) {
	var oAction;
	
	oAction = oRecordsExplorer.MultiActions[iIndex];
	
	reRemoveMultiAction(oRecordsExplorer, iIndex);
	iIndex--;
	reInsertMultiAction(oRecordsExplorer, iIndex);
	
	oRecordsExplorer.MultiActions[iIndex] = oAction;
	
	return true;
};

function reMoveDownMultiAction(oRecordsExplorer, iIndex) {
	var oAction;
	
	oAction = oRecordsExplorer.MultiActions[iIndex];
	
	reRemoveMultiAction(oRecordsExplorer, iIndex);
	iIndex++;
	reInsertMultiAction(oRecordsExplorer, iIndex);
	
	oRecordsExplorer.MultiActions[iIndex] = oAction;
	
	return true;
};



/*
** RecordsExplorerColumn class
*/

function RecordsExplorerColumn(sField, sCaption) {
	this.Field = (sField) ? sField : '';
	this.Caption = (sCaption) ? sCaption : '';
	this.DataType = 'vbVariant';
	this.Visible = true;
	this.SendAsUrlArgument = false;
	this.AlternativeArgumentName = '';
	this.NullValue = null;
	this.DecodeFromFilters = false;
	this.FormatMask = '';
	this.Style = '';
	this.Width = '';
	this.MultiLine = true;
};

function reAddColumn(oRecordsExplorer, oColumn) {
	if (!oColumn) oColumn = new RecordsExplorerColumn();
	if (oRecordsExplorer) oRecordsExplorer.Columns.push(oColumn);
	return oColumn;
};

function reInsertColumn(oRecordsExplorer, iIndex, oColumn) {
	var vPrevColumns = new Array();
	
	if (!oColumn) oColumn = new RecordsExplorerColumn();

	if (iIndex > 0) {
		vPrevColumns = oRecordsExplorer.Columns.slice(0, iIndex);
	};
	
	vPrevColumns = vPrevColumns.concat(oColumn);
	
	oRecordsExplorer.Columns = vPrevColumns.concat(oRecordsExplorer.Columns.slice(iIndex));
	
	return oColumn;
};

function reRemoveColumn(oRecordsExplorer, iIndex) {
	var vPrevColumns = new Array();
	var vNextColumns = new Array();
	
	if (iIndex > 0) {
		vPrevColumns = oRecordsExplorer.Columns.slice(0, iIndex);
	};
	if (iIndex < oRecordsExplorer.Columns.length - 1) {
		vNextColumns = oRecordsExplorer.Columns.slice(iIndex + 1);
	};
	
	oRecordsExplorer.Columns = vPrevColumns.concat(vNextColumns);
	
	return true;
};

function reMoveUpColumn(oRecordsExplorer, iIndex) {
	var oColumn;
	
	oColumn = oRecordsExplorer.Columns[iIndex];
	
	reRemoveColumn(oRecordsExplorer, iIndex);
	iIndex--;
	reInsertColumn(oRecordsExplorer, iIndex);
	
	oRecordsExplorer.Columns[iIndex] = oColumn;
	
	return true;
};

function reMoveDownColumn(oRecordsExplorer, iIndex) {
	var oColumn;
	
	oColumn = oRecordsExplorer.Columns[iIndex];
	
	reRemoveColumn(oRecordsExplorer, iIndex);
	iIndex++;
	reInsertColumn(oRecordsExplorer, iIndex);
	
	oRecordsExplorer.Columns[iIndex] = oColumn;
	
	return true;
};












function GetArrayItem(oArray, iIndex) {
	return oArray[iIndex];
};

function GetIndexedArrayItem(oArray, sProperty, sValue) {
	for (i = 0; i < oArray.length; i++) {
			
		if (eval('oArray[i].' + sProperty) == sValue) {
			return oArray[i];
		};
			
	};
	return null;
};

function reGetObjectProperty(oObject, sProperty, vDefaultValue) {
	var vValue;
	vValue = oObject[sProperty];
	if (vValue == undefined) vValue = vDefaultValue;
	return vValue;
};

function reSetObjectProperty(oObject, sProperty, vValue) {
	try {
		oObject[sProperty] = vValue;
	} catch (e) {
		var oNewProperty = '{' + JSON.stringify(sProperty) + ':' + JSON.stringify(sProperty) + '}';
		oNewProperty = JSON.parse(oNewProperty);
		oObject = oObject.concat(oNewProperty);
	};
};

