/*
 * CList.js
 * $Revision: 1.1 $ $Date: 2003/06/12 18:35:06 $
 */

/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Netscape code.
 *
 * The Initial Developer of the Original Code is
 * Netscape Corporation.
 * Portions created by the Initial Developer are Copyright (C) 2003
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s): Bob Clary <bclary@netscape.com>
 *
 * ***** END LICENSE BLOCK ***** */

function CList(/* Array */ aArray)
{
  this.mArray = aArray || [];
}

CList.prototype.getLength = 
function()
{
  return this.mArray.length;
};

CList.prototype.getAt =
function(/* Number */ aIndex)
{
  if (aIndex < 0 || aIndex >= this.mArray.length)
  {
    return undefined;
  }

  return this.mArray[aIndex];
};

CList.prototype.removeAll = 
function()
{
  this.mArray = [];
};

CList.prototype.removeAt =
function (/* Number */ aIndex)
{
  var length = this.mArray.length;
  if (length  == 0)
  {
    return;
  }

  switch(aIndex)
  {
  case -1:
    break;
  case 0:
    this.mArray.shift();
    break;
  case length - 1:
    this.mArray.pop();
    break;
  default:
    var head = this.mArray.slice(0, aIndex);
    var tail = this.mArray.slice(aIndex+1);
    this.mArray = head.concat(tail);
    break;
  }
};

CList.prototype.insertAt =
function (/* Object */ aObject, /* Number */ aIndex)
{
  switch(aIndex)
  {
  case -1:
    break;
  case 0:
    this.mArray.unshift();
    break;
  case length:
    this.mArray.push();
    break;
  default:
    var head = this.mArray.slice(0, aIndex - 1);
    var tail = this.mArray.slice(aIndex);
    this.mArray = head.concat([aObject]);
    this.mArray = this.mArray.concat(tail);
    break;
  }
};

CList.prototype.findIndexOf = 
function(/* Object */ aObject)
{
  var length = this.mArray.length;
  for (var i = 0; i < length; ++i)
  {
    if (this.mArray[i] == aObject)
    {
      return i;
    }
  }
  return -1;
};

CList.prototype.addUnique =
function (/* Object */ aObject)
{
  var i = this.findIndexOf(aObject);
  if (i == -1)
  {
    this.mArray[this.mArray.length] = aObject;
  }
};

CList.prototype.removeUnique =
function (/* Object */ aObject)
{
  var length = this.mArray.length;
  if (length  == 0)
  {
    return;
  }
  var i = this.findIndexOf(aObject);

  this.removeAt(i);
};


function GenerateForm(tableid)
	{
	var table = document.getElementById(tableid);
	var rows = table.getElementsByTagName("tr");
    	var matrixformtext = '<form method="POST" action=""><p>';
	var matrixformselection = '';

	rowone = rows[0].childNodes
	for (var k=0;k<(rowone.length);k++)
		{ matrixformselection = matrixformselection + '<option value=\'' + rowone[k].innerText + '\'>' + rowone[k].innerText +'</option>'; }

	
	var selectionRow  = '<select size="1" id="formrowname">' + matrixformselection + '</select>Row&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
	var selectionCol  = '<select size="1" id="formcolname">' + matrixformselection + '</select>Column&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
	var CtinCell = '<option value="Ct" selected>Count</option>'	
	var xinCell = '<option value="X">X in Cell</option>'
	var selectionCell  = '<select size="1" id="formcellname">' + CtinCell + xinCell + matrixformselection + '</select>Cell&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';

	matrixformtext = matrixformtext + selectionRow + selectionCol + selectionCell

	matrixformtext = matrixformtext +  '<input type="button" value="Matrix" onclick="submitMatrix(\'' + tableid + '\')" ><input type="reset" value="Reset"></p></form>';

	var matrixform = document.getElementById('matrix');

	if (matrixform !=null)
		{
		if (matrixform.innerText.toString() =='')
			{
			matrixform.innerHTML = matrixformtext;
			matrixform.style.display = 'none'
			}
		}

	if (matrixform.style.display == '' )
		{ matrixform.style.display = 'none';}
	else
		{ matrixform.style.display = '';  }

	}


function GenerateMatrix(tableid, rowname, colname, cellname)
	{
	var table = document.getElementById(tableid);
	var rows = table.getElementsByTagName("tr");
	var listRows = new CList();
	var listCols = new CList();
	var matrixTable = '';
	listRows.addUnique("<br>")
	listCols.addUnique("<br>")
	var row0col0 = "";
	var rowone, rowpos, colpos, cellpos, celltext;
	row0col0 = rowname +' / ' +  colname ;
	rowone = rows[0].childNodes


	//alert (rowname +  colname +  cellname);
	// Find the position of row, col and cell
	for (var k=0;k<(rowone.length);k++)
		{
		if (rowone[k].innerText == rowname) rowpos = k
		if (rowone[k].innerText == colname) colpos = k
	
	if (cellname != 'X' || cellname != 'Ct') 
			{if (rowone[k].innerText == cellname) cellpos = k }
		}
	//alert (rowpos);
	//alert (colpos);

	if (rows.length>1 )
		{
		c= rows[0].childNodes;
		totalcols=c.length;
		}
	// Populate col items from the second col of the table
	for (i=1; i<rows.length;i++)
		{
		c=rows[i].childNodes;

		for (var k=0;k<(c.length);k++)
			{
			if (k == rowpos)
				{
				listRows.addUnique(c[k].innerHTML);
				}
			if (k == colpos)
				{
				listCols.addUnique(c[k].innerHTML);
				}
			}
		}

	var rowLength = listRows.getLength();
	var colLength = listCols.getLength();

	// Build two dimension array
	var Matrix = new Array(rowLength)
	for (x = 0; x <= rowLength; x++) {Matrix [x] = new Array(colLength)}

	// Load Matrix Col and Row name
	for (r = 0; r < rowLength; r++) {
		for (c = 0; c < colLength; c++) {
		  Matrix [r] [c] = "";
		  if ((c == 0 ) && (r == 0)) Matrix[r] [c] = row0col0;
		  if ((r == 0 ) && (c != 0)) Matrix[r] [c] =  listCols.getAt(c);
		  if ((c == 0 ) && (r != 0)) Matrix[r] [c] =  listRows.getAt(r);
		}
	}

	// Load Matrix Cell
	
	for (i=1; i<rows.length;i++)
		{
		c=rows[i].childNodes;
		for (var k=0;k<(c.length);k++)
		{
			if (k == rowpos) var RowPos = listRows.findIndexOf(c[k].innerHTML);
			if (k == colpos) var ColPos = listCols.findIndexOf(c[k].innerHTML);
			if (k == cellpos) celltext = c[k].innerHTML;
		}
		
		//Matrix [RowPos] [ColPos] = '0';
	
		switch (cellname) {
			case 'X': Matrix [RowPos] [ColPos] = cellname; break;
			
			case 'Ct': 
				var iCount = Number(Matrix [RowPos] [ColPos])
				iCount = iCount + 1;
				Matrix [RowPos] [ColPos] = iCount.toString(); 
				break;

			default: Matrix [RowPos] [ColPos]  = celltext;
		}
	}

	// Build Matrix Result Table
	 matrixTable = matrixTable + "<table border='1'>";
	 for (r = 0; r < rowLength; r++) {
	 	matrixTable = matrixTable + "<tr>";
	    for (c = 0; c < colLength; c++) {
		if (Matrix [r] [c] != '')
	     		{ matrixTable = matrixTable + "<td>" + Matrix [r] [c] + "</td>"; }
		else 
			{ matrixTable = matrixTable + "<td><br></td>";}
	    }
	    matrixTable = matrixTable + "</tr>";

	 }
	    matrixTable = matrixTable + "</table>";
	    printHtml (row0col0, matrixTable);
	}


function printHtml(matrixName, matrixTable)
{
	var matrixcontent = new Array(2);
	matrixcontent[0] = matrixName;
	matrixcontent[1] = matrixTable;
	var generator=window.showModalDialog('../../reports/scripts/util/popmatrix.htm',matrixcontent, 'dialogheight:400px;dialogwidth:500px;scroll:yes;resizable:yes;status:yes');

	//var generator=window.open('','matrixName','height=400,width=500, scrollbars=yes, resizable=yes');
}

function submitMatrix(tableid)
{
	//alert (tableid);
	//alert (document.getElementById("formrowname").options[document.getElementById("formrowname").selectedIndex].value);
	var rowname = document.getElementById("formrowname").options[document.getElementById("formrowname").selectedIndex].value;
	var colname = document.getElementById("formcolname").options[document.getElementById("formcolname").selectedIndex].value;
	var cellname = document.getElementById("formcellname").options[document.getElementById("formcellname").selectedIndex].value;
	//alert (rowname + colname + cellname);
	GenerateMatrix(tableid, rowname ,colname , cellname)
}