function _filterOutOfStockAttributes(el) {
    //alert(matrixAttributeGroupControl);
    var matrixGroupProduct = el;
    var matrixAttributeControls = matrixAttributeGroupControl.attrControls();    
    
                var lowPrice = 0;
                var highPrice = 0;
        
        for (var ctr=0; ctr<matrixAttributeControls.length; ctr++) { 
        
            var ddl = matrixAttributeControls[ctr].ctrl;
            
            ddl.selectedIndex = 0;
        
            if ( ctr >0 ) {
                                
                    disableOptions( ddl );
                    $(ddl).attr('disabled',true); // disable it
                
            } else {
            
                // first control is our key
                // disableOptions( ddl );
                
                                
                for (var ctr1=0; ctr1<matrixGroupProduct.matrixProducts.length; ctr1++) {
                
                    var matrixItem = matrixGroupProduct.matrixProducts[ctr1];                    
                    
                    //if (matrixItem.currentUnitMeasure.freeStock > 0) {
                    
                        var price = matrixItem.currentUnitMeasure.price;
                        
                        if (price < lowPrice || lowPrice == 0) {
                            lowPrice = price;
                        }
                        
                        if (price > highPrice || highPrice == 0) {
                            highPrice = price;
                        }
                        
                        // fill attribute drop down with valid options
                        var attributes = matrixItem.getAttributes();
                                            
                        if (attributes.length>0) {
                        
                            var val = attributes[0].value;
                    
                            enableOption( ddl, val );
                        }
                    //}
                }
            } 
            
            if ( lowPrice>0 && highPrice>0 && lowPrice != highPrice ) {
                var pricingControlId = 'lblPrice_' + matrixGroupProduct.id;
	            lowPrice = CurrencyFormatted(lowPrice);
	            highPrice = CurrencyFormatted(highPrice);
                document.getElementById( pricingControlId ).innerHTML = '$ ' + lowPrice + ' - $ ' + highPrice;   
            }
            else if(  lowPrice>0 && highPrice == 0 )
            {
               var pricingControlId = 'lblPrice_' + matrixGroupProduct.id;
               lowPrice = CurrencyFormatted(lowPrice);
               document.getElementById( pricingControlId ).innerHTML = '$ ' + lowPrice;
            }
            else if(  lowPrice==0 && highPrice > 0 )
            {
               var pricingControlId = 'lblPrice_' + matrixGroupProduct.id;
               highPrice = CurrencyFormatted(highPrice);
               document.getElementById( pricingControlId ).innerHTML = '$ ' + highPrice;
            }
            else if(  lowPrice==highPrice )
            {
               var pricingControlId = 'lblPrice_' + matrixGroupProduct.id;
               lowPrice = CurrencyFormatted(lowPrice);
               document.getElementById( pricingControlId ).innerHTML = '$ ' + lowPrice;
            }
            
                  
        }    
}

function CurrencyFormatted(amount) 
{

   var i = parseFloat(amount); 
   if(isNaN(i)) { i = 0.00; } 
   var minus = ''; 
   if(i < 0) { minus = '-'; } 
   i = Math.abs(i);

   i = parseInt((i + .005) * 100);

   i = i / 100;

   s = new String(i); 

   if(s.indexOf('.') < 0) { s += '.00'; } 

   if(s.indexOf('.') == (s.length - 2)) { s += '0'; } 
   s = minus + s;
   return s; 
}

function disableOptions( ddl ) {
	$(ddl).children(':gt(0)').each(function() {
		 $(this).optionDisable(); 
	});
}
function enableOption( ddl, val ) {
    $('#' + val).optionEnable();
}
function _onAttributeChanged(el) { 

    var matrixGroupProduct = matrixAttributeGroupControl.product;
    var matrixAttributeControls = matrixAttributeGroupControl.attrControls();  
    
    if ( matrixAttributeControls.length>0 && matrixAttributeControls[0].ctrl == el ) {
        // key changed lets update the children
                
        var newAttrCode = el.options[0].value;
        var newAttrValue = el.options[el.selectedIndex].value;
        
        for (var ctr=1; ctr<matrixAttributeControls.length; ctr++) { // start on 1 since 0 is the index of our key
    
            var ddl = matrixAttributeControls[ctr].ctrl;
                
            disableOptions( ddl );
            $(ddl).attr('disabled',false);
            
            for (var ctr1=0; ctr1<matrixGroupProduct.matrixProducts.length; ctr1++) {
            
                var matrixItem = matrixGroupProduct.matrixProducts[ctr1];                    
                    
                if (matrixItem.currentUnitMeasure.freeStock > 0) {
                
                    // fill attribute drop down with valid options
                    var attributes = matrixItem.getAttributes();
                                        
                    if (attributes.length>0 && attributes[0].value == newAttrValue) {
                                                        
                        for(var ctr2=1; ctr2<attributes.length; ctr2++) {
                                
                            var val = attributes[ctr2].value;
                            
                            enableOption( ddl, val );
                            
                        }
                    }
                }
            }            
        }        
    }
}
$add_windowLoad(
 function() {
    // onload 
    setTimeout('var product = ise.Products.ProductController.getProduct( document.getElementsByName("ProductID")[0].value );_filterOutOfStockAttributes( product );',500);
    }
);   
$(document).ready(function()
{
	$.fn.extend(
	{
		optionDisable:function()
		{
			var ths = $(this);
			if(ths.attr('tagName').toLowerCase() == 'option')
			{
				//var optionDisabled = ths.attr('value');
				ths.before($('<optgroup></optgroup>').css({color:'#ccc'}).attr({id:ths.attr('value'),label:ths.text()})).remove();
			}
			return ths;
		},
		optionEnable:function()
		{
			var ths = $(this);			

			if (ths.length>0) {
				//var optionEnabled = ths.attr('id');
				var tag = ths.attr('tagName').toLowerCase();
				if(tag == 'option')
				{
					ths.removeAttr('disabled');
				}
				else if(tag == 'optgroup')
				{
					ths.before($('<option />').attr({value:ths.attr('id')}).text(ths.attr('label'))).remove();
				}
			}
			return ths;
		}
	});
});
