/*

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 a button is clicked, not only should an action be triggered, but other buttons
may need to be darkened or made invisible, depending on context.  Such interactions
are all grouped in the methods below.

One exception - Edit Fields is a special case, and is handled in the jax-fields.js
script.

Example:

When 'Sort' is clicked, buttonMakeUtilVisible() will be called, and both 'List' 
and 'Search' buttons will change to a darker color, depending on the css behind
css class "util_choice_span".

*/

var currentIndex = -1;
var currentUtil = null;
var currentTable = null;
var currentAction;
var fieldsVisible = false;

function buttonMakeUtilVisible(util) {
	
	// Only highlight the just-clicked button.
	document.getElementById("util_choice_sort").className = "util_choice_span";
	document.getElementById("util_choice_search").className = "util_choice_span";
	document.getElementById("util_choice_list").className = "util_choice_span";
	
	if (util != null) {
		document.getElementById(util).className = "util_choice_span_highlight";
	}

	var visible = Array("hidden", "hidden", "hidden");

	if (util == "util_choice_sort") visible[0] = "visible";
	if (util == "util_choice_search") visible[1] = "visible";
	if (util == "util_choice_list") visible[2] = "visible";

	document.getElementById("sort_div").style.visibility = visible[0];
	document.getElementById("search_div").style.visibility = visible[1];
	document.getElementById("list_div").style.visibility = visible[2];
	
	if (util == "util_choice_sort") sortDisplay();
	if (util == "util_choice_search") searchDisplay();
	if (util == "util_choice_list") listDisplay();

	if (util != null) currentUtil = util;
}

function buttonDisplayAction(action, element) {
	
	// Check for multiple click - causes div to disappear.
	if (currentAction != null && element == null && action == currentAction) {
		return false;
	}
	
	if (action == "action_add") {
		if (buttonRecordAdd()) return buttonDisplayActionPaint(action);
	} else if (action == "action_view") {
		if (buttonRecordView(element)) return buttonDisplayActionPaint(action);
	} else if (action == "action_edit") {
		if (buttonRecordEdit()) return buttonDisplayActionPaint(action);
	} else if (action == "action_delete") {
		if (buttonRecordDelete()) return buttonDisplayActionPaint(action);
	} else if (action == "action_fields") {
		if (buttonFields()) return buttonDisplayActionPaint(action);
	}
}
function buttonDisplayActionPaint(action) {
	
	if (action == null) action = "";
	
	// Only highlight the just-clicked button.
	document.getElementById("action_add").className = "action_span";
	document.getElementById("action_view").className = "action_span";
	document.getElementById("action_edit").className = "action_span";
	document.getElementById("action_delete").className = "action_span";
	document.getElementById("action_fields").className = "action_span";
	if (action != "" && action != "action_delete") {
		document.getElementById(action).className = "action_span_highlight";
	}
	
	if (action == "action_view" || action == "action_edit") {
		document.getElementById("action_add").style.display = "";
		document.getElementById("action_view").style.display = "";
		document.getElementById("action_edit").style.display = "";
		document.getElementById("action_delete").style.display = "";

	} else if (action == "action_view") {
		document.getElementById("action_add").style.display = "";
		document.getElementById("action_view").style.display = "";
		document.getElementById("action_edit").style.display = "";
		document.getElementById("action_delete").style.display = "";
		
	} else if (action == "action_fields") {
		var block = "";
		if (fieldsVisible) block = "none";
		document.getElementById("action_add").style.display = block;
		document.getElementById("action_view").style.display = block;
		document.getElementById("action_edit").style.display = block;
		document.getElementById("action_delete").style.display = block;

	} else {
		document.getElementById("action_add").style.display = "";
		document.getElementById("action_view").style.display = "none";
		document.getElementById("action_edit").style.display = "none";
		document.getElementById("action_delete").style.display = "none";
	}
	
	if (action != null && action != "action_fields") currentAction = action;
}

function buttonRecordAdd() {
	currentIndex = -1;
	sortPaintTable();
	buttonMakeTableVisible("form_add_div");
	return true;
}

function buttonRecordView(element) {
	if (element) {
		if (currentUtil == "util_choice_sort") {
			currentIndex = mainParseFormatted(element.id.split("_")[1]) + (sortTableHeight * sortPage);
		} else {
			currentIndex = mainParseFormatted(element.id.split("_")[1]);
		}
	}
	if (currentIndex > (mainContacts.length - 1) || currentIndex < 0) return false;
	
	for (var i = 0; i < mainTitles.length; i++) {
		document.getElementById("form_view_" + i).innerHTML = mainContacts[currentIndex][i];
	}
	buttonMakeTableVisible("form_view_div");
	
	return true;
}

function buttonRecordEdit() {
	if (currentIndex == -1 || currentIndex > mainContacts.length) return false;
	for (var i = 0; i < mainTitles.length; i++) {
		document.getElementById("form_edit_" + i).value = mainContacts[currentIndex][i];
	}
	buttonMakeTableVisible("form_edit_div");
	return true;
}

function buttonRecordDelete() {
	if (currentIndex <= -1) return false;
	
	// Remove child from array, reset current record pointer.
	mainContacts.splice(currentIndex, 1);
	currentIndex = -1;
	
	// Repaint the table.
	buttonMakeUtilVisible(currentUtil);
	buttonMakeTableVisible();
	return true;
}

function buttonMakeTableVisible(tableName) {
	document.getElementById("form_view_div").style.visibility = "hidden";
	document.getElementById("form_edit_div").style.visibility = "hidden";
	document.getElementById("form_add_div").style.visibility = "hidden";
	document.getElementById("form_fields_div").style.visibility = "hidden";
	
	if (tableName) {
		document.getElementById(tableName).style.visibility = "visible";
		
		if (tableName == "form_edit_div") {
			var element = document.getElementById("form_edit_div");
			if (element.getElementsByTagName("INPUT")[1] != null) {
				element.getElementsByTagName("INPUT")[1].focus();
				element.getElementsByTagName("INPUT")[1].select();
			}
		} else if (tableName == "form_add_div") {
			var element = document.getElementById("form_add_div");
			if (element.getElementsByTagName("INPUT")[0] != null) {
				element.getElementsByTagName("INPUT")[0].focus();
			}
		}
	}
	if (tableName != null && tableName != "form_fields_div") currentTable = tableName;
}


// 'Form Actions' take place when the user clicks 'Update List'
// at the bottom of the add or edit forms.
function buttonFormAction(action) {
	if (action == null) action = "add";
	if (action == "add") {
		buttonAddRecord();
	} else {
		buttonEditRecord();
	}
}
// User clicked 'Update List' after filling out the Add Contact form.
function buttonAddRecord() {
	
	// Find the greatest ID, increment by one, and zero pad it.
	var index = -1;
	for (var i = 0; i < mainTitles.length; i++) {
		if (mainTitles[i].toUpperCase() == "ID") {
			index = i;
			break;
		}
	}
	var greatest = 0;
	var test = "";
	for (var i = 0; i < mainContacts.length; i++) {
		test = mainContacts[i][index];
		test = parseInt(test.replace(/^(0+)/, ""));
		if (greatest < test) greatest = test;
	}
	var id = (++greatest).toString();
	for (var i = id.length; i < 5; i++) id = "0" + id;
	
	// Increment the sort array by one and fill values.
	currentIndex = mainContacts.length;
	mainContacts[currentIndex] = Array();
	
	for (var i = 0; i < mainTitles.length; i++) {
		mainContacts[currentIndex][i] = document.getElementById("form_add_" + i).value;
		document.getElementById("form_add_" + i).value = "";
	}
	mainContacts[currentIndex][0] = id;
	
	// Repaint the table and switch to view.
	buttonMakeUtilVisible(currentUtil);
	buttonMakeTableVisible();
	buttonDisplayAction("action_view");
	return true;
}
function buttonEditRecord() {
	// Fill the array from form values.
	for (var i = 0; i < mainTitles.length; i++) {
		mainContacts[currentIndex][i] = document.getElementById("form_edit_" + i).value;
		document.getElementById("form_edit_" + i).value = "";
	}
	
	// Repaint the table and switch to view.
	buttonMakeUtilVisible(currentUtil);
	buttonMakeTableVisible();
	buttonDisplayAction("action_view");
	return true;
}
