﻿/**
 * Classe global - Controle de dom, form, etc...	
 * @author	DJ Adriano Fernandes
 * @author	Eurodance.com.br
 * @version	1.0
 */

/**
 * Inicia objeto e métodos da classe global
 */
global = {
	
	/**
	 * Método para controlar eventuais erros JSON
	 * @param	array de objetos
	 * @return	retorna erro ou vazio
	 */
	controlerJson : function( request ) {
		
		this.request  = request;
		this.jsonData = eval( '(' + this.request + ')' );
		
		if( this.jsonData.erro != undefined && this.jsonData.erro != "" ) {
			return "erro";
		}
		if( this.jsonData.redirect != undefined && this.jsonData.redirect != "" ) {
			window.location = this.jsonData.redirect;
		}
		
		return "";
		
	},
	
	/**
	 * Método para controlar eventuais erros AJAX
	 * @param	array de objetos
	 * @return	retorna erro ou vazio	 
	 */		
	controler : function( request ) {
		
		this.request    = request;
		this.arrRequest = this.request.split( "::::" );
		
		if( this.request.indexOf( "Erro::::" ) != -1 ) {
			return "erro";
		}
		if( this.request.indexOf( "Redirect::::" ) != -1 ) {
			window.location = this.arrRequest[ 1 ];
		}
		
		return "";
		
	},
	
	/**
	 * Método para fazer upload de arquivos via AJAX
	 * @param	Nome do formulário
	 */	
	upload : function( formulario ) {
		
		this.formulario = formulario;
		
		global.Dom.$( this.formulario ).target = "iframe";		
		global.Dom.$( this.formulario ).action = "../controller/php/upload.exec.php";		
		
		// - faz upload das imagens
		global.Dom.$( this.formulario ).submit();	
		
		objAjax = new ajax();
		objAjax.carregandoAjax( "carregandoUpload", "<span style='color: #FF0000'>fazendo upload da(s) imagem(s)...</span>" );		
		
	},
	
	/**
	 * Método para fazer limpar formulário de cadastro para inserir novo registro
	 */		
	novo : function( area ) {
		
		this.area = area;
		
		global.Dom.$( 'formRegistro' ).reset();
		global.Dom.setFunction( "inserir", this.area + ".inserir();" );
		global.Dom.$( 'inserir' ).value = "inserir";
		global.Dom.setDisplay( "novo", "none" );		
		
	},
	
	/**
	 * Máscara de moeda
	 **/
	MascaraMoeda : function( objTextBox, SeparadorMilesimo, SeparadorDecimal, e ){
		var sep = 0;
		var key = '';
		var i = j = 0;
		var len = len2 = 0;
		var strCheck = '0123456789';
		var aux = aux2 = '';
		var whichCode = (window.Event) ? e.which : e.keyCode;
		if (whichCode == 13) return true;
		key = String.fromCharCode(whichCode); // Valor para o código da Chave
		if (strCheck.indexOf(key) == -1) return false; // Chave inválida
		len = objTextBox.value.length;
		for(i = 0; i < len; i++)
			if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal)) break;
		aux = '';
		for(; i < len; i++)
			if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) aux += objTextBox.value.charAt(i);
		aux += key;
		len = aux.length;
		if (len == 0) objTextBox.value = '';
		if (len == 1) objTextBox.value = '0'+ SeparadorDecimal + '0' + aux;
		if (len == 2) objTextBox.value = '0'+ SeparadorDecimal + aux;
		if (len > 2) {
			aux2 = '';
			for (j = 0, i = len - 3; i >= 0; i--) {
				if (j == 3) {
					aux2 += SeparadorMilesimo;
					j = 0;
				}
				aux2 += aux.charAt(i);
				j++;
			}
			objTextBox.value = '';
			len2 = aux2.length;
			for (i = len2 - 1; i >= 0; i--)
			objTextBox.value += aux2.charAt(i);
			objTextBox.value += SeparadorDecimal + aux.substr(len - 2, len);
		}
		return false;
	}	
	
}

/**
 * Classe de Doms
 */
global.Dom = {
	
	/**
	 * Método para facilitar a chamada de elementos dom por ID
	 * @param	id do elemento	
	 * @return	retorna o elemento se for true ou vazio se for false
	 */	
	$ : function( elemento ) {
		
		/* -- declara variáveis -- */		
		this.elemento = elemento;
		
		if( this.elemento != '' && typeof( this.elemento ) == 'string' ) {
	 		return document.getElementById( this.elemento );
		} else {
	 		return null;
		}
		
	},
	
	/**
	 * Método para verificar se um elemento existe
	 * @param	id do elemento	
	 * @return	retorna true se o elemento existe e false se não existir
	 */	
	booElemento : function( elemento ) {
		
		/* -- declara variáveis -- */		
		this.elemento = elemento;
		
		if( typeof( this.$( this.elemento ) ) == 'undefined' ) {
			return false;
		} else {
			return true;
		}
		
	},
	
	/**
	 * Método para setar um tipo de display a um elemento
	 * @param	id do elemento	
	 * @param	descrição do display (Ex: none ou inline)	 
	 */
	setDisplay : function( elemento, evento ) {
		
		/* -- declara variáveis -- */		
		this.elemento = elemento;
		this.evento   = evento;		
		
		if( this.booElemento( this.elemento ) ) {
			this.$( this.elemento ).style.display = this.evento;
		}
		
	},
	
	/**
	 * Método para setar um src a uma imagem
	 * @param	id do elemento	
	 * @param	descrição do display (Ex: none ou inline)	 
	 */
	setSrcImage : function( elemento, endereco ) {
		
		/* -- declara variáveis -- */		
		this.elemento = elemento;
		this.endereco = endereco;	
		
		if( this.booElemento( this.elemento ) ) {
			if( this.endereco != "" ) {
				this.$( this.elemento ).src = this.endereco;
			}
		}
		
	},		
	
	/**
	 * Método para controlar as ações do dataGrid
	 * @param	id do item da grid	 
	 */	
	 dataGrid : function( id ) {
		 
		/* -- declara variáveis -- */		
		this.id = parseInt( id );
		
		/**
		 * Verificações
		 */
		if( !isNaN( this.id ) ) {
			
			/**
			 * Muda os ícones do data grid
			 */
			if( this.$( 'icoMaisMenos' + this.id ).src.indexOf( 'icoMais.gif' ) != -1 ) {											
				
				this.setSrcImage( 'icoMaisMenos' + this.id, '../_img/icoMenos.gif' );	
				this.setDisplay( 'acoesGrid' + this.id, "block" );
				 
			} else {
				
			   this.setSrcImage( 'icoMaisMenos' + this.id, '../_img/icoMais.gif' );	
				this.setDisplay( 'acoesGrid' + this.id, "none" );			   
			   
			}	
		
		}
		 
	 },
	 
	/**
	 *	Método que seta uma função a um elemento
	 */
	setFunction : function( elemento, funcao ) {

		objFuncao     = funcao;
		this.elemento = elemento;

		if( typeof( objFuncao ) == 'string' ) {

			if( this.booElemento( this.elemento ) ) {

				this.$( this.elemento ).onclick = function() { eval( parent.objFuncao ); };

			}

		}

	}
	
} 

/**
 * Construtor da classe de formulários
 */
global.formulario = function( id, valor ) {
	this.id    = id;
	this.valor = valor;
}

/**
 * Cria a classe e os métodos
 */
global.formulario.prototype = {

	// --------------------------------------- +
	// - Seta um HTML ou Valor para o elemento
	// --------------------------------------- +
	setValue : function() {	

		if( global.Dom.booElemento( this.id ) ) {

			if( typeof( this.valor ) != 'undefined' ) {
				
				global.Dom.$( this.id ).value = this.valor;
					
			}

		}

	},

	// ------------------------------------ +
	// - Bloqueia elementos de um form
	// ------------------------------------ +
	bloquear : function() {

		if( global.Dom.booElemento( this.id ) ) {
			this.campos = global.Dom.$( this.id ).elements.length;
			for( this.x = 0; this.x < this.campos; this.x++ ) {
				if( !global.Dom.$( this.id ).elements[ this.x ].disabled ) {
					global.Dom.$( this.id ).elements[ this.x ].disabled = true;
				} else {
					global.Dom.$( this.id ).elements[ this.x ].disabled = false;
				}
			}
		}

	},
	
	// ------------------------------------ +
	// - Trava elementos de um form
	// ------------------------------------ +
	desabilitar : function() {

		if( global.Dom.booElemento( this.id ) ) {
			this.campos = global.Dom.$( this.id ).elements.length;
			for( this.x = 0; this.x < this.campos; this.x++ ) {
				if( !global.Dom.$( this.id ).elements[ this.x ].readonly ) {
					global.Dom.$( this.id ).elements[ this.x ].readonly = true;
				} else {
					global.Dom.$( this.id ).elements[ this.x ].readonly = false;
				}
				/**
				 * Bloquear ou desbloquear o botão submit
				 */
				if( global.Dom.$( this.id ).elements[ this.x ].type == 'button' ) {
					if( !global.Dom.$( this.id ).elements[ this.x ].disabled ) {
						global.Dom.$( this.id ).elements[ this.x ].disabled = true;
					} else {
						global.Dom.$( this.id ).elements[ this.x ].disabled = false;
					}					
				}
			}
		}

	},	

	// ---------------------- +
	// - Reseta um form
	// ---------------------- +
	resetar : function() {
		if( global.Dom.booElemento( this.id ) ) {
			global.Dom.$( this.id ).reset();
		}
	}

}