//Adesa Autctions javascript
//10-9-2003  John Basso
//script for use with subscription mgmt accounts
//script can be modified to suit special client 
//requests



var process_choice_type = "";
var cbon_lit = "UNKNOWN";
var cboff_lit = "UNKNOWN";

// FUNCTION: isblank 
// 
// Test if a string is entirely composed of whitespace 
// characters (spaces, newlines, and tabs).
//
// Params : the String - The string to be tested
//
// Returns: True  - Blank string
//        : False - Nonblank string

function isblank(theString)
{ slen = theString.length;              
  for (idx = 0; slen > idx; idx++)
    { schar = theString.charAt(idx);
      if (schar != ' ' && schar!= '\n' && schar != '\t')
        return false;
    }
  return true;
}

// FUNCTION: setCBGrpMasters
//
// Set the state of all checkbox group "master"
// checkboxes upon form load or reset.
//
// Params : theForm - Form object
//
// Returns: NONE 

function setCBGrpMasters(theForm)
{ var CBgr_sta;                                 // The checkbox group's state
  var CBgr_nam;                                 // The checkbox group's name
  var elem_cnt;                                 // Element count
  var innr_idx;                                 // Inner loop index
  var outr_idx;                                 // Outer loop index
  var elemi;                                    // Inner loop element
  var elemo;                                    // Outer loop element
  
// Get the number of elements in the form and enter an outer loop
// in which we'll search for checkbox group masters. (Elements in 
// which the name and checkbox group properties are the same). 

  elem_cnt = theForm.elements.length;
  for (outr_idx = 0; elem_cnt > outr_idx; outr_idx++)
    { elemo = theForm.elements[outr_idx]; 
      if (elemo.CB_group != elemo.name)
        continue;
      CBgr_nam = elemo.CB_group;
      CBgr_sta = true;

// We've found a checkbox group master. We now enter an inner
// loop in which the state of all the other checkboxes in the 
// group is tested. If they're all "checked", then the master 
// will also be set to the "checked" state. Otherwise it will 
// be set to the "unchecked" state.

      for (innr_idx = 0; elem_cnt > innr_idx; innr_idx++)
        { elemi = theForm.elements[innr_idx]; 
          if (elemi.CB_group == elemi.name ||
              elemi.CB_group != CBgr_nam)
            continue;
          if (elemi.checked)
            continue;
          CBgr_sta = false;
          break;
        }
      elemo.checked = CBgr_sta;
    }
}

// FUNCTION: CBox_to_LBox
//
// Set the "selected" state of all listbox 
// group members upon form load or reset.
//
// Params : theForm - Form object
//
// Returns: NONE 

function CBox_to_LBox(theForm)
{ var elem_cnt;                  // Element count
  var outr_idx;                  // Outer loop index
  var innr_idx;                  // Inner loop index
  var cbox_nam;                  // Checkbox name
  var cbox_sta;                  // Checkbox state
  var jscr_cmd;                  // Javascript command buffer
  var slct_tmp;                  // First listbox group member "selected" state
  
// Fetch the number of elements in the form and enter an outer loop
// in which we we'll search for listbox group masters. (Elements in 
// which the name and listbox group properties are the same). 

  elem_cnt = theForm.elements.length;
  for (outr_idx = 0; elem_cnt > outr_idx; outr_idx++)
    { elemo = theForm.elements[outr_idx]; 
      if (elemo.LB_group != elemo.name)
        continue;

// We've found a listbox group master. We now enter an inner loop
// in which the literal "yes/no" value of the checkbox associated 
// with each member of that group is translated into a "selected"
// state of true or false, and assigned to that member.

      for (innr_idx = 0; elemo.length > innr_idx; innr_idx++)
        { cbox_nam = elemo[innr_idx].value;
          jscr_cmd = "cbox_sta = ";
          jscr_cmd += "theForm.";
          jscr_cmd += cbox_nam;
          jscr_cmd += ".value;";
          eval(jscr_cmd);
          if (cbox_sta == cbon_lit)
            elemo[innr_idx].selected = true;
          else
            elemo[innr_idx].selected = false;
        }

// All done. Set, and restore the first listbox group 
// member's "selected" state so that is has focus.

      slct_tmp = elemo[0].selected;
      elemo[0].selected = true;
      elemo[0].selected = slct_tmp;
    }
  return;
}

// FUNCTION: setProps
//
// Set up validation properties and field names for
// subscriber data elements. Executed at form load.
//
// Params : eBase - Base element location
//
// Returns: NONE

function setProps(eBase)
{ 

  eBase.Dealership.field_label = "Dealership";
  eBase.Dealership.mandatory=true;
  eBase.First_Name.field_label = "First Name";
  eBase.First_Name.mandatory=true;
  eBase.Last_Name.field_label = "Last Name";
  eBase.Last_Name.mandatory=true;
  eBase.Address.field_label = "Address";
  eBase.Address.mandatory=true;
  eBase.City.field_label = "City";
  eBase.City.mandatory=true;
  eBase.State.field_label = "State";
  eBase.State.mandatory=true;
  eBase.Zip_Code.field_label = "Zip Code";
  eBase.Zip_Code.mandatory=true;
  eBase.Email.field_label = "Email";
  eBase.Email.mandatory=true;

  
  setCBGrpMasters(eBase);             // Set checkbox group master checkboxes
  CBox_to_LBox(eBase);                // Set listbox group members
  window.scrollTo(0,0);               // Focus to the top of the form
}

// FUNCTION: LBox_to_CBox
//
// Translate the "selected" state of all listbox 
// group members into literal "yes/no" states in 
// their associated checkboxes. 
//
// Params : theForm - Form object
//
// Returns: NONE 

function LBox_to_CBox(theForm)
{ var elem_cnt;                                  // Element count
  var outr_idx;                                  // Outer loop index
  var innr_idx;                                  // Inner loop index
  var cbox_nam;                                  // Checkbox name
  var jscr_cmd;                                  // Javascript command buffer

// Fetch the number of elements in the form and enter an outer loop
// in which we we'll search for listbox group masters. (Elements in 
// which the name and listbox group properties are the same). 
  
  elem_cnt = theForm.elements.length;
  for (outr_idx = 0; elem_cnt > outr_idx; outr_idx++)
    { elemo = theForm.elements[outr_idx]; 
      if (elemo.LB_group != elemo.name)
        continue;

// We've found a listbox group master. We now enter an inner loop
// in which the true/false "select" state of each group member is
// translated into a literal "yes/no" value, and then assigned to 
// the checkbox associated with that member. 

      for (innr_idx = 0; elemo.length > innr_idx; innr_idx++)
        { cbox_nam = elemo[innr_idx].value;
          jscr_cmd = "theForm.";
          jscr_cmd += cbox_nam;
          jscr_cmd += ".value = ";
          if (elemo[innr_idx].selected)
            jscr_cmd += "cbon_lit;";
          else
            jscr_cmd += "cboff_lit;";
          eval(jscr_cmd);
        }
    }
  return;
}

// FUNCTION: validateElements 
//
// Check for the presence of mandatory text and select fields,
// and confirm modification and cancellation requests.
//
// Triggered at the time of form submission.
//
// Params : theForm - The form
//
// Returns: True  - Form contents valid and submission confirmed
//        : False - Data missing/invalid or submission cancelled

function validateElements(theForm)
{ 
// EDITING NOTE:
// The following three messages may be altered for language/customization purposes.

  var confirm_cancel_msg = "Submit cancellation request?";
  var confirm_modify_msg = "Submit modified information?";
  var missing_fields_msg = "The following mandatory fields are missing:";

  var missing_field_names = "";
  var msg_buff = "";
  var theIndex = "";
  var theValue = "";

// Bypass validation checks for cancellations.

  if (process_choice_type != "C")
    { for (var i = 0; theForm.length > i; i++)
        { var e = theForm.elements[i];
          if (e.type == "text" || e.type == "textarea")
            { is_text = true;
              theIndex = 0;
              theValue = e.value;
            }
          else if (e.type == "select-one" || e.type == "select-multiple")
            { is_text = false;
              theIndex = e.selectedIndex;
              if (theIndex == -1)
                theValue = "";
              else
                theValue = e.options[e.selectedIndex].text;
            }
          else
            continue;
          
          if (e.mandatory && (theValue == null || theValue == "" || isblank(theValue) || theIndex == -1))
            missing_field_names += e.field_label + "\n";
        }
      if (missing_field_names.length > 0)
        msg_buff += missing_fields_msg + "\n\n" + missing_field_names + "\n";
      if (msg_buff.length > 0)
        { alert(msg_buff);
          return false;
        }
    }

// Translate listbox group member states, then 
// assign them to their associated checkboxes.

  LBox_to_CBox(theForm);
  
// Confirm the user's choice.

  if (process_choice_type == "C")
    msg_buff = confirm_cancel_msg;
  else if (process_choice_type == "M")
    msg_buff = confirm_modify_msg;
  else
    return true;
  if (confirm(msg_buff))
    return true;
  return false;
}

// FUNCTION: readjCBGrp
//
// Readjust the states of the members of a checkbox 
// group whenever one of them has been clicked.
//
// Params : Clicked_Box - Clicked checkbox object 
//        : the_Form    - Form object
//
// Returns: NONE

function readjCBGrp(Clicked_Box, the_Form)
{ var CBgr_sta;                                 // The checkbox group's state
  var CBgr_nam;                                 // The checkbox group's name
  var elem_cnt;                                 // Element count
  var elem_idx;                                 // Element index
  var elem_nam;                                 // Element name

// Get the checkbox group and name properties of the 
// clicked checkbox, and also the number of elements 
// in the form.

  CBgr_nam = Clicked_Box.CB_group;
  elem_nam = Clicked_Box.name;
  elem_cnt = the_Form.elements.length;

// If the clicked checkbox's group and name are the same, then it's
// the "master" that toggles all the other checkboxes in the group. 
// Its current state is copied to each of those other checkboxes.

  if (CBgr_nam == elem_nam)
    { if (Clicked_Box.checked)
        CBgr_sta = true;
      else
        CBgr_sta = false;
      for (elem_idx = 0; elem_cnt > elem_idx; elem_idx++)
        { if (the_Form.elements[elem_idx].CB_group == CBgr_nam)
            the_Form.elements[elem_idx].checked = CBgr_sta;
        }
    }

// If we get here, then the clicked checkbox isn't the master,
// but merely a member of the group. As it's been individually
// "clicked", the master's state must be set to "unclicked".
  
  else
    { for (elem_idx = 0; elem_cnt > elem_idx; elem_idx++)
        { if (the_Form.elements[elem_idx].CB_group == CBgr_nam &&
              the_Form.elements[elem_idx].name == CBgr_nam)
            { the_Form.elements[elem_idx].checked = false;
              break;
            }
        }
    }
}

// FUNCTION: doReset
//
// Reset the form field values to their defaults. We can't use an
// "onreset" handler, since that only fires off before the actual 
// reset and we need to do some post-reset group manipulation. We
// also can't utilize a javascript "form.reset()" since Netscape,
// in its infinite wisdom, implements form resets asynchronously. 
// This results in the actual reset taking place only after we've 
// done the group manipulations, thus wiping them out. Sheesh!
//
// Params : theForm - Form object
//
// Returns: NONE

function doReset(theForm)
{ var elem_cnt;                                        // Element count
  var elem_idx;                                        // Element index
  var elem_typ;                                        // Element type
  var opt_cnt;                                         // Select option count
  var opt_idx;                                         // Select option index

// Get the number of elements in the form and buzz 
// thru them, setting all to their default values.

  elem_cnt = theForm.elements.length;
  for (elem_idx = 0; elem_cnt > elem_idx; elem_idx++)
    { elem_typ = theForm.elements[elem_idx].type;
      if (elem_typ == "checkbox" || elem_typ == "radio")
        theForm.elements[elem_idx].checked = theForm.elements[elem_idx].defaultChecked; 
      if (elem_typ == "text" || elem_typ == "textarea")
        theForm.elements[elem_idx].value = theForm.elements[elem_idx].defaultValue; 
      if (elem_typ == "select-one" || elem_typ == "select-multiple")
        { opt_cnt = theForm.elements[elem_idx].options.length;
          for (opt_idx = 0; opt_cnt > opt_idx; opt_idx ++)
             theForm.elements[elem_idx].options[opt_idx].selected = theForm.elements[elem_idx].options[opt_idx].defaultSelected;
        }
    }
  setCBGrpMasters(theForm);             // Set checkbox group master checkboxes
  CBox_to_LBox(theForm);                // Set listbox group members
  window.scrollTo(0,0);                 // Focus to the top of the form
}
