function get_new_id (field_values) {
	var data_id = -3;
	var values_for_choice;
	for (var Element = 0; Element < field_values.length-2; Element++) {
		values_for_choice = field_values[Element];
		if (Math.abs(values_for_choice[0]) > data_id) {
			data_id = -(Math.abs(values_for_choice[0])+1);
		}
	}
	return data_id;
}

function add_new_related_data (field_values, choice_list, new_label) {
	var data_id = get_new_id (field_values);
	var Element = 0;
	var values_for_choice, blank_array, fields_array, new_array;
	choice_list.options[choice_list.options.length] = new Option ("New...", "-1");
	choice_list.options[choice_list.options.length-2].text = "";
	choice_list.options[choice_list.options.length-2].value = "0";
	choice_list.options[choice_list.options.length-3].text = new_label;
	choice_list.options[choice_list.options.length-3].value = data_id;
	choice_list.selectedIndex = choice_list.options.length-2;
	blank_array = field_values[field_values.length-2];
	fields_array = field_values[field_values.length-1];
	new_array = new Array();
	for (Element = 0; Element < blank_array.length; Element++) {
		new_array[Element] = blank_array[Element];
	}
	new_array[0] = data_id;
	new_array[1] = new_label;
	if (field_values.length == 2) {
		new_array[new_array.length-2] = true;
	}
	field_values[field_values.length-2] = new_array;
	field_values[field_values.length-1] = blank_array;
	field_values[field_values.length] = fields_array;
	choice_list.selectedIndex = choice_list.options.length-3;
}

function find_field_data_by_id (field_values, data_id) {
	var selected_data = 0;
	for (var Element = 0; Element < field_values.length-2; Element++) {
		values_for_choice = field_values[Element];
		if (values_for_choice[0] == data_id) {
			selected_data = Element;
			Element = field_values.length;
		}
	}
	return selected_data;
}

function fill_input_fields (field_values, choice_list) {
	var fields_to_fill = field_values[field_values.length-1];
	var selected_data = find_field_data_by_id(field_values, parseInt(choice_list.options[choice_list.selectedIndex].value));
	var data_id = -3;
	var Element = 0;
	var new_label = "";
	var new_label_entered = false;
	var skip_fill = false;
	var values_for_choice, blank_array, fields_array, new_array;
	if (choice_list.options[choice_list.selectedIndex].value == "-2") {
		skip_fill = true;
		for (Element = 0; Element < fields_to_fill.length; Element++) {
			if (fields_to_fill[Element].type == "checkbox") {
				fields_to_fill[Element].checked = false;
			} else if (fields_to_fill[Element].type == "select-one") {
				fields_to_fill[Element].selectedIndex = 0;
			} else if (fields_to_fill[Element].name.indexOf("_label") > 0) {
				fields_to_fill[Element].value = choice_list.options[choice_list.selectedIndex].text;
			} else {
				fields_to_fill[Element].value = "";
			}
		}		
	}
	if (choice_list.options[choice_list.selectedIndex].value == "-1") {
		new_label = get_new_label_from_user (choice_list);
		if ((new_label == "") || (new_label == null)) {
			choice_list.selectedIndex = choice_list.length-2;
			skip_fill = true;
		} else {
			var matching_label_element = get_element_in_choice_list (new_label, choice_list);
			add_new_related_data (field_values, choice_list, new_label);
			selected_data = find_field_data_by_id (field_values, parseInt(choice_list.options[choice_list.options.length-3].value));
			new_label_entered = true;
			if (matching_label_element > -1) {
				choice_list.options[matching_label_element].value = choice_list.options[choice_list.selectedIndex].value;
				choice_list.options[choice_list.selectedIndex] = null;
				choice_list.selectedIndex = matching_label_element;
			}
		}
	}
	if (choice_list.options[choice_list.selectedIndex].value == "0") {
		data_id = parseInt(fields_to_fill[0].value, 10);
		selected_data = find_field_data_by_id (field_values, data_id);
		for (Element = 0; Element < choice_list.options.length-2; Element++) {
			if (choice_list.options[Element].value == data_id) {
				choice_list.selectedIndex = Element;
				Element = choice_list.options.length;
			}
		}
	}
	if (!skip_fill) {
		var values_to_use = field_values[selected_data];
		for (Element = 0; Element < fields_to_fill.length; Element++) {
			if (fields_to_fill[Element].type == "checkbox") {
				if (choice_list.selectedIndex < (choice_list.options.length - 2)) {
					fields_to_fill[Element].checked = values_to_use[Element];
				} else {
					fields_to_fill[Element].checked = false;
				}
			} else if (fields_to_fill[Element].type == "select-one") {
				if (choice_list.selectedIndex < (choice_list.options.length - 2)) {
					select_option_in_drop_down(values_to_use[Element], fields_to_fill[Element]);
				}
			} else {
				if (choice_list.selectedIndex < (choice_list.options.length - 2)) {
					fields_to_fill[Element].value = values_to_use[Element];
				} else {
					fields_to_fill[Element].value = "";
				}
			}
		}
	}
	if (choice_list.options[choice_list.selectedIndex].value == "-1") {
		fields_to_fill[0].value = data_id;
	}
	if (new_label_entered) {
		for (var Field = 1; Field < fields_to_fill.length; Field++) {
			if ((fields_to_fill[Field].type == "text") & (fields_to_fill[Field].name.indexOf("_label")<0)) {
				fields_to_fill[Field].focus();
				Field = fields_to_fill.length;
			}
		}
	}
}

function get_new_label_from_user (choice_list) {
	var label_name = choice_list.name.toLowerCase();
	label_name = label_name.replace("_", " ");
	var new_label = "";
	var is_valid = true;
	do {
		new_label = prompt ("Please indicate the new " + label_name + " label:", "");
		if (new_label != null) {
			is_valid = validate_new_label (new_label, choice_list);
		}
	} while ((!is_valid) && (new_label != null));
	return new_label;
}

function validate_new_label (label, choice_list) {
	var is_valid = true;
	var element = get_element_in_choice_list (label, choice_list);
	if (element > -1) {
		if (choice_list.options[element].value != -2) {
			is_valid = false;
			alert (label + " is already used as a label. Please enter a different one.");
		}
	}
	return is_valid;
}

function get_element_in_choice_list (label, choice_list) {
	var found_element = -1;
	for (var Element = 0; Element < choice_list.options.length-2; Element++) {
		if (label.toLowerCase() == choice_list.options[Element].text.toLowerCase()) {
			if (Element != choice_list.selectedIndex) {
				found_element = Element;
			}
		}
	}
	return found_element;
}

function change_label (label, choice_list) {
	choice_list.options[choice_list.selectedIndex].text = label;
}

function change_data_in_array (field, field_values, choice_list) {
	var fields_to_fill = field_values[field_values.length-1];  // Get the list of fields that can be filled.
	var values_to_use = field_values[choice_list.selectedIndex];  // Get the values of the fields for this option.
	var element_to_use = 0;
	// Find the element within the array of fields that represents the field that is changed.
	for (var Element = 0; Element < fields_to_fill.length; Element++) {
		if (fields_to_fill[Element].name == field.name) {
			element_to_use = Element;
		}
	}
	if (element_to_use == 1) {  // If the field changed is the label field...
		if (choice_list.selectedIndex >= (choice_list.options.length - 2)) {  // If the data is a new record...
			add_new_related_data (field_values, choice_list, field.value);  // then add the new record...
			fill_input_fields (field_values, choice_list);  // and fill the input fields with the new data.
			set_data_as_primary (field_values, choice_list);  // Determine if the record is to be the primary one.
			fill_input_fields (field_values, choice_list); // Ensure that the current record is showing in the entry fields.
		} else if (validate_new_label(field.value, choice_list)) { // If the user entered in a label that hasn't already been used...
			change_label(field.value, choice_list);  // Change the label in the drop-down list.
			values_to_use[element_to_use] = field.value;
			indicate_related_data_changed(field_values, choice_list);  // Update the data in the data array.
		} else {  // Otherwise, the user tried to enter a previous label...
			field.value = field.form.previous_value.value;  // So return the field to its previous value.
		}
	} else { // Otherwise, the field changed is something else...
		if (choice_list.selectedIndex >= (choice_list.options.length - 2)) {  // If the user is entering in a new record...
			var new_label = get_new_label_from_user (choice_list);  // Get a label from the user for the record.
			if ((new_label == "") || (new_label == null)) {  // If the user doesn't enter in a label...
				// then notify the user that it is required and return the previous value of the field.
				alert ("Each " + choice_list.name.toLowerCase().replace("e_mail", "e-mail").replace("_", " ") + " must have a label. This entry can't be accepted");
				field.value = field.form.previous_value.value;
			} else {  // Otherwise, the user entered a valid label...
				var matching_label_element = get_element_in_choice_list (new_label, choice_list);
				add_new_related_data (field_values, choice_list, new_label);  // Add a new record in the data array.
				values_to_use = field_values[field_values.length-3];
				values_to_use[element_to_use] = field.value;  // Store the newly entered field into the data array.
				set_data_as_primary (field_values, choice_list);  // Indicate whether this is the primary record or not.
				indicate_related_data_changed(field_values, choice_list);  // Set the modification flags accordingly.
				fill_input_fields (field_values, choice_list); // Make sure that the fields are showing all the data for this record.
				if (matching_label_element > -1) {
					choice_list.options[matching_label_element].value = choice_list.options[choice_list.selectedIndex].value;
					choice_list.options[choice_list.selectedIndex] = null;
					choice_list.selectedIndex = matching_label_element;
				}
			}
		} else if (choice_list.options[choice_list.selectedIndex].value == -2) {  // If the user is entering in a new record with the label already made...
			var field_element = field_values.length-2;
			var selected_choice = choice_list.selectedIndex;
			add_new_related_data (field_values, choice_list, choice_list.options[choice_list.selectedIndex].text);  // Add a new record in the data array.
			values_to_use = field_values[field_element];
			values_to_use[element_to_use] = field.value;  // Store the newly entered field into the data array.
			if (field_values.length <= 3) { // If this is the first data added to the array...
				set_data_as_primary (field_values, choice_list);  // Indicate whether this is the primary record or not.
			}
			indicate_related_data_changed(field_values, choice_list);  // Set the modification flags accordingly.
			fill_input_fields (field_values, choice_list); // Make sure that the fields are showing all the data for this record.
			choice_list.options[selected_choice].value = choice_list.options[choice_list.selectedIndex].value;
			choice_list.options[choice_list.selectedIndex] = null;
			choice_list.selectedIndex = selected_choice;
		} else {  // Otherwise, we are changing the field of an existing record...
			element = find_record_element(field_values, choice_list.options[choice_list.selectedIndex].value);
			values_to_use = field_values[element];  // Get the values of the fields for this option.
			values_to_use[element_to_use] = field.value;
			indicate_related_data_changed(field_values, choice_list);  // so indicate that the record has been changed.
		}
	}
}

function validate_form (fields_to_check) {
	var form_is_valid = true, field_is_valid = true;
	var singular_message ="The following field is missing information:\n";
	var plural_message ="The following fields are missing information:\n";
	var message_prefix = singular_message;
	var message ="";
	var field_name = "";
	for (var Element = 0; Element < fields_to_check.length; Element++) {
		if (fields_to_check[Element].type.indexOf("select") >= 0) {
			if (fields_to_check[Element].selectedIndex < 0) {
				field_is_valid = false;
			} else {
				field_is_valid = (fields_to_check[Element].options[fields_to_check[Element].selectedIndex].value != "");
			}
		} else {
			field_is_valid = fields_to_check[Element].value.replace(" ","") != "";
		}
		if (!field_is_valid) {
			if (form_is_valid) {
				message_prefix = singular_message;
			} else {
				message_prefix = plural_message;
			}
			form_is_valid = false;
			field_name = capitalize(fields_to_check[Element].name, "_");
			field_name = field_name.replace(/_/g, " ");
			message = message + "     " + field_name + "\n";
		}
	}
	if (!form_is_valid) {
		alert (message_prefix + message);
	}
	return form_is_valid;
}

function capitalize (text, delimiter) {
	var position = 0;
	while (text.indexOf(delimiter, position+1) > 0) {
		position = text.indexOf(delimiter, position+1);
		text = text.substring(0, position + 1) + text.substring(position + 1, position + 2).toUpperCase() + text.substring(position + 2);
	}
	return text.substring(0,1).toUpperCase() + text.substring(1);
}

function prepare_related_information (field_values, type) {
	var related_information = "";
	var related_fields = "";
	var delimiter = "\t";
	var Record = 0;
	var Element = 0;
	var index_of_label = 0;
	var index_of_info = 0;
	var index_of_primary = 0;
	for (Record = 0; Record < (field_values.length-2); Record++) {
		record_information = field_values[Record];
		prepare_this_data = ((type == "update") && (record_information[record_information.length-1]) && (parseInt(record_information[0], 10) > 0));
		prepare_this_data = (prepare_this_data || ((type == "insert") && (parseInt(record_information[0], 10) < 0)));
		if (prepare_this_data) {
			for (Element = 0; Element < (record_information.length-2); Element++) {
				related_information = related_information + record_information[Element] + "\t";
			}
			related_information = related_information + record_information[record_information.length-2] + "\n";
		}
	}
	if (related_information != "") {
		var record_information = field_values[field_values.length-1];
		var form_object, alert_message;
		for (Element = 0; Element < record_information.length; Element++) {
			if (Element == (record_information.length-1)) {
				delimiter = "\n";
			}
/*
			if (!record_information[Element].name) {
				if (Element == 0) {
					alert_message = "The 1st form object";
				} else {
					alert_message = "The form object after " + record_information[Element-1].name;
				}
				alert_message = alert_message + " could not be referenced.";
				alert("JavaScript Error: " + alert_message);
			} else {
*/
				index_of_label = record_information[Element].name.toLowerCase().lastIndexOf("_label");
				index_of_info = record_information[Element].name.toLowerCase().lastIndexOf("_information");
				index_of_primary = record_information[Element].name.toLowerCase().lastIndexOf("_is_primary");
				if ((index_of_label ==(record_information[Element].name.length-6)) & (index_of_label > 0)) {
					related_fields = related_fields + "label" + delimiter;
				} else if ((index_of_info ==(record_information[Element].name.length-12)) & (index_of_info > 0)) {
					related_fields = related_fields + "contact_information" + delimiter;
				} else if ((index_of_primary ==(record_information[Element].name.length-11)) & (index_of_primary > 0)) {
					related_fields = related_fields + "is_primary" + delimiter;
				} else if (!record_information[Element].name) {
					related_fields = related_fields + "missing_element_" + Element + delimiter;
				} else {
					related_fields = related_fields + record_information[Element].name + delimiter;
				}
//			}
		}
	}
	related_information = related_fields + related_information;
	return related_information;
}

function indicate_related_data_changed (field_values, choice_list) {
	var element = find_record_element(field_values, choice_list.options[choice_list.selectedIndex].value);
	if (element > -1) {
		var record_information = field_values[element];
		record_information[record_information.length-1] = true;
		field_values[element] = record_information;
	}
}

function prepare_and_submit_form (data_holders, required_fields) {
	var holder, holder_field, holder_info;
	var task_name = "";
	var Element = 0;
	for (var Task=0; Task < data_holders.length; Task++) {
		holder = data_holders[Task];
		task_name = holder[0];
		for (Element=1; Element < holder.length; Element++) {
			holder_info = holder[Element];
			holder_field = holder_info[0];
			data_array = holder_info[1];
			holder_field.value = prepare_related_information(data_array, task_name);
		}
	}
	return validate_form(required_fields);
}

function set_data_as_primary (field_values, choice_list) {
	var record_information;
	var primary_field;
	if (choice_list.selectedIndex >= (choice_list.options.length - 2)) {
		if (choice_list.name == "Address") {
			alert ("Please enter an address first.");
		} else if (choice_list.name == "E_Mail") {
			alert ("Please enter an e-mail first.");
		} else {
			alert ("Please enter a " + choice_list.name.toLowerCase() + " first.");
		}
		record_information = field_values[field_values.length-1];
		primary_field = record_information[record_information.length-1];
		primary_field.checked = false;
	} else {
		var primary_element = find_record_element(field_values, choice_list.options[choice_list.selectedIndex].value);
		for (var Element = 0; Element < (field_values.length-2); Element++) {
			record_information = field_values[Element];
			if (Element == primary_element) {
				record_information[record_information.length-2] = true;
			} else if (record_information[record_information.length-2] == true) {
				record_information[record_information.length-2] = false;
				record_information[record_information.length-1] = true;
			}
			field_values[Element] = record_information;
		}
		indicate_related_data_changed (field_values, choice_list);
	}
}

function delete_related_data (field_values, choice_list, delete_field){
	if (choice_list.selectedIndex >= (choice_list.options.length - 2)) {
		alert ("There is nothing to delete.");
	} else {
		var record_information = field_values[choice_list.selectedIndex];
		var fields = field_values[field_values.length-1];
		var message = "Please verify that you want to delete\nthe '";
		message = message + choice_list.options[choice_list.selectedIndex].text;
		message = message + " " + choice_list.name.replace("E_Mail", "E-Mail").replace("_", " ") + "':\n";
		for (var Field = 1; Field < fields.length; Field++) {
			if ((fields[Field].type == "text") & (fields[Field].name.indexOf("_label")<0)) {
				message = message + "     " + record_information[Field] + "\n";
			}
		}
		if (confirm(message)) {
			var data_id = parseInt(choice_list.options[choice_list.selectedIndex].value, 10);
			if (data_id > 0) {
				field_values = delete_related_data_from_array (field_values, data_id, delete_field);
			}
			choice_list.options[choice_list.selectedIndex] = null;
			choice_list.selectedIndex = 0;
			fill_input_fields (field_values, choice_list);
		}
		return field_values;
	}
}

function delete_related_data_from_array (field_values, record_id, delete_field) {
	var new_values = new Array();
	var new_element = 0;
	var values;
	for (var Record = 0; Record < field_values.length; Record++) {
		values = field_values[Record];
		if (values[0] == record_id) {
			if (record_id > 0) {
				if (delete_field.value == "") {
					delete_field.value = "record_id\n";
				}
				delete_field.value = delete_field.value + values[0] + "\n";
			}
		} else {
			new_values[new_element] = values;
			new_element++;
		}
	}
	return new_values;
}

function select_option_in_drop_down(option, choice_list) {
	var Option_Found = false;
	for (var Element=0; Element < choice_list.options.length; Element++) { // Cycle through each option in the drop down list.
		if (choice_list.options[Element].value==option) { // If the value of the option matches the item to select...
			choice_list.selectedIndex = Element; // then select the option...
			Element = choice_list.options.length; // and stop looking at the remaining options.
			Option_Found = true; // Indicate that we have found the option.
		}
		if (Element < choice_list.options.length) { // If we haven't found the option, yet...
			if (choice_list.options[Element].text==option) { // but the text of the option matches the item to select...
				choice_list.selectedIndex = Element; // then select the option...
				Element = choice_list.options.length; // and stop looking at the remaining options.
				Option_Found = true; // Indicate that we have found the option.
			}
		}
	}
	if (!Option_Found) { // If we haven't found the option...
		choice_list.selectedIndex = 0; // then select the first option.
	}
}

function add_input_area(div_name, input_name, suffix, input_text) {
  var object_name = input_name.replace(" ", "_").toLowerCase();
  var div_object = document.getElementById(div_name);
  var name_to_use_in_request = input_name;
  if (add_input_area.arguments.length > 4) {
    name_to_use_in_request = add_input_area.arguments[4];
  }
  if (name_to_use_in_request == "") {
    name_to_use_in_request = input_name;
  }
  if (div_object == null) {
    alert ("Unable to add a new " + name_to_use_in_request + ". The browser may be incompatible.");
  } else {
    if (input_text == "") {
      input_text = prompt("Please enter the new " + name_to_use_in_request + ":","");
    }
    if (input_text != null) {
      if (input_text != "") {
        var table_object_name = "table_" + object_name + "_" + suffix;
        var table_object = document.createElement("table");
        table_object.setAttribute("width", "100%");
        table_object.setAttribute("cellpadding", "0");
        table_object.setAttribute("id", table_object_name);
        var body_object = document.createElement("tbody");
        var row_object = document.createElement("tr");
        var cell_object = document.createElement("td");
        var input_object = document.getElementById("sample_text_" + object_name).cloneNode(true);
        input_object.setAttribute("name", object_name + "_" + suffix);
        input_object.setAttribute("value", input_text);
        input_object.setAttribute("id", object_name + "_" + suffix);
        cell_object.appendChild(input_object);
        input_object = document.createElement("input");
        input_object.setAttribute("name", object_name + "_" + suffix + "_id");
        input_object.setAttribute("value", -suffix);
        input_object.setAttribute("type", "hidden");
        input_object.setAttribute("id", object_name + "_" + suffix + "_id");
        cell_object.appendChild(input_object);
        row_object.appendChild(cell_object);
        cell_object = document.createElement("td");
        var checkbox_object = document.getElementById("sample_" + object_name).cloneNode(true);
        checkbox_object.setAttribute("name", "delete_" + object_name + "_" + suffix);
        cell_object.appendChild(checkbox_object);
        var extra_object = document.getElementById("extra_" + object_name);
        if (extra_object != null) {
          var new_extra_object = extra_object.cloneNode(true);
          new_extra_object.setAttribute("id", "extra_" + object_name + "_" + suffix);
          cell_object.appendChild(new_extra_object);
        }
        row_object.appendChild(cell_object);
        body_object.appendChild(row_object);
        table_object.appendChild(body_object);
        div_object.appendChild(table_object);
        suffix++;
      }
    }
  }
  return suffix;
}

function add_related_data (data_array, area, related_data_name, id_to_use, form) {
  current_id = id_to_use;
  var name_to_use_in_request = "";
  if (add_related_data.arguments.length > 5) {
    name_to_use_in_request = add_related_data.arguments[5];
  }
  id_to_use  = add_input_area(area, related_data_name, current_id, '', name_to_use_in_request);
  if (current_id != id_to_use) {
    var blank_array = data_array[data_array.length-2];
    var fields_array = data_array[data_array.length-1];
    var object_name = related_data_name.replace(" ", "_").toLowerCase()+"_"+current_id;
    var new_data = form.elements[object_name].value;
    data_array[data_array.length-2] = new Array(-current_id, new_data, blank_array[2], false, false);
    data_array[data_array.length-1] = blank_array;
    data_array[data_array.length] = fields_array;
  }
  return data_array;
}

function delete_input_area(delete_button, area_name, input_type) {
  var delete_button_name = delete_button.name;
  var text_object_name = delete_button_name.replace("delete_", "");
  var text_object = document.getElementById(text_object_name);
  var message = "Please verify that you want to remove the " + input_type + " '";
  message = message + text_object.value + "':";
  var delete_confirmed = false;
  if (confirm(message)) {
    var area_object = document.getElementById(area_name);
    text_object_name = delete_button_name.replace("delete_", "table_");
    text_object = document.getElementById(text_object_name);
    area_object.removeChild(text_object);
    delete_confirmed = true;
  } else if (delete_button.type=="checkbox") {
    delete_button.checked = false;
  }
  return delete_confirmed;
}

function update_related_data(field_values, text_field) {
  var id_object_name = text_field.name + "_id";
  var record_id = parseInt(document.getElementById(id_object_name).value, 10);
  var record;
  if (record_id > 0) {
    for (var element = 0; element < field_values.length; element++) {
      record = field_values[element];
      if (record[0] == record_id) {
        record[1] = text_field.value;
        record[record.length-1] = true;
        field_values[element] = record;
        element = field_values.length;
      }
    }
  }
  return field_values;
}

function keep_enter_key() {
	document.keep_enter_key_active = true;
}

function force_enter_to_submit() {
	set_inactivity_timeout();
	if (document.keep_enter_key_active) {
		document.keep_enter_key_active = false;
	} else {
		var enter_key_pressed = false;
		var parameters = force_enter_to_submit.arguments;
		if (parameters.length > 0) {
			enter_key_pressed = (parameters[0].which == 13);
		} else {
			enter_key_pressed = (event.keyCode == 13);
			if (enter_key_pressed) {
				event.returnValue = false;
			}
		}
		if (enter_key_pressed) {
			submit_form();
		}
		if ((parameters.length > 0) & enter_key_pressed) {
			return false;
		}
	}
}

function moveOver(source, destination)  {
  var boxLength = destination.options.length;
  var selectedItem = source.selectedIndex;
  if (selectedItem>=0){
    var selectedText = source.options[selectedItem].text;
    var selectedValue = source.options[selectedItem].value;
    newoption = new Option(selectedText, selectedValue, false, false);
    destination.options[boxLength] = newoption;
    source.selectedIndex=-1;
    destination.selectedIndex=-1;
  }
}

function is_selected_choice_new (source, destination) {
  var isNew = true; 
  if (source.selectedIndex<0) {
    isNew = false;
    alert ("Please make a selection first.");
  } else {
    var selectedText = source.options[source.selectedIndex].value;
    if (destination.options.length != 0) {
      for (var i = 0; i < destination.options.length; i++) {
        thisitem = destination.options[i].value;
        if (thisitem == selectedText) {
          isNew = false;
          break;
        }
      }
    }
  }
  return isNew;
}

function add_selected_choice (source, destination) {
  if (is_selected_choice_new (source, destination)) {
    moveOver(source, destination);
  } else {
    alert(source.options[source.selectedIndex].text+' is already selected.');
  }
}

function remove_selected_choice (choice_list) {
  choice_list.options[choice_list.selectedIndex] = null;
  choice_list.selectedIndex = -1;
}

function place_focus (form) {
  for (var element=0; element < form.elements.length; element++) {
    if (form.elements[element].type=="text") {
      form.elements[element].focus();
      element = form.elements.length
    }
  }
}

function add_to_date (date_value, number_of_days) {
	var date_to_use = new Date(date_value);
	date_to_use = new Date(date_to_use.getTime()+(number_of_days*24*60*60*1000));
	date_value = (date_to_use.getMonth() + 1) + "/" + date_to_use.getDate() + "/" + date_to_use.getFullYear();
	return date_value;
}

function add_HTML_text (text_area, HTML_to_add) {
	var text_to_add = "";
	var prefix = "";
	var suffix = "";
	var request_message = "Please enter the text that you want to make " + HTML_to_add;
	if (HTML_to_add == "break") {
		text_area.value = text_area.value + "<br>\n<br>\n";
		request_message = "";
	} else if (HTML_to_add == "bold") {
		prefix = "<b>";
		suffix = "</b>";
	} else if (HTML_to_add == "italics") {
		prefix = "<i>";
		suffix = "</i>";
	} else if (HTML_to_add == "link") {
		request_message = "";
		text_to_add = prompt("Please enter the web address of the link (including the 'http://')", "");
		if (text_to_add != null) {
			if (text_to_add != "") {
				request_message = "Please enter the text that you want to display as the link";
				prefix = "<a href=\"" + text_to_add + "\" target=\"_blank\">";
				suffix = "</a>";
			}
		}
	}
	if (request_message != "") {
		text_to_add = prompt (request_message, "");
	}
	if (text_to_add != null) {
		if (text_to_add != "") {
			text_area.value = text_area.value + prefix + text_to_add + suffix;
		}
	}
	text_area.focus();
}

function set_state_option(state_list, state) {
	if (state != "") {
		for (var element = 1; element < state_list.options.length; element++) {
			if (state_list.options[element].value == state) {
				state_list.selectedIndex = element;
				element = state_list.options.length;
			}
		}
	}
}

function find_record_element(field_values, data_id) {
  var record_data;
  var element_to_use = -1;
  var id_element = 0;
  if (find_record_element.arguments.length == 3) {
    id_element = find_record_element.arguments[2];
  }
  // Cycle through each data record...
  for (var element = 0; element < (field_values.length - 2); element++) {
    record_data = field_values[element];  // Load in the data for the record.
    if (record_data[id_element] == data_id) {  // If the record ID matches the supplied ID...
      element_to_use = element;  // then use the element of the record...
      element = field_values.length;  // and stop the for loop.
    }
  }
  return element_to_use;  // Return the element that was found.
}

function Check_E_Mail(field) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(field.value)){
		return (true)
	} else {
		field.value=field.form.previous_value.value;
		alert("Invalid E-Mail Address. Please re-enter in the form 'myname@myaddress.com'.")
		return (false)
	}
}

function update_e_mail(field, data_array, drop_down_list) {
	if (Check_E_Mail(field)) {
		change_data_in_array(field, data_array, drop_down_list)
	}
}

function filter_entry (field, filter_rule, event_object) {
	var filter = "";
	var key_pressed = "";
	var ascii_code = 0;
	if (filter_rule == "letters only") {
		filter = /[^a-zA-Z\t]/gi;
	} else if (filter_rule == "numbers only") {
		filter = /[^0-9\t]/gi;
	} else if (filter_rule == "numbers and decimal") {
		filter = /[^0-9\.\t]/gi;
	} else if (filter_rule == "letters and numbers only") {
		filter = /[^a-zA-Z0-9\t]/gi;
	} else {
		filter = filter_rule;
	} 
	if (event_object.keyCode == 0) {
		key_pressed = String.fromCharCode(event_object.which);
		ascii_code = event_object.which;
	} else {
		key_pressed = String.fromCharCode(event_object.keyCode);
		ascii_code = event_object.keyCode;
	}
	key_pressed = key_pressed.replace(filter,"");
	return ((key_pressed != "") || (ascii_code == 8));
}

function make_inactive (form) {
	var is_delete = false;
	var message = "";
	if (make_inactive.arguments.length > 1) {
		is_delete = make_inactive.arguments[1];
	}
	if (is_delete) {
		message = "Please verify that you want to delete this record.";
	} else {
		message = "Please verify that you want to make this record inactive.";
	}
	if (confirm (message)) {
		if (is_delete) {
			form.delete_record.value="true";
		} else {
			form.inactive.value="true";
		}
		submit_form();
	}
}

function show_delete_button () {
	ShowBlock("Delete_Line_1");
	ShowBlock("Delete_Line_2");
	ShowBlock("Delete_Line_3");
	ShowBlock("Delete_Text");
}

function format_as_currency(value_to_format) {
	var number_to_use = new Number(value_to_format); // Ensure that we are working with a number.
	value_to_format = number_to_use.toFixed(2); // Make sure that we have two decimal places, rounding if necessary.
	for (var position = (value_to_format.length-6); position >  0; position-=3) { // Place commas where necessary.
		value_to_format = value_to_format.substring(0, position) + "," + value_to_format.substring(position);
	}
	if (value_to_format.substring(0, 1) == "-") { // Place the dollar sign in front and include a minus when necessary.
		value_to_format = "-$" + value_to_format;
	} else {
		value_to_format = "$" + value_to_format;
	}
	return value_to_format;	
}

function make_list(list_to_use, field_to_get_list) {
	field_to_get_list.value = "";
	var comma = "";
	for (var element=0; element < list_to_use.options.length; element++) {
		if (list_to_use.options[element].selected == true) {
			field_to_get_list.value += comma + "'" + list_to_use.options[element].value + "'";
			comma = ", ";
		}
	}
}

function load_list(value_to_match, scroll_list, data_array) {
	var Element;
	var data;
	for (Element = 0; scroll_list.options.length > 0;) {
		scroll_list.options[0] = null;
	}
	for (Element = 0; Element < data_array.length; Element++) {
		data = data_array[Element];
		if (data[0]==value_to_match) {
			scroll_list.options[scroll_list.options.length] = new Option(data[2], data[1]);
		}
	}
	scroll_list.selectedIndex = -1;
}

function no_email_in_phone (phone_object) {
	if (phone_object.value.indexOf("@") >= 0) {
		return false;
	} else {
		return true;
	}
}