function stock_replace_callback()
{
 if(doer.readyState == 4)
    document.getElementById(stock_id).innerHTML = doer.responseText;
}

var doer = null;

function check_stock()
{
 if(show_stock === undefined || !show_stock) return;

 check_requested = false; //set flag to acknowledge the request

 if(window.XMLHttpRequest)
    doer = new XMLHttpRequest();
 else if(window.ActiveXObject)
    doer = new ActiveXObject("Microsoft.XMLHTTP");
 else {
    document.getElementById(stock_id).innerHTML = document.getElementById('form_stock_check').innerHTML;
    show_stock = false;
   }

 var selections = '';
 document.getElementById(stock_id).innerHTML = "Checking...";
 
 //id="' . 'attrib-' . $products_options_names->fields['products_options_id'] . '"'
 for(i=0;i<document.forms.length;++i) {
    frm = document.forms[i];
    for(j=0;j<frm.elements.length;++j) {
       var item = frm.elements[j];
       var t_id = item.id;
       var there = t_id.indexOf("attrib-");
       if(there != -1) {
          if(item.type == "radio" && !item.checked) continue;
          selections += "&" + item.name + "=" + encodeURI(item.value);
         }
      }
   }
 
 doer.onreadystatechange = stock_replace_callback;
 doer.open('GET',checker_url + "?products_id=" + products_id + selections, true);
 doer.send(null);
}

var check_requested = false;

function check_stock_onclick()
{
 //Since some browsers don't trigger onchange right away (if at all) this runs to make sure that it still happens.
 check_requested = true;
 setTimeout("if(check_requested) {check_stock();}",100);
}


function check_stock_setup()
{
 for(i=0;i<document.forms.length;++i) {
    frm = document.forms[i];
    for(j=0;j<frm.elements.length;++j) {
       var item = frm.elements[j];
       var t_id = item.id;
       var there = t_id.indexOf("attrib-");
       if(there != -1) {
          if(item.type == "radio" || item.type == "checkbox")
             item.onclick = check_stock_onclick;
          item.onchange = check_stock;
         }
      }
   }
 check_stock();
}