// JavaScript Document

function setGlobalBlindVars(stack_index) {
	var blind_to_set = blind_stack[stack_index];
	// Set Globals
	blind_type = blind_to_set[0];
	blind_colour = blind_to_set[1];
	blind_control_position = blind_to_set[2];
	blind_control_drop = blind_to_set[3];
	control_colour = blind_to_set[4];
	control_upgrade = blind_to_set[5];
	blind_measurement_fit = blind_to_set[6];
	blind_width = blind_to_set[7];
	blind_drop = blind_to_set[8];
	stacked_height = blind_to_set[9];
	blind_price = blind_to_set[10];
	
	setGlobalBlindArray();
}

function setGlobalBlindArray() {
	blind_array = [ blind_type , blind_colour , blind_control_position , blind_control_drop , control_colour , control_upgrade , blind_measurement_fit , blind_width , blind_drop , stacked_height , blind_price ];
}

// THESE FUNCTIONS ARE USED TO DIRECTLY AFFECT THE QUOTE TABLE IN THE DOM

function writeQuoteTableRow(row_number,active) { //writes a row based on the content of the global blind detail variables

	// Display the next step button
	var the_button = document.getElementById("confirm_button");
	the_button.style.display = "block";
	
	// get the reference for the div to place the table in
	var table_body = document.getElementById("shoppingCartTableBody");
	
	// creating all cells
	for (var j = 0; j < 1; j++) {
	
		// Creates a table row
		var row = document.createElement("tr");
		row.setAttribute("id", "blindRow" + row_number);
		
		// Creates the add column
		var duplicate = document.createElement("td");
		var duplicate_content = document.createElement('img');
		if (!active) {
			duplicate_content.setAttribute("id", "dup_but" + row_number);
			duplicate_content.setAttribute("src", "images/add.gif");
			duplicate_content.setAttribute("alt", "Duplicate this product");
		
			duplicate_content.className = "interactive";
			duplicate_content.onclick = function () {
				addTableRow(row_number);
			};
		} else {
			duplicate_content.setAttribute("src", "images/add_off.gif");
			duplicate_content.setAttribute("alt", "Button disabled");
		}
		duplicate.appendChild(duplicate_content);
		row.appendChild(duplicate);
		
		
		//Creates the edit column
		var edit = document.createElement("td");
		var edit_content = document.createElement('img');
		if (!active) {
			edit_content.setAttribute("id", "edit_but" + row_number);
			edit_content.setAttribute("src", "images/edit.gif");
			edit_content.setAttribute("alt", "Edit this product");
		
			edit_content.className = "interactive";
			edit_content.onclick = function () {
				editTableRow(row_number);
			};
		} else {
			edit_content.setAttribute("src", "images/edit_off.gif");
			edit_content.setAttribute("alt", "Button disabled");
		}
			
		edit.appendChild(edit_content);
		row.appendChild(edit);
		
		
		//Creates the remove column
		var remove = document.createElement("td");
		var remove_content = document.createElement('img');
		if (!active) {
			remove_content.setAttribute("id", "rem_but" + row_number);
			remove_content.setAttribute("src", "images/subtract.gif");
			remove_content.setAttribute("alt", "Remove this product");
		
			remove_content.className = "interactive";
			remove_content.onclick = function () {
				removeTableRow(row_number);
			};
		} else {
			remove_content.setAttribute("src", "images/subtract_off.gif");
			remove_content.setAttribute("alt", "Button disabled");
		}
		remove.appendChild(remove_content);
		row.appendChild(remove);
	
		for (var i = 0; i < 11; i++) {
			var cell = document.createElement("td");
			
			switch (i)
				{
					case 0:
					// COSMETICALLY REPLACE REG WITH MOTOR
					if (blind_array[5] == "motor"){
						var blind_temp_name = blind_array[i].replace("reg","motor");
					} else {
						var blind_temp_name = blind_array[i]
					}
  					type_formatted = removeUnderscores(blind_temp_name);
					var cellText = document.createTextNode(type_formatted);
					break
					case 2:
					if (blind_array[5] == "motor"){
						var cellText = document.createTextNode('Motor ' + blind_array[i]);
					} else {
						var cellText = document.createTextNode(blind_array[i]);
					}
					break
					case 3:
					if (blind_array[5] == "motor"){
						var cellText = document.createTextNode('N/a');
					} else {
						var cellText = document.createTextNode(blind_array[i]);
					}
					break
					case 1:
					var blind_temp_colour = blind_array[i];
					colour_formatted = removeUnderscores(blind_temp_colour);
					var cellText = document.createTextNode(colour_formatted);
					break
					case 10:
					// ADDING ANY UPGRADES TO PRICE
					if (blind_array[5] == 'chain') {
						blind_array[10] = blind_array[10] + chain_price;
					} else if (blind_array[5] == 'motor') {
						blind_array[10] = blind_array[10] + motor_price;
					}
					price_formatted = addDollarSign(blind_array[i]);
					var cellText = document.createTextNode(price_formatted);
					break
					default:
					var cellText = document.createTextNode(blind_array[i]);
					break
				} 
					
			cell.appendChild(cellText);
			row.appendChild(cell);
		}
	table_body.appendChild(row);
	}
	writeTotalPrice();
}

function addTableRow(row_number) { //This is to be run when the user duplicates a blind, its the function that should be triggered by the duplicate button
	
	setGlobalBlindVars(row_number);
	// Add the current array to the stack
	addToBlindStack(blind_array);
	//write it into the dom
	writeQuoteTableRow(blind_stack.length -1);
}

function removeTableRow(row_to_remove) { // this removes the entire table, and rewrites it based on the revised stack when the user deletes a row
	var max_rows = blind_stack.length;
	
	// removing all the table rows in order to rewrite them with the new stack order
	for (var i = 0; i < max_rows; i++) {
		var table = document.getElementById("shoppingCartTable");
		var row = document.getElementById("blindRow" + i);
		table.getElementsByTagName("tbody")[0].removeChild(row);
	}
	if (row_to_remove)	{
		removeFromBlindStack(row_to_remove); // STACK MANAGEMENT
	}
	var new_max_rows = blind_stack.length;
	
	// rewriting them from the new blind stack
	for (var i = 0; i < new_max_rows; i++) {
		blind_array = blind_stack[i];
		writeQuoteTableRow(i);
	}
	writeTotalPrice();
}

function calculatePriceAndDisplay() {
	var total_price = 0;
	for (var i = 0; i < blind_stack.length; i++) {
		var blind_row = blind_stack[i];
		price = blind_row[blind_row.length -1];
		total_price = total_price + price;
	}
	return total_price;
}

function writeTotalPrice() {
	var total_price_div = document.getElementById("generatedQuoteTitle");
	var total_price = calculatePriceAndDisplay();
	var total_string = "Total Blind Price: $" + total_price + ".00";
	if (total_price_div.getElementsByTagName("p").length > 0) {
		total_price_div.removeChild(total_price_div.getElementsByTagName('p')[0]);
	}	
	var total_price_p = document.createElement("p");
	var total_price_text = document.createTextNode(total_string);
	total_price_p.appendChild(total_price_text);
	total_price_div.appendChild(total_price_p);
}