var httpReq;
var trgtObj;
var prcObj;
var msgsObj;

function createRequestObject() {
   var ro;

   if (window.XMLHttpRequest) {
      ro = new XMLHttpRequest();
   } else {
      if (window.ActiveXObject) {
         ro = new ActiveXObject("Microsoft.XMLHTTP");
      }
   }
   return ro;
}

function getData() {
   trgtObj = document.getElementById('qDiv');
   prcObj = document.getElementById('prcTxt');
   msgsObj = document.getElementById('msgBox');
   var catID = document.getElementById('category').value;
   var subCatID = document.getElementById('subcategory').value;
   var itemID = document.getElementById('itemid').value;
   var flg = document.getElementById('flag').value;

   httpReq = createRequestObject();
   var aParams = new Array();
   var sParam = encodeURIComponent("itemid");
   sParam += "=";
   sParam += encodeURIComponent(itemID);
   aParams.push(sParam);

   sParam = encodeURIComponent("flag");
   sParam += "=";
   sParam += encodeURIComponent(flg);
   aParams.push(sParam);

   if(catID == 17) {
      sParam = encodeURIComponent("captexture");
      sParam += "=";
      sParam += encodeURIComponent(document.getElementById('captexture')[document.getElementById('captexture').selectedIndex].value);
      aParams.push(sParam);

      sParam = encodeURIComponent("capthickness");
      sParam += "=";
      sParam += encodeURIComponent(document.getElementById('capthickness')[document.getElementById('capthickness').selectedIndex].value);
      aParams.push(sParam);
   }

   if(subCatID == 54 || (flg == 1 && catID == 2)) {
      sParam = encodeURIComponent("dkFraming");
      sParam += "=";
      sParam += encodeURIComponent(document.getElementById('dkFraming')[document.getElementById('dkFraming').selectedIndex].value);
      aParams.push(sParam);

      sParam = encodeURIComponent("dkType");
      sParam += "=";
      sParam += encodeURIComponent(document.getElementById('dkType')[document.getElementById('dkType').selectedIndex].value);
      aParams.push(sParam);

      sParam = encodeURIComponent("dkColor");
      sParam += "=";
      sParam += encodeURIComponent(document.getElementById('dkColor')[document.getElementById('dkColor').selectedIndex].value);
      aParams.push(sParam);

      sParam = encodeURIComponent("dkHinges");
      sParam += "=";
      sParam += encodeURIComponent(document.getElementById('dkHinges')[document.getElementById('dkHinges').selectedIndex].value);
      aParams.push(sParam);

      sParam = encodeURIComponent("dkHrdwrKit");
      sParam += "=";
      sParam += encodeURIComponent(document.getElementById('dkHrdwrKit')[document.getElementById('dkHrdwrKit').selectedIndex].value);
      aParams.push(sParam);
   }

   if(catID == 10 || subCatID == 79 || (flg == 1 && catID == 4)) {
      sParam = encodeURIComponent("rollformColor");
      sParam += "=";
      sParam += encodeURIComponent(document.getElementById('rollformColor')[document.getElementById('rollformColor').selectedIndex].value);
      aParams.push(sParam);

      sParam = encodeURIComponent("rollformTexture");
      sParam += "=";
      var rollformTexture = (itemID == 170 || itemID == 171) ? 'EMBOSSED' : ((document.getElementById('rollformTexture')) ? document.getElementById('rollformTexture')[document.getElementById('rollformTexture').selectedIndex].value : '');
      sParam += encodeURIComponent(rollformTexture);
      aParams.push(sParam);

      sParam = encodeURIComponent("rollformEndCut");
      sParam += "=";
      sParam += encodeURIComponent(((document.getElementById('rollformEndCut')) ? document.getElementById('rollformEndCut')[document.getElementById('rollformEndCut').selectedIndex].value : ''));
      aParams.push(sParam);

      sParam = encodeURIComponent("rollformRibs");
      sParam += "=";
      sParam += encodeURIComponent(((document.getElementById('rollformRibs')) ? document.getElementById('rollformRibs')[document.getElementById('rollformRibs').selectedIndex].value : ''));
      aParams.push(sParam);
   }

   sBody = aParams.join("&");
   url = "get_cust_inv_items.html";
   httpReq.open("POST", url, true);
   httpReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
   httpReq.onreadystatechange = handleResponse;
   httpReq.send(sBody);
}

function checkReadyState() {
   if(httpReq.readyState < 4) {
      msgsObj.style.display = "block";
   } else if(httpReq.readyState == 4) {
      return (httpReq.status == 200);
   }
}

function handleResponse() {
   if(checkReadyState()) {
      if(httpReq.responseText) {
         msgsObj.style.display = "none";
         var fromAJAX = httpReq.responseText.split('|');
         trgtObj.innerHTML = fromAJAX[1];
         prcObj.innerHTML = fromAJAX[0];
         prcObj.style.display = 'block';
      }
   }
}
