var ajaxMM_cp = new cpaint();
// ajaxMM_cp.set_debug(1);
ajaxMM_cp.set_response_type('TEXT');

/* Nick wrote: Note on the long variable names... I did this so it wouldn't conflict with other ajax scripts.
I wish that this could have been accomplished with an object, but the cpaint library won't work that way.
*/

var ajaxMM_makeFirstText='Select a Make';
var ajaxMM_modelFirstText='Select a Model';
// These are just the defaults... you can change them in your app
var ajaxMM_makeList=document.getElementById('makeList');
var ajaxMM_modelList=document.getElementById('modelList');

function ajaxMM_createMakeDropDown() {
  ajaxMM_makeList.options[0].text='Loading...';
  ajaxMM_cp.call('/autos/ajaxMM.php', 'autoMakeList', ajaxMM_populateMakeDropDown, '');
}


function ajaxMM_createModelDropDown() {
  ajaxMM_modelList.options[0].text='Loading...';
  ajaxMM_cp.call('/autos/ajaxMM.php', 'autoModelList', ajaxMM_populateModelDropDown, ajaxMM_makeList.value);
}


function ajaxMM_populateMakeDropDown(list) {
  var reallist=eval(list);

  ajaxMM_clearDD(ajaxMM_makeList, ajaxMM_makeFirstText);
  for (i=0; i < reallist.length; i++) {
    ajaxMM_makeList.options[ajaxMM_makeList.options.length] = new Option(reallist[i].display, reallist[i].value, false, false);
  }
  
  ajaxMM_clearDD(ajaxMM_modelList, ajaxMM_modelFirstText); 
}


function ajaxMM_populateModelDropDown(list) {
  var reallist=eval(list);
  ajaxMM_clearDD(ajaxMM_modelList, ajaxMM_modelFirstText);
  for (i=0; i < reallist.length; i++) {
    ajaxMM_modelList.options[ajaxMM_modelList.options.length] = new Option(reallist[i].display, reallist[i].value, false, false);
  }
}


function ajaxMM_clearDD(field, firstValue){
  if (! field){ // Not all fields have all the drop downs.
    return false;
  }
  field.options.length=0;
  if (firstValue !==''){
    field.options[0]=new Option(firstValue, '', false, false);
  }
  return true;
}

