/*

Copyright (c) 2005, Daniel Juliano
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this 
      list of conditions and the following disclaimer.
    * The names of contributors to this source code may not be used to endorse 
      or promote products derived from this software without specific prior 
      written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

/*

Script Summary:

When the user clicks 'Edit Fields', she/he is transported to a page that allows
updates to the types of information stored for each contact.  Editing the list
of available contact fields takes place separately from the normal scope of 
viewing, sorting, adding, and editing contacts, almost as if the user had hit
the 'pause' button on the application.  Once the user clicks 'Click Here to 
Return to Contacts', it is expected that she/he will be returned back to the
exact spot they were working before editing fields.

Because 'Edit Fields' has its own playground, so to speak, all of its 
functionality is handled separately, here.

*/

function fieldsDisplay() {
	if (fieldsVisible) {
		fieldsVisible = false;
		buttonDisplayActionPaint(currentAction);
		buttonMakeUtilVisible(currentUtil);
		buttonMakeTableVisible(currentTable);
		document.getElementById("action_fields").innerHTML = "Edit Fields";
		document.getElementById("util_choice_div").style.visibility = "visible";
		document.getElementById("fields_div").style.visibility = "hidden";
		
		// Re-initialize all utilities once columns have be rearranged.
		mainCompleteInitialization();
		
	} else {
		fieldsVisible = true;
		buttonDisplayActionPaint("action_fields");
		buttonMakeUtilVisible();
		buttonMakeTableVisible("form_fields_div");
		document.getElementById("action_fields").innerHTML = "Click Here to Return to Contacts";
		document.getElementById("util_choice_div").style.visibility = "hidden";
		document.getElementById("fields_div").style.visibility = "visible";
		
		fieldsPopulate();
		fieldsFillTable();
	}
	return true;
}

function fieldsPopulate() {
	
	// Because dynamically adding <option> tags to a <select> is a
	// royal p.i.t.a., we instead create the full <select> html here.
	
	document.getElementById("fields_add_submit").disabled = true;
	var html = "";

	// Build the edit list of titles.
	html += "<select id=\"fields_edit_list\" onChange=\"fieldsFillControls(this.value);\">\n";
	html += "	<option value=\"-1\">Select a field</option>\n";
	for (var i = 0; i < mainTitles.length; i++) {
		if (mainTitles[i] != "ID") {
			html += "	<option value=" + i + ">" + mainTitles[i] + "</option>\n";
		}
	}
	html += "</select>\n";
	document.getElementById("fields_edit_list_p").innerHTML = html;
	document.getElementById("fields_edit_submit").disabled = true;
	
	// Build the remove list of titles.
	html = "";
	html += "<select id=\"fields_remove_list\" onChange=\"document.getElementById('fields_remove_submit').disabled = false;\">\n";
	html += "	<option value=\"-1\">Select a field</option>\n";
	for (var i = 0; i < mainTitles.length; i++) {
		if (mainTitles[i] != "ID") {
			html += "	<option value=" + i + ">" + mainTitles[i] + "</option>\n";
		}
	}
	html += "</select>\n";
	document.getElementById("fields_remove_list_p").innerHTML = html;
	document.getElementById("fields_remove_submit").disabled = true;
	
	//  Build the add list indices.
	html = "";
	html += "Index:\n";
	html += "<select id=\"fields_add_index\" onChange=\"document.getElementById('fields_add_submit').disabled = false;\">\n";
	for (var i = 0; i < mainTitles.length; i++) {
		if (mainTitles[i] != "ID") {
			html += "	<option value=" + i + ">" + i + "</option>\n";
		}
	}
	html += "	<option value=" + mainTitles.length + " selected>" + mainTitles.length + "</option>\n";
	html += "</select>\n";
	document.getElementById("fields_add_index_p").innerHTML = html;
	
	//  Build the edit list indices.
	html = "";
	html += "Index:\n";
	html += "<select id=\"fields_edit_index\" onChange=\"document.getElementById('fields_edit_submit').disabled = false;\">\n";
	for (var i = 0; i < mainTitles.length; i++) {
		if (mainTitles[i] != "ID") {
			html += "	<option value=" + i + ">" + i + "</option>\n";
		}
	}
	html += "</select>\n";
	document.getElementById("fields_edit_index_p").innerHTML = html;
}

function fieldsFillTable() {
	var html = "";
	html += "<table id=\"fields_table\">\n";
	
	html += "<tr>";
	html += "<td>Index</td>";
	html += "<td>Name</td>";
	html += "<td>Type</td>";
	html += "</tr>\n";
	
	for (var i = 0; i < mainTitles.length; i++) {
		html += "<tr>";
		html += "<td>" + i + "</td>";
		html += "<td>" + mainTitles[i] + "</td>";
		html += "<td>Text</td>";
		html += "</tr>\n";
	}
	html += "</table>\n";
	document.getElementById("fields_div").innerHTML = html;
}

function fieldsFillControls(index) {
	if (index >= 0) {
		document.getElementById("fields_edit_name").value = mainTitles[index];
		document.getElementById("fields_edit_index").value = index;
		document.getElementById("fields_edit_submit").disabled = false;
	} else {
		document.getElementById("fields_edit_name").value = "";
		document.getElementById("fields_edit_index").value = "";
		document.getElementById("fields_edit_submit").disabled = true;
	}
}

function fieldsProcessEdit() {
	
	var index = parseInt(document.getElementById("fields_edit_list").value);
	
	var newName = document.getElementById("fields_edit_name").value;
	if (newName != mainTitles[index]) {
		mainTitles[index] = newName;
	}
	
	var newIndex = document.getElementById("fields_edit_index").value;
	if (index != newIndex) {
		// Switch places between titles.
		var buffer = "";
		buffer = mainTitles[newIndex];
		mainTitles[newIndex] = mainTitles[index];
		mainTitles[index] = buffer;
		
		// Switch places between actual contact information columns.
		buffer = null;
		for (var i = 0; i < mainContacts.length; i++) {
			buffer = mainContacts[i][newIndex];
			mainContacts[i][newIndex] = mainContacts[i][index];
			mainContacts[i][index] = buffer;
		}
	}
	
	fieldsProcessCompleted();
}

function fieldsProcessRemove() {
	
	var index = parseInt(document.getElementById("fields_remove_list").value);

	var message = "";
	message += "You are about to remove the " + mainTitles[index] + " column from the contacts database.\n";
	message += "This will remove all information for this column across all records.\n";
	message += "Are you sure you want to proceed with this change?";
	if (!confirm(message)) return;
	
	// Copy the contents of the titles array to a buffer, minus the 'remove' column.
	var buffer = new Array();
	var count = 0;
	for (var i = 0; i < mainTitles.length; i++) {
		if (i != index) {
			buffer [count++] = mainTitles[i];
		}
	}
	
	// Replace the titles array with the buffer.
	mainTitles = buffer;
	
	// Copy the contents of the contacts array to a buffer, minus the 'remove' column.
	buffer = new Array();
	for (var i = 0; i < mainContacts.length; i++) {
		count = 0;
		buffer[i] = new Array();
		for (var j = 0; j < mainContacts[i].length; j++) {	
			if (j != index) {
				buffer[i][count++] = mainContacts[i][j];
			}
		}
	}
	
	// Replace the contacts array with the buffer.
	mainContacts = buffer;
	
	fieldsProcessCompleted();
}

function fieldsProcessAdd() {
	
	var index = parseInt(document.getElementById("fields_add_index").value);
	var name = document.getElementById("fields_add_name").value;
	
	if (index == mainTitles.length) {
		// Add new column at the end of the arrays.
		mainTitles[index] = name;
		for (var i = 0; i < mainContacts.length; i++) {
			mainContacts[i][index] = "";
		}
		
	} else {
		// Otherwise, make space and push out the arrays by one index,
		// push array columns over, and insert the new column.
		for (var i = mainTitles.length; i > index; i--) {
			mainTitles[i] = mainTitles[i - 1];
		}
		mainTitles[index] = name;
		
		for (var i = 0; i < mainContacts.length; i++) {
			for (var j = mainContacts.length; j > index; j--) {
				mainContacts[i][j] = mainContacts[i][j - 1];
			}
			mainContacts[i][index] = "";
		}
	}
	
	fieldsProcessCompleted();
}
function fieldsProcessCompleted() {
	document.getElementById("fields_add_name").value = "";
	document.getElementById("fields_add_index").selectedIndex = 0;
	document.getElementById("fields_edit_name").value = "";
	document.getElementById("fields_edit_index").selectedIndex = 0;
	fieldsPopulate();
	fieldsFillTable();
}

// Only enable submit button once certain criteria have been met.
function fieldsValidateAdd() {

	var index = parseInt(document.getElementById("fields_add_index").value);
	var name = document.getElementById("fields_add_name").value;
	
	document.getElementById('fields_add_submit').disabled = true;
	if (index == null) return;
	if (index < 0) return;
	if (name == null) return;
	if (name == "") return;
	document.getElementById('fields_add_submit').disabled = false;
}