function eliminar_seccion(t,posicion)
{	var td = t.parentNode;
	var tr = td.parentNode;
	var table = tr.parentNode;
	table.removeChild(tr);
	for(i=parseInt(posicion)+1;i<document.getElementById("tabla_secciones").rows.length-2;i++){ // el 3 es por que hay otros <tr> que no son secciones
		nuevo_valor=i-1;
		document.getElementById('secciones['+i.toString()+'][fuente_id]').name='secciones['+nuevo_valor.toString()+'][fuente_id]';		
		document.getElementById('secciones['+i.toString()+'][fuente_id]').id='secciones['+nuevo_valor.toString()+'][fuente_id]';
		document.getElementById('secciones['+i.toString()+'][fuente_coleccion_id]').name='secciones['+nuevo_valor.toString()+'][fuente_coleccion_id]';		
		document.getElementById('secciones['+i.toString()+'][fuente_coleccion_id]').id='secciones['+nuevo_valor.toString()+'][fuente_coleccion_id]';
		document.getElementById('secciones['+i.toString()+'][key_seccion]').name='secciones['+nuevo_valor.toString()+'][key_seccion]';		
		document.getElementById('secciones['+i.toString()+'][key_seccion]').id='secciones['+nuevo_valor.toString()+'][key_seccion]';
		document.getElementById('secciones['+i.toString()+'][st_descripcion]').name='secciones['+nuevo_valor.toString()+'][st_descripcion]';		
		document.getElementById('secciones['+i.toString()+'][st_descripcion]').id='secciones['+nuevo_valor.toString()+'][st_descripcion]';
		document.getElementById('secciones['+i.toString()+'][f_orden]').name='secciones['+nuevo_valor.toString()+'][f_orden]';
		document.getElementById('secciones['+i.toString()+'][f_orden]').id='secciones['+nuevo_valor.toString()+'][f_orden]';
		document.getElementById(i.toString()).id=nuevo_valor.toString();
	}
}


function agregar_seccion(id){
	var tabla = document.getElementById("tabla_secciones");
	var ubicacion=tabla.rows.length-3;
	var tbody = document.getElementById(id).getElementsByTagName("TBODY")[0];
	var row = document.createElement("TR");
	var td1 = document.createElement("td");
	td1.setAttribute("class",'tdsinbordes');
	td1.setAttribute("width",'600');

	var input1 = document.createElement("input");
	input1.setAttribute("id",'secciones['+ubicacion+'][fuente_id]');
	input1.setAttribute("name",'secciones['+ubicacion+'][fuente_id]');
	input1.setAttribute("value",'');
	input1.setAttribute("type",'text');
	td1.appendChild(input1);
	var td2 = document.createElement("td");
	
	var td2 = document.createElement("td");
	td1.setAttribute("class",'tdsinbordes');
	td1.setAttribute("width",'600');
	var input2 = document.createElement("input");	
	input2.setAttribute("id",'secciones['+ubicacion+'][fuente_coleccion_id]');
	input2.setAttribute("name",'secciones['+ubicacion+'][fuente_coleccion_id]');
	input2.setAttribute("value",'');
	input2.setAttribute("type",'text');	
	td2.appendChild (input2);
	
	var td3 = document.createElement("td");
	td1.setAttribute("class",'tdsinbordes');
	td1.setAttribute("width",'600');
	var input3 = document.createElement("input");	
	input3.setAttribute("id",'secciones['+ubicacion+'][key_seccion]');
	input3.setAttribute("name",'secciones['+ubicacion+'][key_seccion]');
	input3.setAttribute("value",'');
	input3.setAttribute("type",'text');	
	td3.appendChild (input3);
	
	var td4 = document.createElement("td");
	td1.setAttribute("class",'tdsinbordes');
	td1.setAttribute("width",'600');
	var input4 = document.createElement("input");	
	input4.setAttribute("id",'secciones['+ubicacion+'][st_descripcion]');
	input4.setAttribute("name",'secciones['+ubicacion+'][st_descripcion]');
	input4.setAttribute("value",'');
	input4.setAttribute("type",'text');	
	td4.appendChild (input4);
	
	var td5 = document.createElement("td");
	td1.setAttribute("class",'tdsinbordes');
	td1.setAttribute("width",'600');
	var input5 = document.createElement("input");	
	input5.setAttribute("id",'secciones['+ubicacion+'][f_orden]');
	input5.setAttribute("name",'secciones['+ubicacion+'][f_orden]');
	input5.setAttribute("value",'');
	input5.setAttribute("type",'text');	
	td5.appendChild (input5);
	
	var td6 = document.createElement("td");
	td1.setAttribute("class",'tdsinbordes');
	td1.setAttribute("width",'600');
	var boton = document.createElement("input");
	boton.setAttribute('id',ubicacion);
	boton.setAttribute('onclick',"eliminar_seccion(this,this.id);");
	boton.setAttribute('value',"Quitar Sección");
	boton.setAttribute('type',"button");	
	td6.appendChild (boton);
	
	row.appendChild(td1);
	row.appendChild(td2);
	row.appendChild(td3);
	row.appendChild(td4);
	row.appendChild(td5);
	row.appendChild(td6);
	tbody.appendChild(row);
}


function chequear_campos_secciones()
{	var tabla = document.getElementById("tabla_secciones");
	var exp_reg = /^\d+$/
	for (i=0;i < tabla.rows.length-3;i++ )
	{
              if (document.getElementById('secciones['+i.toString()+'][st_descripcion]').value == ""){
			alert("Asegúrese de completar el campo \"Descripción\" de cada sección.");
			return;
		};
              if (!exp_reg.test(document.getElementById('secciones['+i.toString()+'][f_orden]').value)){
			alert("Asegúrese que el campo \"Orden\" sea un valor entero.");
			return;
		};	      
  	}
	document.forms[1].submit();
}

