
var request = null;

/* import this into any file to use Ajax XMLHttpRequests
   params:
   reqType - GET or POST
   url - of servlet that will return data
   asynch - true or false
   respHandle - function that will handle the response
   5th param is optional and should contain the data a POST request is supposed to send
*/
function httpRequest(reqType,url,asynch,respHandle){
   try {
     request = new XMLHttpRequest();
   } catch (trymicrosoft) {
     try {
       request = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (othermicrosoft) {
       try {
         request = new ActiveXObject("Microsoft.XMLHTTP");
       } catch (failed) {
         request = false;
       }
     }
   }

	if (request){
		if(reqType.toLowerCase() != "post"){
			initReq(reqType,url,asynch,respHandle);
		} else {
			var args = arguments[4];
			if (args != null && args.length > 0){
				initReq(reqType,url,asynch,respHandle,args);
			}
		}
		
	} else {
        alert("Error initializing XMLHttpRequest!");
	}
	
}


function initReq(reqType, url, bool, respHandle){
	try{
	request.onreadystatechange = respHandle;
	request.open(reqType,url,bool);
	if (reqType.toLowerCase() == "post"){
			request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
		request.send(arguments[4]);
	} else {
		request.send(null);
	}
	} catch(errv){
		alert("The application cannot contact the server at the moment.\n"+
			 "Please try again in a few seconds.\n" + 
			  "Error detail:" + errv.message);
	}
}
   function getPriceSchemes() {
     //var service = document.getElementById("service_id").value;
     var dbName = "";
     var dbNameElement = document.getElementById("dbname");
     if (dbNameElement != null)
     dbName = dbNameElement.value;
     var url = "/servlet/thetamusic.ymds.server.admin.tools.AjaxServlet?action=getPriceSchemeInfo&dbname=" + dbName;
     request.open("GET", url, true);
     request.onreadystatechange = updatePage;
     request.send(null);
   }

   function updatePage() {
     if (request.readyState == 4) {
       if (request.status == 200) {
      response = request.responseText.split("|");
      updatePriceSchemes(document.getElementById("service_id").value);
       }
  }
   }

function updatePriceSchemes(selectedPrice){

  //var response = request.responseText.split("|");
   for (var i=0; i<response.length; i++) {
    var battalion = response[i];
    var unit = battalion.split("%");
    var service_id = unit[0];
    var service_options = unit[1];
    var optionList = document.getElementById("price_scheme");
    if (service_id == selectedPrice){
      ClearOptions(optionList);
      var eachone = service_options.split(",");
      if (eachone.length > 2){
        AddToOptionList(optionList,0, "All Price Schemes");
      }
      for (var j=0;j<eachone.length;j+=2){
        AddToOptionList(optionList, eachone[j+1], eachone[j]);
      }
    }
  }

 	// each section of response
  if (selectedPrice == 0 || optionList.length == 0){
    var optionList = document.getElementById("price_scheme");
    ClearOptions(optionList);
    AddToOptionList(optionList,0, "All Price Schemes");
  }
  // set price scheme to last set price scheme
  document.getElementById("price_scheme").value = document.getElementById("last_price_scheme_id").value;
}


function ClearOptions(OptionList) {
   for (x = OptionList.length; x >= 0; x--) {
      OptionList[x] = null;
   }
}


function AddToOptionList(OptionList, OptionValue, OptionText) {
   // Add option to the bottom of the list
  ot = new String(OptionText);
  ov = new String(OptionValue);
  ot.replace('"','');
  ov.replace('"','');
   OptionList[OptionList.length] = new Option(ot, ov);
}