var http_request = false;

function makePOSTRequest(url, parameters, type, vartype) {
  http_request = false;
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
     http_request = new XMLHttpRequest();
     if (http_request.overrideMimeType) {
         // set type accordingly to anticipated content type
        //http_request.overrideMimeType('text/xml');
        http_request.overrideMimeType('text/html');
     }
  } else if (window.ActiveXObject) { // IE
     try {
        http_request = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (e) {
        try {
           http_request = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {}
     }
  }
  if (!http_request) {
     alert('Cannot create XMLHTTP instance');
     return false;
  }
  
  if (type=='models') {
    http_request.onreadystatechange = updateModels;
  } else {
    http_request.onreadystatechange = updateFaults;
  }

  http_request.open(vartype, url, true);
  http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http_request.setRequestHeader("Content-length", parameters.length);
  http_request.setRequestHeader("Connection", "close");
  http_request.send(parameters);
}

function updateModels() {
  if (http_request.readyState == 4) {
     if (http_request.status == 200) {

        targetObj = document.getElementById('model_selector');
        targetObj.options.length=0;
        targetObj[0] = new Option('', '');
        
        models = http_request.responseText;
        models = models.split('|');

        for (i = 0; i<models.length-1; i++) {  
            model = models[i].split(':');
            value = model[0];
            text = model[1];                
            targetObj[i+1] = new Option(text, value);
        }
        document.getElementById('model_selector').disabled=false;
     } else {
        alert('There was a problem with the request.');
     }
  }
}

function updateFaults() {
  if (http_request.readyState == 4) {
     if (http_request.status == 200) {
        faults = http_request.responseText;
        document.getElementById('commonfaults').innerHTML = faults;
     } else {
        alert('There was a problem with the request.');
     }
  }
}

function get_models() {
  document.getElementById('model_selector').disabled=true;
  selectedIndex = document.getElementById('make_selector').selectedIndex;
  makeId = document.getElementById('make_selector').options[selectedIndex].value;
  postStr = "make_id=" + makeId;
  makePOSTRequest('models.php', postStr, 'models', 'POST');
}

function get_text() {
  selectedIndex = document.getElementById('make_selector').selectedIndex;
  makeId = document.getElementById('make_selector').options[selectedIndex].value ;
  selectedIndex = document.getElementById('model_selector').selectedIndex;
  modelId = document.getElementById('model_selector').options[selectedIndex].value ;
  postStr = "make_id=" + makeId + "&model_id=" + modelId;
  makePOSTRequest('faults.php', postStr, 'faults', 'POST');
}

function get_text_get(makeId, modelId, pageNumber) {
  postStr = "make_id=" + makeId + "&model_id=" + modelId + "&page=" + pageNumber;
  makePOSTRequest('faults.php', postStr, 'faults', 'GET');
}