
function getModels(src, all)
{
    // Find out which form was submitted
    //var target = window.event ? window.event.srcElement : e ? e.target : null;
    var target = src.form;
    if (!target) return;

    // Check if this form is already in the process of being submitted.
    // If so, don't allow it to be submitted again.
    if (target.ajaxInProgress) return;

    // Set up the request
    var xmlhttp =  new XMLHttpRequest();
    if (all) xmlhttp.open('POST', 'modelsAll_xml.php', true);
    else     xmlhttp.open('POST', 'models_xml.php', true);

    // The callback function
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4) {
            if (xmlhttp.status == 200)
                loadModels(xmlhttp.responseXML, target);
            else
                target.submit();
        }
    }

    // Send the POST request
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

    var data = '';
    var option;
    for (i = 0; i < target.elements['brand'].length; i++) {
        option = target.elements['brand'].options[i];
        if (option.selected) {
            if (data > '') data += '&';
            data += 'brand[]=' + option.value;
        }
    }
    //alert(data);
    xmlhttp.send(data);

    // Add temporary feedback that the request has been sent
    var loadingImg = document.createElement('img');
    loadingImg.src = 'http://www.vaihtoautot.net/images/working.gif';
    loadingImg.name = 'modelImg';
    target.elements['model'].style.display = 'none';
    var oldNode = target.elements['model'];
    oldNode.parentNode.insertBefore(loadingImg, oldNode);
    target.ajaxInProgress = true;

}

function loadModels(responseXML, target)
{
    /*
      var loadingImg = target.getElementsByTagName('img')[0];
      loadingImg.parentNode.removeChild(loadingImg);
    */
      var loadingImg = target.getElementsByTagName('img');
      for (i = 0; i < loadingImg.length; i++) {
          loadingImg[i].style.display = 'none';
          // loadingImg[i].parentNode.removeChild(loadingImg[i]);
      }

      var mTag = target.elements['model'];
      mTag.style.display = 'block';

      mTag.length = 0;

      // Cycle from returned results
      models = responseXML.getElementsByTagName('model');
      for (i = 0; i < models.length; i++) {
          model = models[i];
          mTag.options[mTag.length] = new Option(model.firstChild.data, model.firstChild.data);
      }

      // Free up the form to go again
      target.ajaxInProgress = false;
  }
