// JavaScript Document

function writeEditableTableRow(number) {
	var table_body = document.getElementById("shoppingCartTableBody");
	
	// Creates a table row
		var row = document.createElement("tr");
		row.setAttribute("id", "blindRow" + number);
		row.setAttribute("class", "editRow");
		
		// Creates the add column
		var duplicate = document.createElement("td");
		var duplicate_content = document.createElement('img');
		duplicate_content.setAttribute("src", "images/add_off.gif");
		duplicate_content.setAttribute("alt", "");
		duplicate.appendChild(duplicate_content);
		row.appendChild(duplicate);
		
		
		//Creates the submit column
		var edit = document.createElement("td");
		var edit_content = document.createElement('img');
		edit_content.setAttribute("id", "edit_but" + number);
		edit_content.setAttribute("src", "images/tick.gif");
		edit_content.setAttribute("alt", "Confirm changes");
		
		edit_content.className = "interactive";
		edit_content.onclick = function () {
			confirmEdit(number);
			//getQuote(document.getElementById("edit_form","edit"));
		};
		edit.appendChild(edit_content);
		row.appendChild(edit);
		
		
		//Creates the remove column
		var remove = document.createElement("td");
		var remove_content = document.createElement('img');
		remove_content.setAttribute("src", "images/subtract_off.gif");
		remove_content.setAttribute("alt", "");
		remove.appendChild(remove_content);
		row.appendChild(remove);
		
		for (var i = 0; i < 11; i++) {
			var cell = document.createElement("td");
			
			switch (i)
				{
					case 0:
					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 1:
					var blind_temp_colour = blind_array[i];
					colour_formatted = removeUnderscores(blind_temp_colour);
					var cellText = document.createTextNode(colour_formatted);
					break
					case 2:
					var cellText = document.createElement("select");
					var option_left = document.createElement("option");
					var option_right = document.createElement("option");
					cellText.name = "control_position_edit";
					option_left.value = "left";
					option_right.value = "right";
					var option_left_text = document.createTextNode("Left");
					var option_right_text = document.createTextNode("Right");
					option_left.appendChild(option_left_text);
					option_right.appendChild(option_right_text);
					if (blind_array[i] == "left") {
						cellText.appendChild(option_left);
						cellText.appendChild(option_right);
					} else {
						cellText.appendChild(option_right);
						cellText.appendChild(option_left);
					}
					break
					case 3:
					var cellText = document.createElement("input");
					cellText.value = blind_array[i];
					cellText.name = "control_drop_edit";
					break
					case 6:
					var cellText = document.createElement("select");
					var option_inside = document.createElement("option");
					var option_outside = document.createElement("option");
					cellText.name = "measurement_fit_edit";
					option_inside.value = "inside";
					option_outside.value = "outside";
					var option_inside_text = document.createTextNode("Inside");
					var option_outside_text = document.createTextNode("Outside");
					option_inside.appendChild(option_inside_text);
					option_outside.appendChild(option_outside_text);
					if (blind_array[i] == "inside") {
						cellText.appendChild(option_inside);
						cellText.appendChild(option_outside);
					} else {
						cellText.appendChild(option_outside);
						cellText.appendChild(option_inside);
					}
					break
					case 7:
					var cellText = document.createElement("input");
					cellText.value = blind_array[i];
					cellText.name = "edit_width";
					break
					case 8:
					var cellText = document.createElement("input");
					cellText.value = blind_array[i];
					cellText.name = "edit_drop";
					break
					case 10:
					price_formatted = addDollarSign(blind_array[i]);
					var cellText = document.createTextNode(price_formatted);
					break
					case 11:
					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);
}

function removeTableRows() {
	for (var i = 0; i < blind_stack.length; i++) {
		var table = document.getElementById("shoppingCartTable");
		var row = document.getElementById("blindRow" + i);
		table.getElementsByTagName("tbody")[0].removeChild(row);
	}
}

function editTableRow(number) {
	// REMOVE TABLE ROWS
	removeTableRows();
	
	//REWRITE THE TABLE ROW WITH EDIT FIELDS INSTEAD OF PLAIN TEXT WITH VALUES IN CORRECT PLACES & WITH A DARKER BG FOR THE TABLE ROW
	for (var i = 0; i < blind_stack.length; i++) {
		setGlobalBlindVars(i);
		if (i == number) {
			writeEditableTableRow(i);
		} else {
			writeQuoteTableRow(i,true);
		}
	}
}

function confirmEdit(number) {
	//setGlobalBlindVars(number);
	var error_div = document.getElementById("edit_blind_error");
	error_div.style.display = "none";
	killError(4);
	var form = document.getElementById("edit_form");
	var form_inputs = form.getElementsByTagName("input");
	var new_width = form_inputs[1].value;
	var new_drop = form_inputs[2].value;
	var user_error = "Blinds: ";
	if (new_width < 500) {
		user_error = user_error + "less wide than 500mm, ";
	}
	if (new_width > 3000) {
		user_error = user_error + " wider than 3000mm, ";
	}
	if (new_drop < 500) {
		user_error = user_error + "with a drop less than 500mm, ";
	}
	if (new_drop > 3000) {
		user_error = user_error + "with a greater drop than 3000mm, ";
	}
	if (user_error.length > 8) {
		user_error = user_error + "are a specialist size. Please contact our sales team on 0800 483 638, who will be able to offer you professional advice. ";
	} else {
		user_error = false;
	}
	if (user_error) {
		var error_div = document.getElementById("edit_blind_error");
		var error_div_p = document.createElement("p");
		var error_text = document.createTextNode(user_error);
		error_div_p.appendChild(error_text);
		error_div.appendChild(error_div_p);
		error_div.style.display = "block";
	} else {
		var round_width = roundUp(new_width,'width');
		var round_drop = roundUp(new_drop,'drop');		
		var the_quote = getPrice(round_width,round_drop);
		getStackedHeight(round_drop);
		
		if (the_quote != false) {
			var form_selects = form.getElementsByTagName("select");
			blind_control_position = form_selects[0].value; //form.control_position_edit.value;
			blind_control_drop = form_inputs[0].value; //form.control_drop_edit.value;
			blind_measurement_fit = form_selects[1].value; //form.measurement_fit_edit.value;
			blind_width = new_width;
			blind_drop = new_drop;
			
			setGlobalBlindArray();
			//blind_array = finalBlindRowValidation();
			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];
			insertToBlindStack(blind_array, number);
			// REWRITE THE TABLE
			removeTableRow();
			writeTotalPrice();
		} else {
			errorCheck(101,"This type of blind is not manufactured at this size. Please contact our sales team on 0800 483 638, who will be able to offer you professional advice.");
		}
	}
}