//var debug;
var i;
var j;
var rowCount;
var colCount;
var theTable;
var theHead;
var theBody;
var theRows;
var theValues;
var rowSwitch;
var colSwitch;
var cornerBg;
var cornerContainer;
var oldSize;

window.onload = function(){
    drawTable();
    var cornerBg = document.getElementById("cornerBg");
    cornerBg.onload = fitCornerBg;
    cornerBg.src = 'dtbg.gif';
    //var resizeListener = document.getElementById("resizeListener");    
    //resizeListener.onresize = test;
    //document.body.onresize = function(){
    //    alert('Testing: body resized');
    //}
    toggleAll(true, false);
    toggleAll(false, false);
    setInterval('fitCornerBg();', 500);
}

function drawTable(){
	theTable = document.getElementById("theTable");
	theHead = document.getElementById("theHead");
	theBody = document.getElementById("theBody");
	theRows = theBody.rows;
	rowCount = theRows.length;
	colCount = theHead.rows[0].cells.length - 1;
	theValues = new Array(rowCount);
    cornerBg = document.getElementById("cornerBg");
    cornerContainer = document.getElementById("cornerContainer");
	for(i = 0; i< rowCount; i++)
		theValues[i] = new Array(colCount);
		for(i = 0; i < rowCount; i++){
		for(j = 1; j < colCount + 1; j++){
			theValues[i][j] = theRows[i].cells[j].firstChild.nodeValue;
			theRows[i].cells[j].firstChild.nodeValue="";
		}
	}
	rowSwitch = new Array(rowCount);
	for(i=0; i < rowCount; i++)
		rowSwitch[i] = false;
		colSwitch = new Array(colCount);
		for(i=0; i < colCount; i++)
			colSwitch[i] = false;
	theTable.style.display="block";
}

function toggle(id){
	if(id<20){
		if(!colSwitch[id-1]){
			colSwitch[id-1] = true;
			for(i = 0; i < rowCount; i++){
				if(rowSwitch[i])
					theRows[i].cells[id].firstChild.nodeValue = theValues[i][id];
			}
		}
		else{
			colSwitch[id-1] = false;
			for(i = 0; i < rowCount; i++){
				theRows[i].cells[id].firstChild.nodeValue = "";
			}
		}
	}
	else{
		id-=20;
		if(!rowSwitch[id]){
			rowSwitch[id] = true;
			for(i = 1; i < colCount+1; i++){
				if(colSwitch[i-1])
					theRows[id].cells[i].firstChild.nodeValue = theValues[id][i];
			}
		}
		else{
			rowSwitch[id] = false;
			for(i = 1; i < colCount+1; i++){
				theRows[id].cells[i].firstChild.nodeValue = "";
			}
		}
	}
}

function toggleAll(col, on){
	var inputs = document.getElementsByTagName("input");
	if(col){
		for(k=0; k<inputs.length; k++){
			if(inputs[k].className == "dtcol"){
				inputs[k].checked = on;
				if(colSwitch[+inputs[k].id] != on){
					toggle(+inputs[k].id + 1);
				}
			}
		}
	}
	else{
		for(k=0; k<inputs.length; k++){
			if(inputs[k].className == "dtrow"){
				inputs[k].checked = on;
				if(rowSwitch[+inputs[k].id - 20] != on){
					toggle(+inputs[k].id);
				}
			}
		}
	}
}

function fitCornerBg(){
    if(oldSize != theTable.offsetHeight){
        cornerBg.style.width = '1px';
        cornerBg.style.height = '1px';
        setTimeout("cornerBg.style.width = (cornerContainer.offsetWidth - 5) + 'px'; cornerBg.style.height = (cornerContainer.offsetHeight - 5) + 'px'; oldSize = theTable.offsetHeight;", 5);
    }
}

function test(){
    alert("Sorry, just testing.");
}