/**
 * enables/disables inputs
 * @pInputs Array of Strings representing IDs ofinputs to toggle
 */
function toggleInputs(pInputs) {
  if (pInputs && pInputs[0]) {
    var anInput;
    for (var i = 0; i < pInputs.length; i++) {
      anInput = document.getElementById(pInputs[i]);
      if (anInput) {
        anInput.disabled = !anInput.disabled;
      }
    }
  }
}

/**
 * Java script to update selections
 * @param frm Form
 * @param src source select box
 * @param dest destination multiple selection box
 */
function updateModels(frm, src, dest) {
  var carline=frm[src];
  var theArray = models[carline.selectedIndex];
  modelSelector = frm[dest];
  modelSelector.options.length = theArray.length;
  for (i=0; i<theArray.length; i++) {
    var op = new Option(theArray[i][1], theArray[i][1]);
    if (i==0) op.selected = true;
    modelSelector.options[i] = op;
  }
}

/**
 * fills dependent select box with values form model data (pModelLines) according to selection at pSel
 * @param pSel id of select box for modelline selection
 * @param pDepSel id of dependent  select box, displaying models
 * @param pModelLines array containing modellines, models
 */
function fillDependentDropdown(pSel, pDepSel, pModelLines) {
  var sel = document.getElementById(pSel);
  var depSel = document.getElementById(pDepSel);
  if (sel && depSel && pModelLines && sel.selectedIndex > -1) {
    var selectedModelLine = sel.options[sel.selectedIndex].value;

    var models = pModelLines[selectedModelLine];

    // if no model line selected, offer all models
    if (selectedModelLine.length == 0) {
      models = new Array();
      var modelLine
      for (var i = 0; i < sel.options.length; i++) {
        modelLine = sel.options[i].value
        if (modelLine.length != 0) {
          models = models.concat(pModelLines[modelLine]);
        }
      }
    }

    // clear all bodystyles
    for (var i = depSel.options.length -1; i >= 0; i --) {
      depSel.options[i] = null;
    }

    for (var i = 0; i < models.length; i ++) {
      depSel.options[i] = new Option(models[i],models[i], false, false);
    }
  }
}

/**
 * transfers selected models from model select box to 3 (hidden) inputs storing / carrying them to backend
 * @param pModelSel id of select box models can be chosen from
 * @param pModelStore1 id of store input 1
 * @param pModelStore2 id of store input 2
 * @param pModelStore3 id of store input 3
 */
function storeModelSelection(pModelSel, pModelStore1, pModelStore2, pModelStore3) {
  var sel = document.getElementById(pModelSel);
  var store1 = document.getElementById(pModelStore1);
  var store2 = document.getElementById(pModelStore2);
  var store3 = document.getElementById(pModelStore3);
  if (sel && store1 && store2 && store3) {

    store1.value = '';
    store2.value = '';
    store3.value = '';

    var storeCount = 1;
    for (var i = 0; i < sel.options.length; i++) {
      if (sel.options[i].selected && storeCount <= 3) {
        eval('store' + storeCount++).value = sel.options[i].value;
      }
    }
  }
}

/**
 * (pre-)selects options of select box matching params
 * @param pSel select to have its entries restored
 * @param pParams params to match with select entries
 */
function restoreDependentDropdown(pSel, pParams) {
  var sel = document.getElementById(pSel);
  if (sel && pParams) {
    var optVal;
    var param;
    for (var i = 0; i < sel.options.length; i++) {
      for (var j = 0; j < pParams.length; j++) {
        optVal = sel.options[i].value
        param = pParams[j];
        if (optVal.length > 0 &&param.length > 0 && optVal.indexOf(param) > -1 && param.length == optVal.length) {
          sel.options[i].selected = true;
        }

      }
    }
  }
}

/**
 * Restrict a form object to a certain length.
 * @param pObj Form object to restrict
 */
function isMaxLength(pObj){
  var mlength = pObj.getAttribute ? parseInt(pObj.getAttribute("maxlength")) : "";
  if (pObj.getAttribute && pObj.value.length>mlength) {
    pObj.value = pObj.value.substring(0,mlength);
  }
}

/**
 * Reset the given selection control to the given value
 * @param pFormName
 * @param pSelectionName
 * @param pValue
 */
function resetSelection(pFormName, pSelectionName, pValue) {
  var value = (pValue && pValue != -1) ? pValue : "";
  var selection = document.forms[pFormName][pSelectionName];
  for (var i = 0; i < selection.options.length; i++) {
    var option = selection.options[i];
    option.selected = (option.value == value);
  }
}
/**
 * Reset the given selection control to the given values
 * @param pFormName
 * @param pSelectionName
 * @param pValues Array of values
 */
function resetSelectionToValues(pFormName, pSelectionName, pValues) {
  var selection = document.forms[pFormName][pSelectionName];
  for (var i = 0; i < selection.options.length; i++) {
    var option = selection.options[i];
    option.selected = false;
    for (var j = 0; j < pValues.length; j++) {
      if (option.value == pValues[j]) {
        option.selected = true;
        break;
      }
    }
  }
}

/**
 * Remove all options from the given select control.
 * @param pFormName
 * @param pSelectionName
 */
function clearSelection(pFormName, pSelectionName) {
  var selection = document.forms[pFormName][pSelectionName];
  for (var i = selection.options.length; i >= 0; i--) {
    selection.options[i] = null;
  }
}

/**
 * Limit a multiselection control to given number of selections.
 * @param pSelectControl Selection Control to limit
 * @param pLimit Selection limit
 */
function limitMultiSelect(pSelectControl, pLimit) {
  var counter = 0;
  for (var i = 0; i < pSelectControl.length; i++) {
    if (pSelectControl[i].selected) {
      counter++;
      if (counter > pLimit) {
        pSelectControl[i].selected=false;
        counter--;
      }
    }
  }
}

function resetRadioGroup(pFormName, pGroupName, pValue) {
  var value = pValue ? pValue : "";
  var radioGroup = document.forms[pFormName][pGroupName];
  for (var i = 0; i < radioGroup.length; i++) {
    radioGroup[i].checked = (radioGroup[i].value == pValue);
  }
}

/**
 * Function for populating the BodyStyle selection.
 * @param pFormName Form name
 * @param pSelectName Select control name
 * @param pSelectedIndex Index of selected ModelLine
 * @param pArray Array with available styles
 * @param pSelectedStyles Array with selected styles
 */
function fillSelectFromArray(pFormName, pSelectName, pSelectedIndex, pArray, pSelectedStyles) {

  clearSelection(pFormName, pSelectName);
  var selectCtrl = document.forms[pFormName][pSelectName];
  if (pArray[pSelectedIndex] != null) {
    for (var i = 0; i < pArray[pSelectedIndex][1].length; i++) {
      if (pArray[pSelectedIndex][1][i] != null) {
        var style = pArray[pSelectedIndex][1][i];
        selectCtrl.options[i] = new Option(style[0],style[1],false,false);
        selectCtrl.options[i].selected = false;
        for (var j = 0; pSelectedStyles && j < pSelectedStyles.length; j++) {
          if (selectCtrl.options[i].value == pSelectedStyles[j]) {
            selectCtrl.options[i].selected = true;
          }
        }
      }
    }
  }
}

/**
 * Functions for intermediate animation.
 */
function timeImgs(pNum) {
  thetimer = setTimeout("imgTurn('" + pNum + "')", 350);
}

function imgTurn(pNum) {
  if (document.images) {
    if (pNum == "16") {
      document["progress"].src = eval("progress16.src");
      timeImgs('1');
    } else {
      document["progress"].src = eval("progress" + pNum + ".src");
      timeImgs(pNum = ++pNum);
    }
  }
}

function showProgress() {
  document.getElementById("main").style.visibility="hidden";
  document.getElementById("searchLoading").style.visibility="visible";
  var msgbox=document.getElementById("msg");
  if (msgbox!=undefined) {
    msgbox.style.visibility="hidden";
  }
  timeImgs('2');
  scroll(0,0);
}

/**
 * Helper method for displaying carline group in multi purpose flash.
 * The flash update call is called asynchron and delayd 0.5 sec, to avoid problems with quick hovering.
 */
var aoa_multipurposeflash_setCarlineGroup_carline = null;
function aoa_multipurposeflash_setCarlineGroup(pCarlineGroup) {
  aoa_multipurposeflash_setCarlineGroup_carline = pCarlineGroup.toString();
  window.setTimeout("aoa_multipurposeflash_setCarlineGroup_asynchcall()", 500);
}
function aoa_multipurposeflash_setCarlineGroup_asynchcall() {
  if (aoa_multipurposeflash_setCarlineGroup_carline==null) {
    return;
  }
  var carline = aoa_multipurposeflash_setCarlineGroup_carline;
  aoa_multipurposeflash_setCarlineGroup_carline = null;
  var movie = window.document.stage_flash;
  if (movie) {
    movie.SetVariable("_root.carlineGroup", carline);
  }
}

function aoa_multipurposeflash_setZipCode_onload() {
  if (typeof aoa_multipurposeflash_setZipCode == "function") {
    aoa_multipurposeflash_setZipCode();
  }
}
