 $(document).ready(function() {
	//HERE  I BIND THE ADDFIELD EVENT TO THE BUTTON
		$(".bt_plus").each(function (el){
			$(this).bind("click",addField);
									 });
							});


function addField(){
// ID of div element that was clicked, with 'div_' removed

var clickID = parseInt($(this).parent('div').attr('id').replace('div_',''));

// Generate ID of element to be created
var newID = (clickID+1);

// Creating and adding new element
$newClone = $('#div_'+clickID).clone(true);

//Asign new id to the cloned div
$newClone.attr("id",'div_'+newID);

//Asign new id to the input field and clear the value
$newClone.children("input").eq(0).attr("id",'materiales'+newID).val('').css("color","black");

//Clear the value of the second input field
$newClone.children("input").eq(1).val('');

//Asign new id to the input button
$newClone.children("input").eq(2).attr("id",newID)

//Insert the new cloned div after the original
$newClone.insertAfter($('#div_'+clickID));

//I change the + value of the button into - and then i unbind the addfield function
$("#"+clickID).val('-').unbind("click",addField);

//HEres comes the problem. Im unable to bind the delrow function to the button
$("#"+clickID).bind("click",delRow);

//adding autosugges property
var options = {
	script: "get_materiales.php?limit=6&",
	varname: "materiales",
	json:false,
	maxresults:10,
 timeout:9999999,
	noresults:'Ingresar nuevo material a la base de datos'
};
var as3 = new bsn.AutoSuggest('materiales'+newID, options);


									 
							
					   
}



function delRow() {
// destroying element
$(this).parent('div').remove().log();

}
 jQuery.fn.log = function (msg) {
      console.log("%s: %o", msg, this);
      return this;
  };