    // Célula
    function Celula(texto,id,classe,largura, tipo)
    {
        this.Texto= texto;
        this.Valor = texto;
        this.ID= id;
        this.Classe = classe;
        this.Largura= largura;
        // texto, botao, label, cabeçalho
        this.Tipo= tipo;
    }
    
    // Cabeçalho
    function Cabecalho(id, classe)
    {
        this.Celulas = new Array();
        this.addCelula = addCelula;
        this.show = ShowCabecalho;
        this.HTML = getCabecalhoHTML;
    }
    
    function getCabecalhoHTML()
    {
        var html="<tr>";
        for (i=0;i<this.Celulas.length;i++) 
        {
            html += "<th width='" + this.Celulas[i].Largura + "' class='" + this.Celulas[i].Classe + "'>" + this.Celulas[i].Valor + "</th>";
        }
        html += "</tr>";
        return html;
    }
    
    function ShowCabecalho()
    {
        document.write(this.HTML());
    }
    
    // Linha
    function Linha(id, classe)
    {
        this.Classe = classe;
        this.ID = id;
        this.Index;
        this.Display = true;
        this.ClasseMarcado = "linha_grid_marcado";
        this.Celulas = new Array();
        this.addCelula = addCelula;
        this.show = showLinha;
        this.getHtml = getLinhaHtml;
        this.getMeioHTML = getMeioHTML;
        this.HTML = getFullHTML;
        this.InicioHTML = "<tr id='" + this.ID + "'>";
        this.FimHTML = "</tr>";
    }
    
    function getMeioHTML()
    {
        var html = "";
        var i=0;
		var tam = this.Celulas.length;
		
        for (i=0;i<tam;i++)
        {		    
            if(this.Celulas[i].Tipo == "botao")
			{
				html += "<td width='" + this.Celulas[i].Largura + "' class='" + this.Celulas[i].Classe + "'id='" + this.Celulas[i].ID + "'>" + this.Celulas[i].Texto + "</td>";
			}
			else
			{
				html += "<td width='" + this.Celulas[i].Largura + "' class='" + this.Celulas[i].Classe + "'>" + this.Celulas[i].Texto + "</td>";
			}
		}	
        return html;
    }
    
    function getFullHTML()
    {
       return this.InicioHTML + this.getMeioHTML() + this.FimHTML;
    }
    
    function addCelula(celula)
    {
        this.Celulas[this.Celulas.length] = celula;
    }
    
    function showLinha()
    {
        var i=0;
        if(this.Display)
        {
            document.write(this.HTML());
        }
    }
    
    function getLinhaHtml()
    {
        if(this.Display)
        {
            return this.HTML();
        }
        return "";
    }
    
    //GridEmpty
    function GridEmpty(id,classe,mensagem)
    {
        this.Classe = classe;
        this.ID = id;
        this.Mensagem = mensagem;
        this.HTML = getGridEmptyHTML;
    }
    
    function getGridEmptyHTML()
    {
        return "<div id=\"" + this.ID + "\" class=\"" + this.Classe + "\">"
            + "<b class=\"xtop\"> <b class=\"xb1\"></b> <b class=\"xb2\"></b>"
            + "<b class=\"xb3\"></b> <b class=\"xb4\"></b> </b>"
            + "<div class=\"box-content\">"
            + "<div class=\"box-content-int\">"
            + "<p class=\"centralizado\"><strong>" + this.Mensagem + "</strong></p>"
            + "</div></div>"
            + "<b class=\"xbottom\"> <b class=\"xb4\"></b> <b class=\"xb3\"></b>" 
            + "<b class=\"xb2\"></b> <b class=\"xb1\"></b> </b> </div>"
    }
    
    function Grid(nome,classe,cellspacing,cellpadding,border,cabecalho,gridEmpty)
    {
        this.IDContainer = 'div_grid_' + nome;
        this.ClasseContainer = 'div' + classe;
        this.GridEmpty = gridEmpty;
        this.Nome = nome;
        this.ServerControl = "";
        this.ReadOnly = true;
        this.LabelLinha = false;
        this.BtnExcluir = '';
        this.BtnEditar = '';
        this.Cellspacing = cellspacing;
        this.Cellpadding = cellpadding;
        this.Border = border;
        this.Classe = classe;
        this.ID = 'grid_' + nome;
        this.Cabecalho = cabecalho;
        this.Edicao = false;
        this.Exclusao = false;
        this.Linhas = new Array();
        this.addLinha = addLinha;
        this.updateLinha = updateLinha;
        this.isShow = false;
        this.show = showGrid;
        this.bind = bind;
        this.getLinhasAtivas = getLinhasAtivas;
        this.Form;
        this.Hidden = criaValorHidden;
        this.criaHidden = criaHidden;
		this.excluiHidden = excluiHidden;
        this.SelectedIndex;
        this.addForm = addForm;
        this.InicioHTML = getGridInicioHTML;
        this.ContentHTML = getGridContentHTML;
        this.FimHTML = getGridFimHTML;
        this.TableHTML = getGridTableHTML;
        this.TableFimHTML = getGridTableFimHTML;
        this.HTML = getGridHTML;			
    }
    
    function bind()
    {
        if(!this.ReadOnly)
        {
            if(this.BtnExcluir != '')
            {
                this.Exclusao = true;
            }
            if(this.BtnEditar != '')
            {
                this.Edicao = true;
            }
        }
    }
    
    function getGridTableHTML()
    {
        var html = "<table cellspacing='" + this.Cellspacing + "' "
        html += "cellpadding='" + this.Cellpadding + "' border='" + this.Border + "' "    
        html += "id='" + this.ID + "' class='" + this.Classe + "'>";
        return html;    
    }
    
    function getGridInicioHTML()
    {
        var html = "<div id=\"" + this.IDContainer + "\" class=\"" +  this.ClasseContainer + "\">" 
        html += this.TableHTML();
        return html;
    }
    
    function getGridTableFimHTML()
    {
        return "</table>";
    }

    function getGridFimHTML()
    {
        return this.TableFimHTML() + "<div id=\"div_hidden\"></div>";
    }
    
    function getGridContentHTML()
    {
        var html = this.Cabecalho.HTML();
        var i=0;
        for(i=0;i<this.Linhas.length;i++)
        {
            if(this.Linhas[i].Display)
            {
                html += this.Linhas[i].HTML();
            }
        }
        return html;
    }
    
    function getGridHTML()
    {
        html = this.InicioHTML();
        html += this.ContentHTML();
        html += this.FimHTML();
        return html;
    }
    
    function showGrid()
    {
        this.isShow = true;
        document.write(this.GridEmpty.HTML());
        document.write(this.HTML());

        if(this.getLinhasAtivas() > 0)
        {
            document.getElementById(this.GridEmpty.ID).style.display = "none";
            document.getElementById(this.IDContainer).style.display = "block";
        }
        else
        {
            document.getElementById(this.GridEmpty.ID).style.display = "block";
            document.getElementById(this.IDContainer).style.display = "none";
        }
    }
    
    function getLinhasAtivas()
    {
        var count = 0;
        for(var i=0; i<this.Linhas.length; i++)
        {
            if(this.Linhas[i].Display)
            {
                count++;
            }    
        }
        return count;
    }
    
    function addLinha(linha,nomeIntanciaGrid)
    {
		if (linha.Celulas.length > 0)
		{	
			var valor = "";
	        var tam = this.Linhas.length;
	        linha.Index = tam;

	        if (this.Exclusao)
	        {
	            valor = "<a href='javascript:;' onclick='removeLinha(" + tam + "," + nomeIntanciaGrid + ")'>" + this.BtnExcluir + "</a>";
	            linha.addCelula(new Celula(valor,'','','',"botao"));
	        }

	        if (this.Edicao)
	        {
	            valor = "<a href='javascript:;' onclick='editaLinha(" + tam + "," + nomeIntanciaGrid + ")'>" + this.BtnEditar + "</a>";
	            linha.addCelula(new Celula(valor,'','','',"botao"));
	        }
			
	        this.Linhas[tam] = linha;
			
	        if(this.isShow)
	        {
	           var valor = this.Hidden(linha);
	           document.getElementById(this.IDContainer).innerHTML = this.TableHTML() + this.ContentHTML() + this.FimHTML();
	           this.criaHidden(valor);
	           document.getElementById(this.IDContainer).style.display = "block";
	           document.getElementById(this.GridEmpty.ID).style.display = "none";
	        }
		}
    }
    
    function removeLinha(index, grid)
    {
        var linha = grid.Linhas[index];
        linha.Index = index;
        var valor = grid.Hidden(linha);
        grid.excluiHidden(index);
        linha.Display = false;
        document.getElementById(linha.ID).style.display = 'none';
        
        if(grid.getLinhasAtivas() <= 0)
        {
           document.getElementById(grid.GridEmpty.ID).style.display = "block";
           document.getElementById(grid.IDContainer).style.display = "none";
        }
        else
        {
           document.getElementById(grid.GridEmpty.ID).style.display = "none";
           document.getElementById(grid.IDContainer).style.display = "block";
        }
    }
    
    function addForm(ids)
    {
        this.Form = new Array();
        this.Form = ids.split(';');
    }
    
    function editaLinha(index, grid)
    {
       grid.SelectedIndex = index;
       var linha = grid.Linhas[index];
       document.getElementById(linha.ID).className = "xpto";

       for(var i=0; i<grid.Linhas.length; i++)
       {
            if(grid.Linhas[i].Display)
            {
                linha.Classe = "";
                document.getElementById(grid.Linhas[i].ID).className = "";
            }
       }

       linha.Classe = linha.ClasseMarcado;
       elemLinha = document.getElementById(linha.ID);
       elemLinha.className = linha.ClasseMarcado;
       
       for(var i=0; i<grid.Form.length;i++)
       {
            var elem = document.getElementById(grid.Form[i]);
            if(elem.type == "text")
            {
                elem.value = linha.Celulas[i].Valor;
            }
            else if(elem.type == "select-one")
            {
                var options = elem.options;
                for(var j=0;j<options.length;j++)
                {
                    if(options[j].text == linha.Celulas[i].Texto)
                    {
                        elem.selectedIndex = j;
                        break;
                    }
                }    
            }
        } 
    }
    
    function updateLinha(linha)
    {
        var index = this.SelectedIndex;
        linha.Index = index;
        var valor = this.Hidden(linha);
        this.criaHidden(valor);
        var id = this.Linhas[index].ID;
        var elemHTML = document.getElementById(id);
        var celulas = linha.Celulas;
        var elemCels = elemHTML.getElementsByTagName("td");

        for(var i=0;i<celulas.length;i++)
        {
            elemCels[i].innerHTML = celulas[i].Texto;
            this.Linhas[index].Celulas[i].Valor = celulas[i].Valor;
            this.Linhas[index].Celulas[i].Texto = celulas[i].Texto;
        }
    }
    
    function criaHidden(valor)
    {
        var input = document.createElement("input");
        input.setAttribute("id", "hidden_" + this.Nome + this.Linhas.length);
        input.setAttribute("type", "hidden");
        input.setAttribute("classe", "grid_hidden");
        input.setAttribute("value", valor);
        input.setAttribute("name", "hidden_" + this.Nome + this.Linhas.length);
        var form = document.getElementById(this.Form[0]);        
        form.appendChild(input);		
    }	 
    
	function alteraHidden(index, valor)
	{
        var input = document.getElementById("hidden_" + this.Nome + index);
        input.setAttribute("value", valor);
        var form = document.getElementById(this.Form[0]);        
        form.appendChild(input);		
	}
	
	function excluiHidden(index)
	{
        var input = document.getElementById("hidden_" + this.Nome + (index+1));
		var valor = input.getAttribute("value");

		var novoValor = valor.substr(0,1) + "|E|" + valor.substr(4,valor.length-4);
		
		input.setAttribute("value", novoValor);
	}	

    function criaValorHidden(linha)
    {
        var valor = "";
        if(!this.ReadOnly)
        {
            valor += linha.Index + "|I";
            var celulas = linha.Celulas;
            for(var i=0; i<celulas.length;i++)
            {
                if(celulas[i].Tipo == 'texto' || celulas[i].Tipo == 'label')
                {
                    valor += "|";
                    valor += celulas[i].Valor;
                }
            }
        }
        return valor;
    }   
