/**
 *
 * Script to gather the values of multiple choice checkboxes that have been
 * selected. Strings each value together separated by "::"s.
 *
 * Thomas Nicolosi Red Bus Corp.
 * thomas@redbuscorp.com   http://www.redbuscorp.com
 * 
 * Current Revision: 2.0.0
 * Revision Date: 2008-10-17
 * Revision: 
 * 1) Removed addLoadEvent in order to use jQuery to preload.
 * 2) Removed form validation function checkForm()
 *
 * Rev. 1.0.0.1 5/6/08 - modified to accommodate whitepaper forms
 */
 /**
 * prepareMultiples() adds multipleWrite to the onsubmit event
 * of the form.
 */
function multiNames(){
  var theNames = new Array(
                     "00N50000001ZGUu"//was interestedProducts - 2011-02-14
                     );
  return theNames;
}
/**
 * First grab the names of the multiple choice controls
 * The names of these elements are stored in function multiNames
 * and are returned in an array to multipleReWrite.
 *
 * More multiple value checkboxes may be added by adding their HTML
 * name attributes to the array in multiNames.
 *
 */
function multipleRewrite(){
  /**
   * 
   */
  var multipleNames = multiNames();//Retrieve the names of the multiple choice checkboxes.
  var messageText = new Array();//array to store the values from each checked checkbox.
  var i;//counter
  var checkBoxName;//HTML name attribute for each multiple choice checkbox group.
  var checkBoxArray;//array to store the checkboxes from each checkbox group.
  for(i = 0; i < multipleNames.length; i++)
  {
      //grab the checkbox group HTML name
      checkBoxName = multipleNames[i];
      //load the checkboxes from the group into an array.
      checkBoxArray = nameArray(checkBoxName);
      //Concatenate the values from the checked boxes and write the resulting string to an array element.
      messageText[i] = writeValues(checkBoxArray);
      //set all the checked attributes of the checkboxes in the group to "false".
      clearNamedArray(checkBoxName);
      //Set the value of the first checkbox in the group to the string created above and set the checked attribute to "true". This value is then posted to the server.
      setCheckedValue(checkBoxName,messageText[i]);
  }
}
/**
 * In order to send a single string of values back to the server
 * it is necessary to 1) uncheck all the checkboxes;
 * 2) write the value of the concatenated string to the first
 * checkbox; and 3) check the checkbox where the new value was
 * written.
 * 
 * function clearNamedArray sets all of the checkbox checked 
 * attributes to "false"
 *
 * function setCheckedValue sets the checked attribute of the first checkbox 
 * in the group to "true" and writes the value of the concatentated string
 * created in multipleReWrite to the value attribute. This value is what
 * is sent back to the server.
 *
 */
function setCheckedValue(theName,valueString){
     var checkboxes = document.getElementsByName(theName);
     checkboxes[0].checked = true;
     checkboxes[0].value = valueString;
}
function clearNamedArray(theName){
    var checkboxes = document.getElementsByName(theName);
    var i;
    for(i=0; i< checkboxes.length; i++){
      checkboxes[i].checked = false;
    }
}
/**
 * nameArray gets the checkboxes that are grouped by HTML name attribute and
 * returns them as an array.
 */
function nameArray(theName){
  var checkboxes = document.getElementsByName(theName);
  return checkboxes;
}
function writeValues(theBoxes){
  //theBoxes contains the checkboxes that are grouped by HTML name attribute.
  var j = 0;//outer loop counter
  var aBox = theBoxes[j];//the first check box
  var theValues = new Array();//store checked values
  var checkedText = ""; //concatenated checked values
  var k = 0;//inner loop counter
  //Loop through the array of check boxes in the group. Store the value of each checkbox whose checked attribute is "true" to an array element in theValues.
  for(j = 0; j < theBoxes.length; j++){
    aBox = theBoxes[j];
    if(aBox.checked){
        theValues[k] = aBox.value;
        k++;
    }
  }
  if(theValues.length < 1){
    return checkedText; //Returns an empty value if no boxes have been checked.
  }else{
    var l = 0;
    var aValue = theValues[l];//The first checked box value.
    //Initialize the concatenated string with the first value.
    checkedText = ":" + aValue +":";
    for(l = 1; l < theValues.length; l++){
      aValue = theValues[l];
      //Concatenates the remainder of the checked boxes and separates each value with "::".
      checkedText = checkedText + ":" + aValue + ":";
    }
    return checkedText;//Returns the concatenated string.
  }
}
function prepareMultiples(){
  if(!document.getElementById) return false;
  var theForm = document.getElementById("request-form");
  theForm.onsubmit = function(){
                                var isValid = $("#request-form").valid();
                                if (isValid)
                                {
                                  multipleRewrite();
                                }
                     }
}
/**
 *
 * Add any multiple choice checkbox groups by adding their HTML name attributes
 * to theNames array in multiNames function below.
 *
 */

