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){
       $('#price_' + form[i].name).html('');
       psCriteria += form[i].value+',';
    }
  }
  psCriteria = psCriteria.substr(0, psCriteria.length-1);
  goSelect = element;
  
  document.body.style.cursor='wait';
  $(goSelect).attr('disabled', 'disabled');
  $("#add2cart").attr('disabled', 'disabled');
  
  if(element != '')
     var elem_value = element.value
  else
     var elem_value = '';
     
  if($('#qty').length > 0)
     var qty = $('#qty').val(); 
  else
     var qty = ''; 
  
  $("option", "table.product_attribute").each(function(){$(this).attr('disabled', false)});   
  $("input[type='radio']", "table.product_attribute").each(function(){$(this).parent().parent().parent().show()});
  
  $.get('/lib/ajax/product-option-conflict.php', {product_id:product_id, option_value:psCriteria},
     function(data){
        $(data).find("option").each(function() {
           $("option[value="+$(this).text()+"]", "table.product_attribute").attr('selected', false).attr('disabled', 'disabled'); 
          
           $("input[value="+$(this).text()+"]",  "table.product_attribute").parent().parent().parent().hide();
           $("input[value="+$(this).text()+"]",  "table.product_attribute").attr('checked', false);
        });

        $.get('/lib/ajax/product-option.php', {product_id:product_id, productQty:qty, options:psCriteria, currentOptionValue:elem_value},
           function(data){
                var optionEl = data.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 = $('#price_' + v))
                       $(pr).html((t==0)?'':t);
                  }
                }
                $("#price").html(data.getElementsByTagName("price")[0].childNodes[0].nodeValue);
                
                if($("#option_qty_id").length > 0){
                   if(data.getElementsByTagName("qty")[0].childNodes[0].nodeValue !='empty'){
                      $("#span_qty_id").html(data.getElementsByTagName("qty")[0].childNodes[0].nodeValue);
                      $("#option_qty_id").show();
                   }
                   else{
                       $("#span_qty_id").html('');
                       $("#option_qty_id").hide();
                   }
                }

                if($("#product_img").length > 0 && data.getElementsByTagName("file_preview")[0]) 
                   $("#product_img").attr("src", data.getElementsByTagName("file_preview")[0].childNodes[0].nodeValue);

                if($("#product_img_large").length > 0 && data.getElementsByTagName("file")[0]) 
                   $("#product_img_large").attr("href", data.getElementsByTagName("file")[0].childNodes[0].nodeValue);

                if($("#price-special").length > 0)
                   $("#price-special").html(data.getElementsByTagName("price_special")[0].childNodes[0].nodeValue);
                document.body.style.cursor='auto';
                
                 $(goSelect).attr('disabled', '');
                 $("#add2cart").attr('disabled', '');
              });
           }
        ); 
}
