id=0;
function add_deleteable(innerHTML,before_elm,delete_text){
    var elm=document.createElement('span');
    innerHTML=innerHTML.replace("-delete_but-","<span id='del_but_insert_gen"+id+"'></a>");
    elm.innerHTML=innerHTML;
    var but=document.createElement('button');
    but.innerHTML=delete_text;
    but.del_elm=elm;
    but.onclick=function(){this.del_elm.remove();this.remove();return false};
    $(elm).injectBefore(before_elm);
    if($('del_but_insert_gen'+id) != undefined)$(but).injectBefore($('del_but_insert_gen'+id));
    else $(but).injectBefore(before_elm);
    //document.getElementById(before_elm).innerHTML="test";
    id++;
}
function open_window(url,x,y) {
		var win=window.open(url,'windowPop','toolbar=0,location=0,scrollbars=1,width=' + x + ',height=' + y + ',resizable=0,left=150');
		return win;
}

function table_generator(){
	this.table=false;
	this.rows=0;
	this.cols=0;
	this.first_row=null;
	this.rows_array=new Array;
	this.cols_array=new Array;
	this.del_button='x';
	this.left_top_cell='';
	this.input_obj=new Element('input',{type:'text',size:'2'});
	this.input_events={};
	this.input_enable=false;
	this.select_obj=new Element('select',{});
	this.select_enable=false;
	this.select_events={};
	this.enabled=true;
	this.table_init=function(table_obj){
		if(document.all)this.table=$(table_obj).firstChild;
		else this.table=$(table_obj);
		return this;
	};
	this.add_row=function(){
		if(this.enabled){
			if(this.cols == 0 && this.rows == 0){
				this.init();
			}
			this.rows++;
			var my_row=new Element('tr',{}).inject(this.table);
			var main_cell=new Element('td',{events:{'mousedown':function(){this.func.del_row(this)}},styles:{'cursor':'pointer'}}).setHTML(this.del_button).inject(my_row);
			main_cell.func=this;
			main_cell.row_id=this.rows_array.length;
			row_count=this.cols_array.length;
			this.cols_array[this.rows_array.length]=new Array;
			for(var x=0;x<this.cols_array[0].length;x++){
				if(this.cols_array[0][x] != "empty"){
					var my_td=new Element('td',{}).inject(my_row);
					this.cols_array[row_count][this.cols_array[row_count].length]=my_td;
					if(this.input_enable)this.cols_array[row_count][this.cols_array[row_count].length-1]._input=this.input_obj.clone().inject(my_td).addEvents(this.input_events);
					if(this.select_enable)this.cols_array[row_count][this.cols_array[row_count].length-1]._select=this.select_obj.clone().inject(my_td).addEvents(this.select_events);
				}else this.cols_array[row_count][this.cols_array[row_count].length]="empty";
			}
			this.rows_array[this.rows_array.length]=my_row;
		}	
	};
	this.add_col=function(){
		if(this.enabled){
			if(this.cols == 0 && this.rows == 0){
				this.add_row();
			}
			this.cols++;
			var main_cell=new Element('td',{events:{'mousedown':function(){this.func.del_col(this)}},styles:{'cursor':'pointer'}}).setHTML(this.del_button).inject(this.first_row);
			main_cell.func=this;
			main_cell.col_id=this.cols_array[0].length;
			for(var x=0;x<this.rows_array.length;x++){
				var my_td=new Element('td',{}).inject(this.rows_array[x]);
				this.cols_array[x][this.cols_array[x].length]=my_td;
				if(this.input_enable)this.cols_array[x][this.cols_array[x].length-1]._input=this.input_obj.clone().inject(my_td).addEvents(this.input_events);
				if(this.select_enable)this.cols_array[x][this.cols_array[x].length-1]._select=this.select_obj.clone().inject(my_td).addEvents(this.select_events);
			}
		}
	};
	this.del_row=function(elm){
		if(this.enabled){
			this.rows--;
			if(this.rows == 0 && this.cols == 0){
				this.first_row.remove();
				this.cols=0;
			}
			this.rows_array[elm.row_id].remove();
			this.rows_array[elm.row_id]="empty";
			this.cols_array[elm.row_id]="empty";
		}
	};
	this.del_col=function(elm){
		if(this.enabled){
			this.cols--;
			if(this.rows == 0 && this.cols == 0){
				this.first_row.remove();
				this.rows=0;
			}
			elm.remove();
			this.cols_array.each(function(el){
						el[elm.col_id].remove();
						el[elm.col_id]="empty";
			});
		}
	};
	this.init=function(){
		this.first_row=new Element('tr',{}).inject(this.table);
		new Element('td',{}).setHTML(this.left_top_cell).inject(this.first_row);
	};
	this.data_fill=function(data,property){
		var pos=0;
		data=data.split(';');
		if(property == undefined)property="innerHTML";
		for(var x=0;x<this.cols_array.length;x++){
			for(var y=0;y<this.cols_array[x].length;y++){
				if(this.cols_array[x] != "empty" && this.cols_array[x][y] != "empty")eval('this.cols_array[x][y].'+property+'="'+data[pos++]+'"');
			}
		}
		return true;
	};
	this.data_get=function(property){
		var pos=0;
		var return_array=new Array;
		if(property == undefined)property="innerHTML";
		for(var x=0;x<this.cols_array.length;x++){
			for(var y=0;y<this.cols_array[x].length;y++){
				if(this.cols_array[x] != "empty" && this.cols_array[x][y] != "empty")eval('return_array[return_array.length]=this.cols_array[x][y].'+property);
			}
		}
		return return_array.join(";");
	}
	this.set_property=function(property,value){
		if(property == undefined)property="innerHTML";
		for(var x=0;x<this.cols_array.length;x++){
			for(var y=0;y<this.cols_array[x].length;y++){
				if(this.cols_array[x] != "empty" && this.cols_array[x][y] != "empty")eval('this.cols_array[x][y].'+property+'='+value+'');
			}
		}
		return true;
	};	
}