var retrieveURLCallback;
function retrieveURL(url, element_id, cb_func) {
  var req;
  var elementId;
  if (window.XMLHttpRequest) { // Non-IE browsers
    elementId = element_id;
    req = new XMLHttpRequest();
    req.onreadystatechange = processStateChange;
    req.cb_func = cb_func;
    try {
      req.open("GET", url, true);
    } catch (e) {
      alert(e);
    }
    req.send(null);
  } else if (window.ActiveXObject) { // IE
    elementId = element_id;
    req = new ActiveXObject("Microsoft.XMLHTTP");
    if (req) {
      req.onreadystatechange = processStateChange;
      retrieveURLCallback = cb_func; // *** may break overlapping invocations, but MS does not support custom properties on xmlhttprequest object
      req.open("GET", url, true);
      req.send();
    }
  }
  function processStateChange() {
    if (req.readyState == 4) { // Complete
      if (req.status == 200) { // OK response
        //alert(req.responseText); // *** debug
        document.getElementById(elementId).innerHTML = req.responseText;
        if (req.cb_func) req.cb_func();
        else if (retrieveURLCallback) retrieveURLCallback();
      } else {
        alert("Problem: " + req.statusText);
      }
      if (retrieveURLCallback) retrieveURLCallback = "";
    }
  }
}
function retrieveURLPost(url, element_id, vars) {
  var req;
  var elementId;
  if (window.XMLHttpRequest) { // Non-IE browsers
    elementId = element_id;
    req = new XMLHttpRequest();
    req.onreadystatechange = processStateChange;
    try {
      req.open("POST", url, true);
    } catch (e) {
      alert(e);
    }
    req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
    req.send(vars);
  } else if (window.ActiveXObject) { // IE
    elementId = element_id;
    req = new ActiveXObject("Microsoft.XMLHTTP");
    if (req) {
      req.onreadystatechange = processStateChange;
      req.open("POST", url, true);
      req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
      req.send(vars);
    }
  }
  function processStateChange() {
    if (req.readyState == 4) { // Complete
      if (req.status == 200) { // OK response
        // alert(req.responseText); // *** debug
        document.getElementById(elementId).innerHTML = req.responseText;
      } else {
        alert("Problem: " + req.statusText);
      }
    }
  }
}
function makeSizeDivByColorId(colorId, divId) {
  // Grab the size select box for this color
  retrieveURL('/ajax/ajax_make_size_sel.php?color_id='+colorId, divId);
  return;
}
function showAjaxPopup(id, url) {
	var element = document.getElementById(id);
	element.style.visibility = "visible";
	element.innerHTML = "<span class=\"verdana12BLACKbold\" id=\"popup_content"+id+"\"><img src=\"js/loading.gif\" border=\"0\">Loading...</span>";
	retrieveURL(url, "popup_content"+id);
}
function hideAjaxPopup(id) {
	document.getElementById(id).style.visibility = "hidden";
}
function updateCartPrices(ckey) {
 // Get the old total price
 if (ckey > 0) var otprice = document.getElementById('c'+ckey+'tprice').innerHTML;
 // Get the  subtotal
 var stotal = document.getElementById('stotal').innerHTML;
 // Get the shipping cost
 var ship_cost = document.getElementById('ship_cost').innerHTML;
 // Get rid of any commas
 ship_cost = ship_cost.replace(',','');
 ship_cost = parseFloat(ship_cost);
 // Update the item total price
 var gtotal = document.getElementById('gtotal').innerHTML;
 // Get rid of any commas
 gtotal = gtotal.replace(',','');
 gtotal = parseFloat(gtotal);
 // Update the total price for this item
 if (ckey > 0) {
   var qty = document.getElementById('c'+ckey+'qty').value; 
   var uprice = document.getElementById('c'+ckey+'uprice').innerHTML;
   var tprice = qty*uprice;   
   uprice = uprice.replace(',','');
   document.getElementById('c'+ckey+'tprice').innerHTML = tprice.toFixed(2);
   // If the new price is lower, subtract from the total, else, add to the total
   if(tprice > otprice) {
     gtotal += tprice-parseFloat(otprice);
   } else {
     gtotal -= parseFloat(otprice)-tprice;
   }
 }
 stotal = gtotal;
 gtotal += ship_cost;
 // Add the shipping charges to the grand total
 document.getElementById('ship_cost').innerHTML = addCommas(ship_cost.toFixed(2));
 // Now apply any discounts/shipping charges
 document.getElementById('stotal').innerHTML = addCommas(stotal.toFixed(2));
 document.getElementById('gtotal').innerHTML = addCommas(gtotal.toFixed(2));
}
function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}
-->
