
  function ajax_add_to_cart(pID,check_attributes, url)
  {
    allow_add_to_cart = true;
    
    if(check_attributes==1)
    {
      if(!check_mandatory_attributes())
      {
        allow_add_to_cart = false;
      }
    }
    
    if(!check_min_product_qty(pID))
    {
      allow_add_to_cart = false;
    }

    if(allow_add_to_cart)
    {
      show_add_to_cart_process(pID);

      hide_cart_tip(pID)

      new Ajax.Updater('shopping_cart_box', url, { parameters:  $('products_form_'+pID).serialize(), asynchronous:true, evalScripts:true,
            onComplete: function(transport)
            {
              hide_add_to_cart_process(pID);
              
              //update shopping in header
              url = str_replace('ajax_add_to_cart.php','ajax_add_to_cart_in_header.php',url);
              new Ajax.Updater('shopping_cart_in_header', url, {
                onComplete: function(transport)
                {
                 show_cart_tip(pID);
                }
              });
            }
          });
    }
    
    return false;
  }
  
  function check_min_product_qty(pID)
  {
    qry = $('products_qty_'+pID).value;

    if(qry=='')
    {
      qry = 0;
    }
    
    qry = parseInt(qry);
    min_qty = parseInt($('min_products_qty_'+pID).value);
    
    if(qry<min_qty)
    {
      alert('Warning: minimum quantity '+min_qty);
      
      return false
    }
    else
    {
      if(qry/min_qty != Math.floor(qry/min_qty))
      {
        //alert("Warning: you can't purchase "+qry+"\nQty has to be divisible by the purchase units/pack qty.");
        
        correct_qty = 0;
        while(correct_qty<=(qry-min_qty))
        {
          correct_qty+=min_qty;
        }
        
        alert("Sorry, you want to order \""+qry+"\" but your must order in multiples of \""+correct_qty+"\". \nPlease revise your order.");

        return false
      }
    }
    
    //check max qty
    max_qty = parseInt($('max_products_qty_'+pID).value);
    cart_qty = parseInt($('cart_products_qty_'+pID).value);

    
    if((qry+cart_qty)>max_qty)
    {
      alert("Warning: you can't purchase more then "+max_qty+"\nContact us for pricing.");

      return false
    }
    
    
    return true;
  }

  function ajax_update_cart_product_qty(pID, action, url)
  {
   new Ajax.Updater('shopping_cart_box', url, { parameters:  {action: 'update_cart_product_qty', pID: pID, qty_action: action}, asynchronous:true, evalScripts:true,
        onComplete: function(transport)
        {
          url = str_replace('ajax_add_to_cart.php','ajax_add_to_cart_in_header.php',url);
          new Ajax.Updater('shopping_cart_in_header', url);
        }
      });
  }

  function ajax_cart_remove_product(pID, url)
  {
   new Ajax.Updater('shopping_cart_box', url, { parameters:  {action: 'remove_cat_product', pID: pID}, asynchronous:true, evalScripts:true,
        onComplete: function(transport)
        {
          url = str_replace('ajax_add_to_cart.php','ajax_add_to_cart_in_header.php',url);
          new Ajax.Updater('shopping_cart_in_header', url);
        }
   });
  }

  function show_add_to_cart_process(pID)
  {
  
    buttonX = absPosition($('button_add_to_cart_img_'+pID)).x;
    buttonY = absPosition($('button_add_to_cart_img_'+pID)).y;

    buttonY -=20;

    element = Builder.node('div',{id:'add_to_cart_process_'+pID, style: 'z-index: 1000; position: absolute; top:' + buttonY + 'px; left:'+buttonX+'px;'},[

        Builder.node('img',{src:'lib/ajax_add_to_cart/img/spinner.gif'}),

    ]);
    

    document.body.insertBefore(element, document.body.childNodes[0]);
  }
  
  function hide_add_to_cart_process(pID)
  {
    $('add_to_cart_process_'+pID).style.display = 'none';
    
    show_add_to_cart_success(pID);
  }
  
  function hide_add_to_cart_success(pID)
  {
    $('add_to_cart_success_'+pID).style.display = 'none';

  }
  
  function show_add_to_cart_success(pID)
  {

    buttonX = absPosition($('button_add_to_cart_img_'+pID)).x;
    buttonY = absPosition($('button_add_to_cart_img_'+pID)).y;

    buttonY -=20;

    element = Builder.node('div',{id:'add_to_cart_success_'+pID, style: 'position: absolute; top:' + buttonY + 'px; left:'+buttonX+'px;'},[

        Builder.node('img',{src:'lib/ajax_add_to_cart/img/success.png'}),

    ]);


    document.body.insertBefore(element, document.body.childNodes[0]);
    
    setTimeout("hide_add_to_cart_success("+pID+")",500);

  }
  
  function absPosition(obj) {
      var x = y = 0;
      while(obj) {
            x += obj.offsetLeft;
            y += obj.offsetTop;
            obj = obj.offsetParent;
      }
      return {x:x, y:y};
  }
  
  var btTimer;

  function show_cart_tip(pID)
  {

    jQuery('#button_add_to_cart_img_'+pID).bt($('cartContentTipText').value,{
      fill: '#e9e9ea',
      trigger: 'none',
      strokeStyle: '#bcbec0',
      spikeLength: 10,
      spikeGirth: 10,
      padding: 8,
      cornerRadius: 0,
      cssStyles: {
        fontFamily: '"lucida grande",tahoma,verdana,arial,sans-serif',
        backgroundImage: 'url(themes/default/images/infobox/boxContentFon.png)',
        fontSize: '11px'
      }
    });
    

    
    btTimer=setTimeout("jQuery('#button_add_to_cart_img_"+pID+"').btOn()",150);
    //

  }

  function hide_cart_tip(pID)
  {
    jQuery('#button_add_to_cart_img_'+pID).btOff();
    clearTimeout(btTimer);
  }
  
  function calculateProductPrice(pId)
  {
    price_qty_list = $('price_qty_breaks').value;

    price_qty_array = price_qty_list.split(';');

    qty = parseInt($('products_qty_'+pId).value);
    qty_break = 0;
    price_break = 0;
    
    price_notifi_str = '';
    
    for(i=0;i<price_qty_array.length;i++)
    {
      if(price_qty_array[i].length>=0)
      {

        qty_array = price_qty_array[i].split(':');
        qty_check = parseInt(qty_array[0]);

        if(qty>=qty_check && qty_check>0)
        {
          qty_break = qty_array[0];
          price_break = qty_array[1];
          
          if(price_qty_array[i+1])
          {
            qty_array2 = price_qty_array[i+1].split(':');
            qty_check2 = parseInt(qty_array2[0]);
            price_break2 = qty_array2[1]

            qty_check2_diff = qty_check2-qty;

            if(qty_check2_diff<=500 && qty_check2_diff>0)
            {
              price_notifi_str = 'Increase your order by '+qty_check2_diff+' and your total price<br> would only be $'+number_format((qty_check2*price_break2),4)+'!';
            }
          }
        }
        
        if(qty_check>0)
        {
          if($('table_price_break_'+qty_check))
          $('table_price_break_'+qty_check).removeClassName('selectedPriceQty');
        }
        

        
      }
    }
    
    min_qty = parseInt($('min_products_qty_'+pId).value);
    max_qty = parseInt($('max_products_qty_'+pId).value);
    
    if(qty<min_qty)
    {
      $('productsPriceBlock').innerHTML = 'Minimum quantity '+min_qty;
      
      return false;
    }
    
    if(qty>max_qty)
    {
      $('productsPriceBlock').innerHTML = 'Contact us for pricing.';
      
      return false;
    }
    

    if(qty_break>0)
    {
      if($('table_price_break_'+qty_break))
      $('table_price_break_'+qty_break).addClassName('selectedPriceQty');

      product_price_value = qty*price_break;
      $('productsPriceBlock').innerHTML = '<b>$'+number_format(product_price_value,4)+'</b>';
      
      $('increaseOrderQty').innerHTML = price_notifi_str;
    }
    else
    {
      $('productsPriceBlock').innerHTML = '';
      $('increaseOrderQty').innerHTML = '';
    }
  }
  
  
  function preview_product_info(pID)
  {
    jQuery('#preview_product_info_'+pID).bt($('preview_product_info_text_'+pID).value,{
      hoverIntentOpts: {
        interval: 2000,
        timeout: 2000
      },
      fill: '#ffffff',
      trigger: ['mousout', 'click'],
      strokeStyle: '#4fbde7',
      spikeLength: 10,
      spikeGirth: 10,
      padding: 8,
      cornerRadius: 0,
      width: 390,      
      closeWhenOthersOpen: true,
      cssStyles: {
        fontFamily: '"lucida grande",tahoma,verdana,arial,sans-serif',
        fontSize: '11px'
      }
    });
         

    //jQuery('#preview_product_info_'+pID).btOn();
    btTimer=setTimeout("jQuery('#preview_product_info_"+pID+"').btOn();",150);
  }
  
  function hide_preview_product_info(pID)
  {
    jQuery('#preview_product_info_'+pID).btOff();
    clearTimeout(btTimer);
  }
  
  function preview_faq_text(fID)
  {
    jQuery('#preview_faq_link_'+fID).bt($('preview_faq_text_'+fID).value,{
      hoverIntentOpts: {
        interval: 2000,
        timeout: 2000
      },
      fill: '#ffffff',
      trigger: ['mousout', 'click'],
      strokeStyle: '#4fbde7',
      spikeLength: 10,
      spikeGirth: 10,
      padding: 8,
      cornerRadius: 0,
      width: 410,
      closeWhenOthersOpen: true,
      cssStyles: {
        fontFamily: 'arial,sans-serif',
        fontSize: '13px'
      }
    });

    //jQuery('#preview_product_info_'+pID).btOn();
    btTimer=setTimeout("jQuery('#preview_faq_link_"+fID+"').btOn()",150);
  }
  
  function hide_preview_faq_text(fID)
  {
    jQuery('#preview_faq_link_'+fID).btOff();
    clearTimeout(btTimer);
  }
  
