//seperator used to seperate cars
var DATA_SEPERATOR = "#%#";

// seperator used to seperate car key-value.
var KEY_VALUE_SEPERATOR = "@@";

function keepTop10OptionSelected(){
	var make = document.getElementById('make');
	if (make.value != null){
		for (var i=1; i<=10; i++){
			if (make.options[i].value == make.value) {
				make.selectedIndex = i;
				break;
			}
		}
	}
}

//called on change of car make selection, makes AJAX call on server.
function getCarModels() {
  clearList('modelType', "Please Select Model");
  var make = document.getElementById('make').value;
  var urlPath = "car_carModels.html?needsAnalysis.car.make=" + make;
  // makes a method call for ajax.
  if (make != -1) {
    setAjaxCallDetails(urlPath, retrievedCarModels);
  } else {
    // flush all the content of carTypes if model is set to default value.
	  clearList('modelType', "Please Select Model");
  }
}

//called on change of car make selection, makes AJAX call on server.
function getCarModelsRoot() {
  clearList('modelType', "Please Select Model");
  var make = document.getElementById('make').value;
  var urlPath = "/ajaxwrapper?urlType=model&needsAnalysis.car.make=" + make;
  // makes a method call for ajax.
  if (make != -1) {
    setAjaxCallDetails(urlPath, retrievedCarModels);
  } else {
    // flush all the content of carTypes if model is set to default value.
	  clearList('modelType', "Please Select Model");
  }
}

//Used only for Pop ups - called on change of car make selection, makes AJAX call on server.
// Note : the whole of AJAX calls need to be refactored so that none of the domain objects are
// affected by any AJAX requests to the server. Sinc this will have a wide impact on ECOMM, BACKOFFICE & ROOT
// at this stage, the following method can be used by any component that is not interested in updating any session objects.
function getCarModelsPopup() {
  clearList('modelType', "Please Select Model");
  var make = document.getElementById('make').value;
  var urlPath = "car_carModels.html?make=" + make;
  // makes a method call for ajax.
  if (make != -1) {
    setAjaxCallDetails(urlPath, retrievedCarModels);
  } else {
    // flush all the content of carTypes if model is set to default value.
	  clearList('modelType', "Please Select Model");
  }
}

//called on change of car make selection, makes AJAX call on server.
function getYearsPopup() {
  clearList('year', 'Please Select Year');
  var make = document.getElementById('make').value;
  var model = document.getElementById('modelType').value;
  var urlPath = "car_carYears.html?make=" + make + "&model=" + model;

  // makes a method call for ajax.
  if (make != -1 && model != -1) {
    setAjaxCallDetails(urlPath, retrievedCarYears);
  } else {
    // flush all the content of carTypes if model is set to default value.
	  clearList('year', 'Please Select Year');
  }
}


//The AJAX - CALLBACK method which gets called when results fetched from ajax
//call.
function retrievedCarModels(xmlHttpRequest) {
	var availableCarOptions = document.getElementById('modelType');

	// actual response gets processed here.
	if (xmlHttpRequest && xmlHttpRequest.length > 0) {
		var myObject = eval('(' + xmlHttpRequest + ')');

		var retrievedModels = null;
		if (myObject.carModels.length > 0) {
			retrievedModels = myObject.carModels.split(DATA_SEPERATOR);

			for ( var carType = 0; carType < retrievedModels.length; carType++) {
			 var carKeyValuePair = retrievedModels[carType].split(KEY_VALUE_SEPERATOR);
			 var newOption = document.createElement('option');
			 newOption.value = carKeyValuePair[0];
			 newOption.text = carKeyValuePair[1];
			 var increment = carType + 1;
			 newOption.id = 'availableModelOptions_' + increment;
			 availableCarOptions.options[increment] = newOption;
			}
		}
	}

	ajaxCallComplete = true;
}

function clearList(objectId, initialText) {
	var availableCarOptions = document.getElementById(objectId);

	for(var i =availableCarOptions.length; i >=0 ; i--) {
	  availableCarOptions.options[i] = null;
	}
	//used to add the first select line in combo box.
	var initialOption = document.createElement('option');
    initialOption.text = initialText;
    initialOption.value = "-1";
    availableCarOptions.options[0] = initialOption;
}

//called on change of car make selection, makes AJAX call on server.
function getYears() {
  clearList('year', 'Please Select Year');
  var make = document.getElementById('make').value;
  var model = document.getElementById('modelType').value;
  var urlPath = "car_carYears.html?needsAnalysis.car.make=" + make + "&needsAnalysis.car.model=" + model;

  // makes a method call for ajax.
  if (make != -1 && model != -1) {
    setAjaxCallDetails(urlPath, retrievedCarYears);
  } else {
    // flush all the content of carTypes if model is set to default value.
	  clearList('year', 'Please Select Year');
  }
}

//called on change of car make selection, makes AJAX call on server.
function getYearsRoot() {
  clearList('year', 'Please Select Year');
  var make = document.getElementById('make').value;
  var model = document.getElementById('modelType').value;
  var urlPath = "/ajaxwrapper?urlType=year&needsAnalysis.car.make=" + make + "&needsAnalysis.car.model=" + model;

  // makes a method call for ajax.
  if (make != -1 && model != -1) {
    setAjaxCallDetails(urlPath, retrievedCarYears);
  } else {
    // flush all the content of carTypes if model is set to default value.
	  clearList('year', 'Please Select Year');
  }
}

//The AJAX - CALLBACK method which gets called when results fetched from ajax
//call.
function retrievedCarYears(xmlHttpRequest) {
	var availableOptions = document.getElementById('year');

	// actual response gets processed here.
	if (xmlHttpRequest && xmlHttpRequest.length > 0) {
		var myObject = eval('(' + xmlHttpRequest + ')');

		var retrievedYears = null;
		if (myObject.carYears.length > 0) {
			retrievedYears = myObject.carYears.split(DATA_SEPERATOR);
		}
		for ( var carType = 0; carType < retrievedYears.length; carType++) {

		 var carKeyValuePair = retrievedYears[carType].split(KEY_VALUE_SEPERATOR);
		 var newOption = document.createElement('option');
		 newOption.value = carKeyValuePair[0];
		 newOption.text = carKeyValuePair[1];
		 var increment = carType + 1;
		 newOption.id = 'availableYearOptions_' + increment;
		 availableOptions.options[increment] = newOption;
		}
	}
	ajaxCallComplete = true;
}


