//bookoutsource.js

// ######################## ###################### ##########################
//							   COMMON FUNCTIONS
// ######################## ###################### ##########################

//declare global variables
var baseurl = "/command?action=ev_orig_List";
var UNKNOWN_MAKE = "*Unknown Make";
var UNKNOWN_MODEL = "*Unknown Model";
var UNKNOWN_TRIM = "*Unknown Trim";
var NUMBER_OF_YEARS_TO_DISPLAY = 8; // only for year list, when it's not eBay, only show most recent 8 of the items
var states = document.getElementById("form023");
var years = document.getElementById("form074");
var makes = document.getElementById("form066");
var models = document.getElementById("form071");
var trims = document.getElementById("form094");

// get the type of XMLhttp object based on the browser verion
function GetXmlHttpObject()
{
  var xmlHttp=null;
  try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
  catch (e)
    {
    // Internet Explorer
    try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
      try
      	{		
      	xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      	}
      //if AJAX is not supported
      catch(e){
		  alert ("Your browser does not support AJAX!");      
		}  
      }
    }
  return xmlHttp;
}

// Make the call to AJAX based on the selected list to be populated
function callToXmlHttp(url,listName){

	xmlHttp = GetXmlHttpObject();
	if (xmlHttp==null){
		return;
	} 
	xmlHttp.open("GET",url,true);
	if(listName == "years"){
		xmlHttp.onreadystatechange=updateYearList;
	} else if(listName == "makes"){
		xmlHttp.onreadystatechange=updateMakeList;
	} else if(listName == "models"){
		xmlHttp.onreadystatechange=updateModelList;
	} else if(listName == "trims"){
		xmlHttp.onreadystatechange=updateTrimList;
	}
	xmlHttp.send(null);
}


function disableUnknowns(parentDropDownElementName, sourceDropDownElementName, sourceDropDownHiddenElementName, childDropDownElementName, parentUnknownVal, sourceUnknownVal) {

	var parentDropDownElement = document.getElementById(parentDropDownElementName);
	var sourceDropDownElement = document.getElementById(sourceDropDownElementName);
	var childDropDownElement;

	//if (childDropDownElementName != null) 
	//	childDropDownElement = document.getElementById(childDropDownElementName);

	sourceDropDownElement.focus();

	var parentIndexSelected = getSelectedIndex(parentDropDownElement,parentUnknownVal);

	if(parentDropDownElement.selectedIndex == parentIndexSelected) {
		
		sourceDropDownElement.selectedIndex = getSelectedIndex(sourceDropDownElement,sourceUnknownVal);		
		sourceDropDownElement.disabled = true;
		
		if (childDropDownElementName != null || childDropDownElementName != "") { 
			callRefreshForElement(childDropDownElementName);			
		}
		var hiddenVal = sourceDropDownElement.options[sourceDropDownElement.selectedIndex].value;
		//document.getElementById(sourceDropDownHiddenElementName).value = hiddenVal;
	} else {
		//document.getElementById(sourceDropDownHiddenElementName).value = "";
	}
}

function getSelectedIndex(dropDownElement, unknownVal) {

	var indexSelected;
	for (var i=0;i<dropDownElement.length;i++)
	{
		if (dropDownElement.options[i].text == unknownVal)
		{
			indexSelected = i;
			break;
		}
	}
	return indexSelected;
}

function callRefreshForElement(dropDownElementName) {

	if (dropDownElementName == "modelYear") {
		refreshYearList();
	} else if (dropDownElementName == "make") {
		refreshMakeList();
	} else if (dropDownElementName == "Model") {
		refreshModelList();
	} else if (dropDownElementName == "trim") {
		refreshTrimList();
	}
}

// On change of state this function would be called
function stateSelectionChange(){
	refreshYearList();
}

// On change of year this function would be called
function yearSelectionChange(){
	refreshMakeList();
}

// On change of make this function would be called
function makeSelectionChange(){
	refreshModelList();
}

// On change of model this function would be called
function modelSelectionChange(){
	refreshTrimList();
}

function getElementsForList(xmlHttp, listBox, rootElement, childElement){

		// get the root element of the response XML	
	var results = xmlHttp.responseXML.getElementsByTagName(rootElement);
	
	for(var i = 0; i < results.length; i++) {
				
		//get child elements of the response XML
		var childElementValue = results[i].getElementsByTagName(childElement);
		//call to populate the desired list box
		if(rootElement == "years"){
			populateListBox(listBox,childElementValue,false);
		} else {
			populateListBox(listBox,childElementValue,true);
		}
	}
}
//	populate the list box specfied as optionlist: get the elements 
//	tag by name <code> and <description> and put the values into option value 
//	and text
function populateListBox(optionlist, optionname, flag) {
	var option = null;
	for(var j=0;j<optionname.length;j++){	
		code = optionname[j].getElementsByTagName("code");
		var len = code.length;
		if(flag == true){
			desc = optionname[j].getElementsByTagName("description");
		}
		var ebayuserid = document.getElementById("form039a");
		if (flag == false && (ebayuserid == null || ebayuserid == "")) {
			len = NUMBER_OF_YEARS_TO_DISPLAY; // only for year list, when it's not eBay, only show most recent 8 of the items
		}
		var startpos = code.length - len;
		for (var k=startpos; k<code.length; k++){
			option = document.createElement("option");
			var optioncode = code[k].firstChild.nodeValue;
			var optiondesc = null;
			if(flag == true){
				optiondesc = desc[k].firstChild.nodeValue;
				option.value = optioncode ;
			} else {
				option.value = optioncode;
				optiondesc = optioncode;
			}
		
			option.appendChild(document.createTextNode(optiondesc));
			optionlist.appendChild(option);
		}
	} 
}


// creates the option element with value and text as unknown
function createUnknownOptionElement(optionname){
			var option = document.createElement("option");
			option.value = "Unknown";
			option.appendChild(document.createTextNode("Unknown"));
			optionname.appendChild(option);
}


// removes all the child option tags and clears the list boxes
function removeChildNodes(optionname){
	while(optionname.childNodes.length > 0) {
		optionname.removeChild(optionname.childNodes[0]);
	}
	var option = document.createElement("option");
	option.value = "";
	option.appendChild(document.createTextNode("Select One"));
	optionname.appendChild(option);
}

function disableChildElement(childElementName) {
	var childElement = document.getElementById(childElementName);
	childElement.disabled = true;
}

function enableChildElement(childElementName) {
	var childElement = document.getElementById(childElementName);
	childElement.disabled = false;
}

function emptyHiddenTagVal() {
	var trimHiddenTag = document.getElementById("trimHiddenTag");
	var modelHiddenTag = document.getElementById("modelHiddenTag");

	trimHiddenTag.value = "";
	modelHiddenTag.value = "";
}
// ######################## ###################### ##########################
//							POPULATE YEAR LIST
// ######################## ###################### ##########################
function refreshYearList() {
	var state = document.getElementById("form023").value;

	//if state is blank the year,make, model and trim listbox would also be made blank		
	if(state == "" ) {
		clearYearList();//Clears the year, make, model and trim list
		disableChildElement("form074");
		disableChildElement("form066");
		disableChildElement("form071");
		disableChildElement("form094");
		emptyHiddenTagVal();
		return;
	}

	//enableChildElement("form074");
	disableChildElement("form074");
	disableChildElement("form066");
	disableChildElement("form071");
	disableChildElement("form094");

	// set the url and parameters
	var url = baseurl + "&state=" + state + "&targetList=yearList" + "&ts=" + new Date().getTime();

	//make the call to XMLHTTP Request Object	
	callToXmlHttp(url,"years");
}

function updateYearList() {
	// This state indicates that the XMLhttp request object has returned the XML Response. 
	// It indicates that the load is complete
	if (xmlHttp.readyState==4)
	{
		// This is the status for successful http response
		if (xmlHttp.status==200){
			clearYearList(); // before loading the listboxes with the new values clear the list box
			
			// get the required option list boxes instances
			var years = document.getElementById("form074");
			var makes= document.getElementById("form066");		
			
			//document.body.style.cursor='wait';
			getElementsForList(xmlHttp, years,"years","year");
			//createUnknownOptionElement(makes);
			enableChildElement("form074");
		}
	}
}

// clear option list boxes
function clearYearList() {
	var years = document.getElementById("form074");
	var makes = document.getElementById("form066");
	var models = document.getElementById("form071");
	var trims = document.getElementById("form094");			
	removeChildNodes(years);
	removeChildNodes(makes);
	removeChildNodes(models);
	removeChildNodes(trims);	
}

// ######################## ###################### ##########################
//							POPULATE MAKE LIST
// ######################## ###################### ##########################
function refreshMakeList() {
	var state = document.getElementById("form023").value;
	var year = document.getElementById("form074").value;

	//if year is blank the make,model and trim listbox would also be made blank	
	if(year == "") {
		//var makes = document.getElementById("make");
		clearMakeList();//Clears the make, model and trim list
		disableChildElement("form066");
		disableChildElement("form071");
		disableChildElement("form094");
		emptyHiddenTagVal();
		//createUnknownOptionElement(makes); //creates an unknown element into makes listbox
		return;
	}
	
	//enableChildElement("form066");
	disableChildElement("form066");
	disableChildElement("form071");
	disableChildElement("form094");

	//set the url and pass the parameters
	var url = baseurl + "&state=" + state + "&year=" + year + "&targetList=makeList" + "&ts="  + new Date().getTime();
	
	//make the call to XMLHTTP Request
	callToXmlHttp(url,"makes");
}

function updateMakeList() {
	// This state indicates that the XMLhttp request object has returned the XML Response. 
	// It indicates that the load is complete
	if (xmlHttp.readyState==4)
	{
		// The successful response from HTTP
		if (xmlHttp.status==200){
			clearMakeList();//clears the listboxes before populating with the new values
			
			// get the list box instance 
			var makes = document.getElementById("form066");
			
			//document.body.style.cursor='wait';			 
			// get the first element from the response XML
			getElementsForList(xmlHttp, makes,"makes","make");
			//createUnknownOptionElement(makes);
			//document.body.style.cursor='default';
			enableChildElement("form066");
		}
	}
}

//clear the listboxes
function clearMakeList() {
	var makes = document.getElementById("form066");
	var models = document.getElementById("form071");
	var trims = document.getElementById("form094");	
	removeChildNodes(makes);
	removeChildNodes(models);
	removeChildNodes(trims);	
}


// ######################## ###################### ##########################
//							POPULATE MODEL LIST
// ######################## ###################### ##########################
function refreshModelList() {
	var state = document.getElementById("form023").value;
	var year = document.getElementById("form074").value;
	var make = document.getElementById("form066").value;	

	//if make is blank the model and trim listbox would also be made blank	
	if(make == "") {
		var models = document.getElementById("form071");
		var trims = document.getElementById("form094");
		removeChildNodes(models);
		removeChildNodes(trims);
		disableChildElement("form071");
		disableChildElement("form094");
		emptyHiddenTagVal();
		return;
	}
	
	//enableChildElement("form071");
	disableChildElement("form071");
	disableChildElement("form094");

	//set the url with parameters
	var url = baseurl + "&state=" + state + "&year=" + year 
				+ "&make=" + make + "&targetList=modelList" + "&ts=" + new Date().getTime();
				
	//make the call to XMLhttp based on the url				
	callToXmlHttp(url,"models");
}

function updateModelList() {
	if (xmlHttp.readyState==4)
	{
		if (xmlHttp.status==200){
			var models = document.getElementById("form071");
			var trims = document.getElementById("form094");
			removeChildNodes(models);
			removeChildNodes(trims);

			//document.body.style.cursor='wait';			 
			getElementsForList(xmlHttp, models,"models","model");
			enableChildElement("form071");
			//createUnknownOptionElement(models);
			disableUnknowns("form066","form071","modelHiddenTag","trim",UNKNOWN_MAKE, UNKNOWN_MODEL);
			//document.body.style.cursor='default';
		}
		
	}
}

function clearModelList() {
	var models = document.getElementById("form071");
	var trims = document.getElementById("form094");
	while(models.childNodes.length > 0) {
		models.removeChild(models.childNodes[0]);
	}
	while(trims.childNodes.length > 0) {
		trims.removeChild(trims.childNodes[0]);
	}
}


// ######################## ###################### ##########################
//							POPULATE TRIM LIST
// ######################## ###################### ##########################
function refreshTrimList() {
	var state = document.getElementById("form023").value;
	var year = document.getElementById("form074").value;
	var make = document.getElementById("form066").value;	
	var model = document.getElementById("form071").value;		

	//if model is blank the trim listbox would also be made blank	
	if(model == "") {
		var trims = document.getElementById("form094");
		removeChildNodes(trims);
		disableChildElement("form094");
		emptyHiddenTagVal();
		return;
	}
	
	//enableChildElement("form094");
	disableChildElement("form094");
	//set the url and parameters
	var url = baseurl + "&state=" + state + "&year=" + year 
				+ "&make=" + make + "&model=" + model + "&targetList=trimList" + "&ts=" + new Date().getTime();

	//make the call to XML http object
	callToXmlHttp(url,"trims");
}

function updateTrimList() {
	if (xmlHttp.readyState==4)
	{
		if (xmlHttp.status==200){
			var trims = document.getElementById("form094");
			removeChildNodes(trims);

			//document.body.style.cursor='wait';			 
			getElementsForList(xmlHttp, trims,"trims","trim");
			enableChildElement("form094");
			//createUnknownOptionElement(trims);
			disableUnknowns("form071","form094","trimHiddenTag",null,UNKNOWN_MODEL, UNKNOWN_TRIM);
			//document.body.style.cursor='default';
		}
	}
}

function clearTrimList() {
	var trims = document.getElementById("form094");
	while(trims.childNodes.length > 0) {
		trims.removeChild(trims.childNodes[0]);
	}
}

