var req = false;
var vehicleClassSelection = null;
var yearSelection = null;
var makeSelection = null;
var modelSelection = null;

function populate() {
  var classBox = document.search.vehicleClass;
  classBox.options[classBox.options.length] = new Option('Choose Class', 'none');
  classBox.options[classBox.options.length] = new Option('Passenger Car', 'Passenger Car');
  classBox.options[classBox.options.length] = new Option('Pickup', 'Pickup');
  classBox.options[classBox.options.length] = new Option('Sport Utility Vehicle', 'Sport Utility Vehicle');
  classBox.options[classBox.options.length] = new Option('Van', 'Van');
}

function resetOptions(comboBox, optionText, optionValue) {
  for (i=comboBox.options.length; i >= 0; i--) {
    comboBox.remove(i);
  }
  if (optionText != null) {
    if (optionValue != null) {
      comboBox.options[comboBox.options.length] = new Option(optionText, optionValue);
    } else {
      comboBox.options[comboBox.options.length] = new Option(optionText, optionText);
    }
  }
}

function disableComponents() {
  resetOptions(document.search.year, 'Choose Year', 'none');
  resetOptions(document.search.make, 'Choose Make', 'none');
  resetOptions(document.search.model, 'Choose Model', 'none');

  document.search.year.disabled=true;
  document.search.make.disabled=true;
  document.search.model.disabled=true;
  document.getElementById('submit').disabled=true;
}

function enableComponents() {
  document.search.year.disabled=false;
  document.search.make.disabled=false;
  document.search.model.disabled=false;
  document.getElementById('submit').disabled=false;
}

function classSelected() {
  var classBox = document.search.vehicleClass;
  var yearBox = document.search.year;
  if (classBox.selectedIndex > 0) {
    yearBox.selectedIndex=0;
    classIndex = classBox.selectedIndex - 1;

    resetOptions(yearBox, 'Choose Year', 'none');
    resetOptions(document.search.make, 'Choose Make', 'none');
    resetOptions(document.search.model, 'Choose Model', 'none');
    populateYears();

    document.getElementById('submit').value='View All ' + classBox.value + 's';
    enableComponents();
  } else {
    resetOptions(document.search.year, 'Choose Year', 'none');
    resetOptions(document.search.make, 'Choose Make', 'none');
    resetOptions(document.search.model, 'Choose Model', 'none');
    document.getElementById('submit').value='Select a Car Type';
    // document.search.submit.value='Select Class';
    disableComponents();
  }
}

function populateYears() {
  classBox = document.search.vehicleClass;
  url = "/vgn-ext-templating/select?vehicleClass=" + encodeURIComponent(classBox.value);
  if (typeof XMLHttpRequest != "undefined") {
    req = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
  }
  req.open("GET", url, true);
  req.onreadystatechange = updateYears;
  req.send(null);
}

function updateYears() {
  if (req.readyState == 4 && req.status == 200) {
    yearBox = document.search.year;
    years = req.responseXML.getElementsByTagName("year");
    for (yearPos=0; yearPos < years.length; yearPos++) {
      year = years[yearPos].childNodes[0].nodeValue;
      yearBox.options[yearBox.options.length] = new Option(year, year);
      if (year == yearSelection) {
        document.search.year.selectedIndex = document.search.year.options.length - 1;
        yearSelected();
      }
    }
  }
}

function yearSelected() {
  classIndex = document.search.vehicleClass.selectedIndex - 1;
  if (document.search.year.selectedIndex > 0) {
    yearIndex = document.search.year.selectedIndex - 1;
    makeBox = document.search.make;
    resetOptions(makeBox, 'Choose Make', 'none');
    resetOptions(document.search.model, 'Choose Model', 'none');
    populateMakes();
    document.getElementById('submit').value='View All ' + document.search.year.value + ' ' + document.search.vehicleClass.value + 's';
  } else {
    resetOptions(document.search.make, 'Choose Make', 'none');
    resetOptions(document.search.model, 'Choose Model', 'none');
    document.getElementById('submit').value='View All ' + document.search.vehicleClass.value + 's';
  }
}

function populateMakes() {
  classBox = document.search.vehicleClass;
  yearBox = document.search.year;
  makeBox = document.search.make;
  url = "/vgn-ext-templating/select?vehicleClass=" + encodeURIComponent(classBox.value) + "&year=" + encodeURIComponent(yearBox.value);
  if (typeof XMLHttpRequest != "undefined") {
    req = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
  }
  req.open("GET", url, true);
  req.onreadystatechange = updateMakes;
  req.send(null);
}

function updateMakes() {
  if (req.readyState == 4 && req.status == 200) {
    makeBox = document.search.make;
    makes = req.responseXML.getElementsByTagName("make");
    for (makePos=0; makePos < makes.length; makePos++) {
      make = makes[makePos].childNodes[0].nodeValue;
      makeBox.options[makeBox.options.length] = new Option(make, make);
      if (make == makeSelection) {
        document.search.make.selectedIndex = document.search.make.options.length - 1;
        makeSelected();
      }
    }
  }
}

function makeSelected() {
  classBox = document.search.vehicleClass;
  yearBox = document.search.year;
  makeBox = document.search.make;
  modelBox = document.search.model;
  if (document.search.make.selectedIndex > 0) {
    resetOptions(modelBox, 'Choose Model', 'none');
    populateModels();
    document.getElementById('submit').value='View All ' + yearBox.value + ' ' + makeBox.value + ' ' + classBox.value + 's';
  } else {
    resetOptions(document.search.make, 'Choose Make', 'none');
    resetOptions(document.search.model, 'Choose Model', 'none');
    document.getElementById('submit').value='View All ' + yearBox.value + ' ' + classBox.value + 's';
  }
}

function populateModels() {
  classBox = document.search.vehicleClass;
  yearBox = document.search.year;
  makeBox = document.search.make;
  modelBox = document.search.model;
  url = "/vgn-ext-templating/select?vehicleClass=" + encodeURIComponent(classBox.value) + "&year=" + encodeURIComponent(yearBox.value) + "&make=" + encodeURIComponent(makeBox.value);
  if (typeof XMLHttpRequest != "undefined") {
    req = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
  }
  req.open("GET", url, true);
  req.onreadystatechange = updateModels;
  req.send(null);
}

function updateModels() {
  if (req.readyState == 4 && req.status == 200) {
    modelBox = document.search.model;
    var ids = req.responseXML.getElementsByTagName("id");
    var models = req.responseXML.getElementsByTagName("name");
    for (modelPos=0; modelPos < models.length; modelPos++) {
      id = ids[modelPos].childNodes[0].nodeValue;
      model = models[modelPos].childNodes[0].nodeValue;
      modelBox.options[modelBox.options.length] = new Option(model, id);
    }
  }
}

function modelSelected() {
  if (document.search.model.selectedIndex > 0) {
    classBox = document.search.vehicleClass;
    yearBox = document.search.year;
    makeBox = document.search.make;
    modelBox = document.search.model;

    document.getElementById('submit').value='View ' + yearBox.value + ' ' + makeBox.value + ' ' + modelBox.options[modelBox.selectedIndex].text;
  } else {
    document.getElementById('submit').value='View ' + yearBox.value + ' ' + makeBox.value + ' ' + classBox.value + 's';
  }
}

function submitForm() {
  document.search.submit();
}


function setSelectValues(vehicleClass, year, make, model) {
  vehicleClassSelection = vehicleClass.replace(/\+/g," ");
  yearSelection = year;
  makeSelection = make.replace(/\+/g," ");
  if (model != null && model != "") {
    modelSelection = model.replace(/\+/g," ");
  }
  
  classSelect = document.search.vehicleClass;
  for (i=0; i < classSelect.options.length; i++) {
    if (classSelect.options[i].text == vehicleClassSelection ||
        vehicleClassSelection.search(classSelect.options[i].text) > -1) {
      classSelect.options[i].selected = true;
      break;
    }
  }
  classSelected();
}

function populateBrands() {
  url = "/vgn-ext-templating/tireRatings?action=getBrands";
  if (typeof XMLHttpRequest != "undefined") {
    req = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
  }
  req.open("GET", url, true);
  req.onreadystatechange = updateBrands;
  req.send(null);
}

function updateBrands() {
  if (req.readyState == 4 && req.status == 200) {
    brandsBox = document.search.brand;
    var brands = req.responseXML.getElementsByTagName("brand");
    for (brandPos=0; brandPos < brands.length; brandPos++) {
      brand = brands[brandPos].childNodes[0].nodeValue;
      brandsBox.options[brandsBox.options.length] = new Option(brand, brand);
    }
  }
}


//------------------- BANNER ROTATION -------------------//
intervalInSeconds = 5;
numberOfImages = 3;
imageCapableBrowser = canManipulateImages();
currentImage = 0;

images = new Array(numberOfImages);
images[0] = "/vgn-ext-templating/safercar/img/safercar_homepage_feature.jpg";
images[1] = "/vgn-ext-templating/safercar/img/safercar_homepage_feature1.jpg";
images[2] = "/vgn-ext-templating/safercar/img/safercar_homepage_feature2.jpg";

function canManipulateImages() {
	if (document.images)
		return true;
	else
		return false;
}

function loadImages(imageURL) {
	if (imageCapableBrowser) {
		document.banner.src = imageURL;
		return false;
	}
	else {
		return true;
	}
}

function nextImage() {
  if (document.banner != null) {
    currentImage = (currentImage + 1) % numberOfImages;
    loadImages(images[currentImage]);
  }
}

setInterval("nextImage()", intervalInSeconds * 1000);

