var httpReq;
var targetObj;
var msgsObj;


function parseScript(_source) {
   var source = _source;
   var scripts = new Array();

   // Strip out tags
   while(source.indexOf("<script") > -1 || source.indexOf("</script") > -1) {
      var s = source.indexOf("<script");
      var s_e = source.indexOf(">", s);
      var e = source.indexOf("</script", s);
      var e_e = source.indexOf(">", e);

      // Add to scripts array
      scripts.push(source.substring(s_e+1, e));
      // Strip from source
      source = source.substring(0, s) + source.substring(e_e+1);
   }

   // Loop through every script collected and eval it
   for(var i=0; i<scripts.length; i++) {
      try {
         eval(scripts[i]);
      }
      catch(ex) {
         // do what you want here when a script fails
      }
   }
   // Return the cleaned source
   return source;
}

function createRequestObject() {
   var ro;

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

function getCoatColorNames(yFlag, cGr) {
   if(yFlag > 0) {
      targetObj = document.getElementById('colorCode').value = '';
      if(yFlag == 2) {
         document.getElementById('pcgHead').style.display = 'table-cell';
         document.getElementById('pcgCell').style.display = 'table-cell';
      } else {
         document.getElementById('pcgHead').style.display = 'none';
         document.getElementById('pcgCell').style.display = 'none';
         document.getElementById('colorGroup').selectedIndex = 0;
      }
      targetObj = document.getElementById('cPowderCoatColor');
      msgsObj = document.getElementById('msgBox');
   }

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

   sParam = encodeURIComponent("cg");
   sParam += "=";
   sParam += encodeURIComponent(cGr);
   aParams.push(sParam);

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

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

function handleResponseCN() {
   if(checkReadyStateCN()) {
      if(httpReq.responseText) {
         msgsObj.style.display = "none";
         targetObj.innerHTML = httpReq.responseText;
      }
   }
}

function getColorCode(cn) {
   if(cn.length > 0) {
      targetObj = document.getElementById('colorCode');
      msgsObj = document.getElementById('msgBox');
   }

   yFlag = 1;
   if(document.getElementById('powderCoat')[document.getElementById('powderCoat').selectedIndex].value)
      yFlag = document.getElementById('powderCoat')[document.getElementById('powderCoat').selectedIndex].value;

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

   sParam = encodeURIComponent("yf");
   sParam += "=";
   sParam += encodeURIComponent(yFlag);
   aParams.push(sParam);

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

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

function handleResponseCC() {
   if(checkReadyStateCC()) {
      if(httpReq.responseText) {
         msgsObj.style.display = "none";
         targetObj.value = httpReq.responseText;
      }
   }
}

function getRailModel(id) {
   if(id > 0) {
      targetObj = document.getElementById('railModelInfo');
      msgsObj = document.getElementById('msgBox');
   }

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

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

function checkReadyStateRM() {
   if(httpReq.readyState < 4) {
      msgsObj.innerHTML = "Loading RAIL MODEL PICS & INFO...";
      msgsObj.style.display = "block";
   } else if(httpReq.readyState == 4) {
      return (httpReq.status == 200);
   }
}

function handleResponseRM() {
   var output = '';
   if(checkReadyStateRM()) {
      if(httpReq.responseText) {
         msgsObj.style.display = "none";
         output = httpReq.responseText;
         targetObj.innerHTML = parseScript(output);
      }
   }
}

function getGateModel(gm) {
   targetObj = document.getElementById('gateModelInfo');
   msgsObj = document.getElementById('msgBoxGates');
   rmID = document.getElementById('railModel')[document.getElementById('railModel').selectedIndex].value;

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

   sParam = encodeURIComponent("gm");
   sParam += "=";
   sParam += encodeURIComponent(gm+'x42');
   aParams.push(sParam);

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

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

function handleResponseGM() {
   var output = '';
   if(checkReadyStateGM()) {
      if(httpReq.responseText) {
         msgsObj.style.display = "none";
         output = httpReq.responseText;
         targetObj.innerHTML = parseScript(output);
      }
   }
}

function nextGMPic() { currPicIndxGM++; currPicIndxGM = (currPicIndxGM >= picsGMArr.length) ? 0 : currPicIndxGM; document.getElementById("oPicPreviewGM").innerHTML = picsGMArr[currPicIndxGM]; }
function prevGMPic() { currPicIndxGM--; currPicIndxGM = (currPicIndxGM < 0) ? (picsGMArr.length-1) : currPicIndxGM; document.getElementById("oPicPreviewGM").innerHTML = picsGMArr[currPicIndxGM]; }

function nextPic() { currPicIndx++; currPicIndx = (currPicIndx >= picsArr.length) ? 0 : currPicIndx; document.getElementById("oPicPreview").innerHTML = picsArr[currPicIndx];  }
function prevPic() { currPicIndx--; currPicIndx = (currPicIndx < 0) ? (picsArr.length-1) : currPicIndx; document.getElementById("oPicPreview").innerHTML = picsArr[currPicIndx]; }