function CustomQty() {
    // fixes issue with AJAX not always working
    setTimeout("DelayCustomQty();", 0);
}

function DelayCustomQty() {

    var cboQuantity = document.getElementById('ctrlOrderBox_cboQuantity');
	var quantity = cboQuantity.value;
	
	if (quantity == '-1') {
        var cboMaterial = document.getElementById('ctrlOrderBox_cboMaterial');
	    var productName = cboMaterial.value;    
	    var txtQuantity = document.getElementById('txtCustomQty');
	    var quantity = txtQuantity.value.replace(/\,/g,''); //removes all commas in the qty
	
	    if (quantity <= '0') {
	        HideCustomQtyPrice();
	        alert("Invalid number of sheets.");
	        return;
	    }
	
	    if (quantity > 0) {
	        if (isInteger(quantity) == false) {
	            alert("Invalid number of sheets.");
	            return;
	        }
	    }
	    else {
	       alert("Invalid number of sheets.");
	       return;
	    }
			   
        jsCustomQty = document.createElement('script');
	    jsCustomQty.type = 'text/javascript';
	    jsCustomQty.src = '/Products/GetCustomQtyJS.aspx?ProductName=' + productName + '&Qty=' + quantity;
			
	    var headCustomQty = document.getElementsByTagName('head')[0];
	    headCustomQty.appendChild(jsCustomQty);
	}
	else
	    return;

}

function HideCustomQtyPrice() {
    var customQtyPricing = document.getElementById('customQtyPricing');
    var custRTA_inStock = document.getElementById('custRTA_inStock');
    var custRTA_inProduction = document.getElementById('custRTA_inProduction');

    custRTA_inProduction.style.display = 'none';
    custRTA_inStock.style.display = 'none';
    customQtyPricing.style.display = 'none';
}

function UpdateCustomQty(avail,price,nextQty,nextPrice) {

    var custRTA_inStock = document.getElementById('custRTA_inStock');
    var custRTA_inProduction = document.getElementById('custRTA_inProduction');

    if (avail == -1) {
        HideCustomQtyPrice();
        alert('The quantity that you entered is not available in this product.');
        return;
    }
    else if (avail == 1) {
        custRTA_inStock.style.display = 'block';
        custRTA_inProduction.style.display = 'none';
    }
    else {
        custRTA_inStock.style.display = 'none'
        custRTA_inProduction.style.display = 'block';
    }    
    
    var lblPrice = document.getElementById('lblPrice');
    var lblNextAmount = document.getElementById('lblNextAmount');
    var customQtyPricing = document.getElementById('customQtyPricing');
    
    customQtyPricing.style.display = 'block'
    lblPrice.innerHTML = "Your price is " + price;
    
    if (nextQty == '0' && nextPrice == '0')
        lblNextAmount.innerHTML = "";
    else
        lblNextAmount.innerHTML = "*You can order " + addCommas(nextQty) + " sheets for " + nextPrice;	
}

function ValidateProductTest() {
   
	if (document.getElementById('ctrlOrderBox_cboMaterial').value == '0')
	{
		alert('Please select a material from the drop down list.');
		return false;
	}
	else if (document.getElementById('ctrlOrderBox_cboQuantity').value == '0')
	{
	    alert('Please select the number of sheets required.');
		return false;
	}
	else if (document.getElementById('ctrlOrderBox_cboQuantity').value == '-2')
	{
		alert('Please call us at 1-888-575-2235 for orders over 20,000 sheets.');
		return false;
	}
	else if (document.getElementById('ctrlOrderBox_cboQuantity').value == '-1')
	{
	    var txtQuantity = document.getElementById('txtCustomQty');
	    var quantity = txtQuantity.value.replace(/\,/g,''); //removes all commas in the qty
	    if (quantity > 0) {
	        if (isInteger(quantity) == false) {
	            alert("Invalid number of sheets.");
	            return false;
	        }
	    }
	    else {
	       alert("Invalid number of sheets.");
	       return false;
	    }
	}
	
	return true;
}

function isInteger(s) {
    return (s.toString().search(/^-?[0-9]+$/) == 0);
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function submitOnKeydown(buttonid, e)
{
	var btn = document.getElementById(buttonid);

	if (typeof btn == 'object')
	{
		if (e.keyCode == 13)
		{
			btn.focus();
			return false;
		}
	}
} 