	/*  ###########################################################  */
	/*  ##################  set field as changed  #################  */
	/*  ###########################################################  */
		
		function set_field_changed(changed_node){
			
			var current_node = changed_node;
			while(current_node.nodeName!='FIELDSET'){
				current_node = current_node.parentNode;
			}
			var id = current_node.id;  
			
			// fieldset id should are like: 'fieldset_YYY' where YYY is the indicator code number
			var indicator_code = id.substr(id.lastIndexOf("_")+1);
			document.getElementById(indicator_code+'_form_changed').value=true;
			
		}
		
	/*  #################################################################  */
	/*  #####################  Multi Record Inputs  #####################  */
	/*  #################################################################  */

		function add_row(indicator_code){
		
			var tbody_obj = document.getElementById('tbl_'+indicator_code).tBodies;
			var table_rows = tbody_obj[0].childNodes;
			var num_rows = table_rows.length;
			
			for(r=0;r<num_rows;r++){
				if(table_rows[r].style.display=="none"){
					var last_row_index = r;
					break;
				}
			}
			/* set visible next row*/
			var next_index = parseInt(last_row_index);
			table_rows[next_index].style.display="";
		}

		
	/*  ###############################################################  */
	/*  #####################  Expandible Tables  #####################  */
	/*  ###############################################################  */

		function add_TableRow(indicator_code){
			
			/* table elements TABLE - TBODY - TR */
			var table = document.getElementById('tbl_'+indicator_code);
			if(table.childNodes[0].nodeType==1){
				var table_body = table.childNodes[0];	/* IE */
			} else {
				var table_body = table.childNodes[1];	/* other browsers */
			}
			var row_elems = table_body.getElementsByTagName('tr');
			
			var num_rows = row_elems.length;
			var lastrow = parseInt(num_rows)-1;  /*  num of the last row  */
			if(row_elems[lastrow].id.search(/total/i)>=0){
				lastrow = lastrow-1;    /* if exist "total" (calculated) row */
			} 
			
			/* clone existing row (the 2nd of the table) */
			var src_row = row_elems[1];
			var num_columns = src_row.getElementsByTagName('td').length;
			var new_row = src_row.cloneNode(true);
			
				/* calculate new incremental ID */
				var new_row_id = parseInt(lastrow+1);
				new_row.id = 'row'+new_row_id;
				
				/* calculate new row position */
				new_row_pos = lastrow+1;
				
				update_row_fields(new_row,num_columns,lastrow);
				
			
			/* append new row */
			if(table.childNodes[0].nodeType==1){	
			
				//var input_elem = row_elems[num_rows-1].getElementsByTagName('td')[0].childNodes[0].childNodes[0].value;
							
				/* table with total (calculated) */
				if (row_elems[num_rows - 1].id.search(/total/i) >= 0) {
					table.childNodes[0].insertBefore(new_row,row_elems[new_row_pos]);
				} 
				
				/* table with partial total */
				/*else if(input_elem.search(/Total des autres/i)>=0 ){
					table.childNodes[0].insertBefore(new_row,row_elems[new_row_pos-1]);
				}*/
				
				/* free tables (no total) */
				else {
					table.childNodes[0].appendChild(new_row);
				}
			} else {
				table.childNodes[1].insertBefore(new_row,row_elems[new_row_pos]);
			}
			
			/* suggestion div */
			update_suggestion_field(new_row,num_columns,lastrow);
			
			
		
		}
		
		/* for suggestion List Inputs: increase ID, empty VALUE and activate onkeypress*/ 
		function update_suggestion_field(new_row,num_columns,lastrow){
			
			for(i=0;i<num_columns;i++){
				
				var column = new_row.getElementsByTagName('td')[i];
				
				var input = column.childNodes[0];
				if(input.nodeName!='DIV' && input.nodeName=='SCRIPT'){
				input = column.childNodes[1];
				}
				
				var suggestion = '';
				var old_suggestion_id = '';
				
				if(input.id==''){
					
						// input
						var input_field = input.childNodes[0];
						var old_id = input_field.id;
						input_field.id = old_id.slice(0,old_id.length-1)+parseInt(lastrow);
						input_field.name = input_field.id;
						input_field.value = '';
					
						// suggestion list 
						suggestion = input.childNodes[1];
						old_suggestion_id = suggestion.id;
						suggestion.id = old_suggestion_id.slice(0,old_suggestion_id.length-5)+parseInt(lastrow)+'_div';
						var src_id = old_suggestion_id.slice(0,old_suggestion_id.length-5)+parseInt(lastrow);
						suggestion.name = suggestion.id;
						
						var list_name = input.childNodes[2].value;
						var todo = "createAutoComplete('"+src_id+"',"+list_name+");";
						eval(todo);
						
				}
			}
			
		}
		
		
		/* for each COLUMNS: increase ID and empty VALUE */ 
		function update_row_fields(row,num_columns,lastrow){
			
			for(i=0;i<num_columns;i++){
				var column = row.getElementsByTagName('td')[i];
				
				// get input form
				var nodes = column.childNodes;
				var num = nodes.length;
				for(n=0;n<num;n++){
					
					var input = column.childNodes[n];
				
					// new id 
					if(input.id!=''){
						var old_id = input.id;
						input.id = old_id.slice(0,old_id.length-1)+parseInt(lastrow);
						input.name = input.id;
					}
					
					// class
					if(input.className=='readonly'){
						input.className = '';
					}
					if(input.className!='total'){
						input.readOnly = false;
					}
					
					// value 
					if(input.type!='checkbox'){
						input.value = '';
					}
					input.checked = false;
				}	
			}
			
		}
				
	
	
	
