// JAVASCRIPT FORM CONTROL FUNCTIONS

// Select or deselect a group of checkboxes
function select_all(form, group, val) {
   // We could receive a form name or object - we want an object
   if (typeof(form) != "object") { form = document.forms[form]; }
   if (typeof(form) != "object") {
      alert("Syntax: select_all() expects a form as the first argument.");
      return(false);
   }

   // We could receive a form element name or object - we want the name
   if (typeof(group) == "object") { group = group.name; }
   if (typeof(form.elements[group]) != "object") {
      alert("Syntax: select_all() expects a checkbox group as the second argument.");
      return(false);
   }
   if (typeof(form.elements[group][0]) != "object") {
      alert("Syntax: select_all() expects a checkbox group as the second argument.");
      return(false);
   }

   // Check or uncheck all the checkboxes
   for (i = 0; i < form.elements[group].length; i++) {
      form.elements[group][i].checked = val;
   }
}

// Redirect user to the URL specified in the field value
function surfto(form, selection, scope) {
  // We could receive a form name or object - we want an object
  if (typeof(form) != "object") { form = document.forms[form]; }
  if (typeof(form) != "object") {
    alert("Syntax: surfto() expects a form as the first argument.");
    return(false);
  }

  // We could receive a form element name or object - we want the object
  if (typeof(selection) != "object") { selection = form.elements[selection]; }
  if (
    (typeof(selection) != "object") ||
    (typeof(selection[0]) != "object")
  ) {
    alert("Syntax: surfto() expects a drop-down list as the second argument.");
    return(false);
  }

  var myindex=selection.selectedIndex;
//   var mag=selection.options[myindex].value;
  if (selection.options[myindex].value != "0") {

    // Change current window/frame location
    if (scope==0) {
//      alert(form.action);
//      alert(typeof(form.action));
//      alert(selection.options[myindex].value);
      if (typeof(form.action) == "string") {
        window.location=selection.options[myindex].value;
      } else {
        window.location="index.cfm?action=" + form.action.value + "&" + selection.name + "=" + selection.options[myindex].value;
      }
    }
    // Change top-level frame location
    else if (scope==1) {
      top.location=selection.options[myindex].value;
    }
    else if (scope==2) {
      // Open new window (400x480)
      window.open(selection.options[myindex].value,"","scrollbars,resizable,width=400,height=480");
    }
  }
}

// Get the current value of an drop-down list
function get_select_option(element)
{
   // Sanity check
   if (typeof(element) != "object") {
      alert("form_ctl.js: get_select_option() requires an object, but was passed a(n) " + typeof(element) + "!");
      return(null);
      // NOTREACHED
   }

   // Get value 
   var val = null;
   for (var j = 0; j < element.length; j++) {
      if (element[j].selected) {
         val = element[j].value;
      }
   }
   return(val);
}

// Set the current value of an drop-down list
function set_select_option(element, val)
{
   // Sanity check
   if (typeof(element) != "object") {
      alert("form_ctl.js: set_select_option() requires an object, but was passed a(n) " + typeof(element) + "!");
      return(null);
      // NOTREACHED
   }

   // Get value
   for (var j = 0; j < element.length; j++) {
      if (val == element[j].value) {
         element[j].selected = true;
      }
      else {
         element[j].selected = false;
      }
   }
   return(val);
}

// Get the value of a form element whatever it is
function value(element) {
   // Sanity check
   if (typeof(element) != "object") {
      alert("form_ctl.js: value() requires an object, but was passed a(n) " + typeof(element) + "!");
      return(null);
      // NOTREACHED
   }

   // If value is non-null, return it
   if (element.value != null) {
      return(element.value);
      // NOTREACHED
   }

   // If selected option found, return it
   var val = get_select_option(element);
   if (val != null) {
      return(val);
      // NOTREACHED
   }

   // XXX Fallthrough
   alert("Value of element '" + element.id + "' was not found.");
   return(null);
}

// Set the value of a form element whatever it is
function set(element, val) {
   // Sanity check
   if (typeof(element) != "object") {
      alert("form_ctl.js: set() requires an object, but was passed a(n) " + typeof(element) + "!");
      return;
      // NOTREACHED
   }

   // If value is non-null, return it
   if (element.value != null) {
      element.value = val;
      return;
      // NOTREACHED
   }

   // If selected option found, return it
   var dummy = get_select_option(element);
   if (val != null) {
      set_select_option(element, val);
      return;
      // NOTREACHED
   }

   // XXX Fallthrough
   alert("Value of element '" + element.id + "' was not found.");
   return;
}

// Copy Address from one set of address fields to another
function copy_address (thisform, source, dest) {
	field_list = 'ContactName,OrgName,Street,City,County,Postcode,Country,Phone,Fax,Email,URL';
	field_array = field_list.split(',');

	for (i=0; i<field_array.length; i++) {
//		source_elem = eval("document.forms." + thisform.name + "." + source + "Address" + field_array[i]);
//		source_val = value(source_elem);

//		dest_elem = eval("document.forms." + thisform.name + "." + dest + "Address" + field_array[i]);
//		dest_val = value(dest_elem);

//		set(dest_elem,source_val);
//		alert("Index: " + i + "\nElement: " + field_array[i] + "\nValue: " + source_val);

		set(
			document.forms[thisform.name][dest + "Address" + field_array[i]],
			value(document.forms[thisform.name][source + "Address" + field_array[i]])
		);
	}
}

// Focus formfield element (called by body onLoad event handler)
function focusField (thisform, thiselem) {
	fieldname = eval("document." + thisform + "." + thiselem);
	fieldname.focus();
}
