var english =[ [1,"One"]
				, [2,"Two"]
				, [3,"Three"]
				, [4,"Four"]
				, [5,"Five"]
				, [6,"Six"] ];
				
var french =[ [1,"Un"]
				, [3,"Trois"]
				, [4,"Quatre"]
				, [5,"Cinque"]
				, [6,"Six"]
				, [7,"Sept"]
				, [8,"Huit"] ];
var timers = new Array();

function join(type){
	clearOutput();
	var pictureDiv;
	var divs = document.getElementsByTagName("div");
	for(var i = 0; i < divs.length; i++){
		var element = divs[i];
		if (element.id == "joinsVinDiagram"){
			pictureDiv = element;
		}
	}
	
	var outputTable;
	var tables = document.getElementsByTagName("table");
	for(var i = 0; i < tables.length; i++){
		var element = tables[i];
		if (element.id == "result_output"){
			outputTable = element;
		}
	}
	var rows = outputTable.rows;
	var cells = rows[0].cells;
	var firstCell = cells[0];
	if(type == "crossjoin"){
		pictureDiv.innerHTML = '<img src="http://michaelmclaughlin.info/db1/wp-content/uploads/2010/01/CrossJoinVenn.jpg" />';
		firstCell.innerHTML = "RESULT SET: English CROSS JOIN French";
		crossJoin(french,english);
	}
	else if(type == "innerjoin"){
		pictureDiv.innerHTML = '<img src="http://michaelmclaughlin.info/db1/wp-content/uploads/2010/01/InnerJoinVenn.jpg" />';
		firstCell.innerHTML = "RESULT SET: English INNER JOIN French";
		innerJoin(french,english);
	}
	else if(type == "leftjoin"){
		pictureDiv.innerHTML = '<img src="http://michaelmclaughlin.info/db1/wp-content/uploads/2010/01/LeftJoinVenn.jpg" />';
		firstCell.innerHTML = "RESULT SET: English LEFT JOIN French";
		leftJoin(english,french);
	}
	else if(type == "rightjoin"){
		pictureDiv.innerHTML = '<img src="http://michaelmclaughlin.info/db1/wp-content/uploads/2010/01/RightJoinVenn.jpg" />';
		firstCell.innerHTML = "RESULT SET: English RIGHT JOIN French";
		rightJoin(english,french);
	}
	else if(type == "fulljoin"){
		pictureDiv.innerHTML = '<img src="http://michaelmclaughlin.info/db1/wp-content/uploads/2010/01/FullJoinVenn.jpg" />';
		firstCell.innerHTML = "RESULT SET: English FULL OUTER JOIN French";
		fullJoin(english,french);
	}
}

function crossJoin(array1,array2){
	var count = 1;
	for (var i = 0; i < array1.length; i++) {
		for (var j = 0; j < array2.length; j++) {
			timers.push(setTimeout('writeOutput(\"'+count+'\",\"'+array2[j][0]+'\",\"'+array2[j][1]+'\",\"'+array1[i][0]+'\",\"'+array1[i][1]+'\")',500 * (count)));
			count++;
		}
	}
}

function innerJoin(array1,array2){
	var count = 1;
	var em = document.getElementById("output");
	for (var i = 0; i < array1.length; i++) {
		for (var j = i; j < array2.length; j++) {
			if (array1[i][0] == array2[j][0]) {
				timers.push(setTimeout('writeOutput(\"'+count+'\",\"'+array2[j][0]+'\",\"'+array2[j][1]+'\",\"'+array1[i][0]+'\",\"'+array1[i][1]+'\")',500 * (count)));
				count++;
				break;
			}
		}
	}
}

function leftJoin(array1,array2){
	var count = 1;
	var matchNotFound = 1;
	var em = document.getElementById("output");
	for (var i = 0; i < array1.length; i++) {
		matchNotFound = 1;
		for (var j = 0; j < array2.length; j++) {
			if (array1[i][0] == array2[j][0]){
				matchNotFound = 0;
				timers.push(setTimeout('writeOutput(\"'+count+'\",\"'+array1[i][0]+'\",\"'+array1[i][1]+'\",\"'+array2[j][0]+'\",\"'+array2[j][1]+'\")',500 * (count)));
				count++;
				break;
			}
		}
		if(matchNotFound == 1){
				timers.push(setTimeout('writeOutput(\"'+count+'\",\"'+array1[i][0]+'\",\"'+array1[i][1]+'\",\"'+"NULL"+'\",\"'+"NULL"+'\")',500 * (count)));
				count++;
		}
	}
}

function rightJoin(array1,array2){
	var count = 1;
	var matchNotFound = 1;
	var em = document.getElementById("output");
	for (var i = 0; i < array2.length; i++) {
		matchNotFound = 1;
		for (var j = 0; j < array1.length; j++) {
			if (array1[j][0] == array2[i][0]){
				matchNotFound = 0;
				timers.push(setTimeout('writeOutput(\"'+count+'\",\"'+array1[j][0]+'\",\"'+array1[j][1]+'\",\"'+array2[i][0]+'\",\"'+array2[i][1]+'\")',500 * (count)));
				count++;
				break;
			}
		}
		if(matchNotFound == 1){
			timers.push(setTimeout('writeOutput(\"'+count+'\",\"'+"NULL"+'\",\"'+"NULL"+'\",\"'+array2[i][0]+'\",\"'+array2[i][1]+'\")',500 * (count)));
			count++;
		}
	}
}

function fullJoin(array1,array2){
	var c = 0;
	var matched = false;
	var result_set = new Array();
	var outer_set = new Array();
	// The outer loop s and inner loop the smaller array.
	for (var i = 0; i < array2.length; i++) {
		// Merged joins work with sorted sets, and pare previous values; the ($i + $c) skips interleaved non-matches.
		for (var j = i + c; j < array1.length; j++){
			// Perform a match for an intersection or inner join of two array sets.
			if (array1[j][0] == array2[i][0]) {
				// This selects the elements from the cross joined set, like a SELECT clause in SQL.
				var row = [array1[j][0],array1[j][1],array2[i][0],array2[i][1]];
				result_set.push(row);
				matched = true;
				break; 
			}
			else {
				var row = [array1[j][0],array1[j][1],"NULL","NULL"];
				outer_set.push(row);
				c++; 
			}
		}
		if (matched == false) {
			var row = ["NULL","NULL",array2[i][0],array2[i][1]];
			outer_set.push(row);
		}
		else {
			matched = false;
		}
    }      
 
	// Reads through all potential unmatched values.                            
	for (var i = 0;i < outer_set.length; i++){ 
		// Compares unmatched values to find unmatched values.
		for (var j = i; j < result_set.length; j++) {   
			if (result_set[j][0] != outer_set[i][0]) 
				result_set.push(outer_set[i]);
			// Discontinues search as there can only be one excluded from a join.
			break; 
		}
	}
	
  	//output result set
  	for(var i = 0; i < result_set.length; i++){
  		timers.push(setTimeout('writeOutput(\"'+(i+1)+'\",\"'+result_set[i][0]+'\",\"'+result_set[i][1]+'\",\"'+result_set[i][2]+'\",\"'+result_set[i][3]+'\")',500 * (i + 1)));
  	}
  }

  function writeOutput(cell1,cell2,cell3,cell4,cell5){	
    // Define a target variable for a dynamic table.
  	var outputTable;
  	// Declare a variable to hold the list of tables in the document.
  	var tables = document.getElementsByTagName("table");
  	// Read the list, and assign the table with target ID attribute.
  	for(var i = 0; i < tables.length; i++){
  		var element = tables[i];
  		if (element.id == "result_output"){
  			outputTable = element;
  		}
  	}
  	// Create a new row and populate it with the call parameters.
  	var newRow = outputTable.insertRow(outputTable.rows.length);
  	
  	// Add a cell to the row, assign values to cells, and set element class with a valid CSS value. 
  	var newCell = newRow.insertCell(0);
  	newCell.innerHTML = cell1+"";
  	newCell.className = "joins_index_column";
  	
  	// Add a cell to the row, assign values to cells, and conditionally set element class with a valid CSS value. 
  	var newCell = newRow.insertCell(1);
  	newCell.innerHTML = cell2;
  	if (newCell.innerHTML == "NULL") newCell.className = "joins_id_null"; 
  	else if (cell4 == "NULL") newCell.className = "left_join_result";
  	else if (cell2 == cell4) newCell.className = "inner_join_result";

    var newCell = newRow.insertCell(2);
  	newCell.innerHTML = cell3;
  	if (newCell.innerHTML == "NULL") newCell.className = "joins_data_null";
  	else if (cell4 == "NULL") newCell.className = "left_join_result";
  	else if (cell2 == cell4) newCell.className = "inner_join_result";
  	
  	var newCell = newRow.insertCell(3);
  	newCell.innerHTML = cell4;
  	if (newCell.innerHTML == "NULL") newCell.className = "joins_id_null"; 
  	else if (cell2 == "NULL") newCell.className = "right_join_result";
  	else if (cell2 == cell4) newCell.className = "inner_join_result";
  	
  	var newCell = newRow.insertCell(4);
  	newCell.innerHTML = cell5;
  	if (newCell.innerHTML == "NULL") newCell.className = "joins_data_null";
  	else if (cell2 == "NULL") newCell.className = "right_join_result";
  	else if (cell2 == cell4) newCell.className = "inner_join_result";
  }

function clearOutput() {
  // Clear the Venn Diagram.
	var pictureDiv;
	var divs = document.getElementsByTagName("div");
	for(var i = 0; i < divs.length; i++){
		var element = divs[i];
		if (element.id == "joinsVinDiagram"){
			pictureDiv = element;
		}
	}
	pictureDiv.innerHTML = '';

  // Clear timeout timers.
	for(var i = 0; i < timers.length; i++){
		clearTimeout(timers[i]);
	}
	timers = new Array();

  // Clear the output tables.	
	var outputTable;
	var tables = document.getElementsByTagName("table");
	for(var i = 0; i < tables.length; i++){
		var element = tables[i];
		if (element.id == "result_output"){
			outputTable = element;
		}
	}
	for (var i = outputTable.rows.length - 1; i > 1 ; i--){
		outputTable.deleteRow(i);
	}
	outputTable.rows[0].cells[0].innerHTML = "RESULT SET:";
	outputTable.rows[0].cells[0].style.height = "26px";
}

function fillEnglishTable(){
	var englishTable;
	var tables = document.getElementsByTagName("table");
	for(var i = 0; i < tables.length; i++){
		var element = tables[i];
		if (element.id == "english_input"){
			englishTable = element;
		}
	}
	for(var i = 0; i < english.length; i++){
		var newRow = englishTable.insertRow(englishTable.rows.length);
		var newCell = newRow.insertCell(0);
		newCell.innerHTML = english[i][0]+"";
		newCell = newRow.insertCell(1);
		newCell.innerHTML = english[i][1]+"";
	}
}

function fillFrenchTable(){
		var frenchTable;
		var tables = document.getElementsByTagName("table");
		for(var i = 0; i < tables.length; i++){
			var element = tables[i];
			if (element.id == "french_input"){
				frenchTable = element;
			}
		}
		for(var i = 0; i < french.length; i++){
			var newRow = frenchTable.insertRow(frenchTable.rows.length);
			var newCell = newRow.insertCell(0);
			newCell.innerHTML = french[i][0]+"";
			newCell = newRow.insertCell(1);
			newCell.innerHTML = french[i][1]+"";
		}
}

// Lab #6 Price mutation examples.
function getTable(idName){
	var table;
	var tables = document.getElementsByTagName("table");
	for(var i = 0; i < tables.length; i++){
		var element = tables[i];
		if (element.id == idName){
			table = element;
		}
	}
	return table;
}

function getButton(idName){
	var button;
	var buttons = document.getElementsByTagName("input");
	for(var i = 0; i < buttons.length; i++){
		var element = buttons[i];
		if (element.id == idName){
			button = element;
		}
	}
	return button;
}

function deactivate(){
	var table = getTable('price_insert_distribution');
	var rows = table.rows;
	rows[4].cells[2].innerHTML = '';
	rows[4].cells[6].innerHTML = '21';
	rows[5].cells[1].innerHTML = '';
	rows[5].cells[5].innerHTML = '21';
	rows[6].cells[1].innerHTML = '';
	rows[6].cells[5].innerHTML = '21';
	
	rows[7].cells[4].innerHTML = '24';
	rows[7].cells[6].innerHTML = '24';
	rows[8].cells[3].innerHTML = '24';
	rows[8].cells[5].innerHTML = '24';
	rows[9].cells[3].innerHTML = '24';
	rows[9].cells[5].innerHTML = '24';
	
	rows[10].cells[1].innerHTML = '';
	rows[10].cells[3].innerHTML = '72';
	
	var button = getButton('deactivate_button');
	button.disabled = true;
	
	button = getButton('insert_button');
	button.disabled = false;		
}

function activePrices(){
	var table = getTable('price_insert_distribution');
	var rows = table.rows;
	rows[4].cells[5].innerHTML = '24';
	rows[4].cells[6].innerHTML = '24';
	rows[5].cells[4].innerHTML = '24';
	rows[5].cells[5].innerHTML = '24';
	rows[6].cells[4].innerHTML = '24';
	rows[6].cells[5].innerHTML = '24';
	rows[10].cells[4].innerHTML = '72';
	rows[10].cells[5].innerHTML = '144';
	button = getButton('insert_button');
	button.disabled = true;
}

function clearChanges(){
	var table = getTable('price_insert_distribution');
	var rows = table.rows;
	rows[4].cells[2].innerHTML = '3';
	rows[4].cells[6].innerHTML = '24';
	rows[5].cells[1].innerHTML = '3';
	rows[5].cells[5].innerHTML = '24';
	rows[6].cells[1].innerHTML = '3';
	rows[6].cells[5].innerHTML = '24';

	rows[7].cells[4].innerHTML = '21';
	rows[7].cells[6].innerHTML = '21';
	rows[8].cells[3].innerHTML = '21';
	rows[8].cells[5].innerHTML = '21';
	rows[9].cells[3].innerHTML = '21';
	rows[9].cells[5].innerHTML = '21';
	
	rows[10].cells[1].innerHTML = '9';
	rows[10].cells[3].innerHTML = '63';
	
	rows[4].cells[5].innerHTML = '21';
	rows[5].cells[4].innerHTML = '21';
	rows[6].cells[4].innerHTML = '21';
	rows[10].cells[4].innerHTML = '63';
	rows[10].cells[5].innerHTML = '135';
	var button = getButton('deactivate_button');
	button.disabled = false;
	
	button = getButton('insert_button');
	button.disabled = true;		
}
