////////////////////////////////////////////////////////////////////////////////
//                            Common DOM Functions
//
//  Version: 1.1
//  Authors: Fábio Salles (fsalles@made.com.br)
//  		 Rodrigo Silva (rsilva@made.com.br)
//
//	Incluida funcao buildTextArea e colocado CLASS e FieldType em diversas funcoes
//	Removida funcoes obsoletas
//
////////////////////////////MADE//Internet//Services////////////////////////////

// Function which creates a hidden input
// @input: name
// @input: value
// @output: oHidden - Input Object
function buildHidden( name, value ) {

	var oHidden = document.createElement( "INPUT" ) ;

	oHidden.type = "hidden" ;
	oHidden.name = name ;
	oHidden.id = name ;
	oHidden.value = value ;
	
	return oHidden ;
}

function buildSelect( name, fieldName, classe, aValues ) {

	var oSelect = document.createElement( "SELECT" ) ;
	oSelect.name = name ;
	oSelect.fieldName = fieldName ;
	oSelect.className = classe ;
	
	oSelect.length = aValues.length;

	for ( var i = 0; i < oSelect.length; i++ ) {
		oSelect.options[i].value = aValues[i];
		oSelect.options[i].text = aValues[i];
	}
	

	return oSelect;
}

// Function which creates a format text
// @input: subtitle
// @output: oFont - Font Object
/*
function buildTitle( title ) {

	var oFont = document.createElement( "FONT" ) ;
	var oBold = document.createElement( "B" ) ;

	oFont.className = "text02" ;
	oBold.innerText = title ;
	oFont.appendChild( oBold ) ;
	
	return oFont ;
}
*/

// Function which creates a format text
// @input: subtitle
// @output: oFont - Font Object
/*
function buildSubtitle( subtitle ) {

	var oFont = document.createElement( "FONT" ) ;

	oFont.className = "text03" ;
	oFont.innerText = subtitle ;
	
	return oFont ;
}
*/

// Function which creates a sep
// @input: width
// @input: height
// @output: oSep - Imagem Object
function buildSep( x, y ) {
	var oSep = buildImg( x, y, "imagens/sep.gif", "" ) ;
	return oSep ;
}


// Function which creates an image
// @input: width
// @input: height
// @input: src
// @input: alt
// @output: oSep - Imagem Object
function buildImg( x, y, src, alt ) {

	var oImg = document.createElement( "IMG" ) ;

	oImg.src = src ;
	oImg.width = x ;
	oImg.height = y ;
	oImg.alt = alt ;
	oImg.border = "0" ;
	oImg.style.display = "block" ;
	
	return oImg ;
}


// Function which removes a row
// @input: table
// @input: row Index
// @output: void
function removeRow( oTable, rowId ) {
	var oRow = oTable.all( rowId ) ;
	oTable.deleteRow( oRow.rowIndex ) ;
}


function buildInput( name, fieldName, fieldType, value, classe, size, maxlength ) {

	var oInput = document.createElement( "INPUT" ) ;
	
	oInput.type = "text" ;
	oInput.name = name ;
	oInput.id = name ;
	if (fieldName != null) oInput.fieldName = fieldName ;
	if (fieldType != null) oInput.fieldType = fieldType ;
	oInput.value = value ;
	if (classe != null) oInput.className = classe ;
	if (size != null) oInput.size = size ;
	if (maxlength != null) oInput.maxLength = maxlength ;
	
	return oInput ;
}

function buildTextArea( name, fieldName, fieldType, value, classe, cols, rows ) {

	var oInput = document.createElement( "TEXTAREA" ) ;
	
	oInput.name = name ;
	oInput.id = name ;
	if (fieldName != null) oInput.fieldName = fieldName ;
	if (fieldType != null) oInput.fieldType = fieldType ;
	oInput.value = value ;
	if (classe != null) oInput.className = classe ;
	if (cols != null) oInput.cols = cols ;
	if (rows != null) oInput.rows = rows ;
	
	return oInput ;
}

// Function which moves a row up
// @input: table
// @input: row Index
// @output: void

function upRow( oTable, rowId ) {
	var oRow = oTable.all( rowId ) ;
	if ( oRow.rowIndex > 0 )
		oTable.moveRow( oRow.rowIndex, oRow.rowIndex-1 ) ;
}


// Function which moves a row down
// @input: table
// @input: row Index
// @output: void
function downRow( oTable, rowId ) {
	var oRow = oTable.all( rowId ) ;
	if ( (oRow.rowIndex+1) < oTable.rows.length )
		oTable.moveRow( oRow.rowIndex, oRow.rowIndex+1 ) ;
}

// Funcoes de mover linhas mais modernas
// (exigem apenas o objeto Row a ser movido
function primeiraLinha(oTR)
{
	var oTabela = oTR.parentElement.parentElement;
	
	if(oTR.rowIndex > 1)
		safeMoveRow( oTabela, oTR.rowIndex, 0 );
}

function sobeLinha(oTR)
{
	var oTabela = oTR.parentElement.parentElement;
	
	if(oTR.rowIndex > 1)
		safeMoveRow( oTabela, oTR.rowIndex, oTR.rowIndex - 1 );
}

function desceLinha(oTR)
{
	var oTabela = oTR.parentElement.parentElement;
	if(oTR.rowIndex < oTabela.rows.length - 1 )
		safeMoveRow( oTabela, oTR.rowIndex, oTR.rowIndex + 1 );
}

function ultimaLinha(oTR)
{
	var oTabela = oTR.parentElement.parentElement;
	
	if(oTR.rowIndex < oTabela.rows.length - 1 )
		safeMoveRow( oTabela, oTR.rowIndex, oTabela.rows.length - 1 );
}

function apagaLinha(oTR)
{
	var oTabela = oTR.parentElement.parentElement;
	oTabela.deleteRow( oTR.rowIndex ) ;
}

function safeMoveRow(Table, FromIndex, ToIndex) {

	var Checkboxes = Table.all.tags("INPUT"), i = 0, arr = [];
	
	for(; i < Checkboxes.length; i++)
		if(Checkboxes[i].type == "checkbox") arr.push(Checkboxes[i], Checkboxes[i].checked);
	
	Table.moveRow( FromIndex, ToIndex );
	
	while(arr.length > 0)
		arr.shift().checked = arr.shift();
	
}

function clonaTemplateContainer(oTemplate, oContainer) {

	// clona o template
	var oClone = oTemplate.cloneNode(true); // true = Clone Object and its Children
	
	// zera o id do clone (pra nao ser mais reconhecido como template)
	oClone.id = "";
	
	// Adiciona o clone ao container
	// (verificando antes se o container é uma tabela, que possui tratamento especial)
	if (oContainer.tBodies!=null) {
		oContainer.tBodies[0].appendChild(oClone);
	} else {
		oContainer.appendChild(oClone);
	}
	
	// Varre os elementos do template e zera os fieldTemplate
	// pros campos nao serem ignorados pelo novo madeCheckForm
	var aCampos = oClone.all;			
	for(i=0; i < aCampos.length; i++){
	
		// verifica se esse elemento é um elemento de form
		if ( aCampos[i].form != null ) {
			 
			// zera o fieldTemplate
			aCampos[i].fieldTemplate = null;
		}
		
	}
}
