
var req;
 /*
  * Get the second options by calling a Struts action
  */
function retrieveOptions()
{
	firstBox = document.getElementById('MainCategory');

  var i;

	secondBox = document.getElementById('SubCategory');
	secondBox.selectedIndex = 0;
  for( i = secondBox.options.length-1; i > 0; i--)
  {
    secondBox.remove(i);
  }
	thirdBox = document.getElementById('SubCategory2');
	thirdBox.selectedIndex = 0;
	for( i = thirdBox.options.length-1; i > 0; i--)
  {
    thirdBox.remove(i);
  }

	//Nothing selected
	if(firstBox.selectedIndex==0){
	  return;
	}
	selectedOption = firstBox.options[firstBox.selectedIndex].value;
	//get the (form based) params to push up as part of the get request
	url="http://" + document.location.hostname + "/selection/getCategories/"+selectedOption+"/1";

	//Do the Ajax call
	if (window.XMLHttpRequest){ // Non-IE browsers
	  req = new XMLHttpRequest();
	  //A call-back function is define so the browser knows which function to call after the server gives a reponse back
	  req.onreadystatechange = populateSecondBox;
	  try {
		req.open("GET", url, true); //was get
	  } catch (e) {
		 alert("Cannot connect to server");
	  }
	  req.send(null);
	} else if (window.ActiveXObject) { // IE      
	  req = new ActiveXObject("Microsoft.XMLHTTP");
	  if (req) {
		req.onreadystatechange = populateSecondBox;
		req.open("GET", url, true);
		req.send();
	  }
	}
}

function retrieveSubOptions()
{
	secondBox = document.getElementById('SubCategory');
	
	var i;

	thirdBox = document.getElementById('SubCategory2');
	thirdBox.selectedIndex = 0;
	for( i = thirdBox.options.length-1; i > 0; i--)
  {
    thirdBox.remove(i);
  }

	//Nothing selected
	if(secondBox.selectedIndex==0){
	  return;
	}
	selectedOption = secondBox.options[secondBox.selectedIndex].value;
	//get the (form based) params to push up as part of the get request
	url="http://" + document.location.hostname + "/selection/getCategories/"+selectedOption+"/1";

	//Do the Ajax call
	if (window.XMLHttpRequest){ // Non-IE browsers
	  req = new XMLHttpRequest();
	  //A call-back function is define so the browser knows which function to call after the server gives a reponse back
	  req.onreadystatechange = populateThirdBox;
	  try {
		req.open("GET", url, true); //was get
	  } catch (e) {
		 //alert("Cannot connect to server");
	  }
	  req.send(null);
	} else if (window.ActiveXObject) { // IE      
	  req = new ActiveXObject("Microsoft.XMLHTTP");
	  if (req) {
		req.onreadystatechange = populateThirdBox;
		req.open("GET", url, true);
		req.send();
	  }
	}
}
  
  //Callback function
function populateSecondBox(){

  if (req.readyState == 4) 
  { // Complete
    if (req.status == 200) 
    { // OK response
      var select = document.getElementById("SubCategory");

      if (req.responseText != "")
      {
        returnElements=req.responseText.split("#@#");
        for ( var i=0; i<(returnElements.length/2); i++ ){
          select.options[select.length] = new Option(returnElements[i*2+1], returnElements[i*2]);
        }
      }

	 }
  }
}

function populateThirdBox()
{
  if (req.readyState == 4) 
  { // Complete
    if (req.status == 200) 
    { // OK response
      var select = document.getElementById("SubCategory2");

      if (req.responseText != "")
      {
        returnElements=req.responseText.split("#@#");
        for ( var i=0; i<(returnElements.length/2); i++ ){
          select.options[select.length] = new Option(returnElements[i*2+1], returnElements[i*2]);
        }
      }

	 }
  }
}