/**
* JAVASCRIPTS - TELA DE MANUTENÇÃO DE REGISTROS BÁSICOS E GRIDS
**/

/**
* FUNÇÕES BÁSICAS DO FORMULÁRIO
* -----------------------------
* - NOVO REGISTRO
* - RETORNAR REGISTRO
* - EXCLUIR REGISTRO
* - MONTAR A GRID

* - PARAMETROS
* 	- NOME DO FORM EM FORMA DE CLASSE (HdChamado)
* 	- NOME DO IDENTIFICADOR DA VARIÁVEL GLOBAL
**/

function QForm(className, name)
{
	/**
	* PARÂMETROS SUPERIORES
	**/
	this.className = className;
	this.name = name;
	this.selectedRow;
	this.actualParameters;

	// ARRAYS DE PARAMETROS
	this.arrayColumns;
	this.arrayFields;
	this.arrayKeys;
	this.arrayGroup;
	this.arrayParameters;
	this.arrayTotalsGroup;
	this.arrayExtraParameters;
	this.arrayCheckBox;

	// STRINGS DE PARAMETROS
	this.nmSearch;
	this.nmSecondLine;
	this.dsIcon;
	this.dsIconFunction;
	this.dsIconTitle;
	this.dsGridFunction;
	this.dsDeleteFunction;
	this.dsGroupIcon;
	this.dsGroupIconFunction;
	this.dsGroupIconTitle;

	// FLAGS DA GRID
	this.idEdit;
	this.idDelete;
	this.idAdd;
	this.validateDelete;
	this.pageBreak;
	this.resultsPerPage;
	this.idRefresh;
	this.idPrint;
	this.idExport;
	this.isObject;
	this.idExpanded;
	
	this.gridTitle;
	//this.idDeletePermission;
	this.hasRowCount;

	// TRATA PARÂMETROS
	this.setParameters = function(strColumns, strFields, strKeys, nmSearch, nmSecondLine, dsIcon, dsIconFunction, dsGridFunction, strGroup, strTotalsGroup, strParameters, dsDeleteFunction, dsIconTitle, strExtraParameters, dsGroupIcon, dsGroupIconFunction, dsGroupIconTitle, strCheckBox){
		// COLUNAS, CAMPOS, CHAVES, BUSCA
		if(strColumns != ""){
			strColumns = codificarHTML(strColumns);
			this.arrayColumns = strColumns.split(codificarHTML(","));
		}

		if(strFields != ""){
			strFields = codificarHTML(strFields);
			this.arrayFields = strFields.split(codificarHTML(","));
		}

		if(strKeys != "")
			this.arrayKeys = strKeys.split(",");

		if(strGroup != "")
			this.arrayGroup = strGroup.split(",");

		if(strTotalsGroup != "")
			this.arrayTotalsGroup = strTotalsGroup.split(",");

		if(strParameters != "")
			this.arrayParameters = strParameters.split(",");

		if(nmSearch != "")
			this.nmSearch = nmSearch;

		if(strExtraParameters != "")
			this.arrayExtraParameters = codificarHTML(strExtraParameters).split(codificarHTML(","));

		if(strCheckBox != "") {
			strCheckBox = codificarHTML(strCheckBox);
			this.arrayCheckBox = strCheckBox.split(codificarHTML(","));
		}

		this.dsGridFunction = dsGridFunction;
		this.dsIcon = dsIcon;
		this.dsIconFunction = dsIconFunction;
		this.dsIconTitle = dsIconTitle;
		this.nmSecondLine = nmSecondLine;
		this.dsDeleteFunction = dsDeleteFunction;
		this.dsGroupIcon = dsGroupIcon;
		this.dsGroupIconFunction = dsGroupIconFunction;
		this.dsGroupIconTitle = dsGroupIconTitle;
	},

	// TRATA PARÂMETROS (FLAGS)
	this.setFlags = function(idEdit, idDelete, idAdd, validateDelete, pageBreak, resultsPerPage, idRefresh, idPrint, idExport, gridTitle, hasRowCount, isObject, idExpanded){
		// EDIÇÃO, DELEÇÃO, LIMITAÇÃO DE REGISTROS, PAGINAÇÃO
		this.idEdit = idEdit;
		this.idDelete = idDelete;
		this.idAdd = idAdd;
		this.validateDelete = validateDelete;
		this.pageBreak = pageBreak;
		this.resultsPerPage = resultsPerPage;
		this.idRefresh = idRefresh;
		this.idPrint = idPrint;
		this.idExport = idExport;
		this.isObject = isObject;
		this.idExpanded = idExpanded;
		
		this.gridTitle = gridTitle;
		//this.idDeletePermission = (idDeletePermission == "T" ? true : false);
		this.hasRowCount = hasRowCount;
	},

	/**
	* FUNÇÃO QUE RETORNA CAMPOS CHAVE E PARÂMETROS EM UMA STRING
	* ----------------------------------------------------------
	* PARAMETROS:
	* 1 - PARAMETROS DE ENTRADA
	* 2 - FLAG PARA INCLUSÃO DE PARÂMETROS DE BUSCA
	* 3 - FLAG PARA INCLUSÃO DE PARÂMETROS GRID
	**/
	this.makePars = function(idPars, idSearch, idGrid){
		// TRATAMENTOS DE CAMPOS
		if(!idSearch) idSearch = false;
		if(!idGrid) idGrid = false;
		if(!idPars) idPars = false;

		// PARÂMETROS INICIAIS
		var strPars = "NAME=" + this.name + "&CLASSNAME=" + this.className + "&ADD=" + this.idAdd;

		// TRATA ARRAY DE CHAVES
		if(typeof(this.arrayKeys) == "object")
			strPars += "&KEYS=" + this.arrayKeys.join(",");

		// PESQUISA
		if(idSearch)
		{	// VERIFICA SE O CAMPO DE PESQUISA EXISTE
			if(this.nmSearch)
				strPars += "&" + this.nmSearch + "=" + codificarHTML($F('dspesquisa'));
		}

		// GRID
		if(idGrid)
		{
			strPars += "&COLUMNS=" + this.arrayColumns.join(codificarHTML(","));
			strPars += "&FIELDS=" + this.arrayFields.join(codificarHTML(","));

			if(this.arrayGroup)
				strPars += "&GROUP=" + this.arrayGroup.join(",");

			if(this.arrayTotalsGroup)
				strPars += "&TOTALSGROUP=" + this.arrayTotalsGroup.join(",");

			if(this.arrayParameters)
				strPars += "&PARAMETERS=" + this.arrayParameters.join(",");

			if(this.arrayExtraParameters)
				strPars += "&EXTRAPARAMETERS=" + this.arrayExtraParameters.join(codificarHTML(","));

			if(this.arrayCheckBox)
				strPars += "&CHECKBOXES=" + this.arrayCheckBox.join(codificarHTML(","));

			strPars += "&EDIT=" + this.idEdit + "&DELETE=" + this.idDelete;
			strPars += "&VALIDATEDELETE=" + this.validateDelete;
			strPars += "&ICON=" + this.dsIcon + "&ICONFUNCTION=" + this.dsIconFunction + "&ICONTITLE=" + codificarHTML(this.dsIconTitle);
			strPars += "&SECONDLINE=" + this.nmSecondLine;
			strPars += "&PAGEBREAK=" + this.pageBreak;
			strPars += "&RESULTSPERPAGE=" + this.resultsPerPage;
			strPars += "&GRIDFUNCTION=" + this.dsGridFunction;
			strPars += "&DELETEFUNCTION=" + this.dsDeleteFunction;
			strPars += "&REFRESH=" + this.idRefresh;
			strPars += "&PRINT=" + this.idPrint;
			strPars += "&EXPORT=" + this.idExport;
			strPars += "&ISOBJECT=" + this.isObject;
			strPars += '&EXPANDED=' + this.idExpanded;
			strPars += "&TITLE=" + codificarHTML(this.gridTitle);
			strPars += "&ROWCOUNT=" + this.hasRowCount;
			strPars += "&GROUPICON=" + this.dsGroupIcon + "&GROUPICONFUNCTION=" + this.dsGroupIconFunction + "&GROUPICONTITLE=" + codificarHTML(this.dsGroupIconTitle);
		}

		// PARAMETROS DE LINHA
		if(idPars)
		{
			// VALIDA PARÂMETROS
			if(!this.actualParameters)
				var idForm = true;
			else{
				var idForm = false;
				var arrayPars = this.actualParameters.split(",");
			}

			for(x = 0; x < this.arrayKeys.length; x++)
			{
				strPars += "&" + this.arrayKeys[x] + "=";
				if(idForm)
				{
					if($(this.arrayKeys[x]))
						strPars += codificarHTML($F(this.arrayKeys[x]));
				}else
					strPars += codificarHTML(arrayPars[x]);
			}
		}

		return strPars;
	},

	// FUNÇÃO DE NOVO REGISTRO NA TELA
	this.newRecord = function(){
		resetForm(null, true);
		try{ $("btnExcluir").disabled = true; }catch(e){}
		Form.focusFirstElement(document.forms[0].name);
	},

	// FUNÇÃO QUE RETONA REGISTRO PARA A TELA getRecord('3') E AO COMPLETAR, LIMPA OS PARÂMETROS ATUAIS
	this.getRecord = function(actualParameters, selectedRow){
		// DEFINE LINHA SELECIONADA E ATUALIZA PARAMETROS
		this.selectedRow = selectedRow;
		this.actualParameters = actualParameters;

		var url = pathWeb + "/framework/formxml/basicform/getRecord.php";
		var pars = this.makePars(true);
		var myAjax = new Ajax.Request(url, {method: "get", parameters: pars, showDivQ: true, onComplete: this.actualParameters = null});
	},

	// FUNÇÃO DE CONFIRMAÇÃO DE DELETE
	this.confirmDelete = function(actualParameters, selectedRow){
		// VERIFICA A VALIDAÇÃO DE PERMISSÃO.
		if(typeof(hasPermission) != "undefined" && !hasPermission)
			return showQAlert(QLabel[30], {type: "exclamation"});

		// DEFINE LINHA SELECIONADA E ATUALIZA PARAMETROS
		if(selectedRow) this.selectedRow = selectedRow;
		this.actualParameters = actualParameters;
		showQAlert(QLabel[5], {type: "confirm", funYes: this.name + ".deleteRecord();"});
	},

	// FUNÇÃO DE EXLUIR REGISTRO. APÓS SUCESSO, LIMPA PARÂMETROS ATUAIS
	this.deleteRecord = function(){
		// DELETE REGISTRO
		var url = pathWeb + "/framework/formxml/basicform/getDelete.php";
		var pars = this.makePars(true, false, true) + "&SELECTEDROW=" + this.selectedRow;

		var myAjax = new Ajax.Request(url, {parameters: pars, onComplete: this.actualParameters = null});
	},

	// FUNÇÃO DE MONTAGEM DE GRID
	this.getGrid = function(idPesquisa){
		// VERIFICA SE EXISTE O CAMPO DE PESQUISA
		if(this.nmSearch)
		{
			if(idPesquisa)
			{
				if(isEmpty("dspesquisa"))
				{
					showQAlert(QLabel[28], {type : "exclamation"});
					return;
				}
			}else
				$('dspesquisa').value = "";
		}
		var url = pathWeb + "/framework/formxml/ajax/gridFormXML.php";

		if(this.idAdd == "T")
			var pars = this.makePars(true, idPesquisa, true);
		else
			var pars = this.makePars(false, idPesquisa, true);

		var myAjax = new Ajax.Updater({success: "div" + this.name}, url, {method: "post", parameters: pars, evalScripts: true, showDivQ: true});
	},

	this.processRecord = function(){
		// TRATAMENTO DE OBRIGATORIOS
		msg = {content:""};
		dynReqFields.VerifyFields(msg);

		if (msg.content == "") {
			// DESABILITA BOTÕES
			disableBtnForm();
			var url = pathWeb + "/framework/formxml/basicform/getProcessForm.php";
			var pars = this.makePars() + "&" + Form.serialize(document.forms[0].name);
			var myAjax = new Ajax.Request(url, {method: "post", parameters: pars});
		}else{
			msg.content = QLabel[4] + " :<br/><br/>" + msg.content;
			showQAlert(msg.content, {type: "exclamation"});
		}
	},

	// FUNÇÃO QUE LIMPA A GRID
	this.cleanGrid = function(){
		$("div" + this.name).innerHTML = "<center><font class=\"COLORLABEL\">" + QLabel[25] + "</font></center>";
	}
}