$(document).ready(function() {	
  setUpFitmentsSection();
});

function brandChosen() {
  
  // we need to populate models list and make visible //
  
  if ( $("#brand_id").val() > 0 ) {
  
    $("#revision_id").hide();
  
    document.getElementById('model_id').options.length = 1;
  
    $.ajax({
      type: "POST",
      url: "fitmentAjax.php",
      data: "get_models=1&brand_id=" + $("#brand_id").val(),
      success: populateModelsList,
      error:   function () { alert("Internet connection currently not available!"); }
    });	
    
  } else {
  
    setUpFitmentsSection();
    
  }
}

function populateModelsList( data ) {

   var first_array = data.split("|||");
  
  if ( first_array.length > 0 ) {
    document.getElementById('model_id').options[0] = new Option( "-- MODEL --", "" ); 
    for ( i = 1; i <= first_array.length; i++ ) {
      var parts = first_array[i-1].split("xxx");
      document.getElementById('model_id').options[i] = new Option( parts[1], parts[0] );    
    }  
    $("#model_id").fadeIn();
  }
  
}

function setUpFitmentsSection() {
	$("#revision_id").hide();
	$("#model_id").hide();
	$("#brand_id").val("");
	document.getElementById('revision_id').options.length = 0;
	document.getElementById('model_id').options.length = 0;
}

function modelChosen() {
  
  // we need to populate models list and make visible //
  
  if ( $("#model_id").val() > 0 ) {
  
    document.getElementById('revision_id').options.length = 1;
  
    $.ajax({
      type: "POST",
      url: "fitmentAjax.php",
      data: "get_revisions=1&model_id=" + $("#model_id").val(),
      success: populateRevisionsList,
      error:   function () { alert("Internet connection currently not available!"); }
    });	
    
  } else {
  
    setUpFitmentsSection();
    
  }
}

function populateRevisionsList( data ) {

  var first_array = data.split("|||");
  
  if ( first_array.length > 0 ) {
    document.getElementById('revision_id').options[0] = new Option( "-- REVISION --", "" ); 
    for ( i = 1; i <= first_array.length; i++ ) {
      var parts = first_array[i-1].split("xxx");
      document.getElementById('revision_id').options[i] = new Option( parts[1], parts[0] );    
    }  
    $("#revision_id").fadeIn();
  }
  
}

function takeToBattery() {
	alert( "We have a battery that may fit your car!\n\nPlease confirm fitment by comparing dimensions, capacity, terminal layout, terminal type and any other relevant features to your existing battery.\n\nIf you have any questions, please contact us.\n\nYou will now be taken to the suggested battery." );
	window.location = $("#revision_id").val();
}