function deleteRow(result){
	
	//console.log(result);
	var index = result.index;
	
        var el = $$('#'+ result.id +' tbody');
	
        var rows = el[0].getElements('tr');
	//console.log(result);
	rows[index].destroy();
	
        rows = el[0].getElements('tr');
	
	for(i=index; i<rows.length; i++){
		var inputs = rows[i].getElements('input');
		//inputs = $A(inputs)
		$each(inputs, function(o){
			aidx = getIndex(o.name); 
			o.id = o.name = o.name.replace(''+aidx, ''+(aidx-1));
		});
		
                var selects = rows[i].getElements('select');
		//selects = $A(selects)
		$each(selects, function(o){
			aidx = getIndex(o.name); 
			o.id = o.name = o.name.replace(''+aidx, ''+(aidx-1));
		});
                
                var btn = rows[i].getElements('img');
		//selects = $A(selects)
		$each(btn, function(o){
			aidx = getIndex(o.name); 
			o.id = o.name = o.name.replace(''+aidx, ''+(aidx-1));
                        o.setProperty('rel', ''+(aidx-1));
		});		
	}	
	
	
}


function callDecode(o) {
	var map = new Object();
	
	map["field"] = o.id;
	map["value"] = o.value;
	map["index"] = getIndex(o.id);	
    DwrController.decode(map, { timeout:100000, callback:decode });
}

function createToolCell(id, idx, img, def){
	
    var el = new Element('img', {   'src': img,
                                     'id': id,
								   'name': id,
								    'rel': idx,
								  'class': 'btn'
								   });
        if(def){                                                           
            el.addEvent('click', callDelRow.bindWithEvent(el));
	}
	return el;
}

function createInput(id, value, size, cssClass, valueType){
    
    var el = new Element('input', {'type': 'text',
                                     'id': id,
                                   'name': id,
                                  'class': cssClass
									});
        if(size!=null){
            el.setProperty('size', size);
        }
	
	if(valueType && valueType=='numeric'){
		el.setProperty('class', 'dx');
	}
	
	if(value!=null){
		el.setProperty('value', value);
		if($type(value)=='number') el.addClass('dx');
	}
	
	return el;

}

function createRadio(name, value, i){
    
    var el = new Element('input', {'type': 'radio',
                                     'id': name+i,
								   'name': name,
								   'value': value
									});
	
	
	return el;

}


function createHidden(id, value){
    
    var el = new Element('input', {'type': 'hidden',
                                     'id': id,
								   'name': id
									});
	
	if(value!=null){
		el.setProperty('value', value);
	}
	
	return el;

}

function createSelect(id, value){
    
    var el = new Element('select', {  'id': id,
									'name': id
									});
	
	if(value!=null){
		el.setProperty('value', value);
	}
	
	return el;

}

function pupulateDD(data){
    //DWRUtil.removeAllOptions(data.selobj);
    addOptions(data.selobj, data.theList, data.value, data.label);
    if(data.afterPopulateEvent!=null) $(data.selobj).fireEvent(data.afterPopulateEvent);
}

function addOptions(obj, list, value, label){

    var select = $(obj);
    //console.log(select);
    list.each(function(item, index){
        //console.log(item, index)

        var opt = new Element('option', {
            'value': item[value],
            'html': item[label]
        });

        select.adopt(opt);
    });
}

function getIndex(name){
  return name.substring(name.indexOf('[')+1, name.indexOf(']'));
}

function setupDateInputs(fields,format){
	
	var dateFormat = "%d/%m/%Y";
	
	if(format!=null){dateFormat = format}

    fields.each(
					function(item, index){
						//if($(item)!=null){
							Calendar.setup(
							{
								inputField  : item.id,      // id of the input field
								ifFormat    : dateFormat,
								eventName   : 'focus',
								align		: 'Br'
								//date        : today
							}
							);                
						//}
					});	
}



