/* miniHustle v0.1

   This script contains AJAX functions which can be used on any site/store.

   ~Michael@CommerceV3 */

// Create a new XMLHttpRequest object to talk to the Web server

// Initialize
var xmlHttp = false;

// Begin IE Code
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try {
  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
  try {
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (e2) {
    xmlHttp = false;
  }
}
@end @*/
// End IE Code

if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
  // Mozilla and every other sane browser :)
  xmlHttp = new XMLHttpRequest();
}
var pid = null;
function callServer(params,type,args) {
  pid = args; // pid is in the global spectrum..
  // Call Server using the following url:
  var url = "/services/jers/imageCheck.php";
  if(type == 'checkImage') {
    url += params;
  }
  // open connection.  usage: xmlHttp.open(METHOD,URL,ASYNC)
  xmlHttp.open("GET", url, true);
  // call a function when onreadystatechange updates
  xmlHttp.onreadystatechange = updatePage;
  // must send *something*, so send null
  xmlHttp.send(null);
}

function updatePage() {
  var response = '';
  if (xmlHttp.readyState == 4) {
    // if readState == 4 (page is complete), get responseText
    response = xmlHttp.responseText;
  }
  if(response != '') {
    handleResponse(response);
  }
}

/* End AJAX Functions */

/* Begin Site Functions */

function replaceImage(sku,color) {
  if (sku && color) {
    var img = sku + '_' + color.toLowerCase() + '.jpg';
    var params = '?i='+img;
    callServer(params,'checkImage',arguments[2]);
  }
}

function handleResponse(res) {
  if (res) {
    document.getElementById('pImage'+pid).src = res;
  }
}

/* End Site Functions */
