// this allows for special processing with form fields

function formfield_select_checkbox ( checkbox ) {
   // use this function to automatically select a checkbox or radio button

   // check the box only if actual characters
   switch ( event.keyCode ) {
      case 9: // tab
      case 16: // shift
         break;
      default:
         checkbox.checked = true;
   }
}

var formfield_previous_values = new Array ();
var formfield_previous_checked = null;
function switch_option_and_enable_status ( activeformfield, inactiveformfield, activeoption ) {
   // use this to toggle enabled/disabled status on fields, and automatically change a radio button status

   if ( activeoption.value != formfield_previous_checked ) {

      var activecurrentvalue = activeformfield.value;
      var inactivecurrentvalue = inactiveformfield.value;

      if ( ! formfield_previous_values[inactiveformfield.id] ) formfield_previous_values[inactiveformfield.id] = "";
      if ( ! formfield_previous_values[activeformfield.id] ) formfield_previous_values[activeformfield.id] = "";

      if ( activeformfield.disabled == true ) {
         activeformfield.disabled = false;
         inactiveformfield.disabled = true;
         activeoption.checked = true;

         if ( inactiveformfield.type == "text" )
            inactiveformfield.value = formfield_previous_values[inactiveformfield.id];
         if ( activeformfield.type == "text" )
            activeformfield.value = formfield_previous_values[activeformfield.id];

         activeformfield.focus ();
      }

      formfield_previous_values[activeformfield.id] = activecurrentvalue;
      formfield_previous_values[inactiveformfield.id] = inactivecurrentvalue;

      formfield_previous_checked = activeoption.value;
   }

}

function formfield_reset_switch_option_vars () {
   formfield_previous_values = new Array ();
   formfield_previous_checked = null;
}

function formfield_check_integers () {

   var k = event.keyCode;

   // allow only numbers, tab, enter, left arrow, right arrow, delete, backspace
   if ( ( k >= 48 && k <= 57 ) || ( k >= 96 && k <= 105 ) || k == 9 || k == 13 || k == 37 || k == 39 || k == 46 || k == 8 ) return true;
   else return false;

}

var formfield_form_submitted = false;
function formfield_form_not_submitted () {

   if ( ! formfield_form_submitted ) {
      formfield_form_submitted = true;
      return true;
   }
   else return false;

}

function formfield_field_has_value ( formfield, message ) {

   if ( formfield.value ) return true;
   else {
      alert ( message );
      return false;
   }

}

function formfield_validate_checkbox_selection ( form, prefix ) {

   var numelements = form.elements.length;
   var returnval = false;

   for ( var i = 0; i < numelements; i++ ) {
      if ( form.elements[i].type == "checkbox" && form.elements[i].name.substr ( 0, prefix.length ) == prefix && form.elements[i].checked ) {
         returnval = true;
         break;
      }
   }

   return returnval;

}

var formfield_select_indexes = new Array ();
function formfield_skip_select_separator ( field ) {

   var lastindex = formfield_select_indexes[field.name];
   if ( ! lastindex ) lastindex = 0;

   if ( field.options[field.selectedIndex].value == "separator" ) {

      var newindex = field.selectedIndex;
      newindex += ( newindex > lastindex ) ? 1 : -1;

      if ( ! field.options[newindex] ) newindex = 0;
      field.selectedIndex = newindex;
   }

   formfield_select_indexes[field.name] = field.selectedIndex;

}
