var goSelect
function updateOption(form, product_id, element) {
  var psCriteria='';
  for(i=0;i<form.length;i++) {
    if(form[i].type == 'radio' && form[i].checked)
      psCriteria += form[i].value+',';
    else if(form[i].type == 'select-one' && form[i].value) {
      document.getElementById('price_'+form[i].name).innerHTML = '';
      psCriteria += form[i].value+',';
    }
  }
  psCriteria = psCriteria.substr(0,psCriteria.length-1);
  goSelect=element;

  var strCommaDelimList=getServerData(product_id, psCriteria);
  if(!strCommaDelimList)
    form.submit();
}
function getServerData(product_id, psCriteria) {
  goSelect.disabled=true;
  document.body.style.cursor='wait';
  if(document.getElementById("add2cart")) document.getElementById("add2cart").disabled = true;

  try{
    try{
      xmlhttp=new XMLHttpRequest();
    }
    catch(e) {
      var XMLHTTP_IDS=new Array(
      'MSXML2.XMLHTTP.5.0',
      'MSXML2.XMLHTTP.4.0',
      'MSXML2.XMLHTTP.3.0',
      'MSXML2.XMLHTTP',
      'Microsoft.XMLHTTP' );
      var success=false;
      for (var i=0;i < XMLHTTP_IDS.length && !success;i++) {
        try {
        xmlhttp=new ActiveXObject(XMLHTTP_IDS[i]);
        success=true;
        } catch (e) {}
      }
      if (!success) {
        try{
        xmlhttp=new HTML_AJAX_IframeXHR();
        } catch(e) {
          alert('Sorry. We are unable to load xml file. Please try again later.');
        }
      }
    }
    if(xmlhttp==null)
      return false;

    xmlhttp.open("GET", '/lib/ajax/product-option.php?product_id='+product_id+'&options='+psCriteria,true);
    xmlhttp.onreadystatechange=function(){
      if(xmlhttp.readyState==4){
        var elName=xmlhttp.responseXML.documentElement;
        var optionEl = elName.getElementsByTagName("option");
        if(optionEl.length>0) {
          for(var i=0;optionEl[i];i++){
            var v=optionEl[i].getAttribute('value');
            var t=optionEl[i].childNodes[0].nodeValue;
            if(pr = document.getElementById('price_'+v))
              pr.innerHTML = (t==0)?'':t;
          }
        }
        if(document.getElementById("price")) document.getElementById("price").innerHTML=elName.getElementsByTagName("price")[0].childNodes[0].nodeValue;
        
        if(document.getElementById("option_qty_id")){
            if(elName.getElementsByTagName("qty")[0].childNodes[0].nodeValue !='empty'){
                document.getElementById("span_qty_id").innerHTML=elName.getElementsByTagName("qty")[0].childNodes[0].nodeValue;
                document.getElementById("option_qty_id").style.display = 'block';
            }
            else{
                document.getElementById("span_qty_id").innerHTML='';
                document.getElementById("option_qty_id").style.display = 'none';
            }
        }

        if(document.getElementById("product_img")) document.getElementById("product_img").src=elName.getElementsByTagName("file_preview")[0].childNodes[0].nodeValue;
        if(document.getElementById("product_img_large")) document.getElementById("product_img_large").href=elName.getElementsByTagName("file")[0].childNodes[0].nodeValue;

        if(priceS = document.getElementById("price-special"))
          priceS.innerHTML = elName.getElementsByTagName("price_special")[0].childNodes[0].nodeValue;
        document.body.style.cursor='auto';
        goSelect.disabled=false;
        if(document.getElementById("add2cart")) document.getElementById("add2cart").disabled = false;

        xmlhttp=null
      }
    };
    xmlhttp.send('');
  }
  catch (e) {
    alert("Server is not available at this time to process your request.");
  }
  return true;
}