
	var tableData = new Array();
	
	var maxcolid = 0;
	var maxrowid = 0;

    var row_edited = 0;
    var col_edited = 0;
	
	show_tools = true;	
	
	var nbsp = '\u00a0';
		
	function pageInit() {
		//tableData = new Array();
			
		tableData[0] = new Array("","00","00","00","00","00");
		tableData[1] = new Array("","11","12","13","14","15");
		tableData[2] = new Array("","21","22","23","24","25");
		tableData[3] = new Array("","31","32","33","34","35");

		create_table(5,3, tableData);
	}
	
    function create_table(rows,cols,data_array,divid) {
	
        // get the reference for the body
        var mybody = document.getElementsByTagName("body")[0];

	  	if (typeof(divid)=='undefined') divid="table_editor";
        
        //var mydiv = document.getElementById("table_editor");
        var mydiv = document.getElementById(divid);
		
        // creates <table> and <tbody> elements
        mytable     = document.createElement("table");
        mytablebody = document.createElement("tbody");

		//show_tools = true;
		start = 1;
		if (show_tools) start = 0;
		
        // creating all cells
        for(var j = start; j <= rows; j++) {
            // creates a <tr> element
            mycurrent_row = document.createElement("tr");
            
            if (j==1) {
	            	mycurrent_row.setAttribute("class","tableHeader");
	            	mycurrent_row.className = "tableHeader"; //IE
			}	            	
            if (j>1) {
	            if (j%2 == 0) mycurrent_row.setAttribute("class","grey");
	            if (j%2 == 0) mycurrent_row.className = "grey"; //IE
        	}    
            
			maxrowid = Math.max(maxrowid,rows);
			
            for(var i = start; i <= cols; i++) {
			
				maxcolid = Math.max(maxcolid,cols);
			
                // creates a <td> element
                mycurrent_cell = document.createElement("td");
               
                if (i==0 &&j==0)  mycurrent_cell.setAttribute("width","16");
                
   				//if (i>0) mycurrent_cell.setAttribute("width","10%");
   				
   				//column width calculate
   				if (cols > 0)
   					witdth_value = Math.round(100 / cols);
   				else
   					witdth_value = "";
   				if (i>0) mycurrent_cell.setAttribute("width",witdth_value+"%");
   				
				checkbox = document.createElement("input");
				
				link = document.createElement('A');
				link.innerHTML = '<IMG src="../images/b_drop.gif" border=0>';
				
				if (i==0) {
					checkbox.setAttribute("onclick","javascript:alert(\'row" + j + "\'); table_remove_row(\'"+j+"\');");
					checkbox['onclick'] = new Function("javascript:alert(\'row" + j + "\'); table_remove_row(\'"+j+"\');");
					link.setAttribute ('title', 'Usuń wiersz');
					link.href = "javascript:table_remove_row(\'"+j+"\');";
				} 
				if (j==0) {
					checkbox.setAttribute("onclick","javascript:alert(\'col" + i + "\'); table_remove_col(\'"+i+"\');");
					checkbox['onclick'] = new Function("javascript:alert(\'col" + i + "\'); table_remove_col(\'"+i+"\');");
					link.setAttribute ('title', 'Usuń kolumnę');
					link.href = "javascript:table_remove_col(\'"+i+"\');";
				}
				
				checkbox.setAttribute("type","checkbox");
				
                // creates a Text Node
                //currenttext = document.createTextNode('cell is row ' + j + ', column ' + i);
                currenttext = document.createTextNode(data_array[i][j]);
                // appends the Text Node we created into the cell <td>
				
				//currenttext = document.createElement('SPAN');
				//currenttext.innerHTML = data_array[i][j];
                
                
				if (i==0 || j==0) {
					if (j==0 && i==0) {
						//mycurrent_cell.appendChild(document.createTextNode(nbsp));
						mycurrent_cell.appendChild(document.createTextNode(" "));
					} else {
						//mycurrent_cell.appendChild(checkbox);
						mycurrent_cell.appendChild(link);
					}
				} else {
					//mycurrent_cell.appendChild(currenttext);   //HTML tags not allowed
					mycurrent_cell.innerHTML = data_array[i][j]; //HTML tags allowed
				}

				//mycurrent_cell.setAttribute("onclick",   "javascript:alert(\'row" + j + "\'); table_edit_cell(\'"+j+"\',\'"+i+"\');");
				//mycurrent_cell['onclick'] = new Function("javascript:alert(\'row" + j + "\'); table_edit_cell(\'"+j+"\',\'"+i+"\');");
				mycurrent_cell.setAttribute("onclick",   "javascript:table_edit_cell(\'"+j+"\',\'"+i+"\');");
				mycurrent_cell['onclick'] = new Function("javascript:table_edit_cell(\'"+j+"\',\'"+i+"\');");
								
				mycurrent_cell.setAttribute("id","r" + j + "c" +i );
				mycurrent_cell.setAttribute("name","col" +i );
				
                // appends the cell <td> into the row <tr>
                mycurrent_row.appendChild(mycurrent_cell);
				
            }
			
			mycurrent_row.setAttribute("id","row"+j);
			
            // appends the row <tr> into <tbody>
            mytablebody.appendChild(mycurrent_row);
        }

        // appends <tbody> into <table>
        mytable.appendChild(mytablebody);
		
		mydiv.appendChild(mytable);
		
        // appends <table> into <body>
        //mybody.appendChild(mytable);
        // sets the border attribute of mytable to 2;
        mytablebody.setAttribute("id","tbody_data");
        mytable.setAttribute("id","table_data");
        //mytable.setAttribute("width","100%");
        
        mytable.setAttribute("border","0");
        mytable.setAttribute("width","780");
        mytable.setAttribute("cellspacing","0");
        mytable.setAttribute("cellpadding","0");
        mytable.cellSpacing = "0"; //IE
        mytable.cellPadding ="0"; //IE
        
    }
	
    function table_add_col(data_array) {
				
		mydiv = document.getElementById("table_editor");
	
		myrows = mydiv.getElementsByTagName("tr");
		rows = myrows.length;

		if (show_tools) rows = rows - 1;
		
		//cols = //mydiv.getElementsByTagName("td");
		//cols = //cols.length/(rows+1);
		
		cols = maxcolid + 1;
		
		//show_tools = true;
		start = 1;
		if (show_tools) start = 0;
		
        // creating all cells
        for(var j = start; j <= rows; j++) {
	
	        //mycurrent_row = document.getElementById('row'+j);
			mycurrent_row = myrows[j-start];
			rowid = mycurrent_row.getAttribute("id");
			rowid = rowid.replace('row','');			
			
            for(var i = cols; i <= cols; i++) {
			
				maxcolid = Math.max(maxcolid,cols);
			
                // creates a <td> element
                mycurrent_cell = document.createElement("td");

				checkbox = document.createElement("input");
				
				link = document.createElement('A');
				link.innerHTML = '<IMG src="../images/b_drop.gif" border=0>';
				
				if (i==0) {
					checkbox.setAttribute("onclick","javascript:alert(\'row" + rowid + "\'); table_remove_row(\'"+rowid+"\');");
					checkbox['onclick'] = new Function("javascript:alert(\'row" + rowid + "\'); table_remove_row(\'"+rowid+"\');");
					link.setAttribute ('title', 'Usuń wiersz');
					link.href = "javascript:table_remove_row(\'"+rowid+"\');";
				} 
				if (j==0) {
					checkbox.setAttribute("onclick","javascript:alert(\'col" + i + "\'); table_remove_col(\'"+i+"\');");
					checkbox['onclick'] = new Function("javascript:alert(\'col" + i + "\'); table_remove_col(\'"+i+"\');");
					link.setAttribute ('title', 'Usuń kolumnę');
					link.href = "javascript:table_remove_col(\'"+i+"\');";
				}
				
				
				
				checkbox.setAttribute("type","checkbox");
				
                // creates a Text Node
                //currenttext = document.createTextNode('cell is row ' + rowid + ', column ' + i);
                currenttext = document.createTextNode(' ');
                tableData[i] = "";
				tableData[i][rowid] = 'cell is row ' + rowid + ', column ' + i;
			
                // appends the Text Node we created into the cell <td>
				
				if (i==0 || j==0) {
					if (j==0 && i==0) {
						//mycurrent_cell.appendChild(document.createTextNode(nbsp));
						mycurrent_cell.appendChild(document.createTextNode(" "));
					} else {
						//mycurrent_cell.appendChild(checkbox);
						mycurrent_cell.appendChild(link);
					}
				} else {
					mycurrent_cell.appendChild(currenttext);
				}
				
				//mycurrent_cell.setAttribute("onclick",   "javascript:alert(\'row" + rowid + "\'); table_edit_cell(\'"+rowid+"\',\'"+i+"\');");
				//mycurrent_cell['onclick'] = new Function("javascript:alert(\'row" + rowid + "\'); table_edit_cell(\'"+rowid+"\',\'"+i+"\');");
				mycurrent_cell.setAttribute("onclick",   "javascript:table_edit_cell(\'"+rowid+"\',\'"+i+"\');");
				mycurrent_cell['onclick'] = new Function("javascript:table_edit_cell(\'"+rowid+"\',\'"+i+"\');");
				
				mycurrent_cell.setAttribute("id","r" + rowid + "c" +i );
				mycurrent_cell.setAttribute("name","col" +i );
				
                // appends the cell <td> into the row <tr>
                mycurrent_row.appendChild(mycurrent_cell);
				
            }
			
		}
		
	}

    function table_add_row(data_array) {

		mydiv = document.getElementById("table_editor");
		
        mytablebody = document.getElementById("tbody_data");
		
		myrow0 = document.getElementById("row0");
		if (!myrow0) myrow0 = document.getElementById("row1");
		
		mycols = myrow0.getElementsByTagName("td");

		myrows = mydiv.getElementsByTagName("tr");
		//rows = myrows.length-1;
		rows = myrows.length;
		
		cols = mydiv.getElementsByTagName("td");
		//cols = (cols.length)/(rows+1);
		cols = (cols.length)/(rows);
		
		if (show_tools) cols = cols - 1;
		
		rows = maxrowid + 1;
		
		//show_tools = true;
		start = 1;
		if (show_tools) start = 0;
		
		//alert('cols'+cols+'rows'+rows);
		
        // creating all cells
        for(var j = rows; j <= rows; j++) {
	
			maxrowid = Math.max(maxrowid,rows);
	
	        //mycurrent_row = document.getElementById('row'+j);
			mycurrent_row = document.createElement("tr");

            if (j==1) {
	            	mycurrent_row.setAttribute("class","tableHeader");
	            	mycurrent_row.className = "tableHeader"; //IE
			}	            	
            if (j>1) {
	            if (j%2 == 0) mycurrent_row.setAttribute("class","grey");
	            if (j%2 == 0) mycurrent_row.className = "grey"; //IE
        	}    
			
            for(var i = start; i <= cols; i++) {

				mycurrent_col = mycols[i-start];
				colid = mycurrent_col.getAttribute("name")
				colid = colid.replace('col','');
			
                // creates a <td> element
                mycurrent_cell = document.createElement("td");
				
				checkbox = document.createElement("input");
				
				link = document.createElement('A');
				link.innerHTML = '<IMG src="../images/b_drop.gif" border=0>';
				
				if (i==0) {
					checkbox.setAttribute("onclick","javascript:alert(\'row" + j + "\'); table_remove_row(\'"+j+"\');");
					checkbox['onclick'] = new Function("javascript:alert(\'row" + j + "\'); table_remove_row(\'"+j+"\');");
					link.setAttribute ('title', 'Usuń wiersz');
					link.href = "javascript:table_remove_row(\'"+j+"\');";
				} 
				if (j==0) {
					checkbox.setAttribute("onclick","javascript:alert(\'col" + colid + "\'); table_remove_col(\'"+colid+"\');");
					checkbox['onclick'] = new Function("javascript:alert(\'col" + colid + "\'); table_remove_col(\'"+colid+"\');");
					link.setAttribute ('title', 'Usuń kolumnę');
					link.href = "javascript:table_remove_col(\'"+colid+"\');";
				}
				
				checkbox.setAttribute("type","checkbox");
				
                // creates a Text Node
                //currenttext = document.createTextNode('cell is row ' + j + ', column ' + colid);
                currenttext = document.createTextNode(' ');
                tableData[colid] = "";
				tableData[colid][j] = 'cell is row ' + j + ', column ' + colid;
				
                //currenttext = document.createTextNode('cell is row ' + j + ', column ' + i);
                // appends the Text Node we created into the cell <td>
				
				if (i==0 || j==0) {
					if (j==0 && i==0) {
						//mycurrent_cell.appendChild(document.createTextNode(nbsp));
						mycurrent_cell.appendChild(document.createTextNode(" "));
					} else {
						//mycurrent_cell.appendChild(checkbox);
						mycurrent_cell.appendChild(link);
					}
				} else {
					mycurrent_cell.appendChild(currenttext);
				}
				
				//mycurrent_cell.setAttribute("onclick",   "javascript:alert(\'row" + j + "\'); table_edit_cell(\'"+j+"\',\'"+colid+"\');");
				//mycurrent_cell['onclick'] = new Function("javascript:alert(\'row" + j + "\'); table_edit_cell(\'"+j+"\',\'"+colid+"\');");
				mycurrent_cell.setAttribute("onclick",   "javascript:table_edit_cell(\'"+j+"\',\'"+colid+"\');");
				mycurrent_cell['onclick'] = new Function("javascript:table_edit_cell(\'"+j+"\',\'"+colid+"\');");
				
				mycurrent_cell.setAttribute("id","r" + j + "c" +colid );
				mycurrent_cell.setAttribute("name","col" +colid );
				
                // appends the cell <td> into the row <tr>
                mycurrent_row.appendChild(mycurrent_cell);
				
            }

			mycurrent_row.setAttribute("id","row"+j);
			
            mytablebody.appendChild(mycurrent_row);
		
		}
		
	}
	
	
	
	function table_remove_col(col) {

		mydiv = document.getElementById("table_editor");

		myrows = mydiv.getElementsByTagName("tr");
		rows = myrows.length-1;
	
        for(var j = start; j <= rows; j++) {

			mycurrent_row = myrows[j];
			rowid = mycurrent_row.getAttribute("id");
			rowid = rowid.replace('row','');
			
			var mycurrent_cell = document.getElementById("r" + rowid + "c" + col);
			if (mycurrent_cell) {
				var throwawayNode = mycurrent_row.removeChild(mycurrent_cell);
			}
		}
	
	}
	
	function table_remove_row(row) {

        mytablebody = document.getElementById("tbody_data");
		
		var mycurrent_row = document.getElementById('row'+row);
		if (mycurrent_row) {
			var throwawayNode = mytablebody.removeChild(mycurrent_row);
		}
	
	}		

	function check_edit_cell() {
	
		if (row_edited == 0 || col_edited == 0)	{
			disableElement("tablecell_text");
			//disableElement("tablecell_update");
			
	        var article_text = document.getElementById("tablecell_text");
			article_text.value = "";
			
			return;
		}	
		enableElement("tablecell_text");
		//enableElement("tablecell_update");
		
		return;
	
	}
	
	function table_edit_cell(row,col) {
		
        row_edited = row;
        col_edited = col;
        
		check_edit_cell();
		if (row_edited == 0 || col_edited == 0)	return;
		
		enableElement("tablecell_text");
		//enableElement("tablecell_update");
		
		tablecell_text = document.getElementById("tablecell_text");
		try{ tablecell_text.focus(); } catch (e) {}
		//tablecell_text.focus(); 
		
		mycurrent_cell = document.getElementById("r" + row_edited + "c" +col_edited);
		text = mycurrent_cell.innerHTML;
		        
        var article_text = document.getElementById("tablecell_text");
		try{ article_text.value = text; } catch (e) {}
		//article_text.value = text;
	
	}
	

	function table_update_cell() {

		if (row_edited == 0 || col_edited == 0) return;
		
        var article_text = document.getElementById("tablecell_text");
		text = article_text.value;
		
		mycurrent_cell = document.getElementById("r" + row_edited + "c" +col_edited);
		
		mycurrent_cell.innerHTML = text;
		
		//disableElement("tablecell_update");
		disableElement("tablecell_text");
		
        var article_text = document.getElementById("tablecell_text");
		article_text.value = "";
		
		row_edited = 0;
		col_edited = 0;
		
	}
	
		
		
	function getTableData() {
	
		var tableDataFromDom = new Array();
	
		mydiv = document.getElementById("table_editor");
		myrows = mydiv.getElementsByTagName("tr");
	
		rows = myrows.length-1;
	
        for(var j = start; j <= rows; j++) {

			mycurrent_row = myrows[j];
			rowid = mycurrent_row.getAttribute("id");
			rowid = rowid.replace('row','');
			
			mycols = mycurrent_row.getElementsByTagName("td");
			
			cols =  mycols.length-1;
			
			tableDataFromDom[j] = "";
			_cols = new Array();

			for(var i = start; i <= cols; i++) {
				mycurrent_col = mycols[i];
				
				if (i!=0 && j!=0) {
					//tableDataFromDom[j][i] = mycurrent_col.innerHTML;
					_cols[i] = mycurrent_col.innerHTML;
				} else {
					//tableDataFromDom[j][i] = "";
					_cols[i] = "";
				}
				
			}
			
			tableDataFromDom[j] = _cols;
			
			
		}
		return tableDataFromDom;
	
	}
	
	function ShowTableData(arr) {
	
		if (arr.length>0) {
		
			rows = arr.length-1;
			
	        for(var j = 0; j <= rows; j++) {
			
				cols = arr[j].length-1;
				
				for(var i = 0; i <= cols; i++) {
					alert(arr[j][i]);
				
				}
				
			}
		
		}
		//return out;
		
	}

	function TransformToVarTableData(arr) {
	
		//transformacja po kolumnach
		
		var str = "";
		
		if (arr.length>0) {
		
			rows = arr.length-1;
			cols = arr[0].length-1;
			
	        for(var i = 0; i <= cols; i++) {
			
				//cols = arr[j].length-1;
				
				for(var j = 0; j <= rows; j++) {
					if (j == rows)
						str=str+arr[j][i]+"|-|end|-|";
					else
						str=str+arr[j][i]+"|-|#|-|";
					
				}
				
			}
		
		}
		return str;
		
	}
	
	function TransformToVarTableDataToRow(arr) {
		
		//transformacja po wierszach
	
		var str = "";
		
		if (arr.length>0) {
		
			rows = arr.length-1;
			
	        for(var j = 0; j <= rows; j++) {
			
				cols = arr[j].length-1;
				
				for(var i = 0; i <= cols; i++) {
					if (i == cols)
						str=str+arr[j][i]+"|-|end|-|";
					else
						str=str+arr[j][i]+"|-|#|-|";
					
				}
				
			}
		
		}
		return str;
		
	}
	
	
		
	function getText(captionName)
	{
		
		try	{				
			return document.getElementById(captionName).innerHTML;
		}	catch (e) {}				
		
		return;
		
	}
