/*********************************************
 * Methods for dealing with select elements
 *********************************************/

function getSelectedValue(selectId, controlDocument)
{
	controlDocument = controlDocument == null ? document : controlDocument;
	
	if(controlDocument.getElementById(selectId) == null)
	{
		alert("ERROR:" + selectId + " does not exist");
		return;
	}

	selectedIndex = controlDocument.getElementById(selectId).selectedIndex;
	selectedValue = "";
	if(selectedIndex >= 0)
	{
		selectedValue= controlDocument.getElementById(selectId).options[ controlDocument.getElementById(selectId).selectedIndex ].value;
	}
	return selectedValue;

}

function getSelectedText(selectId, controlDocument)
{
	controlDocument = controlDocument == null ? document : controlDocument;
	
	selectedIndex = controlDocument.getElementById(selectId).selectedIndex;
	selectedText = "";
	if(selectedIndex >= 0)
	{
		selectedText= controlDocument.getElementById(selectId).options[ controlDocument.getElementById(selectId).selectedIndex ].text;
	}
	return selectedText;

}
function setSelectedValue(selectId, selection, controlDocument)
{
	controlDocument = controlDocument == null ? document : controlDocument;
	
	selectElement = controlDocument.getElementById(selectId);
	selectionFound = false;
	for(optionIndex = 0; optionIndex < selectElement.options.length && (!selectionFound); optionIndex++)
	{
		selectionFound = (selectElement.options[optionIndex].value == selection);
		if(selectionFound)
		{
			selectElement.selectedIndex = optionIndex;
		}
	}
	if(!selectionFound && selectElement.options.length > 0 && selectElement.selectedIndex < 0)
	{
		selectElement.selectedIndex = 0;
	}
}



function setSelectedText(selectId, selection, controlDocument)
{
	controlDocument = controlDocument == null ? document : controlDocument;
	
	selectElement = controlDocument.getElementById(selectId);
	selectionFound = false;
	for(optionIndex = 0; optionIndex < selectElement.options.length && (!selectionFound); optionIndex++)
	{
		selectionFound = (selectElement.options[optionIndex].text == selection);
		if(selectionFound)
		{
			selectElement.selectedIndex = optionIndex;
		}
	}
	if(!selectionFound && selectElement.options.length > 0 && selectElement.selectedIndex < 0)
	{
		selectElement.selectedIndex = 0;
	}
}

function addOptionToSelectElement(selectId, optionText, optionValue, before, controlDocument)
{
	controlDocument = controlDocument == null ? document : controlDocument;

	option = controlDocument.createElement("option");
	option.text=optionText;
	option.value=optionValue;
	
	//FUCK M$ IE, FUCK IT UP THE ASS WITH A BASEBALL BAT.  A BIG WOODEN ONE. WITH SPLINTERS.
	try
	{
		controlDocument.getElementById(selectId).add(option, before);
	}
	catch(e)
	{
		if(before == null)
		{
			controlDocument.getElementById(selectId).add(option);
		}
		else
		{
			controlDocument.getElementById(selectId).add(option, before.index);	
		}
	}
}
function removeOptionFromSelectElement(selectId, optionText, controlDocument)
{
	controlDocument = controlDocument == null ? document : controlDocument;
	
	selectElement = controlDocument.getElementById(selectId);
	selectionFound = false;
	for(optionIndex = 0; optionIndex < selectElement.options.length && (!selectionFound); optionIndex++)
	{
		selectionFound = (selectElement.options[optionIndex].text == optionText);
		if(selectionFound)
		{
			selectElement.remove(optionIndex);
		}
	}
}

function removeAllOptionsFromSelectElement(selectElement)
{
	while(selectElement.length > 0)
	{
		try { selectElement.remove(0); } catch(e){}
	}
}

function setAllowableSelections(selectId, allowableValues, allowableNames, controlDocument)
{
	if(controlDocument == null) { controlDocument = document; }

	if(allowableNames != null && allowableValues != null)
	{
		currentSelection=getSelectedValue(selectId, controlDocument);
		removeAllOptionsFromSelectElement(controlDocument.getElementById(selectId));
		for(addIndex=0; addIndex < allowableValues.length; addIndex++)
		{
			addOptionToSelectElement(selectId, allowableNames[addIndex], allowableValues[addIndex], null, controlDocument);
		}
		setSelectedValue(selectId, currentSelection, controlDocument); //restore original settings if still valid
	}
}


/*********************************************
 * Methods for creating / modifying tables
 *********************************************/

function createTable(columnNames, rowData, tableId, rowsAreRemovable, rowsAreMovable, rowRemoveCallback, rowMoveCallback, controlDocument)
{
	controlDocument = controlDocument == null ? document : controlDocument;
	
	var newTable = controlDocument.createElement('table');
	var tableBody = controlDocument.createElement('tbody');
	newTable.appendChild(tableBody);
	newTable.id=tableId;

	row = controlDocument.createElement('tr');
	row.className='header';
	tableBody.appendChild(row);
		
	for (columnIndex in columnNames)
	{
		header = controlDocument.createElement('th');
		if(columnIndex == 0)
		{
			header.className = "file_header";
		}
		if(columnIndex == 1)
		{
			header.className = "md5_header";
		}
		headerContent = controlDocument.createTextNode(columnNames[columnIndex]);
		header.appendChild(headerContent);
		row.appendChild(header);
	}
	if(rowsAreRemovable)
	{
		header = controlDocument.createElement('th');
		headerContent = controlDocument.createTextNode('');
		header.appendChild(headerContent);
		row.appendChild(header);
	}
	if(rowsAreMovable)
	{
		for(i=1; i<2; i++)
		{
			header = controlDocument.createElement('th');
			headerContent = controlDocument.createTextNode('');
			header.appendChild(headerContent);
			row.appendChild(header);
		}
	}
	
	if(rowData != null)
	{
		for (rowIndex in rowData)
		{
			addTableRow(newTable, rowData[rowIndex], rowsAreRemovable, rowsAreMovable, rowRemoveCallback, rowMoveCallback);
		}
	}

	return newTable;
}


function addTableRow(table, rowData, rowsAreRemovable, rowsAreMovable, rowRemoveCallback, rowMoveCallback, controlDocument)
{
	controlDocument = controlDocument == null ? document : controlDocument;
	
	rowRemoveCallback = rowRemoveCallback == null ? function(){} : rowRemoveCallback;
	rowMoveCallback = rowMoveCallback == null ? function(){} : rowMoveCallback;


	row = controlDocument.createElement('tr');
	tableBody=table.firstChild;
	numRows= tableBody.rows.length;
	tableBody.appendChild(row);

	cellIndex = 1;
	while(cellIndex <= rowData.length)
	{
		cell = controlDocument.createElement('td');
		if(typeof(rowData[cellIndex-1]) == 'string')
		{
			cellContent = controlDocument.createTextNode(rowData[cellIndex-1]);
			cell.appendChild(cellContent);
		}
		else
		{
			cell.appendChild(rowData[cellIndex-1]);
		}
		if(cellIndex == 1)
		{
			cell.className = "file";
		}
		if(cellIndex == 2)
		{
			cell.className = "md5";
		}
		//cell.className=table.id + '_column_' + cellIndex;

		row.appendChild(cell);
		cellIndex++;
	}
	if(rowsAreRemovable)
	{
		cellContent = createInput("button", controlDocument);
		cellContent.value = 'Remove';
		cellContent.className="default_button";
		cellContent.onclick= function() { row = this.parentNode.parentNode; table=row.parentNode.parentNode; removeThisCellsRow(this); rowRemoveCallback(table,row); }; 
		cell = controlDocument.createElement('td');
		cell.className=table.id + '_column_' + cellIndex;
		cell.appendChild(cellContent);
		row.appendChild(cell);
		cellIndex++;


	}
	if(rowsAreMovable)
	{
		cellContent = createInput("button", controlDocument);
		cellContent.value = String.fromCharCode(8593);
		cellContent.className="default_button";
		cellContent.onclick= function() { moveThisCellsRowUp(this); rowMoveCallback(this, "up"); };
		cell = controlDocument.createElement('td');
		cell.className=table.id + '_column_' + cellIndex;
		cell.appendChild(cellContent);
		row.appendChild(cell);
		cellIndex++;

		cellContent = createInput("button", controlDocument);
		cellContent.value = String.fromCharCode(8595);
		cellContent.className="default_button";
		cellContent.onclick= function() { moveThisCellsRowDown(this); rowMoveCallback(this, "down"); }; 
		cell = controlDocument.createElement('td');
		cell.className=table.id + '_column_' + cellIndex;
		cell.appendChild(cellContent);
		row.appendChild(cell);
		cellIndex++;

	}
	row.className = numRows % 2 == 0 ? 'even' : 'odd';

}


function removeThisCellsRow(button)
{
	row=button.parentNode.parentNode;
	tableBody= row.parentNode;
	tableBody.removeChild(row);

	setRowClasses(tableBody.parentNode, true);
}
function moveThisCellsRowUp(button)
{
	row=button.parentNode.parentNode;
	tableBody=row.parentNode;
	
	allRows =tableBody.childNodes;
	rowIndex = 1;
	while(rowIndex < allRows.length && allRows[rowIndex] != row ) { rowIndex++; }
	if(rowIndex > 1)
	{
		tmp = allRows[rowIndex];
		tableBody.removeChild(allRows[rowIndex]);
		tableBody.insertBefore(tmp, allRows[rowIndex-1]);
	}
	setRowClasses(tableBody.parentNode, true);

}
function moveThisCellsRowDown(button)
{
	row=button.parentNode.parentNode;
	tableBody=row.parentNode;
	
	allRows =tableBody.childNodes;
	rowIndex = 1;
	while(rowIndex < allRows.length && allRows[rowIndex] != row ) { rowIndex++; }
	if(rowIndex < allRows.length-1 && allRows.length > 2 )
	{
		tmp = allRows[rowIndex+1];
		tableBody.removeChild(allRows[rowIndex+1]);
		tableBody.insertBefore(tmp, allRows[rowIndex]);
	}
	setRowClasses(tableBody.parentNode, true);


}



function getTableDataArray(table, rowsAreRemovable, rowsAreMovable)
{
			
	numEmptyCells = (rowsAreRemovable ? 1 : 0) + (rowsAreMovable ? 2 : 0);
	
	data = new Array();
	rows = table.rows;
	rowIndex = 0;
	while (rowIndex < rows.length)
	{
		row= rows[rowIndex];
		if(row.firstChild.tagName.toLowerCase() == 'td')
		{
			rowData = new Array();
			cells = row.childNodes;

			numCells= row.childNodes.length- numEmptyCells;
			cellNum = 0;
			while(cellNum < numCells)
			{
				cellContent = cells[cellNum].firstChild;
				if(typeof(cellContent.data) == 'string')
				{
					rowData.push( cells[cellNum].firstChild.data);
				}
				else
				{
					rowData.push(cellContent);
				}
				cellNum++;
			}
			data.push(rowData);
		}
		rowIndex++;
	}
	return data;
}


function setRowClasses(table, enabled)
{
	rows = table.rows;
	rows[0].className = enabled==true ? 'header_row' : 'disabled_header_row';

	rowIndex = 1;
	while(rowIndex < rows.length)
	{
		if(enabled==true)
		{
			rows[rowIndex].className = rowIndex % 2 == 0 ? 'even' : 'odd';
		}
		else
		{
			rows[rowIndex].className = rowIndex % 2 == 0 ? 'disabled_even' : 'disabled_odd';
		}
		
		cells = rows[rowIndex].childNodes;
		for(cellIndex = 0; cellIndex < cells.length; cellIndex++)
		{
			cellContent=cells[cellIndex].firstChild;
			if(cellContent.type == "button" )
			{
				cellContent.disabled = (enabled == false);
				cellContent.className = (enabled == false) ? "default_button_disabled" : "default_button";
			}
		}		
		rowIndex++;
	}
}


/**********************************
 * Download page specific code
 *********************************/
var hrefToName = [];
function updateSelection()
{
	var firmwareName = "Firmware Images";
	var downloadTypeOrder = downloadData["DATA_ORDER"];
	setAllowableSelections("download_type", downloadTypeOrder, downloadTypeOrder);
	var type = getSelectedValue("download_type");
	var fileData;
	if(type == firmwareName)
	{
		document.getElementById("firmware_controls").style.display = "block";
		
		var archData = downloadData[firmwareName];
		var archOrder = archData["DATA_ORDER"];
		setAllowableSelections("arch", archOrder, archOrder);
		var arch = getSelectedValue("arch");
		
		var branchData = archData[arch];
		var branchOrder = branchData["DATA_ORDER"];
		setAllowableSelections("branch", branchOrder, branchOrder);
		var branch = getSelectedValue("branch");

		var versionData = branchData[branch];
		var versionOrder = versionData["DATA_ORDER"];
		setAllowableSelections("version", versionOrder, versionOrder);
		var version = getSelectedValue("version");

		fileData = versionData[version];

	}
	else
	{
		document.getElementById("firmware_controls").style.display = "none";
		fileData = downloadData[type];
	}
	
	//createTable(columnNames, rowData, tableId, rowsAreRemovable, rowsAreMovable, rowRemoveCallback, rowMoveCallback, controlDocument)

	hrefToName = [];
	var linkTableData = [];
	var fileOrder = fileData["DATA_ORDER"];
	var fileIndex;
	for(fileIndex=0; fileIndex < fileOrder.length; fileIndex++)
	{
		var fileName = fileOrder[fileIndex];
		var fileInfo = fileData[fileName];
		var link = document.createElement("a");
		
		link.className = "file_link";
		link.href= fileInfo[0];
		hrefToName[link.href] = fileName;
		link.onclick=function() { var name= hrefToName[this.href] ; pageTracker._trackPageview(name); }
		link.appendChild( document.createTextNode(fileName) );
		linkTableData.push( [link, fileInfo[1]] );
	}
	var linkTable = createTable(["File", "MD5 Sum"], linkTableData, "file", false, false);
		
	
	var container = document.getElementById("file_list");
	while(container.childNodes.length > 0)
	{
		container.removeChild(container.firstChild);
	}
	container.appendChild(linkTable);
}



