var jsAvail;

function Availability() {
	var cboMaterial = document.getElementById('ctrlOrderBox_cboMaterial');
	var productName = cboMaterial.value;
	
	var cboQuantity = document.getElementById('ctrlOrderBox_cboQuantity');
	var quantity = cboQuantity.value;

    if (typeof(SetShippingCalcQty) != 'undefined') {
	    SetShippingCalcQty();
	}
	
	var realtime = document.getElementById('realtime');
	var rta = document.getElementById('rta');
	
	if (productName == '0')
		return;
		
	if (quantity <= '0'){
		rta.style.display = 'none';
		realtime.innerHTML = '';
		return;
	}			
	
	jsAvail = document.createElement('script');
	jsAvail.type = 'text/javascript';
	jsAvail.src = '/Products/GetAvailabilityJS.aspx?ProductName=' + productName + '&Qty=' + quantity;
	
	var headAvail = document.getElementsByTagName('head')[0];
	headAvail.appendChild(jsAvail);
}

function AvailabilityTest() {
    var cboMaterial = document.getElementById('ctrlOrderBox_cboMaterial');
	var productName = cboMaterial.value;
	
	var cboQuantity = document.getElementById('ctrlOrderBox_cboQuantity');
	var quantity = cboQuantity.value;
	
	SetShippingCalcQty();
	
	var customQtyBox = document.getElementById('customQtyBox');
	var realtime = document.getElementById('realtime');
	var rta = document.getElementById('rta');
	
	if (productName == '0')
		return;
	
	if (quantity == '-1'){
	
	    customQtyBox.style.display = 'block';
	    	        
	    var txtQuantity = document.getElementById('txtCustomQty');
	    var sheetquantity = txtQuantity.value.replace(/\,/g,''); //removes all commas in the qty
	    
	    if (sheetquantity > 0)
	       CustomQty();
	}
	else
		customQtyBox.style.display = 'none';
	
	if (quantity <= '0'){
		rta.style.display = 'none';
		realtime.innerHTML = '';
		return;
	}
			
	
	jsAvail = document.createElement('script');
	jsAvail.type = 'text/javascript';
	jsAvail.src = '/Products/GetAvailabilityJS.aspx?ProductName=' + productName + '&Qty=' + quantity;
	
	var headAvail = document.getElementsByTagName('head')[0];
	headAvail.appendChild(jsAvail);
}

function UpdateAvailability() {
	
	var realtime = document.getElementById('realtime');
	var rta = document.getElementById('rta');
	var inStock = null;
	
	if(available == true)
		inStock = '<strong>In-stock.</strong> Orders placed before 4:00 PM EST will normally ship the same business day.';
	else
		inStock = '<strong>In production.</strong> This item normally ships 1-3 business days after your order is placed.'; 
	
	realtime.style.display = 'block';

	//Flash
	Fat.fade_element('realtime', 30, 500, '#ffa820', '#e1ecff');
	
	rta.style.display = 'block';
	realtime.innerHTML = inStock;
	
    setTimeout('RemoveAvailabilityJS();', 0);
}

function RemoveAvailabilityJS() {
    var headAvail = jsAvail.parentNode;
    
    if (headAvail) {
	    headAvail.removeChild(jsAvail);
	}
	
	delete jsAvail;
}