//adapted from SOME SITE

// declare a global  XMLHTTP Request object
var XmlHttpObj;

// create an instance of XMLHTTPRequest Object, varies with browser type, try for IE first then Mozilla
function CreateXmlHttpObj()
{
	// try creating for IE (note: we don't know the user's browser type here, just attempting IE first.)
	try
	{
		XmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XmlHttpObj = null;
		}
	}
	// if unable to create using IE specific code then try creating for Mozilla (FireFox) 
	if(!XmlHttpObj && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttpObj = new XMLHttpRequest();
	}
}

// called from onChange or onClick event of the dropdown list
function FillMenu(toplevel)
{

    var TopMenuList = document.getElementById("Testing_Request");

    // get selected option from dropdown list
    var selectedTopMenu = TopMenuList.options[TopMenuList.selectedIndex].value;

    // url of page that will send xml data back to client browser
    var requestUrl;

    // use the following line if using php
    //requestUrl = "xml_data_provider.php" + "?filter=" + encodeURIComponent(selectedContinent);

    // For a js based solution the filename comes from the topmenu value... (would be the filter to php)
    requestUrl = selectedTopMenu;

	CreateXmlHttpObj();

	// verify XmlHttpObj variable was successfully initialized
	if(XmlHttpObj)
	{
        // assign the StateChangeHandler function ( defined below in this file)
        // to be called when the state of the XmlHttpObj changes
        // receiving data back from the server is one such change
		XmlHttpObj.onreadystatechange = StateChangeHandler;

		// define the iteraction with the server -- true for as asynchronous.
		XmlHttpObj.open("GET", requestUrl,  true);

		// send request to server, null arg  when using "GET"
		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 StateChangeHandler()
{
	// 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)
		{
      Populate(XmlHttpObj.responseText);

		}
		else
		{
			alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		}
	}
}

// populate the contents of the dropdown list
// takes textfile from XMLHTTPrequest.responseText as input
function Populate(strMenu)
{

    // clear the former drop down menu (bike list)  -- A bit specific!  I should recode this more generically!
    var BikeList1 = document.getElementById("bike1");
    var BikeList2 = document.getElementById("bike2");
    var BikeList3 = document.getElementById("bike3");

    var count = 0;
	for ( count = BikeList1.options.length-1; count >-1; count--)
	{
		BikeList1.options[count] = null;
		BikeList2.options[count] = null;
		BikeList3.options[count] = null;
	}

   //split the dropdown menu from textfile stream into an array
   //not sure if this split will work on using IE on a mac???? due to poor newline handling
   var delimiter = '\n';
   var DropDownArray = new Array();
   DropDownArray = strMenu.split(delimiter);
   var numDropDownLines = DropDownArray.length;

   //make any blank or short lines into comments; strip off linebreaks
   for (count = 0; count < numDropDownLines; count++)
   {
      if (DropDownArray[count].length <= 2)
      {
        DropDownArray[count]= "//";
      }

     // kludge to fix linebreak problem with IE, which doesn't work well with \n
     // and can leave a \r at the end of line -- be nice to make js trim equivalent
     if (DropDownArray[count].charCodeAt(DropDownArray[count].length-1) < 32)
     {
        DropDownArray[count] = DropDownArray[count].substring(0,DropDownArray[count].length-1)
     }
   }
   
   //While loop to begin parsing lines into the form options
   var counter = i = 0;
        while (counter < numDropDownLines)
        {
            //skip any comment lines by incrementing counter but not option index
            if (DropDownArray[counter].substring(0,2) == "//")
            { counter++; }
            else
            {
              //set option text to submenu name from text file (first line)
              BikeList1.options[i]=new Option (DropDownArray[counter], DropDownArray[counter], false, false);
              BikeList2.options[i]=new Option (DropDownArray[counter], DropDownArray[counter], false, false);
              BikeList3.options[i]=new Option (DropDownArray[counter], DropDownArray[counter], false, false);
              counter++;
              i++;
            }
        }
        // Add the "Have the Rhomberg Report choose" option to secondary menus
        BikeList2.options[i]=new Option ("Have the Rhomberg Report choose", "Have the Rhomberg Report choose", false, false);
        BikeList3.options[i]=new Option ("Have the Rhomberg Report choose", "Have the Rhomberg Report choose", false, false);
  
        //and check the price
        CheckPrice();
}

function CheckPrice() {    
    //reset the price to $10
    document.getElementById('amount').value = "10.00";
    document.getElementById('CurrentPrice').innerHTML = "&nbsp;$10.00&nbsp;";

    //if any bikelist is set to a value containing ($20), 
    //reprogram the amount field to 20.00
    if ( document.getElementById('bike1').value.indexOf("($20)") != -1 ||
         document.getElementById('bike2').value.indexOf("($20)") != -1 ||
         document.getElementById('bike3').value.indexOf("($20)") != -1 ) 
    { 
      document.getElementById('amount').value = "20.00";
      document.getElementById('CurrentPrice').innerHTML = "&nbsp;$20.00&nbsp;";
    }   
}

