function ReplicationPanel( id )
{	
	this.Titulo			= document.getElementById( "lbl"	+ id );
	this.RecordCount	= document.getElementById( "lblRC"	+ id );
	this.AddButton		= document.getElementById( "lnkAdd"	+ id );
	this.id				= id;
	
	this.onAdd				= null;
	this.onAddCompleted		= null;
	this.onRemove			= null;
	this.onRemoveCompleted	= null;
	
	this._tbl				= document.getElementById("tbl" + id ); 
	this.rows				= this._tbl.rows;
	this.rowCount			= this._tbl.rows.length;
}

ReplicationPanel.prototype.Enabled = function( enabled )
{
	this.AddButton.style.display = ( enabled )?"block":"none";
	
}

ReplicationPanel.prototype.ReCount = function( registros )
{
	this.RecordCount.innerHTML = "Registro(s) Encontrado(s): " + registros;
	for( var i=1; i < this._tbl.rows.length; i++ )
	{
		var cell = this._tbl.rows[i].cells[0];
		if(cell)
			cell.innerHTML = i;
	}
}

ReplicationPanel.prototype.AddRecord = function()
{
	if( this.onAdd != null )
	{
		if( eval(this.onAdd) == false )
			return false;
	}

	var row = this._tbl.insertRow();
	for(var i=0; i < this._tbl.rows[0].cells.length; i++)
	{
		var cell = this.cloneObject( this._tbl.rows[0].cells[i] );
		row.appendChild(cell);
	}
	var trTmp = this._tbl.rows[ this._tbl.rows.length - 1 ];
		trTmp.style.display = 'block';

	if( this.onAddCompleted != null )
		eval( this.onAddCompleted );
	
	var btnTmp = FindControlInControl( this._tbl.rows[0].cells[2], "lnkDel" + this.id );
	if( btnTmp.style.filter == "" )
	{
		var btnDel = FindControlInControl( trTmp.cells[2], "lnkDel" + this.id );
			btnDel.attachEvent("onclick", this.RemoveRecord )
	}
	this.ReCount( this._tbl.rows.length - 1 );
	
	return false;
}

ReplicationPanel.prototype.RemoveRecord = function()
{
	var button	= event.srcElement;
	var TR		= button.parentElement.parentElement;
	var TBL		= TR.parentElement;
	
	if( this.onRemove != null )
	{
		if( eval(this.onRemove) == false)
			return false;
	}

	if( confirm("Confirma a exclusao da Linha [" + TR.rowIndex + "] ?" )  )
	{
		TBL.deleteRow( TR.rowIndex );

		if( this.onRemoveCompleted != null )
			eval( this.onRemoveCompleted );
	}

	var id	= button.id.toString().replace('lnkDel', '');
	var aux = new ReplicationPanel( id );
	
	if( TBL.rows && TBL.rows.length )
		aux.ReCount( TBL.rows.length - 1 );
	else
		aux.ReCount( 0 );
}

ReplicationPanel.prototype.cloneObject = function(obj)
{
	var newObj = document.createElement(obj.tagName);

	//Copia as Propriedades do Objeto
	for(var i=0; i < obj.attributes.length; i++)
	{
		if(obj.attributes.item(i).specified)
		{
			if( obj.attributes[i].nodeName.substr(0,2) != "on" )
			{
				if( obj.attributes(i).nodeName == "style" )
				{
					newObj.style.cssText = obj.style.cssText;
				}
				else
				{
					var attrObj		= obj.attributes.getNamedItem( obj.attributes(i).nodeName );
					var attrNewObj	= document.createAttribute( obj.attributes.item(i).nodeName );
						attrNewObj.value = attrObj.value;
					newObj.attributes.setNamedItem( attrNewObj );
				}
			}
		}
	}

	//Copiar Valores
	switch(newObj.tagName)
	{
		case "INPUT":
			if( newObj.type == "text" )
				newObj.value = "";
			else
				newObj.value = obj.value;
			break;
			
		case "TEXTAREA":
			newObj.value = "";
			break;
			
		case "SELECT":
			this.copyCombo(obj, newObj);
			break;
			
		case "P":
			newObj.innerHTML = obj.innerHTML
			break;
			
		case "SPAN":
			newObj.innerHTML = obj.innerHTML
			break;
	}
	
	if( obj.children.length > 0 && obj.tagName != "SELECT")
	{
		for(var i=0; i < obj.children.length; i++)
		{
			var x = this.cloneObject( obj.children[i] );
			newObj.appendChild( x );
		}
	}
	
	return newObj;
}

ReplicationPanel.prototype.copyCombo = function(comboOrigem, comboDestino)
{
	if(comboDestino.options.length > 0)
		this.clearCombo(comboDestino);
		
	for(var i=0; i < comboOrigem.options.length; i++ )
	{
	    comboDestino.options[comboDestino.length] = new Option(comboOrigem.options[i].text, comboOrigem.options[i].value);
	    comboDestino.options[i].selected = comboOrigem.options[i].selected;
	}
	
}

ReplicationPanel.prototype.clearCombo = function(pCombo, pTodos) 
{
	for(vw_for=pCombo.length ; vw_for >= 0 ; vw_for-- )
	    pCombo.options[vw_for] = null ;
}
