
// declare a global  XMLHTTP Request object
var XmlHttpObj;

function CreateXmlHttpObj()
{
try
  {
  // Firefox, Opera 8.0+, Safari
  XmlHttpObj=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    XmlHttpObj=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      XmlHttpObj=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
}

// ################################################################
// ### Airframe Dropdown List
// ################################################################
// called from onChange or onClick event of the airframe dropdown list
function AirframeListOnChange() 
{
 // Get selected item from dropdown list
 var itemListAirframe = document.getElementById("listAirframe");
 var selectedAirframe = itemListAirframe.options[itemListAirframe.selectedIndex].value;
    
 // url of page that will send xml data back to client browser
 var requestUrl;
 requestUrl = "ajax_stock1.asp" + "?airframe=" + encodeURIComponent(selectedAirframe);
    
	CreateXmlHttpObj();
	
	// verify XmlHttpObj variable was successfully initialized
	if(XmlHttpObj)
	{
		XmlHttpObj.onreadystatechange = StateChangeHandlerAirframe;
		
		XmlHttpObj.open("GET", requestUrl,  true);
		XmlHttpObj.send(null);		
	}
}

// this function called when state of  XmlHttpObj changes
// we're interested in the state that indicates data has been
// received from the server
function StateChangeHandlerAirframe()
{
	// state == 4 indicates receiving response data from server is completed
	if(XmlHttpObj.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttpObj.status == 200)
		{			
			PopulateListModel(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
			alert("Problem retrieving airframe model list from the server; status code: "  + XmlHttpObj.status);
		}
	}
}

// populate the contents of the model dropdown list
function PopulateListModel(itemNode)
{
 var newList = document.getElementById("listModel");
 
	// clear the new list 
	for (var count = newList.options.length-1; count >-1; count--)
	{
		newList.options[count] = null;
	}

	var newNodes = itemNode.getElementsByTagName('model');
	var idValue;
	var textValue; 
	var optionItem;
	// populate the dropdown list with data from the xml doc
	for (var count = 0; count < newNodes.length; count++)
	{
		textValue = GetInnerText(newNodes[count]);
		idValue = newNodes[count].getAttribute("id");
		optionItem = new Option(textValue, idValue,  false, false);
		newList.options[newList.length] = optionItem;
	}
 
 // Empty the part number list
 var emptyList = document.getElementById("listPartNo");
 emptyList.disabled = true
 while(emptyList.hasChildNodes()){
  emptyList.removeChild(emptyList.childNodes[0])
 }
 
 // Empty the part type list
 emptyList = document.getElementById("listPartType");
 emptyList.disabled = true
 while(emptyList.hasChildNodes()){
  emptyList.removeChild(emptyList.childNodes[0])
 }
 
 newList.disabled = false;
 newList.focus();
 
 document.getElementById("home_button_show").disabled=false;
 
}

// returns the node text value 
function GetInnerText (node)
{
	 return (node.textContent || node.innerText || node.text) ;
}

// ################################################################
// ### Model Dropdown List
// ################################################################
// called from onChange or onClick event of the model dropdown list
function ModelListOnChange() 
{
 // Determine the selected Model
 var itemListModel = document.getElementById("listModel");
 var selectedModel = itemListModel.options[itemListModel.selectedIndex].value;
 
 // Determine the selected Airframe
 var itemListAirframe = document.getElementById("listAirframe");
 var selectedAirframe = itemListAirframe.options[itemListAirframe.selectedIndex].value;
    
 // url of page that will send xml data back to client browser
 var requestUrl;
 requestUrl = "ajax_stock2.asp" + "?model=" + encodeURIComponent(selectedModel) + "&airframe=" + encodeURIComponent(selectedAirframe);
    
	CreateXmlHttpObj();
	
	// verify XmlHttpObj variable was successfully initialized
	if(XmlHttpObj)
	{
		XmlHttpObj.onreadystatechange = StateChangeHandlerModel;
		
		XmlHttpObj.open("GET", requestUrl,  true);
		XmlHttpObj.send(null);		
	}
}

// Handle list change
function StateChangeHandlerModel()
{
	// state == 4 indicates receiving response data from server is completed
	if(XmlHttpObj.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttpObj.status == 200)
		{			
			PopulateListPartType(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
			alert("Problem retrieving part type list from the server; status code: "  + XmlHttpObj.status);
		}
	}
}

// populate the contents of the part type dropdown list
function PopulateListPartType(itemNode)
{
 var newList = document.getElementById("listPartType");
 
	// clear the new list 
	for (var count = newList.options.length-1; count >-1; count--)
	{
		newList.options[count] = null;
	}

	var newNodes = itemNode.getElementsByTagName('type');
	var idValue;
	var textValue; 
	var optionItem;
	// populate the dropdown list with data from the xml doc
	for (var count = 0; count < newNodes.length; count++)
	{
		textValue = GetInnerText(newNodes[count]);
		idValue = newNodes[count].getAttribute("id");
		optionItem = new Option(textValue, idValue,  false, false);
		newList.options[newList.length] = optionItem;
	}
 
 // Empty the part number list
 var emptyList = document.getElementById("listPartNo");
 emptyList.disabled = true
 while(emptyList.hasChildNodes()){
  emptyList.removeChild(emptyList.childNodes[0])
 }
 
 newList.disabled = false;
 newList.focus();
 
 document.getElementById("home_button_show").disabled=false;
}


// ################################################################
// ### Part Type Dropdown List
// ################################################################
// called from onChange or onClick event of the part type dropdown list
function PartTypeListOnChange() 
{
 // Determine the selected Part Type
 var itemListType = document.getElementById("listPartType");
 var selectedType = itemListType.options[itemListType.selectedIndex].value;

 // Determine the selected Model
 var itemListModel = document.getElementById("listModel");
 var selectedModel = itemListModel.options[itemListModel.selectedIndex].value;
 
 // Determine the selected Airframe
 var itemListAirframe = document.getElementById("listAirframe");
 var selectedAirframe = itemListAirframe.options[itemListAirframe.selectedIndex].value;
    
 // url of page that will send xml data back to client browser
 var requestUrl;
 requestUrl = "ajax_stock3.asp" + "?model=" + encodeURIComponent(selectedModel) + "&airframe=" + encodeURIComponent(selectedAirframe) + "&parttype=" + encodeURIComponent(selectedType);
    
	CreateXmlHttpObj();
	
	// verify XmlHttpObj variable was successfully initialized
	if(XmlHttpObj)
	{
		XmlHttpObj.onreadystatechange = StateChangeHandlerPartType;
		
		XmlHttpObj.open("GET", requestUrl,  true);
		XmlHttpObj.send(null);		
	}
}

// Handle list change
function StateChangeHandlerPartType()
{
	// state == 4 indicates receiving response data from server is completed
	if(XmlHttpObj.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttpObj.status == 200)
		{			
			PopulateListPartNumber(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
			alert("Problem retrieving part number list from the server; status code: "  + XmlHttpObj.status);
		}
	}
}

// populate the contents of the part type dropdown list
function PopulateListPartNumber(itemNode)
{
 var newList = document.getElementById("listPartNo");
 
	// clear the new list 
	for (var count = newList.options.length-1; count >-1; count--)
	{
		newList.options[count] = null;
	}

	var newNodes = itemNode.getElementsByTagName('number');
	var idValue;
	var textValue; 
	var optionItem;
	// populate the dropdown list with data from the xml doc
	for (var count = 0; count < newNodes.length; count++)
	{
		textValue = GetInnerText(newNodes[count]);
		idValue = newNodes[count].getAttribute("id");
		optionItem = new Option(textValue, idValue,  false, false);
		newList.options[newList.length] = optionItem;
	}
 
 newList.disabled = false;
 newList.focus();
 
 document.getElementById("home_button_show").disabled=false;
}




// ################################################################
// ### Create an XML HTTP Object
// ################################################################
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)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}


// ################################################################
// ### Show the part hint for the home page part search field
// ################################################################
function showPartHint(str)
{
if (str.length==0)
  { 
  document.getElementById("txtHint").innerHTML="";
  document.getElementById("parthint").style.display="none";
  document.getElementById("home_button_go").disabled=true;
  return;
  }
  
xmlHttp=GetXmlHttpObject();

if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  } 

var url="ajax_getparthint.asp";
url=url+"?q="+str;
url=url+"&sid="+Math.random();

xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

// State of part no. text field has changed
function stateChanged() 
{ 
if (xmlHttp.readyState==4)
   { 
   document.getElementById("parthint").style.display="block";
   document.getElementById("txtHint").innerHTML=xmlHttp.responseText;   
   document.getElementById("home_button_go").disabled=false;   
   }
}
